Merge svn changes up to r27441
[mplayer.git] / libvo / vo_dxr3.c
blob69038ae0eb22973b90aa6ee06d22c14f40b24941
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);
544 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
546 #endif
548 return 0;
551 static void draw_alpha(int x, int y, int w, int h, unsigned char* src, unsigned char *srca, int srcstride)
553 #ifdef SPU_SUPPORT
554 unsigned char *buf = &osdpicbuf[(y * osdpicbuf_w) + x];
555 int by = 0;
556 register int lx, ly;
557 register int stride = 0;
559 for (ly = 0; ly < h - 1; ly++)
561 for(lx = 0; lx < w; lx++ )
562 if ( ( srca[stride + lx] )&&( src[stride + lx] >= 128 ) ) buf[by + lx] = 3;
563 by+=osdpicbuf_w;
564 stride+=srcstride;
566 pixbuf_encode_rle(x, y, osdpicbuf_w, osdpicbuf_h - 1, osdpicbuf, osdpicbuf_w, spued);
567 #endif
570 extern int vo_osd_changed_flag;
571 extern mp_osd_obj_t* vo_osd_list;
573 static void draw_osd(void)
575 #ifdef SPU_SUPPORT
576 static int cleared = 0;
577 int changed = 0;
579 if ((disposd % 15) == 0)
582 mp_osd_obj_t* obj = vo_osd_list;
583 vo_update_osd( osd_w,osd_h );
584 while( obj )
586 if ( obj->flags & OSDFLAG_VISIBLE ) { changed=1; break; }
587 obj=obj->next;
590 if ( changed )
592 vo_draw_text(osd_w, osd_h, draw_alpha);
593 memset(osdpicbuf, 0, s_width * s_height);
594 cleared=0;
596 else
598 if ( !cleared )
600 spued->count=spubuf->count;
601 fast_memcpy( spued->data,spubuf->data,DATASIZE );
602 cleared=1;
607 /* could stand some check here to see if the subpic hasn't changed
608 * as if it hasn't and we re-send it it will "blink" as the last one
609 * is turned off, and the new one (same one) is turned on
611 /* Subpics are not stable yet =(
612 expect lockups if you enable */
613 #if 1
614 write(fd_spu, spued->data, spued->count);
615 #endif
617 disposd++;
618 #endif
622 static int draw_frame(uint8_t * src[])
624 vo_mpegpes_t *p = (vo_mpegpes_t *) src[0];
626 #ifdef SPU_SUPPORT
627 if (p->id == 0x20) {
628 write(fd_spu, p->data, p->size);
629 } else
630 #endif
631 write(fd_video, p->data, p->size);
632 return 0;
635 static void flip_page(void)
637 #ifdef CONFIG_X11
638 if (dxr3_overlay) {
639 int event = vo_x11_check_events(mDisplay);
640 if (event & VO_EVENT_RESIZE) {
641 Window junkwindow;
642 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
643 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
644 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
646 if (event & VO_EVENT_EXPOSE) {
647 Window junkwindow;
648 XSetWindowBackground(mDisplay, vo_window, KEY_COLOR);
649 XClearWindow(mDisplay, vo_window);
650 XGetWindowAttributes(mDisplay, vo_window, &xwin_attribs);
651 XTranslateCoordinates(mDisplay, vo_window, mRootWin, -xwin_attribs.border_width, -xwin_attribs.border_width, &xwin_attribs.x, &xwin_attribs.y, &junkwindow);
652 overlay_set_window(overlay_data, xwin_attribs.x, xwin_attribs.y, xwin_attribs.width, xwin_attribs.height);
655 #endif
657 if (dxr3_newsync) {
658 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
659 ioval <<= 1;
660 if (vo_pts == 0) {
661 ioval = 0;
662 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
663 pts_offset = 0;
664 } else if ((vo_pts - pts_offset) < (ioval - 7200) || (vo_pts - pts_offset) > (ioval + 7200)) {
665 ioval = (vo_pts + pts_offset) >> 1;
666 ioctl(fd_control, EM8300_IOCTL_SCR_SET, &ioval);
667 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
668 pts_offset = vo_pts - (ioval << 1);
669 if (pts_offset < 0) {
670 pts_offset = 0;
673 ioval = vo_pts + pts_offset;
674 ioctl(fd_video, EM8300_IOCTL_SPU_SETPTS, &ioval);
675 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &ioval);
676 prev_pts = vo_pts;
677 } else if (dxr3_prebuf) {
678 ioctl(fd_spu, EM8300_IOCTL_SPU_SETPTS, &vo_pts);
679 ioctl(fd_video, EM8300_IOCTL_VIDEO_SETPTS, &vo_pts);
683 static int draw_slice(uint8_t *srcimg[], int stride[], int w, int h, int x0, int y0)
685 return -1;
688 static void uninit(void)
690 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Uninitializing);
691 #ifdef CONFIG_X11
692 if (dxr3_overlay) {
693 overlay_set_mode(overlay_data, EM8300_OVERLAY_MODE_OFF);
694 overlay_release(overlay_data);
696 #ifdef CONFIG_GUI
697 if (!use_gui) {
698 #endif
699 vo_x11_uninit();
701 #ifdef CONFIG_GUI
703 #endif
705 #endif
706 if (old_vmode != -1) {
707 if (ioctl(fd_control, EM8300_IOCTL_SET_VIDEOMODE, &old_vmode) < 0) {
708 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedRestoringTVNorm);
712 if (fd_video) {
713 close(fd_video);
715 if (fd_spu) {
716 close(fd_spu);
718 if (fd_control) {
719 close(fd_control);
721 #ifdef SPU_SUPPORT
722 if(osdpicbuf) {
723 free(osdpicbuf);
725 if(spued) {
726 free(spued);
728 #endif
731 static void check_events(void)
735 static int preinit(const char *arg)
737 char devname[MAX_STR_SIZE];
738 int fdflags = O_WRONLY;
740 /* Parse commandline */
741 while (arg) {
742 if (!strncmp("prebuf", arg, 6) && !dxr3_prebuf) {
743 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_EnablingPrebuffering);
744 dxr3_prebuf = 1;
745 } else if (!strncmp("sync", arg, 4) && !dxr3_newsync) {
746 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingNewSyncEngine);
747 dxr3_newsync = 1;
748 } else if (!strncmp("overlay", arg, 7) && !dxr3_overlay) {
749 #ifdef CONFIG_X11
750 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UsingOverlay);
751 dxr3_overlay = 1;
752 #else
753 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorYouNeedToCompileMplayerWithX11);
754 #endif
755 } else if (!strncmp("norm=", arg, 5)) {
756 arg += 5;
757 // dxr3_norm is 0 (-> don't change norm) by default
758 // but maybe someone changes this in the future
760 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_WillSetTVNormTo);
762 if (*arg == '5') {
763 dxr3_norm = 5;
764 mp_msg(MSGT_VO,MSGL_INFO, "NTSC");
765 } else if (*arg == '4') {
766 dxr3_norm = 4;
767 mp_msg(MSGT_VO,MSGL_INFO, "PAL-60");
768 } else if (*arg == '3') {
769 dxr3_norm = 3;
770 mp_msg(MSGT_VO,MSGL_INFO, "PAL");
771 } else if (*arg == '2') {
772 dxr3_norm = 2;
773 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALPAL60);
774 } else if (*arg == '1') {
775 dxr3_norm = 1;
776 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALNTSC);
777 } else if (*arg == '0') {
778 dxr3_norm = 0;
779 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseCurrentNorm);
780 } else {
781 dxr3_norm = 0;
782 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_UseUnknownNormSuppliedCurrentNorm);
785 mp_msg(MSGT_VO,MSGL_INFO, ".\n");
786 } else if (arg[0] == '0' || arg[0] == '1' || arg[0] == '2' || arg[0] == '3') {
787 dxr3_device_num = arg[0];
790 arg = strchr(arg, ':');
791 if (arg) {
792 arg++;
797 /* Open the control interface */
798 sprintf(devname, "/dev/em8300-%d", dxr3_device_num);
799 fd_control = open(devname, fdflags);
800 if (fd_control < 1) {
801 /* Fall back to old naming scheme */
802 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTrying, devname);
803 sprintf(devname, "/dev/em8300");
804 fd_control = open(devname, fdflags);
805 if (fd_control < 1) {
806 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWell);
807 return -1;
809 } else {
810 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
813 /* Open the video interface */
814 sprintf(devname, "/dev/em8300_mv-%d", dxr3_device_num);
815 fd_video = open(devname, fdflags);
816 if (fd_video < 0) {
817 /* Fall back to old naming scheme */
818 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingMV, devname);
819 sprintf(devname, "/dev/em8300_mv");
820 fd_video = open(devname, fdflags);
821 if (fd_video < 0) {
822 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellMV);
823 uninit();
824 return -1;
826 } else {
827 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
829 strcpy(fdv_name, devname);
831 /* Open the subpicture interface */
832 sprintf(devname, "/dev/em8300_sp-%d", dxr3_device_num);
833 fd_spu = open(devname, fdflags);
834 if (fd_spu < 0) {
835 /* Fall back to old naming scheme */
836 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingSP, devname);
837 sprintf(devname, "/dev/em8300_sp");
838 fd_spu = open(devname, fdflags);
839 if (fd_spu < 0) {
840 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellSP);
841 uninit();
842 return -1;
844 } else {
845 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_Opened, devname);
847 strcpy(fds_name, devname);
849 #ifdef CONFIG_X11
850 if (dxr3_overlay) {
852 /* Fucked up hack needed to enable overlay.
853 * Will be removed as soon as I figure out
854 * how to make it work like it should
856 Display *dpy;
857 overlay_t *ov;
858 XWindowAttributes attribs;
860 dpy = XOpenDisplay(NULL);
861 if (!dpy) {
862 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToOpenDisplayDuringHackSetup);
863 return -1;
865 XGetWindowAttributes(dpy, RootWindow(dpy, DefaultScreen(dpy)), &attribs);
866 ov = overlay_init(fd_control);
867 overlay_set_screen(ov, attribs.width, attribs.height, PlanesOfScreen(ScreenOfDisplay(dpy, 0)));
868 overlay_read_state(ov, NULL);
869 overlay_set_keycolor(ov, KEY_COLOR);
870 overlay_set_mode(ov, EM8300_OVERLAY_MODE_OVERLAY);
871 overlay_set_mode(ov, EM8300_OVERLAY_MODE_RECTANGLE);
872 overlay_release(ov);
873 XCloseDisplay(dpy);
874 /* End of fucked up hack */
876 /* Initialize overlay and X11 */
877 overlay_data = overlay_init(fd_control);
878 #ifdef CONFIG_GUI
879 if (!use_gui) {
880 #endif
881 if (!vo_init()) {
882 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_UnableToInitX11);
883 return -1;
885 #ifdef CONFIG_GUI
887 #endif
889 #endif
891 if (dxr3_newsync) {
892 ioctl(fd_control, EM8300_IOCTL_SCR_GET, &ioval);
893 pts_offset = vo_pts - (ioval << 1);
894 if (pts_offset < 0) {
895 pts_offset = 0;
899 return 0;
902 /* Begin overlay.c */
903 static int update_parameters(overlay_t *o)
905 overlay_set_attribute(o, EM9010_ATTRIBUTE_XOFFSET, o->xoffset);
906 overlay_set_attribute(o, EM9010_ATTRIBUTE_YOFFSET, o->yoffset);
907 overlay_set_attribute(o, EM9010_ATTRIBUTE_XCORR, o->xcorr);
908 overlay_set_attribute(o, EM9010_ATTRIBUTE_STABILITY, o->stability);
909 overlay_set_attribute(o, EM9010_ATTRIBUTE_JITTER, o->jitter);
910 return 0;
913 static int overlay_set_attribute(overlay_t *o, int attribute, int value)
915 em8300_attribute_t attr;
917 attr.attribute = attribute;
918 attr.value = value;
919 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SET_ATTRIBUTE, &attr)==-1)
921 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayAttribute);
922 return -1;
925 return 0;
928 static overlay_t *overlay_init(int dev)
930 overlay_t *o;
932 o = (overlay_t *) malloc(sizeof(overlay_t));
934 if(!o)
935 return NULL;
937 memset(o,0,sizeof(overlay_t));
939 o->dev = dev;
940 o->xres = 1280; o->yres=1024; o->xcorr=1000;
941 o->color_interval=10;
943 return o;
946 static int overlay_release(overlay_t *o)
948 if(o)
949 free(o);
951 return 0;
953 #define TYPE_INT 1
954 #define TYPE_XINT 2
955 #define TYPE_COEFF 3
956 #define TYPE_FLOAT 4
958 struct lut_entry {
959 char *name;
960 int type;
961 void *ptr;
964 static struct lut_entry *new_lookuptable(overlay_t *o)
966 struct lut_entry m[] = {
967 {"xoffset", TYPE_INT, &o->xoffset},
968 {"yoffset", TYPE_INT, &o->yoffset},
969 {"xcorr", TYPE_INT, &o->xcorr},
970 {"jitter", TYPE_INT, &o->jitter},
971 {"stability", TYPE_INT, &o->stability},
972 {"keycolor", TYPE_XINT, &o->keycolor},
973 {"colcal_upper", TYPE_COEFF, &o->colcal_upper[0]},
974 {"colcal_lower", TYPE_COEFF, &o->colcal_lower[0]},
975 {"color_interval", TYPE_FLOAT, &o->color_interval},
976 {0,0,0}
977 },*p;
979 p = malloc(sizeof(m));
980 memcpy(p,m,sizeof(m));
981 return p;
984 static int lookup_parameter(overlay_t *o, struct lut_entry *lut, char *name, void **ptr, int *type) {
985 int i;
987 for(i=0; lut[i].name; i++) {
988 if(!strcmp(name,lut[i].name)) {
989 *ptr = lut[i].ptr;
990 *type = lut[i].type;
991 return 1;
994 return 0;
997 static int overlay_read_state(overlay_t *o, char *p)
999 char *a,*tok;
1000 char path[128],fname[128],tmp[128],line[256];
1001 FILE *fp;
1002 struct lut_entry *lut;
1003 void *ptr;
1004 int type;
1005 int j;
1007 if(!p) {
1008 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1009 av_strlcat(fname,"/.overlay", sizeof( fname ));
1010 } else
1011 av_strlcpy(fname, p, sizeof( fname ));
1013 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1014 av_strlcat(fname, tmp, sizeof( fname ));
1016 if(!(fp=fopen(fname,"r")))
1017 return -1;
1019 lut = new_lookuptable(o);
1021 while(!feof(fp)) {
1022 if(!fgets(line,256,fp))
1023 break;
1024 tok=strtok(line," ");
1025 if(lookup_parameter(o,lut,tok,&ptr,&type)) {
1026 tok=strtok(NULL," ");
1027 switch(type) {
1028 case TYPE_INT:
1029 sscanf(tok,"%d",(int *)ptr);
1030 break;
1031 case TYPE_XINT:
1032 sscanf(tok,"%x",(int *)ptr);
1033 break;
1034 case TYPE_FLOAT:
1035 sscanf(tok,"%f",(float *)ptr);
1036 break;
1037 case TYPE_COEFF:
1038 for(j=0;j<3;j++) {
1039 sscanf(tok,"%f",&((struct coeff *)ptr)[j].k);
1040 tok=strtok(NULL," ");
1041 sscanf(tok,"%f",&((struct coeff *)ptr)[j].m);
1042 tok=strtok(NULL," ");
1044 break;
1050 update_parameters(o);
1052 free(lut);
1053 fclose(fp);
1054 return 0;
1057 static void overlay_update_params(overlay_t *o) {
1058 update_parameters(o);
1061 static int overlay_write_state(overlay_t *o, char *p)
1063 char *a;
1064 char path[128],fname[128],tmp[128];
1065 FILE *fp;
1066 char line[256],*tok;
1067 struct lut_entry *lut;
1068 int i,j;
1070 if(!p) {
1071 av_strlcpy(fname, getenv("HOME"), sizeof( fname ));
1072 av_strlcat(fname,"/.overlay", sizeof( fname ));
1073 } else
1074 av_strlcpy(fname, p, sizeof( fname ));
1076 if(access(fname, W_OK|X_OK|R_OK)) {
1077 if(mkdir(fname,0766))
1078 return -1;
1081 sprintf(tmp,"/res_%dx%dx%d",o->xres,o->yres,o->depth);
1082 av_strlcat(fname, tmp, sizeof( fname ));
1084 if(!(fp=fopen(fname,"w")))
1085 return -1;
1087 lut = new_lookuptable(o);
1089 for(i=0; lut[i].name; i++) {
1090 fprintf(fp,"%s ",lut[i].name);
1091 switch(lut[i].type) {
1092 case TYPE_INT:
1093 fprintf(fp,"%d\n",*(int *)lut[i].ptr);
1094 break;
1095 case TYPE_XINT:
1096 fprintf(fp,"%06x\n",*(int *)lut[i].ptr);
1097 break;
1098 case TYPE_FLOAT:
1099 fprintf(fp,"%f\n",*(float *)lut[i].ptr);
1100 break;
1101 case TYPE_COEFF:
1102 for(j=0;j<3;j++)
1103 fprintf(fp,"%f %f ",((struct coeff *)lut[i].ptr)[j].k,
1104 ((struct coeff *)lut[i].ptr)[j].m);
1105 fprintf(fp,"\n");
1106 break;
1110 fclose(fp);
1111 return 0;
1114 static int overlay_set_screen(overlay_t *o, int xres, int yres, int depth)
1116 em8300_overlay_screen_t scr;
1118 o->xres = xres;
1119 o->yres = yres;
1120 o->depth = depth;
1122 scr.xsize = xres;
1123 scr.ysize = yres;
1125 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETSCREEN, &scr)==-1)
1127 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayScreen);
1128 return -1;
1130 return 0;
1133 static int overlay_set_mode(overlay_t *o, int mode)
1135 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETMODE, &mode)==-1) {
1136 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedEnablingOverlay);
1137 return -1;
1139 return 0;
1142 static int overlay_set_window(overlay_t *o, int xpos,int ypos,int width,int height)
1144 em8300_overlay_window_t win;
1145 win.xpos = xpos;
1146 win.ypos = ypos;
1147 win.width = width;
1148 win.height = height;
1150 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1)
1152 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1153 return -1;
1155 return 0;
1158 static int overlay_set_bcs(overlay_t *o, int brightness, int contrast, int saturation)
1160 em8300_bcs_t bcs;
1161 bcs.brightness = brightness;
1162 bcs.contrast = contrast;
1163 bcs.saturation = saturation;
1165 if (ioctl(o->dev, EM8300_IOCTL_GETBCS, &bcs)==-1)
1167 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSettingOverlayBcs);
1168 return -1;
1170 return 0;
1173 static int col_interp(float x, struct coeff c)
1175 float y;
1176 y = x*c.k + c.m;
1177 if(y > 255)
1178 y = 255;
1179 if(y < 0)
1180 y = 0;
1181 return rint(y);
1184 static int overlay_set_keycolor(overlay_t *o, int color) {
1185 int r = (color & 0xff0000) >> 16;
1186 int g = (color & 0x00ff00) >> 8;
1187 int b = (color & 0x0000ff);
1188 float ru,gu,bu;
1189 float rl,gl,bl;
1190 int upper,lower;
1192 ru = r+o->color_interval;
1193 gu = g+o->color_interval;
1194 bu = b+o->color_interval;
1196 rl = r-o->color_interval;
1197 gl = g-o->color_interval;
1198 bl = b-o->color_interval;
1200 upper = (col_interp(ru, o->colcal_upper[0]) << 16) |
1201 (col_interp(gu, o->colcal_upper[1]) << 8) |
1202 (col_interp(bu, o->colcal_upper[2]));
1204 lower = (col_interp(rl, o->colcal_lower[0]) << 16) |
1205 (col_interp(gl, o->colcal_lower[1]) << 8) |
1206 (col_interp(bl, o->colcal_lower[2]));
1208 //printf("0x%06x 0x%06x\n",upper,lower);
1209 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_UPPER,upper);
1210 overlay_set_attribute(o,EM9010_ATTRIBUTE_KEYCOLOR_LOWER,lower);
1211 return 0;
1214 static void least_sq_fit(int *x, int *y, int n, float *k, float *m)
1216 float sx=0,sy=0,sxx=0,sxy=0;
1217 float delta,b;
1218 int i;
1220 for(i=0; i < n; i++) {
1221 sx=sx+x[i];
1222 sy=sy+y[i];
1223 sxx=sxx+x[i]*x[i];
1224 sxy=sxy+x[i]*y[i];
1227 delta=sxx*n-sx*sx;
1229 *m=(sxx*sy-sx*sxy)/delta;
1230 *k=(sxy*n-sx*sy)/delta;
1233 static int overlay_autocalibrate(overlay_t *o, pattern_drawer_cb pd, void *arg)
1235 em8300_overlay_calibrate_t cal;
1236 em8300_overlay_window_t win;
1237 int x[256],r[256],g[256],b[256],n;
1238 float k,m;
1240 int i;
1242 o->draw_pattern=pd;
1243 o->dp_arg = arg;
1245 overlay_set_mode(o, EM8300_OVERLAY_MODE_OVERLAY);
1246 overlay_set_screen(o, o->xres, o->yres, o->depth);
1248 /* Calibrate Y-offset */
1250 o->draw_pattern(0x0000ff, 0, 0, 0, 355, 1, o->dp_arg);
1252 cal.cal_mode = EM8300_OVERLAY_CALMODE_YOFFSET;
1253 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1255 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayYOffsetValues);
1256 return -1;
1258 o->yoffset = cal.result;
1259 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_YOffset,cal.result);
1261 /* Calibrate X-offset */
1263 o->draw_pattern(0x0000ff, 0, 0, 0, 2, 288, o->dp_arg);
1265 cal.cal_mode = EM8300_OVERLAY_CALMODE_XOFFSET;
1266 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1268 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXOffsetValues);
1269 return -1;
1271 o->xoffset = cal.result;
1272 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XOffset,cal.result);
1274 /* Calibrate X scale correction */
1276 o->draw_pattern(0x0000ff, 0, 355, 0, 2, 288, o->dp_arg);
1278 cal.cal_mode = EM8300_OVERLAY_CALMODE_XCORRECTION;
1279 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1281 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedGettingOverlayXScaleCorrection);
1282 return -1;
1284 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_DXR3_XCorrection,cal.result);
1285 o->xcorr = cal.result;
1287 win.xpos = 10;
1288 win.ypos = 10;
1289 win.width = o->xres-20;
1290 win.height = o->yres-20;
1291 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_SETWINDOW, &win)==-1) {
1292 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow);
1293 exit(1);
1296 /* Calibrate key color upper limit */
1298 for(i=128,n=0; i <= 0xff; i+=4) {
1299 o->draw_pattern(i | (i << 8) | (i << 16), 0,
1300 (o->xres-200)/2,0,200,o->yres,o->dp_arg);
1302 cal.arg = i;
1303 cal.arg2 = 1;
1304 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1306 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1308 return -1 ;
1311 x[n] = i;
1312 r[n] = (cal.result>>16)&0xff;
1313 g[n] = (cal.result>>8)&0xff;
1314 b[n] = (cal.result)&0xff;
1315 n++;
1318 least_sq_fit(x,r,n,&o->colcal_upper[0].k,&o->colcal_upper[0].m);
1319 least_sq_fit(x,g,n,&o->colcal_upper[1].k,&o->colcal_upper[1].m);
1320 least_sq_fit(x,b,n,&o->colcal_upper[2].k,&o->colcal_upper[2].m);
1322 /* Calibrate key color lower limit */
1324 for(i=128,n=0; i <= 0xff; i+=4) {
1325 o->draw_pattern(i | (i << 8) | (i << 16), 0xffffff,
1326 (o->xres-200)/2,0,200,o->yres, o->dp_arg);
1328 cal.arg = i;
1329 cal.arg2 = 2;
1330 cal.cal_mode = EM8300_OVERLAY_CALMODE_COLOR;
1332 if (ioctl(o->dev, EM8300_IOCTL_OVERLAY_CALIBRATE, &cal))
1334 return -1 ;
1336 x[n] = i;
1337 r[n] = (cal.result>>16)&0xff;
1338 g[n] = (cal.result>>8)&0xff;
1339 b[n] = (cal.result)&0xff;
1340 n++;
1343 least_sq_fit(x,r,n,&o->colcal_lower[0].k,&o->colcal_lower[0].m);
1344 least_sq_fit(x,g,n,&o->colcal_lower[1].k,&o->colcal_lower[1].m);
1345 least_sq_fit(x,b,n,&o->colcal_lower[2].k,&o->colcal_lower[2].m);
1347 overlay_set_mode(o, EM8300_OVERLAY_MODE_OFF);
1349 return 0;
1353 static int overlay_signalmode(overlay_t *o, int mode) {
1354 if(ioctl(o->dev, EM8300_IOCTL_OVERLAY_SIGNALMODE, &mode) ==-1) {
1355 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_DXR3_FailedSetSignalMix);
1356 return -1;
1358 return 0;
1360 /* End overlay.c */