1 /*
2 * This file is part of MPlayer.
3 *
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "fastmemcpy.h"
20 #include "cpudetect.h"
21 #include "libmpcodecs/vf.h"
22 #include "libswscale/swscale.h"
23 #include "libavutil/imgutils.h"
24 #include "libmpcodecs/vf_scale.h"
25 #include "mp_msg.h"
26 #include "help_mp.h"
27
28 // mga_vid drawing functions
29 static void set_window( void ); /* forward declaration to kill warnings */
30 #ifdef VO_XMGA
31 static void mDrawColorKey( void ); /* forward declaration to kill warnings */
32 #endif
33
34 static int mga_next_frame=0;
35
36 static mga_vid_config_t mga_vid_config;
37 static uint8_t *vid_data, *frames[4];
38 static int f = -1;
39
40 static uint32_t drwX,drwY,drwWidth,drwHeight;
41 static uint32_t drwcX,drwcY;
42
43 static struct SwsContext *sws_ctx;
44
draw_alpha(int x0,int y0,int w,int h,unsigned char * src,unsigned char * srca,int stride)45 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
46 uint32_t bespitch = FFALIGN(mga_vid_config.src_width, 32);
47 x0+=mga_vid_config.src_width*(vo_panscan_x>>1)/(vo_dwidth+vo_panscan_x);
48 switch(mga_vid_config.format){
49 case MGA_VID_FORMAT_YV12:
50 case MGA_VID_FORMAT_IYUV:
51 case MGA_VID_FORMAT_I420:
52 vo_draw_alpha_yv12(w,h,src,srca,stride,vid_data+bespitch*y0+x0,bespitch);
53 break;
54 case MGA_VID_FORMAT_YUY2:
55 vo_draw_alpha_yuy2(w,h,src,srca,stride,vid_data+2*(bespitch*y0+x0),2*bespitch);
56 break;
57 case MGA_VID_FORMAT_UYVY:
58 vo_draw_alpha_yuy2(w,h,src,srca,stride,vid_data+2*(bespitch*y0+x0)+1,2*bespitch);
59 break;
60 }
61 }
62
draw_osd(void)63 static void draw_osd(void)
64 {
65 // vo_draw_text(mga_vid_config.src_width,mga_vid_config.src_height,draw_alpha);
66 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);
67 }
68
69
70 static void
draw_slice_g200(uint8_t * image[],int stride[],int width,int height,int x,int y)71 draw_slice_g200(uint8_t *image[], int stride[], int width,int height,int x,int y)
72 {
73 uint32_t bespitch = FFALIGN(mga_vid_config.src_width, 32);
74 int dst_stride[4] = { bespitch, bespitch };
75 uint8_t *dst[4];
76
77 av_image_fill_pointers(dst, AV_PIX_FMT_NV12, mga_vid_config.src_height,
78 vid_data, dst_stride);
79
80 sws_scale(sws_ctx, image, stride, y, height, dst, dst_stride);
81 }
82
83 static void
draw_slice_g400(uint8_t * image[],int stride[],int w,int h,int x,int y)84 draw_slice_g400(uint8_t *image[], int stride[], int w,int h,int x,int y)
85 {
86 uint8_t *dest;
87 uint8_t *dest2;
88 uint32_t bespitch,bespitch2;
89
90 bespitch = FFALIGN(mga_vid_config.src_width, 32);
91 bespitch2 = bespitch/2;
92
93 dest = vid_data + bespitch * y + x;
94 mem2agpcpy_pic(dest, image[0], w, h, bespitch, stride[0]);
95
96 w/=2;h/=2;x/=2;y/=2;
97
98 dest = vid_data + bespitch*mga_vid_config.src_height + bespitch2 * y + x;
99 dest2= dest + bespitch2*mga_vid_config.src_height / 2;
100
101 if(mga_vid_config.format==MGA_VID_FORMAT_YV12){
102 // mga_vid's YV12 assumes Y,U,V order (instead of Y,V,U) :(
103 mem2agpcpy_pic(dest, image[1], w, h, bespitch2, stride[1]);
104 mem2agpcpy_pic(dest2,image[2], w, h, bespitch2, stride[2]);
105 } else {
106 mem2agpcpy_pic(dest, image[2], w, h, bespitch2, stride[2]);
107 mem2agpcpy_pic(dest2,image[1], w, h, bespitch2, stride[1]);
108 }
109
110 }
111
112 static int
draw_slice(uint8_t * src[],int stride[],int w,int h,int x,int y)113 draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
114 {
115
116 #if 0
117 printf("vo: %p/%d %p/%d %p/%d %dx%d/%d;%d \n",
118 src[0],stride[0],
119 src[1],stride[1],
120 src[2],stride[2],
121 w,h,x,y);
122 #endif
123
124 if (mga_vid_config.card_type == MGA_G200)
125 draw_slice_g200(src,stride,w,h,x,y);
126 else
127 draw_slice_g400(src,stride,w,h,x,y);
128 return 0;
129 }
130
131 static void
vo_mga_flip_page(void)132 vo_mga_flip_page(void)
133 {
134
135 // printf("-- flip to %d --\n",mga_next_frame);
136
137 ioctl(f,MGA_VID_FSEL,&mga_next_frame);
138 mga_next_frame=(mga_next_frame+1)%mga_vid_config.num_frames;
139 vid_data=frames[mga_next_frame];
140
141 }
142
get_image(mp_image_t * mpi)143 static uint32_t get_image(mp_image_t *mpi){
144 uint32_t bespitch = FFALIGN(mga_vid_config.src_width, 32);
145 uint32_t bespitch2 = bespitch/2;
146 // printf("mga: get_image() called\n");
147 if(mpi->type==MP_IMGTYPE_STATIC && mga_vid_config.num_frames>1) return VO_FALSE; // it is not static
148 if(mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE; // slow video ram
149 if(mga_vid_config.card_type == MGA_G200 && mpi->flags&MP_IMGFLAG_PLANAR) return VO_FALSE;
150 // printf("width=%d vs. bespitch=%d, flags=0x%X \n",mpi->width,bespitch,mpi->flags);
151 if((mpi->width==bespitch) ||
152 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH))){
153 // we're lucky or codec accepts stride => ok, let's go!
154 if(mpi->flags&MP_IMGFLAG_PLANAR){
155 mpi->planes[0]=vid_data;
156 if(mpi->flags&MP_IMGFLAG_SWAPPED){
157 mpi->planes[1]=vid_data + bespitch*mga_vid_config.src_height;
158 mpi->planes[2]=mpi->planes[1] + bespitch2*mga_vid_config.src_height/2;
159 } else {
160 mpi->planes[2]=vid_data + bespitch*mga_vid_config.src_height;
161 mpi->planes[1]=mpi->planes[2] + bespitch2*mga_vid_config.src_height/2;
162 }
163 mpi->width=mpi->stride[0]=bespitch;
164 mpi->stride[1]=mpi->stride[2]=bespitch2;
165 } else {
166 mpi->planes[0]=vid_data;
167 mpi->width=bespitch;
168 mpi->stride[0]=mpi->width*(mpi->bpp/8);
169 }
170 mpi->flags|=MP_IMGFLAG_DIRECT;
171 // printf("mga: get_image() SUCCESS -> Direct Rendering ENABLED\n");
172 return VO_TRUE;
173 }
174 return VO_FALSE;
175 }
176
177 static uint32_t
draw_image(mp_image_t * mpi)178 draw_image(mp_image_t *mpi){
179 uint32_t bespitch = FFALIGN(mga_vid_config.src_width, 32);
180
181 // if -dr or -slices then do nothing:
182 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
183
184 if(mpi->flags&MP_IMGFLAG_PLANAR){
185 // copy planar:
186 draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,mpi->x,mpi->y);
187 } else {
188 // copy packed:
189 mem2agpcpy_pic(vid_data, mpi->planes[0], // dst,src
190 mpi->w*(mpi->bpp/8), mpi->h, // w,h
191 bespitch*2, mpi->stride[0]); // dstride,sstride
192 }
193 return VO_TRUE;
194 }
195
196 static int
query_format(uint32_t format)197 query_format(uint32_t format)
198 {
199 switch(format){
200 case IMGFMT_YV12:
201 case IMGFMT_I420:
202 case IMGFMT_IYUV:
203 case IMGFMT_YUY2:
204 case IMGFMT_UYVY:
205 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD|VFCAP_HWSCALE_UP|VFCAP_HWSCALE_DOWN|VFCAP_ACCEPT_STRIDE;
206 }
207 return 0;
208 }
209
210 #ifndef VO_XMGA
mga_fullscreen(void)211 static void mga_fullscreen(void)
212 {
213 uint32_t w,h;
214 if ( !vo_fs ) {
215 vo_fs=VO_TRUE;
216 w=vo_screenwidth; h=vo_screenheight;
217 aspect(&w,&h,A_ZOOM);
218 } else {
219 vo_fs=VO_FALSE;
220 w=vo_dwidth; h=vo_dheight;
221 aspect(&w,&h,A_NOZOOM);
222 }
223 mga_vid_config.dest_width = w;
224 mga_vid_config.dest_height= h;
225 mga_vid_config.x_org=(vo_screenwidth-w)/2;
226 mga_vid_config.y_org=(vo_screenheight-h)/2;
227 if ( ioctl( f,MGA_VID_CONFIG,&mga_vid_config ) )
228 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_ErrorInConfigIoctl );
229 }
230 #endif
231
control(uint32_t request,void * data)232 static int control(uint32_t request, void *data)
233 {
234 switch (request) {
235 case VOCTRL_QUERY_FORMAT:
236 return query_format(*((uint32_t*)data));
237 case VOCTRL_GET_IMAGE:
238 return get_image(data);
239 case VOCTRL_DRAW_IMAGE:
240 return draw_image(data);
241 case VOCTRL_SET_EQUALIZER:
242 {
243 vf_equalizer_t *eq=data;
244 short value;
245 uint32_t luma,prev;
246
247 if ( strcmp( eq->item,"brightness" ) && strcmp( eq->item,"contrast" ) ) return VO_FALSE;
248
249 if (ioctl(f,MGA_VID_GET_LUMA,&prev)) {
250 perror("Error in mga_vid_config ioctl()");
251 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotGetLumaValuesFromTheKernelModule);
252 return VO_FALSE;
253 }
254
255 // printf("GET: 0x%4X 0x%4X \n",(prev>>16),(prev&0xffff));
256
257 // printf("value: %d -> ",eq->value);
258 value=((eq->value+100)*255)/200-128; // maps -100=>-128 and +100=>127
259 // printf("%d \n",value);
260
261 if(!strcmp(data,"contrast"))
262 luma = (prev&0xFFFF0000)|(value&0xFFFF);
263 else
264 luma = (prev&0xFFFF)|(value<<16);
265
266 if (ioctl(f,MGA_VID_SET_LUMA,luma)) {
267 perror("Error in mga_vid_config ioctl()");
268 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotSetLumaValuesFromTheKernelModule);
269 return VO_FALSE;
270 }
271
272 return VO_TRUE;
273 }
274
275 case VOCTRL_GET_EQUALIZER:
276 {
277 vf_equalizer_t *eq=data;
278 short val;
279 uint32_t luma;
280
281 if ( strcmp( eq->item,"brightness" ) && strcmp( eq->item,"contrast" ) ) return VO_FALSE;
282
283 if (ioctl(f,MGA_VID_GET_LUMA,&luma)) {
284 perror("Error in mga_vid_config ioctl()");
285 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldNotGetLumaValuesFromTheKernelModule);
286 return VO_FALSE;
287 }
288
289 if ( !strcmp( eq->item,"contrast" ) )
290 val=(luma & 0xFFFF);
291 else
292 val=(luma >> 16);
293
294 eq->value = (val*200)/255;
295
296 return VO_TRUE;
297 }
298
299 #ifndef VO_XMGA
300 case VOCTRL_FULLSCREEN:
301 if (vo_screenwidth && vo_screenheight)
302 mga_fullscreen();
303 else
304 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_ScreenWidthHeightUnknown);
305 return VO_TRUE;
306 case VOCTRL_GET_PANSCAN:
307 if ( !vo_fs ) return VO_FALSE;
308 return VO_TRUE;
309 #endif
310
311 #if defined(VO_XMGA)
312 case VOCTRL_GUISUPPORT:
313 return VO_TRUE;
314 #endif
315
316 #ifdef VO_XMGA
317 case VOCTRL_ONTOP:
318 vo_x11_ontop();
319 return VO_TRUE;
320 case VOCTRL_GET_PANSCAN:
321 if ( !initialized || !vo_fs ) return VO_FALSE;
322 return VO_TRUE;
323 case VOCTRL_FULLSCREEN:
324 vo_x11_fullscreen();
325 vo_panscan_amount=0;
326 /* intended, fallthrough to update panscan on fullscreen/windowed switch */
327 #endif
328 case VOCTRL_SET_PANSCAN:
329 if ( vo_fs && ( vo_panscan != vo_panscan_amount ) ) // || ( !vo_fs && vo_panscan_amount ) )
330 {
331 // int old_y = vo_panscan_y;
332 panscan_calc();
333 // if ( old_y != vo_panscan_y )
334 set_window();
335 }
336 return VO_TRUE;
337 case VOCTRL_UPDATE_SCREENINFO:
338 #ifdef VO_XMGA
339 update_xinerama_info();
340 #else
341 aspect_save_screenres(vo_screenwidth, vo_screenheight);
342 #endif
343 return VO_TRUE;
344 }
345 return VO_NOTIMPL;
346 }
347
348
mga_init(int width,int height,unsigned int format)349 static int mga_init(int width,int height,unsigned int format){
350
351 uint32_t bespitch = FFALIGN(width, 32);
352 switch(format){
353 case IMGFMT_YV12:
354 width = FFALIGN(width, 2);
355 height = FFALIGN(height, 2);
356 mga_vid_config.frame_size = bespitch * height + (bespitch * height) / 2;
357 mga_vid_config.format=MGA_VID_FORMAT_I420; break;
358 case IMGFMT_I420:
359 case IMGFMT_IYUV:
360 width = FFALIGN(width, 2);
361 height = FFALIGN(height, 2);
362 mga_vid_config.frame_size = bespitch * height + (bespitch * height) / 2;
363 mga_vid_config.format=MGA_VID_FORMAT_YV12; break;
364 case IMGFMT_YUY2:
365 mga_vid_config.frame_size = bespitch * height * 2;
366 mga_vid_config.format=MGA_VID_FORMAT_YUY2; break;
367 case IMGFMT_UYVY:
368 mga_vid_config.frame_size = bespitch * height * 2;
369 mga_vid_config.format=MGA_VID_FORMAT_UYVY; break;
370 default:
371 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_InvalidOutputFormat,format);
372 return -1;
373 }
374
375 mga_vid_config.src_width = width;
376 mga_vid_config.src_height= height;
377 if(!mga_vid_config.dest_width)
378 mga_vid_config.dest_width = width;
379 if(!mga_vid_config.dest_height)
380 mga_vid_config.dest_height= height;
381
382 mga_vid_config.colkey_on=0;
383
384 mga_vid_config.num_frames=(vo_directrendering && !vo_doublebuffering)?1:3;
385 mga_vid_config.version=MGA_VID_VERSION;
386
387 if(width > 1024 && height > 1024)
388 {
389 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_MGA_ResolutionTooHigh);
390 return -1;
391 } else if(height <= 1024)
392 {
393 // try whether we have a G550
394 int ret;
395 if ((ret = ioctl(f,MGA_VID_CONFIG,&mga_vid_config)))
396 {
397 if(mga_vid_config.card_type != MGA_G550)
398 {
399 // we don't have a G550, so our resolution is too high
400 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_MGA_ResolutionTooHigh);
401 return -1;
402 } else {
403 // there is a deeper problem
404 // we have a G550, but still couldn't configure mga_vid
405 perror("Error in mga_vid_config ioctl()");
406 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_IncompatibleDriverVersion);
407 return -1;
408 }
409 // if we arrived here, then we could successfully configure mga_vid
410 // at this high resolution
411 }
412 } else {
413 // configure mga_vid in case resolution is < 1024x1024 too
414 if (ioctl(f,MGA_VID_CONFIG,&mga_vid_config))
415 {
416 perror("Error in mga_vid_config ioctl()");
417 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_IncompatibleDriverVersion);
418 return -1;
419 }
420 }
421 if (mga_vid_config.card_type == MGA_G200) {
422 sws_ctx = sws_getContext(width, height, AV_PIX_FMT_YUV420P,
423 width, height, AV_PIX_FMT_NV12,
424 SWS_BILINEAR, NULL, NULL, NULL);
425 if (!sws_ctx) {
426 mp_msg(MSGT_VO, MSGL_FATAL,
427 "Could not get swscale context to scale for G200.\n");
428 return -1;
429 }
430 mp_msg(MSGT_VO, MSGL_WARN, "G200 cards support is untested. "
431 "Please report whether it works.\n");
432 }
433
434 mp_msg(MSGT_VO,MSGL_V,"[MGA] Using %d buffers.\n",mga_vid_config.num_frames);
435
436 frames[0] = mmap(0,mga_vid_config.frame_size*mga_vid_config.num_frames,PROT_WRITE,MAP_SHARED,f,0);
437 frames[1] = frames[0] + 1*mga_vid_config.frame_size;
438 frames[2] = frames[0] + 2*mga_vid_config.frame_size;
439 frames[3] = frames[0] + 3*mga_vid_config.frame_size;
440 mga_next_frame = 0;
441 vid_data = frames[mga_next_frame];
442
443 //clear the buffer
444 memset(frames[0],0x80,mga_vid_config.frame_size*mga_vid_config.num_frames);
445
446 #ifndef VO_XMGA
447 ioctl(f,MGA_VID_ON,0);
448 #endif
449
450 return 0;
451 }
452
mga_uninit(void)453 static int mga_uninit(void){
454 if(f>=0){
455 ioctl( f,MGA_VID_OFF,0 );
456 munmap(frames[0],mga_vid_config.frame_size*mga_vid_config.num_frames);
457 close(f);
458 f = -1;
459 }
460 if (sws_ctx) {
461 sws_freeContext(sws_ctx);
462 }
463 return 0;
464 }
465
preinit(const char * vo_subdevice)466 static int preinit(const char *vo_subdevice)
467 {
468 uint32_t ver;
469 const char *devname=vo_subdevice?vo_subdevice:"/dev/mga_vid";
470
471 f = open(devname,O_RDWR);
472 if(f == -1)
473 {
474 perror("open");
475 mp_msg(MSGT_VO,MSGL_WARN, MSGTR_LIBVO_MGA_CouldntOpen,devname);
476 return -1;
477 }
478
479 // check whether the mga_vid driver has the same
480 // version as we expect
481
482 if(ioctl(f,MGA_VID_GET_VERSION,&ver) == -1 || MGA_VID_VERSION != ver)
483 {
484 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_MGA_mgavidVersionMismatch, ver, MGA_VID_VERSION);
485 return -1;
486 }
487
488 #ifdef VO_XMGA
489 if (!vo_init()) {
490 close(f);
491 return -1;
492 }
493 #endif
494
495 return 0;
496 }
497
set_window(void)498 static void set_window( void ){
499
500 drwcX = vo_dx;
501 drwcY = vo_dy;
502 drwWidth = vo_dwidth;
503 drwHeight = vo_dheight;
504
505 aspect(&drwWidth, &drwHeight, A_WINZOOM);
506 panscan_calc_windowed();
507 drwWidth += vo_panscan_x;
508 drwHeight += vo_panscan_y;
509 drwWidth = FFMIN(drwWidth, vo_screenwidth);
510 drwHeight = FFMIN(drwHeight, vo_screenheight);
511 drwX = (vo_dwidth - drwWidth ) / 2;
512 drwY = (vo_dheight - drwHeight) / 2;
513 drwcX += drwX;
514 drwcY += drwY;
515
516 #ifdef VO_XMGA
517 #ifdef CONFIG_XINERAMA
518 if(XineramaIsActive(mDisplay))
519 {
520 XineramaScreenInfo *screens;
521 int num_screens;
522 int i;
523
524 screens = XineramaQueryScreens(mDisplay,&num_screens);
525
526 /* find the screen we are on */
527 i = 0;
528 while(i<num_screens &&
529 ((screens[i].x_org < drwcX) ||
530 (screens[i].y_org < drwcY) ||
531 (screens[i].x_org + screens[i].width >= drwcX) ||
532 (screens[i].y_org + screens[i].height >= drwcY)))
533 {
534 i++;
535 }
536
537 if(i<num_screens)
538 {
539 /* save the screen we are on */
540 xinerama_screen = i;
541 } else {
542 /* oops.. couldnt find the screen we are on
543 * because the upper left corner left the
544 * visual range. assume we are still on the
545 * same screen
546 */
547 i = xinerama_screen;
548 }
549
550 if(xinerama_screen == -1)
551 {
552 // The default value of the xinerama_screen is
553 // still there. Which means we could never
554 // figure out on which screen we are.
555 // Choose the first screen as default
556 xinerama_screen = i = 0;
557 }
558
559 /* set drwcX and drwcY to the right values */
560 drwcX = drwcX - screens[i].x_org;
561 drwcY = drwcY - screens[i].y_org;
562 XFree(screens);
563 }
564
565 #endif
566
567 mDrawColorKey();
568 #endif
569
570 mga_vid_config.x_org=drwcX;
571 mga_vid_config.y_org=drwcY;
572 mga_vid_config.dest_width=drwWidth;
573 mga_vid_config.dest_height=drwHeight;
574 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?)" );
575 }
576