Merge branch 'mplayer1_changes'
[mplayer.git] / libvo / video_out.c
blob443ef87cded1db5fef73855484d63a204d000961
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_gl2;
88 extern struct vo_driver video_out_matrixview;
89 extern struct vo_driver video_out_dga;
90 extern struct vo_driver video_out_sdl;
91 extern struct vo_driver video_out_3dfx;
92 extern struct vo_driver video_out_tdfxfb;
93 extern struct vo_driver video_out_s3fb;
94 extern struct vo_driver video_out_wii;
95 extern struct vo_driver video_out_null;
96 extern struct vo_driver video_out_bl;
97 extern struct vo_driver video_out_fbdev;
98 extern struct vo_driver video_out_fbdev2;
99 extern struct vo_driver video_out_svga;
100 extern struct vo_driver video_out_png;
101 extern struct vo_driver video_out_ggi;
102 extern struct vo_driver video_out_aa;
103 extern struct vo_driver video_out_caca;
104 extern struct vo_driver video_out_mpegpes;
105 extern struct vo_driver video_out_yuv4mpeg;
106 extern struct vo_driver video_out_direct3d;
107 extern struct vo_driver video_out_directx;
108 extern struct vo_driver video_out_kva;
109 extern struct vo_driver video_out_dxr3;
110 extern struct vo_driver video_out_ivtv;
111 extern struct vo_driver video_out_v4l2;
112 extern struct vo_driver video_out_jpeg;
113 extern struct vo_driver video_out_gif89a;
114 extern struct vo_driver video_out_vesa;
115 extern struct vo_driver video_out_directfb;
116 extern struct vo_driver video_out_dfbmga;
117 extern struct vo_driver video_out_tdfx_vid;
118 extern struct vo_driver video_out_xvr100;
119 extern struct vo_driver video_out_tga;
120 extern struct vo_driver video_out_corevideo;
121 extern struct vo_driver video_out_quartz;
122 extern struct vo_driver video_out_pnm;
123 extern struct vo_driver video_out_md5sum;
125 const struct vo_driver *video_out_drivers[] =
127 #ifdef CONFIG_XVR100
128 &video_out_xvr100,
129 #endif
130 #ifdef CONFIG_TDFX_VID
131 &video_out_tdfx_vid,
132 #endif
133 #ifdef CONFIG_DIRECTX
134 &video_out_directx,
135 #endif
136 #ifdef CONFIG_DIRECT3D
137 &video_out_direct3d,
138 #endif
139 #ifdef CONFIG_KVA
140 &video_out_kva,
141 #endif
142 #ifdef CONFIG_COREVIDEO
143 &video_out_corevideo,
144 #endif
145 #ifdef CONFIG_QUARTZ
146 &video_out_quartz,
147 #endif
148 #ifdef CONFIG_XMGA
149 &video_out_xmga,
150 #endif
151 #ifdef CONFIG_MGA
152 &video_out_mga,
153 #endif
154 #ifdef CONFIG_TDFXFB
155 &video_out_tdfxfb,
156 #endif
157 #ifdef CONFIG_S3FB
158 &video_out_s3fb,
159 #endif
160 #ifdef CONFIG_WII
161 &video_out_wii,
162 #endif
163 #ifdef CONFIG_3DFX
164 &video_out_3dfx,
165 #endif
166 #if CONFIG_VDPAU
167 &video_out_vdpau,
168 #endif
169 #ifdef CONFIG_XV
170 &video_out_xv,
171 #endif
172 #ifdef CONFIG_X11
173 #ifdef CONFIG_GL
174 &video_out_gl_nosw,
175 #endif
176 &video_out_x11,
177 &video_out_xover,
178 #endif
179 #ifdef CONFIG_SDL
180 &video_out_sdl,
181 #endif
182 #ifdef CONFIG_GL
183 &video_out_gl,
184 &video_out_gl2,
185 #endif
186 #ifdef CONFIG_DGA
187 &video_out_dga,
188 #endif
189 #ifdef CONFIG_GGI
190 &video_out_ggi,
191 #endif
192 #ifdef CONFIG_FBDEV
193 &video_out_fbdev,
194 &video_out_fbdev2,
195 #endif
196 #ifdef CONFIG_SVGALIB
197 &video_out_svga,
198 #endif
199 #ifdef CONFIG_MATRIXVIEW
200 &video_out_matrixview,
201 #endif
202 #ifdef CONFIG_AA
203 &video_out_aa,
204 #endif
205 #ifdef CONFIG_CACA
206 &video_out_caca,
207 #endif
208 #ifdef CONFIG_DXR3
209 &video_out_dxr3,
210 #endif
211 #ifdef CONFIG_IVTV
212 &video_out_ivtv,
213 #endif
214 #ifdef CONFIG_V4L2_DECODER
215 &video_out_v4l2,
216 #endif
217 #ifdef CONFIG_BL
218 &video_out_bl,
219 #endif
220 #ifdef CONFIG_VESA
221 &video_out_vesa,
222 #endif
223 #ifdef CONFIG_DIRECTFB
224 &video_out_dfbmga,
225 #endif
226 &video_out_null,
227 // should not be auto-selected
228 #ifdef CONFIG_DIRECTFB
229 // vo directfb can call exit() if initialization fails
230 &video_out_directfb,
231 #endif
232 &video_out_mpegpes,
233 #ifdef CONFIG_YUV4MPEG
234 &video_out_yuv4mpeg,
235 #endif
236 #ifdef CONFIG_FFMPEG
237 &video_out_png,
238 #endif
239 #ifdef CONFIG_JPEG
240 &video_out_jpeg,
241 #endif
242 #ifdef CONFIG_GIF
243 &video_out_gif89a,
244 #endif
245 #ifdef CONFIG_TGA
246 &video_out_tga,
247 #endif
248 #ifdef CONFIG_PNM
249 &video_out_pnm,
250 #endif
251 #ifdef CONFIG_MD5SUM
252 &video_out_md5sum,
253 #endif
254 NULL
258 static int vo_preinit(struct vo *vo, const char *arg)
260 return vo->driver->preinit(vo, arg);
263 int vo_control(struct vo *vo, uint32_t request, void *data)
265 return vo->driver->control(vo, request, data);
268 // Return -1 if driver appears not to support a draw_image interface,
269 // 0 otherwise (whether the driver actually drew something or not).
270 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
272 if (!vo->config_ok)
273 return 0;
274 if (vo->driver->buffer_frames) {
275 vo->driver->draw_image(vo, mpi, pts);
276 return 0;
278 vo->frame_loaded = true;
279 vo->next_pts = pts;
280 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
281 return -1;
282 return 0;
285 int vo_get_buffered_frame(struct vo *vo, bool eof)
287 if (!vo->config_ok)
288 return -1;
289 if (vo->frame_loaded)
290 return 0;
291 if (!vo->driver->buffer_frames)
292 return -1;
293 vo->driver->get_buffered_frame(vo, eof);
294 return vo->frame_loaded ? 0 : -1;
297 void vo_skip_frame(struct vo *vo)
299 vo->frame_loaded = false;
302 int vo_draw_frame(struct vo *vo, uint8_t *src[])
304 assert(!vo->driver->is_new);
305 if (!vo->config_ok)
306 return 0;
307 return old_vo_draw_frame(vo, src);
310 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
312 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
315 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
317 if (!vo->config_ok)
318 return;
319 vo->driver->draw_osd(vo, osd);
322 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
324 if (!vo->config_ok)
325 return;
326 vo->frame_loaded = false;
327 vo->next_pts = MP_NOPTS_VALUE;
328 if (vo->driver->flip_page_timed)
329 vo->driver->flip_page_timed(vo, pts_us, duration);
330 else
331 vo->driver->flip_page(vo);
334 void vo_check_events(struct vo *vo)
336 if (!vo->config_ok) {
337 if (vo->registered_fd != -1)
338 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
339 vo->registered_fd = -1;
340 return;
342 vo->driver->check_events(vo);
345 void vo_seek_reset(struct vo *vo)
347 vo_control(vo, VOCTRL_RESET, NULL);
348 vo->frame_loaded = false;
351 void vo_destroy(struct vo *vo)
353 if (vo->registered_fd != -1)
354 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
355 vo->driver->uninit(vo);
356 talloc_free(vo);
359 void list_video_out(void)
361 int i = 0;
362 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
363 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
364 while (video_out_drivers[i]) {
365 const vo_info_t *info = video_out_drivers[i++]->info;
366 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
368 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
371 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
372 struct mp_fifo *key_fifo,
373 struct input_ctx *input_ctx)
375 char **vo_list = opts->video_driver_list;
376 int i;
377 struct vo *vo = talloc_ptrtype(NULL, vo);
378 struct vo initial_values = {
379 .opts = opts,
380 .x11 = x11,
381 .key_fifo = key_fifo,
382 .input_ctx = input_ctx,
383 .event_fd = -1,
384 .registered_fd = -1,
386 // first try the preferred drivers, with their optional subdevice param:
387 if (vo_list && vo_list[0])
388 while (vo_list[0][0]) {
389 char *name = strdup(vo_list[0]);
390 vo_subdevice = strchr(name,':');
391 if (!strcmp(name, "pgm"))
392 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
393 if (!strcmp(name, "md5"))
394 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
395 if (vo_subdevice) {
396 vo_subdevice[0] = 0;
397 ++vo_subdevice;
399 for (i = 0; video_out_drivers[i]; i++) {
400 const struct vo_driver *video_driver = video_out_drivers[i];
401 const vo_info_t *info = video_driver->info;
402 if (!strcmp(info->short_name, name)) {
403 // name matches, try it
404 *vo = initial_values;
405 vo->driver = video_driver;
406 if (!vo_preinit(vo, vo_subdevice)) {
407 free(name);
408 return vo; // success!
410 talloc_free_children(vo);
413 // continue...
414 free(name);
415 ++vo_list;
416 if (!(vo_list[0]))
417 return NULL; // do NOT fallback to others
419 // now try the rest...
420 vo_subdevice = NULL;
421 for (i = 0; video_out_drivers[i]; i++) {
422 const struct vo_driver *video_driver = video_out_drivers[i];
423 *vo = initial_values;
424 vo->driver = video_driver;
425 if (!vo_preinit(vo, vo_subdevice))
426 return vo; // success!
427 talloc_free_children(vo);
429 free(vo);
430 return NULL;
433 static int event_fd_callback(void *ctx, int fd)
435 struct vo *vo = ctx;
436 vo_check_events(vo);
437 return mplayer_get_key(vo->key_fifo, 0);
440 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
441 uint32_t d_width, uint32_t d_height, uint32_t flags,
442 char *title, uint32_t format)
444 struct MPOpts *opts = vo->opts;
445 panscan_init(vo);
446 aspect_save_orig(vo, width, height);
447 aspect_save_prescale(vo, d_width, d_height);
449 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
450 aspect(vo, &d_width, &d_height, A_NOZOOM);
451 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
452 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
453 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
454 opts->vo_screenwidth, opts->vo_screenheight);
455 geometry_xy_changed |= xinerama_screen >= 0;
456 vo->dx += xinerama_x;
457 vo->dy += xinerama_y;
458 vo->dwidth = d_width;
459 vo->dheight = d_height;
462 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
463 title, format);
464 vo->config_ok = (ret == 0);
465 vo->config_count += vo->config_ok;
466 if (vo->registered_fd == -1 && vo->event_fd != -1 && vo->config_ok) {
467 mp_input_add_key_fd(vo->input_ctx, vo->event_fd, 1, event_fd_callback,
468 NULL, vo);
469 vo->registered_fd = vo->event_fd;
471 return ret;
475 * \brief lookup an integer in a table, table must have 0 as the last key
476 * \param key key to search for
477 * \result translation corresponding to key or "to" value of last mapping
478 * if not found.
480 int lookup_keymap_table(const struct mp_keymap *map, int key) {
481 while (map->from && map->from != key) map++;
482 return map->to;
486 * \brief helper function for the kind of panscan-scaling that needs a source
487 * and destination rectangle like Direct3D and VDPAU
489 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
490 int *src_start, int *src_end, int *dst_start, int *dst_end) {
491 if (scaled_src_size > dst_size) {
492 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
493 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
494 border = (border / 2 + 1) & ~1;
495 *src_start = border;
496 *src_end = src_size - border;
497 *dst_start = 0;
498 *dst_end = dst_size;
499 } else {
500 *src_start = 0;
501 *src_end = src_size;
502 *dst_start = (dst_size - scaled_src_size) / 2;
503 *dst_end = *dst_start + scaled_src_size;
508 * Calculate the appropriate source and destination rectangle to
509 * get a correctly scaled picture, including pan-scan.
510 * Can be extended to take future cropping support into account.
512 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
513 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
514 * may be NULL and only left and top are currently valid.
516 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
517 struct vo_rect *src, struct vo_rect *dst,
518 struct vo_rect *borders, const struct vo_rect *crop)
520 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
521 int scaled_width = 0;
522 int scaled_height = 0;
523 if (!crop) crop = &no_crop;
524 src_width -= crop->left + crop->right;
525 src_height -= crop->top + crop->bottom;
526 if (src_width < 2) src_width = 2;
527 if (src_height < 2) src_height = 2;
528 dst->left = 0; dst->right = vo->dwidth;
529 dst->top = 0; dst->bottom = vo->dheight;
530 src->left = 0; src->right = src_width;
531 src->top = 0; src->bottom = src_height;
532 if (borders) {
533 borders->left = 0; borders->top = 0;
535 if (aspect_scaling()) {
536 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
537 panscan_calc_windowed(vo);
538 scaled_width += vo->panscan_x;
539 scaled_height += vo->panscan_y;
540 if (borders) {
541 borders->left = (vo->dwidth - scaled_width ) / 2;
542 borders->top = (vo->dheight - scaled_height) / 2;
544 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
545 &src->left, &src->right, &dst->left, &dst->right);
546 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
547 &src->top, &src->bottom, &dst->top, &dst->bottom);
549 src->left += crop->left; src->right += crop->left;
550 src->top += crop->top; src->bottom += crop->top;
551 src->width = src->right - src->left;
552 src->height = src->bottom - src->top;
553 dst->width = dst->right - dst->left;
554 dst->height = dst->bottom - dst->top;
558 * Generates a mouse movement message if those are enable and sends it
559 * to the "main" MPlayer.
561 * \param posx new x position of mouse
562 * \param posy new y position of mouse
564 void vo_mouse_movement(struct vo *vo, int posx, int posy)
566 char cmd_str[40];
567 if (!enable_mouse_movements)
568 return;
569 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i", posx, posy);
570 mp_input_queue_cmd(vo->input_ctx, mp_input_parse_cmd(cmd_str));
573 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
574 /* Borrowed from vo_fbdev.c
575 Monitor ranges related functions*/
577 char *monitor_hfreq_str = NULL;
578 char *monitor_vfreq_str = NULL;
579 char *monitor_dotclock_str = NULL;
581 float range_max(range_t *r)
583 float max = 0;
585 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
586 if (max < r->max) max = r->max;
587 return max;
591 int in_range(range_t *r, float f)
593 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
594 if (f >= r->min && f <= r->max)
595 return 1;
596 return 0;
599 range_t *str2range(char *s)
601 float tmp_min, tmp_max;
602 char *endptr = s; // to start the loop
603 range_t *r = NULL;
604 int i;
606 if (!s)
607 return NULL;
608 for (i = 0; *endptr; i++) {
609 if (*s == ',')
610 goto out_err;
611 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
612 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
613 return NULL;
615 tmp_min = strtod(s, &endptr);
616 if (*endptr == 'k' || *endptr == 'K') {
617 tmp_min *= 1000.0;
618 endptr++;
619 } else if (*endptr == 'm' || *endptr == 'M') {
620 tmp_min *= 1000000.0;
621 endptr++;
623 if (*endptr == '-') {
624 tmp_max = strtod(endptr + 1, &endptr);
625 if (*endptr == 'k' || *endptr == 'K') {
626 tmp_max *= 1000.0;
627 endptr++;
628 } else if (*endptr == 'm' || *endptr == 'M') {
629 tmp_max *= 1000000.0;
630 endptr++;
632 if (*endptr != ',' && *endptr)
633 goto out_err;
634 } else if (*endptr == ',' || !*endptr) {
635 tmp_max = tmp_min;
636 } else
637 goto out_err;
638 r[i].min = tmp_min;
639 r[i].max = tmp_max;
640 if (r[i].min < 0 || r[i].max < 0)
641 goto out_err;
642 s = endptr + 1;
644 r[i].min = r[i].max = -1;
645 return r;
646 out_err:
647 free(r);
648 return NULL;
651 /* Borrowed from vo_fbdev.c END */
652 #endif