Merge remote branch 'mplayer/master'
[mplayer/glamo.git] / libvo / video_out.c
blobc980ce8a5039c1f6734b6f2bf3fecb3c51d00d4c
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>
24 #include <assert.h>
25 #include <stdbool.h>
27 #include <unistd.h>
28 //#include <sys/mman.h>
30 #include "config.h"
31 #include "options.h"
32 #include "talloc.h"
33 #include "video_out.h"
34 #include "aspect.h"
35 #include "geometry.h"
36 #include "old_vo_wrapper.h"
38 #include "mp_msg.h"
40 #include "osdep/shmem.h"
41 #ifdef CONFIG_X11
42 #include "x11_common.h"
43 #endif
45 int xinerama_screen = -1;
46 int xinerama_x;
47 int xinerama_y;
49 int vo_nomouse_input = 0;
50 int vo_grabpointer = 1;
51 int vo_doublebuffering = 1;
52 int vo_vsync = 0;
53 int vo_fs = 0;
54 int vo_fsmode = 0;
55 float vo_panscan = 0.0f;
56 int vo_adapter_num=0;
57 int vo_refresh_rate=0;
58 int vo_keepaspect=1;
59 int vo_rootwin=0;
60 int vo_border=1;
61 int64_t WinID = -1;
63 int vo_pts=0; // for hw decoding
64 float vo_fps=0;
66 char *vo_subdevice = NULL;
67 int vo_directrendering=0;
69 int vo_colorkey = 0x0000ff00; // default colorkey is green
70 // (0xff000000 means that colorkey has been disabled)
72 // name to be used instead of the vo's default
73 char *vo_winname;
74 // title to be applied to movie window
75 char *vo_wintitle;
78 // Externally visible list of all vo drivers
80 extern struct vo_driver video_out_mga;
81 extern struct vo_driver video_out_xmga;
82 extern struct vo_driver video_out_x11;
83 extern struct vo_driver video_out_xover;
84 extern struct vo_driver video_out_xvmc;
85 extern struct vo_driver video_out_vdpau;
86 extern struct vo_driver video_out_xv;
87 extern struct vo_driver video_out_gl_nosw;
88 extern struct vo_driver video_out_gl;
89 extern struct vo_driver video_out_gl2;
90 extern struct vo_driver video_out_matrixview;
91 extern struct vo_driver video_out_dga;
92 extern struct vo_driver video_out_sdl;
93 extern struct vo_driver video_out_3dfx;
94 extern struct vo_driver video_out_tdfxfb;
95 extern struct vo_driver video_out_s3fb;
96 extern struct vo_driver video_out_wii;
97 extern struct vo_driver video_out_null;
98 extern struct vo_driver video_out_zr;
99 extern struct vo_driver video_out_zr2;
100 extern struct vo_driver video_out_bl;
101 extern struct vo_driver video_out_fbdev;
102 extern struct vo_driver video_out_fbdev2;
103 extern struct vo_driver video_out_svga;
104 extern struct vo_driver video_out_png;
105 extern struct vo_driver video_out_ggi;
106 extern struct vo_driver video_out_aa;
107 extern struct vo_driver video_out_caca;
108 extern struct vo_driver video_out_mpegpes;
109 extern struct vo_driver video_out_yuv4mpeg;
110 extern struct vo_driver video_out_direct3d;
111 extern struct vo_driver video_out_directx;
112 extern struct vo_driver video_out_kva;
113 extern struct vo_driver video_out_dxr2;
114 extern struct vo_driver video_out_dxr3;
115 extern struct vo_driver video_out_ivtv;
116 extern struct vo_driver video_out_v4l2;
117 extern struct vo_driver video_out_jpeg;
118 extern struct vo_driver video_out_gif89a;
119 extern struct vo_driver video_out_vesa;
120 extern struct vo_driver video_out_directfb;
121 extern struct vo_driver video_out_dfbmga;
122 extern struct vo_driver video_out_xvidix;
123 extern struct vo_driver video_out_winvidix;
124 extern struct vo_driver video_out_cvidix;
125 extern struct vo_driver video_out_tdfx_vid;
126 extern struct vo_driver video_out_xvr100;
127 extern struct vo_driver video_out_tga;
128 extern struct vo_driver video_out_corevideo;
129 extern struct vo_driver video_out_quartz;
130 extern struct vo_driver video_out_pnm;
131 extern struct vo_driver video_out_md5sum;
132 extern struct vo_driver video_out_glamo;
134 const struct vo_driver *video_out_drivers[] =
136 #ifdef CONFIG_XVR100
137 &video_out_xvr100,
138 #endif
139 #ifdef CONFIG_TDFX_VID
140 &video_out_tdfx_vid,
141 #endif
142 #ifdef CONFIG_DIRECTX
143 &video_out_directx,
144 #endif
145 #ifdef CONFIG_DIRECT3D
146 &video_out_direct3d,
147 #endif
148 #ifdef CONFIG_KVA
149 &video_out_kva,
150 #endif
151 #ifdef CONFIG_COREVIDEO
152 &video_out_corevideo,
153 #endif
154 #ifdef CONFIG_QUARTZ
155 &video_out_quartz,
156 #endif
157 #ifdef CONFIG_XMGA
158 &video_out_xmga,
159 #endif
160 #ifdef CONFIG_MGA
161 &video_out_mga,
162 #endif
163 #ifdef CONFIG_TDFXFB
164 &video_out_tdfxfb,
165 #endif
166 #ifdef CONFIG_S3FB
167 &video_out_s3fb,
168 #endif
169 #ifdef CONFIG_WII
170 &video_out_wii,
171 #endif
172 #ifdef CONFIG_3DFX
173 &video_out_3dfx,
174 #endif
175 #if CONFIG_VDPAU
176 &video_out_vdpau,
177 #endif
178 #ifdef CONFIG_XV
179 &video_out_xv,
180 #endif
181 #ifdef CONFIG_X11
182 #ifdef CONFIG_GL
183 &video_out_gl_nosw,
184 #endif
185 &video_out_x11,
186 &video_out_xover,
187 #endif
188 #ifdef CONFIG_GL
189 &video_out_gl,
190 &video_out_gl2,
191 #endif
192 #ifdef CONFIG_DGA
193 &video_out_dga,
194 #endif
195 #ifdef CONFIG_SDL
196 &video_out_sdl,
197 #endif
198 #ifdef CONFIG_GGI
199 &video_out_ggi,
200 #endif
201 #ifdef CONFIG_FBDEV
202 &video_out_fbdev,
203 &video_out_fbdev2,
204 #endif
205 #ifdef CONFIG_SVGALIB
206 &video_out_svga,
207 #endif
208 #ifdef CONFIG_MATRIXVIEW
209 &video_out_matrixview,
210 #endif
211 #ifdef CONFIG_AA
212 &video_out_aa,
213 #endif
214 #ifdef CONFIG_CACA
215 &video_out_caca,
216 #endif
217 #ifdef CONFIG_DXR2
218 &video_out_dxr2,
219 #endif
220 #ifdef CONFIG_DXR3
221 &video_out_dxr3,
222 #endif
223 #ifdef CONFIG_IVTV
224 &video_out_ivtv,
225 #endif
226 #ifdef CONFIG_V4L2_DECODER
227 &video_out_v4l2,
228 #endif
229 #ifdef CONFIG_ZR
230 &video_out_zr,
231 &video_out_zr2,
232 #endif
233 #ifdef CONFIG_BL
234 &video_out_bl,
235 #endif
236 #ifdef CONFIG_VESA
237 &video_out_vesa,
238 #endif
239 #ifdef CONFIG_DFBMGA
240 &video_out_dfbmga,
241 #endif
242 #ifdef CONFIG_VIDIX
243 #ifdef CONFIG_X11
244 &video_out_xvidix,
245 #endif
246 #if defined(__MINGW32__) || defined(__CYGWIN__)
247 &video_out_winvidix,
248 #endif
249 &video_out_cvidix,
250 #endif
251 &video_out_null,
252 // should not be auto-selected
253 #ifdef CONFIG_DIRECTFB
254 // vo directfb can call exit() if initialization fails
255 &video_out_directfb,
256 #endif
257 #if CONFIG_XVMC
258 &video_out_xvmc,
259 #endif
260 &video_out_mpegpes,
261 #ifdef CONFIG_YUV4MPEG
262 &video_out_yuv4mpeg,
263 #endif
264 #ifdef CONFIG_LIBAVCODEC
265 &video_out_png,
266 #endif
267 #ifdef CONFIG_JPEG
268 &video_out_jpeg,
269 #endif
270 #ifdef CONFIG_GIF
271 &video_out_gif89a,
272 #endif
273 #ifdef CONFIG_TGA
274 &video_out_tga,
275 #endif
276 #ifdef CONFIG_PNM
277 &video_out_pnm,
278 #endif
279 #ifdef CONFIG_MD5SUM
280 &video_out_md5sum,
281 #endif
282 #ifdef CONFIG_GLAMO
283 &video_out_glamo,
284 #endif
285 NULL
289 static int vo_preinit(struct vo *vo, const char *arg)
291 return vo->driver->preinit(vo, arg);
294 int vo_control(struct vo *vo, uint32_t request, void *data)
296 return vo->driver->control(vo, request, data);
299 // Return -1 if driver appears not to support a draw_image interface,
300 // 0 otherwise (whether the driver actually drew something or not).
301 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
303 if (!vo->config_ok)
304 return 0;
305 if (vo->driver->buffer_frames) {
306 vo->driver->draw_image(vo, mpi, pts);
307 return 0;
309 vo->frame_loaded = true;
310 vo->next_pts = pts;
311 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
312 return -1;
313 return 0;
316 int vo_get_buffered_frame(struct vo *vo, bool eof)
318 if (!vo->config_ok)
319 return -1;
320 if (vo->frame_loaded)
321 return 0;
322 if (!vo->driver->buffer_frames)
323 return -1;
324 vo->driver->get_buffered_frame(vo, eof);
325 return vo->frame_loaded ? 0 : -1;
328 int vo_draw_frame(struct vo *vo, uint8_t *src[])
330 assert(!vo->driver->is_new);
331 if (!vo->config_ok)
332 return 0;
333 return old_vo_draw_frame(vo, src);
336 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
338 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
341 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
343 if (!vo->config_ok)
344 return;
345 vo->driver->draw_osd(vo, osd);
348 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
350 if (!vo->config_ok)
351 return;
352 vo->frame_loaded = false;
353 vo->next_pts = MP_NOPTS_VALUE;
354 if (vo->driver->flip_page_timed)
355 vo->driver->flip_page_timed(vo, pts_us, duration);
356 else
357 vo->driver->flip_page(vo);
360 void vo_check_events(struct vo *vo)
362 if (!vo->config_ok)
363 return;
364 vo->driver->check_events(vo);
367 void vo_seek_reset(struct vo *vo)
369 vo_control(vo, VOCTRL_RESET, NULL);
370 vo->frame_loaded = false;
373 void vo_destroy(struct vo *vo)
375 vo->driver->uninit(vo);
376 talloc_free(vo);
379 void list_video_out(void)
381 int i = 0;
382 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
383 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
384 while (video_out_drivers[i]) {
385 const vo_info_t *info = video_out_drivers[i++]->info;
386 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
388 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
391 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
392 struct mp_fifo *key_fifo,
393 struct input_ctx *input_ctx)
395 char **vo_list = opts->video_driver_list;
396 int i;
397 struct vo *vo = talloc_ptrtype(NULL, vo);
398 struct vo initial_values = {
399 .opts = opts,
400 .x11 = x11,
401 .key_fifo = key_fifo,
402 .input_ctx = input_ctx,
404 // first try the preferred drivers, with their optional subdevice param:
405 if (vo_list && vo_list[0])
406 while (vo_list[0][0]) {
407 char *name = strdup(vo_list[0]);
408 vo_subdevice = strchr(name,':');
409 if (!strcmp(name, "pgm"))
410 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
411 if (!strcmp(name, "md5"))
412 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
413 if (vo_subdevice) {
414 vo_subdevice[0] = 0;
415 ++vo_subdevice;
417 for (i = 0; video_out_drivers[i]; i++) {
418 const struct vo_driver *video_driver = video_out_drivers[i];
419 const vo_info_t *info = video_driver->info;
420 if (!strcmp(info->short_name, name)) {
421 // name matches, try it
422 *vo = initial_values;
423 vo->driver = video_driver;
424 if (!vo_preinit(vo, vo_subdevice)) {
425 free(name);
426 return vo; // success!
428 talloc_free_children(vo);
431 // continue...
432 free(name);
433 ++vo_list;
434 if (!(vo_list[0]))
435 return NULL; // do NOT fallback to others
437 // now try the rest...
438 vo_subdevice = NULL;
439 for (i = 0; video_out_drivers[i]; i++) {
440 const struct vo_driver *video_driver = video_out_drivers[i];
441 *vo = initial_values;
442 vo->driver = video_driver;
443 if (!vo_preinit(vo, vo_subdevice))
444 return vo; // success!
445 talloc_free_children(vo);
447 free(vo);
448 return NULL;
451 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
452 uint32_t d_width, uint32_t d_height, uint32_t flags,
453 char *title, uint32_t format)
455 struct MPOpts *opts = vo->opts;
456 panscan_init(vo);
457 aspect_save_orig(vo, width, height);
458 aspect_save_prescale(vo, d_width, d_height);
460 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
461 aspect(vo, &d_width, &d_height, A_NOZOOM);
462 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
463 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
464 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
465 opts->vo_screenwidth, opts->vo_screenheight);
466 vo->dx += xinerama_x;
467 vo->dy += xinerama_y;
468 vo->dwidth = d_width;
469 vo->dheight = d_height;
472 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
473 title, format);
474 vo->config_ok = (ret == 0);
475 vo->config_count += vo->config_ok;
476 return ret;
480 * \brief lookup an integer in a table, table must have 0 as the last key
481 * \param key key to search for
482 * \result translation corresponding to key or "to" value of last mapping
483 * if not found.
485 int lookup_keymap_table(const struct keymap *map, int key) {
486 while (map->from && map->from != key) map++;
487 return map->to;
491 * \brief helper function for the kind of panscan-scaling that needs a source
492 * and destination rectangle like Direct3D and VDPAU
494 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
495 int *src_start, int *src_end, int *dst_start, int *dst_end) {
496 if (scaled_src_size > dst_size) {
497 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
498 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
499 border = (border / 2 + 1) & ~1;
500 *src_start = border;
501 *src_end = src_size - border;
502 *dst_start = 0;
503 *dst_end = dst_size;
504 } else {
505 *src_start = 0;
506 *src_end = src_size;
507 *dst_start = (dst_size - scaled_src_size) / 2;
508 *dst_end = *dst_start + scaled_src_size;
513 * Calculate the appropriate source and destination rectangle to
514 * get a correctly scaled picture, including pan-scan.
515 * Can be extended to take future cropping support into account.
517 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
518 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
519 * may be NULL and only left and top are currently valid.
521 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
522 struct vo_rect *src, struct vo_rect *dst,
523 struct vo_rect *borders, const struct vo_rect *crop)
525 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
526 int scaled_width = 0;
527 int scaled_height = 0;
528 if (!crop) crop = &no_crop;
529 src_width -= crop->left + crop->right;
530 src_height -= crop->top + crop->bottom;
531 if (src_width < 2) src_width = 2;
532 if (src_height < 2) src_height = 2;
533 dst->left = 0; dst->right = vo->dwidth;
534 dst->top = 0; dst->bottom = vo->dheight;
535 src->left = 0; src->right = src_width;
536 src->top = 0; src->bottom = src_height;
537 if (borders) {
538 borders->left = 0; borders->top = 0;
540 if (aspect_scaling()) {
541 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
542 panscan_calc_windowed(vo);
543 scaled_width += vo->panscan_x;
544 scaled_height += vo->panscan_y;
545 if (borders) {
546 borders->left = (vo->dwidth - scaled_width ) / 2;
547 borders->top = (vo->dheight - scaled_height) / 2;
549 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
550 &src->left, &src->right, &dst->left, &dst->right);
551 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
552 &src->top, &src->bottom, &dst->top, &dst->bottom);
554 src->left += crop->left; src->right += crop->left;
555 src->top += crop->top; src->bottom += crop->top;
556 src->width = src->right - src->left;
557 src->height = src->bottom - src->top;
558 dst->width = dst->right - dst->left;
559 dst->height = dst->bottom - dst->top;
562 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
563 /* Borrowed from vo_fbdev.c
564 Monitor ranges related functions*/
566 char *monitor_hfreq_str = NULL;
567 char *monitor_vfreq_str = NULL;
568 char *monitor_dotclock_str = NULL;
570 float range_max(range_t *r)
572 float max = 0;
574 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
575 if (max < r->max) max = r->max;
576 return max;
580 int in_range(range_t *r, float f)
582 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
583 if (f >= r->min && f <= r->max)
584 return 1;
585 return 0;
588 range_t *str2range(char *s)
590 float tmp_min, tmp_max;
591 char *endptr = s; // to start the loop
592 range_t *r = NULL;
593 int i;
595 if (!s)
596 return NULL;
597 for (i = 0; *endptr; i++) {
598 if (*s == ',')
599 goto out_err;
600 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
601 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
602 return NULL;
604 tmp_min = strtod(s, &endptr);
605 if (*endptr == 'k' || *endptr == 'K') {
606 tmp_min *= 1000.0;
607 endptr++;
608 } else if (*endptr == 'm' || *endptr == 'M') {
609 tmp_min *= 1000000.0;
610 endptr++;
612 if (*endptr == '-') {
613 tmp_max = strtod(endptr + 1, &endptr);
614 if (*endptr == 'k' || *endptr == 'K') {
615 tmp_max *= 1000.0;
616 endptr++;
617 } else if (*endptr == 'm' || *endptr == 'M') {
618 tmp_max *= 1000000.0;
619 endptr++;
621 if (*endptr != ',' && *endptr)
622 goto out_err;
623 } else if (*endptr == ',' || !*endptr) {
624 tmp_max = tmp_min;
625 } else
626 goto out_err;
627 r[i].min = tmp_min;
628 r[i].max = tmp_max;
629 if (r[i].min < 0 || r[i].max < 0)
630 goto out_err;
631 s = endptr + 1;
633 r[i].min = r[i].max = -1;
634 return r;
635 out_err:
636 if (r)
637 free(r);
638 return NULL;
641 /* Borrowed from vo_fbdev.c END */
642 #endif