Merge svn changes up to r27979
[mplayer.git] / libvo / video_out.c
blob5ca841ec552685c151d1ea51c4bfd5c622d2c132
2 /* this file contains libvo's common functions, variables used by
3 many/all drivers. */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include <unistd.h>
10 //#include <sys/mman.h>
12 #include "config.h"
13 #include "options.h"
14 #include "talloc.h"
15 #include "video_out.h"
16 #include "aspect.h"
17 #include "geometry.h"
19 #include "mp_msg.h"
20 #include "help_mp.h"
22 #include "osdep/shmem.h"
23 #ifdef CONFIG_X11
24 #include "x11_common.h"
25 #endif
27 int xinerama_screen = -1;
28 int xinerama_x;
29 int xinerama_y;
31 int vo_nomouse_input = 0;
32 int vo_grabpointer = 1;
33 int vo_doublebuffering = 1;
34 int vo_vsync = 0;
35 int vo_fs = 0;
36 int vo_fsmode = 0;
37 float vo_panscan = 0.0f;
38 int vo_adapter_num=0;
39 int vo_refresh_rate=0;
40 int vo_keepaspect=1;
41 int vo_rootwin=0;
42 int vo_border=1;
43 int WinID = -1;
45 int vo_pts=0; // for hw decoding
46 float vo_fps=0;
48 char *vo_subdevice = NULL;
49 int vo_directrendering=0;
51 int vo_colorkey = 0x0000ff00; // default colorkey is green
52 // (0xff000000 means that colorkey has been disabled)
55 // Externally visible list of all vo drivers
57 extern struct vo_driver video_out_mga;
58 extern struct vo_driver video_out_xmga;
59 extern struct vo_driver video_out_x11;
60 extern struct vo_driver video_out_xover;
61 extern struct vo_driver video_out_xvmc;
62 extern struct vo_driver video_out_xv;
63 extern struct vo_driver video_out_gl;
64 extern struct vo_driver video_out_gl2;
65 extern struct vo_driver video_out_dga;
66 extern struct vo_driver video_out_sdl;
67 extern struct vo_driver video_out_3dfx;
68 extern struct vo_driver video_out_tdfxfb;
69 extern struct vo_driver video_out_s3fb;
70 extern struct vo_driver video_out_wii;
71 extern struct vo_driver video_out_null;
72 extern struct vo_driver video_out_zr;
73 extern struct vo_driver video_out_zr2;
74 extern struct vo_driver video_out_bl;
75 extern struct vo_driver video_out_fbdev;
76 extern struct vo_driver video_out_fbdev2;
77 extern struct vo_driver video_out_svga;
78 extern struct vo_driver video_out_png;
79 extern struct vo_driver video_out_ggi;
80 extern struct vo_driver video_out_aa;
81 extern struct vo_driver video_out_caca;
82 extern struct vo_driver video_out_mpegpes;
83 extern struct vo_driver video_out_yuv4mpeg;
84 extern struct vo_driver video_out_direct3d;
85 extern struct vo_driver video_out_directx;
86 extern struct vo_driver video_out_dxr2;
87 extern struct vo_driver video_out_dxr3;
88 extern struct vo_driver video_out_ivtv;
89 extern struct vo_driver video_out_v4l2;
90 extern struct vo_driver video_out_jpeg;
91 extern struct vo_driver video_out_gif89a;
92 extern struct vo_driver video_out_vesa;
93 extern struct vo_driver video_out_directfb;
94 extern struct vo_driver video_out_dfbmga;
95 extern struct vo_driver video_out_xvidix;
96 extern struct vo_driver video_out_winvidix;
97 extern struct vo_driver video_out_cvidix;
98 extern struct vo_driver video_out_tdfx_vid;
99 extern struct vo_driver video_out_xvr100;
100 extern struct vo_driver video_out_tga;
101 extern struct vo_driver video_out_macosx;
102 extern struct vo_driver video_out_quartz;
103 extern struct vo_driver video_out_pnm;
104 extern struct vo_driver video_out_md5sum;
106 const struct vo_driver *video_out_drivers[] =
108 #ifdef CONFIG_XVR100
109 &video_out_xvr100,
110 #endif
111 #ifdef CONFIG_TDFX_VID
112 &video_out_tdfx_vid,
113 #endif
114 #ifdef CONFIG_DIRECTX
115 &video_out_directx,
116 #endif
117 #ifdef CONFIG_DIRECT3D
118 &video_out_direct3d,
119 #endif
120 #ifdef CONFIG_COREVIDEO
121 &video_out_macosx,
122 #endif
123 #ifdef CONFIG_QUARTZ
124 &video_out_quartz,
125 #endif
126 #ifdef CONFIG_XMGA
127 &video_out_xmga,
128 #endif
129 #ifdef CONFIG_MGA
130 &video_out_mga,
131 #endif
132 #ifdef CONFIG_TDFXFB
133 &video_out_tdfxfb,
134 #endif
135 #ifdef CONFIG_S3FB
136 &video_out_s3fb,
137 #endif
138 #ifdef CONFIG_WII
139 &video_out_wii,
140 #endif
141 #ifdef CONFIG_3DFX
142 &video_out_3dfx,
143 #endif
144 #ifdef CONFIG_XV
145 &video_out_xv,
146 #endif
147 #ifdef CONFIG_X11
148 &video_out_x11,
149 &video_out_xover,
150 #endif
151 #ifdef CONFIG_GL
152 &video_out_gl,
153 &video_out_gl2,
154 #endif
155 #ifdef CONFIG_DGA
156 &video_out_dga,
157 #endif
158 #ifdef CONFIG_SDL
159 &video_out_sdl,
160 #endif
161 #ifdef CONFIG_GGI
162 &video_out_ggi,
163 #endif
164 #ifdef CONFIG_FBDEV
165 &video_out_fbdev,
166 &video_out_fbdev2,
167 #endif
168 #ifdef CONFIG_SVGALIB
169 &video_out_svga,
170 #endif
171 #ifdef CONFIG_AA
172 &video_out_aa,
173 #endif
174 #ifdef CONFIG_CACA
175 &video_out_caca,
176 #endif
177 #ifdef CONFIG_DXR2
178 &video_out_dxr2,
179 #endif
180 #ifdef CONFIG_DXR3
181 &video_out_dxr3,
182 #endif
183 #ifdef CONFIG_IVTV
184 &video_out_ivtv,
185 #endif
186 #ifdef CONFIG_V4L2_DECODER
187 &video_out_v4l2,
188 #endif
189 #ifdef CONFIG_ZR
190 &video_out_zr,
191 &video_out_zr2,
192 #endif
193 #ifdef CONFIG_BL
194 &video_out_bl,
195 #endif
196 #ifdef CONFIG_VESA
197 &video_out_vesa,
198 #endif
199 #ifdef CONFIG_DIRECTFB
200 &video_out_directfb,
201 #endif
202 #ifdef CONFIG_DFBMGA
203 &video_out_dfbmga,
204 #endif
205 #ifdef CONFIG_VIDIX
206 #ifdef CONFIG_X11
207 &video_out_xvidix,
208 #endif
209 #if defined(__MINGW32__) || defined(__CYGWIN__)
210 &video_out_winvidix,
211 #endif
212 &video_out_cvidix,
213 #endif
214 &video_out_null,
215 // should not be auto-selected
216 #ifdef HAVE_XVMC
217 &video_out_xvmc,
218 #endif
219 &video_out_mpegpes,
220 #ifdef CONFIG_YUV4MPEG
221 &video_out_yuv4mpeg,
222 #endif
223 #ifdef CONFIG_PNG
224 &video_out_png,
225 #endif
226 #ifdef CONFIG_JPEG
227 &video_out_jpeg,
228 #endif
229 #ifdef CONFIG_GIF
230 &video_out_gif89a,
231 #endif
232 #ifdef CONFIG_TGA
233 &video_out_tga,
234 #endif
235 #ifdef CONFIG_PNM
236 &video_out_pnm,
237 #endif
238 #ifdef CONFIG_MD5SUM
239 &video_out_md5sum,
240 #endif
241 NULL
245 static int vo_preinit(struct vo *vo, const char *arg)
247 return vo->driver->preinit(vo, arg);
250 int vo_control(struct vo *vo, uint32_t request, void *data)
252 return vo->driver->control(vo, request, data);
255 int vo_draw_frame(struct vo *vo, uint8_t *src[])
257 if (!vo->config_ok)
258 return 0;
259 return vo->driver->draw_frame(vo, src);
262 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
264 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
267 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
269 if (!vo->config_ok)
270 return;
271 vo->driver->draw_osd(vo, osd);
274 void vo_flip_page(struct vo *vo)
276 if (!vo->config_ok)
277 return;
278 vo->driver->flip_page(vo);
281 void vo_check_events(struct vo *vo)
283 if (!vo->config_ok)
284 return;
285 vo->driver->check_events(vo);
288 void vo_destroy(struct vo *vo)
290 vo->driver->uninit(vo);
291 talloc_free(vo);
294 void list_video_out(void)
296 int i = 0;
297 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoOutputDrivers);
298 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
299 while (video_out_drivers[i]) {
300 const vo_info_t *info = video_out_drivers[i++]->info;
301 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
303 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
306 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
307 struct mp_fifo *key_fifo,
308 struct input_ctx *input_ctx)
310 char **vo_list = opts->video_driver_list;
311 int i;
312 struct vo *vo = talloc_ptrtype(NULL, vo);
313 struct vo initial_values = {
314 .opts = opts,
315 .x11 = x11,
316 .key_fifo = key_fifo,
317 .input_ctx = input_ctx,
319 // first try the preferred drivers, with their optional subdevice param:
320 if (vo_list && vo_list[0])
321 while (vo_list[0][0]) {
322 char *name = strdup(vo_list[0]);
323 vo_subdevice = strchr(name,':');
324 if (!strcmp(name, "pgm"))
325 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_PGM_HasBeenReplaced);
326 if (!strcmp(name, "md5"))
327 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_MD5_HasBeenReplaced);
328 if (vo_subdevice) {
329 vo_subdevice[0] = 0;
330 ++vo_subdevice;
332 for (i = 0; video_out_drivers[i]; i++) {
333 const struct vo_driver *video_driver = video_out_drivers[i];
334 const vo_info_t *info = video_driver->info;
335 if (!strcmp(info->short_name, name)) {
336 // name matches, try it
337 *vo = initial_values;
338 vo->driver = video_driver;
339 if (!vo_preinit(vo, vo_subdevice)) {
340 free(name);
341 return vo; // success!
345 // continue...
346 free(name);
347 ++vo_list;
348 if (!(vo_list[0]))
349 return NULL; // do NOT fallback to others
351 // now try the rest...
352 vo_subdevice = NULL;
353 for (i = 0; video_out_drivers[i]; i++) {
354 const struct vo_driver *video_driver = video_out_drivers[i];
355 *vo = initial_values;
356 vo->driver = video_driver;
357 if (!vo_preinit(vo, vo_subdevice))
358 return vo; // success!
360 free(vo);
361 return NULL;
364 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
365 uint32_t d_width, uint32_t d_height, uint32_t flags,
366 char *title, uint32_t format)
368 struct MPOpts *opts = vo->opts;
369 panscan_init(vo);
370 aspect_save_orig(vo, width, height);
371 aspect_save_prescale(vo, d_width, d_height);
373 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
374 aspect(vo, &d_width, &d_height, A_NOZOOM);
375 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
376 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
377 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
378 opts->vo_screenwidth, opts->vo_screenheight);
379 vo->dx += xinerama_x;
380 vo->dy += xinerama_y;
381 vo->dwidth = d_width;
382 vo->dheight = d_height;
385 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
386 title, format);
387 vo->config_ok = (ret == 0);
388 vo->config_count += vo->config_ok;
389 return ret;
392 #if defined(CONFIG_FBDEV)||defined(CONFIG_VESA)
393 /* Borrowed from vo_fbdev.c
394 Monitor ranges related functions*/
396 char *monitor_hfreq_str = NULL;
397 char *monitor_vfreq_str = NULL;
398 char *monitor_dotclock_str = NULL;
400 float range_max(range_t *r)
402 float max = 0;
404 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
405 if (max < r->max) max = r->max;
406 return max;
410 int in_range(range_t *r, float f)
412 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
413 if (f >= r->min && f <= r->max)
414 return 1;
415 return 0;
418 range_t *str2range(char *s)
420 float tmp_min, tmp_max;
421 char *endptr = s; // to start the loop
422 range_t *r = NULL;
423 int i;
425 if (!s)
426 return NULL;
427 for (i = 0; *endptr; i++) {
428 if (*s == ',')
429 goto out_err;
430 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) {
431 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
432 return NULL;
434 tmp_min = strtod(s, &endptr);
435 if (*endptr == 'k' || *endptr == 'K') {
436 tmp_min *= 1000.0;
437 endptr++;
438 } else if (*endptr == 'm' || *endptr == 'M') {
439 tmp_min *= 1000000.0;
440 endptr++;
442 if (*endptr == '-') {
443 tmp_max = strtod(endptr + 1, &endptr);
444 if (*endptr == 'k' || *endptr == 'K') {
445 tmp_max *= 1000.0;
446 endptr++;
447 } else if (*endptr == 'm' || *endptr == 'M') {
448 tmp_max *= 1000000.0;
449 endptr++;
451 if (*endptr != ',' && *endptr)
452 goto out_err;
453 } else if (*endptr == ',' || !*endptr) {
454 tmp_max = tmp_min;
455 } else
456 goto out_err;
457 r[i].min = tmp_min;
458 r[i].max = tmp_max;
459 if (r[i].min < 0 || r[i].max < 0)
460 goto out_err;
461 s = endptr + 1;
463 r[i].min = r[i].max = -1;
464 return r;
465 out_err:
466 if (r)
467 free(r);
468 return NULL;
471 /* Borrowed from vo_fbdev.c END */
472 #endif