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