Merge svn changes up to r27441
[mplayer.git] / libvo / mga_common.c
blob34eee958e391990c08da3c0895c68a5c7fffba9e
2 #include "fastmemcpy.h"
3 #include "cpudetect.h"
4 #include "libswscale/swscale.h"
5 #include "libswscale/rgb2rgb.h"
6 #include "libmpcodecs/vf_scale.h"
7 #include "mp_msg.h"
8 #include "help_mp.h"
9 #include "old_vo_wrapper.h"
11 // mga_vid drawing functions
12 static void set_window( void ); /* forward declaration to kill warnings */
13 #ifdef VO_XMGA
14 static void mDrawColorKey( void ); /* forward declaration to kill warnings */
15 #endif
17 static int mga_next_frame=0;
19 static mga_vid_config_t mga_vid_config;
20 static uint8_t *vid_data, *frames[4];
21 static int f = -1;
23 static uint32_t drwX,drwY,drwWidth,drwHeight;
24 #ifdef VO_XMGA
25 static uint32_t drwBorderWidth,drwDepth;
26 #endif
27 static uint32_t drwcX,drwcY,dwidth,dheight;
29 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
30 uint32_t bespitch = (mga_vid_config.src_width + 31) & ~31;
31 x0+=mga_vid_config.src_width*(vo_panscan_x>>1)/(vo_dwidth+vo_panscan_x);
32 switch(mga_vid_config.format){
33 case MGA_VID_FORMAT_YV12:
34 case MGA_VID_FORMAT_IYUV:
35 case MGA_VID_FORMAT_I420:
36 vo_draw_alpha_yv12(w,h,src,srca,stride,vid_data+bespitch*y0+x0,bespitch);
37 break;
38 case MGA_VID_FORMAT_YUY2:
39 vo_draw_alpha_yuy2(w,h,src,srca,stride,vid_data+2*(bespitch*y0+x0),2*bespitch);
40 break;
41 case MGA_VID_FORMAT_UYVY:
42 vo_draw_alpha_yuy2(w,h,src,srca,stride,vid_data+2*(bespitch*y0+x0)+1,2*bespitch);
43 break;
47 static void draw_osd(void)
49 // vo_draw_text(mga_vid_config.src_width,mga_vid_config.src_height,draw_alpha);
50 vo_draw_text(mga_vid_config.src_width-mga_vid_config.src_width*vo_panscan_x/(vo_dwidth+vo_panscan_x),mga_vid_config.src_height,draw_alpha);
54 //static void
55 //write_slice_g200(uint8_t *y,uint8_t *cr, uint8_t *cb,uint32_t slice_num)
57 static void
58 draw_slice_g200(uint8_t *image[], int stride[], int width,int height,int x,int y)
60 uint8_t *dest;
61 uint32_t bespitch = (mga_vid_config.src_width + 31) & ~31;
63 dest = vid_data + bespitch*y + x;
64 mem2agpcpy_pic(dest, image[0], width, height, bespitch, stride[0]);
66 width/=2;height/=2;x/=2;y/=2;
68 dest = vid_data + bespitch*mga_vid_config.src_height + bespitch*y + 2*x;
70 interleaveBytes(image[1],image[2],dest,
71 width, height,
72 stride[1], stride[2], bespitch);
75 static void
76 draw_slice_g400(uint8_t *image[], int stride[], int w,int h,int x,int y)
78 uint8_t *dest;
79 uint8_t *dest2;
80 uint32_t bespitch,bespitch2;
82 bespitch = (mga_vid_config.src_width + 31) & ~31;
83 bespitch2 = bespitch/2;
85 dest = vid_data + bespitch * y + x;
86 mem2agpcpy_pic(dest, image[0], w, h, bespitch, stride[0]);
88 w/=2;h/=2;x/=2;y/=2;
90 dest = vid_data + bespitch*mga_vid_config.src_height + bespitch2 * y + x;
91 dest2= dest + bespitch2*mga_vid_config.src_height / 2;
93 if(mga_vid_config.format==MGA_VID_FORMAT_YV12){
94 // mga_vid's YV12 assumes Y,U,V order (insteda of Y,V,U) :(
95 mem2agpcpy_pic(dest, image[1], w, h, bespitch2, stride[1]);
96 mem2agpcpy_pic(dest2,image[2], w, h, bespitch2, stride[2]);
97 } else {
98 mem2agpcpy_pic(dest, image[2], w, h, bespitch2, stride[2]);
99 mem2agpcpy_pic(dest2,image[1], w, h, bespitch2, stride[1]);
104 static int
105 draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
108 #if 0
109 printf("vo: %p/%d %p/%d %p/%d %dx%d/%d;%d \n",
110 src[0],stride[0],
111 src[1],stride[1],
112 src[2],stride[2],
113 w,h,x,y);
114 #endif
116 if (mga_vid_config.card_type == MGA_G200)
117 draw_slice_g200(src,stride,w,h,x,y);
118 else
119 draw_slice_g400(src,stride,w,h,x,y);
120 return 0;
123 static void
124 vo_mga_flip_page(void)
127 // printf("-- flip to %d --\n",mga_next_frame);
129 #if 1
130 ioctl(f,MGA_VID_FSEL,&mga_next_frame);
131 mga_next_frame=(mga_next_frame+1)%mga_vid_config.num_frames;
132 vid_data=frames[mga_next_frame];
133 #endif
137 static int
138 draw_frame(uint8_t *src[])
140 mp_msg(MSGT_VO,MSGL_WARN,"!!! mga::draw_frame() called !!!\n");
141 return 0;
144 static uint32_t get_image(mp_image_t *mpi){
145 uint32_t bespitch = (mga_vid_config.src_width + 31) & ~31;
146 uint32_t bespitch2 = bespitch/2;
147 // printf("mga: get_image() called\n");
148 if(mpi->type==MP_IMGTYPE_STATIC && mga_vid_config.num_frames>1) return VO_FALSE; // it is not static
149 if(mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE; // slow video ram
150 if(mga_vid_config.card_type == MGA_G200 && mpi->flags&MP_IMGFLAG_PLANAR) return VO_FALSE;
151 // printf("width=%d vs. bespitch=%d, flags=0x%X \n",mpi->width,bespitch,mpi->flags);
152 if((mpi->width==bespitch) ||
153 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH))){
154 // we're lucky or codec accepts stride => ok, let's go!
155 if(mpi->flags&MP_IMGFLAG_PLANAR){
156 mpi->planes[0]=vid_data;
157 if(mpi->flags&MP_IMGFLAG_SWAPPED){
158 mpi->planes[1]=vid_data + bespitch*mga_vid_config.src_height;
159 mpi->planes[2]=mpi->planes[1] + bespitch2*mga_vid_config.src_height/2;
160 } else {
161 mpi->planes[2]=vid_data + bespitch*mga_vid_config.src_height;
162 mpi->planes[1]=mpi->planes[2] + bespitch2*mga_vid_config.src_height/2;
164 mpi->width=mpi->stride[0]=bespitch;
165 mpi->stride[1]=mpi->stride[2]=bespitch2;
166 } else {
167 mpi->planes[0]=vid_data;
168 mpi->width=bespitch;
169 mpi->stride[0]=mpi->width*(mpi->bpp/8);
171 mpi->flags|=MP_IMGFLAG_DIRECT;
172 // printf("mga: get_image() SUCCESS -> Direct Rendering ENABLED\n");
173 return VO_TRUE;
175 return VO_FALSE;
178 static uint32_t
179 draw_image(mp_image_t *mpi){
180 uint32_t bespitch = (mga_vid_config.src_width + 31) & ~31;
182 // if -dr or -slices then do nothing:
183 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
185 if(mpi->flags&MP_IMGFLAG_PLANAR){
186 // copy planar:
187 draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,mpi->x,mpi->y);
188 } else {
189 // copy packed:
190 mem2agpcpy_pic(vid_data, mpi->planes[0], // dst,src
191 mpi->w*(mpi->bpp/8), mpi->h, // w,h
192 bespitch*2, mpi->stride[0]); // dstride,sstride
194 return VO_TRUE;
197 static int
198 query_format(uint32_t format)
200 switch(format){
201 case IMGFMT_YV12:
202 case IMGFMT_I420:
203 case IMGFMT_IYUV:
204 case IMGFMT_YUY2:
205 case IMGFMT_UYVY:
206 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD|VFCAP_HWSCALE_UP|VFCAP_HWSCALE_DOWN|VFCAP_ACCEPT_STRIDE;
208 return 0;
211 #ifndef VO_XMGA
212 static void mga_fullscreen()
214 uint32_t w,h;
215 if ( !vo_fs ) {
216 vo_fs=VO_TRUE;
217 w=vo_screenwidth; h=vo_screenheight;
218 aspect(&w,&h,A_ZOOM);
219 } else {
220 vo_fs=VO_FALSE;
221 w=vo_dwidth; h=vo_dheight;
222 aspect(&w,&h,A_NOZOOM);
224 mga_vid_config.dest_width = w;
225 mga_vid_config.dest_height= h;
226 mga_vid_config.x_org=(vo_screenwidth-w)/2;
227 mga_vid_config.y_org=(vo_screenheight-h)/2;
228 if ( ioctl( f,MGA_VID_CONFIG,&mga_vid_config ) )
229 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_ErrorInConfigIoctl );
231 #endif
233 static int control(uint32_t request, void *data)
235 switch (request) {
236 case VOCTRL_QUERY_FORMAT:
237 return query_format(*((uint32_t*)data));
238 case VOCTRL_GET_IMAGE:
239 return get_image(data);
240 case VOCTRL_DRAW_IMAGE:
241 return draw_image(data);
242 case VOCTRL_SET_EQUALIZER:
244 short value;
245 uint32_t luma,prev;
246 struct voctrl_set_equalizer_args *args = data;
248 if (strcmp(args->name, "brightness") && strcmp(args->name, "contrast"))
249 return VO_FALSE;
251 if (ioctl(f,MGA_VID_GET_LUMA,&prev)) {
252 perror("Error in mga_vid_config ioctl()");
253 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotGetLumaValuesFromTheKernelModule);
254 return VO_FALSE;
257 // printf("GET: 0x%4X 0x%4X \n",(prev>>16),(prev&0xffff));
259 value = args->value;
261 // printf("value: %d -> ",value);
262 value=((value+100)*255)/200-128; // maps -100=>-128 and +100=>127
263 // printf("%d \n",value);
265 if (!strcmp(args->name, "contrast"))
266 luma = (prev&0xFFFF0000)|(value&0xFFFF);
267 else
268 luma = (prev&0xFFFF)|(value<<16);
270 if (ioctl(f,MGA_VID_SET_LUMA,luma)) {
271 perror("Error in mga_vid_config ioctl()");
272 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotSetLumaValuesFromTheKernelModule);
273 return VO_FALSE;
276 return VO_TRUE;
279 case VOCTRL_GET_EQUALIZER:
281 short val;
282 uint32_t luma;
283 struct voctrl_get_equalizer_args *args = data;
285 if (strcmp(args->name, "brightness") && strcmp(args->name, "contrast"))
286 return VO_FALSE;
288 if (ioctl(f,MGA_VID_GET_LUMA,&luma)) {
289 perror("Error in mga_vid_config ioctl()");
290 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotGetLumaValuesFromTheKernelModule);
291 return VO_FALSE;
294 if (!strcmp(args->name, "contrast"))
295 val=(luma & 0xFFFF);
296 else
297 val=(luma >> 16);
299 *args->valueptr = (val*200)/255;
301 return VO_TRUE;
304 #ifndef VO_XMGA
305 case VOCTRL_FULLSCREEN:
306 if (vo_screenwidth && vo_screenheight)
307 mga_fullscreen();
308 else
309 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_ScreenWidthHeightUnknown);
310 return VO_TRUE;
311 case VOCTRL_GET_PANSCAN:
312 if ( !vo_fs ) return VO_FALSE;
313 return VO_TRUE;
314 #endif
316 #if defined(VO_XMGA) && defined(CONFIG_GUI)
317 case VOCTRL_GUISUPPORT:
318 return VO_TRUE;
319 #endif
321 #ifdef VO_XMGA
322 case VOCTRL_ONTOP:
323 vo_x11_ontop();
324 return VO_TRUE;
325 case VOCTRL_GET_PANSCAN:
326 if ( !initialized || !vo_fs ) return VO_FALSE;
327 return VO_TRUE;
328 case VOCTRL_FULLSCREEN:
329 vo_x11_fullscreen();
330 vo_panscan_amount=0;
331 /* indended, fallthrough to update panscan on fullscreen/windowed switch */
332 #endif
333 case VOCTRL_SET_PANSCAN:
334 if ( vo_fs && ( vo_panscan != vo_panscan_amount ) ) // || ( !vo_fs && vo_panscan_amount ) )
336 // int old_y = vo_panscan_y;
337 panscan_calc();
338 // if ( old_y != vo_panscan_y )
339 set_window();
341 return VO_TRUE;
343 return VO_NOTIMPL;
347 static int mga_init(int width,int height,unsigned int format){
349 switch(format){
350 case IMGFMT_YV12:
351 width+=width&1;height+=height&1;
352 mga_vid_config.frame_size = ((width + 31) & ~31) * height + (((width + 31) & ~31) * height) / 2;
353 mga_vid_config.format=MGA_VID_FORMAT_I420; break;
354 case IMGFMT_I420:
355 case IMGFMT_IYUV:
356 width+=width&1;height+=height&1;
357 mga_vid_config.frame_size = ((width + 31) & ~31) * height + (((width + 31) & ~31) * height) / 2;
358 mga_vid_config.format=MGA_VID_FORMAT_YV12; break;
359 case IMGFMT_YUY2:
360 mga_vid_config.frame_size = ((width + 31) & ~31) * height * 2;
361 mga_vid_config.format=MGA_VID_FORMAT_YUY2; break;
362 case IMGFMT_UYVY:
363 mga_vid_config.frame_size = ((width + 31) & ~31) * height * 2;
364 mga_vid_config.format=MGA_VID_FORMAT_UYVY; break;
365 default:
366 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_InvalidOutputFormat,format);
367 return -1;
370 mga_vid_config.src_width = width;
371 mga_vid_config.src_height= height;
372 if(!mga_vid_config.dest_width)
373 mga_vid_config.dest_width = width;
374 if(!mga_vid_config.dest_height)
375 mga_vid_config.dest_height= height;
377 mga_vid_config.colkey_on=0;
379 mga_vid_config.num_frames=(vo_directrendering && !vo_doublebuffering)?1:3;
380 mga_vid_config.version=MGA_VID_VERSION;
382 if(width > 1024 && height > 1024)
384 mp_msg(MSGT_VO,MSGL_ERR, MGSTR_LIBVO_MGA_ResolutionTooHigh);
385 return -1;
386 } else if(height <= 1024)
388 // try whether we have a G550
389 int ret;
390 if ((ret = ioctl(f,MGA_VID_CONFIG,&mga_vid_config)))
392 if(mga_vid_config.card_type != MGA_G550)
394 // we don't have a G550, so our resolution is too high
395 mp_msg(MSGT_VO,MSGL_ERR, MGSTR_LIBVO_MGA_ResolutionTooHigh);
396 return -1;
397 } else {
398 // there is a deeper problem
399 // we have a G550, but still couldn't configure mga_vid
400 perror("Error in mga_vid_config ioctl()");
401 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_IncompatibleDriverVersion);
402 return -1;
404 // if we arrived here, then we could successfully configure mga_vid
405 // at this high resolution
407 } else {
408 // configure mga_vid in case resolution is < 1024x1024 too
409 if (ioctl(f,MGA_VID_CONFIG,&mga_vid_config))
411 perror("Error in mga_vid_config ioctl()");
412 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_IncompatibleDriverVersion);
413 return -1;
417 mp_msg(MSGT_VO,MSGL_V,"[MGA] Using %d buffers.\n",mga_vid_config.num_frames);
419 frames[0] = (char*)mmap(0,mga_vid_config.frame_size*mga_vid_config.num_frames,PROT_WRITE,MAP_SHARED,f,0);
420 frames[1] = frames[0] + 1*mga_vid_config.frame_size;
421 frames[2] = frames[0] + 2*mga_vid_config.frame_size;
422 frames[3] = frames[0] + 3*mga_vid_config.frame_size;
423 mga_next_frame = 0;
424 vid_data = frames[mga_next_frame];
426 //clear the buffer
427 memset(frames[0],0x80,mga_vid_config.frame_size*mga_vid_config.num_frames);
429 #ifndef VO_XMGA
430 ioctl(f,MGA_VID_ON,0);
431 #endif
433 return 0;
436 static int mga_uninit(){
437 if(f>=0){
438 ioctl( f,MGA_VID_OFF,0 );
439 munmap(frames[0],mga_vid_config.frame_size*mga_vid_config.num_frames);
440 close(f);
441 f = -1;
443 return 0;
446 static int preinit(const char *vo_subdevice)
448 uint32_t ver;
449 const char *devname=vo_subdevice?vo_subdevice:"/dev/mga_vid";
450 sws_rgb2rgb_init(get_sws_cpuflags());
452 f = open(devname,O_RDWR);
453 if(f == -1)
455 perror("open");
456 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldntOpen,devname);
457 return -1;
460 // check whether the mga_vid driver has the same
461 // version as we expect
463 ioctl(f,MGA_VID_GET_VERSION,&ver);
464 if(MGA_VID_VERSION != ver)
466 mp_msg(MSGT_VO, MSGL_ERR, MGSTR_LIBVO_MGA_mgavidVersionMismatch, ver, MGA_VID_VERSION);
467 return -1;
470 #ifdef VO_XMGA
471 if (!vo_init()) {
472 close(f);
473 return -1;
475 #endif
477 return 0;
480 static void set_window( void ){
482 #ifdef VO_XMGA
483 if ( WinID )
485 XGetGeometry( mDisplay,vo_window,&mRoot,&drwX,&drwY,&drwWidth,&drwHeight,&drwBorderWidth,&drwDepth );
486 mp_msg(MSGT_VO,MSGL_V,"[xmga] x: %d y: %d w: %d h: %d\n",drwX,drwY,drwWidth,drwHeight );
487 drwX=0; drwY=0;
488 XTranslateCoordinates( mDisplay,vo_window,mRoot,0,0,&drwcX,&drwcY,&mRoot );
489 mp_msg(MSGT_VO,MSGL_V,"[xmga] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",drwcX,drwcY,drwX,drwY,drwWidth,drwHeight );
492 else
493 #endif
494 { drwX=drwcX=vo_dx; drwY=drwcY=vo_dy; drwWidth=vo_dwidth; drwHeight=vo_dheight; }
496 aspect(&dwidth,&dheight,A_NOZOOM);
497 if ( vo_fs )
499 aspect(&dwidth,&dheight,A_ZOOM);
500 drwX=( vo_screenwidth - (dwidth > vo_screenwidth?vo_screenwidth:dwidth) ) / 2;
501 drwcX+=drwX;
502 drwY=( vo_screenheight - (dheight > vo_screenheight?vo_screenheight:dheight) ) / 2;
503 drwcY+=drwY;
504 drwWidth=(dwidth > vo_screenwidth?vo_screenwidth:dwidth);
505 drwHeight=(dheight > vo_screenheight?vo_screenheight:dheight);
506 mp_msg(MSGT_VO,MSGL_V,"[xmga-fs] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",drwcX,drwcY,drwX,drwY,drwWidth,drwHeight );
508 vo_dwidth=drwWidth; vo_dheight=drwHeight;
510 #ifdef VO_XMGA
511 #ifdef CONFIG_XINERAMA
512 if(XineramaIsActive(mDisplay))
514 XineramaScreenInfo *screens;
515 int num_screens;
516 int i;
518 screens = XineramaQueryScreens(mDisplay,&num_screens);
520 /* find the screen we are on */
521 i = 0;
522 while(i<num_screens &&
523 ((screens[i].x_org < drwcX) ||
524 (screens[i].y_org < drwcY) ||
525 (screens[i].x_org + screens[i].width >= drwcX) ||
526 (screens[i].y_org + screens[i].height >= drwcY)))
528 i++;
531 if(i<num_screens)
533 /* save the screen we are on */
534 xinerama_screen = i;
535 } else {
536 /* oops.. couldnt find the screen we are on
537 * because the upper left corner left the
538 * visual range. assume we are still on the
539 * same screen
541 i = xinerama_screen;
544 if(xinerama_screen == -1)
546 // The default value of the xinerama_screen is
547 // still there. Which means we could never
548 // figure out on which screen we are.
549 // Choose the first screen as default
550 xinerama_screen = i = 0;
553 /* set drwcX and drwcY to the right values */
554 drwcX = drwcX - screens[i].x_org;
555 drwcY = drwcY - screens[i].y_org;
556 XFree(screens);
559 #endif
561 mDrawColorKey();
562 #endif
564 mga_vid_config.x_org=drwcX;
565 mga_vid_config.y_org=drwcY;
566 mga_vid_config.dest_width=drwWidth;
567 mga_vid_config.dest_height=drwHeight;
568 if ( vo_panscan > 0.0f && vo_fs )
570 drwX-=vo_panscan_x>>1;
571 drwY-=vo_panscan_y>>1;
572 drwWidth+=vo_panscan_x;
573 drwHeight+=vo_panscan_y;
575 mga_vid_config.x_org-=vo_panscan_x>>1;
576 mga_vid_config.y_org-=vo_panscan_y>>1;
577 mga_vid_config.dest_width=drwWidth;
578 mga_vid_config.dest_height=drwHeight;
579 #ifdef VO_XMGA
580 mDrawColorKey();
581 #endif
583 if ( ioctl( f,MGA_VID_CONFIG,&mga_vid_config ) ) mp_msg(MSGT_VO,MSGL_WARN,"Error in mga_vid_config ioctl (wrong mga_vid.o version?)" );