Fix dvdnav call broken in pause changes
[mplayer.git] / libvo / vo_dxr3.c
blob3fa0ed9c6dbec1793cd0046cfc8f2ec9ac4252cd
1 /*
2 * vo_dxr3.c - DXR3/H+ video out
4 * Copyright (C) 2002-2003 David Holm <david@realityrift.com>
6 */
8 #include <linux/em8300.h>
9 #include <sys/ioctl.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <sys/select.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <time.h>
20 #include <math.h>
22 #include "config.h"
23 #include "mp_msg.h"
24 #include "help_mp.h"
25 #ifdef HAVE_MALLOC_H
26 #include <malloc.h>
27 #endif
28 #include "fastmemcpy.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
32 #include "aspect.h"
33 #include "spuenc.h"
34 #include "sub.h"
35 #ifdef CONFIG_GUI
36 #include "gui/interface.h"
37 #endif
38 #ifdef CONFIG_X11
39 #include "x11_common.h"
40 #endif
41 #include "libavutil/avstring.h"
43 #define SPU_SUPPORT
45 static const vo_info_t info =
47 "DXR3/H+ video out",
48 "dxr3",
49 "David Holm <dholm@iname.com>",
52 const LIBVO_EXTERN (dxr3)
54 /* Resolutions and positions */
55 static int v_width, v_height;
56 static int s_width, s_height;
57 static int osd_w, osd_h;
58 static int img_format;
60 /* Configuration values
61 * Don't declare these static, they
62 * should be accessible from the gui.
64 int dxr3_prebuf = 0;
65 int dxr3_newsync = 0;
66 int dxr3_overlay = 0;
67 int dxr3_device_num = 0;
68 int dxr3_norm = 0;
70 #define MAX_STR_SIZE 80 /* length for the static strings */
72 /* File descriptors */
73 static int fd_control = -1;
74 static int fd_video = -1;
75 static int fd_spu = -1;
76 static char fdv_name[MAX_STR_SIZE];
77 static char fds_name[MAX_STR_SIZE];
79 #ifdef SPU_SUPPORT
80 /* on screen display/subpics */
81 static char *osdpicbuf;
82 static int osdpicbuf_w;
83 static int osdpicbuf_h;
84 static int disposd;
85 static encodedata *spued;
86 static encodedata *spubuf;
87 #endif
90 /* Static variable used in ioctl's */
91 static int ioval;
92 static int prev_pts;
93 static int pts_offset;
94 static int old_vmode = -1;
97 /* Begin overlay.h */
99 Simple analog overlay API for DXR3/H+ linux driver.
101 Henrik Johansson
105 /* Pattern drawing callback used by the calibration functions.
106 The function is expected to:
107 Clear the entire screen.
108 Fill the screen with color bgcol (0xRRGGBB)
109 Draw a rectangle at (xpos,ypos) of size (width,height) in fgcol (0xRRGGBB)
112 typedef int (*pattern_drawer_cb)(int fgcol, int bgcol,
113 int xpos, int ypos, int width, int height, void *arg);
115 struct coeff {
116 float k,m;
119 typedef struct {
120 int dev;
122 int xres, yres,depth;
123 int xoffset,yoffset,xcorr;
124 int jitter;
125 int stability;
126 int keycolor;
127 struct coeff colcal_upper[3];
128 struct coeff colcal_lower[3];
129 float color_interval;
131 pattern_drawer_cb draw_pattern;
132 void *dp_arg;
133 } overlay_t;
136 static overlay_t *overlay_init(int dev);
137 static int overlay_release(overlay_t *);
139 static int overlay_read_state(overlay_t *o, char *path);
140 static int overlay_write_state(overlay_t *o, char *path);
142 static int overlay_set_screen(overlay_t *o, int xres, int yres, int depth);
143 static int overlay_set_mode(overlay_t *o, int mode);
144 static int overlay_set_attribute(overlay_t *o, int attribute, int val);
145 static int overlay_set_keycolor(overlay_t *o, int color);
146 static int overlay_set_window(overlay_t *o, int xpos,int ypos,int width,int height);
147 static int overlay_set_bcs(overlay_t *o, int brightness, int contrast, int saturation);
149 static int overlay_autocalibrate(overlay_t *o, pattern_drawer_cb pd, void *arg);
150 static void overlay_update_params(overlay_t *o);
151 static int overlay_signalmode(overlay_t *o, int mode);
152 /* End overlay.h */
155 #ifdef CONFIG_X11
156 #define KEY_COLOR 0x80a040
157 static XWindowAttributes xwin_attribs;
158 static overlay_t *overlay_data;
159 #endif
162 /* Functions for working with the em8300's internal clock */
163 /* End of internal clock functions */
165 static int control(uint32_t request, void *data)
167 switch (request) {
168 case VOCTRL_GUISUPPORT:
169 return VO_TRUE;
170 case VOCTRL_GUI_NOWINDOW:
171 if (dxr3_overlay) {
172 return VO_FALSE;
174 return VO_TRUE;
175 case VOCTRL_SET_SPU_PALETTE:
176 if (ioctl(fd_spu, EM8300_IOCTL_SPU_SETPALETTE, data) < 0) {
177 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToLoadNewSPUPalette);
178 return VO_ERROR;
180 return VO_TRUE;
181 #ifdef CONFIG_X11
182 case VOCTRL_ONTOP:
183 vo_x11_ontop();
184 return VO_TRUE;
185 case VOCTRL_FULLSCREEN:
186 if (dxr3_overlay) {
187 vo_x11_fullscreen();
188 overlay_signalmode(overlay_data,
189 vo_fs ? EM8300_OVERLAY_SIGNAL_ONLY :
190 EM8300_OVERLAY_SIGNAL_WITH_VGA);
191 return VO_TRUE;
193 return VO_FALSE;
194 #endif
195 case VOCTRL_RESUME:
196 if (dxr3_newsync) {
197 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
198 pts_offset = vo_pts - (ioval << 1);
199 if (pts_offset < 0) {
200 pts_offset = 0;
204 if (dxr3_prebuf) {
205 ioval = EM8300_PLAYMODE_PLAY;
206 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
207 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
210 return VO_TRUE;
211 case VOCTRL_PAUSE:
212 if (dxr3_prebuf) {
213 ioval = EM8300_PLAYMODE_PAUSED;
214 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
215 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
218 return VO_TRUE;
219 case VOCTRL_RESET:
220 if (dxr3_prebuf) {
221 close(fd_video);
222 fd_video = open(fdv_name, O_WRONLY);
223 close(fd_spu);
224 fd_spu = open(fds_name, O_WRONLY);
225 fsync(fd_video);
226 fsync(fd_spu);
228 return VO_TRUE;
229 case VOCTRL_QUERY_FORMAT:
231 uint32_t flag = 0;
233 if (*((uint32_t*)data) != IMGFMT_MPEGPES)
234 return 0;
236 flag = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_SPU;
237 if (dxr3_prebuf)
238 flag |= VFCAP_TIMER;
239 return flag;
241 case VOCTRL_SET_EQUALIZER:
243 em8300_bcs_t bcs;
244 struct voctrl_set_equalizer_args *args = data;
246 if (ioctl(fd_control, EM8300_IOCTL_GETBCS, &bcs) < 0)
247 return VO_FALSE;
248 if (!strcasecmp(args->name, "brightness"))
249 bcs.brightness = (args->value+100)*5;
250 else if (!strcasecmp(args->name, "contrast"))
251 bcs.contrast = (args->value+100)*5;
252 else if (!strcasecmp(args->name, "saturation"))
253 bcs.saturation = (args->value+100)*5;
254 else return VO_FALSE;
256 if (ioctl(fd_control, EM8300_IOCTL_SETBCS, &bcs) < 0)
257 return VO_FALSE;
258 return VO_TRUE;
260 case VOCTRL_GET_EQUALIZER:
262 em8300_bcs_t bcs;
263 struct voctrl_get_equalizer_args *args = data;
265 if (ioctl(fd_control, EM8300_IOCTL_GETBCS, &bcs) < 0)
266 return VO_FALSE;
268 if (!strcasecmp(args->name, "brightness"))
269 *args->valueptr = (bcs.brightness/5)-100;
270 else if (!strcasecmp(args->name, "contrast"))
271 *args->valueptr = (bcs.contrast/5)-100;
272 else if (!strcasecmp(args->name, "saturation"))
273 *args->valueptr = (bcs.saturation/5)-100;
274 else return VO_FALSE;
276 return VO_TRUE;
279 return VO_NOTIMPL;
282 void calculate_cvals(unsigned long mask, int *shift, int *prec)
284 /* Calculate shift and precision */
285 (*shift) = 0;
286 (*prec) = 0;
288 while (!(mask & 0x1)) {
289 (*shift)++;
290 mask >>= 1;
293 while (mask & 0x1) {
294 (*prec)++;
295 mask >>= 1;
299 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)
301 int tmp1, tmp2, size;
302 em8300_register_t reg;
304 /* Softzoom turned on, downscale */
305 /* This activates the subpicture processor, you can safely disable this and still send */
306 /* broken subpics to the em8300, if it's enabled and you send broken subpics you will end */
307 /* up in a lockup */
308 ioval = EM8300_SPUMODE_ON;
309 if (ioctl(fd_control, EM8300_IOCTL_SET_SPUMODE, &ioval) < 0) {
310 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetSubpictureMode);
311 uninit();
312 return -1;
315 /* Set the playmode to play (just in case another app has set it to something else) */
316 ioval = EM8300_PLAYMODE_PLAY;
317 if (ioctl(fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval) < 0) {
318 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetPlaymode);
321 /* Start em8300 prebuffering and sync engine */
322 reg.microcode_register = 1;
323 reg.reg = 0;
324 reg.val = MVCOMMAND_SYNC;
325 ioctl(fd_control, EM8300_IOCTL_WRITEREG, &reg);
327 /* Clean buffer by syncing it */
328 ioval = EM8300_SUBDEVICE_VIDEO;
329 ioctl(fd_control, EM8300_IOCTL_FLUSH, &ioval);
330 ioval = EM8300_SUBDEVICE_AUDIO;
331 ioctl(fd_control, EM8300_IOCTL_FLUSH, &ioval);
333 /* Sync the video device to make sure the buffers are empty
334 * and set the playback speed to normal. Also reset the
335 * em8300 internal clock.
337 fsync(fd_video);
338 ioval = 0x900;
339 ioctl(fd_control, EM8300_IOCTL_SCR_SETSPEED, &ioval);
341 /* Store some variables statically that we need later in another scope */
342 img_format = format;
343 v_width = width;
344 v_height = height;
346 /* Set monitor_aspect to avoid jitter */
347 monitor_aspect = (float) width / (float) height;
349 if (ioctl(fd_control, EM8300_IOCTL_GET_VIDEOMODE, &old_vmode) < 0) {
350 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToGetTVNorm);
351 old_vmode = -1;
354 /* adjust TV norm */
355 if (dxr3_norm != 0) {
356 if (dxr3_norm == 5) {
357 ioval = EM8300_VIDEOMODE_NTSC;
358 } else if (dxr3_norm == 4) {
359 ioval = EM8300_VIDEOMODE_PAL60;
360 } else if (dxr3_norm == 3) {
361 ioval = EM8300_VIDEOMODE_PAL;
362 } else if (dxr3_norm == 2) {
363 if (vo_fps > 28) {
364 ioval = EM8300_VIDEOMODE_PAL60;
365 } else {
366 ioval = EM8300_VIDEOMODE_PAL;
369 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate);
370 ioval == EM8300_VIDEOMODE_PAL60 ? mp_msg(MSGT_VO,MSGL_INFO, "PAL-60") : mp_msg(MSGT_VO,MSGL_INFO, "PAL");
371 printf(".\n");
372 } else {
373 if (vo_fps > 28) {
374 ioval = EM8300_VIDEOMODE_NTSC;
375 } else {
376 ioval = EM8300_VIDEOMODE_PAL;
379 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate);
380 ioval == EM8300_VIDEOMODE_NTSC ? mp_msg(MSGT_VO,MSGL_INFO, "NTSC") : mp_msg(MSGT_VO,MSGL_INFO, "PAL");
381 printf(".\n");
384 if (old_vmode != ioval) {
385 if (ioctl(fd_control, EM8300_IOCTL_SET_VIDEOMODE, &ioval) < 0) {
386 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToSetTVNorm);
392 /* libavcodec requires a width and height that is x|16 */
393 aspect_save_orig(width, height);
394 aspect_save_prescale(d_width, d_height);
395 ioctl(fd_control, EM8300_IOCTL_GET_VIDEOMODE, &ioval);
396 if (ioval == EM8300_VIDEOMODE_NTSC) {
397 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingUpForNTSC);
398 aspect_save_screenres(352, 240);
399 } else {
400 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingUpForPALSECAM);
401 aspect_save_screenres(352, 288);
403 aspect(&s_width, &s_height, A_ZOOM);
404 s_width -= s_width % 16;
405 s_height -= s_height % 16;
407 /* Try to figure out whether to use widescreen output or not */
408 /* Anamorphic widescreen modes makes this a pain in the ass */
409 tmp1 = abs(d_height - ((d_width / 4) * 3));
410 tmp2 = abs(d_height - (int) (d_width / 2.35));
411 if (tmp1 < tmp2) {
412 ioval = EM8300_ASPECTRATIO_4_3;
413 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingAspectRatioTo43);
414 } else {
415 ioval = EM8300_ASPECTRATIO_16_9;
416 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_SettingAspectRatioTo169);
418 ioctl(fd_control, EM8300_IOCTL_SET_ASPECTRATIO, &ioval);
420 #ifdef SPU_SUPPORT
421 #ifdef CONFIG_FREETYPE
422 if (ioval == EM8300_ASPECTRATIO_16_9) {
423 s_width *= d_height*1.78/s_height*(d_width*1.0/d_height)/2.35;
424 } else {
425 s_width *= 0.84;
427 //printf("VO: [dxr3] sw/sh:dw/dh ->%i,%i,%i,%i\n",s_width,s_height,d_width,d_height);
428 #else
429 s_width*=2;
430 s_height*=2;
431 #endif
433 osdpicbuf = calloc( 1,s_width * s_height);
434 if (osdpicbuf == NULL) {
435 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
436 return -1;
438 spued = (encodedata *) malloc(sizeof(encodedata));
439 if (spued == NULL) {
440 free( osdpicbuf );
441 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
442 return -1;
444 spubuf = (encodedata *) malloc(sizeof(encodedata));
445 if (spubuf == NULL) {
446 free( osdpicbuf );
447 free( spued );
448 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_OutOfMemory);
449 return -1;
451 osd_w = s_width;
452 osd_h = s_height;
453 osdpicbuf_w = s_width;
454 osdpicbuf_h = s_height;
456 spubuf->count=0;
457 pixbuf_encode_rle( 0,0,osdpicbuf_w,osdpicbuf_h - 1,osdpicbuf,osdpicbuf_w,spubuf );
459 #endif
461 #ifdef CONFIG_X11
462 if (dxr3_overlay) {
463 XVisualInfo vinfo;
464 XSetWindowAttributes xswa;
465 XSizeHints hint;
466 unsigned long xswamask;
467 Colormap cmap;
468 XColor key_color;
469 Window junkwindow;
470 Screen *scr;
471 int depth, red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec, acq_color;
472 em8300_overlay_screen_t ovlscr;
473 em8300_attribute_t ovlattr;
475 vo_dx = (vo_screenwidth - d_width) / 2;
476 vo_dy = (vo_screenheight - d_height) / 2;
477 vo_dwidth = d_width;
478 vo_dheight = d_height;
479 #ifdef CONFIG_GUI
480 if (use_gui) {
481 guiGetEvent(guiSetShVideo, 0);
482 XSetWindowBackground(mDisplay, vo_window, KEY_COLOR);
483 XClearWindow(mDisplay, vo_window);
484 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &xwin_attribs);
485 depth = xwin_attribs.depth;
486 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) {
487 depth = 24;
489 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
490 } else
491 #endif
493 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &xwin_attribs);
494 depth = xwin_attribs.depth;
495 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) {
496 depth = 24;
498 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
499 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy,
500 d_width, d_height, flags,
501 CopyFromParent, "Viewing Window", title);
502 xswa.background_pixel = KEY_COLOR;
503 xswa.border_pixel = 0;
504 xswamask = CWBackPixel | CWBorderPixel;
505 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
508 /* Start setting up overlay */
509 XGetWindowAttributes(mDisplay, mRootWin, &xwin_attribs);
510 overlay_set_screen(overlay_data, xwin_attribs.width, xwin_attribs.height, xwin_attribs.depth);
511 overlay_read_state(overlay_data, NULL);
513 /* Allocate keycolor */
514 cmap = vo_x11_create_colormap(&vinfo);
515 calculate_cvals(vinfo.red_mask, &red_shift, &red_prec);
516 calculate_cvals(vinfo.green_mask, &green_shift, &green_prec);
517 calculate_cvals(vinfo.blue_mask, &blue_shift, &blue_prec);
519 key_color.red = ((KEY_COLOR >> 16) & 0xff) * 256;
520 key_color.green = ((KEY_COLOR >> 8) & 0xff) * 256;
521 key_color.blue = (KEY_COLOR & 0xff) * 256;
522 key_color.pixel = (((key_color.red >> (16 - red_prec)) << red_shift) +
523 ((key_color.green >> (16 - green_prec)) << green_shift) +
524 ((key_color.blue >> (16 - blue_prec)) << blue_shift));
525 key_color.flags = DoRed | DoGreen | DoBlue;
526 if (!XAllocColor(mDisplay, cmap, &key_color)) {
527 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToAllocateKeycolor);
528 return -1;
531 acq_color = ((key_color.red / 256) << 16) | ((key_color.green / 256) << 8) | key_color.blue;
532 if (key_color.pixel != KEY_COLOR) {
533 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_UnableToAllocateExactKeycolor, key_color.pixel);
536 /* Set keycolor and activate overlay */
537 XSetWindowBackground(mDisplay, vo_window, key_color.pixel);
538 XClearWindow(mDisplay, vo_window);
539 overlay_set_keycolor(overlay_data, key_color.pixel);
540 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_OVERLAY);
541 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_RECTANGLE);
543 #endif
545 return 0;
548 static void draw_alpha(int x, int y, int w, int h, unsigned char* src, unsigned char *srca, int srcstride)
550 #ifdef SPU_SUPPORT
551 unsigned char *buf = &osdpicbuf[(y * osdpicbuf_w) + x];
552 int by = 0;
553 register int lx, ly;
554 register int stride = 0;
556 for (ly = 0; ly < h - 1; ly++)
558 for(lx = 0; lx < w; lx++ )
559 if ( ( srca[stride + lx] )&&( src[stride + lx] >= 128 ) ) buf[by + lx] = 3;
560 by+=osdpicbuf_w;
561 stride+=srcstride;
563 pixbuf_encode_rle(x, y, osdpicbuf_w, osdpicbuf_h - 1, osdpicbuf, osdpicbuf_w, spued);
564 #endif
567 extern int vo_osd_changed_flag;
568 extern mp_osd_obj_t* vo_osd_list;
570 static void draw_osd(void)
572 #ifdef SPU_SUPPORT
573 static int cleared = 0;
574 int changed = 0;
576 if ((disposd % 15) == 0)
579 mp_osd_obj_t* obj = vo_osd_list;
580 vo_update_osd( osd_w,osd_h );
581 while( obj )
583 if ( obj->flags & OSDFLAG_VISIBLE ) { changed=1; break; }
584 obj=obj->next;
587 if ( changed )
589 vo_draw_text(osd_w, osd_h, draw_alpha);
590 memset(osdpicbuf, 0, s_width * s_height);
591 cleared=0;
593 else
595 if ( !cleared )
597 spued->count=spubuf->count;
598 fast_memcpy( spued->data,spubuf->data,DATASIZE );
599 cleared=1;
604 /* could stand some check here to see if the subpic hasn't changed
605 * as if it hasn't and we re-send it it will "blink" as the last one
606 * is turned off, and the new one (same one) is turned on
608 /* Subpics are not stable yet =(
609 expect lockups if you enable */
610 #if 1
611 write(fd_spu, spued->data, spued->count);
612 #endif
614 disposd++;
615 #endif
619 static int draw_frame(uint8_t * src[])
621 vo_mpegpes_t *p = (vo_mpegpes_t *) src[0];
623 #ifdef SPU_SUPPORT
624 if (p->id == 0x20) {
625 write(fd_spu, p->data, p->size);
626 } else
627 #endif
628 write(fd_video, p->data, p->size);
629 return 0;
632 static void flip_page(void)
634 #ifdef CONFIG_X11
635 if (dxr3_overlay) {
636 int event = vo_x11_check_events(mDisplay);
637 if (event & VO_EVENT_RESIZE) {
638 Window junkwindow;
639 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
640 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
641 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
643 if (event & VO_EVENT_EXPOSE) {
644 Window junkwindow;
645 XSetWindowBackground(mDisplay, vo_window, KEY_COLOR);
646 XClearWindow(mDisplay, vo_window);
647 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
648 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
649 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
652 #endif
654 if (dxr3_newsync) {
655 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
656 ioval <<= 1;
657 if (vo_pts == 0) {
658 ioval = 0;
659 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
660 pts_offset = 0;
661 } else if ((vo_pts - pts_offset) < (ioval - 7200) || (vo_pts - pts_offset) > (ioval + 7200)) {
662 ioval = (vo_pts + pts_offset) >> 1;
663 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
664 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
665 pts_offset = vo_pts - (ioval << 1);
666 if (pts_offset < 0) {
667 pts_offset = 0;
670 ioval = vo_pts + pts_offset;
671 ioctl(fd_video, EM8300_IOCTL_SPU_SETPTS, &ioval);
672 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &ioval);
673 prev_pts = vo_pts;
674 } else if (dxr3_prebuf) {
675 ioctl(fd_spu, EM8300_IOCTL_SPU_SETPTS, &vo_pts);
676 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &vo_pts);
680 static int draw_slice(uint8_t *srcimg[], int stride[], int w, int h, int x0, int y0)
682 return -1;
685 static void uninit(void)
687 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Uninitializing);
688 #ifdef CONFIG_X11
689 if (dxr3_overlay) {
690 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_OFF);
691 overlay_release(overlay_data);
693 #ifdef CONFIG_GUI
694 if (!use_gui) {
695 #endif
696 vo_x11_uninit();
698 #ifdef CONFIG_GUI
700 #endif
702 #endif
703 if (old_vmode != -1) {
704 if (ioctl(fd_control, EM8300_IOCTL_SET_VIDEOMODE, &old_vmode) < 0) {
705 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedRestoringTVNorm);
709 if (fd_video) {
710 close(fd_video);
712 if (fd_spu) {
713 close(fd_spu);
715 if (fd_control) {
716 close(fd_control);
718 #ifdef SPU_SUPPORT
719 if(osdpicbuf) {
720 free(osdpicbuf);
722 if(spued) {
723 free(spued);
725 #endif
728 static void check_events(void)
732 static int preinit(const char *arg)
734 char devname[MAX_STR_SIZE];
735 int fdflags = O_WRONLY;
737 /* Parse commandline */
738 while (arg) {
739 if (!strncmp("prebuf", arg, 6) && !dxr3_prebuf) {
740 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_EnablingPrebuffering);
741 dxr3_prebuf = 1;
742 } else if (!strncmp("sync", arg, 4) && !dxr3_newsync) {
743 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingNewSyncEngine);
744 dxr3_newsync = 1;
745 } else if (!strncmp("overlay", arg, 7) && !dxr3_overlay) {
746 #ifdef CONFIG_X11
747 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingOverlay);
748 dxr3_overlay = 1;
749 #else
750 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorYouNeedToCompileMplayerWithX11);
751 #endif
752 } else if (!strncmp("norm=", arg, 5)) {
753 arg += 5;
754 // dxr3_norm is 0 (-> don't change norm) by default
755 // but maybe someone changes this in the future
757 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_WillSetTVNormTo);
759 if (*arg == '5') {
760 dxr3_norm = 5;
761 mp_msg(MSGT_VO,MSGL_INFO, "NTSC");
762 } else if (*arg == '4') {
763 dxr3_norm = 4;
764 mp_msg(MSGT_VO,MSGL_INFO, "PAL-60");
765 } else if (*arg == '3') {
766 dxr3_norm = 3;
767 mp_msg(MSGT_VO,MSGL_INFO, "PAL");
768 } else if (*arg == '2') {
769 dxr3_norm = 2;
770 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALPAL60);
771 } else if (*arg == '1') {
772 dxr3_norm = 1;
773 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALNTSC);
774 } else if (*arg == '0') {
775 dxr3_norm = 0;
776 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseCurrentNorm);
777 } else {
778 dxr3_norm = 0;
779 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseUnknownNormSuppliedCurrentNorm);
782 mp_msg(MSGT_VO,MSGL_INFO, ".\n");
783 } else if (arg[0] == '0' || arg[0] == '1' || arg[0] == '2' || arg[0] == '3') {
784 dxr3_device_num = arg[0];
787 arg = strchr(arg, ':');
788 if (arg) {
789 arg++;
794 /* Open the control interface */
795 sprintf(devname, "/dev/em8300-%d", dxr3_device_num);
796 fd_control = open(devname, fdflags);
797 if (fd_control < 1) {
798 /* Fall back to old naming scheme */
799 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTrying, devname);
800 sprintf(devname, "/dev/em8300");
801 fd_control = open(devname, fdflags);
802 if (fd_control < 1) {
803 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWell);
804 return -1;
806 } else {
807 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
810 /* Open the video interface */
811 sprintf(devname, "/dev/em8300_mv-%d", dxr3_device_num);
812 fd_video = open(devname, fdflags);
813 if (fd_video < 0) {
814 /* Fall back to old naming scheme */
815 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingMV, devname);
816 sprintf(devname, "/dev/em8300_mv");
817 fd_video = open(devname, fdflags);
818 if (fd_video < 0) {
819 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellMV);
820 uninit();
821 return -1;
823 } else {
824 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
826 strcpy(fdv_name, devname);
828 /* Open the subpicture interface */
829 sprintf(devname, "/dev/em8300_sp-%d", dxr3_device_num);
830 fd_spu = open(devname, fdflags);
831 if (fd_spu < 0) {
832 /* Fall back to old naming scheme */
833 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingSP, devname);
834 sprintf(devname, "/dev/em8300_sp");
835 fd_spu = open(devname, fdflags);
836 if (fd_spu < 0) {
837 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellSP);
838 uninit();
839 return -1;
841 } else {
842 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
844 strcpy(fds_name, devname);
846 #ifdef CONFIG_X11
847 if (dxr3_overlay) {
849 /* Fucked up hack needed to enable overlay.
850 * Will be removed as soon as I figure out
851 * how to make it work like it should
853 Display *dpy;
854 overlay_t *ov;
855 XWindowAttributes attribs;
857 dpy = XOpenDisplay(NULL);
858 if (!dpy) {
859 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToOpenDisplayDuringHackSetup);
860 return -1;
862 XGetWindowAttributes(dpy, RootWindow(dpy, DefaultScreen(dpy)), &attribs);
863 ov = overlay_init(fd_control);
864 overlay_set_screen(ov, attribs.width, attribs.height, PlanesOfScreen(ScreenOfDisplay(dpy, 0)));
865 overlay_read_state(ov, NULL);
866 overlay_set_keycolor(ov, KEY_COLOR);
867 overlay_set_mode(ov, EM8300_OVERLAY_MODE_OVERLAY);
868 overlay_set_mode(ov, EM8300_OVERLAY_MODE_RECTANGLE);
869 overlay_release(ov);
870 XCloseDisplay(dpy);
871 /* End of fucked up hack */
873 /* Initialize overlay and X11 */
874 overlay_data = overlay_init(fd_control);
875 #ifdef CONFIG_GUI
876 if (!use_gui) {
877 #endif
878 if (!vo_init()) {
879 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToInitX11);
880 return -1;
882 #ifdef CONFIG_GUI
884 #endif
886 #endif
888 if (dxr3_newsync) {
889 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
890 pts_offset = vo_pts - (ioval << 1);
891 if (pts_offset < 0) {
892 pts_offset = 0;
896 return 0;
899 /* Begin overlay.c */
900 static int update_parameters(overlay_t *o)
902 overlay_set_attribute(o, EM9010_ATTRIBUTE_XOFFSET, o->xoffset);
903 overlay_set_attribute(o, EM9010_ATTRIBUTE_YOFFSET, o->yoffset);
904 overlay_set_attribute(o, EM9010_ATTRIBUTE_XCORR, o->xcorr);
905 overlay_set_attribute(o, EM9010_ATTRIBUTE_STABILITY, o->stability);
906 overlay_set_attribute(o, EM9010_ATTRIBUTE_JITTER, o->jitter);
907 return 0;
910 static int overlay_set_attribute(overlay_t *o, int attribute, int value)
912 em8300_attribute_t attr;
914 attr.attribute = attribute;
915 attr.value = value;
916 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SET_ATTRIBUTE, &attr)==-1)
918 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayAttribute);
919 return -1;
922 return 0;
925 static overlay_t *overlay_init(int dev)
927 overlay_t *o;
929 o = (overlay_t *) malloc(sizeof(overlay_t));
931 if(!o)
932 return NULL;
934 memset(o,0,sizeof(overlay_t));
936 o->dev = dev;
937 o->xres = 1280; o->yres=1024; o->xcorr=1000;
938 o->color_interval=10;
940 return o;
943 static int overlay_release(overlay_t *o)
945 if(o)
946 free(o);
948 return 0;
950 #define TYPE_INT 1
951 #define TYPE_XINT 2
952 #define TYPE_COEFF 3
953 #define TYPE_FLOAT 4
955 struct lut_entry {
956 char *name;
957 int type;
958 void *ptr;
961 static struct lut_entry *new_lookuptable(overlay_t *o)
963 struct lut_entry m[] = {
964 {"xoffset", TYPE_INT, &o->xoffset},
965 {"yoffset", TYPE_INT, &o->yoffset},
966 {"xcorr", TYPE_INT, &o->xcorr},
967 {"jitter", TYPE_INT, &o->jitter},
968 {"stability", TYPE_INT, &o->stability},
969 {"keycolor", TYPE_XINT, &o->keycolor},
970 {"colcal_upper", TYPE_COEFF, &o->colcal_upper[0]},
971 {"colcal_lower", TYPE_COEFF, &o->colcal_lower[0]},
972 {"color_interval", TYPE_FLOAT, &o->color_interval},
973 {0,0,0}
974 },*p;
976 p = malloc(sizeof(m));
977 memcpy(p,m,sizeof(m));
978 return p;
981 static int lookup_parameter(overlay_t *o, struct lut_entry *lut, char *name, void **ptr, int *type) {
982 int i;
984 for(i=0; lut[i].name; i++) {
985 if(!strcmp(name,lut[i].name)) {
986 *ptr = lut[i].ptr;
987 *type = lut[i].type;
988 return 1;
991 return 0;
994 static int overlay_read_state(overlay_t *o, char *p)
996 char *a,*tok;
997 char path[128],fname[128],tmp[128],line[256];
998 FILE *fp;
999 struct lut_entry *lut;
1000 void *ptr;
1001 int type;
1002 int j;
1004 if(!p) {
1005 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1006 av_strlcat(fname,"/.overlay", sizeof( fname ));
1007 } else
1008 av_strlcpy(fname, p, sizeof( fname ));
1010 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1011 av_strlcat(fname, tmp, sizeof( fname ));
1013 if(!(fp=fopen(fname,"r")))
1014 return -1;
1016 lut = new_lookuptable(o);
1018 while(!feof(fp)) {
1019 if(!fgets(line,256,fp))
1020 break;
1021 tok=strtok(line," ");
1022 if(lookup_parameter(o,lut,tok,&ptr,&type)) {
1023 tok=strtok(NULL," ");
1024 switch(type) {
1025 case TYPE_INT:
1026 sscanf(tok,"%d",(int *)ptr);
1027 break;
1028 case TYPE_XINT:
1029 sscanf(tok,"%x",(int *)ptr);
1030 break;
1031 case TYPE_FLOAT:
1032 sscanf(tok,"%f",(float *)ptr);
1033 break;
1034 case TYPE_COEFF:
1035 for(j=0;j<3;j++) {
1036 sscanf(tok,"%f",&((struct coeff *)ptr)[j].k);
1037 tok=strtok(NULL," ");
1038 sscanf(tok,"%f",&((struct coeff *)ptr)[j].m);
1039 tok=strtok(NULL," ");
1041 break;
1047 update_parameters(o);
1049 free(lut);
1050 fclose(fp);
1051 return 0;
1054 static void overlay_update_params(overlay_t *o) {
1055 update_parameters(o);
1058 static int overlay_write_state(overlay_t *o, char *p)
1060 char *a;
1061 char path[128],fname[128],tmp[128];
1062 FILE *fp;
1063 char line[256],*tok;
1064 struct lut_entry *lut;
1065 int i,j;
1067 if(!p) {
1068 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1069 av_strlcat(fname,"/.overlay", sizeof( fname ));
1070 } else
1071 av_strlcpy(fname, p, sizeof( fname ));
1073 if(access(fname, W_OK|X_OK|R_OK)) {
1074 if(mkdir(fname,0766))
1075 return -1;
1078 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1079 av_strlcat(fname, tmp, sizeof( fname ));
1081 if(!(fp=fopen(fname,"w")))
1082 return -1;
1084 lut = new_lookuptable(o);
1086 for(i=0; lut[i].name; i++) {
1087 fprintf(fp,"%s ",lut[i].name);
1088 switch(lut[i].type) {
1089 case TYPE_INT:
1090 fprintf(fp,"%d\n",*(int *)lut[i].ptr);
1091 break;
1092 case TYPE_XINT:
1093 fprintf(fp,"%06x\n",*(int *)lut[i].ptr);
1094 break;
1095 case TYPE_FLOAT:
1096 fprintf(fp,"%f\n",*(float *)lut[i].ptr);
1097 break;
1098 case TYPE_COEFF:
1099 for(j=0;j<3;j++)
1100 fprintf(fp,"%f %f ",((struct coeff *)lut[i].ptr)[j].k,
1101 ((struct coeff *)lut[i].ptr)[j].m);
1102 fprintf(fp,"\n");
1103 break;
1107 fclose(fp);
1108 return 0;
1111 static int overlay_set_screen(overlay_t *o, int xres, int yres, int depth)
1113 em8300_overlay_screen_t scr;
1115 o->xres = xres;
1116 o->yres = yres;
1117 o->depth = depth;
1119 scr.xsize = xres;
1120 scr.ysize = yres;
1122 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETSCREEN, &scr)==-1)
1124 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayScreen);
1125 return -1;
1127 return 0;
1130 static int overlay_set_mode(overlay_t *o, int mode)
1132 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETMODE, &mode)==-1) {
1133 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedEnablingOverlay);
1134 return -1;
1136 return 0;
1139 static int overlay_set_window(overlay_t *o, int xpos,int ypos,int width,int height)
1141 em8300_overlay_window_t win;
1142 win.xpos = xpos;
1143 win.ypos = ypos;
1144 win.width = width;
1145 win.height = height;
1147 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1)
1149 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1150 return -1;
1152 return 0;
1155 static int overlay_set_bcs(overlay_t *o, int brightness, int contrast, int saturation)
1157 em8300_bcs_t bcs;
1158 bcs.brightness = brightness;
1159 bcs.contrast = contrast;
1160 bcs.saturation = saturation;
1162 if (ioctl(o->dev, EM8300_IOCTL_GETBCS, &bcs)==-1)
1164 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayBcs);
1165 return -1;
1167 return 0;
1170 static int col_interp(float x, struct coeff c)
1172 float y;
1173 y = x*c.k + c.m;
1174 if(y > 255)
1175 y = 255;
1176 if(y < 0)
1177 y = 0;
1178 return rint(y);
1181 static int overlay_set_keycolor(overlay_t *o, int color) {
1182 int r = (color & 0xff0000) >> 16;
1183 int g = (color & 0x00ff00) >> 8;
1184 int b = (color & 0x0000ff);
1185 float ru,gu,bu;
1186 float rl,gl,bl;
1187 int upper,lower;
1189 ru = r+o->color_interval;
1190 gu = g+o->color_interval;
1191 bu = b+o->color_interval;
1193 rl = r-o->color_interval;
1194 gl = g-o->color_interval;
1195 bl = b-o->color_interval;
1197 upper = (col_interp(ru, o->colcal_upper[0]) << 16) |
1198 (col_interp(gu, o->colcal_upper[1]) << 8) |
1199 (col_interp(bu, o->colcal_upper[2]));
1201 lower = (col_interp(rl, o->colcal_lower[0]) << 16) |
1202 (col_interp(gl, o->colcal_lower[1]) << 8) |
1203 (col_interp(bl, o->colcal_lower[2]));
1205 //printf("0x%06x 0x%06x\n",upper,lower);
1206 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_UPPER,upper);
1207 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_LOWER,lower);
1208 return 0;
1211 static void least_sq_fit(int *x, int *y, int n, float *k, float *m)
1213 float sx=0,sy=0,sxx=0,sxy=0;
1214 float delta,b;
1215 int i;
1217 for(i=0; i < n; i++) {
1218 sx=sx+x[i];
1219 sy=sy+y[i];
1220 sxx=sxx+x[i]*x[i];
1221 sxy=sxy+x[i]*y[i];
1224 delta=sxx*n-sx*sx;
1226 *m=(sxx*sy-sx*sxy)/delta;
1227 *k=(sxy*n-sx*sy)/delta;
1230 static int overlay_autocalibrate(overlay_t *o, pattern_drawer_cb pd, void *arg)
1232 em8300_overlay_calibrate_t cal;
1233 em8300_overlay_window_t win;
1234 int x[256],r[256],g[256],b[256],n;
1235 float k,m;
1237 int i;
1239 o->draw_pattern=pd;
1240 o->dp_arg = arg;
1242 overlay_set_mode(o, EM8300_OVERLAY_MODE_OVERLAY);
1243 overlay_set_screen(o, o->xres, o->yres, o->depth);
1245 /* Calibrate Y-offset */
1247 o->draw_pattern(0x0000ff, 0, 0, 0, 355, 1, o->dp_arg);
1249 cal.cal_mode = EM8300_OVERLAY_CALMODE_YOFFSET;
1250 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1252 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayYOffsetValues);
1253 return -1;
1255 o->yoffset = cal.result;
1256 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_YOffset,cal.result);
1258 /* Calibrate X-offset */
1260 o->draw_pattern(0x0000ff, 0, 0, 0, 2, 288, o->dp_arg);
1262 cal.cal_mode = EM8300_OVERLAY_CALMODE_XOFFSET;
1263 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1265 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXOffsetValues);
1266 return -1;
1268 o->xoffset = cal.result;
1269 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XOffset,cal.result);
1271 /* Calibrate X scale correction */
1273 o->draw_pattern(0x0000ff, 0, 355, 0, 2, 288, o->dp_arg);
1275 cal.cal_mode = EM8300_OVERLAY_CALMODE_XCORRECTION;
1276 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1278 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXScaleCorrection);
1279 return -1;
1281 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XCorrection,cal.result);
1282 o->xcorr = cal.result;
1284 win.xpos = 10;
1285 win.ypos = 10;
1286 win.width = o->xres-20;
1287 win.height = o->yres-20;
1288 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1) {
1289 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1290 exit(1);
1293 /* Calibrate key color upper limit */
1295 for(i=128,n=0; i <= 0xff; i+=4) {
1296 o->draw_pattern(i | (i << 8) | (i << 16), 0,
1297 (o->xres-200)/2,0,200,o->yres,o->dp_arg);
1299 cal.arg = i;
1300 cal.arg2 = 1;
1301 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1303 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1305 return -1 ;
1308 x[n] = i;
1309 r[n] = (cal.result>>16)&0xff;
1310 g[n] = (cal.result>>8)&0xff;
1311 b[n] = (cal.result)&0xff;
1312 n++;
1315 least_sq_fit(x,r,n,&o->colcal_upper[0].k,&o->colcal_upper[0].m);
1316 least_sq_fit(x,g,n,&o->colcal_upper[1].k,&o->colcal_upper[1].m);
1317 least_sq_fit(x,b,n,&o->colcal_upper[2].k,&o->colcal_upper[2].m);
1319 /* Calibrate key color lower limit */
1321 for(i=128,n=0; i <= 0xff; i+=4) {
1322 o->draw_pattern(i | (i << 8) | (i << 16), 0xffffff,
1323 (o->xres-200)/2,0,200,o->yres, o->dp_arg);
1325 cal.arg = i;
1326 cal.arg2 = 2;
1327 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1329 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1331 return -1 ;
1333 x[n] = i;
1334 r[n] = (cal.result>>16)&0xff;
1335 g[n] = (cal.result>>8)&0xff;
1336 b[n] = (cal.result)&0xff;
1337 n++;
1340 least_sq_fit(x,r,n,&o->colcal_lower[0].k,&o->colcal_lower[0].m);
1341 least_sq_fit(x,g,n,&o->colcal_lower[1].k,&o->colcal_lower[1].m);
1342 least_sq_fit(x,b,n,&o->colcal_lower[2].k,&o->colcal_lower[2].m);
1344 overlay_set_mode(o, EM8300_OVERLAY_MODE_OFF);
1346 return 0;
1350 static int overlay_signalmode(overlay_t *o, int mode) {
1351 if(ioctl(o->dev, EM8300_IOCTL_OVERLAY_SIGNALMODE, &mode) ==-1) {
1352 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSetSignalMix);
1353 return -1;
1355 return 0;
1357 /* End overlay.c */