core: restructure main play loop, continue audio after video
[mplayer.git] / libvo / video_out.c
blob18ca3e99e9a60b58e7e18a0341c8c73aace1736e
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_vdpau;
84 extern struct vo_driver video_out_xv;
85 extern struct vo_driver video_out_gl_nosw;
86 extern struct vo_driver video_out_gl;
87 extern struct vo_driver video_out_gl_sdl;
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_bl;
96 extern struct vo_driver video_out_fbdev;
97 extern struct vo_driver video_out_fbdev2;
98 extern struct vo_driver video_out_svga;
99 extern struct vo_driver video_out_png;
100 extern struct vo_driver video_out_ggi;
101 extern struct vo_driver video_out_aa;
102 extern struct vo_driver video_out_caca;
103 extern struct vo_driver video_out_mpegpes;
104 extern struct vo_driver video_out_yuv4mpeg;
105 extern struct vo_driver video_out_direct3d;
106 extern struct vo_driver video_out_directx;
107 extern struct vo_driver video_out_kva;
108 extern struct vo_driver video_out_dxr3;
109 extern struct vo_driver video_out_ivtv;
110 extern struct vo_driver video_out_v4l2;
111 extern struct vo_driver video_out_jpeg;
112 extern struct vo_driver video_out_gif89a;
113 extern struct vo_driver video_out_vesa;
114 extern struct vo_driver video_out_directfb;
115 extern struct vo_driver video_out_dfbmga;
116 extern struct vo_driver video_out_tdfx_vid;
117 extern struct vo_driver video_out_xvr100;
118 extern struct vo_driver video_out_tga;
119 extern struct vo_driver video_out_corevideo;
120 extern struct vo_driver video_out_quartz;
121 extern struct vo_driver video_out_pnm;
122 extern struct vo_driver video_out_md5sum;
124 const struct vo_driver *video_out_drivers[] =
126 #ifdef CONFIG_XVR100
127 &video_out_xvr100,
128 #endif
129 #ifdef CONFIG_TDFX_VID
130 &video_out_tdfx_vid,
131 #endif
132 #ifdef CONFIG_DIRECTX
133 &video_out_directx,
134 #endif
135 #ifdef CONFIG_DIRECT3D
136 &video_out_direct3d,
137 #endif
138 #ifdef CONFIG_KVA
139 &video_out_kva,
140 #endif
141 #ifdef CONFIG_COREVIDEO
142 &video_out_corevideo,
143 #endif
144 #ifdef CONFIG_QUARTZ
145 &video_out_quartz,
146 #endif
147 #ifdef CONFIG_XMGA
148 &video_out_xmga,
149 #endif
150 #ifdef CONFIG_MGA
151 &video_out_mga,
152 #endif
153 #ifdef CONFIG_TDFXFB
154 &video_out_tdfxfb,
155 #endif
156 #ifdef CONFIG_S3FB
157 &video_out_s3fb,
158 #endif
159 #ifdef CONFIG_WII
160 &video_out_wii,
161 #endif
162 #ifdef CONFIG_3DFX
163 &video_out_3dfx,
164 #endif
165 #if CONFIG_VDPAU
166 &video_out_vdpau,
167 #endif
168 #ifdef CONFIG_XV
169 &video_out_xv,
170 #endif
171 #ifdef CONFIG_X11
172 #ifdef CONFIG_GL
173 &video_out_gl_nosw,
174 #endif
175 &video_out_x11,
176 &video_out_xover,
177 #endif
178 #ifdef CONFIG_SDL
179 &video_out_sdl,
180 #endif
181 #ifdef CONFIG_GL
182 &video_out_gl,
183 #endif
184 #ifdef CONFIG_GL_SDL
185 &video_out_gl_sdl,
186 #endif
187 #ifdef CONFIG_DGA
188 &video_out_dga,
189 #endif
190 #ifdef CONFIG_GGI
191 &video_out_ggi,
192 #endif
193 #ifdef CONFIG_FBDEV
194 &video_out_fbdev,
195 &video_out_fbdev2,
196 #endif
197 #ifdef CONFIG_SVGALIB
198 &video_out_svga,
199 #endif
200 #ifdef CONFIG_AA
201 &video_out_aa,
202 #endif
203 #ifdef CONFIG_CACA
204 &video_out_caca,
205 #endif
206 #ifdef CONFIG_DXR3
207 &video_out_dxr3,
208 #endif
209 #ifdef CONFIG_IVTV
210 &video_out_ivtv,
211 #endif
212 #ifdef CONFIG_V4L2_DECODER
213 &video_out_v4l2,
214 #endif
215 #ifdef CONFIG_BL
216 &video_out_bl,
217 #endif
218 #ifdef CONFIG_VESA
219 &video_out_vesa,
220 #endif
221 #ifdef CONFIG_DIRECTFB
222 &video_out_dfbmga,
223 #endif
224 &video_out_null,
225 // should not be auto-selected
226 #ifdef CONFIG_DIRECTFB
227 // vo directfb can call exit() if initialization fails
228 &video_out_directfb,
229 #endif
230 &video_out_mpegpes,
231 #ifdef CONFIG_YUV4MPEG
232 &video_out_yuv4mpeg,
233 #endif
234 &video_out_png,
235 #ifdef CONFIG_JPEG
236 &video_out_jpeg,
237 #endif
238 #ifdef CONFIG_GIF
239 &video_out_gif89a,
240 #endif
241 #ifdef CONFIG_TGA
242 &video_out_tga,
243 #endif
244 #ifdef CONFIG_PNM
245 &video_out_pnm,
246 #endif
247 #ifdef CONFIG_MD5SUM
248 &video_out_md5sum,
249 #endif
250 NULL
254 static int vo_preinit(struct vo *vo, const char *arg)
256 return vo->driver->preinit(vo, arg);
259 int vo_control(struct vo *vo, uint32_t request, void *data)
261 return vo->driver->control(vo, request, data);
264 // Return -1 if driver appears not to support a draw_image interface,
265 // 0 otherwise (whether the driver actually drew something or not).
266 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
268 if (!vo->config_ok)
269 return 0;
270 if (vo->driver->buffer_frames) {
271 vo->driver->draw_image(vo, mpi, pts);
272 return 0;
274 vo->frame_loaded = true;
275 vo->next_pts = pts;
276 // Guaranteed to support at least DRAW_IMAGE later
277 if (vo->driver->is_new) {
278 vo->waiting_mpi = mpi;
279 return 0;
281 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
282 return -1;
283 return 0;
286 int vo_redraw_frame(struct vo *vo)
288 if (!vo->config_ok || !vo->hasframe)
289 return -1;
290 if (vo_control(vo, VOCTRL_REDRAW_FRAME, NULL) == true) {
291 vo->redrawing = true;
292 return 0;
294 return -1;
297 int vo_get_buffered_frame(struct vo *vo, bool eof)
299 if (!vo->config_ok)
300 return -1;
301 if (vo->frame_loaded)
302 return 0;
303 if (!vo->driver->buffer_frames)
304 return -1;
305 vo->driver->get_buffered_frame(vo, eof);
306 return vo->frame_loaded ? 0 : -1;
309 void vo_skip_frame(struct vo *vo)
311 vo_control(vo, VOCTRL_SKIPFRAME, NULL);
312 vo->frame_loaded = false;
315 int vo_draw_frame(struct vo *vo, uint8_t *src[])
317 assert(!vo->driver->is_new);
318 if (!vo->config_ok)
319 return 0;
320 return old_vo_draw_frame(vo, src);
323 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
325 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
328 void vo_new_frame_imminent(struct vo *vo)
330 if (!vo->driver->is_new)
331 return;
332 if (vo->driver->buffer_frames)
333 vo_control(vo, VOCTRL_NEWFRAME, NULL);
334 else {
335 vo_control(vo, VOCTRL_DRAW_IMAGE, vo->waiting_mpi);
336 vo->waiting_mpi = NULL;
340 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
342 if (!vo->config_ok)
343 return;
344 vo->driver->draw_osd(vo, osd);
347 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
349 if (!vo->config_ok)
350 return;
351 if (!vo->redrawing) {
352 vo->frame_loaded = false;
353 vo->next_pts = MP_NOPTS_VALUE;
355 vo->want_redraw = false;
356 vo->redrawing = false;
357 if (vo->driver->flip_page_timed)
358 vo->driver->flip_page_timed(vo, pts_us, duration);
359 else
360 vo->driver->flip_page(vo);
361 vo->hasframe = true;
364 void vo_check_events(struct vo *vo)
366 if (!vo->config_ok) {
367 if (vo->registered_fd != -1)
368 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
369 vo->registered_fd = -1;
370 return;
372 vo->driver->check_events(vo);
375 void vo_seek_reset(struct vo *vo)
377 vo_control(vo, VOCTRL_RESET, NULL);
378 vo->frame_loaded = false;
379 vo->hasframe = false;
382 void vo_destroy(struct vo *vo)
384 if (vo->registered_fd != -1)
385 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
386 vo->driver->uninit(vo);
387 talloc_free(vo);
390 void list_video_out(void)
392 int i = 0;
393 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
394 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
395 while (video_out_drivers[i]) {
396 const vo_info_t *info = video_out_drivers[i++]->info;
397 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
399 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
402 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
403 struct mp_fifo *key_fifo,
404 struct input_ctx *input_ctx)
406 char **vo_list = opts->video_driver_list;
407 int i;
408 struct vo *vo = talloc_ptrtype(NULL, vo);
409 struct vo initial_values = {
410 .opts = opts,
411 .x11 = x11,
412 .key_fifo = key_fifo,
413 .input_ctx = input_ctx,
414 .event_fd = -1,
415 .registered_fd = -1,
417 // first try the preferred drivers, with their optional subdevice param:
418 if (vo_list && vo_list[0])
419 while (vo_list[0][0]) {
420 char *name = strdup(vo_list[0]);
421 vo_subdevice = strchr(name,':');
422 if (!strcmp(name, "pgm"))
423 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
424 if (!strcmp(name, "md5"))
425 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
426 if (vo_subdevice) {
427 vo_subdevice[0] = 0;
428 ++vo_subdevice;
430 for (i = 0; video_out_drivers[i]; i++) {
431 const struct vo_driver *video_driver = video_out_drivers[i];
432 const vo_info_t *info = video_driver->info;
433 if (!strcmp(info->short_name, name)) {
434 // name matches, try it
435 *vo = initial_values;
436 vo->driver = video_driver;
437 if (!vo_preinit(vo, vo_subdevice)) {
438 free(name);
439 return vo; // success!
441 talloc_free_children(vo);
444 // continue...
445 free(name);
446 ++vo_list;
447 if (!(vo_list[0]))
448 return NULL; // do NOT fallback to others
450 // now try the rest...
451 vo_subdevice = NULL;
452 for (i = 0; video_out_drivers[i]; i++) {
453 const struct vo_driver *video_driver = video_out_drivers[i];
454 *vo = initial_values;
455 vo->driver = video_driver;
456 if (!vo_preinit(vo, vo_subdevice))
457 return vo; // success!
458 talloc_free_children(vo);
460 free(vo);
461 return NULL;
464 static int event_fd_callback(void *ctx, int fd)
466 struct vo *vo = ctx;
467 vo_check_events(vo);
468 return MP_INPUT_NOTHING;
471 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
472 uint32_t d_width, uint32_t d_height, uint32_t flags,
473 uint32_t format)
475 struct MPOpts *opts = vo->opts;
476 panscan_init(vo);
477 aspect_save_orig(vo, width, height);
478 aspect_save_prescale(vo, d_width, d_height);
480 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
481 aspect(vo, &d_width, &d_height, A_NOZOOM);
482 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
483 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
484 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
485 opts->vo_screenwidth, opts->vo_screenheight);
486 geometry_xy_changed |= xinerama_screen >= 0;
487 vo->dx += xinerama_x;
488 vo->dy += xinerama_y;
489 vo->dwidth = d_width;
490 vo->dheight = d_height;
493 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
494 format);
495 vo->config_ok = (ret == 0);
496 vo->config_count += vo->config_ok;
497 if (vo->registered_fd == -1 && vo->event_fd != -1 && vo->config_ok) {
498 mp_input_add_key_fd(vo->input_ctx, vo->event_fd, 1, event_fd_callback,
499 NULL, vo);
500 vo->registered_fd = vo->event_fd;
502 vo->frame_loaded = false;
503 vo->waiting_mpi = NULL;
504 vo->redrawing = false;
505 vo->hasframe = false;
506 return ret;
510 * \brief lookup an integer in a table, table must have 0 as the last key
511 * \param key key to search for
512 * \result translation corresponding to key or "to" value of last mapping
513 * if not found.
515 int lookup_keymap_table(const struct mp_keymap *map, int key) {
516 while (map->from && map->from != key) map++;
517 return map->to;
521 * \brief helper function for the kind of panscan-scaling that needs a source
522 * and destination rectangle like Direct3D and VDPAU
524 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
525 int *src_start, int *src_end, int *dst_start, int *dst_end) {
526 if (scaled_src_size > dst_size) {
527 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
528 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
529 border = (border / 2 + 1) & ~1;
530 *src_start = border;
531 *src_end = src_size - border;
532 *dst_start = 0;
533 *dst_end = dst_size;
534 } else {
535 *src_start = 0;
536 *src_end = src_size;
537 *dst_start = (dst_size - scaled_src_size) / 2;
538 *dst_end = *dst_start + scaled_src_size;
543 * Calculate the appropriate source and destination rectangle to
544 * get a correctly scaled picture, including pan-scan.
545 * Can be extended to take future cropping support into account.
547 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
548 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
549 * may be NULL and only left and top are currently valid.
551 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
552 struct vo_rect *src, struct vo_rect *dst,
553 struct vo_rect *borders, const struct vo_rect *crop)
555 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
556 int scaled_width = 0;
557 int scaled_height = 0;
558 if (!crop) crop = &no_crop;
559 src_width -= crop->left + crop->right;
560 src_height -= crop->top + crop->bottom;
561 if (src_width < 2) src_width = 2;
562 if (src_height < 2) src_height = 2;
563 dst->left = 0; dst->right = vo->dwidth;
564 dst->top = 0; dst->bottom = vo->dheight;
565 src->left = 0; src->right = src_width;
566 src->top = 0; src->bottom = src_height;
567 if (borders) {
568 borders->left = 0; borders->top = 0;
570 if (aspect_scaling()) {
571 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
572 panscan_calc_windowed(vo);
573 scaled_width += vo->panscan_x;
574 scaled_height += vo->panscan_y;
575 if (borders) {
576 borders->left = (vo->dwidth - scaled_width ) / 2;
577 borders->top = (vo->dheight - scaled_height) / 2;
579 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
580 &src->left, &src->right, &dst->left, &dst->right);
581 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
582 &src->top, &src->bottom, &dst->top, &dst->bottom);
584 src->left += crop->left; src->right += crop->left;
585 src->top += crop->top; src->bottom += crop->top;
586 src->width = src->right - src->left;
587 src->height = src->bottom - src->top;
588 dst->width = dst->right - dst->left;
589 dst->height = dst->bottom - dst->top;
592 // Return the window title the VO should set. Always returns a null terminated
593 // string. The string is valid until frontend code is invoked again. Copy it if
594 // you need to keep the string for an extended period of time.
595 const char *vo_get_window_title(struct vo *vo)
597 if (vo->opts->vo_wintitle) {
598 return vo->opts->vo_wintitle;
599 } else {
600 return "mplayer2";
605 * Generates a mouse movement message if those are enable and sends it
606 * to the "main" MPlayer.
608 * \param posx new x position of mouse
609 * \param posy new y position of mouse
611 void vo_mouse_movement(struct vo *vo, int posx, int posy)
613 char cmd_str[40];
614 if (!enable_mouse_movements)
615 return;
616 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i", posx, posy);
617 mp_input_queue_cmd(vo->input_ctx, mp_input_parse_cmd(cmd_str));
620 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
621 /* Borrowed from vo_fbdev.c
622 Monitor ranges related functions*/
624 char *monitor_hfreq_str = NULL;
625 char *monitor_vfreq_str = NULL;
626 char *monitor_dotclock_str = NULL;
628 float range_max(range_t *r)
630 float max = 0;
632 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
633 if (max < r->max) max = r->max;
634 return max;
638 int in_range(range_t *r, float f)
640 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
641 if (f >= r->min && f <= r->max)
642 return 1;
643 return 0;
646 range_t *str2range(char *s)
648 float tmp_min, tmp_max;
649 char *endptr = s; // to start the loop
650 range_t *r = NULL;
651 int i;
653 if (!s)
654 return NULL;
655 for (i = 0; *endptr; i++) {
656 if (*s == ',')
657 goto out_err;
658 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
659 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
660 return NULL;
662 tmp_min = strtod(s, &endptr);
663 if (*endptr == 'k' || *endptr == 'K') {
664 tmp_min *= 1000.0;
665 endptr++;
666 } else if (*endptr == 'm' || *endptr == 'M') {
667 tmp_min *= 1000000.0;
668 endptr++;
670 if (*endptr == '-') {
671 tmp_max = strtod(endptr + 1, &endptr);
672 if (*endptr == 'k' || *endptr == 'K') {
673 tmp_max *= 1000.0;
674 endptr++;
675 } else if (*endptr == 'm' || *endptr == 'M') {
676 tmp_max *= 1000000.0;
677 endptr++;
679 if (*endptr != ',' && *endptr)
680 goto out_err;
681 } else if (*endptr == ',' || !*endptr) {
682 tmp_max = tmp_min;
683 } else
684 goto out_err;
685 r[i].min = tmp_min;
686 r[i].max = tmp_max;
687 if (r[i].min < 0 || r[i].max < 0)
688 goto out_err;
689 s = endptr + 1;
691 r[i].min = r[i].max = -1;
692 return r;
693 out_err:
694 free(r);
695 return NULL;
698 /* Borrowed from vo_fbdev.c END */
699 #endif