typo fixes
[mplayer/greg.git] / libvo / vo_dxr2.c
blob89d6933721c811c5fbe94c8db72fbef034473bc5
2 #include "fastmemcpy.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/ioctl.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <errno.h>
15 #include "config.h"
16 #include "video_out.h"
17 #include "video_out_internal.h"
18 #include "mp_msg.h"
19 #include "m_option.h"
20 #include "sub.h"
22 #ifdef X11_FULLSCREEN
23 #include "x11_common.h"
24 #endif
26 #include <dxr2ioctl.h>
29 extern char *get_path(char *filename);
31 extern float monitor_aspect;
32 extern float movie_aspect;
34 int dxr2_fd = -1;
36 static int movie_w,movie_h;
37 static int playing = 0;
38 static int last_freq_id = -1;
40 // vo device used to blank the screen for the overlay init
41 static vo_functions_t* sub_vo = NULL;
43 static uint8_t* sub_img = NULL;
44 static int sub_x,sub_y,sub_w,sub_h;
45 static int sub_x_off,sub_y_off;
46 static int sub_config_count;
47 static int aspect;
48 static int sub_vo_win = 0;
50 static int use_ol = 1;
51 static int ol_ratio = 1000;
52 static char *norm = NULL;
53 static char *ucode = NULL;
54 static int ar_mode = DXR2_ASPECTRATIOMODE_LETTERBOX;
55 static int mv_mode = DXR2_MACROVISION_OFF;
56 static int _75ire_mode = DXR2_75IRE_OFF;
57 static int bw_mode = DXR2_BLACKWHITE_OFF;
58 static int interlaced_mode = DXR2_INTERLACED_ON;
59 static int pixel_mode = DXR2_PIXEL_CCIR601;
60 static int iec958_mode = DXR2_IEC958_DECODED;
61 static int mute_mode = DXR2_AUDIO_MUTE_OFF;
62 static int ignore_cache = 0;
63 static int update_cache = 0;
64 static int olw_cor = 0, olh_cor = 0,olx_cor = 0, oly_cor = 0;
65 static int ol_osd = 0;
66 static int ck_rmin = 0x40;
67 static int ck_rmax = 0xFF;
68 static int ck_r = 0xFF;
69 static int ck_gmin = 0x00;
70 static int ck_gmax = 0x20;
71 static int ck_g = 0;
72 static int ck_bmin = 0x40;
73 static int ck_bmax = 0xFF;
74 static int ck_b = 0xFF;
75 static int cr_left = 0, cr_right = 0;
76 static int cr_top = 55, cr_bot = 300;
78 m_option_t dxr2_opts[] = {
79 { "overlay", &use_ol, CONF_TYPE_FLAG, 0, 0, 1, NULL},
80 { "nooverlay", &use_ol, CONF_TYPE_FLAG, 0, 1, 0, NULL},
81 { "overlay-ratio", &ol_ratio, CONF_TYPE_INT, CONF_RANGE, 1, 2500, NULL },
82 { "ucode", &ucode, CONF_TYPE_STRING,0, 0, 0, NULL},
84 { "norm", &norm, CONF_TYPE_STRING,0, 0, 0, NULL},
86 { "ar-mode",&ar_mode, CONF_TYPE_INT, CONF_RANGE,0,2,NULL },
88 { "macrovision",&mv_mode,CONF_TYPE_INT,CONF_RANGE,0,3, NULL },
90 { "75ire",&_75ire_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
91 { "no75ire",&_75ire_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
93 { "bw",&bw_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
94 { "color",&bw_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
96 { "interlaced",&interlaced_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
97 { "nointerlaced",&interlaced_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
99 { "square-pixel",&pixel_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
100 { "ccir601-pixel",&pixel_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
102 { "iec958-encoded",&iec958_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
103 { "iec958-decoded",&iec958_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
105 { "mute", &mute_mode,CONF_TYPE_FLAG, 0, 1, 0, NULL},
106 { "nomute",&mute_mode,CONF_TYPE_FLAG, 0, 0, 1, NULL},
108 { "ignore-cache",&ignore_cache,CONF_TYPE_FLAG, 0, 0, 1, NULL},
109 { "update-cache",&update_cache,CONF_TYPE_FLAG, 0, 0, 1, NULL},
111 { "olh-cor", &olh_cor, CONF_TYPE_INT, CONF_RANGE, -20, 20, NULL},
112 { "olw-cor", &olw_cor, CONF_TYPE_INT, CONF_RANGE, -20, 20, NULL},
113 { "olx-cor", &olx_cor, CONF_TYPE_INT, CONF_RANGE, -20, 20, NULL},
114 { "oly-cor", &oly_cor, CONF_TYPE_INT, CONF_RANGE, -20, 20, NULL},
116 { "ol-osd", &ol_osd, CONF_TYPE_FLAG, 0, 0, 1, NULL},
117 { "nool-osd", &ol_osd, CONF_TYPE_FLAG, 0, 1, 0, NULL},
119 { "ck-rmin", &ck_rmin, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
120 { "ck-rmax", &ck_rmax, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
121 { "ck-r", &ck_r, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
122 { "ck-gmin", &ck_gmin, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
123 { "ck-gmax", &ck_gmax, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
124 { "ck-g", &ck_g, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
125 { "ck-bmin", &ck_bmin, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
126 { "ck-bmax", &ck_bmax, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
127 { "ck-b", &ck_b, CONF_TYPE_INT, CONF_RANGE, 0, 0xFF, NULL},
128 { "cr-left", &cr_left, CONF_TYPE_INT, CONF_RANGE, 0, 500, NULL},
129 { "cr-right", &cr_right, CONF_TYPE_INT, CONF_RANGE, 0, 500, NULL},
130 { "cr-top", &cr_top, CONF_TYPE_INT, CONF_RANGE, 0, 500, NULL},
131 { "cr-bot", &cr_bot, CONF_TYPE_INT, CONF_RANGE, 0, 500, NULL},
133 { NULL,NULL, 0, 0, 0, 0, NULL}
136 static vo_info_t info = {
137 "DXR2 video out",
138 "dxr2",
139 "Alban Bedel <albeu@free.fr> and Tobias Diedrich <ranma@gmx.at>",
143 LIBVO_EXTERN (dxr2)
145 static char *ucodesearchpath[] = {
146 "/usr/local/lib/dxr2/dvd12.ux",
147 "/usr/lib/dxr2/dvd12.ux",
148 "/usr/src/dvd12.ux",
149 NULL,
152 #define BUF_SIZE 2048
154 static unsigned char dxr2buf[BUF_SIZE];
155 static unsigned int dxr2bufpos = 0;
157 static void write_dxr2(void *data, int len)
159 int w = 0;
160 while (len>0) if ((dxr2bufpos+len) <= BUF_SIZE) {
161 memcpy(dxr2buf+dxr2bufpos, data, len);
162 dxr2bufpos+=len;
163 len=0;
164 } else {
165 int copylen=BUF_SIZE-dxr2bufpos;
166 if(copylen > 0) {
167 memcpy(dxr2buf+dxr2bufpos, data, copylen);
168 dxr2bufpos += copylen;
169 data+=copylen;
170 len-=copylen;
172 w = write(dxr2_fd, dxr2buf, BUF_SIZE);
173 if(w < 0) {
174 mp_msg(MSGT_VO,MSGL_WARN,"DXR2 : write failed : %s \n",strerror(errno));
175 dxr2bufpos = 0;
176 break;
178 dxr2bufpos -= w;
179 if(dxr2bufpos)
180 memmove(dxr2buf,dxr2buf + w,dxr2bufpos);
184 static void flush_dxr2()
186 int w;
187 while (dxr2bufpos) {
188 w = write(dxr2_fd, dxr2buf, dxr2bufpos);
189 if(w < 0) {
190 mp_msg(MSGT_VO,MSGL_WARN,"DXR2 : write failed %s \n",strerror(errno));
191 dxr2bufpos = 0;
192 break;
194 dxr2bufpos -= w;
198 #define PACK_MAX_SIZE 2048
200 static unsigned char pack[PACK_MAX_SIZE];
202 static unsigned char mpg_header[]={
203 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00,
204 0x04, 0x01, 0x01, 0x86, 0xa3, 0xf8
207 static unsigned char mpg_eof[]={
208 0x00, 0x00, 0x01, 0xb9
211 static void dxr2_send_header(void)
213 write_dxr2(&mpg_header, sizeof(mpg_header));
216 static void dxr2_send_eof(void)
218 write_dxr2(&mpg_eof, sizeof(mpg_eof));
221 void dxr2_send_packet(unsigned char* data,int len,int id,int timestamp)
223 int ptslen=5;
225 if(dxr2_fd < 0) {
226 mp_msg(MSGT_VO,MSGL_ERR,"DXR2 fd is not valid\n");
227 return;
230 mp_msg(MSGT_VO,MSGL_DBG2,"DXR2 packet : 0x%x => %d \n",id,timestamp);
231 dxr2_send_header();
233 // startcode:
234 pack[0]=pack[1]=0;pack[2]=0x01;
235 // stream id
236 pack[3]=id;
238 while(len>0){
239 int payload_size=len; // data + PTS
240 if(9+ptslen+payload_size>PACK_MAX_SIZE) payload_size=PACK_MAX_SIZE-(6+ptslen);
242 // construct PES header: (code from ffmpeg's libav)
243 // packetsize:
244 pack[4]=(3+ptslen+payload_size)>>8;
245 pack[5]=(3+ptslen+payload_size)&255;
247 pack[6]=0x81;
248 if(ptslen){
249 int x;
250 pack[7]=0x80;
251 pack[8]=ptslen;
252 // presentation time stamp:
253 x=(0x02 << 4) | (((timestamp >> 30) & 0x07) << 1) | 1;
254 pack[9]=x;
255 x=((((timestamp >> 15) & 0x7fff) << 1) | 1);
256 pack[10]=x>>8; pack[11]=x&255;
257 x=((((timestamp) & 0x7fff) << 1) | 1);
258 pack[12]=x>>8; pack[13]=x&255;
259 } else {
260 pack[7]=0x00;
261 pack[8]=0x00;
264 write_dxr2(pack, 9+ptslen);
265 write_dxr2(data, payload_size);
267 len-=payload_size; data+=payload_size;
268 ptslen=0; // store PTS only once, at first packet!
272 void dxr2_send_lpcm_packet(unsigned char* data,int len,int id,unsigned int timestamp,int freq_id)
274 int arg;
275 int ptslen=5;
277 if(dxr2_fd < 0) {
278 mp_msg(MSGT_VO,MSGL_ERR,"DXR2 fd is not valid\n");
279 return;
282 if(last_freq_id != freq_id) {
283 ioctl(dxr2_fd, DXR2_IOC_SET_AUDIO_SAMPLE_FREQUENCY, &freq_id);
284 last_freq_id = freq_id;
287 if (((int) timestamp)<0)
288 timestamp=0;
290 mp_msg(MSGT_VO,MSGL_DBG2,"dxr2_send_lpcm_packet(timestamp=%d)\n", timestamp);
291 // startcode:
292 pack[0]=pack[1]=0;pack[2]=0x01;
294 // stream id
295 pack[3]=0xBD;
297 while(len>=4){
298 int payload_size;
300 payload_size=PACK_MAX_SIZE-6-3-ptslen-7; // max possible data len
301 if(payload_size>len) payload_size=len;
302 payload_size&=(~3); // align!
304 // packetsize:
305 pack[4]=(payload_size+3+ptslen+7)>>8;
306 pack[5]=(payload_size+3+ptslen+7)&255;
308 // stuffing:
309 pack[6]=0x81;
310 // pack[7]=0x00; //0x80
312 // hdrlen:
313 pack[8]=ptslen;
315 if(ptslen){
316 int x;
317 pack[7]=0x80;
318 // presentation time stamp:
319 x=(0x02 << 4) | (((timestamp >> 30) & 0x07) << 1) | 1;
320 pack[9]=x;
321 x=((((timestamp >> 15) & 0x7fff) << 1) | 1);
322 pack[10]=x>>8; pack[11]=x&255;
323 x=((((timestamp) & 0x7fff) << 1) | 1);
324 pack[12]=x>>8; pack[13]=x&255;
325 } else {
326 pack[7]=0x00;
329 // ============ LPCM header: (7 bytes) =================
330 // Info by mocm@convergence.de
332 // ID:
333 pack[ptslen+9]=id;
335 // number of frames:
336 pack[ptslen+10]=0x07;
338 // first acces unit pointer, i.e. start of audio frame:
339 pack[ptslen+11]=0x00;
340 pack[ptslen+12]=0x04;
342 // audio emphasis on-off 1 bit
343 // audio mute on-off 1 bit
344 // reserved 1 bit
345 // audio frame number 5 bit
346 pack[ptslen+13]=0x0C;
348 // quantization word length 2 bit
349 // audio sampling frequency (48khz = 0, 96khz = 1) 2 bit
350 // reserved 1 bit
351 // number of audio channels - 1 (e.g. stereo = 1) 3 bit
352 pack[ptslen+14]=1;
354 // dynamic range control (0x80 if off)
355 pack[ptslen+15]=0x80;
357 write_dxr2(pack, 6+3+ptslen+7);
358 write_dxr2(data, payload_size);
360 len-=payload_size; data+=payload_size;
361 timestamp+=90000/4*payload_size/48000;
365 void dxr2_send_sub_packet(unsigned char* data,int len,int id,unsigned int timestamp) {
366 int ptslen=5;
368 if(dxr2_fd < 0) {
369 mp_msg(MSGT_VO,MSGL_ERR,"DXR2 fd is not valid\n");
370 return;
373 if (((int) timestamp)<0)
374 timestamp=0;
376 mp_msg(MSGT_VO,MSGL_DBG2,"dxr2_send_sub_packet(timestamp=%d)\n", timestamp);
377 // startcode:
378 pack[0]=pack[1]=0;pack[2]=0x01;
380 // stream id
381 pack[3]=0xBD;
383 while(len>=4){
384 int payload_size= PACK_MAX_SIZE-(7+ptslen+3);
385 if(payload_size>len) payload_size= len;
387 pack[4]=(3+ptslen+1+payload_size)>>8;
388 pack[5]=(3+ptslen+1+payload_size)&255;
390 pack[6]=0x81;
391 if(ptslen){
392 int x;
393 pack[7]=0x80;
394 pack[8]=ptslen;
395 // presentation time stamp:
396 x=(0x02 << 4) | (((timestamp >> 30) & 0x07) << 1) | 1;
397 pack[9]=x;
398 x=((((timestamp >> 15) & 0x7fff) << 1) | 1);
399 pack[10]=x>>8; pack[11]=x&255;
400 x=((((timestamp) & 0x7fff) << 1) | 1);
401 pack[12]=x>>8; pack[13]=x&255;
402 } else {
403 pack[7]=0x00;
404 pack[8]=0x00;
406 pack[ptslen+9] = id;
408 write_dxr2(pack,7+ptslen+3);
409 write_dxr2(data,payload_size);
410 len -= payload_size;
411 data += payload_size;
412 ptslen = 0;
416 static int dxr2_set_vga_params(dxr2_vgaParams_t* vga,int detect) {
417 // Init the overlay, don't ask me how it work ;-)
418 dxr2_sixArg_t oc;
419 dxr2_oneArg_t om;
420 dxr2_twoArg_t win;
421 dxr2_fourArg_t crop;
423 crop.arg1= cr_left;
424 crop.arg2= cr_right;
425 crop.arg3 = cr_top;
426 crop.arg4 = cr_bot;
427 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_CROPPING, &crop);
429 oc.arg1 = 0x40;
430 oc.arg2 = 0xff;
431 oc.arg3 = 0x40;
432 oc.arg4 = 0xff;
433 oc.arg5 = 0x40;
434 oc.arg6 = 0xff;
435 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_COLOUR, &oc);
437 om.arg = ol_ratio;
438 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_RATIO,&om);
440 win.arg1 = 100;
441 win.arg2 = 3;
442 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_POSITION,&win);
444 win.arg1 = vo_screenwidth;
445 win.arg2 = vo_screenheight;
446 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_DIMENSION,&win);
448 om.arg = 0;
449 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_IN_DELAY,&om);
451 if(detect) {
452 // First we need a white screen
453 uint8_t* img = malloc(vo_screenwidth*vo_screenheight*3);
454 uint8_t* src[] = { img, NULL, NULL };
455 int stride[] = { vo_screenwidth * 3 , 0, 0 };
456 int cc = vo_config_count;
458 memset(img,255,vo_screenwidth*vo_screenheight*3);
459 vo_config_count = sub_config_count;
460 if(sub_vo->config(vo_screenwidth,vo_screenheight,vo_screenwidth,vo_screenheight,
461 VOFLAG_FULLSCREEN ,"DXR2 sub vo",IMGFMT_BGR24) != 0) {
462 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] sub vo config failed => No overlay\n");
463 sub_vo->uninit();
464 sub_vo = NULL;
465 use_ol = 0;
466 vo_config_count = cc;
467 return 0;
469 sub_vo->draw_slice(src,stride,vo_screenwidth,vo_screenheight,0,0);
470 sub_vo->flip_page();
471 free(img);
472 sub_config_count++;
473 vo_config_count = cc;
475 om.arg = DXR2_OVERLAY_WINDOW_COLOUR_KEY;
476 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_MODE,&om);
478 vga->xScreen = vo_screenwidth;
479 vga->yScreen = vo_screenheight;
480 vga->hOffWinKey = 100;
481 vga->vOffWinKey = 3;
482 ioctl(dxr2_fd, DXR2_IOC_CALCULATE_VGA_PARAMETERS, vga);
484 ioctl(dxr2_fd, DXR2_IOC_SET_VGA_PARAMETERS,vga);
486 return 1;
489 static int dxr2_save_vga_params(dxr2_vgaParams_t* vga,char* name) {
490 struct stat s;
491 char* p = get_path("dxr2_cache");
492 int p_len = strlen(p), name_len = strlen(name);
493 char cache_path[p_len + name_len + 2];
494 int ret;
495 FILE* fd;
497 if(stat(p,&s) !=0) {
498 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] No vga cache dir found (%s)\n",strerror(errno));
499 if(errno == EACCES) {
500 free(p);
501 return 0;
503 // Try to create the dir
504 if(mkdir(p,S_IRWXU) != 0) {
505 mp_msg(MSGT_VO,MSGL_ERR,"VO: [dxr2] Unable to create vga cache dir %s (%s)\n",p,strerror(errno));
506 free(p);
507 return 0;
510 sprintf(cache_path,"%s/%s",p,name);
511 free(p);
512 fd = fopen(cache_path,"w");
513 if(fd == NULL) {
514 mp_msg(MSGT_VO,MSGL_ERR,"VO: [dxr2] Unable to open cache file %s for writing (%s)\n",cache_path,strerror(errno));
515 return 0;
518 ret = fprintf(fd,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
519 vga->hOffWinKey,
520 vga->vOffWinKey,
521 vga->xScreen,
522 vga->yScreen,
523 vga->hsyncPol,
524 vga->vsyncPol,
525 vga->blankStart,
526 vga->blankWidth,
527 vga->hOffset,
528 vga->vOffset,
529 vga->ratio,
530 olx_cor,
531 oly_cor,
532 olw_cor,
533 olh_cor);
535 fclose(fd);
536 return ret >= 11 ? 1 : 0;
539 static int dxr2_load_vga_params(dxr2_vgaParams_t* vga,char* name) {
540 char* p = get_path("dxr2_cache");
541 int p_len = strlen(p), name_len = strlen(name);
542 char cache_path[p_len + name_len + 2];
543 int ret;
544 int xc,yc,wc,hc;
545 FILE* fd;
547 sprintf(cache_path,"%s/%s",p,name);
548 free(p);
550 fd = fopen(cache_path,"r");
551 if(fd == NULL) {
552 mp_msg(MSGT_VO,MSGL_ERR,"VO: [dxr2] Unable to open cache file %s for reading (%s)\n",cache_path,strerror(errno));
553 return 0;
555 ret = fscanf(fd, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
556 &vga->hOffWinKey,
557 &vga->vOffWinKey,
558 &vga->xScreen,
559 &vga->yScreen,
560 &vga->hsyncPol,
561 &vga->vsyncPol,
562 &vga->blankStart,
563 &vga->blankWidth,
564 &vga->hOffset,
565 &vga->vOffset,
566 &vga->ratio,
567 &xc,
568 &yc,
569 &wc,
570 &hc);
572 fclose(fd);
573 if(ret > 11 && !olx_cor) olx_cor = xc;
574 if(ret > 12 && !oly_cor) oly_cor = yc;
575 if(ret > 13 && !olw_cor) olw_cor = wc;
576 if(ret > 14 && !olh_cor) olh_cor = hc;
577 return ret >= 11 ? 1 : 0;
580 static int dxr2_setup_vga_params(void) {
581 const vo_info_t* vi = sub_vo->info;
582 dxr2_vgaParams_t vga;
584 int loaded = dxr2_load_vga_params(&vga,(char*)vi->short_name);
585 if(!dxr2_set_vga_params(&vga,(update_cache || ignore_cache) ? 1 : !loaded ))
586 return 0;
587 if(!loaded || update_cache)
588 dxr2_save_vga_params(&vga,(char*)vi->short_name);
589 return 1;
592 static void dxr2_set_overlay_window(void) {
593 uint8_t* src[] = { sub_img, NULL, NULL };
594 int stride[] = { movie_w * 3 , 0, 0 };
595 dxr2_twoArg_t win;
596 int redisp = 0;
597 int cc = vo_config_count;
598 vo_config_count = sub_config_count;
599 sub_vo->draw_slice(src,stride,movie_w,movie_h,0,0);
600 sub_vo->flip_page();
601 vo_config_count = cc;
604 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] setting overlay with correction x=%d y=%d w=%d h=%d\n",olx_cor,oly_cor, olw_cor,olh_cor);
606 // Sub vo isn't a windowed one, fill in the needed stuff
607 if(!sub_vo_win) {
608 if(vo_fs) {
609 vo_dwidth = vo_screenwidth;
610 vo_dheight = vo_screenheight;
611 vo_dx = vo_dy = 0;
612 } else {
613 vo_dwidth = movie_w;
614 vo_dheight = movie_h;
615 vo_dx = (vo_screenwidth - vo_dwidth) / 2;
616 vo_dy = (vo_screenheight - vo_dheight) / 2;
620 if(sub_w != vo_dwidth || sub_h != vo_dheight) {
621 int new_aspect = ((1<<16)*vo_dwidth + vo_dheight/2)/vo_dheight;
622 sub_w = vo_dwidth;
623 sub_h = vo_dheight;
624 if(new_aspect > aspect)
625 sub_w = (sub_h*aspect + (1<<15))>>16;
626 else
627 sub_h = ((sub_w<<16) + (aspect>>1)) /aspect;
628 sub_w += olw_cor;
629 sub_h += olh_cor;
630 sub_x_off = (vo_dwidth-sub_w) / 2;
631 sub_y_off = (vo_dheight-sub_h) / 2;
632 sub_x = -vo_dx; // Be sure to also replace the overlay
633 win.arg1 = sub_w;
634 win.arg2 = sub_h;
635 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] set win size w=%d h=%d and offset x=%d y=%d \n",win.arg1,win.arg2,sub_x_off,sub_y_off);
636 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_DIMENSION, &win);
639 if(vo_dx != sub_x || vo_dy != sub_y) {
640 sub_x = vo_dx + olx_cor + sub_x_off;
641 sub_y = vo_dy + oly_cor + sub_y_off;
642 win.arg1 = (sub_x > 0 ? sub_x : 0);
643 win.arg2 = (sub_y > 0 ? sub_y : 0);
644 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] set pos x=%d y=%d \n",win.arg1,win.arg2);
645 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_POSITION,&win);
650 static int config(uint32_t s_width, uint32_t s_height, uint32_t width, uint32_t height, uint32_t flags, char *title, uint32_t format)
652 int arg;
653 dxr2_threeArg_t arg3;
655 if(dxr2_fd < 0) {
656 mp_msg(MSGT_VO,MSGL_ERR,"DXR2 fd is not valid\n");
657 return VO_ERROR;
660 if(playing) {
661 dxr2_send_eof();
662 flush_dxr2();
663 ioctl(dxr2_fd, DXR2_IOC_STOP, NULL);
664 playing = 0;
667 last_freq_id = -1;
669 // Video stream setup
670 arg3.arg1 = DXR2_STREAM_VIDEO;
671 arg3.arg2 = 0;
672 ioctl(dxr2_fd, DXR2_IOC_SELECT_STREAM, &arg3);
673 if (vo_fps > 28)
674 arg3.arg1 = DXR2_SRC_VIDEO_FREQ_30;
675 else arg3.arg1 = DXR2_SRC_VIDEO_FREQ_25;
676 arg3.arg2 = s_width;
677 arg3.arg3 = s_height;
678 ioctl(dxr2_fd, DXR2_IOC_SET_SOURCE_VIDEO_FORMAT, &arg3);
679 arg = DXR2_BITSTREAM_TYPE_MPEG_VOB;
680 ioctl(dxr2_fd, DXR2_IOC_SET_BITSTREAM_TYPE, &arg);
682 // Aspect ratio
683 if (1.76 <= movie_aspect && movie_aspect <= 1.80) {
684 arg = DXR2_ASPECTRATIO_16_9;
685 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] source aspect ratio 16:9\n");
686 } else {
687 arg = DXR2_ASPECTRATIO_4_3;
688 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] source aspect ratio 4:3\n");
690 ioctl(dxr2_fd, DXR2_IOC_SET_SOURCE_ASPECT_RATIO, &arg);
691 if (1.76 <= monitor_aspect && monitor_aspect <=1.80) {
692 arg = DXR2_ASPECTRATIO_16_9;
693 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] monitor aspect ratio 16:9\n");
694 } else {
695 arg = DXR2_ASPECTRATIO_4_3;
696 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] monitor aspect ratio 4:3\n");
698 ioctl(dxr2_fd, DXR2_IOC_SET_OUTPUT_ASPECT_RATIO, &arg);
700 arg = ar_mode;
701 ioctl(dxr2_fd, DXR2_IOC_SET_ASPECT_RATIO_MODE, &arg);
703 // TV setup
704 arg = mv_mode;
705 ioctl(dxr2_fd, DXR2_IOC_SET_TV_MACROVISION_MODE, &arg);
706 arg = _75ire_mode;
707 ioctl(dxr2_fd, DXR2_IOC_SET_TV_75IRE_MODE, &arg);
708 arg = bw_mode;
709 ioctl(dxr2_fd, DXR2_IOC_SET_TV_BLACKWHITE_MODE, &arg);
710 arg = interlaced_mode;
711 ioctl(dxr2_fd, DXR2_IOC_SET_TV_INTERLACED_MODE, &arg);
712 arg = pixel_mode;
713 ioctl(dxr2_fd, DXR2_IOC_SET_TV_PIXEL_MODE, &arg);
715 if (norm) {
716 if (strcmp(norm, "ntsc")==0)
717 arg = DXR2_OUTPUTFORMAT_NTSC;
718 else if (strcmp(norm, "pal")==0) {
719 if (vo_fps > 28) {
720 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] you want pal, but we play at 30 fps, selecting pal60 instead\n");
721 arg = DXR2_OUTPUTFORMAT_PAL_60;
722 norm="pal60";
723 } else arg = DXR2_OUTPUTFORMAT_PAL_BDGHI;
724 } else if (strcmp(norm, "pal60")==0) {
725 if (vo_fps > 28)
726 arg = DXR2_OUTPUTFORMAT_PAL_60;
727 else {
728 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] you want pal60, but we play at 25 fps, selecting pal instead\n");
729 arg = DXR2_OUTPUTFORMAT_PAL_BDGHI;
730 norm="pal";
732 } else if (strcmp(norm, "palm")==0)
733 arg = DXR2_OUTPUTFORMAT_PAL_M;
734 else if (strcmp(norm, "paln")==0)
735 arg = DXR2_OUTPUTFORMAT_PAL_N;
736 else if (strcmp(norm, "palnc")==0)
737 arg = DXR2_OUTPUTFORMAT_PAL_Nc;
738 else {
739 mp_msg(MSGT_VO,MSGL_WARN,"[dxr2] invalid norm %s\n", norm);
740 mp_msg(MSGT_VO,MSGL_WARN,"Valid values are ntsc,pal,pal60,palm,paln,palnc\n");
741 mp_msg(MSGT_VO,MSGL_WARN,"Using ntsc\n");
742 norm="ntsc";
744 } else {
745 if (vo_fps > 28) {
746 arg = DXR2_OUTPUTFORMAT_NTSC;
747 norm="ntsc";
748 } else {
749 arg = DXR2_OUTPUTFORMAT_PAL_BDGHI;
750 norm="pal";
753 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] output norm set to %s\n", norm);
754 ioctl(dxr2_fd, DXR2_IOC_SET_TV_OUTPUT_FORMAT, &arg);
756 // Subtitles
758 arg = DXR2_SUBPICTURE_ON;
759 ioctl(dxr2_fd,DXR2_IOC_ENABLE_SUBPICTURE,&arg);
760 arg3.arg1 = DXR2_STREAM_SUBPICTURE;
761 arg3.arg2 = 0;
762 ioctl(dxr2_fd, DXR2_IOC_SELECT_STREAM, &arg3);
764 // Audio
765 arg = iec958_mode;
766 ioctl(dxr2_fd, DXR2_IOC_IEC958_OUTPUT_MODE, &arg);
767 arg = DXR2_AUDIO_WIDTH_16;
768 ioctl(dxr2_fd, DXR2_IOC_SET_AUDIO_DATA_WIDTH, &arg);
769 arg = DXR2_AUDIO_FREQ_48;
770 ioctl(dxr2_fd, DXR2_IOC_SET_AUDIO_SAMPLE_FREQUENCY, &arg);
771 arg3.arg1 = DXR2_STREAM_AUDIO_LPCM;
772 arg3.arg2 = 0;
773 ioctl(dxr2_fd, DXR2_IOC_SELECT_STREAM, &arg3);
774 arg = 19;
775 ioctl(dxr2_fd, DXR2_IOC_SET_AUDIO_VOLUME, &arg);
776 arg = mute_mode;
777 ioctl(dxr2_fd, DXR2_IOC_AUDIO_MUTE, &arg);
779 movie_w = width;
780 movie_h = height;
781 //vo_fs = flags & VOFLAG_FULLSCREEN ? 1 : 0;
782 // Overlay
783 while(use_ol) {
784 dxr2_twoArg_t win;
785 dxr2_oneArg_t om;
786 int cc = vo_config_count;
787 vo_config_count = sub_config_count;
788 // Load or detect the overlay stuff
789 if(!dxr2_setup_vga_params()) {
790 sub_vo->uninit();
791 sub_vo = NULL;
792 vo_config_count = cc;
793 break;
795 // Does the sub vo support the x11 stuff
796 // Fix me : test the other x11 vo's and enable them
797 if(strcmp(sub_vo->info->short_name,"x11") == 0)
798 sub_vo_win = 1;
799 else
800 sub_vo_win = 0;
802 // No window and no osd => we don't need any subdriver
803 if(!sub_vo_win && !ol_osd) {
804 sub_vo->uninit();
805 sub_vo = NULL;
808 while(sub_vo) {
809 dxr2_sixArg_t oc;
810 int i,sub_flags = VOFLAG_SWSCALE | (flags & VOFLAG_FULLSCREEN);
811 if(sub_vo->config(width,height,width,height,sub_flags,
812 "MPlayer DXR2 render",IMGFMT_BGR24) != 0) {
813 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] sub vo config failed => No X11 window\n");
814 sub_vo->uninit();
815 sub_vo = NULL;
816 break;
818 sub_config_count++;
820 // Feel free to try some other other color and report your results
821 oc.arg1 = ck_rmin;
822 oc.arg2 = ck_rmax;
823 oc.arg3 = ck_gmin;
824 oc.arg4 = ck_gmax;
825 oc.arg5 = ck_bmin;
826 oc.arg6 = ck_bmax;
827 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_COLOUR, &oc);
829 om.arg = DXR2_OVERLAY_WINDOW_COLOUR_KEY;
830 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_MODE,&om);
831 sub_img = malloc(width*height*3);
832 for(i = 0 ; i < width*height*3 ; i += 3) {
833 sub_img[i] = ck_b;
834 sub_img[i+1] = ck_g;
835 sub_img[i+2] = ck_r;
837 aspect = ((1<<16)*width + height/2)/height;
838 sub_w = sub_h = 0;
839 dxr2_set_overlay_window();
840 break;
842 vo_config_count = cc;
843 if(!sub_vo) { // Fallback on non windowed overlay
844 vo_fs = flags & VOFLAG_FULLSCREEN ? 1 : 0;
845 om.arg = DXR2_OVERLAY_WINDOW_KEY;
846 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_MODE,&om);
847 win.arg1 = flags & VOFLAG_FULLSCREEN ? vo_screenwidth : width;
848 win.arg2 = flags & VOFLAG_FULLSCREEN ? vo_screenheight : height;
849 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_DIMENSION, &win);
850 win.arg1 = (vo_screenwidth - win.arg1) / 2;
851 win.arg2 = (vo_screenheight - win.arg2) / 2;
852 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_POSITION,&win);
854 break;
857 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
859 // start playing
860 if(ioctl(dxr2_fd, DXR2_IOC_PLAY, NULL) == 0) {
861 playing = 1;
862 return 0;
863 } else
864 return VO_ERROR;
867 static void clear_alpha(int x0,int y0, int w,int h) {
868 uint8_t* src[] = { sub_img , NULL, NULL };
869 int stride[] = { movie_w * 3, 0, 0 };
871 sub_vo->draw_slice(src,stride,w,h,x0,y0);
874 static void draw_osd(void)
876 if(sub_vo && ol_osd) {
877 vo_remove_text(movie_w,movie_h,clear_alpha);
878 sub_vo->draw_osd();
882 static int draw_frame(uint8_t * src[])
884 vo_mpegpes_t *p=(vo_mpegpes_t *)src[0];
885 if(p->id == 0x1E0) {// Video
886 dxr2_send_packet(p->data, p->size, p->id, p->timestamp);
887 } else if(p->id == 0x20) // Subtitles
888 dxr2_send_sub_packet(p->data, p->size, p->id, p->timestamp);
889 return 0;
892 static void flip_page (void)
894 if(sub_vo && ol_osd && vo_osd_changed_flag)
895 sub_vo->flip_page();
898 static int draw_slice( uint8_t *srcimg[], int stride[], int w, int h, int x0, int y0 )
900 return 0;
904 static int query_format(uint32_t format)
906 if (format==IMGFMT_MPEGPES)
907 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_TIMER|VFCAP_SPU;
908 return 0;
912 static void uninit(void)
914 mp_msg(MSGT_VO,MSGL_DBG2, "VO: [dxr2] Uninitializing\n" );
916 if (dxr2_fd > 0) {
917 if(playing) {
918 dxr2_send_eof();
919 flush_dxr2();
920 playing = 0;
922 close(dxr2_fd);
923 dxr2_fd = -1;
925 if(sub_img) {
926 free(sub_img);
927 sub_img = NULL;
929 if(sub_vo) {
930 int cc = vo_config_count;
931 vo_config_count = sub_config_count;
932 sub_vo->uninit();
933 sub_vo = NULL;
934 vo_config_count = cc;
939 static void check_events(void)
941 // I'd like to have this done in an x11 independent way
942 // It's because of this that we are limited to vo_x11 for windowed overlay :-(
943 #ifdef X11_FULLSCREEN
944 if(sub_vo && sub_vo_win) {
945 int e=vo_x11_check_events(mDisplay);
946 if ( !(e&VO_EVENT_RESIZE) && !(e&VO_EVENT_EXPOSE) ) return;
947 XSetBackground(mDisplay, vo_gc, 0);
948 XClearWindow(mDisplay, vo_window);
949 dxr2_set_overlay_window();
951 #endif
954 static int preinit(const char *arg) {
955 int uCodeFD = -1;
956 int uCodeSize;
957 dxr2_uCode_t* uCode;
958 dxr2_fourArg_t crop;
959 int n=0;
961 sub_vo = NULL;
962 sub_config_count = 0;
963 if(use_ol) {
964 if (arg) {
965 for(n = 0 ; video_out_drivers[n] != NULL ; n++) {
966 const vo_info_t* vi = video_out_drivers[n]->info;
967 if(!vi)
968 continue;
969 if(strcasecmp(arg,vi->short_name) == 0)
970 break;
972 sub_vo = video_out_drivers[n];
973 } else {
974 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] We need a sub driver to initialize the overlay\n");
975 use_ol = 0;
979 if(!sub_vo) {
980 if(use_ol)
981 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] Sub driver '%s' not found => no overlay\n",arg);
982 use_ol = 0;
983 } else {
984 if(sub_vo->preinit(NULL) != 0) {
985 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] Sub vo %s preinit failed => no overlay\n",arg);
986 sub_vo = NULL;
987 use_ol = 0;
988 } else {
989 uint32_t fmt = IMGFMT_BGR24;
990 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] Sub vo %s inited\n",arg);
991 if(sub_vo->control(VOCTRL_QUERY_FORMAT,&fmt) <= 0) {
992 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] Sub vo %s doesn't support BGR24 => no overlay\n",arg);
993 sub_vo->uninit();
994 sub_vo = NULL;
995 use_ol = 0;
1000 dxr2_fd = open( "/dev/dxr2", O_WRONLY);
1001 if( dxr2_fd < 0 ) {
1002 mp_msg(MSGT_VO,MSGL_V, "VO: [dxr2] Error opening /dev/dxr2 for writing!\n" );
1003 return VO_ERROR;
1006 if(ucode)
1007 uCodeFD = open(ucode, O_RDONLY);
1008 else for (n=0; ucodesearchpath[n] != NULL; n++) {
1009 mp_msg(MSGT_VO,MSGL_V,"VO: [dxr2] Looking for microcode in %s... ",
1010 ucodesearchpath[n]);
1011 if ((uCodeFD = open(ucodesearchpath[n], O_RDONLY))>0) {
1012 mp_msg(MSGT_VO,MSGL_V,"ok\n");
1013 break;
1014 } else {
1015 mp_msg(MSGT_VO,MSGL_V,"failed (%s)\n", strerror(errno));
1018 if (uCodeFD < 0) {
1019 mp_msg(MSGT_VO,MSGL_ERR,"VO: [dxr2] Could not open microcode\n");
1020 return VO_ERROR;
1023 uCodeSize = lseek(uCodeFD, 0, SEEK_END);
1024 if ((uCode = malloc(uCodeSize + 4)) == NULL) {
1026 mp_msg(MSGT_VO,MSGL_FATAL,"VO: [dxr2] Could not allocate memory for uCode: %s\n", strerror(errno));
1027 return VO_ERROR;
1029 lseek(uCodeFD, 0, SEEK_SET);
1030 if (read(uCodeFD, uCode+4, uCodeSize) != uCodeSize) {
1032 mp_msg(MSGT_VO,MSGL_ERR,"VO: [dxr2] Could not read uCode uCode: %s\n", strerror(errno));
1033 return VO_ERROR;
1035 close(uCodeFD);
1036 uCode->uCodeLength = uCodeSize;
1038 // upload ucode
1039 ioctl(dxr2_fd, DXR2_IOC_INIT_ZIVADS, uCode);
1041 // reset card
1042 ioctl(dxr2_fd, DXR2_IOC_RESET, NULL);
1043 playing = 0;
1045 if(!use_ol) {
1046 crop.arg1=0;
1047 crop.arg2=0;
1048 crop.arg3=0;
1049 crop.arg4=0;
1050 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_CROPPING, &crop);
1052 return 0;
1055 static int control(uint32_t request, void *data, ...)
1057 switch (request) {
1058 case VOCTRL_QUERY_FORMAT:
1059 return query_format(*((uint32_t*)data));
1060 case VOCTRL_PAUSE:
1061 ioctl(dxr2_fd,DXR2_IOC_PAUSE, NULL);
1062 return VO_TRUE;
1063 case VOCTRL_RESUME:
1064 ioctl(dxr2_fd, DXR2_IOC_PLAY, NULL);
1065 return VO_TRUE;
1066 case VOCTRL_RESET:
1067 flush_dxr2();
1068 ioctl(dxr2_fd, DXR2_IOC_PLAY, NULL);
1069 return VO_TRUE;
1070 case VOCTRL_ONTOP:
1071 vo_x11_ontop();
1072 return VO_TRUE;
1073 case VOCTRL_FULLSCREEN:
1074 if(!use_ol)
1075 return VO_NOTIMPL;
1076 else if(sub_vo) {
1077 int r = sub_vo->control(VOCTRL_FULLSCREEN,0);
1078 if(r == VO_TRUE && !sub_vo_win)
1079 dxr2_set_overlay_window();
1080 return r;
1081 } else {
1082 dxr2_twoArg_t win;
1083 vo_fs = !vo_fs;
1084 win.arg1 = vo_fs ? vo_screenwidth : movie_w;
1085 win.arg2 = vo_fs ? vo_screenheight : movie_h;
1086 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_DIMENSION, &win);
1087 win.arg1 = (vo_screenwidth - win.arg1) / 2;
1088 win.arg2 = (vo_screenheight - win.arg2) / 2;
1089 ioctl(dxr2_fd, DXR2_IOC_SET_OVERLAY_POSITION,&win);
1090 return VO_TRUE;
1092 case VOCTRL_SET_SPU_PALETTE: {
1093 if(ioctl(dxr2_fd,DXR2_IOC_SET_SUBPICTURE_PALETTE,data) < 0) {
1094 mp_msg(MSGT_VO,MSGL_WARN,"VO: [dxr2] SPU palette loading failed\n");
1095 return VO_ERROR;
1097 return VO_TRUE;
1100 return VO_NOTIMPL;