cfg-mplayer.h: add casts to silence warnings
[mplayer/glamo.git] / libvo / video_out.c
blob92dfcf47b6d521dc13038b99cfb9019dcbc51e02
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;
133 const struct vo_driver *video_out_drivers[] =
135 #ifdef CONFIG_XVR100
136 &video_out_xvr100,
137 #endif
138 #ifdef CONFIG_TDFX_VID
139 &video_out_tdfx_vid,
140 #endif
141 #ifdef CONFIG_DIRECTX
142 &video_out_directx,
143 #endif
144 #ifdef CONFIG_DIRECT3D
145 &video_out_direct3d,
146 #endif
147 #ifdef CONFIG_KVA
148 &video_out_kva,
149 #endif
150 #ifdef CONFIG_COREVIDEO
151 &video_out_corevideo,
152 #endif
153 #ifdef CONFIG_QUARTZ
154 &video_out_quartz,
155 #endif
156 #ifdef CONFIG_XMGA
157 &video_out_xmga,
158 #endif
159 #ifdef CONFIG_MGA
160 &video_out_mga,
161 #endif
162 #ifdef CONFIG_TDFXFB
163 &video_out_tdfxfb,
164 #endif
165 #ifdef CONFIG_S3FB
166 &video_out_s3fb,
167 #endif
168 #ifdef CONFIG_WII
169 &video_out_wii,
170 #endif
171 #ifdef CONFIG_3DFX
172 &video_out_3dfx,
173 #endif
174 #if CONFIG_VDPAU
175 &video_out_vdpau,
176 #endif
177 #ifdef CONFIG_XV
178 &video_out_xv,
179 #endif
180 #ifdef CONFIG_X11
181 #ifdef CONFIG_GL
182 &video_out_gl_nosw,
183 #endif
184 &video_out_x11,
185 &video_out_xover,
186 #endif
187 #ifdef CONFIG_GL
188 &video_out_gl,
189 &video_out_gl2,
190 #endif
191 #ifdef CONFIG_DGA
192 &video_out_dga,
193 #endif
194 #ifdef CONFIG_SDL
195 &video_out_sdl,
196 #endif
197 #ifdef CONFIG_GGI
198 &video_out_ggi,
199 #endif
200 #ifdef CONFIG_FBDEV
201 &video_out_fbdev,
202 &video_out_fbdev2,
203 #endif
204 #ifdef CONFIG_SVGALIB
205 &video_out_svga,
206 #endif
207 #ifdef CONFIG_MATRIXVIEW
208 &video_out_matrixview,
209 #endif
210 #ifdef CONFIG_AA
211 &video_out_aa,
212 #endif
213 #ifdef CONFIG_CACA
214 &video_out_caca,
215 #endif
216 #ifdef CONFIG_DXR2
217 &video_out_dxr2,
218 #endif
219 #ifdef CONFIG_DXR3
220 &video_out_dxr3,
221 #endif
222 #ifdef CONFIG_IVTV
223 &video_out_ivtv,
224 #endif
225 #ifdef CONFIG_V4L2_DECODER
226 &video_out_v4l2,
227 #endif
228 #ifdef CONFIG_ZR
229 &video_out_zr,
230 &video_out_zr2,
231 #endif
232 #ifdef CONFIG_BL
233 &video_out_bl,
234 #endif
235 #ifdef CONFIG_VESA
236 &video_out_vesa,
237 #endif
238 #ifdef CONFIG_DFBMGA
239 &video_out_dfbmga,
240 #endif
241 #ifdef CONFIG_VIDIX
242 #ifdef CONFIG_X11
243 &video_out_xvidix,
244 #endif
245 #if defined(__MINGW32__) || defined(__CYGWIN__)
246 &video_out_winvidix,
247 #endif
248 &video_out_cvidix,
249 #endif
250 &video_out_null,
251 // should not be auto-selected
252 #ifdef CONFIG_DIRECTFB
253 // vo directfb can call exit() if initialization fails
254 &video_out_directfb,
255 #endif
256 #if CONFIG_XVMC
257 &video_out_xvmc,
258 #endif
259 &video_out_mpegpes,
260 #ifdef CONFIG_YUV4MPEG
261 &video_out_yuv4mpeg,
262 #endif
263 #ifdef CONFIG_LIBAVCODEC
264 &video_out_png,
265 #endif
266 #ifdef CONFIG_JPEG
267 &video_out_jpeg,
268 #endif
269 #ifdef CONFIG_GIF
270 &video_out_gif89a,
271 #endif
272 #ifdef CONFIG_TGA
273 &video_out_tga,
274 #endif
275 #ifdef CONFIG_PNM
276 &video_out_pnm,
277 #endif
278 #ifdef CONFIG_MD5SUM
279 &video_out_md5sum,
280 #endif
281 NULL
285 static int vo_preinit(struct vo *vo, const char *arg)
287 return vo->driver->preinit(vo, arg);
290 int vo_control(struct vo *vo, uint32_t request, void *data)
292 return vo->driver->control(vo, request, data);
295 // Return -1 if driver appears not to support a draw_image interface,
296 // 0 otherwise (whether the driver actually drew something or not).
297 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
299 if (!vo->config_ok)
300 return 0;
301 if (vo->driver->buffer_frames) {
302 vo->driver->draw_image(vo, mpi, pts);
303 return 0;
305 vo->frame_loaded = true;
306 vo->next_pts = pts;
307 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
308 return -1;
309 return 0;
312 int vo_get_buffered_frame(struct vo *vo, bool eof)
314 if (!vo->config_ok)
315 return -1;
316 if (vo->frame_loaded)
317 return 0;
318 if (!vo->driver->buffer_frames)
319 return -1;
320 vo->driver->get_buffered_frame(vo, eof);
321 return vo->frame_loaded ? 0 : -1;
324 int vo_draw_frame(struct vo *vo, uint8_t *src[])
326 assert(!vo->driver->is_new);
327 if (!vo->config_ok)
328 return 0;
329 return old_vo_draw_frame(vo, src);
332 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
334 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
337 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
339 if (!vo->config_ok)
340 return;
341 vo->driver->draw_osd(vo, osd);
344 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
346 if (!vo->config_ok)
347 return;
348 vo->frame_loaded = false;
349 vo->next_pts = MP_NOPTS_VALUE;
350 if (vo->driver->flip_page_timed)
351 vo->driver->flip_page_timed(vo, pts_us, duration);
352 else
353 vo->driver->flip_page(vo);
356 void vo_check_events(struct vo *vo)
358 if (!vo->config_ok)
359 return;
360 vo->driver->check_events(vo);
363 void vo_seek_reset(struct vo *vo)
365 vo_control(vo, VOCTRL_RESET, NULL);
366 vo->frame_loaded = false;
369 void vo_destroy(struct vo *vo)
371 vo->driver->uninit(vo);
372 talloc_free(vo);
375 void list_video_out(void)
377 int i = 0;
378 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
379 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
380 while (video_out_drivers[i]) {
381 const vo_info_t *info = video_out_drivers[i++]->info;
382 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
384 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
387 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
388 struct mp_fifo *key_fifo,
389 struct input_ctx *input_ctx)
391 char **vo_list = opts->video_driver_list;
392 int i;
393 struct vo *vo = talloc_ptrtype(NULL, vo);
394 struct vo initial_values = {
395 .opts = opts,
396 .x11 = x11,
397 .key_fifo = key_fifo,
398 .input_ctx = input_ctx,
400 // first try the preferred drivers, with their optional subdevice param:
401 if (vo_list && vo_list[0])
402 while (vo_list[0][0]) {
403 char *name = strdup(vo_list[0]);
404 vo_subdevice = strchr(name,':');
405 if (!strcmp(name, "pgm"))
406 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
407 if (!strcmp(name, "md5"))
408 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
409 if (vo_subdevice) {
410 vo_subdevice[0] = 0;
411 ++vo_subdevice;
413 for (i = 0; video_out_drivers[i]; i++) {
414 const struct vo_driver *video_driver = video_out_drivers[i];
415 const vo_info_t *info = video_driver->info;
416 if (!strcmp(info->short_name, name)) {
417 // name matches, try it
418 *vo = initial_values;
419 vo->driver = video_driver;
420 if (!vo_preinit(vo, vo_subdevice)) {
421 free(name);
422 return vo; // success!
424 talloc_free_children(vo);
427 // continue...
428 free(name);
429 ++vo_list;
430 if (!(vo_list[0]))
431 return NULL; // do NOT fallback to others
433 // now try the rest...
434 vo_subdevice = NULL;
435 for (i = 0; video_out_drivers[i]; i++) {
436 const struct vo_driver *video_driver = video_out_drivers[i];
437 *vo = initial_values;
438 vo->driver = video_driver;
439 if (!vo_preinit(vo, vo_subdevice))
440 return vo; // success!
441 talloc_free_children(vo);
443 free(vo);
444 return NULL;
447 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
448 uint32_t d_width, uint32_t d_height, uint32_t flags,
449 char *title, uint32_t format)
451 struct MPOpts *opts = vo->opts;
452 panscan_init(vo);
453 aspect_save_orig(vo, width, height);
454 aspect_save_prescale(vo, d_width, d_height);
456 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
457 aspect(vo, &d_width, &d_height, A_NOZOOM);
458 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
459 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
460 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
461 opts->vo_screenwidth, opts->vo_screenheight);
462 vo->dx += xinerama_x;
463 vo->dy += xinerama_y;
464 vo->dwidth = d_width;
465 vo->dheight = d_height;
468 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
469 title, format);
470 vo->config_ok = (ret == 0);
471 vo->config_count += vo->config_ok;
472 return ret;
476 * \brief lookup an integer in a table, table must have 0 as the last key
477 * \param key key to search for
478 * \result translation corresponding to key or "to" value of last mapping
479 * if not found.
481 int lookup_keymap_table(const struct keymap *map, int key) {
482 while (map->from && map->from != key) map++;
483 return map->to;
487 * \brief helper function for the kind of panscan-scaling that needs a source
488 * and destination rectangle like Direct3D and VDPAU
490 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
491 int *src_start, int *src_end, int *dst_start, int *dst_end) {
492 if (scaled_src_size > dst_size) {
493 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
494 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
495 border = (border / 2 + 1) & ~1;
496 *src_start = border;
497 *src_end = src_size - border;
498 *dst_start = 0;
499 *dst_end = dst_size;
500 } else {
501 *src_start = 0;
502 *src_end = src_size;
503 *dst_start = (dst_size - scaled_src_size) / 2;
504 *dst_end = *dst_start + scaled_src_size;
509 * Calculate the appropriate source and destination rectangle to
510 * get a correctly scaled picture, including pan-scan.
511 * Can be extended to take future cropping support into account.
513 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
514 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
515 * may be NULL and only left and top are currently valid.
517 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
518 struct vo_rect *src, struct vo_rect *dst,
519 struct vo_rect *borders, const struct vo_rect *crop)
521 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
522 int scaled_width = 0;
523 int scaled_height = 0;
524 if (!crop) crop = &no_crop;
525 src_width -= crop->left + crop->right;
526 src_height -= crop->top + crop->bottom;
527 if (src_width < 2) src_width = 2;
528 if (src_height < 2) src_height = 2;
529 dst->left = 0; dst->right = vo->dwidth;
530 dst->top = 0; dst->bottom = vo->dheight;
531 src->left = 0; src->right = src_width;
532 src->top = 0; src->bottom = src_height;
533 if (borders) {
534 borders->left = 0; borders->top = 0;
536 if (aspect_scaling()) {
537 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
538 panscan_calc_windowed(vo);
539 scaled_width += vo->panscan_x;
540 scaled_height += vo->panscan_y;
541 if (borders) {
542 borders->left = (vo->dwidth - scaled_width ) / 2;
543 borders->top = (vo->dheight - scaled_height) / 2;
545 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
546 &src->left, &src->right, &dst->left, &dst->right);
547 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
548 &src->top, &src->bottom, &dst->top, &dst->bottom);
550 src->left += crop->left; src->right += crop->left;
551 src->top += crop->top; src->bottom += crop->top;
552 src->width = src->right - src->left;
553 src->height = src->bottom - src->top;
554 dst->width = dst->right - dst->left;
555 dst->height = dst->bottom - dst->top;
558 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
559 /* Borrowed from vo_fbdev.c
560 Monitor ranges related functions*/
562 char *monitor_hfreq_str = NULL;
563 char *monitor_vfreq_str = NULL;
564 char *monitor_dotclock_str = NULL;
566 float range_max(range_t *r)
568 float max = 0;
570 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
571 if (max < r->max) max = r->max;
572 return max;
576 int in_range(range_t *r, float f)
578 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
579 if (f >= r->min && f <= r->max)
580 return 1;
581 return 0;
584 range_t *str2range(char *s)
586 float tmp_min, tmp_max;
587 char *endptr = s; // to start the loop
588 range_t *r = NULL;
589 int i;
591 if (!s)
592 return NULL;
593 for (i = 0; *endptr; i++) {
594 if (*s == ',')
595 goto out_err;
596 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
597 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
598 return NULL;
600 tmp_min = strtod(s, &endptr);
601 if (*endptr == 'k' || *endptr == 'K') {
602 tmp_min *= 1000.0;
603 endptr++;
604 } else if (*endptr == 'm' || *endptr == 'M') {
605 tmp_min *= 1000000.0;
606 endptr++;
608 if (*endptr == '-') {
609 tmp_max = strtod(endptr + 1, &endptr);
610 if (*endptr == 'k' || *endptr == 'K') {
611 tmp_max *= 1000.0;
612 endptr++;
613 } else if (*endptr == 'm' || *endptr == 'M') {
614 tmp_max *= 1000000.0;
615 endptr++;
617 if (*endptr != ',' && *endptr)
618 goto out_err;
619 } else if (*endptr == ',' || !*endptr) {
620 tmp_max = tmp_min;
621 } else
622 goto out_err;
623 r[i].min = tmp_min;
624 r[i].max = tmp_max;
625 if (r[i].min < 0 || r[i].max < 0)
626 goto out_err;
627 s = endptr + 1;
629 r[i].min = r[i].max = -1;
630 return r;
631 out_err:
632 if (r)
633 free(r);
634 return NULL;
637 /* Borrowed from vo_fbdev.c END */
638 #endif