Fix crash with partial fribidi conversion.
[mplayer/greg.git] / libvo / video_out.c
blobec4cd9c109e8cb131db69d9a2eaa6e33f8411796
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 "video_out.h"
30 #include "aspect.h"
31 #include "geometry.h"
33 #include "mp_msg.h"
34 #include "help_mp.h"
36 #include "osdep/shmem.h"
38 //int vo_flags=0;
40 int xinerama_screen = -1;
41 int xinerama_x;
42 int xinerama_y;
44 // currect resolution/bpp on screen: (should be autodetected by vo_init())
45 int vo_depthonscreen=0;
46 int vo_screenwidth=0;
47 int vo_screenheight=0;
49 int vo_config_count=0;
51 // requested resolution/bpp: (-x -y -bpp options)
52 int vo_dx=0;
53 int vo_dy=0;
54 int vo_dwidth=0;
55 int vo_dheight=0;
56 int vo_dbpp=0;
58 int vo_nomouse_input = 0;
59 int vo_grabpointer = 1;
60 int vo_doublebuffering = 1;
61 int vo_vsync = 0;
62 int vo_fs = 0;
63 int vo_fsmode = 0;
64 float vo_panscan = 0.0f;
65 int vo_ontop = 0;
66 int vo_adapter_num=0;
67 int vo_refresh_rate=0;
68 int vo_keepaspect=1;
69 int vo_rootwin=0;
70 int vo_border=1;
71 int64_t WinID = -1;
73 int vo_pts=0; // for hw decoding
74 float vo_fps=0;
76 char *vo_subdevice = NULL;
77 int vo_directrendering=0;
79 int vo_colorkey = 0x0000ff00; // default colorkey is green
80 // (0xff000000 means that colorkey has been disabled)
82 // name to be used instead of the vo's default
83 char *vo_winname;
84 // title to be applied to movie window
85 char *vo_wintitle;
88 // Externally visible list of all vo drivers
90 extern vo_functions_t video_out_mga;
91 extern vo_functions_t video_out_xmga;
92 extern vo_functions_t video_out_x11;
93 extern vo_functions_t video_out_xover;
94 extern vo_functions_t video_out_xvmc;
95 extern vo_functions_t video_out_vdpau;
96 extern vo_functions_t video_out_xv;
97 extern vo_functions_t video_out_gl;
98 extern vo_functions_t video_out_gl2;
99 extern vo_functions_t video_out_dga;
100 extern vo_functions_t video_out_sdl;
101 extern vo_functions_t video_out_3dfx;
102 extern vo_functions_t video_out_tdfxfb;
103 extern vo_functions_t video_out_s3fb;
104 extern vo_functions_t video_out_wii;
105 extern vo_functions_t video_out_null;
106 extern vo_functions_t video_out_zr;
107 extern vo_functions_t video_out_zr2;
108 extern vo_functions_t video_out_bl;
109 extern vo_functions_t video_out_fbdev;
110 extern vo_functions_t video_out_fbdev2;
111 extern vo_functions_t video_out_svga;
112 extern vo_functions_t video_out_png;
113 extern vo_functions_t video_out_ggi;
114 extern vo_functions_t video_out_aa;
115 extern vo_functions_t video_out_caca;
116 extern vo_functions_t video_out_mpegpes;
117 extern vo_functions_t video_out_yuv4mpeg;
118 extern vo_functions_t video_out_direct3d;
119 extern vo_functions_t video_out_directx;
120 extern vo_functions_t video_out_kva;
121 extern vo_functions_t video_out_dxr2;
122 extern vo_functions_t video_out_dxr3;
123 extern vo_functions_t video_out_ivtv;
124 extern vo_functions_t video_out_v4l2;
125 extern vo_functions_t video_out_jpeg;
126 extern vo_functions_t video_out_gif89a;
127 extern vo_functions_t video_out_vesa;
128 extern vo_functions_t video_out_directfb;
129 extern vo_functions_t video_out_dfbmga;
130 extern vo_functions_t video_out_xvidix;
131 extern vo_functions_t video_out_winvidix;
132 extern vo_functions_t video_out_cvidix;
133 extern vo_functions_t video_out_tdfx_vid;
134 extern vo_functions_t video_out_xvr100;
135 extern vo_functions_t video_out_tga;
136 extern vo_functions_t video_out_corevideo;
137 extern vo_functions_t video_out_quartz;
138 extern vo_functions_t video_out_pnm;
139 extern vo_functions_t video_out_md5sum;
141 const vo_functions_t* const video_out_drivers[] =
143 #ifdef CONFIG_XVR100
144 &video_out_xvr100,
145 #endif
146 #ifdef CONFIG_TDFX_VID
147 &video_out_tdfx_vid,
148 #endif
149 #ifdef CONFIG_DIRECTX
150 &video_out_directx,
151 #endif
152 #ifdef CONFIG_DIRECT3D
153 &video_out_direct3d,
154 #endif
155 #ifdef CONFIG_KVA
156 &video_out_kva,
157 #endif
158 #ifdef CONFIG_COREVIDEO
159 &video_out_corevideo,
160 #endif
161 #ifdef CONFIG_QUARTZ
162 &video_out_quartz,
163 #endif
164 #ifdef CONFIG_XMGA
165 &video_out_xmga,
166 #endif
167 #ifdef CONFIG_MGA
168 &video_out_mga,
169 #endif
170 #ifdef CONFIG_TDFXFB
171 &video_out_tdfxfb,
172 #endif
173 #ifdef CONFIG_S3FB
174 &video_out_s3fb,
175 #endif
176 #ifdef CONFIG_WII
177 &video_out_wii,
178 #endif
179 #ifdef CONFIG_3DFX
180 &video_out_3dfx,
181 #endif
182 #if CONFIG_VDPAU
183 &video_out_vdpau,
184 #endif
185 #ifdef CONFIG_XV
186 &video_out_xv,
187 #endif
188 #ifdef CONFIG_X11
189 &video_out_x11,
190 &video_out_xover,
191 #endif
192 #ifdef CONFIG_GL
193 &video_out_gl,
194 &video_out_gl2,
195 #endif
196 #ifdef CONFIG_DGA
197 &video_out_dga,
198 #endif
199 #ifdef CONFIG_SDL
200 &video_out_sdl,
201 #endif
202 #ifdef CONFIG_GGI
203 &video_out_ggi,
204 #endif
205 #ifdef CONFIG_FBDEV
206 &video_out_fbdev,
207 &video_out_fbdev2,
208 #endif
209 #ifdef CONFIG_SVGALIB
210 &video_out_svga,
211 #endif
212 #ifdef CONFIG_AA
213 &video_out_aa,
214 #endif
215 #ifdef CONFIG_CACA
216 &video_out_caca,
217 #endif
218 #ifdef CONFIG_DXR2
219 &video_out_dxr2,
220 #endif
221 #ifdef CONFIG_DXR3
222 &video_out_dxr3,
223 #endif
224 #ifdef CONFIG_IVTV
225 &video_out_ivtv,
226 #endif
227 #ifdef CONFIG_V4L2_DECODER
228 &video_out_v4l2,
229 #endif
230 #ifdef CONFIG_ZR
231 &video_out_zr,
232 &video_out_zr2,
233 #endif
234 #ifdef CONFIG_BL
235 &video_out_bl,
236 #endif
237 #ifdef CONFIG_VESA
238 &video_out_vesa,
239 #endif
240 #ifdef CONFIG_DIRECTFB
241 &video_out_directfb,
242 #endif
243 #ifdef CONFIG_DFBMGA
244 &video_out_dfbmga,
245 #endif
246 #ifdef CONFIG_VIDIX
247 #ifdef CONFIG_X11
248 &video_out_xvidix,
249 #endif
250 #if defined(__MINGW32__) || defined(__CYGWIN__)
251 &video_out_winvidix,
252 #endif
253 &video_out_cvidix,
254 #endif
255 &video_out_null,
256 // should not be auto-selected
257 #if CONFIG_XVMC
258 &video_out_xvmc,
259 #endif
260 &video_out_mpegpes,
261 #ifdef CONFIG_YUV4MPEG
262 &video_out_yuv4mpeg,
263 #endif
264 #ifdef CONFIG_PNG
265 &video_out_png,
266 #endif
267 #ifdef CONFIG_JPEG
268 &video_out_jpeg,
269 #endif
270 #ifdef CONFIG_GIF
271 &video_out_gif89a,
272 #endif
273 #ifdef CONFIG_TGA
274 &video_out_tga,
275 #endif
276 #ifdef CONFIG_PNM
277 &video_out_pnm,
278 #endif
279 #ifdef CONFIG_MD5SUM
280 &video_out_md5sum,
281 #endif
282 NULL
285 void list_video_out(void){
286 int i=0;
287 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoOutputDrivers);
288 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
289 while (video_out_drivers[i]) {
290 const vo_info_t *info = video_out_drivers[i++]->info;
291 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
293 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
296 const vo_functions_t* init_best_video_out(char** vo_list){
297 int i;
298 // first try the preferred drivers, with their optional subdevice param:
299 if(vo_list && vo_list[0])
300 while(vo_list[0][0]){
301 char* vo=strdup(vo_list[0]);
302 vo_subdevice=strchr(vo,':');
303 if (!strcmp(vo, "pgm"))
304 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_PGM_HasBeenReplaced);
305 if (!strcmp(vo, "md5"))
306 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_VO_MD5_HasBeenReplaced);
307 if(vo_subdevice){
308 vo_subdevice[0]=0;
309 ++vo_subdevice;
311 for(i=0;video_out_drivers[i];i++){
312 const vo_functions_t* video_driver=video_out_drivers[i];
313 const vo_info_t *info = video_driver->info;
314 if(!strcmp(info->short_name,vo)){
315 // name matches, try it
316 if(!video_driver->preinit(vo_subdevice))
318 free(vo);
319 return video_driver; // success!
323 // continue...
324 free(vo);
325 ++vo_list;
326 if(!(vo_list[0])) return NULL; // do NOT fallback to others
328 // now try the rest...
329 vo_subdevice=NULL;
330 for(i=0;video_out_drivers[i];i++){
331 const vo_functions_t* video_driver=video_out_drivers[i];
332 if(!video_driver->preinit(vo_subdevice))
333 return video_driver; // success!
335 return NULL;
338 int config_video_out(const vo_functions_t *vo, uint32_t width, uint32_t height,
339 uint32_t d_width, uint32_t d_height, uint32_t flags,
340 char *title, uint32_t format) {
341 panscan_init();
342 aspect_save_orig(width,height);
343 aspect_save_prescale(d_width,d_height);
345 if (vo->control(VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
346 aspect(&d_width,&d_height,A_NOZOOM);
347 vo_dx = (int)(vo_screenwidth - d_width) / 2;
348 vo_dy = (int)(vo_screenheight - d_height) / 2;
349 geometry(&vo_dx, &vo_dy, &d_width, &d_height,
350 vo_screenwidth, vo_screenheight);
351 vo_dx += xinerama_x;
352 vo_dy += xinerama_y;
353 vo_dwidth = d_width;
354 vo_dheight = d_height;
357 return vo->config(width, height, d_width, d_height, flags, title, format);
361 * \brief lookup an integer in a table, table must have 0 as the last key
362 * \param key key to search for
363 * \result translation corresponding to key or "to" value of last mapping
364 * if not found.
366 int lookup_keymap_table(const struct keymap *map, int key) {
367 while (map->from && map->from != key) map++;
368 return map->to;
372 * \brief helper function for the kind of panscan-scaling that needs a source
373 * and destination rectangle like Direct3D and VDPAU
375 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
376 int *src_start, int *src_end, int *dst_start, int *dst_end) {
377 if (scaled_src_size > dst_size) {
378 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
379 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
380 border = (border / 2 + 1) & ~1;
381 *src_start = border;
382 *src_end = src_size - border;
383 *dst_start = 0;
384 *dst_end = dst_size;
385 } else {
386 *src_start = 0;
387 *src_end = src_size;
388 *dst_start = (dst_size - scaled_src_size) / 2;
389 *dst_end = *dst_start + scaled_src_size;
394 * Calculate the appropriate source and destination rectangle to
395 * get a correctly scaled picture, including pan-scan.
396 * Can be extended to take future cropping support into account.
398 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
399 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
400 * may be NULL and only left and top are currently valid.
402 void calc_src_dst_rects(int src_width, int src_height, struct vo_rect *src, struct vo_rect *dst,
403 struct vo_rect *borders, const struct vo_rect *crop) {
404 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
405 int scaled_width = 0;
406 int scaled_height = 0;
407 if (!crop) crop = &no_crop;
408 src_width -= crop->left + crop->right;
409 src_height -= crop->top + crop->bottom;
410 if (src_width < 2) src_width = 2;
411 if (src_height < 2) src_height = 2;
412 dst->left = 0; dst->right = vo_dwidth;
413 dst->top = 0; dst->bottom = vo_dheight;
414 src->left = 0; src->right = src_width;
415 src->top = 0; src->bottom = src_height;
416 if (borders) {
417 borders->left = 0; borders->top = 0;
419 if (aspect_scaling()) {
420 aspect(&scaled_width, &scaled_height, A_WINZOOM);
421 panscan_calc_windowed();
422 scaled_width += vo_panscan_x;
423 scaled_height += vo_panscan_y;
424 if (borders) {
425 borders->left = (vo_dwidth - scaled_width ) / 2;
426 borders->top = (vo_dheight - scaled_height) / 2;
428 src_dst_split_scaling(src_width, vo_dwidth, scaled_width,
429 &src->left, &src->right, &dst->left, &dst->right);
430 src_dst_split_scaling(src_height, vo_dheight, scaled_height,
431 &src->top, &src->bottom, &dst->top, &dst->bottom);
433 src->left += crop->left; src->right += crop->left;
434 src->top += crop->top; src->bottom += crop->top;
435 src->width = src->right - src->left;
436 src->height = src->bottom - src->top;
437 dst->width = dst->right - dst->left;
438 dst->height = dst->bottom - dst->top;
441 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
442 /* Borrowed from vo_fbdev.c
443 Monitor ranges related functions*/
445 char *monitor_hfreq_str = NULL;
446 char *monitor_vfreq_str = NULL;
447 char *monitor_dotclock_str = NULL;
449 float range_max(range_t *r)
451 float max = 0;
453 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
454 if (max < r->max) max = r->max;
455 return max;
459 int in_range(range_t *r, float f)
461 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
462 if (f >= r->min && f <= r->max)
463 return 1;
464 return 0;
467 range_t *str2range(char *s)
469 float tmp_min, tmp_max;
470 char *endptr = s; // to start the loop
471 range_t *r = NULL;
472 int i;
474 if (!s)
475 return NULL;
476 for (i = 0; *endptr; i++) {
477 if (*s == ',')
478 goto out_err;
479 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) {
480 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
481 return NULL;
483 tmp_min = strtod(s, &endptr);
484 if (*endptr == 'k' || *endptr == 'K') {
485 tmp_min *= 1000.0;
486 endptr++;
487 } else if (*endptr == 'm' || *endptr == 'M') {
488 tmp_min *= 1000000.0;
489 endptr++;
491 if (*endptr == '-') {
492 tmp_max = strtod(endptr + 1, &endptr);
493 if (*endptr == 'k' || *endptr == 'K') {
494 tmp_max *= 1000.0;
495 endptr++;
496 } else if (*endptr == 'm' || *endptr == 'M') {
497 tmp_max *= 1000000.0;
498 endptr++;
500 if (*endptr != ',' && *endptr)
501 goto out_err;
502 } else if (*endptr == ',' || !*endptr) {
503 tmp_max = tmp_min;
504 } else
505 goto out_err;
506 r[i].min = tmp_min;
507 r[i].max = tmp_max;
508 if (r[i].min < 0 || r[i].max < 0)
509 goto out_err;
510 s = endptr + 1;
512 r[i].min = r[i].max = -1;
513 return r;
514 out_err:
515 if (r)
516 free(r);
517 return NULL;
520 /* Borrowed from vo_fbdev.c END */
521 #endif