Translation system changes part 2: replace macros by strings
[mplayer/glamo.git] / libvo / video_out.c
blob13f4f937f7ffd3c576d9bb839d2e2bb01158bc80
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>
25 #include <unistd.h>
26 //#include <sys/mman.h>
28 #include "config.h"
29 #include "options.h"
30 #include "talloc.h"
31 #include "video_out.h"
32 #include "aspect.h"
33 #include "geometry.h"
35 #include "mp_msg.h"
36 #include "help_mp.h"
38 #include "osdep/shmem.h"
39 #ifdef CONFIG_X11
40 #include "x11_common.h"
41 #endif
43 int xinerama_screen = -1;
44 int xinerama_x;
45 int xinerama_y;
47 int vo_nomouse_input = 0;
48 int vo_grabpointer = 1;
49 int vo_doublebuffering = 1;
50 int vo_vsync = 0;
51 int vo_fs = 0;
52 int vo_fsmode = 0;
53 float vo_panscan = 0.0f;
54 int vo_adapter_num=0;
55 int vo_refresh_rate=0;
56 int vo_keepaspect=1;
57 int vo_rootwin=0;
58 int vo_border=1;
59 int64_t WinID = -1;
61 int vo_pts=0; // for hw decoding
62 float vo_fps=0;
64 char *vo_subdevice = NULL;
65 int vo_directrendering=0;
67 int vo_colorkey = 0x0000ff00; // default colorkey is green
68 // (0xff000000 means that colorkey has been disabled)
71 // Externally visible list of all vo drivers
73 extern struct vo_driver video_out_mga;
74 extern struct vo_driver video_out_xmga;
75 extern struct vo_driver video_out_x11;
76 extern struct vo_driver video_out_xover;
77 extern struct vo_driver video_out_xvmc;
78 extern struct vo_driver video_out_vdpau;
79 extern struct vo_driver video_out_xv;
80 extern struct vo_driver video_out_gl;
81 extern struct vo_driver video_out_gl2;
82 extern struct vo_driver video_out_dga;
83 extern struct vo_driver video_out_sdl;
84 extern struct vo_driver video_out_3dfx;
85 extern struct vo_driver video_out_tdfxfb;
86 extern struct vo_driver video_out_s3fb;
87 extern struct vo_driver video_out_wii;
88 extern struct vo_driver video_out_null;
89 extern struct vo_driver video_out_zr;
90 extern struct vo_driver video_out_zr2;
91 extern struct vo_driver video_out_bl;
92 extern struct vo_driver video_out_fbdev;
93 extern struct vo_driver video_out_fbdev2;
94 extern struct vo_driver video_out_svga;
95 extern struct vo_driver video_out_png;
96 extern struct vo_driver video_out_ggi;
97 extern struct vo_driver video_out_aa;
98 extern struct vo_driver video_out_caca;
99 extern struct vo_driver video_out_mpegpes;
100 extern struct vo_driver video_out_yuv4mpeg;
101 extern struct vo_driver video_out_direct3d;
102 extern struct vo_driver video_out_directx;
103 extern struct vo_driver video_out_kva;
104 extern struct vo_driver video_out_dxr2;
105 extern struct vo_driver video_out_dxr3;
106 extern struct vo_driver video_out_ivtv;
107 extern struct vo_driver video_out_v4l2;
108 extern struct vo_driver video_out_jpeg;
109 extern struct vo_driver video_out_gif89a;
110 extern struct vo_driver video_out_vesa;
111 extern struct vo_driver video_out_directfb;
112 extern struct vo_driver video_out_dfbmga;
113 extern struct vo_driver video_out_xvidix;
114 extern struct vo_driver video_out_winvidix;
115 extern struct vo_driver video_out_cvidix;
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 #ifdef CONFIG_XV
166 &video_out_xv,
167 #endif
168 #if CONFIG_VDPAU
169 &video_out_vdpau,
170 #endif
171 #ifdef CONFIG_X11
172 &video_out_x11,
173 &video_out_xover,
174 #endif
175 #ifdef CONFIG_GL
176 &video_out_gl,
177 &video_out_gl2,
178 #endif
179 #ifdef CONFIG_DGA
180 &video_out_dga,
181 #endif
182 #ifdef CONFIG_SDL
183 &video_out_sdl,
184 #endif
185 #ifdef CONFIG_GGI
186 &video_out_ggi,
187 #endif
188 #ifdef CONFIG_FBDEV
189 &video_out_fbdev,
190 &video_out_fbdev2,
191 #endif
192 #ifdef CONFIG_SVGALIB
193 &video_out_svga,
194 #endif
195 #ifdef CONFIG_AA
196 &video_out_aa,
197 #endif
198 #ifdef CONFIG_CACA
199 &video_out_caca,
200 #endif
201 #ifdef CONFIG_DXR2
202 &video_out_dxr2,
203 #endif
204 #ifdef CONFIG_DXR3
205 &video_out_dxr3,
206 #endif
207 #ifdef CONFIG_IVTV
208 &video_out_ivtv,
209 #endif
210 #ifdef CONFIG_V4L2_DECODER
211 &video_out_v4l2,
212 #endif
213 #ifdef CONFIG_ZR
214 &video_out_zr,
215 &video_out_zr2,
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_directfb,
225 #endif
226 #ifdef CONFIG_DFBMGA
227 &video_out_dfbmga,
228 #endif
229 #ifdef CONFIG_VIDIX
230 #ifdef CONFIG_X11
231 &video_out_xvidix,
232 #endif
233 #if defined(__MINGW32__) || defined(__CYGWIN__)
234 &video_out_winvidix,
235 #endif
236 &video_out_cvidix,
237 #endif
238 &video_out_null,
239 // should not be auto-selected
240 #if CONFIG_XVMC
241 &video_out_xvmc,
242 #endif
243 &video_out_mpegpes,
244 #ifdef CONFIG_YUV4MPEG
245 &video_out_yuv4mpeg,
246 #endif
247 #ifdef CONFIG_PNG
248 &video_out_png,
249 #endif
250 #ifdef CONFIG_JPEG
251 &video_out_jpeg,
252 #endif
253 #ifdef CONFIG_GIF
254 &video_out_gif89a,
255 #endif
256 #ifdef CONFIG_TGA
257 &video_out_tga,
258 #endif
259 #ifdef CONFIG_PNM
260 &video_out_pnm,
261 #endif
262 #ifdef CONFIG_MD5SUM
263 &video_out_md5sum,
264 #endif
265 NULL
269 static int vo_preinit(struct vo *vo, const char *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 int vo_draw_frame(struct vo *vo, uint8_t *src[])
281 if (!vo->config_ok)
282 return 0;
283 return vo->driver->draw_frame(vo, src);
286 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
288 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
291 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
293 if (!vo->config_ok)
294 return;
295 vo->driver->draw_osd(vo, osd);
298 void vo_flip_page(struct vo *vo)
300 if (!vo->config_ok)
301 return;
302 vo->driver->flip_page(vo);
305 void vo_check_events(struct vo *vo)
307 if (!vo->config_ok)
308 return;
309 vo->driver->check_events(vo);
312 void vo_destroy(struct vo *vo)
314 vo->driver->uninit(vo);
315 talloc_free(vo);
318 void list_video_out(void)
320 int i = 0;
321 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
322 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
323 while (video_out_drivers[i]) {
324 const vo_info_t *info = video_out_drivers[i++]->info;
325 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
327 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
330 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
331 struct mp_fifo *key_fifo,
332 struct input_ctx *input_ctx)
334 char **vo_list = opts->video_driver_list;
335 int i;
336 struct vo *vo = talloc_ptrtype(NULL, vo);
337 struct vo initial_values = {
338 .opts = opts,
339 .x11 = x11,
340 .key_fifo = key_fifo,
341 .input_ctx = input_ctx,
343 // first try the preferred drivers, with their optional subdevice param:
344 if (vo_list && vo_list[0])
345 while (vo_list[0][0]) {
346 char *name = strdup(vo_list[0]);
347 vo_subdevice = strchr(name,':');
348 if (!strcmp(name, "pgm"))
349 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
350 if (!strcmp(name, "md5"))
351 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
352 if (vo_subdevice) {
353 vo_subdevice[0] = 0;
354 ++vo_subdevice;
356 for (i = 0; video_out_drivers[i]; i++) {
357 const struct vo_driver *video_driver = video_out_drivers[i];
358 const vo_info_t *info = video_driver->info;
359 if (!strcmp(info->short_name, name)) {
360 // name matches, try it
361 *vo = initial_values;
362 vo->driver = video_driver;
363 if (!vo_preinit(vo, vo_subdevice)) {
364 free(name);
365 return vo; // success!
367 talloc_free_children(vo);
370 // continue...
371 free(name);
372 ++vo_list;
373 if (!(vo_list[0]))
374 return NULL; // do NOT fallback to others
376 // now try the rest...
377 vo_subdevice = NULL;
378 for (i = 0; video_out_drivers[i]; i++) {
379 const struct vo_driver *video_driver = video_out_drivers[i];
380 *vo = initial_values;
381 vo->driver = video_driver;
382 if (!vo_preinit(vo, vo_subdevice))
383 return vo; // success!
384 talloc_free_children(vo);
386 free(vo);
387 return NULL;
390 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
391 uint32_t d_width, uint32_t d_height, uint32_t flags,
392 char *title, uint32_t format)
394 struct MPOpts *opts = vo->opts;
395 panscan_init(vo);
396 aspect_save_orig(vo, width, height);
397 aspect_save_prescale(vo, d_width, d_height);
399 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
400 aspect(vo, &d_width, &d_height, A_NOZOOM);
401 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
402 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
403 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
404 opts->vo_screenwidth, opts->vo_screenheight);
405 vo->dx += xinerama_x;
406 vo->dy += xinerama_y;
407 vo->dwidth = d_width;
408 vo->dheight = d_height;
411 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
412 title, format);
413 vo->config_ok = (ret == 0);
414 vo->config_count += vo->config_ok;
415 return ret;
419 * \brief lookup an integer in a table, table must have 0 as the last key
420 * \param key key to search for
421 * \result translation corresponding to key or "to" value of last mapping
422 * if not found.
424 int lookup_keymap_table(const struct keymap *map, int key) {
425 while (map->from && map->from != key) map++;
426 return map->to;
430 * \brief helper function for the kind of panscan-scaling that needs a source
431 * and destination rectangle like Direct3D and VDPAU
433 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
434 int *src_start, int *src_end, int *dst_start, int *dst_end) {
435 if (scaled_src_size > dst_size) {
436 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
437 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
438 border = (border / 2 + 1) & ~1;
439 *src_start = border;
440 *src_end = src_size - border;
441 *dst_start = 0;
442 *dst_end = dst_size;
443 } else {
444 *src_start = 0;
445 *src_end = src_size;
446 *dst_start = (dst_size - scaled_src_size) / 2;
447 *dst_end = *dst_start + scaled_src_size;
452 * Calculate the appropriate source and destination rectangle to
453 * get a correctly scaled picture, including pan-scan.
454 * Can be extended to take future cropping support into account.
456 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
457 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
458 * may be NULL and only left and top are currently valid.
460 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
461 struct vo_rect *src, struct vo_rect *dst,
462 struct vo_rect *borders, const struct vo_rect *crop)
464 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
465 int scaled_width = 0;
466 int scaled_height = 0;
467 if (!crop) crop = &no_crop;
468 src_width -= crop->left + crop->right;
469 src_height -= crop->top + crop->bottom;
470 if (src_width < 2) src_width = 2;
471 if (src_height < 2) src_height = 2;
472 dst->left = 0; dst->right = vo->dwidth;
473 dst->top = 0; dst->bottom = vo->dheight;
474 src->left = 0; src->right = src_width;
475 src->top = 0; src->bottom = src_height;
476 if (borders) {
477 borders->left = 0; borders->top = 0;
479 if (vo_fs) {
480 aspect(vo, &scaled_width, &scaled_height, A_ZOOM);
481 panscan_calc(vo);
482 scaled_width += vo->panscan_x;
483 scaled_height += vo->panscan_y;
484 if (borders) {
485 borders->left = (vo->dwidth - scaled_width ) / 2;
486 borders->top = (vo->dheight - scaled_height) / 2;
488 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
489 &src->left, &src->right, &dst->left, &dst->right);
490 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
491 &src->top, &src->bottom, &dst->top, &dst->bottom);
493 src->left += crop->left; src->right += crop->left;
494 src->top += crop->top; src->bottom += crop->top;
495 src->width = src->right - src->left;
496 src->height = src->bottom - src->top;
497 dst->width = dst->right - dst->left;
498 dst->height = dst->bottom - dst->top;
501 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
502 /* Borrowed from vo_fbdev.c
503 Monitor ranges related functions*/
505 char *monitor_hfreq_str = NULL;
506 char *monitor_vfreq_str = NULL;
507 char *monitor_dotclock_str = NULL;
509 float range_max(range_t *r)
511 float max = 0;
513 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
514 if (max < r->max) max = r->max;
515 return max;
519 int in_range(range_t *r, float f)
521 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
522 if (f >= r->min && f <= r->max)
523 return 1;
524 return 0;
527 range_t *str2range(char *s)
529 float tmp_min, tmp_max;
530 char *endptr = s; // to start the loop
531 range_t *r = NULL;
532 int i;
534 if (!s)
535 return NULL;
536 for (i = 0; *endptr; i++) {
537 if (*s == ',')
538 goto out_err;
539 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) {
540 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
541 return NULL;
543 tmp_min = strtod(s, &endptr);
544 if (*endptr == 'k' || *endptr == 'K') {
545 tmp_min *= 1000.0;
546 endptr++;
547 } else if (*endptr == 'm' || *endptr == 'M') {
548 tmp_min *= 1000000.0;
549 endptr++;
551 if (*endptr == '-') {
552 tmp_max = strtod(endptr + 1, &endptr);
553 if (*endptr == 'k' || *endptr == 'K') {
554 tmp_max *= 1000.0;
555 endptr++;
556 } else if (*endptr == 'm' || *endptr == 'M') {
557 tmp_max *= 1000000.0;
558 endptr++;
560 if (*endptr != ',' && *endptr)
561 goto out_err;
562 } else if (*endptr == ',' || !*endptr) {
563 tmp_max = tmp_min;
564 } else
565 goto out_err;
566 r[i].min = tmp_min;
567 r[i].max = tmp_max;
568 if (r[i].min < 0 || r[i].max < 0)
569 goto out_err;
570 s = endptr + 1;
572 r[i].min = r[i].max = -1;
573 return r;
574 out_err:
575 if (r)
576 free(r);
577 return NULL;
580 /* Borrowed from vo_fbdev.c END */
581 #endif