Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libvo / vo_dxr3.c
blob5b9d53d299dda0e534fc0d54d02063f60eb0c7a2
1 /*
2 * vo_dxr3.c - DXR3/H+ video out
4 * Copyright (C) 2002-2003 David Holm <david@realityrift.com>
6 */
8 /* ChangeLog added 2002-01-10
9 * 2003-11-28:
10 * Added a patch from Anders Rune Jensen to support the latest em8300 CVS
11 * changes.
13 * 2003-02-19:
14 * Yet another patch from Tamas Kohegyi to fix subpic placement.
16 * 2003-01-12:
17 * Added patch from Tamas Kohegyi to fix subpic placement with freetype.
19 * 2003-01-02:
20 * Added patch from Jens Axboe that makes vo_dxr3 return to previous TV norm
21 * after quiting.
22 * Added patch from Thomas Jarosch that fixed a lot of textual ouput
23 * errors.
25 * 2002-12-24: (Hohoho)
26 * Added patch from Thomas Jarosch <tomj@simonv.com> which adds support
27 * for setting the TV norm by movie framerate.
29 * 2002-11-03:
30 * Cleaned up syncing code and renamed setup variables so
31 * they can be accessed from the GUI.
33 * 2002-11-02:
34 * Added native overlay support, activate with :overlay
35 * you have to run dxr3view to modify settings (or manually
36 * edit the files in ~/.overlay.
38 * 2002-10-29:
39 * Added new sync-engine, activate with :sync option.
40 * Greatly improved commandline parser.
41 * Replaced :noprebuf with :prebuf and made noprebuf the default.
43 * 2002-10-28:
44 * Fixed multicard bug on athlons
46 * 2002-07-18:
47 * Disabled spuenc support, this is still not stable enough =(
49 * 2002-07-05:
50 * Removed lavc and fame encoder to be compatible with new libvo style.
51 * Added graphic equalizer support.
53 * 2002-04-15:
54 * The spuenc code isn't 100% stable yet, therefore I'm disabling
55 * it due to the upcoming stable release.
57 * 2002-04-03:
58 * Carl George added spuenc support
60 * 2002-03-26:
61 * XorA added an option parser and support for selecting encoder
62 * codec. We thank him again.
64 * 2002-03-25:
65 * A couple of bugfixes by XorA
67 * 2002-03-23:
68 * Thanks to Marcel Hild <hild@b4mad.net> the jitter-bug experienced
69 * with some videos have been fixed, many thanks goes to him.
71 * 2002-03-16:
72 * Fixed problems with fame, it gives a better picture than avcodec,
73 * but is slightly slower. Most notably the wobbling effect is gone
74 * with fame.
76 * 2002-03-13:
77 * Preliminary fame support added (it breaks after seeking, why?)
79 * 2002-02-18:
80 * Fixed sync problems when pausing video (while using prebuffering)
82 * 2002-02-16:
83 * Fixed bug which would case invalid output when using :noprebuf
84 * Removed equalization code, it caused problems on slow systems
86 * 2002-02-13:
87 * Using the swscaler instead of the old hand coded shit. (Checkout man mplayer and search for sws ;).
88 * Using aspect function to setup a proper mpeg1, no more hassling with odd resolutions or GOP-sizes,
89 * this would only create jitter on some vids!
90 * The swscaler sometimes exits with sig8 on mpegs, I don't know why yet (just use -vc mpegpes in this
91 * case, and report to me if you have any avi's etc which does this...)
93 * 2002-02-09:
94 * Thanks to the new control() method I have finally been able to enable the em8300 prebuffering.
95 * This should speed up playback on all systems, the vout cpu usage should rocket since I will be hogging
96 * the pci bus. Not to worry though, since frames are prebuffered it should be able to take a few blows
97 * if you start doing other stuff simultaneously.
99 * 2002-02-03:
100 * Removal of libmp1e, libavcodec has finally become faster (and it's code is helluva lot cleaner)
102 * 2002-02-02:
103 * Cleaned out some old code which might have slowed down writes
105 * 2002-01-17:
106 * Testrelease of new sync engine (using previously undocumented feature of em8300).
108 * 2002-01-15:
109 * Preliminary subpic support with -vc mpegpes and dvd's
110 * Device interfaces tries the new naming scheme by default (even though most users probably still use the old one)
112 * 2002-01-10:
113 * I rehauled the entire codebase. I have now changed to
114 * Kernighan & Ritchie codingstyle, please mail me if you
115 * find any inconcistencies.
118 #include <linux/em8300.h>
119 #include <sys/ioctl.h>
120 #include <sys/stat.h>
121 #include <sys/types.h>
122 #include <sys/select.h>
123 #include <unistd.h>
124 #include <stdio.h>
125 #include <stdlib.h>
126 #include <string.h>
127 #include <fcntl.h>
128 #include <stdio.h>
129 #include <time.h>
130 #include <math.h>
132 #include "config.h"
133 #include "mp_msg.h"
134 #include "help_mp.h"
135 #ifdef HAVE_MALLOC_H
136 #include <malloc.h>
137 #endif
138 #include "fastmemcpy.h"
140 #include "video_out.h"
141 #include "video_out_internal.h"
142 #include "aspect.h"
143 #include "spuenc.h"
144 #include "sub.h"
145 #ifdef HAVE_NEW_GUI
146 #include "gui/interface.h"
147 #endif
148 #ifdef HAVE_X11
149 #include "x11_common.h"
150 #endif
151 #include "libavutil/avstring.h"
153 #define SPU_SUPPORT
155 static const vo_info_t info =
157 "DXR3/H+ video out",
158 "dxr3",
159 "David Holm <dholm@iname.com>",
162 const LIBVO_EXTERN (dxr3)
164 /* Resolutions and positions */
165 static int v_width, v_height;
166 static int s_width, s_height;
167 static int osd_w, osd_h;
168 static int img_format;
170 /* Configuration values
171 * Don't declare these static, they
172 * should be accessible from the gui.
174 int dxr3_prebuf = 0;
175 int dxr3_newsync = 0;
176 int dxr3_overlay = 0;
177 int dxr3_device_num = 0;
178 int dxr3_norm = 0;
180 #define MAX_STR_SIZE 80 /* length for the static strings */
182 /* File descriptors */
183 static int fd_control = -1;
184 static int fd_video = -1;
185 static int fd_spu = -1;
186 static char fdv_name[MAX_STR_SIZE];
187 static char fds_name[MAX_STR_SIZE];
189 #ifdef SPU_SUPPORT
190 /* on screen display/subpics */
191 static char *osdpicbuf;
192 static int osdpicbuf_w;
193 static int osdpicbuf_h;
194 static int disposd;
195 static encodedata *spued;
196 static encodedata *spubuf;
197 #endif
200 /* Static variable used in ioctl's */
201 static int ioval;
202 static int prev_pts;
203 static int pts_offset;
204 static int old_vmode = -1;
207 /* Begin overlay.h */
209 Simple analog overlay API for DXR3/H+ linux driver.
211 Henrik Johansson
215 /* Pattern drawing callback used by the calibration functions.
216 The function is expected to:
217 Clear the entire screen.
218 Fill the screen with color bgcol (0xRRGGBB)
219 Draw a rectangle at (xpos,ypos) of size (width,height) in fgcol (0xRRGGBB)
222 typedef int (*pattern_drawer_cb)(int fgcol, int bgcol,
223 int xpos, int ypos, int width, int height, void *arg);
225 struct coeff {
226 float k,m;
229 typedef struct {
230 int dev;
232 int xres, yres,depth;
233 int xoffset,yoffset,xcorr;
234 int jitter;
235 int stability;
236 int keycolor;
237 struct coeff colcal_upper[3];
238 struct coeff colcal_lower[3];
239 float color_interval;
241 pattern_drawer_cb draw_pattern;
242 void *dp_arg;
243 } overlay_t;
246 static overlay_t *overlay_init(int dev);
247 static int overlay_release(overlay_t *);
249 static int overlay_read_state(overlay_t *o, char *path);
250 static int overlay_write_state(overlay_t *o, char *path);
252 static int overlay_set_screen(overlay_t *o, int xres, int yres, int depth);
253 static int overlay_set_mode(overlay_t *o, int mode);
254 static int overlay_set_attribute(overlay_t *o, int attribute, int val);
255 static int overlay_set_keycolor(overlay_t *o, int color);
256 static int overlay_set_window(overlay_t *o, int xpos,int ypos,int width,int height);
257 static int overlay_set_bcs(overlay_t *o, int brightness, int contrast, int saturation);
259 static int overlay_autocalibrate(overlay_t *o, pattern_drawer_cb pd, void *arg);
260 static void overlay_update_params(overlay_t *o);
261 static int overlay_signalmode(overlay_t *o, int mode);
262 /* End overlay.h */
265 #ifdef HAVE_X11
266 #define KEY_COLOR 0x80a040
267 static XWindowAttributes xwin_attribs;
268 static overlay_t *overlay_data;
269 #endif
272 /* Functions for working with the em8300's internal clock */
273 /* End of internal clock functions */
275 static int control(uint32_t request, void *data, ...)
277 switch (request) {
278 case VOCTRL_GUISUPPORT:
279 return VO_TRUE;
280 case VOCTRL_GUI_NOWINDOW:
281 if (dxr3_overlay) {
282 return VO_FALSE;
284 return VO_TRUE;
285 case VOCTRL_SET_SPU_PALETTE:
286 if (ioctl(fd_spu, EM8300_IOCTL_SPU_SETPALETTE, data) < 0) {
287 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToLoadNewSPUPalette);
288 return VO_ERROR;
290 return VO_TRUE;
291 #ifdef HAVE_X11
292 case VOCTRL_ONTOP:
293 vo_x11_ontop();
294 return VO_TRUE;
295 case VOCTRL_FULLSCREEN:
296 if (dxr3_overlay) {
297 vo_x11_fullscreen();
298 overlay_signalmode(overlay_data,
299 vo_fs ? EM8300_OVERLAY_SIGNAL_ONLY :
300 EM8300_OVERLAY_SIGNAL_WITH_VGA);
301 return VO_TRUE;
303 return VO_FALSE;
304 #endif
305 case VOCTRL_RESUME:
306 if (dxr3_newsync) {
307 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
308 pts_offset = vo_pts - (ioval << 1);
309 if (pts_offset < 0) {
310 pts_offset = 0;
314 if (dxr3_prebuf) {
315 ioval = EM8300_PLAYMODE_PLAY;
316 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
317 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
320 return VO_TRUE;
321 case VOCTRL_PAUSE:
322 if (dxr3_prebuf) {
323 ioval = EM8300_PLAYMODE_PAUSED;
324 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
325 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
328 return VO_TRUE;
329 case VOCTRL_RESET:
330 if (dxr3_prebuf) {
331 close(fd_video);
332 fd_video = open(fdv_name, O_WRONLY);
333 close(fd_spu);
334 fd_spu = open(fds_name, O_WRONLY);
335 fsync(fd_video);
336 fsync(fd_spu);
338 return VO_TRUE;
339 case VOCTRL_QUERY_FORMAT:
341 uint32_t flag = 0;
343 if (*((uint32_t*)data) != IMGFMT_MPEGPES)
344 return 0;
346 flag = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_SPU;
347 if (dxr3_prebuf)
348 flag |= VFCAP_TIMER;
349 return flag;
351 case VOCTRL_SET_EQUALIZER:
353 va_list ap;
354 int value;
355 em8300_bcs_t bcs;
357 va_start(ap, data);
358 value = va_arg(ap, int);
359 va_end(ap);
361 if (ioctl(fd_control, EM8300_IOCTL_GETBCS, &bcs) < 0)
362 return VO_FALSE;
363 if (!strcasecmp(data, "brightness"))
364 bcs.brightness = (value+100)*5;
365 else if (!strcasecmp(data, "contrast"))
366 bcs.contrast = (value+100)*5;
367 else if (!strcasecmp(data, "saturation"))
368 bcs.saturation = (value+100)*5;
369 else return VO_FALSE;
371 if (ioctl(fd_control, EM8300_IOCTL_SETBCS, &bcs) < 0)
372 return VO_FALSE;
373 return VO_TRUE;
375 case VOCTRL_GET_EQUALIZER:
377 va_list ap;
378 int *value;
379 em8300_bcs_t bcs;
381 va_start(ap, data);
382 value = va_arg(ap, int*);
383 va_end(ap);
385 if (ioctl(fd_control, EM8300_IOCTL_GETBCS, &bcs) < 0)
386 return VO_FALSE;
388 if (!strcasecmp(data, "brightness"))
389 *value = (bcs.brightness/5)-100;
390 else if (!strcasecmp(data, "contrast"))
391 *value = (bcs.contrast/5)-100;
392 else if (!strcasecmp(data, "saturation"))
393 *value = (bcs.saturation/5)-100;
394 else return VO_FALSE;
396 return VO_TRUE;
399 return VO_NOTIMPL;
402 void calculate_cvals(unsigned long mask, int *shift, int *prec)
404 /* Calculate shift and precision */
405 (*shift) = 0;
406 (*prec) = 0;
408 while (!(mask & 0x1)) {
409 (*shift)++;
410 mask >>= 1;
413 while (mask & 0x1) {
414 (*prec)++;
415 mask >>= 1;
419 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
421 int tmp1, tmp2, size;
422 em8300_register_t reg;
423 extern float monitor_aspect;
425 /* Softzoom turned on, downscale */
426 /* This activates the subpicture processor, you can safely disable this and still send */
427 /* broken subpics to the em8300, if it's enabled and you send broken subpics you will end */
428 /* up in a lockup */
429 ioval = EM8300_SPUMODE_ON;
430 if (ioctl(fd_control, EM8300_IOCTL_SET_SPUMODE, &ioval) < 0) {
431 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetSubpictureMode);
432 uninit();
433 return -1;
436 /* Set the playmode to play (just in case another app has set it to something else) */
437 ioval = EM8300_PLAYMODE_PLAY;
438 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
439 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
442 /* Start em8300 prebuffering and sync engine */
443 reg.microcode_register = 1;
444 reg.reg = 0;
445 reg.val = MVCOMMAND_SYNC;
446 ioctl(fd_control, EM8300_IOCTL_WRITEREG, &reg);
448 /* Clean buffer by syncing it */
449 ioval = EM8300_SUBDEVICE_VIDEO;
450 ioctl(fd_control, EM8300_IOCTL_FLUSH, &ioval);
451 ioval = EM8300_SUBDEVICE_AUDIO;
452 ioctl(fd_control, EM8300_IOCTL_FLUSH, &ioval);
454 /* Sync the video device to make sure the buffers are empty
455 * and set the playback speed to normal. Also reset the
456 * em8300 internal clock.
458 fsync(fd_video);
459 ioval = 0x900;
460 ioctl(fd_control, EM8300_IOCTL_SCR_SETSPEED, &ioval);
462 /* Store some variables statically that we need later in another scope */
463 img_format = format;
464 v_width = width;
465 v_height = height;
467 /* Set monitor_aspect to avoid jitter */
468 monitor_aspect = (float) width / (float) height;
470 if (ioctl(fd_control, EM8300_IOCTL_GET_VIDEOMODE, &old_vmode) < 0) {
471 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToGetTVNorm);
472 old_vmode = -1;
475 /* adjust TV norm */
476 if (dxr3_norm != 0) {
477 if (dxr3_norm == 5) {
478 ioval = EM8300_VIDEOMODE_NTSC;
479 } else if (dxr3_norm == 4) {
480 ioval = EM8300_VIDEOMODE_PAL60;
481 } else if (dxr3_norm == 3) {
482 ioval = EM8300_VIDEOMODE_PAL;
483 } else if (dxr3_norm == 2) {
484 if (vo_fps > 28) {
485 ioval = EM8300_VIDEOMODE_PAL60;
486 } else {
487 ioval = EM8300_VIDEOMODE_PAL;
490 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate);
491 ioval == EM8300_VIDEOMODE_PAL60 ? mp_msg(MSGT_VO,MSGL_INFO, "PAL-60") : mp_msg(MSGT_VO,MSGL_INFO, "PAL");
492 printf(".\n");
493 } else {
494 if (vo_fps > 28) {
495 ioval = EM8300_VIDEOMODE_NTSC;
496 } else {
497 ioval = EM8300_VIDEOMODE_PAL;
500 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate);
501 ioval == EM8300_VIDEOMODE_NTSC ? mp_msg(MSGT_VO,MSGL_INFO, "NTSC") : mp_msg(MSGT_VO,MSGL_INFO, "PAL");
502 printf(".\n");
505 if (old_vmode != ioval) {
506 if (ioctl(fd_control, EM8300_IOCTL_SET_VIDEOMODE, &ioval) < 0) {
507 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetTVNorm);
513 /* libavcodec requires a width and height that is x|16 */
514 aspect_save_orig(width, height);
515 aspect_save_prescale(d_width, d_height);
516 ioctl(fd_control, EM8300_IOCTL_GET_VIDEOMODE, &ioval);
517 if (ioval == EM8300_VIDEOMODE_NTSC) {
518 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingUpForNTSC);
519 aspect_save_screenres(352, 240);
520 } else {
521 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingUpForPALSECAM);
522 aspect_save_screenres(352, 288);
524 aspect(&s_width, &s_height, A_ZOOM);
525 s_width -= s_width % 16;
526 s_height -= s_height % 16;
528 /* Try to figure out whether to use widescreen output or not */
529 /* Anamorphic widescreen modes makes this a pain in the ass */
530 tmp1 = abs(d_height - ((d_width / 4) * 3));
531 tmp2 = abs(d_height - (int) (d_width / 2.35));
532 if (tmp1 < tmp2) {
533 ioval = EM8300_ASPECTRATIO_4_3;
534 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingAspectRatioTo43);
535 } else {
536 ioval = EM8300_ASPECTRATIO_16_9;
537 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingAspectRatioTo169);
539 ioctl(fd_control, EM8300_IOCTL_SET_ASPECTRATIO, &ioval);
541 #ifdef SPU_SUPPORT
542 #ifdef HAVE_FREETYPE
543 if (ioval == EM8300_ASPECTRATIO_16_9) {
544 s_width *= d_height*1.78/s_height*(d_width*1.0/d_height)/2.35;
545 } else {
546 s_width *= 0.84;
548 //printf("VO: [dxr3] sw/sh:dw/dh ->%i,%i,%i,%i\n",s_width,s_height,d_width,d_height);
549 #else
550 s_width*=2;
551 s_height*=2;
552 #endif
554 osdpicbuf = calloc( 1,s_width * s_height);
555 if (osdpicbuf == NULL) {
556 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
557 return -1;
559 spued = (encodedata *) malloc(sizeof(encodedata));
560 if (spued == NULL) {
561 free( osdpicbuf );
562 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
563 return -1;
565 spubuf = (encodedata *) malloc(sizeof(encodedata));
566 if (spubuf == NULL) {
567 free( osdpicbuf );
568 free( spued );
569 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
570 return -1;
572 osd_w = s_width;
573 osd_h = s_height;
574 osdpicbuf_w = s_width;
575 osdpicbuf_h = s_height;
577 spubuf->count=0;
578 pixbuf_encode_rle( 0,0,osdpicbuf_w,osdpicbuf_h - 1,osdpicbuf,osdpicbuf_w,spubuf );
580 #endif
582 #ifdef HAVE_X11
583 if (dxr3_overlay) {
584 XVisualInfo vinfo;
585 XSetWindowAttributes xswa;
586 XSizeHints hint;
587 unsigned long xswamask;
588 Colormap cmap;
589 XColor key_color;
590 Window junkwindow;
591 Screen *scr;
592 int depth, red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec, acq_color;
593 em8300_overlay_screen_t ovlscr;
594 em8300_attribute_t ovlattr;
596 vo_dx = (vo_screenwidth - d_width) / 2;
597 vo_dy = (vo_screenheight - d_height) / 2;
598 vo_dwidth = d_width;
599 vo_dheight = d_height;
600 #ifdef HAVE_NEW_GUI
601 if (use_gui) {
602 guiGetEvent(guiSetShVideo, 0);
603 XSetWindowBackground(mDisplay, vo_window, KEY_COLOR);
604 XClearWindow(mDisplay, vo_window);
605 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &xwin_attribs);
606 depth = xwin_attribs.depth;
607 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) {
608 depth = 24;
610 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
611 } else
612 #endif
614 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &xwin_attribs);
615 depth = xwin_attribs.depth;
616 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) {
617 depth = 24;
619 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
620 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy,
621 d_width, d_height, flags,
622 CopyFromParent, "Viewing Window", title);
623 xswa.background_pixel = KEY_COLOR;
624 xswa.border_pixel = 0;
625 xswamask = CWBackPixel | CWBorderPixel;
626 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
629 /* Start setting up overlay */
630 XGetWindowAttributes(mDisplay, mRootWin, &xwin_attribs);
631 overlay_set_screen(overlay_data, xwin_attribs.width, xwin_attribs.height, xwin_attribs.depth);
632 overlay_read_state(overlay_data, NULL);
634 /* Allocate keycolor */
635 cmap = vo_x11_create_colormap(&vinfo);
636 calculate_cvals(vinfo.red_mask, &red_shift, &red_prec);
637 calculate_cvals(vinfo.green_mask, &green_shift, &green_prec);
638 calculate_cvals(vinfo.blue_mask, &blue_shift, &blue_prec);
640 key_color.red = ((KEY_COLOR >> 16) & 0xff) * 256;
641 key_color.green = ((KEY_COLOR >> 8) & 0xff) * 256;
642 key_color.blue = (KEY_COLOR & 0xff) * 256;
643 key_color.pixel = (((key_color.red >> (16 - red_prec)) << red_shift) +
644 ((key_color.green >> (16 - green_prec)) << green_shift) +
645 ((key_color.blue >> (16 - blue_prec)) << blue_shift));
646 key_color.flags = DoRed | DoGreen | DoBlue;
647 if (!XAllocColor(mDisplay, cmap, &key_color)) {
648 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToAllocateKeycolor);
649 return -1;
652 acq_color = ((key_color.red / 256) << 16) | ((key_color.green / 256) << 8) | key_color.blue;
653 if (key_color.pixel != KEY_COLOR) {
654 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToAllocateExactKeycolor, key_color.pixel);
657 /* Set keycolor and activate overlay */
658 XSetWindowBackground(mDisplay, vo_window, key_color.pixel);
659 XClearWindow(mDisplay, vo_window);
660 overlay_set_keycolor(overlay_data, key_color.pixel);
661 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_OVERLAY);
662 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_RECTANGLE);
665 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
667 #endif
669 return 0;
672 static void draw_alpha(int x, int y, int w, int h, unsigned char* src, unsigned char *srca, int srcstride)
674 #ifdef SPU_SUPPORT
675 unsigned char *buf = &osdpicbuf[(y * osdpicbuf_w) + x];
676 int by = 0;
677 register int lx, ly;
678 register int stride = 0;
680 for (ly = 0; ly < h - 1; ly++)
682 for(lx = 0; lx < w; lx++ )
683 if ( ( srca[stride + lx] )&&( src[stride + lx] >= 128 ) ) buf[by + lx] = 3;
684 by+=osdpicbuf_w;
685 stride+=srcstride;
687 pixbuf_encode_rle(x, y, osdpicbuf_w, osdpicbuf_h - 1, osdpicbuf, osdpicbuf_w, spued);
688 #endif
691 extern int vo_osd_changed_flag;
692 extern mp_osd_obj_t* vo_osd_list;
694 static void draw_osd(void)
696 #ifdef SPU_SUPPORT
697 static int cleared = 0;
698 int changed = 0;
700 if ((disposd % 15) == 0)
703 mp_osd_obj_t* obj = vo_osd_list;
704 vo_update_osd( osd_w,osd_h );
705 while( obj )
707 if ( obj->flags & OSDFLAG_VISIBLE ) { changed=1; break; }
708 obj=obj->next;
711 if ( changed )
713 vo_draw_text(osd_w, osd_h, draw_alpha);
714 memset(osdpicbuf, 0, s_width * s_height);
715 cleared=0;
717 else
719 if ( !cleared )
721 spued->count=spubuf->count;
722 fast_memcpy( spued->data,spubuf->data,DATASIZE );
723 cleared=1;
728 /* could stand some check here to see if the subpic hasn't changed
729 * as if it hasn't and we re-send it it will "blink" as the last one
730 * is turned off, and the new one (same one) is turned on
732 /* Subpics are not stable yet =(
733 expect lockups if you enable */
734 #if 1
735 write(fd_spu, spued->data, spued->count);
736 #endif
738 disposd++;
739 #endif
743 static int draw_frame(uint8_t * src[])
745 vo_mpegpes_t *p = (vo_mpegpes_t *) src[0];
747 #ifdef SPU_SUPPORT
748 if (p->id == 0x20) {
749 write(fd_spu, p->data, p->size);
750 } else
751 #endif
752 write(fd_video, p->data, p->size);
753 return 0;
756 static void flip_page(void)
758 #ifdef HAVE_X11
759 if (dxr3_overlay) {
760 int event = vo_x11_check_events(mDisplay);
761 if (event & VO_EVENT_RESIZE) {
762 Window junkwindow;
763 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
764 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
765 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
767 if (event & VO_EVENT_EXPOSE) {
768 Window junkwindow;
769 XSetWindowBackground(mDisplay, vo_window, KEY_COLOR);
770 XClearWindow(mDisplay, vo_window);
771 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
772 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
773 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
776 #endif
778 if (dxr3_newsync) {
779 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
780 ioval <<= 1;
781 if (vo_pts == 0) {
782 ioval = 0;
783 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
784 pts_offset = 0;
785 } else if ((vo_pts - pts_offset) < (ioval - 7200) || (vo_pts - pts_offset) > (ioval + 7200)) {
786 ioval = (vo_pts + pts_offset) >> 1;
787 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
788 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
789 pts_offset = vo_pts - (ioval << 1);
790 if (pts_offset < 0) {
791 pts_offset = 0;
794 ioval = vo_pts + pts_offset;
795 ioctl(fd_video, EM8300_IOCTL_SPU_SETPTS, &ioval);
796 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &ioval);
797 prev_pts = vo_pts;
798 } else if (dxr3_prebuf) {
799 ioctl(fd_spu, EM8300_IOCTL_SPU_SETPTS, &vo_pts);
800 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &vo_pts);
804 static int draw_slice(uint8_t *srcimg[], int stride[], int w, int h, int x0, int y0)
806 return -1;
809 static void uninit(void)
811 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Uninitializing);
812 #ifdef HAVE_X11
813 if (dxr3_overlay) {
814 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_OFF);
815 overlay_release(overlay_data);
817 #ifdef HAVE_NEW_GUI
818 if (!use_gui) {
819 #endif
820 vo_x11_uninit();
822 #ifdef HAVE_NEW_GUI
824 #endif
826 #endif
827 if (old_vmode != -1) {
828 if (ioctl(fd_control, EM8300_IOCTL_SET_VIDEOMODE, &old_vmode) < 0) {
829 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedRestoringTVNorm);
833 if (fd_video) {
834 close(fd_video);
836 if (fd_spu) {
837 close(fd_spu);
839 if (fd_control) {
840 close(fd_control);
842 #ifdef SPU_SUPPORT
843 if(osdpicbuf) {
844 free(osdpicbuf);
846 if(spued) {
847 free(spued);
849 #endif
852 static void check_events(void)
856 static int preinit(const char *arg)
858 char devname[MAX_STR_SIZE];
859 int fdflags = O_WRONLY;
861 /* Parse commandline */
862 while (arg) {
863 if (!strncmp("prebuf", arg, 6) && !dxr3_prebuf) {
864 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_EnablingPrebuffering);
865 dxr3_prebuf = 1;
866 } else if (!strncmp("sync", arg, 4) && !dxr3_newsync) {
867 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingNewSyncEngine);
868 dxr3_newsync = 1;
869 } else if (!strncmp("overlay", arg, 7) && !dxr3_overlay) {
870 #ifdef HAVE_X11
871 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingOverlay);
872 dxr3_overlay = 1;
873 #else
874 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorYouNeedToCompileMplayerWithX11);
875 #endif
876 } else if (!strncmp("norm=", arg, 5)) {
877 arg += 5;
878 // dxr3_norm is 0 (-> don't change norm) by default
879 // but maybe someone changes this in the future
881 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_WillSetTVNormTo);
883 if (*arg == '5') {
884 dxr3_norm = 5;
885 mp_msg(MSGT_VO,MSGL_INFO, "NTSC");
886 } else if (*arg == '4') {
887 dxr3_norm = 4;
888 mp_msg(MSGT_VO,MSGL_INFO, "PAL-60");
889 } else if (*arg == '3') {
890 dxr3_norm = 3;
891 mp_msg(MSGT_VO,MSGL_INFO, "PAL");
892 } else if (*arg == '2') {
893 dxr3_norm = 2;
894 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALPAL60);
895 } else if (*arg == '1') {
896 dxr3_norm = 1;
897 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALNTSC);
898 } else if (*arg == '0') {
899 dxr3_norm = 0;
900 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseCurrentNorm);
901 } else {
902 dxr3_norm = 0;
903 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseUnknownNormSuppliedCurrentNorm);
906 mp_msg(MSGT_VO,MSGL_INFO, ".\n");
907 } else if (arg[0] == '0' || arg[0] == '1' || arg[0] == '2' || arg[0] == '3') {
908 dxr3_device_num = arg[0];
911 arg = strchr(arg, ':');
912 if (arg) {
913 arg++;
918 /* Open the control interface */
919 sprintf(devname, "/dev/em8300-%d", dxr3_device_num);
920 fd_control = open(devname, fdflags);
921 if (fd_control < 1) {
922 /* Fall back to old naming scheme */
923 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTrying, devname);
924 sprintf(devname, "/dev/em8300");
925 fd_control = open(devname, fdflags);
926 if (fd_control < 1) {
927 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWell);
928 return -1;
930 } else {
931 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
934 /* Open the video interface */
935 sprintf(devname, "/dev/em8300_mv-%d", dxr3_device_num);
936 fd_video = open(devname, fdflags);
937 if (fd_video < 0) {
938 /* Fall back to old naming scheme */
939 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingMV, devname);
940 sprintf(devname, "/dev/em8300_mv");
941 fd_video = open(devname, fdflags);
942 if (fd_video < 0) {
943 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellMV);
944 uninit();
945 return -1;
947 } else {
948 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
950 strcpy(fdv_name, devname);
952 /* Open the subpicture interface */
953 sprintf(devname, "/dev/em8300_sp-%d", dxr3_device_num);
954 fd_spu = open(devname, fdflags);
955 if (fd_spu < 0) {
956 /* Fall back to old naming scheme */
957 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingSP, devname);
958 sprintf(devname, "/dev/em8300_sp");
959 fd_spu = open(devname, fdflags);
960 if (fd_spu < 0) {
961 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellSP);
962 uninit();
963 return -1;
965 } else {
966 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
968 strcpy(fds_name, devname);
970 #ifdef HAVE_X11
971 if (dxr3_overlay) {
973 /* Fucked up hack needed to enable overlay.
974 * Will be removed as soon as I figure out
975 * how to make it work like it should
977 Display *dpy;
978 overlay_t *ov;
979 XWindowAttributes attribs;
981 dpy = XOpenDisplay(NULL);
982 if (!dpy) {
983 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToOpenDisplayDuringHackSetup);
984 return -1;
986 XGetWindowAttributes(dpy, RootWindow(dpy, DefaultScreen(dpy)), &attribs);
987 ov = overlay_init(fd_control);
988 overlay_set_screen(ov, attribs.width, attribs.height, PlanesOfScreen(ScreenOfDisplay(dpy, 0)));
989 overlay_read_state(ov, NULL);
990 overlay_set_keycolor(ov, KEY_COLOR);
991 overlay_set_mode(ov, EM8300_OVERLAY_MODE_OVERLAY);
992 overlay_set_mode(ov, EM8300_OVERLAY_MODE_RECTANGLE);
993 overlay_release(ov);
994 XCloseDisplay(dpy);
995 /* End of fucked up hack */
997 /* Initialize overlay and X11 */
998 overlay_data = overlay_init(fd_control);
999 #ifdef HAVE_NEW_GUI
1000 if (!use_gui) {
1001 #endif
1002 if (!vo_init()) {
1003 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToInitX11);
1004 return -1;
1006 #ifdef HAVE_NEW_GUI
1008 #endif
1010 #endif
1012 if (dxr3_newsync) {
1013 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
1014 pts_offset = vo_pts - (ioval << 1);
1015 if (pts_offset < 0) {
1016 pts_offset = 0;
1020 return 0;
1023 /* Begin overlay.c */
1024 static int update_parameters(overlay_t *o)
1026 overlay_set_attribute(o, EM9010_ATTRIBUTE_XOFFSET, o->xoffset);
1027 overlay_set_attribute(o, EM9010_ATTRIBUTE_YOFFSET, o->yoffset);
1028 overlay_set_attribute(o, EM9010_ATTRIBUTE_XCORR, o->xcorr);
1029 overlay_set_attribute(o, EM9010_ATTRIBUTE_STABILITY, o->stability);
1030 overlay_set_attribute(o, EM9010_ATTRIBUTE_JITTER, o->jitter);
1031 return 0;
1034 static int overlay_set_attribute(overlay_t *o, int attribute, int value)
1036 em8300_attribute_t attr;
1038 attr.attribute = attribute;
1039 attr.value = value;
1040 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SET_ATTRIBUTE, &attr)==-1)
1042 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayAttribute);
1043 return -1;
1046 return 0;
1049 static overlay_t *overlay_init(int dev)
1051 overlay_t *o;
1053 o = (overlay_t *) malloc(sizeof(overlay_t));
1055 if(!o)
1056 return NULL;
1058 memset(o,0,sizeof(overlay_t));
1060 o->dev = dev;
1061 o->xres = 1280; o->yres=1024; o->xcorr=1000;
1062 o->color_interval=10;
1064 return o;
1067 static int overlay_release(overlay_t *o)
1069 if(o)
1070 free(o);
1072 return 0;
1074 #define TYPE_INT 1
1075 #define TYPE_XINT 2
1076 #define TYPE_COEFF 3
1077 #define TYPE_FLOAT 4
1079 struct lut_entry {
1080 char *name;
1081 int type;
1082 void *ptr;
1085 static struct lut_entry *new_lookuptable(overlay_t *o)
1087 struct lut_entry m[] = {
1088 {"xoffset", TYPE_INT, &o->xoffset},
1089 {"yoffset", TYPE_INT, &o->yoffset},
1090 {"xcorr", TYPE_INT, &o->xcorr},
1091 {"jitter", TYPE_INT, &o->jitter},
1092 {"stability", TYPE_INT, &o->stability},
1093 {"keycolor", TYPE_XINT, &o->keycolor},
1094 {"colcal_upper", TYPE_COEFF, &o->colcal_upper[0]},
1095 {"colcal_lower", TYPE_COEFF, &o->colcal_lower[0]},
1096 {"color_interval", TYPE_FLOAT, &o->color_interval},
1097 {0,0,0}
1098 },*p;
1100 p = malloc(sizeof(m));
1101 memcpy(p,m,sizeof(m));
1102 return p;
1105 static int lookup_parameter(overlay_t *o, struct lut_entry *lut, char *name, void **ptr, int *type) {
1106 int i;
1108 for(i=0; lut[i].name; i++) {
1109 if(!strcmp(name,lut[i].name)) {
1110 *ptr = lut[i].ptr;
1111 *type = lut[i].type;
1112 return 1;
1115 return 0;
1118 static int overlay_read_state(overlay_t *o, char *p)
1120 char *a,*tok;
1121 char path[128],fname[128],tmp[128],line[256];
1122 FILE *fp;
1123 struct lut_entry *lut;
1124 void *ptr;
1125 int type;
1126 int j;
1128 if(!p) {
1129 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1130 av_strlcat(fname,"/.overlay", sizeof( fname ));
1131 } else
1132 av_strlcpy(fname, p, sizeof( fname ));
1134 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1135 av_strlcat(fname, tmp, sizeof( fname ));
1137 if(!(fp=fopen(fname,"r")))
1138 return -1;
1140 lut = new_lookuptable(o);
1142 while(!feof(fp)) {
1143 if(!fgets(line,256,fp))
1144 break;
1145 tok=strtok(line," ");
1146 if(lookup_parameter(o,lut,tok,&ptr,&type)) {
1147 tok=strtok(NULL," ");
1148 switch(type) {
1149 case TYPE_INT:
1150 sscanf(tok,"%d",(int *)ptr);
1151 break;
1152 case TYPE_XINT:
1153 sscanf(tok,"%x",(int *)ptr);
1154 break;
1155 case TYPE_FLOAT:
1156 sscanf(tok,"%f",(float *)ptr);
1157 break;
1158 case TYPE_COEFF:
1159 for(j=0;j<3;j++) {
1160 sscanf(tok,"%f",&((struct coeff *)ptr)[j].k);
1161 tok=strtok(NULL," ");
1162 sscanf(tok,"%f",&((struct coeff *)ptr)[j].m);
1163 tok=strtok(NULL," ");
1165 break;
1171 update_parameters(o);
1173 free(lut);
1174 fclose(fp);
1175 return 0;
1178 static void overlay_update_params(overlay_t *o) {
1179 update_parameters(o);
1182 static int overlay_write_state(overlay_t *o, char *p)
1184 char *a;
1185 char path[128],fname[128],tmp[128];
1186 FILE *fp;
1187 char line[256],*tok;
1188 struct lut_entry *lut;
1189 int i,j;
1191 if(!p) {
1192 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1193 av_strlcat(fname,"/.overlay", sizeof( fname ));
1194 } else
1195 av_strlcpy(fname, p, sizeof( fname ));
1197 if(access(fname, W_OK|X_OK|R_OK)) {
1198 if(mkdir(fname,0766))
1199 return -1;
1202 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1203 av_strlcat(fname, tmp, sizeof( fname ));
1205 if(!(fp=fopen(fname,"w")))
1206 return -1;
1208 lut = new_lookuptable(o);
1210 for(i=0; lut[i].name; i++) {
1211 fprintf(fp,"%s ",lut[i].name);
1212 switch(lut[i].type) {
1213 case TYPE_INT:
1214 fprintf(fp,"%d\n",*(int *)lut[i].ptr);
1215 break;
1216 case TYPE_XINT:
1217 fprintf(fp,"%06x\n",*(int *)lut[i].ptr);
1218 break;
1219 case TYPE_FLOAT:
1220 fprintf(fp,"%f\n",*(float *)lut[i].ptr);
1221 break;
1222 case TYPE_COEFF:
1223 for(j=0;j<3;j++)
1224 fprintf(fp,"%f %f ",((struct coeff *)lut[i].ptr)[j].k,
1225 ((struct coeff *)lut[i].ptr)[j].m);
1226 fprintf(fp,"\n");
1227 break;
1231 fclose(fp);
1232 return 0;
1235 static int overlay_set_screen(overlay_t *o, int xres, int yres, int depth)
1237 em8300_overlay_screen_t scr;
1239 o->xres = xres;
1240 o->yres = yres;
1241 o->depth = depth;
1243 scr.xsize = xres;
1244 scr.ysize = yres;
1246 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETSCREEN, &scr)==-1)
1248 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayScreen);
1249 return -1;
1251 return 0;
1254 static int overlay_set_mode(overlay_t *o, int mode)
1256 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETMODE, &mode)==-1) {
1257 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedEnablingOverlay);
1258 return -1;
1260 return 0;
1263 static int overlay_set_window(overlay_t *o, int xpos,int ypos,int width,int height)
1265 em8300_overlay_window_t win;
1266 win.xpos = xpos;
1267 win.ypos = ypos;
1268 win.width = width;
1269 win.height = height;
1271 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1)
1273 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1274 return -1;
1276 return 0;
1279 static int overlay_set_bcs(overlay_t *o, int brightness, int contrast, int saturation)
1281 em8300_bcs_t bcs;
1282 bcs.brightness = brightness;
1283 bcs.contrast = contrast;
1284 bcs.saturation = saturation;
1286 if (ioctl(o->dev, EM8300_IOCTL_GETBCS, &bcs)==-1)
1288 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayBcs);
1289 return -1;
1291 return 0;
1294 static int col_interp(float x, struct coeff c)
1296 float y;
1297 y = x*c.k + c.m;
1298 if(y > 255)
1299 y = 255;
1300 if(y < 0)
1301 y = 0;
1302 return rint(y);
1305 static int overlay_set_keycolor(overlay_t *o, int color) {
1306 int r = (color & 0xff0000) >> 16;
1307 int g = (color & 0x00ff00) >> 8;
1308 int b = (color & 0x0000ff);
1309 float ru,gu,bu;
1310 float rl,gl,bl;
1311 int upper,lower;
1313 ru = r+o->color_interval;
1314 gu = g+o->color_interval;
1315 bu = b+o->color_interval;
1317 rl = r-o->color_interval;
1318 gl = g-o->color_interval;
1319 bl = b-o->color_interval;
1321 upper = (col_interp(ru, o->colcal_upper[0]) << 16) |
1322 (col_interp(gu, o->colcal_upper[1]) << 8) |
1323 (col_interp(bu, o->colcal_upper[2]));
1325 lower = (col_interp(rl, o->colcal_lower[0]) << 16) |
1326 (col_interp(gl, o->colcal_lower[1]) << 8) |
1327 (col_interp(bl, o->colcal_lower[2]));
1329 //printf("0x%06x 0x%06x\n",upper,lower);
1330 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_UPPER,upper);
1331 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_LOWER,lower);
1332 return 0;
1335 static void least_sq_fit(int *x, int *y, int n, float *k, float *m)
1337 float sx=0,sy=0,sxx=0,sxy=0;
1338 float delta,b;
1339 int i;
1341 for(i=0; i < n; i++) {
1342 sx=sx+x[i];
1343 sy=sy+y[i];
1344 sxx=sxx+x[i]*x[i];
1345 sxy=sxy+x[i]*y[i];
1348 delta=sxx*n-sx*sx;
1350 *m=(sxx*sy-sx*sxy)/delta;
1351 *k=(sxy*n-sx*sy)/delta;
1354 static int overlay_autocalibrate(overlay_t *o, pattern_drawer_cb pd, void *arg)
1356 em8300_overlay_calibrate_t cal;
1357 em8300_overlay_window_t win;
1358 int x[256],r[256],g[256],b[256],n;
1359 float k,m;
1361 int i;
1363 o->draw_pattern=pd;
1364 o->dp_arg = arg;
1366 overlay_set_mode(o, EM8300_OVERLAY_MODE_OVERLAY);
1367 overlay_set_screen(o, o->xres, o->yres, o->depth);
1369 /* Calibrate Y-offset */
1371 o->draw_pattern(0x0000ff, 0, 0, 0, 355, 1, o->dp_arg);
1373 cal.cal_mode = EM8300_OVERLAY_CALMODE_YOFFSET;
1374 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1376 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayYOffsetValues);
1377 return -1;
1379 o->yoffset = cal.result;
1380 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_YOffset,cal.result);
1382 /* Calibrate X-offset */
1384 o->draw_pattern(0x0000ff, 0, 0, 0, 2, 288, o->dp_arg);
1386 cal.cal_mode = EM8300_OVERLAY_CALMODE_XOFFSET;
1387 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1389 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXOffsetValues);
1390 return -1;
1392 o->xoffset = cal.result;
1393 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XOffset,cal.result);
1395 /* Calibrate X scale correction */
1397 o->draw_pattern(0x0000ff, 0, 355, 0, 2, 288, o->dp_arg);
1399 cal.cal_mode = EM8300_OVERLAY_CALMODE_XCORRECTION;
1400 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1402 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXScaleCorrection);
1403 return -1;
1405 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XCorrection,cal.result);
1406 o->xcorr = cal.result;
1408 win.xpos = 10;
1409 win.ypos = 10;
1410 win.width = o->xres-20;
1411 win.height = o->yres-20;
1412 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1) {
1413 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1414 exit(1);
1417 /* Calibrate key color upper limit */
1419 for(i=128,n=0; i <= 0xff; i+=4) {
1420 o->draw_pattern(i | (i << 8) | (i << 16), 0,
1421 (o->xres-200)/2,0,200,o->yres,o->dp_arg);
1423 cal.arg = i;
1424 cal.arg2 = 1;
1425 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1427 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1429 return -1 ;
1432 x[n] = i;
1433 r[n] = (cal.result>>16)&0xff;
1434 g[n] = (cal.result>>8)&0xff;
1435 b[n] = (cal.result)&0xff;
1436 n++;
1439 least_sq_fit(x,r,n,&o->colcal_upper[0].k,&o->colcal_upper[0].m);
1440 least_sq_fit(x,g,n,&o->colcal_upper[1].k,&o->colcal_upper[1].m);
1441 least_sq_fit(x,b,n,&o->colcal_upper[2].k,&o->colcal_upper[2].m);
1443 /* Calibrate key color lower limit */
1445 for(i=128,n=0; i <= 0xff; i+=4) {
1446 o->draw_pattern(i | (i << 8) | (i << 16), 0xffffff,
1447 (o->xres-200)/2,0,200,o->yres, o->dp_arg);
1449 cal.arg = i;
1450 cal.arg2 = 2;
1451 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1453 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1455 return -1 ;
1457 x[n] = i;
1458 r[n] = (cal.result>>16)&0xff;
1459 g[n] = (cal.result>>8)&0xff;
1460 b[n] = (cal.result)&0xff;
1461 n++;
1464 least_sq_fit(x,r,n,&o->colcal_lower[0].k,&o->colcal_lower[0].m);
1465 least_sq_fit(x,g,n,&o->colcal_lower[1].k,&o->colcal_lower[1].m);
1466 least_sq_fit(x,b,n,&o->colcal_lower[2].k,&o->colcal_lower[2].m);
1468 overlay_set_mode(o, EM8300_OVERLAY_MODE_OFF);
1470 return 0;
1474 static int overlay_signalmode(overlay_t *o, int mode) {
1475 if(ioctl(o->dev, EM8300_IOCTL_OVERLAY_SIGNALMODE, &mode) ==-1) {
1476 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSetSignalMix);
1477 return -1;
1479 return 0;
1481 /* End overlay.c */