Merge svn changes up to r28862
[mplayer.git] / libvo / video_out.c
blob2791ba54b3e8edccf1ae509250b6791f6abf85c9
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_dxr2;
104 extern struct vo_driver video_out_dxr3;
105 extern struct vo_driver video_out_ivtv;
106 extern struct vo_driver video_out_v4l2;
107 extern struct vo_driver video_out_jpeg;
108 extern struct vo_driver video_out_gif89a;
109 extern struct vo_driver video_out_vesa;
110 extern struct vo_driver video_out_directfb;
111 extern struct vo_driver video_out_dfbmga;
112 extern struct vo_driver video_out_xvidix;
113 extern struct vo_driver video_out_winvidix;
114 extern struct vo_driver video_out_cvidix;
115 extern struct vo_driver video_out_tdfx_vid;
116 extern struct vo_driver video_out_xvr100;
117 extern struct vo_driver video_out_tga;
118 extern struct vo_driver video_out_macosx;
119 extern struct vo_driver video_out_quartz;
120 extern struct vo_driver video_out_pnm;
121 extern struct vo_driver video_out_md5sum;
123 const struct vo_driver *video_out_drivers[] =
125 #ifdef CONFIG_XVR100
126 &video_out_xvr100,
127 #endif
128 #ifdef CONFIG_TDFX_VID
129 &video_out_tdfx_vid,
130 #endif
131 #ifdef CONFIG_DIRECTX
132 &video_out_directx,
133 #endif
134 #ifdef CONFIG_DIRECT3D
135 &video_out_direct3d,
136 #endif
137 #ifdef CONFIG_COREVIDEO
138 &video_out_macosx,
139 #endif
140 #ifdef CONFIG_QUARTZ
141 &video_out_quartz,
142 #endif
143 #ifdef CONFIG_XMGA
144 &video_out_xmga,
145 #endif
146 #ifdef CONFIG_MGA
147 &video_out_mga,
148 #endif
149 #ifdef CONFIG_TDFXFB
150 &video_out_tdfxfb,
151 #endif
152 #ifdef CONFIG_S3FB
153 &video_out_s3fb,
154 #endif
155 #ifdef CONFIG_WII
156 &video_out_wii,
157 #endif
158 #ifdef CONFIG_3DFX
159 &video_out_3dfx,
160 #endif
161 #ifdef CONFIG_XV
162 &video_out_xv,
163 #endif
164 #if CONFIG_VDPAU
165 &video_out_vdpau,
166 #endif
167 #ifdef CONFIG_X11
168 &video_out_x11,
169 &video_out_xover,
170 #endif
171 #ifdef CONFIG_GL
172 &video_out_gl,
173 &video_out_gl2,
174 #endif
175 #ifdef CONFIG_DGA
176 &video_out_dga,
177 #endif
178 #ifdef CONFIG_SDL
179 &video_out_sdl,
180 #endif
181 #ifdef CONFIG_GGI
182 &video_out_ggi,
183 #endif
184 #ifdef CONFIG_FBDEV
185 &video_out_fbdev,
186 &video_out_fbdev2,
187 #endif
188 #ifdef CONFIG_SVGALIB
189 &video_out_svga,
190 #endif
191 #ifdef CONFIG_AA
192 &video_out_aa,
193 #endif
194 #ifdef CONFIG_CACA
195 &video_out_caca,
196 #endif
197 #ifdef CONFIG_DXR2
198 &video_out_dxr2,
199 #endif
200 #ifdef CONFIG_DXR3
201 &video_out_dxr3,
202 #endif
203 #ifdef CONFIG_IVTV
204 &video_out_ivtv,
205 #endif
206 #ifdef CONFIG_V4L2_DECODER
207 &video_out_v4l2,
208 #endif
209 #ifdef CONFIG_ZR
210 &video_out_zr,
211 &video_out_zr2,
212 #endif
213 #ifdef CONFIG_BL
214 &video_out_bl,
215 #endif
216 #ifdef CONFIG_VESA
217 &video_out_vesa,
218 #endif
219 #ifdef CONFIG_DIRECTFB
220 &video_out_directfb,
221 #endif
222 #ifdef CONFIG_DFBMGA
223 &video_out_dfbmga,
224 #endif
225 #ifdef CONFIG_VIDIX
226 #ifdef CONFIG_X11
227 &video_out_xvidix,
228 #endif
229 #if defined(__MINGW32__) || defined(__CYGWIN__)
230 &video_out_winvidix,
231 #endif
232 &video_out_cvidix,
233 #endif
234 &video_out_null,
235 // should not be auto-selected
236 #if CONFIG_XVMC
237 &video_out_xvmc,
238 #endif
239 &video_out_mpegpes,
240 #ifdef CONFIG_YUV4MPEG
241 &video_out_yuv4mpeg,
242 #endif
243 #ifdef CONFIG_PNG
244 &video_out_png,
245 #endif
246 #ifdef CONFIG_JPEG
247 &video_out_jpeg,
248 #endif
249 #ifdef CONFIG_GIF
250 &video_out_gif89a,
251 #endif
252 #ifdef CONFIG_TGA
253 &video_out_tga,
254 #endif
255 #ifdef CONFIG_PNM
256 &video_out_pnm,
257 #endif
258 #ifdef CONFIG_MD5SUM
259 &video_out_md5sum,
260 #endif
261 NULL
265 static int vo_preinit(struct vo *vo, const char *arg)
267 return vo->driver->preinit(vo, arg);
270 int vo_control(struct vo *vo, uint32_t request, void *data)
272 return vo->driver->control(vo, request, data);
275 int vo_draw_frame(struct vo *vo, uint8_t *src[])
277 if (!vo->config_ok)
278 return 0;
279 return vo->driver->draw_frame(vo, src);
282 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
284 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
287 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
289 if (!vo->config_ok)
290 return;
291 vo->driver->draw_osd(vo, osd);
294 void vo_flip_page(struct vo *vo)
296 if (!vo->config_ok)
297 return;
298 vo->driver->flip_page(vo);
301 void vo_check_events(struct vo *vo)
303 if (!vo->config_ok)
304 return;
305 vo->driver->check_events(vo);
308 void vo_destroy(struct vo *vo)
310 vo->driver->uninit(vo);
311 talloc_free(vo);
314 void list_video_out(void)
316 int i = 0;
317 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoOutputDrivers);
318 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
319 while (video_out_drivers[i]) {
320 const vo_info_t *info = video_out_drivers[i++]->info;
321 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
323 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
326 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
327 struct mp_fifo *key_fifo,
328 struct input_ctx *input_ctx)
330 char **vo_list = opts->video_driver_list;
331 int i;
332 struct vo *vo = talloc_ptrtype(NULL, vo);
333 struct vo initial_values = {
334 .opts = opts,
335 .x11 = x11,
336 .key_fifo = key_fifo,
337 .input_ctx = input_ctx,
339 // first try the preferred drivers, with their optional subdevice param:
340 if (vo_list && vo_list[0])
341 while (vo_list[0][0]) {
342 char *name = strdup(vo_list[0]);
343 vo_subdevice = strchr(name,':');
344 if (!strcmp(name, "pgm"))
345 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_PGM_HasBeenReplaced);
346 if (!strcmp(name, "md5"))
347 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_MD5_HasBeenReplaced);
348 if (vo_subdevice) {
349 vo_subdevice[0] = 0;
350 ++vo_subdevice;
352 for (i = 0; video_out_drivers[i]; i++) {
353 const struct vo_driver *video_driver = video_out_drivers[i];
354 const vo_info_t *info = video_driver->info;
355 if (!strcmp(info->short_name, name)) {
356 // name matches, try it
357 *vo = initial_values;
358 vo->driver = video_driver;
359 if (!vo_preinit(vo, vo_subdevice)) {
360 free(name);
361 return vo; // success!
365 // continue...
366 free(name);
367 ++vo_list;
368 if (!(vo_list[0]))
369 return NULL; // do NOT fallback to others
371 // now try the rest...
372 vo_subdevice = NULL;
373 for (i = 0; video_out_drivers[i]; i++) {
374 const struct vo_driver *video_driver = video_out_drivers[i];
375 *vo = initial_values;
376 vo->driver = video_driver;
377 if (!vo_preinit(vo, vo_subdevice))
378 return vo; // success!
380 free(vo);
381 return NULL;
384 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
385 uint32_t d_width, uint32_t d_height, uint32_t flags,
386 char *title, uint32_t format)
388 struct MPOpts *opts = vo->opts;
389 panscan_init(vo);
390 aspect_save_orig(vo, width, height);
391 aspect_save_prescale(vo, d_width, d_height);
393 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
394 aspect(vo, &d_width, &d_height, A_NOZOOM);
395 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
396 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
397 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
398 opts->vo_screenwidth, opts->vo_screenheight);
399 vo->dx += xinerama_x;
400 vo->dy += xinerama_y;
401 vo->dwidth = d_width;
402 vo->dheight = d_height;
405 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
406 title, format);
407 vo->config_ok = (ret == 0);
408 vo->config_count += vo->config_ok;
409 return ret;
413 * \brief lookup an integer in a table, table must have 0 as the last key
414 * \param key key to search for
415 * \result translation corresponding to key or "to" value of last mapping
416 * if not found.
418 int lookup_keymap_table(const struct keymap *map, int key) {
419 while (map->from && map->from != key) map++;
420 return map->to;
424 * \brief helper function for the kind of panscan-scaling that needs a source
425 * and destination rectangle like Direct3D and VDPAU
427 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
428 int *src_start, int *src_end, int *dst_start, int *dst_end) {
429 if (scaled_src_size > dst_size) {
430 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
431 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
432 border = (border / 2 + 1) & ~1;
433 *src_start = border;
434 *src_end = src_size - border;
435 *dst_start = 0;
436 *dst_end = dst_size;
437 } else {
438 *src_start = 0;
439 *src_end = src_size;
440 *dst_start = (dst_size - scaled_src_size) / 2;
441 *dst_end = *dst_start + scaled_src_size;
446 * Calculate the appropriate source and destination rectangle to
447 * get a correctly scaled picture, including pan-scan.
448 * Can be extended to take future cropping support into account.
450 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
451 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
452 * may be NULL and only left and top are currently valid.
454 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
455 struct vo_rect *src, struct vo_rect *dst,
456 struct vo_rect *borders, const struct vo_rect *crop)
458 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
459 int scaled_width = 0;
460 int scaled_height = 0;
461 if (!crop) crop = &no_crop;
462 src_width -= crop->left + crop->right;
463 src_height -= crop->top + crop->bottom;
464 if (src_width < 2) src_width = 2;
465 if (src_height < 2) src_height = 2;
466 dst->left = 0; dst->right = vo->dwidth;
467 dst->top = 0; dst->bottom = vo->dheight;
468 src->left = 0; src->right = src_width;
469 src->top = 0; src->bottom = src_height;
470 if (borders) {
471 borders->left = 0; borders->top = 0;
473 if (vo_fs) {
474 aspect(vo, &scaled_width, &scaled_height, A_ZOOM);
475 panscan_calc(vo);
476 scaled_width += vo->panscan_x;
477 scaled_height += vo->panscan_y;
478 if (borders) {
479 borders->left = (vo->dwidth - scaled_width ) / 2;
480 borders->top = (vo->dheight - scaled_height) / 2;
482 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
483 &src->left, &src->right, &dst->left, &dst->right);
484 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
485 &src->top, &src->bottom, &dst->top, &dst->bottom);
487 src->left += crop->left; src->right += crop->left;
488 src->top += crop->top; src->bottom += crop->top;
489 src->width = src->right - src->left;
490 src->height = src->bottom - src->top;
491 dst->width = dst->right - dst->left;
492 dst->height = dst->bottom - dst->top;
495 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
496 /* Borrowed from vo_fbdev.c
497 Monitor ranges related functions*/
499 char *monitor_hfreq_str = NULL;
500 char *monitor_vfreq_str = NULL;
501 char *monitor_dotclock_str = NULL;
503 float range_max(range_t *r)
505 float max = 0;
507 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
508 if (max < r->max) max = r->max;
509 return max;
513 int in_range(range_t *r, float f)
515 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
516 if (f >= r->min && f <= r->max)
517 return 1;
518 return 0;
521 range_t *str2range(char *s)
523 float tmp_min, tmp_max;
524 char *endptr = s; // to start the loop
525 range_t *r = NULL;
526 int i;
528 if (!s)
529 return NULL;
530 for (i = 0; *endptr; i++) {
531 if (*s == ',')
532 goto out_err;
533 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) {
534 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
535 return NULL;
537 tmp_min = strtod(s, &endptr);
538 if (*endptr == 'k' || *endptr == 'K') {
539 tmp_min *= 1000.0;
540 endptr++;
541 } else if (*endptr == 'm' || *endptr == 'M') {
542 tmp_min *= 1000000.0;
543 endptr++;
545 if (*endptr == '-') {
546 tmp_max = strtod(endptr + 1, &endptr);
547 if (*endptr == 'k' || *endptr == 'K') {
548 tmp_max *= 1000.0;
549 endptr++;
550 } else if (*endptr == 'm' || *endptr == 'M') {
551 tmp_max *= 1000000.0;
552 endptr++;
554 if (*endptr != ',' && *endptr)
555 goto out_err;
556 } else if (*endptr == ',' || !*endptr) {
557 tmp_max = tmp_min;
558 } else
559 goto out_err;
560 r[i].min = tmp_min;
561 r[i].max = tmp_max;
562 if (r[i].min < 0 || r[i].max < 0)
563 goto out_err;
564 s = endptr + 1;
566 r[i].min = r[i].max = -1;
567 return r;
568 out_err:
569 if (r)
570 free(r);
571 return NULL;
574 /* Borrowed from vo_fbdev.c END */
575 #endif