Merge branch 'master' of git://repo.or.cz/mplayer into mob
[mplayer/glamo.git] / libvo / video_out.c
blobbc25c61329f48cb5d9f068b392d4867cb3dffc52
1 /*
2 * libvo common functions, variables used by many/all drivers.
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <unistd.h>
26 //#include <sys/mman.h>
28 #include "config.h"
29 #include "options.h"
30 #include "talloc.h"
31 #include "video_out.h"
32 #include "aspect.h"
33 #include "geometry.h"
35 #include "mp_msg.h"
36 #include "help_mp.h"
38 #include "osdep/shmem.h"
39 #ifdef CONFIG_X11
40 #include "x11_common.h"
41 #endif
43 int xinerama_screen = -1;
44 int xinerama_x;
45 int xinerama_y;
47 int vo_nomouse_input = 0;
48 int vo_grabpointer = 1;
49 int vo_doublebuffering = 1;
50 int vo_vsync = 0;
51 int vo_fs = 0;
52 int vo_fsmode = 0;
53 float vo_panscan = 0.0f;
54 int vo_adapter_num=0;
55 int vo_refresh_rate=0;
56 int vo_keepaspect=1;
57 int vo_rootwin=0;
58 int vo_border=1;
59 int64_t WinID = -1;
61 int vo_pts=0; // for hw decoding
62 float vo_fps=0;
64 char *vo_subdevice = NULL;
65 int vo_directrendering=0;
67 int vo_colorkey = 0x0000ff00; // default colorkey is green
68 // (0xff000000 means that colorkey has been disabled)
71 // Externally visible list of all vo drivers
73 extern struct vo_driver video_out_mga;
74 extern struct vo_driver video_out_xmga;
75 extern struct vo_driver video_out_x11;
76 extern struct vo_driver video_out_xover;
77 extern struct vo_driver video_out_xvmc;
78 extern struct vo_driver video_out_vdpau;
79 extern struct vo_driver video_out_xv;
80 extern struct vo_driver video_out_gl;
81 extern struct vo_driver video_out_gl2;
82 extern struct vo_driver video_out_dga;
83 extern struct vo_driver video_out_sdl;
84 extern struct vo_driver video_out_3dfx;
85 extern struct vo_driver video_out_tdfxfb;
86 extern struct vo_driver video_out_s3fb;
87 extern struct vo_driver video_out_wii;
88 extern struct vo_driver video_out_null;
89 extern struct vo_driver video_out_zr;
90 extern struct vo_driver video_out_zr2;
91 extern struct vo_driver video_out_bl;
92 extern struct vo_driver video_out_fbdev;
93 extern struct vo_driver video_out_fbdev2;
94 extern struct vo_driver video_out_svga;
95 extern struct vo_driver video_out_png;
96 extern struct vo_driver video_out_ggi;
97 extern struct vo_driver video_out_aa;
98 extern struct vo_driver video_out_caca;
99 extern struct vo_driver video_out_mpegpes;
100 extern struct vo_driver video_out_yuv4mpeg;
101 extern struct vo_driver video_out_direct3d;
102 extern struct vo_driver video_out_directx;
103 extern struct vo_driver video_out_kva;
104 extern struct vo_driver video_out_dxr2;
105 extern struct vo_driver video_out_dxr3;
106 extern struct vo_driver video_out_ivtv;
107 extern struct vo_driver video_out_v4l2;
108 extern struct vo_driver video_out_jpeg;
109 extern struct vo_driver video_out_gif89a;
110 extern struct vo_driver video_out_vesa;
111 extern struct vo_driver video_out_directfb;
112 extern struct vo_driver video_out_dfbmga;
113 extern struct vo_driver video_out_xvidix;
114 extern struct vo_driver video_out_winvidix;
115 extern struct vo_driver video_out_cvidix;
116 extern struct vo_driver video_out_tdfx_vid;
117 extern struct vo_driver video_out_xvr100;
118 extern struct vo_driver video_out_tga;
119 extern struct vo_driver video_out_corevideo;
120 extern struct vo_driver video_out_quartz;
121 extern struct vo_driver video_out_pnm;
122 extern struct vo_driver video_out_md5sum;
123 extern struct vo_driver video_out_glamo;
125 const struct vo_driver *video_out_drivers[] =
127 #ifdef CONFIG_XVR100
128 &video_out_xvr100,
129 #endif
130 #ifdef CONFIG_TDFX_VID
131 &video_out_tdfx_vid,
132 #endif
133 #ifdef CONFIG_DIRECTX
134 &video_out_directx,
135 #endif
136 #ifdef CONFIG_DIRECT3D
137 &video_out_direct3d,
138 #endif
139 #ifdef CONFIG_KVA
140 &video_out_kva,
141 #endif
142 #ifdef CONFIG_COREVIDEO
143 &video_out_corevideo,
144 #endif
145 #ifdef CONFIG_QUARTZ
146 &video_out_quartz,
147 #endif
148 #ifdef CONFIG_XMGA
149 &video_out_xmga,
150 #endif
151 #ifdef CONFIG_MGA
152 &video_out_mga,
153 #endif
154 #ifdef CONFIG_TDFXFB
155 &video_out_tdfxfb,
156 #endif
157 #ifdef CONFIG_S3FB
158 &video_out_s3fb,
159 #endif
160 #ifdef CONFIG_WII
161 &video_out_wii,
162 #endif
163 #ifdef CONFIG_3DFX
164 &video_out_3dfx,
165 #endif
166 #ifdef CONFIG_XV
167 &video_out_xv,
168 #endif
169 #if CONFIG_VDPAU
170 &video_out_vdpau,
171 #endif
172 #ifdef CONFIG_X11
173 &video_out_x11,
174 &video_out_xover,
175 #endif
176 #ifdef CONFIG_GL
177 &video_out_gl,
178 &video_out_gl2,
179 #endif
180 #ifdef CONFIG_DGA
181 &video_out_dga,
182 #endif
183 #ifdef CONFIG_SDL
184 &video_out_sdl,
185 #endif
186 #ifdef CONFIG_GGI
187 &video_out_ggi,
188 #endif
189 #ifdef CONFIG_FBDEV
190 &video_out_fbdev,
191 &video_out_fbdev2,
192 #endif
193 #ifdef CONFIG_SVGALIB
194 &video_out_svga,
195 #endif
196 #ifdef CONFIG_AA
197 &video_out_aa,
198 #endif
199 #ifdef CONFIG_CACA
200 &video_out_caca,
201 #endif
202 #ifdef CONFIG_DXR2
203 &video_out_dxr2,
204 #endif
205 #ifdef CONFIG_DXR3
206 &video_out_dxr3,
207 #endif
208 #ifdef CONFIG_IVTV
209 &video_out_ivtv,
210 #endif
211 #ifdef CONFIG_V4L2_DECODER
212 &video_out_v4l2,
213 #endif
214 #ifdef CONFIG_ZR
215 &video_out_zr,
216 &video_out_zr2,
217 #endif
218 #ifdef CONFIG_BL
219 &video_out_bl,
220 #endif
221 #ifdef CONFIG_VESA
222 &video_out_vesa,
223 #endif
224 #ifdef CONFIG_DIRECTFB
225 &video_out_directfb,
226 #endif
227 #ifdef CONFIG_DFBMGA
228 &video_out_dfbmga,
229 #endif
230 #ifdef CONFIG_VIDIX
231 #ifdef CONFIG_X11
232 &video_out_xvidix,
233 #endif
234 #if defined(__MINGW32__) || defined(__CYGWIN__)
235 &video_out_winvidix,
236 #endif
237 &video_out_cvidix,
238 #endif
239 &video_out_null,
240 // should not be auto-selected
241 #if CONFIG_XVMC
242 &video_out_xvmc,
243 #endif
244 &video_out_mpegpes,
245 #ifdef CONFIG_YUV4MPEG
246 &video_out_yuv4mpeg,
247 #endif
248 #ifdef CONFIG_PNG
249 &video_out_png,
250 #endif
251 #ifdef CONFIG_JPEG
252 &video_out_jpeg,
253 #endif
254 #ifdef CONFIG_GIF
255 &video_out_gif89a,
256 #endif
257 #ifdef CONFIG_TGA
258 &video_out_tga,
259 #endif
260 #ifdef CONFIG_PNM
261 &video_out_pnm,
262 #endif
263 #ifdef CONFIG_MD5SUM
264 &video_out_md5sum,
265 #endif
266 #ifdef HAVE_GLAMO
267 &video_out_glamo,
268 #endif
269 NULL
273 static int vo_preinit(struct vo *vo, const char *arg)
275 return vo->driver->preinit(vo, arg);
278 int vo_control(struct vo *vo, uint32_t request, void *data)
280 return vo->driver->control(vo, request, data);
283 int vo_draw_frame(struct vo *vo, uint8_t *src[])
285 if (!vo->config_ok)
286 return 0;
287 return vo->driver->draw_frame(vo, src);
290 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
292 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
295 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
297 if (!vo->config_ok)
298 return;
299 vo->driver->draw_osd(vo, osd);
302 void vo_flip_page(struct vo *vo)
304 if (!vo->config_ok)
305 return;
306 vo->driver->flip_page(vo);
309 void vo_check_events(struct vo *vo)
311 if (!vo->config_ok)
312 return;
313 vo->driver->check_events(vo);
316 void vo_destroy(struct vo *vo)
318 vo->driver->uninit(vo);
319 talloc_free(vo);
322 void list_video_out(void)
324 int i = 0;
325 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
326 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
327 while (video_out_drivers[i]) {
328 const vo_info_t *info = video_out_drivers[i++]->info;
329 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
331 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
334 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
335 struct mp_fifo *key_fifo,
336 struct input_ctx *input_ctx)
338 char **vo_list = opts->video_driver_list;
339 int i;
340 struct vo *vo = talloc_ptrtype(NULL, vo);
341 struct vo initial_values = {
342 .opts = opts,
343 .x11 = x11,
344 .key_fifo = key_fifo,
345 .input_ctx = input_ctx,
347 // first try the preferred drivers, with their optional subdevice param:
348 if (vo_list && vo_list[0])
349 while (vo_list[0][0]) {
350 char *name = strdup(vo_list[0]);
351 vo_subdevice = strchr(name,':');
352 if (!strcmp(name, "pgm"))
353 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
354 if (!strcmp(name, "md5"))
355 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
356 if (vo_subdevice) {
357 vo_subdevice[0] = 0;
358 ++vo_subdevice;
360 for (i = 0; video_out_drivers[i]; i++) {
361 const struct vo_driver *video_driver = video_out_drivers[i];
362 const vo_info_t *info = video_driver->info;
363 if (!strcmp(info->short_name, name)) {
364 // name matches, try it
365 *vo = initial_values;
366 vo->driver = video_driver;
367 if (!vo_preinit(vo, vo_subdevice)) {
368 free(name);
369 return vo; // success!
371 talloc_free_children(vo);
374 // continue...
375 free(name);
376 ++vo_list;
377 if (!(vo_list[0]))
378 return NULL; // do NOT fallback to others
380 // now try the rest...
381 vo_subdevice = NULL;
382 for (i = 0; video_out_drivers[i]; i++) {
383 const struct vo_driver *video_driver = video_out_drivers[i];
384 *vo = initial_values;
385 vo->driver = video_driver;
386 if (!vo_preinit(vo, vo_subdevice))
387 return vo; // success!
388 talloc_free_children(vo);
390 free(vo);
391 return NULL;
394 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
395 uint32_t d_width, uint32_t d_height, uint32_t flags,
396 char *title, uint32_t format)
398 struct MPOpts *opts = vo->opts;
399 panscan_init(vo);
400 aspect_save_orig(vo, width, height);
401 aspect_save_prescale(vo, d_width, d_height);
403 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
404 aspect(vo, &d_width, &d_height, A_NOZOOM);
405 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
406 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
407 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
408 opts->vo_screenwidth, opts->vo_screenheight);
409 vo->dx += xinerama_x;
410 vo->dy += xinerama_y;
411 vo->dwidth = d_width;
412 vo->dheight = d_height;
415 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
416 title, format);
417 vo->config_ok = (ret == 0);
418 vo->config_count += vo->config_ok;
419 return ret;
423 * \brief lookup an integer in a table, table must have 0 as the last key
424 * \param key key to search for
425 * \result translation corresponding to key or "to" value of last mapping
426 * if not found.
428 int lookup_keymap_table(const struct keymap *map, int key) {
429 while (map->from && map->from != key) map++;
430 return map->to;
434 * \brief helper function for the kind of panscan-scaling that needs a source
435 * and destination rectangle like Direct3D and VDPAU
437 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
438 int *src_start, int *src_end, int *dst_start, int *dst_end) {
439 if (scaled_src_size > dst_size) {
440 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
441 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
442 border = (border / 2 + 1) & ~1;
443 *src_start = border;
444 *src_end = src_size - border;
445 *dst_start = 0;
446 *dst_end = dst_size;
447 } else {
448 *src_start = 0;
449 *src_end = src_size;
450 *dst_start = (dst_size - scaled_src_size) / 2;
451 *dst_end = *dst_start + scaled_src_size;
456 * Calculate the appropriate source and destination rectangle to
457 * get a correctly scaled picture, including pan-scan.
458 * Can be extended to take future cropping support into account.
460 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
461 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
462 * may be NULL and only left and top are currently valid.
464 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
465 struct vo_rect *src, struct vo_rect *dst,
466 struct vo_rect *borders, const struct vo_rect *crop)
468 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
469 int scaled_width = 0;
470 int scaled_height = 0;
471 if (!crop) crop = &no_crop;
472 src_width -= crop->left + crop->right;
473 src_height -= crop->top + crop->bottom;
474 if (src_width < 2) src_width = 2;
475 if (src_height < 2) src_height = 2;
476 dst->left = 0; dst->right = vo->dwidth;
477 dst->top = 0; dst->bottom = vo->dheight;
478 src->left = 0; src->right = src_width;
479 src->top = 0; src->bottom = src_height;
480 if (borders) {
481 borders->left = 0; borders->top = 0;
483 if (vo_fs) {
484 aspect(vo, &scaled_width, &scaled_height, A_ZOOM);
485 panscan_calc(vo);
486 scaled_width += vo->panscan_x;
487 scaled_height += vo->panscan_y;
488 if (borders) {
489 borders->left = (vo->dwidth - scaled_width ) / 2;
490 borders->top = (vo->dheight - scaled_height) / 2;
492 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
493 &src->left, &src->right, &dst->left, &dst->right);
494 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
495 &src->top, &src->bottom, &dst->top, &dst->bottom);
497 src->left += crop->left; src->right += crop->left;
498 src->top += crop->top; src->bottom += crop->top;
499 src->width = src->right - src->left;
500 src->height = src->bottom - src->top;
501 dst->width = dst->right - dst->left;
502 dst->height = dst->bottom - dst->top;
505 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
506 /* Borrowed from vo_fbdev.c
507 Monitor ranges related functions*/
509 char *monitor_hfreq_str = NULL;
510 char *monitor_vfreq_str = NULL;
511 char *monitor_dotclock_str = NULL;
513 float range_max(range_t *r)
515 float max = 0;
517 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
518 if (max < r->max) max = r->max;
519 return max;
523 int in_range(range_t *r, float f)
525 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
526 if (f >= r->min && f <= r->max)
527 return 1;
528 return 0;
531 range_t *str2range(char *s)
533 float tmp_min, tmp_max;
534 char *endptr = s; // to start the loop
535 range_t *r = NULL;
536 int i;
538 if (!s)
539 return NULL;
540 for (i = 0; *endptr; i++) {
541 if (*s == ',')
542 goto out_err;
543 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) {
544 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
545 return NULL;
547 tmp_min = strtod(s, &endptr);
548 if (*endptr == 'k' || *endptr == 'K') {
549 tmp_min *= 1000.0;
550 endptr++;
551 } else if (*endptr == 'm' || *endptr == 'M') {
552 tmp_min *= 1000000.0;
553 endptr++;
555 if (*endptr == '-') {
556 tmp_max = strtod(endptr + 1, &endptr);
557 if (*endptr == 'k' || *endptr == 'K') {
558 tmp_max *= 1000.0;
559 endptr++;
560 } else if (*endptr == 'm' || *endptr == 'M') {
561 tmp_max *= 1000000.0;
562 endptr++;
564 if (*endptr != ',' && *endptr)
565 goto out_err;
566 } else if (*endptr == ',' || !*endptr) {
567 tmp_max = tmp_min;
568 } else
569 goto out_err;
570 r[i].min = tmp_min;
571 r[i].max = tmp_max;
572 if (r[i].min < 0 || r[i].max < 0)
573 goto out_err;
574 s = endptr + 1;
576 r[i].min = r[i].max = -1;
577 return r;
578 out_err:
579 if (r)
580 free(r);
581 return NULL;
584 /* Borrowed from vo_fbdev.c END */
585 #endif