2 * vo_dxr3.c - DXR3/H+ video out
4 * Copyright (C) 2002-2003 David Holm <david@realityrift.com>
8 /* ChangeLog added 2002-01-10
10 * Added a patch from Anders Rune Jensen to support the latest em8300 CVS
14 * Yet another patch from Tamas Kohegyi to fix subpic placement.
17 * Added patch from Tamas Kohegyi to fix subpic placement with freetype.
20 * Added patch from Jens Axboe that makes vo_dxr3 return to previous TV norm
22 * Added patch from Thomas Jarosch that fixed a lot of textual ouput
25 * 2002-12-24: (Hohoho)
26 * Added patch from Thomas Jarosch <tomj@simonv.com> which adds support
27 * for setting the TV norm by movie framerate.
30 * Cleaned up syncing code and renamed setup variables so
31 * they can be accessed from the GUI.
34 * Added native overlay support, activate with :overlay
35 * you have to run dxr3view to modify settings (or manually
36 * edit the files in ~/.overlay.
39 * Added new sync-engine, activate with :sync option.
40 * Greatly improved commandline parser.
41 * Replaced :noprebuf with :prebuf and made noprebuf the default.
44 * Fixed multicard bug on athlons
47 * Disabled spuenc support, this is still not stable enough =(
50 * Removed lavc and fame encoder to be compatible with new libvo style.
51 * Added graphic equalizer support.
54 * The spuenc code isn't 100% stable yet, therefore I'm disabling
55 * it due to the upcoming stable release.
58 * Carl George added spuenc support
61 * XorA added an option parser and support for selecting encoder
62 * codec. We thank him again.
65 * A couple of bugfixes by XorA
68 * Thanks to Marcel Hild <hild@b4mad.net> the jitter-bug experienced
69 * with some videos have been fixed, many thanks goes to him.
72 * Fixed problems with fame, it gives a better picture than avcodec,
73 * but is slightly slower. Most notably the wobbling effect is gone
77 * Preliminary fame support added (it breaks after seeking, why?)
80 * Fixed sync problems when pausing video (while using prebuffering)
83 * Fixed bug which would case invalid output when using :noprebuf
84 * Removed equalization code, it caused problems on slow systems
87 * Using the swscaler instead of the old hand coded shit. (Checkout man mplayer and search for sws ;).
88 * Using aspect function to setup a proper mpeg1, no more hassling with odd resolutions or GOP-sizes,
89 * this would only create jitter on some vids!
90 * The swscaler sometimes exits with sig8 on mpegs, I don't know why yet (just use -vc mpegpes in this
91 * case, and report to me if you have any avi's etc which does this...)
94 * Thanks to the new control() method I have finally been able to enable the em8300 prebuffering.
95 * This should speed up playback on all systems, the vout cpu usage should rocket since I will be hogging
96 * the pci bus. Not to worry though, since frames are prebuffered it should be able to take a few blows
97 * if you start doing other stuff simultaneously.
100 * Removal of libmp1e, libavcodec has finally become faster (and it's code is helluva lot cleaner)
103 * Cleaned out some old code which might have slowed down writes
106 * Testrelease of new sync engine (using previously undocumented feature of em8300).
109 * Preliminary subpic support with -vc mpegpes and dvd's
110 * Device interfaces tries the new naming scheme by default (even though most users probably still use the old one)
113 * I rehauled the entire codebase. I have now changed to
114 * Kernighan & Ritchie codingstyle, please mail me if you
115 * find any inconcistencies.
118 #include <linux/em8300.h>
119 #include <sys/ioctl.h>
120 #include <sys/stat.h>
121 #include <sys/types.h>
122 #include <sys/select.h>
138 #include "fastmemcpy.h"
140 #include "video_out.h"
141 #include "video_out_internal.h"
146 #include "gui/interface.h"
149 #include "x11_common.h"
151 #include "libavutil/avstring.h"
155 static const vo_info_t info
=
159 "David Holm <dholm@iname.com>",
162 const LIBVO_EXTERN (dxr3
)
164 /* Resolutions and positions */
165 static int v_width
, v_height
;
166 static int s_width
, s_height
;
167 static int osd_w
, osd_h
;
168 static int img_format
;
170 /* Configuration values
171 * Don't declare these static, they
172 * should be accessible from the gui.
175 int dxr3_newsync
= 0;
176 int dxr3_overlay
= 0;
177 int dxr3_device_num
= 0;
180 #define MAX_STR_SIZE 80 /* length for the static strings */
182 /* File descriptors */
183 static int fd_control
= -1;
184 static int fd_video
= -1;
185 static int fd_spu
= -1;
186 static char fdv_name
[MAX_STR_SIZE
];
187 static char fds_name
[MAX_STR_SIZE
];
190 /* on screen display/subpics */
191 static char *osdpicbuf
;
192 static int osdpicbuf_w
;
193 static int osdpicbuf_h
;
195 static encodedata
*spued
;
196 static encodedata
*spubuf
;
200 /* Static variable used in ioctl's */
203 static int pts_offset
;
204 static int old_vmode
= -1;
207 /* Begin overlay.h */
209 Simple analog overlay API for DXR3/H+ linux driver.
215 /* Pattern drawing callback used by the calibration functions.
216 The function is expected to:
217 Clear the entire screen.
218 Fill the screen with color bgcol (0xRRGGBB)
219 Draw a rectangle at (xpos,ypos) of size (width,height) in fgcol (0xRRGGBB)
222 typedef int (*pattern_drawer_cb
)(int fgcol
, int bgcol
,
223 int xpos
, int ypos
, int width
, int height
, void *arg
);
232 int xres
, yres
,depth
;
233 int xoffset
,yoffset
,xcorr
;
237 struct coeff colcal_upper
[3];
238 struct coeff colcal_lower
[3];
239 float color_interval
;
241 pattern_drawer_cb draw_pattern
;
246 static overlay_t
*overlay_init(int dev
);
247 static int overlay_release(overlay_t
*);
249 static int overlay_read_state(overlay_t
*o
, char *path
);
250 static int overlay_write_state(overlay_t
*o
, char *path
);
252 static int overlay_set_screen(overlay_t
*o
, int xres
, int yres
, int depth
);
253 static int overlay_set_mode(overlay_t
*o
, int mode
);
254 static int overlay_set_attribute(overlay_t
*o
, int attribute
, int val
);
255 static int overlay_set_keycolor(overlay_t
*o
, int color
);
256 static int overlay_set_window(overlay_t
*o
, int xpos
,int ypos
,int width
,int height
);
257 static int overlay_set_bcs(overlay_t
*o
, int brightness
, int contrast
, int saturation
);
259 static int overlay_autocalibrate(overlay_t
*o
, pattern_drawer_cb pd
, void *arg
);
260 static void overlay_update_params(overlay_t
*o
);
261 static int overlay_signalmode(overlay_t
*o
, int mode
);
266 #define KEY_COLOR 0x80a040
267 static XWindowAttributes xwin_attribs
;
268 static overlay_t
*overlay_data
;
272 /* Functions for working with the em8300's internal clock */
273 /* End of internal clock functions */
275 static int control(uint32_t request
, void *data
, ...)
278 case VOCTRL_GUISUPPORT
:
280 case VOCTRL_GUI_NOWINDOW
:
285 case VOCTRL_SET_SPU_PALETTE
:
286 if (ioctl(fd_spu
, EM8300_IOCTL_SPU_SETPALETTE
, data
) < 0) {
287 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToLoadNewSPUPalette
);
295 case VOCTRL_FULLSCREEN
:
298 overlay_signalmode(overlay_data
,
299 vo_fs
? EM8300_OVERLAY_SIGNAL_ONLY
:
300 EM8300_OVERLAY_SIGNAL_WITH_VGA
);
307 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
308 pts_offset
= vo_pts
- (ioval
<< 1);
309 if (pts_offset
< 0) {
315 ioval
= EM8300_PLAYMODE_PLAY
;
316 if (ioctl(fd_control
, EM8300_IOCTL_SET_PLAYMODE
, &ioval
) < 0) {
317 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToSetPlaymode
);
323 ioval
= EM8300_PLAYMODE_PAUSED
;
324 if (ioctl(fd_control
, EM8300_IOCTL_SET_PLAYMODE
, &ioval
) < 0) {
325 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToSetPlaymode
);
332 fd_video
= open(fdv_name
, O_WRONLY
);
334 fd_spu
= open(fds_name
, O_WRONLY
);
339 case VOCTRL_QUERY_FORMAT
:
343 if (*((uint32_t*)data
) != IMGFMT_MPEGPES
)
346 flag
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_SPU
;
351 case VOCTRL_SET_EQUALIZER
:
358 value
= va_arg(ap
, int);
361 if (ioctl(fd_control
, EM8300_IOCTL_GETBCS
, &bcs
) < 0)
363 if (!strcasecmp(data
, "brightness"))
364 bcs
.brightness
= (value
+100)*5;
365 else if (!strcasecmp(data
, "contrast"))
366 bcs
.contrast
= (value
+100)*5;
367 else if (!strcasecmp(data
, "saturation"))
368 bcs
.saturation
= (value
+100)*5;
369 else return VO_FALSE
;
371 if (ioctl(fd_control
, EM8300_IOCTL_SETBCS
, &bcs
) < 0)
375 case VOCTRL_GET_EQUALIZER
:
382 value
= va_arg(ap
, int*);
385 if (ioctl(fd_control
, EM8300_IOCTL_GETBCS
, &bcs
) < 0)
388 if (!strcasecmp(data
, "brightness"))
389 *value
= (bcs
.brightness
/5)-100;
390 else if (!strcasecmp(data
, "contrast"))
391 *value
= (bcs
.contrast
/5)-100;
392 else if (!strcasecmp(data
, "saturation"))
393 *value
= (bcs
.saturation
/5)-100;
394 else return VO_FALSE
;
402 void calculate_cvals(unsigned long mask
, int *shift
, int *prec
)
404 /* Calculate shift and precision */
408 while (!(mask
& 0x1)) {
419 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
)
421 int tmp1
, tmp2
, size
;
422 em8300_register_t reg
;
423 extern float monitor_aspect
;
425 /* Softzoom turned on, downscale */
426 /* This activates the subpicture processor, you can safely disable this and still send */
427 /* broken subpics to the em8300, if it's enabled and you send broken subpics you will end */
429 ioval
= EM8300_SPUMODE_ON
;
430 if (ioctl(fd_control
, EM8300_IOCTL_SET_SPUMODE
, &ioval
) < 0) {
431 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToSetSubpictureMode
);
436 /* Set the playmode to play (just in case another app has set it to something else) */
437 ioval
= EM8300_PLAYMODE_PLAY
;
438 if (ioctl(fd_control
, EM8300_IOCTL_SET_PLAYMODE
, &ioval
) < 0) {
439 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToSetPlaymode
);
442 /* Start em8300 prebuffering and sync engine */
443 reg
.microcode_register
= 1;
445 reg
.val
= MVCOMMAND_SYNC
;
446 ioctl(fd_control
, EM8300_IOCTL_WRITEREG
, ®
);
448 /* Clean buffer by syncing it */
449 ioval
= EM8300_SUBDEVICE_VIDEO
;
450 ioctl(fd_control
, EM8300_IOCTL_FLUSH
, &ioval
);
451 ioval
= EM8300_SUBDEVICE_AUDIO
;
452 ioctl(fd_control
, EM8300_IOCTL_FLUSH
, &ioval
);
454 /* Sync the video device to make sure the buffers are empty
455 * and set the playback speed to normal. Also reset the
456 * em8300 internal clock.
460 ioctl(fd_control
, EM8300_IOCTL_SCR_SETSPEED
, &ioval
);
462 /* Store some variables statically that we need later in another scope */
467 /* Set monitor_aspect to avoid jitter */
468 monitor_aspect
= (float) width
/ (float) height
;
470 if (ioctl(fd_control
, EM8300_IOCTL_GET_VIDEOMODE
, &old_vmode
) < 0) {
471 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToGetTVNorm
);
476 if (dxr3_norm
!= 0) {
477 if (dxr3_norm
== 5) {
478 ioval
= EM8300_VIDEOMODE_NTSC
;
479 } else if (dxr3_norm
== 4) {
480 ioval
= EM8300_VIDEOMODE_PAL60
;
481 } else if (dxr3_norm
== 3) {
482 ioval
= EM8300_VIDEOMODE_PAL
;
483 } else if (dxr3_norm
== 2) {
485 ioval
= EM8300_VIDEOMODE_PAL60
;
487 ioval
= EM8300_VIDEOMODE_PAL
;
490 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate
);
491 ioval
== EM8300_VIDEOMODE_PAL60
? mp_msg(MSGT_VO
,MSGL_INFO
, "PAL-60") : mp_msg(MSGT_VO
,MSGL_INFO
, "PAL");
495 ioval
= EM8300_VIDEOMODE_NTSC
;
497 ioval
= EM8300_VIDEOMODE_PAL
;
500 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoSelectedTVNormByFrameRate
);
501 ioval
== EM8300_VIDEOMODE_NTSC
? mp_msg(MSGT_VO
,MSGL_INFO
, "NTSC") : mp_msg(MSGT_VO
,MSGL_INFO
, "PAL");
505 if (old_vmode
!= ioval
) {
506 if (ioctl(fd_control
, EM8300_IOCTL_SET_VIDEOMODE
, &ioval
) < 0) {
507 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToSetTVNorm
);
513 /* libavcodec requires a width and height that is x|16 */
514 aspect_save_orig(width
, height
);
515 aspect_save_prescale(d_width
, d_height
);
516 ioctl(fd_control
, EM8300_IOCTL_GET_VIDEOMODE
, &ioval
);
517 if (ioval
== EM8300_VIDEOMODE_NTSC
) {
518 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_SettingUpForNTSC
);
519 aspect_save_screenres(352, 240);
521 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_SettingUpForPALSECAM
);
522 aspect_save_screenres(352, 288);
524 aspect(&s_width
, &s_height
, A_ZOOM
);
525 s_width
-= s_width
% 16;
526 s_height
-= s_height
% 16;
528 /* Try to figure out whether to use widescreen output or not */
529 /* Anamorphic widescreen modes makes this a pain in the ass */
530 tmp1
= abs(d_height
- ((d_width
/ 4) * 3));
531 tmp2
= abs(d_height
- (int) (d_width
/ 2.35));
533 ioval
= EM8300_ASPECTRATIO_4_3
;
534 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_SettingAspectRatioTo43
);
536 ioval
= EM8300_ASPECTRATIO_16_9
;
537 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_SettingAspectRatioTo169
);
539 ioctl(fd_control
, EM8300_IOCTL_SET_ASPECTRATIO
, &ioval
);
543 if (ioval
== EM8300_ASPECTRATIO_16_9
) {
544 s_width
*= d_height
*1.78/s_height
*(d_width
*1.0/d_height
)/2.35;
548 //printf("VO: [dxr3] sw/sh:dw/dh ->%i,%i,%i,%i\n",s_width,s_height,d_width,d_height);
554 osdpicbuf
= calloc( 1,s_width
* s_height
);
555 if (osdpicbuf
== NULL
) {
556 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
559 spued
= (encodedata
*) malloc(sizeof(encodedata
));
562 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
565 spubuf
= (encodedata
*) malloc(sizeof(encodedata
));
566 if (spubuf
== NULL
) {
569 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_OutOfMemory
);
574 osdpicbuf_w
= s_width
;
575 osdpicbuf_h
= s_height
;
578 pixbuf_encode_rle( 0,0,osdpicbuf_w
,osdpicbuf_h
- 1,osdpicbuf
,osdpicbuf_w
,spubuf
);
585 XSetWindowAttributes xswa
;
587 unsigned long xswamask
;
592 int depth
, red_shift
, red_prec
, green_shift
, green_prec
, blue_shift
, blue_prec
, acq_color
;
593 em8300_overlay_screen_t ovlscr
;
594 em8300_attribute_t ovlattr
;
596 vo_dx
= (vo_screenwidth
- d_width
) / 2;
597 vo_dy
= (vo_screenheight
- d_height
) / 2;
599 vo_dheight
= d_height
;
602 guiGetEvent(guiSetShVideo
, 0);
603 XSetWindowBackground(mDisplay
, vo_window
, KEY_COLOR
);
604 XClearWindow(mDisplay
, vo_window
);
605 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
), &xwin_attribs
);
606 depth
= xwin_attribs
.depth
;
607 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32) {
610 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
614 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
), &xwin_attribs
);
615 depth
= xwin_attribs
.depth
;
616 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32) {
619 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
620 vo_x11_create_vo_window(&vinfo
, vo_dx
, vo_dy
,
621 d_width
, d_height
, flags
,
622 CopyFromParent
, "Viewing Window", title
);
623 xswa
.background_pixel
= KEY_COLOR
;
624 xswa
.border_pixel
= 0;
625 xswamask
= CWBackPixel
| CWBorderPixel
;
626 XChangeWindowAttributes(mDisplay
, vo_window
, xswamask
, &xswa
);
629 /* Start setting up overlay */
630 XGetWindowAttributes(mDisplay
, mRootWin
, &xwin_attribs
);
631 overlay_set_screen(overlay_data
, xwin_attribs
.width
, xwin_attribs
.height
, xwin_attribs
.depth
);
632 overlay_read_state(overlay_data
, NULL
);
634 /* Allocate keycolor */
635 cmap
= vo_x11_create_colormap(&vinfo
);
636 calculate_cvals(vinfo
.red_mask
, &red_shift
, &red_prec
);
637 calculate_cvals(vinfo
.green_mask
, &green_shift
, &green_prec
);
638 calculate_cvals(vinfo
.blue_mask
, &blue_shift
, &blue_prec
);
640 key_color
.red
= ((KEY_COLOR
>> 16) & 0xff) * 256;
641 key_color
.green
= ((KEY_COLOR
>> 8) & 0xff) * 256;
642 key_color
.blue
= (KEY_COLOR
& 0xff) * 256;
643 key_color
.pixel
= (((key_color
.red
>> (16 - red_prec
)) << red_shift
) +
644 ((key_color
.green
>> (16 - green_prec
)) << green_shift
) +
645 ((key_color
.blue
>> (16 - blue_prec
)) << blue_shift
));
646 key_color
.flags
= DoRed
| DoGreen
| DoBlue
;
647 if (!XAllocColor(mDisplay
, cmap
, &key_color
)) {
648 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToAllocateKeycolor
);
652 acq_color
= ((key_color
.red
/ 256) << 16) | ((key_color
.green
/ 256) << 8) | key_color
.blue
;
653 if (key_color
.pixel
!= KEY_COLOR
) {
654 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_UnableToAllocateExactKeycolor
, key_color
.pixel
);
657 /* Set keycolor and activate overlay */
658 XSetWindowBackground(mDisplay
, vo_window
, key_color
.pixel
);
659 XClearWindow(mDisplay
, vo_window
);
660 overlay_set_keycolor(overlay_data
, key_color
.pixel
);
661 overlay_set_mode(overlay_data
, EM8300_OVERLAY_MODE_OVERLAY
);
662 overlay_set_mode(overlay_data
, EM8300_OVERLAY_MODE_RECTANGLE
);
665 if (vo_ontop
) vo_x11_setlayer(mDisplay
, vo_window
, vo_ontop
);
672 static void draw_alpha(int x
, int y
, int w
, int h
, unsigned char* src
, unsigned char *srca
, int srcstride
)
675 unsigned char *buf
= &osdpicbuf
[(y
* osdpicbuf_w
) + x
];
678 register int stride
= 0;
680 for (ly
= 0; ly
< h
- 1; ly
++)
682 for(lx
= 0; lx
< w
; lx
++ )
683 if ( ( srca
[stride
+ lx
] )&&( src
[stride
+ lx
] >= 128 ) ) buf
[by
+ lx
] = 3;
687 pixbuf_encode_rle(x
, y
, osdpicbuf_w
, osdpicbuf_h
- 1, osdpicbuf
, osdpicbuf_w
, spued
);
691 extern int vo_osd_changed_flag
;
692 extern mp_osd_obj_t
* vo_osd_list
;
694 static void draw_osd(void)
697 static int cleared
= 0;
700 if ((disposd
% 15) == 0)
703 mp_osd_obj_t
* obj
= vo_osd_list
;
704 vo_update_osd( osd_w
,osd_h
);
707 if ( obj
->flags
& OSDFLAG_VISIBLE
) { changed
=1; break; }
713 vo_draw_text(osd_w
, osd_h
, draw_alpha
);
714 memset(osdpicbuf
, 0, s_width
* s_height
);
721 spued
->count
=spubuf
->count
;
722 fast_memcpy( spued
->data
,spubuf
->data
,DATASIZE
);
728 /* could stand some check here to see if the subpic hasn't changed
729 * as if it hasn't and we re-send it it will "blink" as the last one
730 * is turned off, and the new one (same one) is turned on
732 /* Subpics are not stable yet =(
733 expect lockups if you enable */
735 write(fd_spu
, spued
->data
, spued
->count
);
743 static int draw_frame(uint8_t * src
[])
745 vo_mpegpes_t
*p
= (vo_mpegpes_t
*) src
[0];
749 write(fd_spu
, p
->data
, p
->size
);
752 write(fd_video
, p
->data
, p
->size
);
756 static void flip_page(void)
760 int event
= vo_x11_check_events(mDisplay
);
761 if (event
& VO_EVENT_RESIZE
) {
763 XGetWindowAttributes(mDisplay
, vo_window
, &xwin_attribs
);
764 XTranslateCoordinates(mDisplay
, vo_window
, mRootWin
, -xwin_attribs
.border_width
, -xwin_attribs
.border_width
, &xwin_attribs
.x
, &xwin_attribs
.y
, &junkwindow
);
765 overlay_set_window(overlay_data
, xwin_attribs
.x
, xwin_attribs
.y
, xwin_attribs
.width
, xwin_attribs
.height
);
767 if (event
& VO_EVENT_EXPOSE
) {
769 XSetWindowBackground(mDisplay
, vo_window
, KEY_COLOR
);
770 XClearWindow(mDisplay
, vo_window
);
771 XGetWindowAttributes(mDisplay
, vo_window
, &xwin_attribs
);
772 XTranslateCoordinates(mDisplay
, vo_window
, mRootWin
, -xwin_attribs
.border_width
, -xwin_attribs
.border_width
, &xwin_attribs
.x
, &xwin_attribs
.y
, &junkwindow
);
773 overlay_set_window(overlay_data
, xwin_attribs
.x
, xwin_attribs
.y
, xwin_attribs
.width
, xwin_attribs
.height
);
779 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
783 ioctl(fd_control
, EM8300_IOCTL_SCR_SET
, &ioval
);
785 } else if ((vo_pts
- pts_offset
) < (ioval
- 7200) || (vo_pts
- pts_offset
) > (ioval
+ 7200)) {
786 ioval
= (vo_pts
+ pts_offset
) >> 1;
787 ioctl(fd_control
, EM8300_IOCTL_SCR_SET
, &ioval
);
788 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
789 pts_offset
= vo_pts
- (ioval
<< 1);
790 if (pts_offset
< 0) {
794 ioval
= vo_pts
+ pts_offset
;
795 ioctl(fd_video
, EM8300_IOCTL_SPU_SETPTS
, &ioval
);
796 ioctl(fd_video
, EM8300_IOCTL_VIDEO_SETPTS
, &ioval
);
798 } else if (dxr3_prebuf
) {
799 ioctl(fd_spu
, EM8300_IOCTL_SPU_SETPTS
, &vo_pts
);
800 ioctl(fd_video
, EM8300_IOCTL_VIDEO_SETPTS
, &vo_pts
);
804 static int draw_slice(uint8_t *srcimg
[], int stride
[], int w
, int h
, int x0
, int y0
)
809 static void uninit(void)
811 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Uninitializing
);
814 overlay_set_mode(overlay_data
, EM8300_OVERLAY_MODE_OFF
);
815 overlay_release(overlay_data
);
827 if (old_vmode
!= -1) {
828 if (ioctl(fd_control
, EM8300_IOCTL_SET_VIDEOMODE
, &old_vmode
) < 0) {
829 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedRestoringTVNorm
);
852 static void check_events(void)
856 static int preinit(const char *arg
)
858 char devname
[MAX_STR_SIZE
];
859 int fdflags
= O_WRONLY
;
861 /* Parse commandline */
863 if (!strncmp("prebuf", arg
, 6) && !dxr3_prebuf
) {
864 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_EnablingPrebuffering
);
866 } else if (!strncmp("sync", arg
, 4) && !dxr3_newsync
) {
867 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UsingNewSyncEngine
);
869 } else if (!strncmp("overlay", arg
, 7) && !dxr3_overlay
) {
871 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UsingOverlay
);
874 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_ErrorYouNeedToCompileMplayerWithX11
);
876 } else if (!strncmp("norm=", arg
, 5)) {
878 // dxr3_norm is 0 (-> don't change norm) by default
879 // but maybe someone changes this in the future
881 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_WillSetTVNormTo
);
885 mp_msg(MSGT_VO
,MSGL_INFO
, "NTSC");
886 } else if (*arg
== '4') {
888 mp_msg(MSGT_VO
,MSGL_INFO
, "PAL-60");
889 } else if (*arg
== '3') {
891 mp_msg(MSGT_VO
,MSGL_INFO
, "PAL");
892 } else if (*arg
== '2') {
894 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALPAL60
);
895 } else if (*arg
== '1') {
897 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_AutoAdjustToMovieFrameRatePALNTSC
);
898 } else if (*arg
== '0') {
900 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UseCurrentNorm
);
903 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_UseUnknownNormSuppliedCurrentNorm
);
906 mp_msg(MSGT_VO
,MSGL_INFO
, ".\n");
907 } else if (arg
[0] == '0' || arg
[0] == '1' || arg
[0] == '2' || arg
[0] == '3') {
908 dxr3_device_num
= arg
[0];
911 arg
= strchr(arg
, ':');
918 /* Open the control interface */
919 sprintf(devname
, "/dev/em8300-%d", dxr3_device_num
);
920 fd_control
= open(devname
, fdflags
);
921 if (fd_control
< 1) {
922 /* Fall back to old naming scheme */
923 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTrying
, devname
);
924 sprintf(devname
, "/dev/em8300");
925 fd_control
= open(devname
, fdflags
);
926 if (fd_control
< 1) {
927 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWell
);
931 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Opened
, devname
);
934 /* Open the video interface */
935 sprintf(devname
, "/dev/em8300_mv-%d", dxr3_device_num
);
936 fd_video
= open(devname
, fdflags
);
938 /* Fall back to old naming scheme */
939 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingMV
, devname
);
940 sprintf(devname
, "/dev/em8300_mv");
941 fd_video
= open(devname
, fdflags
);
943 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellMV
);
948 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Opened
, devname
);
950 strcpy(fdv_name
, devname
);
952 /* Open the subpicture interface */
953 sprintf(devname
, "/dev/em8300_sp-%d", dxr3_device_num
);
954 fd_spu
= open(devname
, fdflags
);
956 /* Fall back to old naming scheme */
957 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingTryingSP
, devname
);
958 sprintf(devname
, "/dev/em8300_sp");
959 fd_spu
= open(devname
, fdflags
);
961 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_ErrorOpeningForWritingAsWellSP
);
966 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_Opened
, devname
);
968 strcpy(fds_name
, devname
);
973 /* Fucked up hack needed to enable overlay.
974 * Will be removed as soon as I figure out
975 * how to make it work like it should
979 XWindowAttributes attribs
;
981 dpy
= XOpenDisplay(NULL
);
983 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToOpenDisplayDuringHackSetup
);
986 XGetWindowAttributes(dpy
, RootWindow(dpy
, DefaultScreen(dpy
)), &attribs
);
987 ov
= overlay_init(fd_control
);
988 overlay_set_screen(ov
, attribs
.width
, attribs
.height
, PlanesOfScreen(ScreenOfDisplay(dpy
, 0)));
989 overlay_read_state(ov
, NULL
);
990 overlay_set_keycolor(ov
, KEY_COLOR
);
991 overlay_set_mode(ov
, EM8300_OVERLAY_MODE_OVERLAY
);
992 overlay_set_mode(ov
, EM8300_OVERLAY_MODE_RECTANGLE
);
995 /* End of fucked up hack */
997 /* Initialize overlay and X11 */
998 overlay_data
= overlay_init(fd_control
);
1003 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_UnableToInitX11
);
1013 ioctl(fd_control
, EM8300_IOCTL_SCR_GET
, &ioval
);
1014 pts_offset
= vo_pts
- (ioval
<< 1);
1015 if (pts_offset
< 0) {
1023 /* Begin overlay.c */
1024 static int update_parameters(overlay_t
*o
)
1026 overlay_set_attribute(o
, EM9010_ATTRIBUTE_XOFFSET
, o
->xoffset
);
1027 overlay_set_attribute(o
, EM9010_ATTRIBUTE_YOFFSET
, o
->yoffset
);
1028 overlay_set_attribute(o
, EM9010_ATTRIBUTE_XCORR
, o
->xcorr
);
1029 overlay_set_attribute(o
, EM9010_ATTRIBUTE_STABILITY
, o
->stability
);
1030 overlay_set_attribute(o
, EM9010_ATTRIBUTE_JITTER
, o
->jitter
);
1034 static int overlay_set_attribute(overlay_t
*o
, int attribute
, int value
)
1036 em8300_attribute_t attr
;
1038 attr
.attribute
= attribute
;
1040 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SET_ATTRIBUTE
, &attr
)==-1)
1042 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSettingOverlayAttribute
);
1049 static overlay_t
*overlay_init(int dev
)
1053 o
= (overlay_t
*) malloc(sizeof(overlay_t
));
1058 memset(o
,0,sizeof(overlay_t
));
1061 o
->xres
= 1280; o
->yres
=1024; o
->xcorr
=1000;
1062 o
->color_interval
=10;
1067 static int overlay_release(overlay_t
*o
)
1076 #define TYPE_COEFF 3
1077 #define TYPE_FLOAT 4
1085 static struct lut_entry
*new_lookuptable(overlay_t
*o
)
1087 struct lut_entry m
[] = {
1088 {"xoffset", TYPE_INT
, &o
->xoffset
},
1089 {"yoffset", TYPE_INT
, &o
->yoffset
},
1090 {"xcorr", TYPE_INT
, &o
->xcorr
},
1091 {"jitter", TYPE_INT
, &o
->jitter
},
1092 {"stability", TYPE_INT
, &o
->stability
},
1093 {"keycolor", TYPE_XINT
, &o
->keycolor
},
1094 {"colcal_upper", TYPE_COEFF
, &o
->colcal_upper
[0]},
1095 {"colcal_lower", TYPE_COEFF
, &o
->colcal_lower
[0]},
1096 {"color_interval", TYPE_FLOAT
, &o
->color_interval
},
1100 p
= malloc(sizeof(m
));
1101 memcpy(p
,m
,sizeof(m
));
1105 static int lookup_parameter(overlay_t
*o
, struct lut_entry
*lut
, char *name
, void **ptr
, int *type
) {
1108 for(i
=0; lut
[i
].name
; i
++) {
1109 if(!strcmp(name
,lut
[i
].name
)) {
1111 *type
= lut
[i
].type
;
1118 static int overlay_read_state(overlay_t
*o
, char *p
)
1121 char path
[128],fname
[128],tmp
[128],line
[256];
1123 struct lut_entry
*lut
;
1129 av_strlcpy(fname
, getenv("HOME"), sizeof( fname
));
1130 av_strlcat(fname
,"/.overlay", sizeof( fname
));
1132 av_strlcpy(fname
, p
, sizeof( fname
));
1134 sprintf(tmp
,"/res_%dx%dx%d",o
->xres
,o
->yres
,o
->depth
);
1135 av_strlcat(fname
, tmp
, sizeof( fname
));
1137 if(!(fp
=fopen(fname
,"r")))
1140 lut
= new_lookuptable(o
);
1143 if(!fgets(line
,256,fp
))
1145 tok
=strtok(line
," ");
1146 if(lookup_parameter(o
,lut
,tok
,&ptr
,&type
)) {
1147 tok
=strtok(NULL
," ");
1150 sscanf(tok
,"%d",(int *)ptr
);
1153 sscanf(tok
,"%x",(int *)ptr
);
1156 sscanf(tok
,"%f",(float *)ptr
);
1160 sscanf(tok
,"%f",&((struct coeff
*)ptr
)[j
].k
);
1161 tok
=strtok(NULL
," ");
1162 sscanf(tok
,"%f",&((struct coeff
*)ptr
)[j
].m
);
1163 tok
=strtok(NULL
," ");
1171 update_parameters(o
);
1178 static void overlay_update_params(overlay_t
*o
) {
1179 update_parameters(o
);
1182 static int overlay_write_state(overlay_t
*o
, char *p
)
1185 char path
[128],fname
[128],tmp
[128];
1187 char line
[256],*tok
;
1188 struct lut_entry
*lut
;
1192 av_strlcpy(fname
, getenv("HOME"), sizeof( fname
));
1193 av_strlcat(fname
,"/.overlay", sizeof( fname
));
1195 av_strlcpy(fname
, p
, sizeof( fname
));
1197 if(access(fname
, W_OK
|X_OK
|R_OK
)) {
1198 if(mkdir(fname
,0766))
1202 sprintf(tmp
,"/res_%dx%dx%d",o
->xres
,o
->yres
,o
->depth
);
1203 av_strlcat(fname
, tmp
, sizeof( fname
));
1205 if(!(fp
=fopen(fname
,"w")))
1208 lut
= new_lookuptable(o
);
1210 for(i
=0; lut
[i
].name
; i
++) {
1211 fprintf(fp
,"%s ",lut
[i
].name
);
1212 switch(lut
[i
].type
) {
1214 fprintf(fp
,"%d\n",*(int *)lut
[i
].ptr
);
1217 fprintf(fp
,"%06x\n",*(int *)lut
[i
].ptr
);
1220 fprintf(fp
,"%f\n",*(float *)lut
[i
].ptr
);
1224 fprintf(fp
,"%f %f ",((struct coeff
*)lut
[i
].ptr
)[j
].k
,
1225 ((struct coeff
*)lut
[i
].ptr
)[j
].m
);
1235 static int overlay_set_screen(overlay_t
*o
, int xres
, int yres
, int depth
)
1237 em8300_overlay_screen_t scr
;
1246 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SETSCREEN
, &scr
)==-1)
1248 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSettingOverlayScreen
);
1254 static int overlay_set_mode(overlay_t
*o
, int mode
)
1256 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SETMODE
, &mode
)==-1) {
1257 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedEnablingOverlay
);
1263 static int overlay_set_window(overlay_t
*o
, int xpos
,int ypos
,int width
,int height
)
1265 em8300_overlay_window_t win
;
1269 win
.height
= height
;
1271 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SETWINDOW
, &win
)==-1)
1273 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow
);
1279 static int overlay_set_bcs(overlay_t
*o
, int brightness
, int contrast
, int saturation
)
1282 bcs
.brightness
= brightness
;
1283 bcs
.contrast
= contrast
;
1284 bcs
.saturation
= saturation
;
1286 if (ioctl(o
->dev
, EM8300_IOCTL_GETBCS
, &bcs
)==-1)
1288 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSettingOverlayBcs
);
1294 static int col_interp(float x
, struct coeff c
)
1305 static int overlay_set_keycolor(overlay_t
*o
, int color
) {
1306 int r
= (color
& 0xff0000) >> 16;
1307 int g
= (color
& 0x00ff00) >> 8;
1308 int b
= (color
& 0x0000ff);
1313 ru
= r
+o
->color_interval
;
1314 gu
= g
+o
->color_interval
;
1315 bu
= b
+o
->color_interval
;
1317 rl
= r
-o
->color_interval
;
1318 gl
= g
-o
->color_interval
;
1319 bl
= b
-o
->color_interval
;
1321 upper
= (col_interp(ru
, o
->colcal_upper
[0]) << 16) |
1322 (col_interp(gu
, o
->colcal_upper
[1]) << 8) |
1323 (col_interp(bu
, o
->colcal_upper
[2]));
1325 lower
= (col_interp(rl
, o
->colcal_lower
[0]) << 16) |
1326 (col_interp(gl
, o
->colcal_lower
[1]) << 8) |
1327 (col_interp(bl
, o
->colcal_lower
[2]));
1329 //printf("0x%06x 0x%06x\n",upper,lower);
1330 overlay_set_attribute(o
,EM9010_ATTRIBUTE_KEYCOLOR_UPPER
,upper
);
1331 overlay_set_attribute(o
,EM9010_ATTRIBUTE_KEYCOLOR_LOWER
,lower
);
1335 static void least_sq_fit(int *x
, int *y
, int n
, float *k
, float *m
)
1337 float sx
=0,sy
=0,sxx
=0,sxy
=0;
1341 for(i
=0; i
< n
; i
++) {
1350 *m
=(sxx
*sy
-sx
*sxy
)/delta
;
1351 *k
=(sxy
*n
-sx
*sy
)/delta
;
1354 static int overlay_autocalibrate(overlay_t
*o
, pattern_drawer_cb pd
, void *arg
)
1356 em8300_overlay_calibrate_t cal
;
1357 em8300_overlay_window_t win
;
1358 int x
[256],r
[256],g
[256],b
[256],n
;
1366 overlay_set_mode(o
, EM8300_OVERLAY_MODE_OVERLAY
);
1367 overlay_set_screen(o
, o
->xres
, o
->yres
, o
->depth
);
1369 /* Calibrate Y-offset */
1371 o
->draw_pattern(0x0000ff, 0, 0, 0, 355, 1, o
->dp_arg
);
1373 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_YOFFSET
;
1374 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1376 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedGettingOverlayYOffsetValues
);
1379 o
->yoffset
= cal
.result
;
1380 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_YOffset
,cal
.result
);
1382 /* Calibrate X-offset */
1384 o
->draw_pattern(0x0000ff, 0, 0, 0, 2, 288, o
->dp_arg
);
1386 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_XOFFSET
;
1387 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1389 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedGettingOverlayXOffsetValues
);
1392 o
->xoffset
= cal
.result
;
1393 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_XOffset
,cal
.result
);
1395 /* Calibrate X scale correction */
1397 o
->draw_pattern(0x0000ff, 0, 355, 0, 2, 288, o
->dp_arg
);
1399 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_XCORRECTION
;
1400 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1402 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedGettingOverlayXScaleCorrection
);
1405 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_DXR3_XCorrection
,cal
.result
);
1406 o
->xcorr
= cal
.result
;
1410 win
.width
= o
->xres
-20;
1411 win
.height
= o
->yres
-20;
1412 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SETWINDOW
, &win
)==-1) {
1413 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_DXR3_FailedResizingOverlayWindow
);
1417 /* Calibrate key color upper limit */
1419 for(i
=128,n
=0; i
<= 0xff; i
+=4) {
1420 o
->draw_pattern(i
| (i
<< 8) | (i
<< 16), 0,
1421 (o
->xres
-200)/2,0,200,o
->yres
,o
->dp_arg
);
1425 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_COLOR
;
1427 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1433 r
[n
] = (cal
.result
>>16)&0xff;
1434 g
[n
] = (cal
.result
>>8)&0xff;
1435 b
[n
] = (cal
.result
)&0xff;
1439 least_sq_fit(x
,r
,n
,&o
->colcal_upper
[0].k
,&o
->colcal_upper
[0].m
);
1440 least_sq_fit(x
,g
,n
,&o
->colcal_upper
[1].k
,&o
->colcal_upper
[1].m
);
1441 least_sq_fit(x
,b
,n
,&o
->colcal_upper
[2].k
,&o
->colcal_upper
[2].m
);
1443 /* Calibrate key color lower limit */
1445 for(i
=128,n
=0; i
<= 0xff; i
+=4) {
1446 o
->draw_pattern(i
| (i
<< 8) | (i
<< 16), 0xffffff,
1447 (o
->xres
-200)/2,0,200,o
->yres
, o
->dp_arg
);
1451 cal
.cal_mode
= EM8300_OVERLAY_CALMODE_COLOR
;
1453 if (ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_CALIBRATE
, &cal
))
1458 r
[n
] = (cal
.result
>>16)&0xff;
1459 g
[n
] = (cal
.result
>>8)&0xff;
1460 b
[n
] = (cal
.result
)&0xff;
1464 least_sq_fit(x
,r
,n
,&o
->colcal_lower
[0].k
,&o
->colcal_lower
[0].m
);
1465 least_sq_fit(x
,g
,n
,&o
->colcal_lower
[1].k
,&o
->colcal_lower
[1].m
);
1466 least_sq_fit(x
,b
,n
,&o
->colcal_lower
[2].k
,&o
->colcal_lower
[2].m
);
1468 overlay_set_mode(o
, EM8300_OVERLAY_MODE_OFF
);
1474 static int overlay_signalmode(overlay_t
*o
, int mode
) {
1475 if(ioctl(o
->dev
, EM8300_IOCTL_OVERLAY_SIGNALMODE
, &mode
) ==-1) {
1476 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_DXR3_FailedSetSignalMix
);