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