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.
28 //#include <sys/mman.h>
33 #include "video_out.h"
36 #include "old_vo_wrapper.h"
37 #include "input/input.h"
42 #include "osdep/shmem.h"
44 #include "x11_common.h"
47 int xinerama_screen
= -1;
51 int vo_nomouse_input
= 0;
52 int vo_grabpointer
= 1;
53 int vo_doublebuffering
= 1;
57 float vo_panscan
= 0.0f
;
59 int vo_refresh_rate
=0;
65 int vo_pts
=0; // for hw decoding
68 char *vo_subdevice
= NULL
;
69 int vo_directrendering
=0;
71 int vo_colorkey
= 0x0000ff00; // default colorkey is green
72 // (0xff000000 means that colorkey has been disabled)
76 // Externally visible list of all vo drivers
78 extern struct vo_driver video_out_mga
;
79 extern struct vo_driver video_out_xmga
;
80 extern struct vo_driver video_out_x11
;
81 extern struct vo_driver video_out_xover
;
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_gl_sdl
;
87 extern struct vo_driver video_out_dga
;
88 extern struct vo_driver video_out_sdl
;
89 extern struct vo_driver video_out_3dfx
;
90 extern struct vo_driver video_out_tdfxfb
;
91 extern struct vo_driver video_out_s3fb
;
92 extern struct vo_driver video_out_wii
;
93 extern struct vo_driver video_out_null
;
94 extern struct vo_driver video_out_bl
;
95 extern struct vo_driver video_out_fbdev
;
96 extern struct vo_driver video_out_fbdev2
;
97 extern struct vo_driver video_out_svga
;
98 extern struct vo_driver video_out_png
;
99 extern struct vo_driver video_out_ggi
;
100 extern struct vo_driver video_out_aa
;
101 extern struct vo_driver video_out_caca
;
102 extern struct vo_driver video_out_mpegpes
;
103 extern struct vo_driver video_out_yuv4mpeg
;
104 extern struct vo_driver video_out_direct3d
;
105 extern struct vo_driver video_out_directx
;
106 extern struct vo_driver video_out_kva
;
107 extern struct vo_driver video_out_dxr3
;
108 extern struct vo_driver video_out_ivtv
;
109 extern struct vo_driver video_out_v4l2
;
110 extern struct vo_driver video_out_jpeg
;
111 extern struct vo_driver video_out_gif89a
;
112 extern struct vo_driver video_out_vesa
;
113 extern struct vo_driver video_out_directfb
;
114 extern struct vo_driver video_out_dfbmga
;
115 extern struct vo_driver video_out_tdfx_vid
;
116 extern struct vo_driver video_out_xvr100
;
117 extern struct vo_driver video_out_tga
;
118 extern struct vo_driver video_out_corevideo
;
119 extern struct vo_driver video_out_sharedbuffer
;
120 extern struct vo_driver video_out_pnm
;
121 extern struct vo_driver video_out_md5sum
;
123 const struct vo_driver
*video_out_drivers
[] =
128 #ifdef CONFIG_TDFX_VID
131 #ifdef CONFIG_DIRECTX
134 #ifdef CONFIG_DIRECT3D
140 #ifdef CONFIG_GL_COCOA
143 #ifdef CONFIG_COREVIDEO
144 &video_out_corevideo
,
180 #if (defined CONFIG_GL && !defined CONFIG_GL_COCOA)
196 #ifdef CONFIG_SVGALIB
211 #ifdef CONFIG_V4L2_DECODER
220 #ifdef CONFIG_DIRECTFB
224 // should not be auto-selected
225 #ifdef CONFIG_DIRECTFB
226 // vo directfb can call exit() if initialization fails
230 #ifdef CONFIG_YUV4MPEG
249 #ifdef CONFIG_SHAREDBUFFER
250 &video_out_sharedbuffer
,
256 static int vo_preinit(struct vo
*vo
, char *arg
)
258 if (vo
->driver
->privsize
)
259 vo
->priv
= talloc_zero_size(vo
, vo
->driver
->privsize
);
260 if (vo
->driver
->options
) {
261 struct m_config
*cfg
= m_config_simple(vo
->driver
->options
);
262 m_config_initialize(cfg
, vo
->priv
);
264 int l
= snprintf(n
, sizeof(n
), "vo/%s", vo
->driver
->info
->short_name
);
265 assert(l
< sizeof(n
));
266 int r
= m_config_parse_suboptions(cfg
, vo
->priv
, n
, arg
);
271 return vo
->driver
->preinit(vo
, arg
);
274 int vo_control(struct vo
*vo
, uint32_t request
, void *data
)
276 return vo
->driver
->control(vo
, request
, data
);
279 // Return -1 if driver appears not to support a draw_image interface,
280 // 0 otherwise (whether the driver actually drew something or not).
281 int vo_draw_image(struct vo
*vo
, struct mp_image
*mpi
, double pts
)
285 if (vo
->driver
->buffer_frames
) {
286 vo
->driver
->draw_image(vo
, mpi
, pts
);
289 vo
->frame_loaded
= true;
291 // Guaranteed to support at least DRAW_IMAGE later
292 if (vo
->driver
->is_new
) {
293 vo
->waiting_mpi
= mpi
;
296 if (vo_control(vo
, VOCTRL_DRAW_IMAGE
, mpi
) == VO_NOTIMPL
)
301 int vo_redraw_frame(struct vo
*vo
)
303 if (!vo
->config_ok
|| !vo
->hasframe
)
305 if (vo_control(vo
, VOCTRL_REDRAW_FRAME
, NULL
) == true) {
306 vo
->redrawing
= true;
312 int vo_get_buffered_frame(struct vo
*vo
, bool eof
)
316 if (vo
->frame_loaded
)
318 if (!vo
->driver
->buffer_frames
)
320 vo
->driver
->get_buffered_frame(vo
, eof
);
321 return vo
->frame_loaded
? 0 : -1;
324 void vo_skip_frame(struct vo
*vo
)
326 vo_control(vo
, VOCTRL_SKIPFRAME
, NULL
);
327 vo
->frame_loaded
= false;
330 int vo_draw_frame(struct vo
*vo
, uint8_t *src
[])
332 assert(!vo
->driver
->is_new
);
335 return old_vo_draw_frame(vo
, src
);
338 int vo_draw_slice(struct vo
*vo
, uint8_t *src
[], int stride
[], int w
, int h
, int x
, int y
)
340 return vo
->driver
->draw_slice(vo
, src
, stride
, w
, h
, x
, y
);
343 void vo_new_frame_imminent(struct vo
*vo
)
345 if (!vo
->driver
->is_new
)
347 if (vo
->driver
->buffer_frames
)
348 vo_control(vo
, VOCTRL_NEWFRAME
, NULL
);
350 vo_control(vo
, VOCTRL_DRAW_IMAGE
, vo
->waiting_mpi
);
351 vo
->waiting_mpi
= NULL
;
355 void vo_draw_osd(struct vo
*vo
, struct osd_state
*osd
)
359 vo
->driver
->draw_osd(vo
, osd
);
362 void vo_flip_page(struct vo
*vo
, unsigned int pts_us
, int duration
)
366 if (!vo
->redrawing
) {
367 vo
->frame_loaded
= false;
368 vo
->next_pts
= MP_NOPTS_VALUE
;
370 vo
->want_redraw
= false;
371 vo
->redrawing
= false;
372 if (vo
->driver
->flip_page_timed
)
373 vo
->driver
->flip_page_timed(vo
, pts_us
, duration
);
375 vo
->driver
->flip_page(vo
);
379 void vo_check_events(struct vo
*vo
)
381 if (!vo
->config_ok
) {
382 if (vo
->registered_fd
!= -1)
383 mp_input_rm_key_fd(vo
->input_ctx
, vo
->registered_fd
);
384 vo
->registered_fd
= -1;
387 vo
->driver
->check_events(vo
);
390 void vo_seek_reset(struct vo
*vo
)
392 vo_control(vo
, VOCTRL_RESET
, NULL
);
393 vo
->frame_loaded
= false;
394 vo
->hasframe
= false;
397 void vo_destroy(struct vo
*vo
)
399 if (vo
->registered_fd
!= -1)
400 mp_input_rm_key_fd(vo
->input_ctx
, vo
->registered_fd
);
401 vo
->driver
->uninit(vo
);
405 void list_video_out(void)
408 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available video output drivers:\n");
409 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_OUTPUTS\n");
410 while (video_out_drivers
[i
]) {
411 const vo_info_t
*info
= video_out_drivers
[i
++]->info
;
412 mp_msg(MSGT_GLOBAL
, MSGL_INFO
,"\t%s\t%s\n", info
->short_name
, info
->name
);
414 mp_msg(MSGT_GLOBAL
, MSGL_INFO
,"\n");
417 struct vo
*init_best_video_out(struct MPOpts
*opts
, struct vo_x11_state
*x11
,
418 struct mp_fifo
*key_fifo
,
419 struct input_ctx
*input_ctx
)
421 char **vo_list
= opts
->video_driver_list
;
423 struct vo
*vo
= talloc_ptrtype(NULL
, vo
);
424 struct vo initial_values
= {
427 .key_fifo
= key_fifo
,
428 .input_ctx
= input_ctx
,
432 // first try the preferred drivers, with their optional subdevice param:
433 if (vo_list
&& vo_list
[0])
434 while (vo_list
[0][0]) {
435 char *name
= strdup(vo_list
[0]);
436 vo_subdevice
= strchr(name
,':');
437 if (!strcmp(name
, "pgm"))
438 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
439 if (!strcmp(name
, "md5"))
440 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "The md5 video output driver has been replaced by -vo md5sum.\n");
445 for (i
= 0; video_out_drivers
[i
]; i
++) {
446 const struct vo_driver
*video_driver
= video_out_drivers
[i
];
447 const vo_info_t
*info
= video_driver
->info
;
448 if (!strcmp(info
->short_name
, name
)) {
449 // name matches, try it
450 *vo
= initial_values
;
451 vo
->driver
= video_driver
;
452 if (!vo_preinit(vo
, vo_subdevice
)) {
454 return vo
; // success!
456 talloc_free_children(vo
);
463 return NULL
; // do NOT fallback to others
465 // now try the rest...
467 for (i
= 0; video_out_drivers
[i
]; i
++) {
468 const struct vo_driver
*video_driver
= video_out_drivers
[i
];
469 *vo
= initial_values
;
470 vo
->driver
= video_driver
;
471 if (!vo_preinit(vo
, vo_subdevice
))
472 return vo
; // success!
473 talloc_free_children(vo
);
479 static int event_fd_callback(void *ctx
, int fd
)
483 return MP_INPUT_NOTHING
;
486 int vo_config(struct vo
*vo
, uint32_t width
, uint32_t height
,
487 uint32_t d_width
, uint32_t d_height
, uint32_t flags
,
490 struct MPOpts
*opts
= vo
->opts
;
492 aspect_save_orig(vo
, width
, height
);
493 aspect_save_prescale(vo
, d_width
, d_height
);
495 if (vo_control(vo
, VOCTRL_UPDATE_SCREENINFO
, NULL
) == VO_TRUE
) {
496 aspect(vo
, &d_width
, &d_height
, A_NOZOOM
);
497 vo
->dx
= (int)(opts
->vo_screenwidth
- d_width
) / 2;
498 vo
->dy
= (int)(opts
->vo_screenheight
- d_height
) / 2;
499 geometry(&vo
->dx
, &vo
->dy
, &d_width
, &d_height
,
500 opts
->vo_screenwidth
, opts
->vo_screenheight
);
501 geometry_xy_changed
|= xinerama_screen
>= 0;
502 vo
->dx
+= xinerama_x
;
503 vo
->dy
+= xinerama_y
;
504 vo
->dwidth
= d_width
;
505 vo
->dheight
= d_height
;
508 int ret
= vo
->driver
->config(vo
, width
, height
, d_width
, d_height
, flags
,
510 vo
->config_ok
= (ret
== 0);
511 vo
->config_count
+= vo
->config_ok
;
512 if (vo
->registered_fd
== -1 && vo
->event_fd
!= -1 && vo
->config_ok
) {
513 mp_input_add_key_fd(vo
->input_ctx
, vo
->event_fd
, 1, event_fd_callback
,
515 vo
->registered_fd
= vo
->event_fd
;
517 vo
->frame_loaded
= false;
518 vo
->waiting_mpi
= NULL
;
519 vo
->redrawing
= false;
520 vo
->hasframe
= false;
525 * \brief lookup an integer in a table, table must have 0 as the last key
526 * \param key key to search for
527 * \result translation corresponding to key or "to" value of last mapping
530 int lookup_keymap_table(const struct mp_keymap
*map
, int key
) {
531 while (map
->from
&& map
->from
!= key
) map
++;
536 * \brief helper function for the kind of panscan-scaling that needs a source
537 * and destination rectangle like Direct3D and VDPAU
539 static void src_dst_split_scaling(int src_size
, int dst_size
, int scaled_src_size
,
540 int *src_start
, int *src_end
, int *dst_start
, int *dst_end
) {
541 if (scaled_src_size
> dst_size
) {
542 int border
= src_size
* (scaled_src_size
- dst_size
) / scaled_src_size
;
543 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
544 border
= (border
/ 2 + 1) & ~1;
546 *src_end
= src_size
- border
;
552 *dst_start
= (dst_size
- scaled_src_size
) / 2;
553 *dst_end
= *dst_start
+ scaled_src_size
;
558 * Calculate the appropriate source and destination rectangle to
559 * get a correctly scaled picture, including pan-scan.
560 * Can be extended to take future cropping support into account.
562 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
563 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
564 * may be NULL and only left and top are currently valid.
566 void calc_src_dst_rects(struct vo
*vo
, int src_width
, int src_height
,
567 struct vo_rect
*src
, struct vo_rect
*dst
,
568 struct vo_rect
*borders
, const struct vo_rect
*crop
)
570 static const struct vo_rect no_crop
= {0, 0, 0, 0, 0, 0};
571 int scaled_width
= 0;
572 int scaled_height
= 0;
573 if (!crop
) crop
= &no_crop
;
574 src_width
-= crop
->left
+ crop
->right
;
575 src_height
-= crop
->top
+ crop
->bottom
;
576 if (src_width
< 2) src_width
= 2;
577 if (src_height
< 2) src_height
= 2;
578 dst
->left
= 0; dst
->right
= vo
->dwidth
;
579 dst
->top
= 0; dst
->bottom
= vo
->dheight
;
580 src
->left
= 0; src
->right
= src_width
;
581 src
->top
= 0; src
->bottom
= src_height
;
583 borders
->left
= 0; borders
->top
= 0;
585 if (aspect_scaling()) {
586 aspect(vo
, &scaled_width
, &scaled_height
, A_WINZOOM
);
587 panscan_calc_windowed(vo
);
588 scaled_width
+= vo
->panscan_x
;
589 scaled_height
+= vo
->panscan_y
;
591 borders
->left
= (vo
->dwidth
- scaled_width
) / 2;
592 borders
->top
= (vo
->dheight
- scaled_height
) / 2;
594 src_dst_split_scaling(src_width
, vo
->dwidth
, scaled_width
,
595 &src
->left
, &src
->right
, &dst
->left
, &dst
->right
);
596 src_dst_split_scaling(src_height
, vo
->dheight
, scaled_height
,
597 &src
->top
, &src
->bottom
, &dst
->top
, &dst
->bottom
);
599 src
->left
+= crop
->left
; src
->right
+= crop
->left
;
600 src
->top
+= crop
->top
; src
->bottom
+= crop
->top
;
601 src
->width
= src
->right
- src
->left
;
602 src
->height
= src
->bottom
- src
->top
;
603 dst
->width
= dst
->right
- dst
->left
;
604 dst
->height
= dst
->bottom
- dst
->top
;
607 // Return the window title the VO should set. Always returns a null terminated
608 // string. The string is valid until frontend code is invoked again. Copy it if
609 // you need to keep the string for an extended period of time.
610 const char *vo_get_window_title(struct vo
*vo
)
612 if (vo
->opts
->vo_wintitle
) {
613 return vo
->opts
->vo_wintitle
;
620 * Generates a mouse movement message if those are enable and sends it
621 * to the "main" MPlayer.
623 * \param posx new x position of mouse
624 * \param posy new y position of mouse
626 void vo_mouse_movement(struct vo
*vo
, int posx
, int posy
)
629 if (!enable_mouse_movements
)
631 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i", posx
, posy
);
632 mp_input_queue_cmd(vo
->input_ctx
, mp_input_parse_cmd(cmd_str
));
635 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
636 /* Borrowed from vo_fbdev.c
637 Monitor ranges related functions*/
639 char *monitor_hfreq_str
= NULL
;
640 char *monitor_vfreq_str
= NULL
;
641 char *monitor_dotclock_str
= NULL
;
643 float range_max(range_t
*r
)
647 for (/* NOTHING */; (r
->min
!= -1 && r
->max
!= -1); r
++)
648 if (max
< r
->max
) max
= r
->max
;
653 int in_range(range_t
*r
, float f
)
655 for (/* NOTHING */; (r
->min
!= -1 && r
->max
!= -1); r
++)
656 if (f
>= r
->min
&& f
<= r
->max
)
661 range_t
*str2range(char *s
)
663 float tmp_min
, tmp_max
;
664 char *endptr
= s
; // to start the loop
670 for (i
= 0; *endptr
; i
++) {
673 if (!(r
= realloc(r
, sizeof(*r
) * (i
+ 2)))) {
674 mp_msg(MSGT_GLOBAL
, MSGL_WARN
,"can't realloc 'r'\n");
677 tmp_min
= strtod(s
, &endptr
);
678 if (*endptr
== 'k' || *endptr
== 'K') {
681 } else if (*endptr
== 'm' || *endptr
== 'M') {
682 tmp_min
*= 1000000.0;
685 if (*endptr
== '-') {
686 tmp_max
= strtod(endptr
+ 1, &endptr
);
687 if (*endptr
== 'k' || *endptr
== 'K') {
690 } else if (*endptr
== 'm' || *endptr
== 'M') {
691 tmp_max
*= 1000000.0;
694 if (*endptr
!= ',' && *endptr
)
696 } else if (*endptr
== ',' || !*endptr
) {
702 if (r
[i
].min
< 0 || r
[i
].max
< 0)
706 r
[i
].min
= r
[i
].max
= -1;
713 /* Borrowed from vo_fbdev.c END */