Document lmin and lmax lavcopts; mpeg vrc_buf_size values
[mplayer/greg.git] / libvo / vo_xvmc.c
blob17eee511fbf520f84193aabe02ad9a0e5a4261a0
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "video_out.h"
9 #include "video_out_internal.h"
10 #include "fastmemcpy.h"
11 #include "osdep/timer.h"
13 #include <X11/Xlib.h>
14 #include <X11/Xutil.h>
15 #include <X11/Xatom.h>
17 #ifdef HAVE_SHM
18 #include <sys/ipc.h>
19 #include <sys/shm.h>
20 #include <X11/extensions/XShm.h>
21 #endif
23 #include <X11/extensions/Xv.h>
24 #include <X11/extensions/Xvlib.h>
25 #include <X11/extensions/XvMClib.h>
27 #include "x11_common.h"
28 #include "xvmc_render.h"
30 #include "sub.h"
31 #include "aspect.h"
33 #ifdef HAVE_NEW_GUI
34 #include "../Gui/interface.h"
35 #endif
37 //no chanse xinerama to be suported in near future
38 #undef HAVE_XINERAMA
40 #undef NDEBUG
41 #include <assert.h>
44 #define UNUSED(x) ((void)(x))
47 extern int vo_directrendering;
48 extern int vo_verbose;
50 static int benchmark;
51 static int busy_wait;
52 static int use_queue;
54 static int image_width,image_height;
55 static uint32_t drwX,drwY;
57 static XvPortID xv_port;
59 #define AUTO_COLORKEY 0
60 #define BACKGROUND_COLORKEY 1
61 #define AUTOPAINT_COLORKEY 2
62 #define MANUALFILL_COLORKEY 3
63 static int keycolor_handling;
64 static unsigned long keycolor;
66 #define NO_SUBPICTURE 0
67 #define OVERLAY_SUBPICTURE 1
68 #define BLEND_SUBPICTURE 2
69 #define BACKEND_SUBPICTURE 3
71 static int subpicture_mode;
72 static int subpicture_alloc;
73 static XvMCSubpicture subpicture;
74 static XvImageFormatValues subpicture_info;
75 static int subpicture_clear_color;//transparent color for the subpicture or color key for overlay
77 static XvMCSurfaceInfo surface_info;
78 static XvMCContext ctx;
79 static XvMCBlockArray data_blocks;
80 static XvMCMacroBlockArray mv_blocks;
82 #define MAX_SURFACES 8
83 static int number_of_surfaces=0;
84 static XvMCSurface surface_array[MAX_SURFACES];
85 static xvmc_render_state_t * surface_render;
87 static xvmc_render_state_t * p_render_surface_to_show=NULL;
88 static xvmc_render_state_t * p_render_surface_visible=NULL;
90 //display queue, kinda render ahead
91 static xvmc_render_state_t * show_queue[MAX_SURFACES];
92 static int free_element;
95 static void (*draw_osd_fnc)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
96 static void (*clear_osd_fnc)(int x0,int y0, int w,int h);
97 static void (*init_osd_fnc)(void);
99 static void draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
100 static void draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
101 static void clear_osd_subpic(int x0,int y0, int w,int h);
102 static void init_osd_yuv_pal(void);
105 static const struct{
106 int id;//id as xvimages or as mplayer RGB|{8,15,16,24,32}
107 void (* init_func_ptr)();
108 void (* draw_func_ptr)();
109 void (* clear_func_ptr)();
110 } osd_render[]={
111 {0x34344149,init_osd_yuv_pal,draw_osd_AI44,clear_osd_subpic},
112 {0x34344941,init_osd_yuv_pal,draw_osd_IA44,clear_osd_subpic},
113 {0,NULL,NULL,NULL}
116 static void xvmc_free(void);
117 static int count_free_surfaces();
118 static xvmc_render_state_t * find_free_surface();
120 static vo_info_t info = {
121 "XVideo Motion Compensation",
122 "xvmc",
123 "Ivan Kalvachev <iive@users.sf.net>",
127 LIBVO_EXTERN(xvmc);
129 //shm stuff from vo_xv
130 #ifdef HAVE_SHM
131 /* since it doesn't seem to be defined on some platforms */
132 int XShmGetEventBase(Display*);
133 static XShmSegmentInfo Shminfo;
134 static int Shmem_Flag;
135 #endif
136 XvImage * xvimage;
139 static void allocate_xvimage(int xvimage_width,int xvimage_height,int xv_format)
142 * allocate XvImages. FIXME: no error checking, without
143 * mit-shm this will bomb... trzing to fix ::atmos
145 #ifdef HAVE_SHM
146 if ( mLocalDisplay && XShmQueryExtension( mDisplay ) ) Shmem_Flag = 1;
147 else
149 Shmem_Flag = 0;
150 mp_msg(MSGT_VO,MSGL_INFO, "Shared memory not supported\nReverting to normal Xv\n" );
152 if ( Shmem_Flag )
154 xvimage = (XvImage *) XvShmCreateImage(mDisplay, xv_port, xv_format,
155 NULL, xvimage_width, xvimage_height, &Shminfo);
157 Shminfo.shmid = shmget(IPC_PRIVATE, xvimage->data_size, IPC_CREAT | 0777);
158 Shminfo.shmaddr = (char *) shmat(Shminfo.shmid, 0, 0);
159 Shminfo.readOnly = False;
161 xvimage->data = Shminfo.shmaddr;
162 XShmAttach(mDisplay, &Shminfo);
163 XSync(mDisplay, False);
164 shmctl(Shminfo.shmid, IPC_RMID, 0);
166 else
167 #endif
169 xvimage = (XvImage *) XvCreateImage(mDisplay, xv_port, xv_format, NULL, xvimage_width, xvimage_height);
170 xvimage->data = malloc(xvimage->data_size);
171 XSync(mDisplay,False);
173 // memset(xvimage->data,128,xvimage->data_size);
174 return;
177 static void deallocate_xvimage()
179 #ifdef HAVE_SHM
180 if ( Shmem_Flag )
182 XShmDetach( mDisplay,&Shminfo );
183 shmdt( Shminfo.shmaddr );
185 else
186 #endif
188 free(xvimage->data);
190 XFree(xvimage);
192 XFlush( mDisplay );
193 XSync(mDisplay, False);
194 return;
196 //end of vo_xv shm/xvimage code
199 static void init_keycolor(){
200 Atom xv_atom;
201 XvAttribute * attributes;
202 int colorkey;
203 int rez;
204 int attrib_count,i;
206 keycolor=2110;
208 if(keycolor_handling == AUTO_COLORKEY){
209 //XV_AUTOPING_COLORKEY doesn't work for XvMC yet(NVidia 43.63)
210 attributes = XvQueryPortAttributes(mDisplay, xv_port, &attrib_count);
211 if(attributes!=NULL)
212 for (i = 0; i < attrib_count; i++)
213 if (!strcmp(attributes[i].name, "XV_AUTOPAINT_COLORKEY"))
215 xv_atom = XInternAtom(mDisplay, "XV_AUTOPAINT_COLORKEY", False);
216 if(xv_atom!=None)
218 rez=XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1);
219 if(rez == Success)
220 keycolor_handling = AUTOPAINT_COLORKEY;
222 break;
224 XFree(attributes);
227 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
228 if(xv_atom == None) return;
229 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey);
230 if(rez == Success){
231 keycolor = colorkey;
232 if(keycolor_handling == AUTO_COLORKEY){
233 keycolor_handling = MANUALFILL_COLORKEY;
238 //from vo_xmga
239 static void mDrawColorKey(uint32_t x,uint32_t y, uint32_t w, uint32_t h)
241 if( (keycolor_handling != AUTOPAINT_COLORKEY) &&
242 (keycolor_handling != MANUALFILL_COLORKEY) )
243 return;
245 XSetBackground( mDisplay,vo_gc,0 );
246 XClearWindow( mDisplay,vo_window );
248 if(keycolor_handling == MANUALFILL_COLORKEY){
249 XSetForeground( mDisplay,vo_gc,keycolor );
250 XFillRectangle( mDisplay,vo_window,vo_gc,x,y,w,h);
252 XFlush( mDisplay );
256 static int xvmc_check_surface_format(uint32_t format, XvMCSurfaceInfo * surf_info){
257 if ( format == IMGFMT_XVMC_IDCT_MPEG2 ){
258 if( surf_info->mc_type != (XVMC_IDCT|XVMC_MPEG_2) ) return -1;
259 if( surf_info->chroma_format != XVMC_CHROMA_FORMAT_420 ) return -1;
260 return 0;
262 if ( format == IMGFMT_XVMC_MOCO_MPEG2 ){
263 if(surf_info->mc_type != XVMC_MPEG_2) return -1;
264 if(surf_info->chroma_format != XVMC_CHROMA_FORMAT_420) return -1;
265 return 0;
267 return -1;//fail
270 //print all info needed to add new format
271 static void print_xvimage_format_values(XvImageFormatValues *xifv){
272 int i;
273 printf("Format_ID = 0x%X\n",xifv->id);
275 printf(" type = ");
276 if(xifv->type == XvRGB) printf("RGB\n");
277 else if(xifv->type == XvYUV) printf("YUV\n");
278 else printf("Unknown\n");
280 printf(" byte_order = ");
281 if(xifv->byte_order == LSBFirst) printf("LSB First\n");
282 else if(xifv->type == MSBFirst) printf("MSB First\n");
283 else printf("Unknown\n");//yes Linux support other types too
285 printf(" guid = ");
286 for(i=0;i<16;i++)
287 printf("%02X ",(unsigned char)xifv->guid[i]);
288 printf("\n");
290 printf(" bits_per_pixel = %d\n",xifv->bits_per_pixel);
292 printf(" format = ");
293 if(xifv->format == XvPacked) printf("XvPacked\n");
294 else if(xifv->format == XvPlanar) printf("XvPlanar\n");
295 else printf("Unknown\n");
297 printf(" num_planes = %d\n",xifv->num_planes);
299 if(xifv->type == XvRGB){
300 printf(" red_mask = %0X\n", xifv->red_mask);
301 printf(" green_mask = %0X\n",xifv->green_mask);
302 printf(" blue_mask = %0X\n", xifv->blue_mask);
304 if(xifv->type == XvYUV){
305 printf(" y_sample_bits = %d\n u_sample_bits = %d\n v_sample_bits = %d\n",
306 xifv->y_sample_bits,xifv->u_sample_bits,xifv->v_sample_bits);
307 printf(" horz_y_period = %d\n horz_u_period = %d\n horz_v_period = %d\n",
308 xifv->horz_y_period,xifv->horz_u_period,xifv->horz_v_period);
309 printf(" vert_y_period = %d\n vert_u_period = %d\n vert_v_period = %d\n",
310 xifv->vert_y_period,xifv->vert_u_period,xifv->vert_v_period);
312 printf(" component_order = ");
313 for(i=0;i<32;i++)
314 if(xifv->component_order[i]>=32)
315 printf("%c",xifv->component_order[i]);
316 printf("\n");
318 printf(" scanline = ");
319 if(xifv->scanline_order == XvTopToBottom) printf("XvTopToBottom\n");
320 else if(xifv->scanline_order == XvBottomToTop) printf("XvBottomToTop\n");
321 else printf("Unknown\n");
323 printf("\n");
326 // WARNING This function may changes xv_port and surface_info!
327 static int xvmc_find_surface_by_format(int format,int width,int height,
328 XvMCSurfaceInfo * surf_info,int query){
329 int rez;
330 XvAdaptorInfo * ai;
331 int num_adaptors,i;
332 unsigned long p;
333 int s,mc_surf_num;
334 XvMCSurfaceInfo * mc_surf_list;
336 rez = XvQueryAdaptors(mDisplay,DefaultRootWindow(mDisplay),&num_adaptors,&ai);
337 if( rez != Success ) return -1;
338 if( verbose > 2 ) printf("vo_xvmc: Querying %d adaptors\n",num_adaptors);
339 for(i=0; i<num_adaptors; i++)
341 if( verbose > 2) printf("vo_xvmc: Quering adaptor #%d\n",i);
342 if( ai[i].type == 0 ) continue;// we need at least dummy type!
343 //probing ports
344 for(p=ai[i].base_id; p<ai[i].base_id+ai[i].num_ports; p++)
346 if( verbose > 2) printf("vo_xvmc: probing port #%ld\n",p);
347 mc_surf_list = XvMCListSurfaceTypes(mDisplay,p,&mc_surf_num);
348 if( mc_surf_list == NULL || mc_surf_num == 0){
349 if( verbose > 2) printf("vo_xvmc: No XvMC supported. \n");
350 continue;
352 if( verbose > 2) printf("vo_xvmc: XvMC list have %d surfaces\n",mc_surf_num);
353 //we have XvMC list!
354 for(s=0; s<mc_surf_num; s++)
356 if( width > mc_surf_list[s].max_width ) continue;
357 if( height > mc_surf_list[s].max_height ) continue;
358 if( xvmc_check_surface_format(format,&mc_surf_list[s])<0 ) continue;
359 //we have match!
361 if(!query){
362 rez = XvGrabPort(mDisplay,p,CurrentTime);
363 if(rez != Success){
364 if (verbose > 2) printf("vo_xvmc: Fail to grab port %ld\n",p);
365 continue;
367 printf("vo_xvmc: Port %ld grabed\n",p);
368 xv_port = p;
370 goto surface_found;
371 }//for mc surf
372 XFree(mc_surf_list);//if mc_surf_num==0 is list==NULL ?
373 }//for ports
374 }//for adaptors
376 if(!query) printf("vo_xvmc: Could not find free matching surface. Sorry.\n");
377 return 0;
379 // somebody know cleaner way to escape from 3 internal loops?
380 surface_found:
382 memcpy(surf_info,&mc_surf_list[s],sizeof(XvMCSurfaceInfo));
383 if( verbose > 2 || !query)
384 printf("vo_xvmc: Found matching surface with id=%X on %ld port at %d adapter\n",
385 mc_surf_list[s].surface_type_id,p,i);
386 return mc_surf_list[s].surface_type_id;
389 static uint32_t xvmc_draw_image(mp_image_t *mpi){
390 xvmc_render_state_t * rndr;
392 assert(mpi!=NULL);
393 assert(mpi->flags &MP_IMGFLAG_DIRECT);
394 // assert(mpi->flags &MP_IMGFLAGS_DRAWBACK);
396 rndr = (xvmc_render_state_t*)mpi->priv;//there is copy in plane[2]
397 assert( rndr != NULL );
398 assert( rndr->magic == MP_XVMC_RENDER_MAGIC );
399 if( verbose > 3 )
400 printf("vo_xvmc: draw_image(show rndr=%p)\n",rndr);
401 // the surface have passed vf system without been skiped, it will be displayed
402 rndr->state |= MP_XVMC_STATE_DISPLAY_PENDING;
403 p_render_surface_to_show = rndr;
404 return VO_TRUE;
407 static uint32_t preinit(const char *arg){
408 int xv_version,xv_release,xv_request_base,xv_event_base,xv_error_base;
409 int mc_eventBase,mc_errorBase;
410 int mc_ver,mc_rev;
412 //Obtain display handler
413 if (!vo_init()) return -1;//vo_xv
415 //XvMC is subdivision of XVideo
416 if (Success != XvQueryExtension(mDisplay,&xv_version,&xv_release,&xv_request_base,
417 &xv_event_base,&xv_error_base) ){
418 mp_msg(MSGT_VO,MSGL_ERR,"Sorry, Xv(MC) not supported by this X11 version/driver\n");
419 mp_msg(MSGT_VO,MSGL_ERR,"********** Try with -vo x11 or -vo sdl ***********\n");
420 return -1;
422 printf("vo_xvmc: X-Video extension %d.%d\n",xv_version,xv_release);
424 if( True != XvMCQueryExtension(mDisplay,&mc_eventBase,&mc_errorBase) ){
425 printf("vo_xvmc: No X-Video MotionCompensation Extension on %s\n",
426 XDisplayName(NULL));
427 return -1;
430 if(Success == XvMCQueryVersion(mDisplay, &mc_ver, &mc_rev) ){
431 printf("vo_xvmc: X-Video MotionCompensation Extension version %i.%i\n",
432 mc_ver,mc_rev);
434 else{
435 printf("vo_xvmc: Error querying version info!\n");
436 return -1;
438 surface_render = NULL;
439 xv_port = 0;
440 number_of_surfaces = 0;
441 keycolor_handling = MANUALFILL_COLORKEY;//fixme
442 subpicture_alloc = 0;
444 benchmark = 0; //disable PutImageto allow faster display than screen refresh
445 busy_wait = 1;
446 use_queue = 0;
447 if(arg)
448 while(*arg){
449 if(strncmp(arg,"benchmark",9) == 0){
450 arg+=9;
451 if(*arg == ':') arg++;
452 benchmark = 1;//disable PutImageto allow faster display than screen refresh
453 continue;
455 if(strncmp(arg,"wait",4) == 0){
456 arg+=4;
457 if(*arg == ':') arg++;
458 busy_wait = 1;
459 continue;
461 if(strncmp(arg,"sleep",5) == 0){
462 arg+=5;
463 if(*arg == ':') arg++;
464 busy_wait = 0;
465 continue;
467 if(strncmp(arg,"queue",5) == 0){
468 arg+=5;
469 if(*arg == ':') arg++;
470 use_queue = 1;
471 continue;
473 break;
476 return 0;
479 static uint32_t config(uint32_t width, uint32_t height,
480 uint32_t d_width, uint32_t d_height,
481 uint32_t flags, char *title, uint32_t format){
482 int i,mode_id,rez;
483 int numblocks,blocks_per_macroblock;//bpmb we have 6,8,12
485 //from vo_xv
486 char *hello = (title == NULL) ? "XvMC render" : title;
487 XSizeHints hint;
488 XVisualInfo vinfo;
489 XGCValues xgcv;
490 XSetWindowAttributes xswa;
491 XWindowAttributes attribs;
492 unsigned long xswamask;
493 int depth;
494 #ifdef HAVE_XF86VM
495 int vm=0;
496 unsigned int modeline_width, modeline_height;
497 static uint32_t vm_width;
498 static uint32_t vm_height;
499 #endif
500 //end of vo_xv
502 if( !IMGFMT_IS_XVMC(format) )
504 assert(0);//should never happen, abort on debug or
505 return 1;//return error on relese
508 // Find free port that supports MC, by querying adaptors
509 if( xv_port != 0 || number_of_surfaces != 0 ){
510 xvmc_free();
512 numblocks=((width+15)/16)*((height+15)/16);
513 // Find Supported Surface Type
514 mode_id = xvmc_find_surface_by_format(format,width,height,&surface_info,0);//false=1 to grab port, not query
516 rez = XvMCCreateContext(mDisplay, xv_port,mode_id,width,height,XVMC_DIRECT,&ctx);
517 if( rez != Success ) return -1;
518 if( ctx.flags & XVMC_DIRECT ){
519 printf("vo_xvmc: Allocated Direct Context\n");
520 }else{
521 printf("vo_xvmc: Allocated Indirect Context!\n");
525 blocks_per_macroblock = 6;
526 if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_422)
527 blocks_per_macroblock = 8;
528 if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_444)
529 blocks_per_macroblock = 12;
531 rez = XvMCCreateBlocks(mDisplay,&ctx,numblocks*blocks_per_macroblock,&data_blocks);
532 if( rez != Success ){
533 XvMCDestroyContext(mDisplay,&ctx);
534 return -1;
536 printf("vo_xvmc: data_blocks allocated\n");
538 rez = XvMCCreateMacroBlocks(mDisplay,&ctx,numblocks,&mv_blocks);
539 if( rez != Success ){
540 XvMCDestroyBlocks(mDisplay,&data_blocks);
541 XvMCDestroyContext(mDisplay,&ctx);
542 return -1;
544 printf("vo_xvmc: mv_blocks allocated\n");
546 if(surface_render==NULL)
547 surface_render=malloc(MAX_SURFACES*sizeof(xvmc_render_state_t));//easy mem debug
549 for(i=0; i<MAX_SURFACES; i++){
550 rez=XvMCCreateSurface(mDisplay,&ctx,&surface_array[i]);
551 if( rez != Success )
552 break;
553 memset(&surface_render[i],0,sizeof(xvmc_render_state_t));
554 surface_render[i].magic = MP_XVMC_RENDER_MAGIC;
555 surface_render[i].data_blocks = data_blocks.blocks;
556 surface_render[i].mv_blocks = mv_blocks.macro_blocks;
557 surface_render[i].total_number_of_mv_blocks = numblocks;
558 surface_render[i].total_number_of_data_blocks = numblocks*blocks_per_macroblock;;
559 surface_render[i].mc_type = surface_info.mc_type & (~XVMC_IDCT);
560 surface_render[i].idct = (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT;
561 surface_render[i].chroma_format = surface_info.chroma_format;
562 surface_render[i].unsigned_intra = (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED;
563 surface_render[i].p_surface = &surface_array[i];
564 if( verbose > 3 )
565 printf("vo_xvmc: surface[%d] = %p .rndr=%p\n",i,&surface_array[i], &surface_render[i]);
567 number_of_surfaces = i;
568 if( number_of_surfaces < 4 ){// +2 I or P and +2 for B (to avoid visible motion drawing)
569 printf("vo_xvmc: Unable to allocate at least 4 Surfaces\n");
570 uninit();
571 return -1;
573 printf("vo_xvmc: Motion Compensation context allocated - %d surfaces\n",
574 number_of_surfaces);
576 //debug
577 printf("vo_xvmc: idct=%d unsigned_intra=%d\n",
578 (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT,
579 (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED);
581 // Find way to display OSD & subtitle
582 printf("vo_xvmc: looking for OSD support\n");
583 subpicture_mode = NO_SUBPICTURE;
584 if(surface_info.flags & XVMC_OVERLAID_SURFACE)
585 subpicture_mode = OVERLAY_SUBPICTURE;
587 if(surface_info.subpicture_max_width != 0 &&
588 surface_info.subpicture_max_height != 0 ){
589 int s,k,num_subpic;
591 XvImageFormatValues * xvfmv;
592 xvfmv = XvMCListSubpictureTypes(mDisplay, xv_port,
593 surface_info.surface_type_id, &num_subpic);
595 if(num_subpic != 0 && xvfmv != NULL){
596 if(verbose > 3){//Print All subpicture types for debug
597 for(s=0;s<num_subpic;s++)
598 printf(" Subpicture id 0x%08X\n",xvfmv[s].id);
601 for(s=0;s<num_subpic;s++){
602 for(k=0;osd_render[k].draw_func_ptr!=NULL;k++){
603 if(xvfmv[s].id == osd_render[k].id)
605 init_osd_fnc = osd_render[k].init_func_ptr;
606 draw_osd_fnc = osd_render[k].draw_func_ptr;
607 clear_osd_fnc = osd_render[k].clear_func_ptr;
609 subpicture_mode = BLEND_SUBPICTURE;
610 subpicture_info = xvfmv[s];
611 print_xvimage_format_values(&subpicture_info);
612 goto found_subpic;
616 found_subpic:
617 XFree(xvfmv);
619 //Blend2 supicture is always possible, blend1 only at backend
620 if( (subpicture_mode == BLEND_SUBPICTURE) &&
621 (surface_info.flags & XVMC_BACKEND_SUBPICTURE) )
623 subpicture_mode = BACKEND_SUBPICTURE;
628 switch(subpicture_mode){
629 case NO_SUBPICTURE:
630 printf("vo_xvmc: No OSD support for this mode\n");
631 break;
632 case OVERLAY_SUBPICTURE:
633 printf("vo_xvmc: OSD support via color key tricks\n");
634 printf("vo_xvmc: not yet implemented:(\n");
635 break;
636 case BLEND_SUBPICTURE:
637 printf("vo_xvmc: OSD support by additional frontend rendering\n");
638 break;
639 case BACKEND_SUBPICTURE:
640 printf("vo_xvmc: OSD support by beckend rendering (fast)\n");
641 printf("vo_xvmc: Pleace send feedback to configrm that it work,otherwise send bugreport!\n");
642 break;
645 init_keycolor();// take keycolor value and choose method for handling it
647 //taken from vo_xv
648 panscan_init();
650 aspect_save_orig(width,height);
651 aspect_save_prescale(d_width,d_height);
653 image_height = height;
654 image_width = width;
656 vo_mouse_autohide = 1;
658 vo_dx=( vo_screenwidth - d_width ) / 2; vo_dy=( vo_screenheight - d_height ) / 2;
659 geometry(&vo_dx, &vo_dy, &d_width, &d_height, vo_screenwidth, vo_screenheight);
660 vo_dwidth=d_width; vo_dheight=d_height;
662 #ifdef HAVE_XF86VM
663 if( flags&0x02 ) vm = 1;
664 #endif
666 aspect_save_screenres(vo_screenwidth,vo_screenheight);
668 #ifdef HAVE_NEW_GUI
669 if(use_gui)
670 guiGetEvent( guiSetShVideo,0 ); // let the GUI to setup/resize our window
671 else
672 #endif
674 hint.x = vo_dx;
675 hint.y = vo_dy;
676 aspect(&d_width,&d_height,A_NOZOOM);
677 hint.width = d_width;
678 hint.height = d_height;
679 #ifdef HAVE_XF86VM
680 if ( vm )
682 if ((d_width==0) && (d_height==0))
683 { vm_width=image_width; vm_height=image_height; }
684 else
685 { vm_width=d_width; vm_height=d_height; }
686 vo_vm_switch(vm_width, vm_height,&modeline_width, &modeline_height);
687 hint.x=(vo_screenwidth-modeline_width)/2;
688 hint.y=(vo_screenheight-modeline_height)/2;
689 hint.width=modeline_width;
690 hint.height=modeline_height;
691 aspect_save_screenres(modeline_width,modeline_height);
693 else
694 #endif
695 if ( vo_fs )
697 #ifdef X11_FULLSCREEN
698 /* this code replaces X11_FULLSCREEN hack in mplayer.c
699 * aspect() is available through aspect.h for all vos.
700 * besides zooming should only be done with -zoom,
701 * but I leave the old -fs behaviour so users don't get
702 * irritated for now (and send lots o' mails ;) ::atmos
705 aspect(&d_width,&d_height,A_ZOOM);
706 #endif
709 vo_dwidth=d_width; vo_dheight=d_height;
710 hint.flags = PPosition | PSize /* | PBaseSize */;
711 hint.base_width = hint.width; hint.base_height = hint.height;
712 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
713 depth=attribs.depth;
714 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) depth = 24;
715 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
717 xswa.background_pixel = 0;
718 if (keycolor_handling == BACKGROUND_COLORKEY)
719 xswa.background_pixel = keycolor;// 2110;
720 xswa.border_pixel = 0;
721 xswamask = CWBackPixel | CWBorderPixel;
723 if ( WinID>=0 ){
724 vo_window = WinID ? ((Window)WinID) : mRootWin;
725 if ( WinID )
727 XUnmapWindow( mDisplay,vo_window );
728 XChangeWindowAttributes( mDisplay,vo_window,xswamask,&xswa );
729 vo_x11_selectinput_witherr( mDisplay,vo_window,StructureNotifyMask | KeyPressMask | PropertyChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask );
730 XMapWindow( mDisplay,vo_window );
731 } else { drwX=vo_dx; drwY=vo_dy; }
732 } else
733 if ( vo_window == None ){
734 vo_window = XCreateWindow(mDisplay, mRootWin,
735 hint.x, hint.y, hint.width, hint.height,
736 0, depth,CopyFromParent,vinfo.visual,xswamask,&xswa);
738 vo_x11_classhint( mDisplay,vo_window,"xvmc" );
739 vo_hidecursor(mDisplay,vo_window);
741 vo_x11_selectinput_witherr(mDisplay, vo_window, StructureNotifyMask | KeyPressMask | PropertyChangeMask | ExposureMask |
742 ((WinID==0) ? 0 : (PointerMotionMask
743 | ButtonPressMask | ButtonReleaseMask)) );
744 XSetStandardProperties(mDisplay, vo_window, hello, hello, None, NULL, 0, &hint);
745 XSetWMNormalHints( mDisplay,vo_window,&hint );
746 XMapWindow(mDisplay, vo_window);
747 if ( flags&1 ) vo_x11_fullscreen();
748 else {
749 #ifdef HAVE_XINERAMA
750 vo_x11_xinerama_move(mDisplay,vo_window);
751 #endif
752 vo_x11_sizehint( hint.x, hint.y, hint.width, hint.height,0 );
754 } else {
755 // vo_fs set means we were already at fullscreen
756 vo_x11_sizehint( hint.x, hint.y, hint.width, hint.height,0 );
757 if ( !vo_fs ) XMoveResizeWindow( mDisplay,vo_window,hint.x,hint.y,hint.width,hint.height );
758 if ( flags&1 && !vo_fs ) vo_x11_fullscreen(); // handle -fs on non-first file
761 // vo_x11_sizehint( hint.x, hint.y, hint.width, hint.height,0 );
763 if ( vo_gc != None ) XFreeGC( mDisplay,vo_gc );
764 vo_gc = XCreateGC(mDisplay, vo_window, GCForeground, &xgcv);
765 XFlush(mDisplay);
766 XSync(mDisplay, False);
767 #ifdef HAVE_XF86VM
768 if ( vm )
770 /* Grab the mouse pointer in our window */
771 if(vo_grabpointer)
772 XGrabPointer(mDisplay, vo_window, True, 0,
773 GrabModeAsync, GrabModeAsync,
774 vo_window, None, CurrentTime );
775 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
777 #endif
780 aspect(&vo_dwidth,&vo_dheight,A_NOZOOM);
781 if ( (( flags&1 )&&( WinID <= 0 )) || vo_fs )
783 aspect(&vo_dwidth,&vo_dheight,A_ZOOM);
784 drwX=( vo_screenwidth - (vo_dwidth > vo_screenwidth?vo_screenwidth:vo_dwidth) ) / 2;
785 drwY=( vo_screenheight - (vo_dheight > vo_screenheight?vo_screenheight:vo_dheight) ) / 2;
786 vo_dwidth=(vo_dwidth > vo_screenwidth?vo_screenwidth:vo_dwidth);
787 vo_dheight=(vo_dheight > vo_screenheight?vo_screenheight:vo_dheight);
788 mp_msg(MSGT_VO,MSGL_V, "[xvmc-fs] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,vo_dwidth,vo_dheight );
791 panscan_calc();
793 mp_msg(MSGT_VO,MSGL_V, "[xvmc] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,vo_dwidth,vo_dheight );
795 saver_off(mDisplay); // turning off screen saver
796 //end vo_xv
798 /* store image dimesions for displaying */
799 p_render_surface_visible = NULL;
800 p_render_surface_to_show = NULL;
802 free_element = 0;
804 vo_directrendering = 1;//ugly hack, coz xvmc works only with direct rendering
805 return 0;
808 static uint32_t draw_frame(uint8_t *srcp[]){
809 UNUSED(srcp);
810 assert(0);
813 static void init_osd_yuv_pal(){
814 char * palette;
815 int rez;
816 int i,j;
817 int snum,seb;
818 int Y,U,V;
820 subpicture_clear_color = 0;
822 if(subpicture.num_palette_entries > 0){
824 snum = subpicture.num_palette_entries;
825 seb = subpicture.entry_bytes;
826 palette = (char*)malloc(snum*seb);//check fail
827 if(palette == NULL) return;
828 for(i=0; i<snum; i++){
829 // 0-black max-white the other are gradients
830 Y = i*(1 << subpicture_info.y_sample_bits)/snum;//snum=2;->(0),(1*(1<<1)/2)
831 U = 1 << (subpicture_info.u_sample_bits - 1);
832 V = 1 << (subpicture_info.v_sample_bits - 1);
833 for(j=0; j<seb; j++)
834 switch(subpicture.component_order[j]){
835 case 'U': palette[i*seb+j] = U; break;
836 case 'V': palette[i*seb+j] = V; break;
837 case 'Y':
838 default:
839 palette[i*seb+j] = Y; break;
842 rez = XvMCSetSubpicturePalette(mDisplay, &subpicture, palette);
843 if(rez!=Success){
844 printf("vo_xvmc: set pallete fail\n");
849 static void clear_osd_subpic(int x0, int y0, int w, int h){
850 int rez;
851 rez=XvMCClearSubpicture(mDisplay, &subpicture,
852 x0, y0, w,h,
853 subpicture_clear_color);
854 if(rez != Success)
855 printf("vo_xvmc: XvMCClearSubpicture failed!\n");
858 static void OSD_init(){
859 unsigned short osd_height, osd_width;
860 int rez;
862 if(subpicture_alloc){
863 if(verbose>3)
864 printf("vo_xvmc: destroying subpicture\n");
865 XvMCDestroySubpicture(mDisplay,&subpicture);
866 deallocate_xvimage();
867 subpicture_alloc = 0;
870 /* if(surface_info.flags & XVMC_SUBPICTURE_INDEPENDENT_SCALING){
871 osd_width = vo_dwidth;
872 osd_height = vo_dheight;
873 }else*/
875 osd_width = image_width;
876 osd_height = image_height;
879 if(osd_width > surface_info.subpicture_max_width)
880 osd_width = surface_info.subpicture_max_width;
881 if(osd_height > surface_info.subpicture_max_height)
882 osd_height = surface_info.subpicture_max_height;
883 if(osd_width == 0 || osd_height == 0)
884 return;//if called before window size is known
886 if(verbose > 3)
887 printf("vo_xvmc: creating subpicture (%d,%d) format %X\n",
888 osd_width,osd_height,subpicture_info.id);
890 rez = XvMCCreateSubpicture(mDisplay,&ctx,&subpicture,
891 osd_width,osd_height,subpicture_info.id);
892 if(rez != Success){
893 subpicture_mode = NO_SUBPICTURE;
894 printf("vo_xvmc: Create Subpicture failed, OSD disabled\n");
895 return;
897 if(verbose > 3){
898 int i;
899 printf("vo_xvmc: Created Subpicture:\n");
900 printf(" xvimage_id=0x%X\n",subpicture.xvimage_id);
901 printf(" width=%d\n",subpicture.width);
902 printf(" height=%d\n",subpicture.height);
903 printf(" num_palette_entries=0x%X\n",subpicture.num_palette_entries);
904 printf(" entry_bytes=0x%X\n",subpicture.entry_bytes);
906 printf(" component_order=\"");
907 for(i=0; i<4; i++)
908 if(subpicture.component_order[i] >= 32)
909 printf("%c", subpicture.component_order[i]);
910 printf("\"\n");
913 //call init for the surface type
914 init_osd_fnc();//init palete,clear color etc ...
915 if(verbose > 3)
916 printf("vo_xvmc: clearing subpicture\n");
917 clear_osd_fnc(0, 0, subpicture.width, subpicture.height);
919 allocate_xvimage(subpicture.width, subpicture.height, subpicture_info.id);
920 subpicture_alloc = 1;
923 static void draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
924 int ox,oy;
925 int rez;
927 if(verbose > 3)
928 printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);
930 for(ox=0; ox<w; ox++){
931 for(oy=0; oy<h; oy++){
932 xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]>>4) | ((0-srca[oy*stride+ox])&0xf0);
935 rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
936 w,h,x0,y0);
937 if(rez != Success){
938 printf("vo_xvmc: composite subpicture failed\n");
939 assert(0);
943 static void draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
944 int ox,oy;
945 int rez;
946 if( verbose > 3)
947 printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);
949 for(ox=0; ox<w; ox++){
950 for(oy=0; oy<h; oy++){
951 xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]&0xf0) | (((0-srca[oy*stride+ox])>>4)&0xf);
954 rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
955 w,h,x0,y0);
956 if(rez != Success){
957 printf("vo_xvmc: composite subpicture failed\n");
958 assert(0);
962 static void draw_osd(void){
963 xvmc_render_state_t * osd_rndr;
964 int osd_has_changed;
965 int have_osd_to_draw;
966 int rez;
968 if(verbose > 3)
969 printf("vo_xvmc: draw_osd ,OSD_mode=%d, surface_to_show=%p\n",
970 subpicture_mode,p_render_surface_to_show);
972 if(subpicture_mode == BLEND_SUBPICTURE ||
973 subpicture_mode == BACKEND_SUBPICTURE ){
975 if(!subpicture_alloc) //allocate subpicture when dimensions are known
976 OSD_init();
977 if(!subpicture_alloc)
978 return;//dimensions still unknown.
980 osd_has_changed = vo_update_osd(subpicture.width, subpicture.height);
981 have_osd_to_draw = vo_osd_check_range_update(0, 0, subpicture.width,
982 subpicture.height);
984 if(!have_osd_to_draw)
985 return;//nothing to draw,no subpic, no blend
987 if(osd_has_changed){
988 //vo_remove_text(subpicture.width, subpicture.height,clear_osd_fnc)
989 clear_osd_fnc(0,0,subpicture.width,subpicture.height);
990 vo_draw_text(subpicture.width, subpicture.height, draw_osd_fnc);
992 XvMCSyncSubpicture(mDisplay,&subpicture);//todo usleeep wait!
994 if(subpicture_mode == BLEND_SUBPICTURE){
995 osd_rndr = find_free_surface();
996 if(osd_rndr == NULL)
997 return;// no free surface to draw OSD in
999 rez = XvMCBlendSubpicture2(mDisplay,
1000 p_render_surface_to_show->p_surface, osd_rndr->p_surface,
1001 &subpicture,
1002 0, 0, subpicture.width, subpicture.height,
1003 0, 0, image_width, image_height);
1004 if(rez!=Success){
1005 printf("vo_xvmc: BlendSubpicture failed rez=%d\n",rez);
1006 assert(0);
1007 return;
1009 // XvMCFlushSurface(mDisplay,osd_rndr->p_surface);//fixme- should I?
1011 //When replaceing the surface with osd one, save the flags too!
1012 osd_rndr->picture_structure = p_render_surface_to_show->picture_structure;
1013 osd_rndr->display_flags = p_render_surface_to_show->display_flags;
1014 //add more if needed osd_rndr-> = p_render_surface_to_show->;
1016 p_render_surface_to_show->state &= ~MP_XVMC_STATE_DISPLAY_PENDING;
1017 p_render_surface_to_show->state |= MP_XVMC_STATE_OSD_SOURCE;
1018 p_render_surface_to_show->p_osd_target_surface_render = osd_rndr;
1020 p_render_surface_to_show = osd_rndr;
1021 p_render_surface_to_show->state = MP_XVMC_STATE_DISPLAY_PENDING;
1023 if(verbose > 3)
1024 printf("vo_xvmc:draw_osd: surface_to_show changed to %p\n",osd_rndr);
1025 }//endof if(BLEND)
1026 if(subpicture_mode == BACKEND_SUBPICTURE){
1027 rez = XvMCBlendSubpicture(mDisplay,
1028 p_render_surface_to_show->p_surface,
1029 &subpicture,
1030 0, 0, subpicture.width, subpicture.height,
1031 0, 0, image_width, image_height);
1035 }//if(BLEND||BACKEND)
1038 static void xvmc_sync_surface(XvMCSurface * srf){
1039 int status,rez;
1040 rez = XvMCGetSurfaceStatus(mDisplay,srf,&status);
1041 assert(rez==Success);
1042 if((status & XVMC_RENDERING) == 0)
1043 return;//surface is already complete
1044 if(!busy_wait){
1045 rez = XvMCFlushSurface(mDisplay, srf);
1046 assert(rez==Success);
1049 usec_sleep(1000);//1ms (may be 20ms on linux)
1050 XvMCGetSurfaceStatus(mDisplay,srf,&status);
1051 } while (status & XVMC_RENDERING);
1052 return;//done
1055 XvMCSyncSurface(mDisplay, srf);
1058 static void flip_page(void){
1059 int rez;
1060 int clipX,clipY,clipW,clipH;
1061 int i,cfs;
1063 clipX = drwX-(vo_panscan_x>>1);
1064 clipY = drwY-(vo_panscan_y>>1);
1065 clipW = vo_dwidth+vo_panscan_x;
1066 clipH = vo_dheight+vo_panscan_y;
1068 if( verbose > 3 )
1069 printf("vo_xvmc: flip_page show(rndr=%p)\n\n",p_render_surface_to_show);
1071 if(p_render_surface_to_show == NULL) return;
1072 assert( p_render_surface_to_show->magic == MP_XVMC_RENDER_MAGIC );
1073 //fixme assert( p_render_surface_to_show != p_render_surface_visible);
1075 if(use_queue){
1076 // fill the queue until only n free surfaces remain
1077 // after that start displaying
1078 cfs = count_free_surfaces();
1079 show_queue[free_element++] = p_render_surface_to_show;
1080 if(cfs > 3){//well have 3 free surfaces after add queue
1081 if(free_element > 1)//a little voodoo magic
1082 xvmc_sync_surface(show_queue[0]->p_surface);
1083 return;
1085 p_render_surface_to_show=show_queue[0];
1086 if(verbose > 4)
1087 printf("vo_xvmc: flip_queue free_element=%d\n",free_element);
1088 free_element--;
1089 for(i=0; i<free_element; i++){
1090 show_queue[i] = show_queue[i+1];
1092 show_queue[free_element] = NULL;
1095 // make sure the rendering is done
1096 xvmc_sync_surface(p_render_surface_to_show->p_surface);
1098 //the visible surface won't be displayed anymore, mark it as free
1099 if(p_render_surface_visible != NULL)
1100 p_render_surface_visible->state &= ~MP_XVMC_STATE_DISPLAY_PENDING;
1102 //!!fixme assert(p_render_surface_to_show->state & MP_XVMC_STATE_DISPLAY_PENDING);
1104 //show it, displaying is always vsynced, so skip it for benchmark
1105 if(!benchmark){
1106 rez = XvMCPutSurface(mDisplay, p_render_surface_to_show->p_surface,
1107 vo_window,
1108 0, 0, image_width, image_height,
1109 clipX, clipY, clipW, clipH,
1110 3);//p_render_surface_to_show->display_flags);
1111 if(rez != Success){
1112 printf("vo_xvmc: PutSurface failer, critical error!\n");
1113 assert(0);
1117 p_render_surface_visible = p_render_surface_to_show;
1118 p_render_surface_to_show = NULL;
1121 static void check_events(void){
1122 int dwidth,dheight;
1123 Window mRoot;
1124 uint32_t drwBorderWidth,drwDepth;
1126 int e=vo_x11_check_events(mDisplay);
1127 if(e&VO_EVENT_RESIZE)
1129 e |= VO_EVENT_EXPOSE;
1131 XGetGeometry( mDisplay,vo_window,&mRoot,&drwX,&drwY,&vo_dwidth,&vo_dheight,
1132 &drwBorderWidth,&drwDepth );
1133 drwX = drwY = 0;
1134 mp_msg(MSGT_VO,MSGL_V, "[xvmc] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,
1135 vo_dwidth,vo_dheight );
1137 aspect(&dwidth,&dheight,A_NOZOOM);
1138 if ( vo_fs )
1140 aspect(&dwidth,&dheight,A_ZOOM);
1141 drwX=( vo_screenwidth - (dwidth > vo_screenwidth?vo_screenwidth:dwidth) ) / 2;
1142 drwY=( vo_screenheight - (dheight > vo_screenheight?vo_screenheight:dheight) ) / 2;
1143 vo_dwidth=(dwidth > vo_screenwidth?vo_screenwidth:dwidth);
1144 vo_dheight=(dheight > vo_screenheight?vo_screenheight:dheight);
1145 mp_msg(MSGT_VO,MSGL_V, "[xvmc-fs] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,vo_dwidth,vo_dheight );
1148 if ( e & VO_EVENT_EXPOSE )
1150 mDrawColorKey(drwX,drwY,vo_dwidth,vo_dheight);
1151 if(p_render_surface_visible != NULL)
1152 XvMCPutSurface(mDisplay, p_render_surface_visible->p_surface,vo_window,
1153 0, 0, image_width, image_height,
1154 drwX,drwY,vo_dwidth,vo_dheight,
1155 3);//,p_render_surface_visible->display_flags);!!
1159 static void xvmc_free(void){
1160 int i;
1161 if( subpicture_alloc ){
1163 XvMCDestroySubpicture(mDisplay,&subpicture);
1164 deallocate_xvimage();
1166 subpicture_alloc = 0;
1168 if(verbose > 3)
1169 printf("vo_xvmc: subpicture destroyed\n");
1172 if( number_of_surfaces ){
1174 XvMCDestroyMacroBlocks(mDisplay,&mv_blocks);
1175 XvMCDestroyBlocks(mDisplay,&data_blocks);
1177 for(i=0; i<number_of_surfaces; i++)
1179 XvMCHideSurface(mDisplay,&surface_array[i]);//it doesn't hurt, I hope
1180 XvMCDestroySurface(mDisplay,&surface_array[i]);
1182 if( (surface_render[i].state != 0) &&
1183 (p_render_surface_visible != &surface_render[i]) )
1184 printf("vo_xvmc::uninit surface_render[%d].status=%d\n",i,
1185 surface_render[i].state);
1188 free(surface_render);surface_render=NULL;
1190 XvMCDestroyContext(mDisplay,&ctx);
1191 number_of_surfaces = 0;
1193 if(verbose > 3) printf("vo_xvmc: Context sucessfuly freed\n");
1197 if( xv_port !=0 ){
1198 XvUngrabPort(mDisplay,xv_port,CurrentTime);
1199 xv_port = 0;
1200 if(verbose > 3) printf("vo_xvmc: xv_port sucessfuly ungrabed\n");
1204 static void uninit(void){
1205 if( verbose > 3 ) printf("vo_xvmc: uninit called\n");
1206 xvmc_free();
1207 //from vo_xv
1208 saver_on(mDisplay);
1209 vo_vm_close(mDisplay);
1210 vo_x11_uninit();
1213 static uint32_t query_format(uint32_t format){
1214 uint32_t flags;
1215 XvMCSurfaceInfo qsurface_info;
1216 int mode_id;
1218 if(verbose > 3)
1219 printf("vo_xvmc: query_format=%X\n",format);
1221 if(!IMGFMT_IS_XVMC(format)) return 0;// no caps supported
1222 mode_id = xvmc_find_surface_by_format(format, 16, 16, &qsurface_info, 1);//true=1 - quering
1224 if( mode_id == 0 ) return 0;
1226 flags = VFCAP_CSP_SUPPORTED |
1227 VFCAP_CSP_SUPPORTED_BY_HW |
1228 VFCAP_ACCEPT_STRIDE;
1230 if( (qsurface_info.subpicture_max_width != 0) &&
1231 (qsurface_info.subpicture_max_height != 0) )
1232 flags|=VFCAP_OSD;
1233 return flags;
1237 static uint32_t draw_slice(uint8_t *image[], int stride[],
1238 int w, int h, int x, int y){
1239 xvmc_render_state_t * rndr;
1240 int rez;
1242 if(verbose > 3)
1243 printf("vo_xvmc: draw_slice y=%d\n",y);
1245 rndr = (xvmc_render_state_t*)image[2];//this is copy of priv-ate
1246 assert( rndr != NULL );
1247 assert( rndr->magic == MP_XVMC_RENDER_MAGIC );
1249 rez = XvMCRenderSurface(mDisplay,&ctx,rndr->picture_structure,
1250 rndr->p_surface,
1251 rndr->p_past_surface,
1252 rndr->p_future_surface,
1253 rndr->flags,
1254 rndr->filled_mv_blocks_num,rndr->start_mv_blocks_num,
1255 &mv_blocks,&data_blocks);
1256 #if 1
1257 if(rez != Success)
1259 int i;
1260 printf("vo_xvmc::slice: RenderSirface returned %d\n",rez);
1262 printf("vo_xvmc::slice: pict=%d,flags=%x,start_blocks=%d,num_blocks=%d\n",
1263 rndr->picture_structure,rndr->flags,rndr->start_mv_blocks_num,
1264 rndr->filled_mv_blocks_num);
1265 printf("vo_xvmc::slice: this_surf=%p, past_surf=%p, future_surf=%p\n",
1266 rndr->p_surface,rndr->p_past_surface,rndr->p_future_surface);
1268 for(i=0; i<rndr->filled_mv_blocks_num; i++){
1269 XvMCMacroBlock* testblock;
1270 testblock = &mv_blocks.macro_blocks[i];
1272 printf("vo_xvmc::slice: mv_block - x=%d,y=%d,mb_type=0x%x,mv_type=0x%x,mv_field_select=%d\n",
1273 testblock->x,testblock->y,testblock->macroblock_type,
1274 testblock->motion_type,testblock->motion_vertical_field_select);
1275 printf("vo_xvmc::slice: dct_type=%d,data_index=0x%x,cbp=%d,pad0=%d\n",
1276 testblock->dct_type,testblock->index,testblock->coded_block_pattern,
1277 testblock->pad0);
1278 printf("vo_xvmc::slice: PMV[0][0][0/1]=(%d,%d)\n",
1279 testblock->PMV[0][0][0],testblock->PMV[0][0][1]);
1282 #endif
1283 assert(rez==Success);
1284 if(verbose > 3 ) printf("vo_xvmc: flush surface\n");
1285 rez = XvMCFlushSurface(mDisplay, rndr->p_surface);
1286 assert(rez==Success);
1288 // rndr->start_mv_blocks_num += rndr->filled_mv_blocks_num;
1289 rndr->start_mv_blocks_num = 0;
1290 rndr->filled_mv_blocks_num = 0;
1292 rndr->next_free_data_block_num = 0;
1294 return VO_TRUE;
1297 //XvMCHide hides the surface on next retrace, so
1298 //check if the surface is not still displaying
1299 static void check_osd_source(xvmc_render_state_t * src_rndr){
1300 xvmc_render_state_t * osd_rndr;
1301 int stat;
1302 //If this is source surface, check does the OSD rendering is compleate
1303 if(src_rndr->state & MP_XVMC_STATE_OSD_SOURCE){
1304 if(verbose > 3)
1305 printf("vo_xvmc: OSD surface=%p quering\n",src_rndr);
1306 osd_rndr = src_rndr->p_osd_target_surface_render;
1307 XvMCGetSurfaceStatus(mDisplay, osd_rndr->p_surface, &stat);
1308 if(!(stat & XVMC_RENDERING))
1309 src_rndr->state &= ~MP_XVMC_STATE_OSD_SOURCE;
1312 static int count_free_surfaces(){
1313 int i,num;
1314 num=0;
1315 for(i=0; i<number_of_surfaces; i++){
1316 check_osd_source(&surface_render[i]);
1317 if(surface_render[i].state == 0)
1318 num++;
1320 return num;
1323 static xvmc_render_state_t * find_free_surface(){
1324 int i,t;
1325 int stat;
1326 xvmc_render_state_t * visible_rndr;
1328 visible_rndr = NULL;
1329 for(i=0; i<number_of_surfaces; i++){
1331 check_osd_source(&surface_render[i]);
1332 if( surface_render[i].state == 0){
1333 XvMCGetSurfaceStatus(mDisplay, surface_render[i].p_surface,&stat);
1334 if( (stat & XVMC_DISPLAYING) == 0 )
1335 return &surface_render[i];
1336 visible_rndr = &surface_render[i];// remember it, use as last resort
1340 //all surfaces are busy, but there is one that will be free
1341 //on next monitor retrace, we just have to wait
1342 if(visible_rndr != NULL){
1343 printf("vo_xvmc: waiting retrace\n");
1344 for(t=0;t<1000;t++){
1345 usec_sleep(1000);//1ms
1346 XvMCGetSurfaceStatus(mDisplay, visible_rndr->p_surface,&stat);
1347 if( (stat & XVMC_DISPLAYING) == 0 )
1348 return visible_rndr;
1351 //todo remove when stable
1352 printf("vo_xvmc: no free surfaces, this should not happen in g1\n");
1353 for(i=0;i<number_of_surfaces;i++)
1354 printf("vo_xvmc: surface[%d].state=%d\n",i,surface_render[i].state);
1355 return NULL;
1358 static uint32_t get_image(mp_image_t *mpi){
1359 xvmc_render_state_t * rndr;
1361 rndr = find_free_surface();
1363 if(rndr == NULL){
1364 printf("vo_xvmc: get_image failed\n");
1365 return VO_FALSE;
1368 assert(rndr->start_mv_blocks_num == 0);
1369 assert(rndr->filled_mv_blocks_num == 0);
1370 assert(rndr->next_free_data_block_num == 0);
1372 mpi->flags |= MP_IMGFLAG_DIRECT;
1373 //keep strides 0 to avoid field manipulations
1374 mpi->stride[0] = 0;
1375 mpi->stride[1] = 0;
1376 mpi->stride[2] = 0;
1378 // these are shared!! so watch out
1379 // do call RenderSurface before overwriting
1380 mpi->planes[0] = (char*)data_blocks.blocks;
1381 mpi->planes[1] = (char*)mv_blocks.macro_blocks;
1382 mpi->priv =
1383 mpi->planes[2] = (char*)rndr;
1385 rndr->picture_structure = 0;
1386 rndr->flags = 0;
1387 rndr->state = 0;
1388 rndr->start_mv_blocks_num = 0;
1389 rndr->filled_mv_blocks_num = 0;
1390 rndr->next_free_data_block_num = 0;
1392 if( verbose > 3 )
1393 printf("vo_xvmc: get_image: rndr=%p (surface=%p) \n",
1394 rndr,rndr->p_surface);
1395 return VO_TRUE;
1398 static uint32_t control(uint32_t request, void *data, ... )
1400 switch (request){
1401 case VOCTRL_QUERY_FORMAT:
1402 return query_format(*((uint32_t*)data));
1403 case VOCTRL_DRAW_IMAGE:
1404 return xvmc_draw_image((mp_image_t *)data);
1405 case VOCTRL_GET_IMAGE:
1406 return get_image((mp_image_t *)data);
1407 //vo_xv
1408 case VOCTRL_GUISUPPORT:
1409 return VO_TRUE;
1410 case VOCTRL_FULLSCREEN:
1411 vo_x11_fullscreen();
1412 case VOCTRL_GET_PANSCAN:
1413 if ( !vo_config_count || !vo_fs ) return VO_FALSE;
1414 return VO_TRUE;
1415 // indended, fallthrough to update panscan on fullscreen/windowed switch
1416 case VOCTRL_SET_PANSCAN:
1417 if ( ( vo_fs && ( vo_panscan != vo_panscan_amount ) ) || ( !vo_fs && vo_panscan_amount ) )
1419 int old_y = vo_panscan_y;
1420 panscan_calc();
1422 if(old_y != vo_panscan_y)
1424 XClearWindow(mDisplay, vo_window);
1425 XFlush(mDisplay);
1428 return VO_TRUE;
1430 case VOCTRL_SET_EQUALIZER:
1432 va_list ap;
1433 int value;
1435 va_start(ap, data);
1436 value = va_arg(ap, int);
1437 va_end(ap);
1439 return(vo_xv_set_eq(xv_port, data, value));
1442 case VOCTRL_GET_EQUALIZER:
1444 va_list ap;
1445 int *value;
1447 va_start(ap, data);
1448 value = va_arg(ap, int*);
1449 va_end(ap);
1451 return(vo_xv_get_eq(xv_port, data, value));
1454 return VO_NOTIMPL;