options: move -name and -title to option struct
[mplayer.git] / libvo / video_out.c
blob57e0444094c88c632592d5c61f502c18285cbe1c
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"
37 #include "input/input.h"
39 #include "mp_msg.h"
41 #include "osdep/shmem.h"
42 #ifdef CONFIG_X11
43 #include "x11_common.h"
44 #endif
46 int xinerama_screen = -1;
47 int xinerama_x;
48 int xinerama_y;
50 int vo_nomouse_input = 0;
51 int vo_grabpointer = 1;
52 int vo_doublebuffering = 1;
53 int vo_vsync = 0;
54 int vo_fs = 0;
55 int vo_fsmode = 0;
56 float vo_panscan = 0.0f;
57 int vo_adapter_num=0;
58 int vo_refresh_rate=0;
59 int vo_keepaspect=1;
60 int vo_rootwin=0;
61 int vo_border=1;
62 int64_t WinID = -1;
64 int vo_pts=0; // for hw decoding
65 float vo_fps=0;
67 char *vo_subdevice = NULL;
68 int vo_directrendering=0;
70 int vo_colorkey = 0x0000ff00; // default colorkey is green
71 // (0xff000000 means that colorkey has been disabled)
75 // Externally visible list of all vo drivers
77 extern struct vo_driver video_out_mga;
78 extern struct vo_driver video_out_xmga;
79 extern struct vo_driver video_out_x11;
80 extern struct vo_driver video_out_xover;
81 extern struct vo_driver video_out_xvmc;
82 extern struct vo_driver video_out_vdpau;
83 extern struct vo_driver video_out_xv;
84 extern struct vo_driver video_out_gl_nosw;
85 extern struct vo_driver video_out_gl;
86 extern struct vo_driver video_out_gl2;
87 extern struct vo_driver video_out_matrixview;
88 extern struct vo_driver video_out_dga;
89 extern struct vo_driver video_out_sdl;
90 extern struct vo_driver video_out_3dfx;
91 extern struct vo_driver video_out_tdfxfb;
92 extern struct vo_driver video_out_s3fb;
93 extern struct vo_driver video_out_wii;
94 extern struct vo_driver video_out_null;
95 extern struct vo_driver video_out_zr;
96 extern struct vo_driver video_out_zr2;
97 extern struct vo_driver video_out_bl;
98 extern struct vo_driver video_out_fbdev;
99 extern struct vo_driver video_out_fbdev2;
100 extern struct vo_driver video_out_svga;
101 extern struct vo_driver video_out_png;
102 extern struct vo_driver video_out_ggi;
103 extern struct vo_driver video_out_aa;
104 extern struct vo_driver video_out_caca;
105 extern struct vo_driver video_out_mpegpes;
106 extern struct vo_driver video_out_yuv4mpeg;
107 extern struct vo_driver video_out_direct3d;
108 extern struct vo_driver video_out_directx;
109 extern struct vo_driver video_out_kva;
110 extern struct vo_driver video_out_dxr2;
111 extern struct vo_driver video_out_dxr3;
112 extern struct vo_driver video_out_ivtv;
113 extern struct vo_driver video_out_v4l2;
114 extern struct vo_driver video_out_jpeg;
115 extern struct vo_driver video_out_gif89a;
116 extern struct vo_driver video_out_vesa;
117 extern struct vo_driver video_out_directfb;
118 extern struct vo_driver video_out_dfbmga;
119 extern struct vo_driver video_out_xvidix;
120 extern struct vo_driver video_out_winvidix;
121 extern struct vo_driver video_out_cvidix;
122 extern struct vo_driver video_out_tdfx_vid;
123 extern struct vo_driver video_out_xvr100;
124 extern struct vo_driver video_out_tga;
125 extern struct vo_driver video_out_corevideo;
126 extern struct vo_driver video_out_quartz;
127 extern struct vo_driver video_out_pnm;
128 extern struct vo_driver video_out_md5sum;
130 const struct vo_driver *video_out_drivers[] =
132 #ifdef CONFIG_XVR100
133 &video_out_xvr100,
134 #endif
135 #ifdef CONFIG_TDFX_VID
136 &video_out_tdfx_vid,
137 #endif
138 #ifdef CONFIG_DIRECTX
139 &video_out_directx,
140 #endif
141 #ifdef CONFIG_DIRECT3D
142 &video_out_direct3d,
143 #endif
144 #ifdef CONFIG_KVA
145 &video_out_kva,
146 #endif
147 #ifdef CONFIG_COREVIDEO
148 &video_out_corevideo,
149 #endif
150 #ifdef CONFIG_QUARTZ
151 &video_out_quartz,
152 #endif
153 #ifdef CONFIG_XMGA
154 &video_out_xmga,
155 #endif
156 #ifdef CONFIG_MGA
157 &video_out_mga,
158 #endif
159 #ifdef CONFIG_TDFXFB
160 &video_out_tdfxfb,
161 #endif
162 #ifdef CONFIG_S3FB
163 &video_out_s3fb,
164 #endif
165 #ifdef CONFIG_WII
166 &video_out_wii,
167 #endif
168 #ifdef CONFIG_3DFX
169 &video_out_3dfx,
170 #endif
171 #if CONFIG_VDPAU
172 &video_out_vdpau,
173 #endif
174 #ifdef CONFIG_XV
175 &video_out_xv,
176 #endif
177 #ifdef CONFIG_X11
178 #ifdef CONFIG_GL
179 &video_out_gl_nosw,
180 #endif
181 &video_out_x11,
182 &video_out_xover,
183 #endif
184 #ifdef CONFIG_SDL
185 &video_out_sdl,
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_GGI
195 &video_out_ggi,
196 #endif
197 #ifdef CONFIG_FBDEV
198 &video_out_fbdev,
199 &video_out_fbdev2,
200 #endif
201 #ifdef CONFIG_SVGALIB
202 &video_out_svga,
203 #endif
204 #ifdef CONFIG_MATRIXVIEW
205 &video_out_matrixview,
206 #endif
207 #ifdef CONFIG_AA
208 &video_out_aa,
209 #endif
210 #ifdef CONFIG_CACA
211 &video_out_caca,
212 #endif
213 #ifdef CONFIG_DXR2
214 &video_out_dxr2,
215 #endif
216 #ifdef CONFIG_DXR3
217 &video_out_dxr3,
218 #endif
219 #ifdef CONFIG_IVTV
220 &video_out_ivtv,
221 #endif
222 #ifdef CONFIG_V4L2_DECODER
223 &video_out_v4l2,
224 #endif
225 #ifdef CONFIG_ZR
226 &video_out_zr,
227 &video_out_zr2,
228 #endif
229 #ifdef CONFIG_BL
230 &video_out_bl,
231 #endif
232 #ifdef CONFIG_VESA
233 &video_out_vesa,
234 #endif
235 #ifdef CONFIG_DIRECTFB
236 &video_out_dfbmga,
237 #endif
238 #ifdef CONFIG_VIDIX
239 #ifdef CONFIG_X11
240 &video_out_xvidix,
241 #endif
242 #if defined(__MINGW32__) || defined(__CYGWIN__)
243 &video_out_winvidix,
244 #endif
245 &video_out_cvidix,
246 #endif
247 &video_out_null,
248 // should not be auto-selected
249 #ifdef CONFIG_DIRECTFB
250 // vo directfb can call exit() if initialization fails
251 &video_out_directfb,
252 #endif
253 #if CONFIG_XVMC
254 &video_out_xvmc,
255 #endif
256 &video_out_mpegpes,
257 #ifdef CONFIG_YUV4MPEG
258 &video_out_yuv4mpeg,
259 #endif
260 #ifdef CONFIG_FFMPEG
261 &video_out_png,
262 #endif
263 #ifdef CONFIG_JPEG
264 &video_out_jpeg,
265 #endif
266 #ifdef CONFIG_GIF
267 &video_out_gif89a,
268 #endif
269 #ifdef CONFIG_TGA
270 &video_out_tga,
271 #endif
272 #ifdef CONFIG_PNM
273 &video_out_pnm,
274 #endif
275 #ifdef CONFIG_MD5SUM
276 &video_out_md5sum,
277 #endif
278 NULL
282 static int vo_preinit(struct vo *vo, const char *arg)
284 return vo->driver->preinit(vo, arg);
287 int vo_control(struct vo *vo, uint32_t request, void *data)
289 return vo->driver->control(vo, request, data);
292 // Return -1 if driver appears not to support a draw_image interface,
293 // 0 otherwise (whether the driver actually drew something or not).
294 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
296 if (!vo->config_ok)
297 return 0;
298 if (vo->driver->buffer_frames) {
299 vo->driver->draw_image(vo, mpi, pts);
300 return 0;
302 vo->frame_loaded = true;
303 vo->next_pts = pts;
304 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
305 return -1;
306 return 0;
309 int vo_get_buffered_frame(struct vo *vo, bool eof)
311 if (!vo->config_ok)
312 return -1;
313 if (vo->frame_loaded)
314 return 0;
315 if (!vo->driver->buffer_frames)
316 return -1;
317 vo->driver->get_buffered_frame(vo, eof);
318 return vo->frame_loaded ? 0 : -1;
321 int vo_draw_frame(struct vo *vo, uint8_t *src[])
323 assert(!vo->driver->is_new);
324 if (!vo->config_ok)
325 return 0;
326 return old_vo_draw_frame(vo, src);
329 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
331 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
334 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
336 if (!vo->config_ok)
337 return;
338 vo->driver->draw_osd(vo, osd);
341 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
343 if (!vo->config_ok)
344 return;
345 vo->frame_loaded = false;
346 vo->next_pts = MP_NOPTS_VALUE;
347 if (vo->driver->flip_page_timed)
348 vo->driver->flip_page_timed(vo, pts_us, duration);
349 else
350 vo->driver->flip_page(vo);
353 void vo_check_events(struct vo *vo)
355 if (!vo->config_ok)
356 return;
357 vo->driver->check_events(vo);
360 void vo_seek_reset(struct vo *vo)
362 vo_control(vo, VOCTRL_RESET, NULL);
363 vo->frame_loaded = false;
366 void vo_destroy(struct vo *vo)
368 vo->driver->uninit(vo);
369 talloc_free(vo);
372 void list_video_out(void)
374 int i = 0;
375 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
376 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
377 while (video_out_drivers[i]) {
378 const vo_info_t *info = video_out_drivers[i++]->info;
379 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
381 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
384 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
385 struct mp_fifo *key_fifo,
386 struct input_ctx *input_ctx)
388 char **vo_list = opts->video_driver_list;
389 int i;
390 struct vo *vo = talloc_ptrtype(NULL, vo);
391 struct vo initial_values = {
392 .opts = opts,
393 .x11 = x11,
394 .key_fifo = key_fifo,
395 .input_ctx = input_ctx,
397 // first try the preferred drivers, with their optional subdevice param:
398 if (vo_list && vo_list[0])
399 while (vo_list[0][0]) {
400 char *name = strdup(vo_list[0]);
401 vo_subdevice = strchr(name,':');
402 if (!strcmp(name, "pgm"))
403 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
404 if (!strcmp(name, "md5"))
405 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
406 if (vo_subdevice) {
407 vo_subdevice[0] = 0;
408 ++vo_subdevice;
410 for (i = 0; video_out_drivers[i]; i++) {
411 const struct vo_driver *video_driver = video_out_drivers[i];
412 const vo_info_t *info = video_driver->info;
413 if (!strcmp(info->short_name, name)) {
414 // name matches, try it
415 *vo = initial_values;
416 vo->driver = video_driver;
417 if (!vo_preinit(vo, vo_subdevice)) {
418 free(name);
419 return vo; // success!
421 talloc_free_children(vo);
424 // continue...
425 free(name);
426 ++vo_list;
427 if (!(vo_list[0]))
428 return NULL; // do NOT fallback to others
430 // now try the rest...
431 vo_subdevice = NULL;
432 for (i = 0; video_out_drivers[i]; i++) {
433 const struct vo_driver *video_driver = video_out_drivers[i];
434 *vo = initial_values;
435 vo->driver = video_driver;
436 if (!vo_preinit(vo, vo_subdevice))
437 return vo; // success!
438 talloc_free_children(vo);
440 free(vo);
441 return NULL;
444 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
445 uint32_t d_width, uint32_t d_height, uint32_t flags,
446 char *title, uint32_t format)
448 struct MPOpts *opts = vo->opts;
449 panscan_init(vo);
450 aspect_save_orig(vo, width, height);
451 aspect_save_prescale(vo, d_width, d_height);
453 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
454 aspect(vo, &d_width, &d_height, A_NOZOOM);
455 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
456 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
457 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
458 opts->vo_screenwidth, opts->vo_screenheight);
459 geometry_xy_changed |= xinerama_screen >= 0;
460 vo->dx += xinerama_x;
461 vo->dy += xinerama_y;
462 vo->dwidth = d_width;
463 vo->dheight = d_height;
466 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
467 title, format);
468 vo->config_ok = (ret == 0);
469 vo->config_count += vo->config_ok;
470 return ret;
474 * \brief lookup an integer in a table, table must have 0 as the last key
475 * \param key key to search for
476 * \result translation corresponding to key or "to" value of last mapping
477 * if not found.
479 int lookup_keymap_table(const struct mp_keymap *map, int key) {
480 while (map->from && map->from != key) map++;
481 return map->to;
485 * \brief helper function for the kind of panscan-scaling that needs a source
486 * and destination rectangle like Direct3D and VDPAU
488 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
489 int *src_start, int *src_end, int *dst_start, int *dst_end) {
490 if (scaled_src_size > dst_size) {
491 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
492 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
493 border = (border / 2 + 1) & ~1;
494 *src_start = border;
495 *src_end = src_size - border;
496 *dst_start = 0;
497 *dst_end = dst_size;
498 } else {
499 *src_start = 0;
500 *src_end = src_size;
501 *dst_start = (dst_size - scaled_src_size) / 2;
502 *dst_end = *dst_start + scaled_src_size;
507 * Calculate the appropriate source and destination rectangle to
508 * get a correctly scaled picture, including pan-scan.
509 * Can be extended to take future cropping support into account.
511 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
512 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
513 * may be NULL and only left and top are currently valid.
515 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
516 struct vo_rect *src, struct vo_rect *dst,
517 struct vo_rect *borders, const struct vo_rect *crop)
519 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
520 int scaled_width = 0;
521 int scaled_height = 0;
522 if (!crop) crop = &no_crop;
523 src_width -= crop->left + crop->right;
524 src_height -= crop->top + crop->bottom;
525 if (src_width < 2) src_width = 2;
526 if (src_height < 2) src_height = 2;
527 dst->left = 0; dst->right = vo->dwidth;
528 dst->top = 0; dst->bottom = vo->dheight;
529 src->left = 0; src->right = src_width;
530 src->top = 0; src->bottom = src_height;
531 if (borders) {
532 borders->left = 0; borders->top = 0;
534 if (aspect_scaling()) {
535 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
536 panscan_calc_windowed(vo);
537 scaled_width += vo->panscan_x;
538 scaled_height += vo->panscan_y;
539 if (borders) {
540 borders->left = (vo->dwidth - scaled_width ) / 2;
541 borders->top = (vo->dheight - scaled_height) / 2;
543 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
544 &src->left, &src->right, &dst->left, &dst->right);
545 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
546 &src->top, &src->bottom, &dst->top, &dst->bottom);
548 src->left += crop->left; src->right += crop->left;
549 src->top += crop->top; src->bottom += crop->top;
550 src->width = src->right - src->left;
551 src->height = src->bottom - src->top;
552 dst->width = dst->right - dst->left;
553 dst->height = dst->bottom - dst->top;
557 * Generates a mouse movement message if those are enable and sends it
558 * to the "main" MPlayer.
560 * \param posx new x position of mouse
561 * \param posy new y position of mouse
563 void vo_mouse_movement(struct vo *vo, int posx, int posy)
565 char cmd_str[40];
566 if (!enable_mouse_movements)
567 return;
568 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i", posx, posy);
569 mp_input_queue_cmd(vo->input_ctx, mp_input_parse_cmd(cmd_str));
572 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
573 /* Borrowed from vo_fbdev.c
574 Monitor ranges related functions*/
576 char *monitor_hfreq_str = NULL;
577 char *monitor_vfreq_str = NULL;
578 char *monitor_dotclock_str = NULL;
580 float range_max(range_t *r)
582 float max = 0;
584 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
585 if (max < r->max) max = r->max;
586 return max;
590 int in_range(range_t *r, float f)
592 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
593 if (f >= r->min && f <= r->max)
594 return 1;
595 return 0;
598 range_t *str2range(char *s)
600 float tmp_min, tmp_max;
601 char *endptr = s; // to start the loop
602 range_t *r = NULL;
603 int i;
605 if (!s)
606 return NULL;
607 for (i = 0; *endptr; i++) {
608 if (*s == ',')
609 goto out_err;
610 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
611 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
612 return NULL;
614 tmp_min = strtod(s, &endptr);
615 if (*endptr == 'k' || *endptr == 'K') {
616 tmp_min *= 1000.0;
617 endptr++;
618 } else if (*endptr == 'm' || *endptr == 'M') {
619 tmp_min *= 1000000.0;
620 endptr++;
622 if (*endptr == '-') {
623 tmp_max = strtod(endptr + 1, &endptr);
624 if (*endptr == 'k' || *endptr == 'K') {
625 tmp_max *= 1000.0;
626 endptr++;
627 } else if (*endptr == 'm' || *endptr == 'M') {
628 tmp_max *= 1000000.0;
629 endptr++;
631 if (*endptr != ',' && *endptr)
632 goto out_err;
633 } else if (*endptr == ',' || !*endptr) {
634 tmp_max = tmp_min;
635 } else
636 goto out_err;
637 r[i].min = tmp_min;
638 r[i].max = tmp_max;
639 if (r[i].min < 0 || r[i].max < 0)
640 goto out_err;
641 s = endptr + 1;
643 r[i].min = r[i].max = -1;
644 return r;
645 out_err:
646 if (r)
647 free(r);
648 return NULL;
651 /* Borrowed from vo_fbdev.c END */
652 #endif