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"
43 #include "osdep/shmem.h"
45 #include "x11_common.h"
48 int xinerama_screen
= -1;
52 int vo_nomouse_input
= 0;
53 int vo_grabpointer
= 1;
54 int vo_doublebuffering
= 1;
58 float vo_panscan
= 0.0f
;
60 int vo_refresh_rate
=0;
66 int vo_pts
=0; // for hw decoding
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
[] =
129 #ifdef CONFIG_TDFX_VID
132 #ifdef CONFIG_DIRECTX
135 #ifdef CONFIG_DIRECT3D
141 #ifdef CONFIG_COREVIDEO
142 &video_out_corevideo
,
197 #ifdef CONFIG_SVGALIB
212 #ifdef CONFIG_V4L2_DECODER
221 #ifdef CONFIG_DIRECTFB
225 // should not be auto-selected
226 #ifdef CONFIG_DIRECTFB
227 // vo directfb can call exit() if initialization fails
231 #ifdef CONFIG_YUV4MPEG
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
)
270 if (vo
->driver
->buffer_frames
) {
271 vo
->driver
->draw_image(vo
, mpi
, pts
);
274 vo
->frame_loaded
= true;
276 // Guaranteed to support at least DRAW_IMAGE later
277 if (vo
->driver
->is_new
) {
278 vo
->waiting_mpi
= mpi
;
281 if (vo_control(vo
, VOCTRL_DRAW_IMAGE
, mpi
) == VO_NOTIMPL
)
286 int vo_redraw_frame(struct vo
*vo
)
288 if (!vo
->config_ok
|| !vo
->hasframe
)
290 if (vo_control(vo
, VOCTRL_REDRAW_FRAME
, NULL
) == true) {
291 vo
->redrawing
= true;
297 int vo_get_buffered_frame(struct vo
*vo
, bool eof
)
301 if (vo
->frame_loaded
)
303 if (!vo
->driver
->buffer_frames
)
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
);
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
)
332 if (vo
->driver
->buffer_frames
)
333 vo_control(vo
, VOCTRL_NEWFRAME
, NULL
);
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
)
344 vo
->driver
->draw_osd(vo
, osd
);
347 void vo_flip_page(struct vo
*vo
, unsigned int pts_us
, int duration
)
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
);
360 vo
->driver
->flip_page(vo
);
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;
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;
381 void vo_destroy(struct vo
*vo
)
383 if (vo
->registered_fd
!= -1)
384 mp_input_rm_key_fd(vo
->input_ctx
, vo
->registered_fd
);
385 vo
->driver
->uninit(vo
);
389 void list_video_out(void)
392 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available video output drivers:\n");
393 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_OUTPUTS\n");
394 while (video_out_drivers
[i
]) {
395 const vo_info_t
*info
= video_out_drivers
[i
++]->info
;
396 mp_msg(MSGT_GLOBAL
, MSGL_INFO
,"\t%s\t%s\n", info
->short_name
, info
->name
);
398 mp_msg(MSGT_GLOBAL
, MSGL_INFO
,"\n");
401 struct vo
*init_best_video_out(struct MPOpts
*opts
, struct vo_x11_state
*x11
,
402 struct mp_fifo
*key_fifo
,
403 struct input_ctx
*input_ctx
)
405 char **vo_list
= opts
->video_driver_list
;
407 struct vo
*vo
= talloc_ptrtype(NULL
, vo
);
408 struct vo initial_values
= {
411 .key_fifo
= key_fifo
,
412 .input_ctx
= input_ctx
,
416 // first try the preferred drivers, with their optional subdevice param:
417 if (vo_list
&& vo_list
[0])
418 while (vo_list
[0][0]) {
419 char *name
= strdup(vo_list
[0]);
420 vo_subdevice
= strchr(name
,':');
421 if (!strcmp(name
, "pgm"))
422 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
423 if (!strcmp(name
, "md5"))
424 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "The md5 video output driver has been replaced by -vo md5sum.\n");
429 for (i
= 0; video_out_drivers
[i
]; i
++) {
430 const struct vo_driver
*video_driver
= video_out_drivers
[i
];
431 const vo_info_t
*info
= video_driver
->info
;
432 if (!strcmp(info
->short_name
, name
)) {
433 // name matches, try it
434 *vo
= initial_values
;
435 vo
->driver
= video_driver
;
436 if (!vo_preinit(vo
, vo_subdevice
)) {
438 return vo
; // success!
440 talloc_free_children(vo
);
447 return NULL
; // do NOT fallback to others
449 // now try the rest...
451 for (i
= 0; video_out_drivers
[i
]; i
++) {
452 const struct vo_driver
*video_driver
= video_out_drivers
[i
];
453 *vo
= initial_values
;
454 vo
->driver
= video_driver
;
455 if (!vo_preinit(vo
, vo_subdevice
))
456 return vo
; // success!
457 talloc_free_children(vo
);
463 static int event_fd_callback(void *ctx
, int fd
)
467 return MP_INPUT_NOTHING
;
470 int vo_config(struct vo
*vo
, uint32_t width
, uint32_t height
,
471 uint32_t d_width
, uint32_t d_height
, uint32_t flags
,
474 struct MPOpts
*opts
= vo
->opts
;
476 aspect_save_orig(vo
, width
, height
);
477 aspect_save_prescale(vo
, d_width
, d_height
);
479 if (vo_control(vo
, VOCTRL_UPDATE_SCREENINFO
, NULL
) == VO_TRUE
) {
480 aspect(vo
, &d_width
, &d_height
, A_NOZOOM
);
481 vo
->dx
= (int)(opts
->vo_screenwidth
- d_width
) / 2;
482 vo
->dy
= (int)(opts
->vo_screenheight
- d_height
) / 2;
483 geometry(&vo
->dx
, &vo
->dy
, &d_width
, &d_height
,
484 opts
->vo_screenwidth
, opts
->vo_screenheight
);
485 geometry_xy_changed
|= xinerama_screen
>= 0;
486 vo
->dx
+= xinerama_x
;
487 vo
->dy
+= xinerama_y
;
488 vo
->dwidth
= d_width
;
489 vo
->dheight
= d_height
;
492 int ret
= vo
->driver
->config(vo
, width
, height
, d_width
, d_height
, flags
,
494 vo
->config_ok
= (ret
== 0);
495 vo
->config_count
+= vo
->config_ok
;
496 if (vo
->registered_fd
== -1 && vo
->event_fd
!= -1 && vo
->config_ok
) {
497 mp_input_add_key_fd(vo
->input_ctx
, vo
->event_fd
, 1, event_fd_callback
,
499 vo
->registered_fd
= vo
->event_fd
;
501 vo
->frame_loaded
= false;
502 vo
->waiting_mpi
= NULL
;
503 vo
->redrawing
= false;
504 vo
->hasframe
= false;
509 * \brief lookup an integer in a table, table must have 0 as the last key
510 * \param key key to search for
511 * \result translation corresponding to key or "to" value of last mapping
514 int lookup_keymap_table(const struct mp_keymap
*map
, int key
) {
515 while (map
->from
&& map
->from
!= key
) map
++;
520 * \brief helper function for the kind of panscan-scaling that needs a source
521 * and destination rectangle like Direct3D and VDPAU
523 static void src_dst_split_scaling(int src_size
, int dst_size
, int scaled_src_size
,
524 int *src_start
, int *src_end
, int *dst_start
, int *dst_end
) {
525 if (scaled_src_size
> dst_size
) {
526 int border
= src_size
* (scaled_src_size
- dst_size
) / scaled_src_size
;
527 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
528 border
= (border
/ 2 + 1) & ~1;
530 *src_end
= src_size
- border
;
536 *dst_start
= (dst_size
- scaled_src_size
) / 2;
537 *dst_end
= *dst_start
+ scaled_src_size
;
542 * Calculate the appropriate source and destination rectangle to
543 * get a correctly scaled picture, including pan-scan.
544 * Can be extended to take future cropping support into account.
546 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
547 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
548 * may be NULL and only left and top are currently valid.
550 void calc_src_dst_rects(struct vo
*vo
, int src_width
, int src_height
,
551 struct vo_rect
*src
, struct vo_rect
*dst
,
552 struct vo_rect
*borders
, const struct vo_rect
*crop
)
554 static const struct vo_rect no_crop
= {0, 0, 0, 0, 0, 0};
555 int scaled_width
= 0;
556 int scaled_height
= 0;
557 if (!crop
) crop
= &no_crop
;
558 src_width
-= crop
->left
+ crop
->right
;
559 src_height
-= crop
->top
+ crop
->bottom
;
560 if (src_width
< 2) src_width
= 2;
561 if (src_height
< 2) src_height
= 2;
562 dst
->left
= 0; dst
->right
= vo
->dwidth
;
563 dst
->top
= 0; dst
->bottom
= vo
->dheight
;
564 src
->left
= 0; src
->right
= src_width
;
565 src
->top
= 0; src
->bottom
= src_height
;
567 borders
->left
= 0; borders
->top
= 0;
569 if (aspect_scaling()) {
570 aspect(vo
, &scaled_width
, &scaled_height
, A_WINZOOM
);
571 panscan_calc_windowed(vo
);
572 scaled_width
+= vo
->panscan_x
;
573 scaled_height
+= vo
->panscan_y
;
575 borders
->left
= (vo
->dwidth
- scaled_width
) / 2;
576 borders
->top
= (vo
->dheight
- scaled_height
) / 2;
578 src_dst_split_scaling(src_width
, vo
->dwidth
, scaled_width
,
579 &src
->left
, &src
->right
, &dst
->left
, &dst
->right
);
580 src_dst_split_scaling(src_height
, vo
->dheight
, scaled_height
,
581 &src
->top
, &src
->bottom
, &dst
->top
, &dst
->bottom
);
583 src
->left
+= crop
->left
; src
->right
+= crop
->left
;
584 src
->top
+= crop
->top
; src
->bottom
+= crop
->top
;
585 src
->width
= src
->right
- src
->left
;
586 src
->height
= src
->bottom
- src
->top
;
587 dst
->width
= dst
->right
- dst
->left
;
588 dst
->height
= dst
->bottom
- dst
->top
;
591 // Return the window title the VO should set. Always returns a null terminated
592 // string. The string is valid until frontend code is invoked again. Copy it if
593 // you need to keep the string for an extended period of time.
594 const char *vo_get_window_title(struct vo
*vo
)
596 if (vo
->opts
->vo_wintitle
) {
597 return vo
->opts
->vo_wintitle
;
604 * Generates a mouse movement message if those are enable and sends it
605 * to the "main" MPlayer.
607 * \param posx new x position of mouse
608 * \param posy new y position of mouse
610 void vo_mouse_movement(struct vo
*vo
, int posx
, int posy
)
613 if (!enable_mouse_movements
)
615 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i", posx
, posy
);
616 mp_input_queue_cmd(vo
->input_ctx
, mp_input_parse_cmd(cmd_str
));
619 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
620 /* Borrowed from vo_fbdev.c
621 Monitor ranges related functions*/
623 char *monitor_hfreq_str
= NULL
;
624 char *monitor_vfreq_str
= NULL
;
625 char *monitor_dotclock_str
= NULL
;
627 float range_max(range_t
*r
)
631 for (/* NOTHING */; (r
->min
!= -1 && r
->max
!= -1); r
++)
632 if (max
< r
->max
) max
= r
->max
;
637 int in_range(range_t
*r
, float f
)
639 for (/* NOTHING */; (r
->min
!= -1 && r
->max
!= -1); r
++)
640 if (f
>= r
->min
&& f
<= r
->max
)
645 range_t
*str2range(char *s
)
647 float tmp_min
, tmp_max
;
648 char *endptr
= s
; // to start the loop
654 for (i
= 0; *endptr
; i
++) {
657 if (!(r
= realloc(r
, sizeof(*r
) * (i
+ 2)))) {
658 mp_msg(MSGT_GLOBAL
, MSGL_WARN
,"can't realloc 'r'\n");
661 tmp_min
= strtod(s
, &endptr
);
662 if (*endptr
== 'k' || *endptr
== 'K') {
665 } else if (*endptr
== 'm' || *endptr
== 'M') {
666 tmp_min
*= 1000000.0;
669 if (*endptr
== '-') {
670 tmp_max
= strtod(endptr
+ 1, &endptr
);
671 if (*endptr
== 'k' || *endptr
== 'K') {
674 } else if (*endptr
== 'm' || *endptr
== 'M') {
675 tmp_max
*= 1000000.0;
678 if (*endptr
!= ',' && *endptr
)
680 } else if (*endptr
== ',' || !*endptr
) {
686 if (r
[i
].min
< 0 || r
[i
].max
< 0)
690 r
[i
].min
= r
[i
].max
= -1;
697 /* Borrowed from vo_fbdev.c END */