2 * vo_dxr3.c - DXR3/H+ video out
4 * Copyright (C) 2002-2003 David Holm <david@realityrift.com>
8 #include <linux/em8300.h>
11 #include <sys/types.h>
12 #include <sys/select.h>
28 #include "fastmemcpy.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
36 #include "gui/interface.h"
39 #include "x11_common.h"
41 #include "libavutil/avstring.h"
45 static const vo_info_t info
=
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.
67 int dxr3_device_num
= 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
];
80 /* on screen display/subpics */
81 static char *osdpicbuf
;
82 static int osdpicbuf_w
;
83 static int osdpicbuf_h
;
85 static encodedata
*spued
;
86 static encodedata
*spubuf
;
90 /* Static variable used in ioctl's */
93 static int pts_offset
;
94 static int old_vmode
= -1;
99 Simple analog overlay API for DXR3/H+ linux driver.
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
);
122 int xres
, yres
,depth
;
123 int xoffset
,yoffset
,xcorr
;
127 struct coeff colcal_upper
[3];
128 struct coeff colcal_lower
[3];
129 float color_interval
;
131 pattern_drawer_cb draw_pattern
;
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
);
156 #define KEY_COLOR 0x80a040
157 static XWindowAttributes xwin_attribs
;
158 static overlay_t
*overlay_data
;
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
)
168 case VOCTRL_GUISUPPORT
:
170 case VOCTRL_GUI_NOWINDOW
:
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
);
185 case VOCTRL_FULLSCREEN
:
188 overlay_signalmode(overlay_data
,
189 vo_fs
? EM8300_OVERLAY_SIGNAL_ONLY
:
190 EM8300_OVERLAY_SIGNAL_WITH_VGA
);
197 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
198 pts_offset
= vo_pts
- (ioval
<< 1);
199 if (pts_offset
< 0) {
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
);
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
);
222 fd_video
= open(fdv_name
, O_WRONLY
);
224 fd_spu
= open(fds_name
, O_WRONLY
);
229 case VOCTRL_QUERY_FORMAT
:
233 if (*((uint32_t*)data
) != IMGFMT_MPEGPES
)
236 flag
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_SPU
;
241 case VOCTRL_SET_EQUALIZER
:
244 struct voctrl_set_equalizer_args
*args
= data
;
246 if (ioctl(fd_control
, EM8300_IOCTL_GETBCS
, &bcs
) < 0)
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)
260 case VOCTRL_GET_EQUALIZER
:
263 struct voctrl_get_equalizer_args
*args
= data
;
265 if (ioctl(fd_control
, EM8300_IOCTL_GETBCS
, &bcs
) < 0)
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
;
282 void calculate_cvals(unsigned long mask
, int *shift
, int *prec
)
284 /* Calculate shift and precision */
288 while (!(mask
& 0x1)) {
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 */
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
);
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;
324 reg
.val
= MVCOMMAND_SYNC
;
325 ioctl(fd_control
, EM8300_IOCTL_WRITEREG
, ®
);
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.
339 ioctl(fd_control
, EM8300_IOCTL_SCR_SETSPEED
, &ioval
);
341 /* Store some variables statically that we need later in another scope */
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
);
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) {
364 ioval
= EM8300_VIDEOMODE_PAL60
;
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");
374 ioval
= EM8300_VIDEOMODE_NTSC
;
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");
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);
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));
412 ioval
= EM8300_ASPECTRATIO_4_3
;
413 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_SettingAspectRatioTo43
);
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
);
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;
427 //printf("VO: [dxr3] sw/sh:dw/dh ->%i,%i,%i,%i\n",s_width,s_height,d_width,d_height);
433 osdpicbuf
= calloc( 1,s_width
* s_height
);
434 if (osdpicbuf
== NULL
) {
435 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
438 spued
= (encodedata
*) malloc(sizeof(encodedata
));
441 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
444 spubuf
= (encodedata
*) malloc(sizeof(encodedata
));
445 if (spubuf
== NULL
) {
448 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
453 osdpicbuf_w
= s_width
;
454 osdpicbuf_h
= s_height
;
457 pixbuf_encode_rle( 0,0,osdpicbuf_w
,osdpicbuf_h
- 1,osdpicbuf
,osdpicbuf_w
,spubuf
);
464 XSetWindowAttributes xswa
;
466 unsigned long xswamask
;
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;
478 vo_dheight
= d_height
;
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) {
489 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
493 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
), &xwin_attribs
);
494 depth
= xwin_attribs
.depth
;
495 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32) {
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
);
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
);
551 static void draw_alpha(int x
, int y
, int w
, int h
, unsigned char* src
, unsigned char *srca
, int srcstride
)
554 unsigned char *buf
= &osdpicbuf
[(y
* osdpicbuf_w
) + x
];
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;
566 pixbuf_encode_rle(x
, y
, osdpicbuf_w
, osdpicbuf_h
- 1, osdpicbuf
, osdpicbuf_w
, spued
);
570 extern int vo_osd_changed_flag
;
571 extern mp_osd_obj_t
* vo_osd_list
;
573 static void draw_osd(void)
576 static int cleared
= 0;
579 if ((disposd
% 15) == 0)
582 mp_osd_obj_t
* obj
= vo_osd_list
;
583 vo_update_osd( osd_w
,osd_h
);
586 if ( obj
->flags
& OSDFLAG_VISIBLE
) { changed
=1; break; }
592 vo_draw_text(osd_w
, osd_h
, draw_alpha
);
593 memset(osdpicbuf
, 0, s_width
* s_height
);
600 spued
->count
=spubuf
->count
;
601 fast_memcpy( spued
->data
,spubuf
->data
,DATASIZE
);
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 */
614 write(fd_spu
, spued
->data
, spued
->count
);
622 static int draw_frame(uint8_t * src
[])
624 vo_mpegpes_t
*p
= (vo_mpegpes_t
*) src
[0];
628 write(fd_spu
, p
->data
, p
->size
);
631 write(fd_video
, p
->data
, p
->size
);
635 static void flip_page(void)
639 int event
= vo_x11_check_events(mDisplay
);
640 if (event
& VO_EVENT_RESIZE
) {
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
) {
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
);
658 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
662 ioctl(fd_control
, EM8300_IOCTL_SCR_SET
, &ioval
);
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) {
673 ioval
= vo_pts
+ pts_offset
;
674 ioctl(fd_video
, EM8300_IOCTL_SPU_SETPTS
, &ioval
);
675 ioctl(fd_video
, EM8300_IOCTL_VIDEO_SETPTS
, &ioval
);
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
)
688 static void uninit(void)
690 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Uninitializing
);
693 overlay_set_mode(overlay_data
, EM8300_OVERLAY_MODE_OFF
);
694 overlay_release(overlay_data
);
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
);
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 */
742 if (!strncmp("prebuf", arg
, 6) && !dxr3_prebuf
) {
743 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_EnablingPrebuffering
);
745 } else if (!strncmp("sync", arg
, 4) && !dxr3_newsync
) {
746 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UsingNewSyncEngine
);
748 } else if (!strncmp("overlay", arg
, 7) && !dxr3_overlay
) {
750 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UsingOverlay
);
753 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_ErrorYouNeedToCompileMplayerWithX11
);
755 } else if (!strncmp("norm=", 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
);
764 mp_msg(MSGT_VO
,MSGL_INFO
, "NTSC");
765 } else if (*arg
== '4') {
767 mp_msg(MSGT_VO
,MSGL_INFO
, "PAL-60");
768 } else if (*arg
== '3') {
770 mp_msg(MSGT_VO
,MSGL_INFO
, "PAL");
771 } else if (*arg
== '2') {
773 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALPAL60
);
774 } else if (*arg
== '1') {
776 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALNTSC
);
777 } else if (*arg
== '0') {
779 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UseCurrentNorm
);
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
, ':');
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
);
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
);
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
);
822 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellMV
);
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
);
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
);
840 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellSP
);
845 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Opened
, devname
);
847 strcpy(fds_name
, devname
);
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
858 XWindowAttributes attribs
;
860 dpy
= XOpenDisplay(NULL
);
862 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToOpenDisplayDuringHackSetup
);
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
);
874 /* End of fucked up hack */
876 /* Initialize overlay and X11 */
877 overlay_data
= overlay_init(fd_control
);
882 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToInitX11
);
892 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
893 pts_offset
= vo_pts
- (ioval
<< 1);
894 if (pts_offset
< 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
);
913 static int overlay_set_attribute(overlay_t
*o
, int attribute
, int value
)
915 em8300_attribute_t attr
;
917 attr
.attribute
= attribute
;
919 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SET_ATTRIBUTE
, &attr
)==-1)
921 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSettingOverlayAttribute
);
928 static overlay_t
*overlay_init(int dev
)
932 o
= (overlay_t
*) malloc(sizeof(overlay_t
));
937 memset(o
,0,sizeof(overlay_t
));
940 o
->xres
= 1280; o
->yres
=1024; o
->xcorr
=1000;
941 o
->color_interval
=10;
946 static int overlay_release(overlay_t
*o
)
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
},
979 p
= malloc(sizeof(m
));
980 memcpy(p
,m
,sizeof(m
));
984 static int lookup_parameter(overlay_t
*o
, struct lut_entry
*lut
, char *name
, void **ptr
, int *type
) {
987 for(i
=0; lut
[i
].name
; i
++) {
988 if(!strcmp(name
,lut
[i
].name
)) {
997 static int overlay_read_state(overlay_t
*o
, char *p
)
1000 char path
[128],fname
[128],tmp
[128],line
[256];
1002 struct lut_entry
*lut
;
1008 av_strlcpy(fname
, getenv("HOME"), sizeof( fname
));
1009 av_strlcat(fname
,"/.overlay", sizeof( fname
));
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")))
1019 lut
= new_lookuptable(o
);
1022 if(!fgets(line
,256,fp
))
1024 tok
=strtok(line
," ");
1025 if(lookup_parameter(o
,lut
,tok
,&ptr
,&type
)) {
1026 tok
=strtok(NULL
," ");
1029 sscanf(tok
,"%d",(int *)ptr
);
1032 sscanf(tok
,"%x",(int *)ptr
);
1035 sscanf(tok
,"%f",(float *)ptr
);
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
," ");
1050 update_parameters(o
);
1057 static void overlay_update_params(overlay_t
*o
) {
1058 update_parameters(o
);
1061 static int overlay_write_state(overlay_t
*o
, char *p
)
1064 char path
[128],fname
[128],tmp
[128];
1066 char line
[256],*tok
;
1067 struct lut_entry
*lut
;
1071 av_strlcpy(fname
, getenv("HOME"), sizeof( fname
));
1072 av_strlcat(fname
,"/.overlay", sizeof( fname
));
1074 av_strlcpy(fname
, p
, sizeof( fname
));
1076 if(access(fname
, W_OK
|X_OK
|R_OK
)) {
1077 if(mkdir(fname
,0766))
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")))
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
) {
1093 fprintf(fp
,"%d\n",*(int *)lut
[i
].ptr
);
1096 fprintf(fp
,"%06x\n",*(int *)lut
[i
].ptr
);
1099 fprintf(fp
,"%f\n",*(float *)lut
[i
].ptr
);
1103 fprintf(fp
,"%f %f ",((struct coeff
*)lut
[i
].ptr
)[j
].k
,
1104 ((struct coeff
*)lut
[i
].ptr
)[j
].m
);
1114 static int overlay_set_screen(overlay_t
*o
, int xres
, int yres
, int depth
)
1116 em8300_overlay_screen_t scr
;
1125 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SETSCREEN
, &scr
)==-1)
1127 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSettingOverlayScreen
);
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
);
1142 static int overlay_set_window(overlay_t
*o
, int xpos
,int ypos
,int width
,int height
)
1144 em8300_overlay_window_t win
;
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
);
1158 static int overlay_set_bcs(overlay_t
*o
, int brightness
, int contrast
, int saturation
)
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
);
1173 static int col_interp(float x
, struct coeff c
)
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);
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
);
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;
1220 for(i
=0; i
< n
; i
++) {
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
;
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
);
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
);
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
);
1284 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_XCorrection
,cal
.result
);
1285 o
->xcorr
= cal
.result
;
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
);
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
);
1304 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_COLOR
;
1306 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1312 r
[n
] = (cal
.result
>>16)&0xff;
1313 g
[n
] = (cal
.result
>>8)&0xff;
1314 b
[n
] = (cal
.result
)&0xff;
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
);
1330 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_COLOR
;
1332 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1337 r
[n
] = (cal
.result
>>16)&0xff;
1338 g
[n
] = (cal
.result
>>8)&0xff;
1339 b
[n
] = (cal
.result
)&0xff;
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
);
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
);