mplayer.c: #include mplayer.h and access_mpcontext.h
[mplayer.git] / libvo / vo_xvmc.c
blob749e4f43642d3a567d20ee16c869d2344707c637
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 "osdep/timer.h"
12 #include <X11/Xlib.h>
13 #include <X11/Xutil.h>
14 #include <X11/Xatom.h>
16 #ifdef HAVE_SHM
17 #include <sys/ipc.h>
18 #include <sys/shm.h>
19 #include <X11/extensions/XShm.h>
20 #endif
22 #include <X11/extensions/Xv.h>
23 #include <X11/extensions/Xvlib.h>
24 #include <X11/extensions/XvMClib.h>
26 #include "x11_common.h"
27 #include "xvmc_render.h"
29 #include "sub.h"
30 #include "aspect.h"
32 #include "subopt-helper.h"
34 #ifdef CONFIG_GUI
35 #include "gui/interface.h"
36 #endif
38 #include "libavutil/common.h"
40 //no chance for xinerama to be supported in the near future
41 #undef CONFIG_XINERAMA
43 #undef NDEBUG
44 #include <assert.h>
47 #define UNUSED(x) ((void)(x))
49 #include "libavcodec/avcodec.h"
50 #if LIBAVCODEC_BUILD < ((51<<16)+(40<<8)+2)
51 #error You need at least libavcodecs v51.40.2
52 #endif
55 static int benchmark;
56 static int use_sleep;
57 static int first_frame;//draw colorkey on first frame
58 static int use_queue;
59 static int xv_port_request = 0;
60 static int xv_adaptor = -1;
61 static int bob_deinterlace;
62 static int top_field_first;
64 static int image_width,image_height;
65 static int image_format;
66 static uint32_t drwX,drwY;
68 #define NO_SUBPICTURE 0
69 #define OVERLAY_SUBPICTURE 1
70 #define BLEND_SUBPICTURE 2
71 #define BACKEND_SUBPICTURE 3
73 static int subpicture_mode;
74 static int subpicture_alloc;
75 static XvMCSubpicture subpicture;
76 static XvImageFormatValues subpicture_info;
77 static int subpicture_clear_color;//transparent color for the subpicture or color key for overlay
79 static XvMCSurfaceInfo surface_info;
80 static XvMCContext ctx;
81 static XvMCBlockArray data_blocks;
82 static XvMCMacroBlockArray mv_blocks;
84 #define MAX_SURFACES 8
85 static int number_of_surfaces=0;
86 static XvMCSurface surface_array[MAX_SURFACES];
87 static xvmc_render_state_t * surface_render;
89 static xvmc_render_state_t * p_render_surface_to_show=NULL;
90 static xvmc_render_state_t * p_render_surface_visible=NULL;
92 //display queue, kinda render ahead
93 static xvmc_render_state_t * show_queue[MAX_SURFACES];
94 static int free_element;
97 static void (*draw_osd_fnc)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
98 static void (*clear_osd_fnc)(int x0,int y0, int w,int h);
99 static void (*init_osd_fnc)(void);
101 static void draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
102 static void draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
103 static void clear_osd_subpic(int x0,int y0, int w,int h);
104 static void init_osd_yuv_pal(void);
107 static const struct{
108 int id;//id as xvimages or as mplayer RGB|{8,15,16,24,32}
109 void (* init_func_ptr)();
110 void (* draw_func_ptr)();
111 void (* clear_func_ptr)();
112 } osd_render[]={
113 {0x34344149,init_osd_yuv_pal,draw_osd_AI44,clear_osd_subpic},
114 {0x34344941,init_osd_yuv_pal,draw_osd_IA44,clear_osd_subpic},
115 {0,NULL,NULL,NULL}
118 static void xvmc_free(void);
119 static void xvmc_clean_surfaces(void);
120 static int count_free_surfaces();
121 static xvmc_render_state_t * find_free_surface();
123 static const vo_info_t info = {
124 "XVideo Motion Compensation",
125 "xvmc",
126 "Ivan Kalvachev <iive@users.sf.net>",
130 const LIBVO_EXTERN(xvmc);
132 //shm stuff from vo_xv
133 #ifdef HAVE_SHM
134 /* since it doesn't seem to be defined on some platforms */
135 int XShmGetEventBase(Display*);
136 static XShmSegmentInfo Shminfo;
137 static int Shmem_Flag;
138 #endif
139 XvImage * xvimage;
142 static void allocate_xvimage(int xvimage_width,int xvimage_height,int xv_format)
145 * allocate XvImages. FIXME: no error checking, without
146 * mit-shm this will bomb... trzing to fix ::atmos
148 #ifdef HAVE_SHM
149 if ( mLocalDisplay && XShmQueryExtension( mDisplay ) ) Shmem_Flag = 1;
150 else
152 Shmem_Flag = 0;
153 mp_msg(MSGT_VO,MSGL_INFO, "Shared memory not supported\nReverting to normal Xv\n" );
155 if ( Shmem_Flag )
157 xvimage = (XvImage *) XvShmCreateImage(mDisplay, xv_port, xv_format,
158 NULL, xvimage_width, xvimage_height, &Shminfo);
160 Shminfo.shmid = shmget(IPC_PRIVATE, xvimage->data_size, IPC_CREAT | 0777);
161 Shminfo.shmaddr = (char *) shmat(Shminfo.shmid, 0, 0);
162 Shminfo.readOnly = False;
164 xvimage->data = Shminfo.shmaddr;
165 XShmAttach(mDisplay, &Shminfo);
166 XSync(mDisplay, False);
167 shmctl(Shminfo.shmid, IPC_RMID, 0);
169 else
170 #endif
172 xvimage = (XvImage *) XvCreateImage(mDisplay, xv_port, xv_format, NULL, xvimage_width, xvimage_height);
173 xvimage->data = malloc(xvimage->data_size);
174 XSync(mDisplay,False);
176 // memset(xvimage->data,128,xvimage->data_size);
177 return;
180 static void deallocate_xvimage()
182 #ifdef HAVE_SHM
183 if ( Shmem_Flag )
185 XShmDetach( mDisplay,&Shminfo );
186 shmdt( Shminfo.shmaddr );
188 else
189 #endif
191 free(xvimage->data);
193 XFree(xvimage);
195 XSync(mDisplay, False);
196 return;
198 //end of vo_xv shm/xvimage code
200 static int xvmc_check_surface_format(uint32_t format, XvMCSurfaceInfo * surf_info){
201 if ( format == IMGFMT_XVMC_IDCT_MPEG2 ){
202 if( surf_info->mc_type != (XVMC_IDCT|XVMC_MPEG_2) ) return -1;
203 if( surf_info->chroma_format != XVMC_CHROMA_FORMAT_420 ) return -1;
204 return 0;
206 if ( format == IMGFMT_XVMC_MOCO_MPEG2 ){
207 if(surf_info->mc_type != XVMC_MPEG_2) return -1;
208 if(surf_info->chroma_format != XVMC_CHROMA_FORMAT_420) return -1;
209 return 0;
211 return -1;//fail
214 //print all info needed to add new format
215 static void print_xvimage_format_values(XvImageFormatValues *xifv){
216 int i;
217 printf("Format_ID = 0x%X\n",xifv->id);
219 printf(" type = ");
220 if(xifv->type == XvRGB) printf("RGB\n");
221 else if(xifv->type == XvYUV) printf("YUV\n");
222 else printf("Unknown\n");
224 printf(" byte_order = ");
225 if(xifv->byte_order == LSBFirst) printf("LSB First\n");
226 else if(xifv->type == MSBFirst) printf("MSB First\n");
227 else printf("Unknown\n");//yes Linux support other types too
229 printf(" guid = ");
230 for(i=0;i<16;i++)
231 printf("%02X ",(unsigned char)xifv->guid[i]);
232 printf("\n");
234 printf(" bits_per_pixel = %d\n",xifv->bits_per_pixel);
236 printf(" format = ");
237 if(xifv->format == XvPacked) printf("XvPacked\n");
238 else if(xifv->format == XvPlanar) printf("XvPlanar\n");
239 else printf("Unknown\n");
241 printf(" num_planes = %d\n",xifv->num_planes);
243 if(xifv->type == XvRGB){
244 printf(" red_mask = %0X\n", xifv->red_mask);
245 printf(" green_mask = %0X\n",xifv->green_mask);
246 printf(" blue_mask = %0X\n", xifv->blue_mask);
248 if(xifv->type == XvYUV){
249 printf(" y_sample_bits = %d\n u_sample_bits = %d\n v_sample_bits = %d\n",
250 xifv->y_sample_bits,xifv->u_sample_bits,xifv->v_sample_bits);
251 printf(" horz_y_period = %d\n horz_u_period = %d\n horz_v_period = %d\n",
252 xifv->horz_y_period,xifv->horz_u_period,xifv->horz_v_period);
253 printf(" vert_y_period = %d\n vert_u_period = %d\n vert_v_period = %d\n",
254 xifv->vert_y_period,xifv->vert_u_period,xifv->vert_v_period);
256 printf(" component_order = ");
257 for(i=0;i<32;i++)
258 if(xifv->component_order[i]>=32)
259 printf("%c",xifv->component_order[i]);
260 printf("\n");
262 printf(" scanline = ");
263 if(xifv->scanline_order == XvTopToBottom) printf("XvTopToBottom\n");
264 else if(xifv->scanline_order == XvBottomToTop) printf("XvBottomToTop\n");
265 else printf("Unknown\n");
267 printf("\n");
270 // WARNING This function may changes xv_port and surface_info!
271 static int xvmc_find_surface_by_format(int format,int width,int height,
272 XvMCSurfaceInfo * surf_info,int query){
273 int rez;
274 XvAdaptorInfo * ai;
275 int num_adaptors,i;
276 unsigned long p;
277 int s,mc_surf_num;
278 XvMCSurfaceInfo * mc_surf_list;
280 rez = XvQueryAdaptors(mDisplay,DefaultRootWindow(mDisplay),&num_adaptors,&ai);
281 if( rez != Success ) return -1;
282 if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
283 printf("vo_xvmc: Querying %d adaptors\n",num_adaptors); }
284 for(i=0; i<num_adaptors; i++)
286 /* check if adaptor number has been specified */
287 if (xv_adaptor != -1 && xv_adaptor != i)
288 continue;
289 if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
290 printf("vo_xvmc: Quering adaptor #%d\n",i); }
291 if( ai[i].type == 0 ) continue;// we need at least dummy type!
292 //probing ports
293 for(p=ai[i].base_id; p<ai[i].base_id+ai[i].num_ports; p++)
295 if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
296 printf("vo_xvmc: probing port #%ld\n",p); }
297 mc_surf_list = XvMCListSurfaceTypes(mDisplay,p,&mc_surf_num);
298 if( mc_surf_list == NULL || mc_surf_num == 0){
299 if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
300 printf("vo_xvmc: No XvMC supported. \n"); }
301 continue;
303 if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
304 printf("vo_xvmc: XvMC list have %d surfaces\n",mc_surf_num); }
305 //we have XvMC list!
306 for(s=0; s<mc_surf_num; s++)
308 if( width > mc_surf_list[s].max_width ) continue;
309 if( height > mc_surf_list[s].max_height ) continue;
310 if( xvmc_check_surface_format(format,&mc_surf_list[s])<0 ) continue;
311 //we have match!
312 /* respect the users wish */
313 if ( xv_port_request != 0 && xv_port_request != p )
315 continue;
318 if(!query){
319 rez = XvGrabPort(mDisplay,p,CurrentTime);
320 if(rez != Success){
321 if ( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
322 printf("vo_xvmc: Fail to grab port %ld\n",p); }
323 continue;
325 printf("vo_xvmc: Using Xv Adaptor #%d (%s)\n", i, ai[i].name);
326 printf("vo_xvmc: Port %ld grabed\n",p);
327 xv_port = p;
329 goto surface_found;
330 }//for mc surf
331 XFree(mc_surf_list);//if mc_surf_num==0 is list==NULL ?
332 }//for ports
333 }//for adaptors
334 XvFreeAdaptorInfo(ai);
336 if(!query) printf("vo_xvmc: Could not find free matching surface. Sorry.\n");
337 return 0;
339 // somebody know cleaner way to escape from 3 internal loops?
340 surface_found:
341 XvFreeAdaptorInfo(ai);
343 memcpy(surf_info,&mc_surf_list[s],sizeof(XvMCSurfaceInfo));
344 if( mp_msg_test(MSGT_VO,MSGL_DBG3) || !query)
345 printf("vo_xvmc: Found matching surface with id=%X on %ld port at %d adapter\n",
346 mc_surf_list[s].surface_type_id,p,i);
347 return mc_surf_list[s].surface_type_id;
350 static uint32_t xvmc_draw_image(mp_image_t *mpi){
351 xvmc_render_state_t * rndr;
353 assert(mpi!=NULL);
354 assert(mpi->flags &MP_IMGFLAG_DIRECT);
355 // assert(mpi->flags &MP_IMGFLAGS_DRAWBACK);
357 rndr = (xvmc_render_state_t*)mpi->priv;//there is copy in plane[2]
358 assert( rndr != NULL );
359 assert( rndr->magic == MP_XVMC_RENDER_MAGIC );
360 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
361 printf("vo_xvmc: draw_image(show rndr=%p)\n",rndr);
362 // the surface have passed vf system without been skiped, it will be displayed
363 rndr->state |= MP_XVMC_STATE_DISPLAY_PENDING;
364 p_render_surface_to_show = rndr;
365 top_field_first = mpi->fields & MP_IMGFIELD_TOP_FIRST;
366 return VO_TRUE;
369 static int preinit(const char *arg){
370 int xv_version,xv_release,xv_request_base,xv_event_base,xv_error_base;
371 int mc_eventBase,mc_errorBase;
372 int mc_ver,mc_rev;
373 strarg_t ck_src_arg = { 0, NULL };
374 strarg_t ck_method_arg = { 0, NULL };
375 opt_t subopts [] =
377 /* name arg type arg var test */
378 { "port", OPT_ARG_INT, &xv_port_request, (opt_test_f)int_pos },
379 { "adaptor", OPT_ARG_INT, &xv_adaptor, (opt_test_f)int_non_neg },
380 { "ck", OPT_ARG_STR, &ck_src_arg, xv_test_ck },
381 { "ck-method", OPT_ARG_STR, &ck_method_arg, xv_test_ckm },
382 { "benchmark", OPT_ARG_BOOL, &benchmark, NULL },
383 { "sleep", OPT_ARG_BOOL, &use_sleep, NULL },
384 { "queue", OPT_ARG_BOOL, &use_queue, NULL },
385 { "bobdeint", OPT_ARG_BOOL, &bob_deinterlace, NULL },
386 { NULL }
389 //Obtain display handler
390 if (!vo_init()) return -1;//vo_xv
392 //XvMC is subdivision of XVideo
393 if (Success != XvQueryExtension(mDisplay,&xv_version,&xv_release,&xv_request_base,
394 &xv_event_base,&xv_error_base) ){
395 mp_msg(MSGT_VO,MSGL_ERR,"Sorry, Xv(MC) not supported by this X11 version/driver\n");
396 mp_msg(MSGT_VO,MSGL_ERR,"********** Try with -vo x11 or -vo sdl ***********\n");
397 return -1;
399 printf("vo_xvmc: X-Video extension %d.%d\n",xv_version,xv_release);
401 if( True != XvMCQueryExtension(mDisplay,&mc_eventBase,&mc_errorBase) ){
402 printf("vo_xvmc: No X-Video MotionCompensation Extension on %s\n",
403 XDisplayName(NULL));
404 return -1;
407 if(Success == XvMCQueryVersion(mDisplay, &mc_ver, &mc_rev) ){
408 printf("vo_xvmc: X-Video MotionCompensation Extension version %i.%i\n",
409 mc_ver,mc_rev);
411 else{
412 printf("vo_xvmc: Error querying version info!\n");
413 return -1;
415 surface_render = NULL;
416 xv_port = 0;
417 number_of_surfaces = 0;
418 subpicture_alloc = 0;
420 benchmark = 0; //disable PutImageto allow faster display than screen refresh
421 use_sleep = 0;
422 use_queue = 0;
423 bob_deinterlace = 0;
425 /* parse suboptions */
426 if ( subopt_parse( arg, subopts ) != 0 )
428 return -1;
431 xv_setup_colorkeyhandling( ck_method_arg.str, ck_src_arg.str );
433 return 0;
436 static void calc_drwXY(uint32_t *drwX, uint32_t *drwY) {
437 *drwX = *drwY = 0;
438 if (vo_fs) {
439 aspect(&vo_dwidth, &vo_dheight, A_ZOOM);
440 vo_dwidth = FFMIN(vo_dwidth, vo_screenwidth);
441 vo_dheight = FFMIN(vo_dheight, vo_screenheight);
442 *drwX = (vo_screenwidth - vo_dwidth) / 2;
443 *drwY = (vo_screenheight - vo_dheight) / 2;
444 mp_msg(MSGT_VO, MSGL_V, "[xvmc-fs] dx: %d dy: %d dw: %d dh: %d\n",
445 *drwX, *drwY, vo_dwidth, vo_dheight);
446 } else if (WinID == 0) {
447 *drwX = vo_dx;
448 *drwY = vo_dy;
452 static int config(uint32_t width, uint32_t height,
453 uint32_t d_width, uint32_t d_height,
454 uint32_t flags, char *title, uint32_t format){
455 int i,mode_id,rez;
456 int numblocks,blocks_per_macroblock;//bpmb we have 6,8,12
458 //from vo_xv
459 XSizeHints hint;
460 XVisualInfo vinfo;
461 XGCValues xgcv;
462 XSetWindowAttributes xswa;
463 XWindowAttributes attribs;
464 unsigned long xswamask;
465 int depth;
466 #ifdef CONFIG_XF86VM
467 int vm=0;
468 unsigned int modeline_width, modeline_height;
469 static uint32_t vm_width;
470 static uint32_t vm_height;
471 #endif
472 //end of vo_xv
474 if( !IMGFMT_IS_XVMC(format) )
476 assert(0);//should never happen, abort on debug or
477 return 1;//return error on relese
480 // Find free port that supports MC, by querying adaptors
481 if( xv_port != 0 || number_of_surfaces != 0 ){
482 if( height==image_height && width==image_width && image_format==format){
483 xvmc_clean_surfaces();
484 goto skip_surface_allocation;
486 xvmc_free();
488 numblocks=((width+15)/16)*((height+15)/16);
489 // Find Supported Surface Type
490 mode_id = xvmc_find_surface_by_format(format,width,height,&surface_info,0);//false=1 to grab port, not query
491 if ( mode_id == 0 )
493 return -1;
496 rez = XvMCCreateContext(mDisplay, xv_port,mode_id,width,height,XVMC_DIRECT,&ctx);
497 if( rez != Success ){
498 printf("vo_xvmc: XvMCCreateContext failed with error %d\n",rez);
499 return -1;
501 if( ctx.flags & XVMC_DIRECT ){
502 printf("vo_xvmc: Allocated Direct Context\n");
503 }else{
504 printf("vo_xvmc: Allocated Indirect Context!\n");
508 blocks_per_macroblock = 6;
509 if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_422)
510 blocks_per_macroblock = 8;
511 if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_444)
512 blocks_per_macroblock = 12;
514 rez = XvMCCreateBlocks(mDisplay,&ctx,numblocks*blocks_per_macroblock,&data_blocks);
515 if( rez != Success ){
516 XvMCDestroyContext(mDisplay,&ctx);
517 return -1;
519 printf("vo_xvmc: data_blocks allocated\n");
521 rez = XvMCCreateMacroBlocks(mDisplay,&ctx,numblocks,&mv_blocks);
522 if( rez != Success ){
523 XvMCDestroyBlocks(mDisplay,&data_blocks);
524 XvMCDestroyContext(mDisplay,&ctx);
525 return -1;
527 printf("vo_xvmc: mv_blocks allocated\n");
529 if(surface_render==NULL)
530 surface_render=malloc(MAX_SURFACES*sizeof(xvmc_render_state_t));//easy mem debug
531 memset(surface_render,0,MAX_SURFACES*sizeof(xvmc_render_state_t));
533 for(i=0; i<MAX_SURFACES; i++){
534 rez=XvMCCreateSurface(mDisplay,&ctx,&surface_array[i]);
535 if( rez != Success )
536 break;
537 surface_render[i].magic = MP_XVMC_RENDER_MAGIC;
538 surface_render[i].data_blocks = data_blocks.blocks;
539 surface_render[i].mv_blocks = mv_blocks.macro_blocks;
540 surface_render[i].total_number_of_mv_blocks = numblocks;
541 surface_render[i].total_number_of_data_blocks = numblocks*blocks_per_macroblock;;
542 surface_render[i].mc_type = surface_info.mc_type & (~XVMC_IDCT);
543 surface_render[i].idct = (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT;
544 surface_render[i].chroma_format = surface_info.chroma_format;
545 surface_render[i].unsigned_intra = (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED;
546 surface_render[i].p_surface = &surface_array[i];
547 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
548 printf("vo_xvmc: surface[%d] = %p .rndr=%p\n",i,&surface_array[i], &surface_render[i]);
550 number_of_surfaces = i;
551 if( number_of_surfaces < 4 ){// +2 I or P and +2 for B (to avoid visible motion drawing)
552 printf("vo_xvmc: Unable to allocate at least 4 Surfaces\n");
553 uninit();
554 return -1;
556 printf("vo_xvmc: Motion Compensation context allocated - %d surfaces\n",
557 number_of_surfaces);
559 //debug
560 printf("vo_xvmc: idct=%d unsigned_intra=%d\n",
561 (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT,
562 (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED);
564 // Find way to display OSD & subtitle
565 printf("vo_xvmc: looking for OSD support\n");
566 subpicture_mode = NO_SUBPICTURE;
567 if(surface_info.flags & XVMC_OVERLAID_SURFACE)
568 subpicture_mode = OVERLAY_SUBPICTURE;
570 if(surface_info.subpicture_max_width != 0 &&
571 surface_info.subpicture_max_height != 0 ){
572 int s,k,num_subpic;
574 XvImageFormatValues * xvfmv;
575 xvfmv = XvMCListSubpictureTypes(mDisplay, xv_port,
576 surface_info.surface_type_id, &num_subpic);
578 if(num_subpic != 0 && xvfmv != NULL){
579 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ){//Print all subpicture types for debug
580 for(s=0;s<num_subpic;s++)
581 print_xvimage_format_values(&xvfmv[s]);
584 for(s=0;s<num_subpic;s++){
585 for(k=0;osd_render[k].draw_func_ptr!=NULL;k++){
586 if(xvfmv[s].id == osd_render[k].id)
588 init_osd_fnc = osd_render[k].init_func_ptr;
589 draw_osd_fnc = osd_render[k].draw_func_ptr;
590 clear_osd_fnc = osd_render[k].clear_func_ptr;
592 subpicture_mode = BLEND_SUBPICTURE;
593 subpicture_info = xvfmv[s];
594 printf(" Subpicture id 0x%08X\n",subpicture_info.id);
595 goto found_subpic;
599 found_subpic:
600 XFree(xvfmv);
602 //Blend2 supicture is always possible, blend1 only at backend
603 if( (subpicture_mode == BLEND_SUBPICTURE) &&
604 (surface_info.flags & XVMC_BACKEND_SUBPICTURE) )
606 subpicture_mode = BACKEND_SUBPICTURE;
611 switch(subpicture_mode){
612 case NO_SUBPICTURE:
613 printf("vo_xvmc: No OSD support for this mode\n");
614 break;
615 case OVERLAY_SUBPICTURE:
616 printf("vo_xvmc: OSD support via color key tricks\n");
617 printf("vo_xvmc: not yet implemented:(\n");
618 break;
619 case BLEND_SUBPICTURE:
620 printf("vo_xvmc: OSD support by additional frontend rendering\n");
621 break;
622 case BACKEND_SUBPICTURE:
623 printf("vo_xvmc: OSD support by backend rendering (fast)\n");
624 printf("vo_xvmc: Please send feedback to confirm that it works,otherwise send bugreport!\n");
625 break;
628 //take keycolor value and choose method for handling it
629 if ( !vo_xv_init_colorkey() )
631 return -1; // bail out, colorkey setup failed
634 vo_xv_enable_vsync();//it won't break anything
636 //taken from vo_xv
637 image_height = height;
638 image_width = width;
640 skip_surface_allocation:
642 vo_mouse_autohide = 1;
644 #ifdef CONFIG_XF86VM
645 if( flags&VOFLAG_MODESWITCHING ) vm = 1;
646 #endif
648 #ifdef CONFIG_GUI
649 if(use_gui)
650 guiGetEvent( guiSetShVideo,0 ); // let the GUI to setup/resize our window
651 else
652 #endif
654 hint.x = vo_dx;
655 hint.y = vo_dy;
656 hint.width = d_width;
657 hint.height = d_height;
658 #ifdef CONFIG_XF86VM
659 if ( vm )
661 if ((d_width==0) && (d_height==0))
662 { vm_width=image_width; vm_height=image_height; }
663 else
664 { vm_width=d_width; vm_height=d_height; }
665 vo_vm_switch(vm_width, vm_height,&modeline_width, &modeline_height);
666 hint.x=(vo_screenwidth-modeline_width)/2;
667 hint.y=(vo_screenheight-modeline_height)/2;
668 hint.width=modeline_width;
669 hint.height=modeline_height;
670 aspect_save_screenres(modeline_width,modeline_height);
672 else
673 #endif
674 hint.flags = PPosition | PSize /* | PBaseSize */;
675 hint.base_width = hint.width; hint.base_height = hint.height;
676 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
677 depth=attribs.depth;
678 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) depth = 24;
679 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
681 xswa.background_pixel = 0;
682 if (xv_ck_info.method == CK_METHOD_BACKGROUND)
683 xswa.background_pixel = xv_colorkey;
684 xswa.border_pixel = 0;
685 xswamask = CWBackPixel | CWBorderPixel;
687 if ( WinID>=0 ){
688 vo_window = WinID ? ((Window)WinID) : mRootWin;
689 if ( WinID )
691 Window mRoot;
692 uint32_t drwBorderWidth, drwDepth;
693 XUnmapWindow( mDisplay,vo_window );
694 XChangeWindowAttributes( mDisplay,vo_window,xswamask,&xswa );
695 vo_x11_selectinput_witherr( mDisplay,vo_window,StructureNotifyMask | KeyPressMask | PropertyChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask );
696 XMapWindow( mDisplay,vo_window );
697 XGetGeometry(mDisplay, vo_window, &mRoot,
698 &drwX, &drwY, &vo_dwidth, &vo_dheight,
699 &drwBorderWidth, &drwDepth);
700 aspect_save_prescale(vo_dwidth, vo_dheight);
702 } else
703 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height, flags,
704 CopyFromParent, "xvmc", title);
705 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
707 if ( vo_gc != None ) XFreeGC( mDisplay,vo_gc );
708 vo_gc = XCreateGC(mDisplay, vo_window, GCForeground, &xgcv);
709 XSync(mDisplay, False);
710 #ifdef CONFIG_XF86VM
711 if ( vm )
713 /* Grab the mouse pointer in our window */
714 if(vo_grabpointer)
715 XGrabPointer(mDisplay, vo_window, True, 0,
716 GrabModeAsync, GrabModeAsync,
717 vo_window, None, CurrentTime );
718 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
720 #endif
723 aspect(&vo_dwidth, &vo_dheight, A_NOZOOM);
724 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0) vo_fs = 1;
725 calc_drwXY(&drwX, &drwY);
727 panscan_calc();
729 mp_msg(MSGT_VO,MSGL_V, "[xvmc] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,vo_dwidth,vo_dheight );
731 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
733 //end vo_xv
735 /* store image dimesions for displaying */
736 p_render_surface_visible = NULL;
737 p_render_surface_to_show = NULL;
739 free_element = 0;
740 first_frame = 1;
742 vo_directrendering = 1;//ugly hack, coz xvmc works only with direct rendering
743 image_format=format;
744 return 0;
747 static int draw_frame(uint8_t *srcp[]){
748 UNUSED(srcp);
749 assert(0);
752 static void init_osd_yuv_pal(){
753 char * palette;
754 int rez;
755 int i,j;
756 int snum,seb;
757 int Y,U,V;
759 subpicture_clear_color = 0;
761 if(subpicture.num_palette_entries > 0){
763 snum = subpicture.num_palette_entries;
764 seb = subpicture.entry_bytes;
765 palette = malloc(snum*seb);//check fail
766 if(palette == NULL) return;
767 for(i=0; i<snum; i++){
768 // 0-black max-white the other are gradients
769 Y = i*(1 << subpicture_info.y_sample_bits)/snum;//snum=2;->(0),(1*(1<<1)/2)
770 U = 1 << (subpicture_info.u_sample_bits - 1);
771 V = 1 << (subpicture_info.v_sample_bits - 1);
772 for(j=0; j<seb; j++)
773 switch(subpicture.component_order[j]){
774 case 'U': palette[i*seb+j] = U; break;
775 case 'V': palette[i*seb+j] = V; break;
776 case 'Y':
777 default:
778 palette[i*seb+j] = Y; break;
781 rez = XvMCSetSubpicturePalette(mDisplay, &subpicture, palette);
782 if(rez!=Success){
783 printf("vo_xvmc: Setting palette failed.\n");
785 free(palette);
789 static void clear_osd_subpic(int x0, int y0, int w, int h){
790 int rez;
791 rez=XvMCClearSubpicture(mDisplay, &subpicture,
792 x0, y0, w,h,
793 subpicture_clear_color);
794 if(rez != Success)
795 printf("vo_xvmc: XvMCClearSubpicture failed!\n");
798 static void OSD_init(){
799 unsigned short osd_height, osd_width;
800 int rez;
802 if(subpicture_alloc){
803 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
804 printf("vo_xvmc: destroying subpicture\n");
805 XvMCDestroySubpicture(mDisplay,&subpicture);
806 deallocate_xvimage();
807 subpicture_alloc = 0;
810 /* if(surface_info.flags & XVMC_SUBPICTURE_INDEPENDENT_SCALING){
811 osd_width = vo_dwidth;
812 osd_height = vo_dheight;
813 }else*/
815 osd_width = image_width;
816 osd_height = image_height;
819 if(osd_width > surface_info.subpicture_max_width)
820 osd_width = surface_info.subpicture_max_width;
821 if(osd_height > surface_info.subpicture_max_height)
822 osd_height = surface_info.subpicture_max_height;
823 if(osd_width == 0 || osd_height == 0)
824 return;//if called before window size is known
826 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
827 printf("vo_xvmc: creating subpicture (%d,%d) format %X\n",
828 osd_width,osd_height,subpicture_info.id);
830 rez = XvMCCreateSubpicture(mDisplay,&ctx,&subpicture,
831 osd_width,osd_height,subpicture_info.id);
832 if(rez != Success){
833 subpicture_mode = NO_SUBPICTURE;
834 printf("vo_xvmc: Create Subpicture failed, OSD disabled\n");
835 return;
837 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ){
838 int i;
839 printf("vo_xvmc: Created Subpicture:\n");
840 printf(" xvimage_id=0x%X\n",subpicture.xvimage_id);
841 printf(" width=%d\n",subpicture.width);
842 printf(" height=%d\n",subpicture.height);
843 printf(" num_palette_entries=0x%X\n",subpicture.num_palette_entries);
844 printf(" entry_bytes=0x%X\n",subpicture.entry_bytes);
846 printf(" component_order=\"");
847 for(i=0; i<4; i++)
848 if(subpicture.component_order[i] >= 32)
849 printf("%c", subpicture.component_order[i]);
850 printf("\"\n");
853 //call init for the surface type
854 init_osd_fnc();//init palete,clear color etc ...
855 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
856 printf("vo_xvmc: clearing subpicture\n");
857 clear_osd_fnc(0, 0, subpicture.width, subpicture.height);
859 allocate_xvimage(subpicture.width, subpicture.height, subpicture_info.id);
860 subpicture_alloc = 1;
863 static void draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
864 int ox,oy;
865 int rez;
867 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
868 printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);
870 for(ox=0; ox<w; ox++){
871 for(oy=0; oy<h; oy++){
872 xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]>>4) | ((0-srca[oy*stride+ox])&0xf0);
875 rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
876 w,h,x0,y0);
877 if(rez != Success){
878 printf("vo_xvmc: composite subpicture failed\n");
879 assert(0);
883 static void draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
884 int ox,oy;
885 int rez;
886 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
887 printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);
889 for(ox=0; ox<w; ox++){
890 for(oy=0; oy<h; oy++){
891 xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]&0xf0) | (((0-srca[oy*stride+ox])>>4)&0xf);
894 rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
895 w,h,x0,y0);
896 if(rez != Success){
897 printf("vo_xvmc: composite subpicture failed\n");
898 assert(0);
902 static void draw_osd(void){
903 xvmc_render_state_t * osd_rndr;
904 int osd_has_changed;
905 int have_osd_to_draw;
906 int rez;
908 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
909 printf("vo_xvmc: draw_osd ,OSD_mode=%d, surface_to_show=%p\n",
910 subpicture_mode,p_render_surface_to_show);
912 if(subpicture_mode == BLEND_SUBPICTURE ||
913 subpicture_mode == BACKEND_SUBPICTURE ){
915 if(!subpicture_alloc) //allocate subpicture when dimensions are known
916 OSD_init();
917 if(!subpicture_alloc)
918 return;//dimensions still unknown.
920 osd_has_changed = vo_update_osd(subpicture.width, subpicture.height);
921 have_osd_to_draw = vo_osd_check_range_update(0, 0, subpicture.width,
922 subpicture.height);
924 if(!have_osd_to_draw)
925 return;//nothing to draw,no subpic, no blend
927 if(osd_has_changed){
928 //vo_remove_text(subpicture.width, subpicture.height,clear_osd_fnc)
929 clear_osd_fnc(0,0,subpicture.width,subpicture.height);
930 vo_draw_text(subpicture.width, subpicture.height, draw_osd_fnc);
932 XvMCSyncSubpicture(mDisplay,&subpicture);//todo usleeep wait!
934 if(subpicture_mode == BLEND_SUBPICTURE){
935 osd_rndr = find_free_surface();
936 if(osd_rndr == NULL)
937 return;// no free surface to draw OSD in
939 rez = XvMCBlendSubpicture2(mDisplay,
940 p_render_surface_to_show->p_surface, osd_rndr->p_surface,
941 &subpicture,
942 0, 0, subpicture.width, subpicture.height,
943 0, 0, image_width, image_height);
944 if(rez!=Success){
945 printf("vo_xvmc: BlendSubpicture failed rez=%d\n",rez);
946 assert(0);
947 return;
949 // XvMCFlushSurface(mDisplay,osd_rndr->p_surface);//fixme- should I?
951 //When replaceing the surface with osd one, save the flags too!
952 osd_rndr->picture_structure = p_render_surface_to_show->picture_structure;
953 osd_rndr->display_flags = p_render_surface_to_show->display_flags;
954 //add more if needed osd_rndr-> = p_render_surface_to_show->;
956 p_render_surface_to_show->state &= ~MP_XVMC_STATE_DISPLAY_PENDING;
957 p_render_surface_to_show->state |= MP_XVMC_STATE_OSD_SOURCE;
958 p_render_surface_to_show->p_osd_target_surface_render = osd_rndr;
960 p_render_surface_to_show = osd_rndr;
961 p_render_surface_to_show->state = MP_XVMC_STATE_DISPLAY_PENDING;
963 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
964 printf("vo_xvmc:draw_osd: surface_to_show changed to %p\n",osd_rndr);
965 }//endof if(BLEND)
966 if(subpicture_mode == BACKEND_SUBPICTURE){
967 rez = XvMCBlendSubpicture(mDisplay,
968 p_render_surface_to_show->p_surface,
969 &subpicture,
970 0, 0, subpicture.width, subpicture.height,
971 0, 0, image_width, image_height);
975 }//if(BLEND||BACKEND)
978 static void xvmc_sync_surface(XvMCSurface * srf){
979 int status,rez;
980 rez = XvMCGetSurfaceStatus(mDisplay,srf,&status);
981 assert(rez==Success);
982 if((status & XVMC_RENDERING) == 0)
983 return;//surface is already complete
984 if(use_sleep){
985 rez = XvMCFlushSurface(mDisplay, srf);
986 assert(rez==Success);
989 usec_sleep(1000);//1ms (may be 20ms on linux)
990 XvMCGetSurfaceStatus(mDisplay,srf,&status);
991 } while (status & XVMC_RENDERING);
992 return;//done
995 XvMCSyncSurface(mDisplay, srf);
998 static void put_xvmc_image(xvmc_render_state_t * p_render_surface, int draw_ck){
999 int rez;
1000 int clipX,clipY,clipW,clipH;
1001 int i;
1003 if(p_render_surface == NULL)
1004 return;
1006 clipX = drwX-(vo_panscan_x>>1);
1007 clipY = drwY-(vo_panscan_y>>1);
1008 clipW = vo_dwidth+vo_panscan_x;
1009 clipH = vo_dheight+vo_panscan_y;
1011 if(draw_ck)
1012 vo_xv_draw_colorkey(clipX,clipY,clipW,clipH);
1014 if(benchmark)
1015 return;
1017 for (i = 1; i <= bob_deinterlace + 1; i++) {
1018 int field = top_field_first ? i : i ^ 3;
1019 rez = XvMCPutSurface(mDisplay, p_render_surface->p_surface,
1020 vo_window,
1021 0, 0, image_width, image_height,
1022 clipX, clipY, clipW, clipH,
1023 bob_deinterlace ? field : 3);
1024 //p_render_surface_to_show->display_flags);
1025 if(rez != Success){
1026 printf("vo_xvmc: PutSurface failer, critical error %d!\n",rez);
1027 assert(0);
1030 XFlush(mDisplay);
1033 static void flip_page(void){
1034 int i,cfs;
1037 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1038 printf("vo_xvmc: flip_page show(rndr=%p)\n\n",p_render_surface_to_show);
1040 if(p_render_surface_to_show == NULL) return;
1041 assert( p_render_surface_to_show->magic == MP_XVMC_RENDER_MAGIC );
1042 //fixme assert( p_render_surface_to_show != p_render_surface_visible);
1044 if(use_queue){
1045 // fill the queue until only n free surfaces remain
1046 // after that start displaying
1047 cfs = count_free_surfaces();
1048 show_queue[free_element++] = p_render_surface_to_show;
1049 if(cfs > 3){//well have 3 free surfaces after add queue
1050 if(free_element > 1)//a little voodoo magic
1051 xvmc_sync_surface(show_queue[0]->p_surface);
1052 return;
1054 p_render_surface_to_show=show_queue[0];
1055 if( mp_msg_test(MSGT_VO,MSGL_DBG5) )
1056 printf("vo_xvmc: flip_queue free_element=%d\n",free_element);
1057 free_element--;
1058 for(i=0; i<free_element; i++){
1059 show_queue[i] = show_queue[i+1];
1061 show_queue[free_element] = NULL;
1064 // make sure the rendering is done
1065 xvmc_sync_surface(p_render_surface_to_show->p_surface);
1067 //the visible surface won't be displayed anymore, mark it as free
1068 if(p_render_surface_visible != NULL)
1069 p_render_surface_visible->state &= ~MP_XVMC_STATE_DISPLAY_PENDING;
1071 //!!fixme assert(p_render_surface_to_show->state & MP_XVMC_STATE_DISPLAY_PENDING);
1073 //show it, displaying is always vsynced, so skip it for benchmark
1074 put_xvmc_image(p_render_surface_to_show,first_frame);
1075 first_frame=0;//make sure we won't draw it anymore
1077 p_render_surface_visible = p_render_surface_to_show;
1078 p_render_surface_to_show = NULL;
1081 static void check_events(void){
1082 Window mRoot;
1083 uint32_t drwBorderWidth,drwDepth;
1085 int e=vo_x11_check_events(mDisplay);
1086 if(e&VO_EVENT_RESIZE)
1088 e |= VO_EVENT_EXPOSE;
1090 XGetGeometry( mDisplay,vo_window,&mRoot,&drwX,&drwY,&vo_dwidth,&vo_dheight,
1091 &drwBorderWidth,&drwDepth );
1092 mp_msg(MSGT_VO,MSGL_V, "[xvmc] dx: %d dy: %d dw: %d dh: %d\n",drwX,drwY,
1093 vo_dwidth,vo_dheight );
1095 calc_drwXY(&drwX, &drwY);
1097 if ( e & VO_EVENT_EXPOSE )
1099 put_xvmc_image(p_render_surface_visible,1);
1103 static void xvmc_free(void){
1104 int i;
1105 if( subpicture_alloc ){
1107 XvMCDestroySubpicture(mDisplay,&subpicture);
1108 deallocate_xvimage();
1110 subpicture_alloc = 0;
1112 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1113 printf("vo_xvmc: subpicture destroyed\n");
1116 if( number_of_surfaces ){
1118 XvMCDestroyMacroBlocks(mDisplay,&mv_blocks);
1119 XvMCDestroyBlocks(mDisplay,&data_blocks);
1121 for(i=0; i<number_of_surfaces; i++)
1123 XvMCHideSurface(mDisplay,&surface_array[i]);//it doesn't hurt, I hope
1124 XvMCDestroySurface(mDisplay,&surface_array[i]);
1126 if( (surface_render[i].state != 0) &&
1127 (p_render_surface_visible != &surface_render[i]) )
1128 printf("vo_xvmc::uninit surface_render[%d].status=%d\n",i,
1129 surface_render[i].state);
1132 memset(surface_render,0,MAX_SURFACES*sizeof(xvmc_render_state_t));//for debuging
1133 free(surface_render);surface_render=NULL;
1135 XvMCDestroyContext(mDisplay,&ctx);
1136 number_of_surfaces = 0;
1138 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
1139 printf("vo_xvmc: Context sucessfuly freed\n"); }
1143 if( xv_port !=0 ){
1144 XvUngrabPort(mDisplay,xv_port,CurrentTime);
1145 xv_port = 0;
1146 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
1147 printf("vo_xvmc: xv_port sucessfuly ungrabed\n"); }
1151 static void uninit(void){
1152 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
1153 printf("vo_xvmc: uninit called\n"); }
1154 xvmc_free();
1155 //from vo_xv
1156 #ifdef CONFIG_XF86VM
1157 vo_vm_close(mDisplay);
1158 #endif
1159 vo_x11_uninit();
1162 static int query_format(uint32_t format){
1163 uint32_t flags;
1164 XvMCSurfaceInfo qsurface_info;
1165 int mode_id;
1167 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1168 printf("vo_xvmc: query_format=%X\n",format);
1170 if(!IMGFMT_IS_XVMC(format)) return 0;// no caps supported
1171 mode_id = xvmc_find_surface_by_format(format, 16, 16, &qsurface_info, 1);//true=1 - quering
1173 if( mode_id == 0 ) return 0;
1175 flags = VFCAP_CSP_SUPPORTED |
1176 VFCAP_CSP_SUPPORTED_BY_HW |
1177 VFCAP_ACCEPT_STRIDE;
1179 if( (qsurface_info.subpicture_max_width != 0) &&
1180 (qsurface_info.subpicture_max_height != 0) )
1181 flags|=VFCAP_OSD;
1182 return flags;
1186 static int draw_slice(uint8_t *image[], int stride[],
1187 int w, int h, int x, int y){
1188 xvmc_render_state_t * rndr;
1189 int rez;
1191 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1192 printf("vo_xvmc: draw_slice y=%d\n",y);
1194 rndr = (xvmc_render_state_t*)image[2];//this is copy of priv-ate
1195 assert( rndr != NULL );
1196 assert( rndr->magic == MP_XVMC_RENDER_MAGIC );
1198 rez = XvMCRenderSurface(mDisplay,&ctx,rndr->picture_structure,
1199 rndr->p_surface,
1200 rndr->p_past_surface,
1201 rndr->p_future_surface,
1202 rndr->flags,
1203 rndr->filled_mv_blocks_num,rndr->start_mv_blocks_num,
1204 &mv_blocks,&data_blocks);
1205 #if 1
1206 if(rez != Success)
1208 int i;
1209 printf("vo_xvmc::slice: RenderSirface returned %d\n",rez);
1211 printf("vo_xvmc::slice: pict=%d,flags=%x,start_blocks=%d,num_blocks=%d\n",
1212 rndr->picture_structure,rndr->flags,rndr->start_mv_blocks_num,
1213 rndr->filled_mv_blocks_num);
1214 printf("vo_xvmc::slice: this_surf=%p, past_surf=%p, future_surf=%p\n",
1215 rndr->p_surface,rndr->p_past_surface,rndr->p_future_surface);
1217 for(i=0; i<rndr->filled_mv_blocks_num; i++){
1218 XvMCMacroBlock* testblock;
1219 testblock = &mv_blocks.macro_blocks[i];
1221 printf("vo_xvmc::slice: mv_block - x=%d,y=%d,mb_type=0x%x,mv_type=0x%x,mv_field_select=%d\n",
1222 testblock->x,testblock->y,testblock->macroblock_type,
1223 testblock->motion_type,testblock->motion_vertical_field_select);
1224 printf("vo_xvmc::slice: dct_type=%d,data_index=0x%x,cbp=%d,pad0=%d\n",
1225 testblock->dct_type,testblock->index,testblock->coded_block_pattern,
1226 testblock->pad0);
1227 printf("vo_xvmc::slice: PMV[0][0][0/1]=(%d,%d)\n",
1228 testblock->PMV[0][0][0],testblock->PMV[0][0][1]);
1231 #endif
1232 assert(rez==Success);
1233 if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) printf("vo_xvmc: flush surface\n");
1234 rez = XvMCFlushSurface(mDisplay, rndr->p_surface);
1235 assert(rez==Success);
1237 // rndr->start_mv_blocks_num += rndr->filled_mv_blocks_num;
1238 rndr->start_mv_blocks_num = 0;
1239 rndr->filled_mv_blocks_num = 0;
1241 rndr->next_free_data_block_num = 0;
1243 return VO_TRUE;
1246 //XvMCHide hides the surface on next retrace, so
1247 //check if the surface is not still displaying
1248 static void check_osd_source(xvmc_render_state_t * src_rndr){
1249 xvmc_render_state_t * osd_rndr;
1250 int stat;
1251 //If this is source surface, check does the OSD rendering is compleate
1252 if(src_rndr->state & MP_XVMC_STATE_OSD_SOURCE){
1253 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1254 printf("vo_xvmc: OSD surface=%p quering\n",src_rndr);
1255 osd_rndr = src_rndr->p_osd_target_surface_render;
1256 XvMCGetSurfaceStatus(mDisplay, osd_rndr->p_surface, &stat);
1257 if(!(stat & XVMC_RENDERING))
1258 src_rndr->state &= ~MP_XVMC_STATE_OSD_SOURCE;
1261 static int count_free_surfaces(){
1262 int i,num;
1263 num=0;
1264 for(i=0; i<number_of_surfaces; i++){
1265 check_osd_source(&surface_render[i]);
1266 if(surface_render[i].state == 0)
1267 num++;
1269 return num;
1272 static xvmc_render_state_t * find_free_surface(){
1273 int i,t;
1274 int stat;
1275 xvmc_render_state_t * visible_rndr;
1277 visible_rndr = NULL;
1278 for(i=0; i<number_of_surfaces; i++){
1280 check_osd_source(&surface_render[i]);
1281 if( surface_render[i].state == 0){
1282 XvMCGetSurfaceStatus(mDisplay, surface_render[i].p_surface,&stat);
1283 if( (stat & XVMC_DISPLAYING) == 0 )
1284 return &surface_render[i];
1285 visible_rndr = &surface_render[i];// remember it, use as last resort
1289 //all surfaces are busy, but there is one that will be free
1290 //on next monitor retrace, we just have to wait
1291 if(visible_rndr != NULL){
1292 printf("vo_xvmc: waiting retrace\n");
1293 for(t=0;t<1000;t++){
1294 usec_sleep(1000);//1ms
1295 XvMCGetSurfaceStatus(mDisplay, visible_rndr->p_surface,&stat);
1296 if( (stat & XVMC_DISPLAYING) == 0 )
1297 return visible_rndr;
1300 //todo remove when stable
1301 printf("vo_xvmc: no free surfaces, this should not happen in g1\n");
1302 for(i=0;i<number_of_surfaces;i++)
1303 printf("vo_xvmc: surface[%d].state=%d\n",i,surface_render[i].state);
1304 return NULL;
1307 static void xvmc_clean_surfaces(void){
1308 int i;
1310 for(i=0; i<number_of_surfaces; i++){
1312 surface_render[i].state&=!( MP_XVMC_STATE_DISPLAY_PENDING |
1313 MP_XVMC_STATE_OSD_SOURCE |
1315 surface_render[i].p_osd_target_surface_render=NULL;
1316 if(surface_render[i].state != 0){
1317 mp_msg(MSGT_VO,MSGL_WARN,"vo_xvmc: surface[%d].state=%d\n",
1318 i,surface_render[i].state);
1321 free_element=0;//clean up the queue
1324 static uint32_t get_image(mp_image_t *mpi){
1325 xvmc_render_state_t * rndr;
1327 rndr = find_free_surface();
1329 if(rndr == NULL){
1330 printf("vo_xvmc: get_image failed\n");
1331 return VO_FALSE;
1334 assert(rndr->start_mv_blocks_num == 0);
1335 assert(rndr->filled_mv_blocks_num == 0);
1336 assert(rndr->next_free_data_block_num == 0);
1338 mpi->flags |= MP_IMGFLAG_DIRECT;
1339 //keep strides 0 to avoid field manipulations
1340 mpi->stride[0] = 0;
1341 mpi->stride[1] = 0;
1342 mpi->stride[2] = 0;
1344 // these are shared!! so watch out
1345 // do call RenderSurface before overwriting
1346 mpi->planes[0] = (char*)data_blocks.blocks;
1347 mpi->planes[1] = (char*)mv_blocks.macro_blocks;
1348 mpi->priv =
1349 mpi->planes[2] = (char*)rndr;
1351 rndr->picture_structure = 0;
1352 rndr->flags = 0;
1353 rndr->state = 0;
1354 rndr->start_mv_blocks_num = 0;
1355 rndr->filled_mv_blocks_num = 0;
1356 rndr->next_free_data_block_num = 0;
1358 if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
1359 printf("vo_xvmc: get_image: rndr=%p (surface=%p) \n",
1360 rndr,rndr->p_surface);
1361 return VO_TRUE;
1364 static int control(uint32_t request, void *data)
1366 switch (request){
1367 case VOCTRL_GET_DEINTERLACE:
1368 *(int*)data = bob_deinterlace;
1369 return VO_TRUE;
1370 case VOCTRL_SET_DEINTERLACE:
1371 bob_deinterlace = *(int*)data;
1372 return VO_TRUE;
1373 case VOCTRL_QUERY_FORMAT:
1374 return query_format(*((uint32_t*)data));
1375 case VOCTRL_DRAW_IMAGE:
1376 return xvmc_draw_image((mp_image_t *)data);
1377 case VOCTRL_GET_IMAGE:
1378 return get_image((mp_image_t *)data);
1379 //vo_xv
1380 case VOCTRL_GUISUPPORT:
1381 return VO_TRUE;
1382 case VOCTRL_ONTOP:
1383 vo_x11_ontop();
1384 return VO_TRUE;
1385 case VOCTRL_FULLSCREEN:
1386 vo_x11_fullscreen();
1387 // indended, fallthrough to update panscan on fullscreen/windowed switch
1388 case VOCTRL_SET_PANSCAN:
1389 if ( ( vo_fs && ( vo_panscan != vo_panscan_amount ) ) || ( !vo_fs && vo_panscan_amount ) )
1391 int old_y = vo_panscan_y;
1392 panscan_calc();
1394 if(old_y != vo_panscan_y)
1396 //this also draws the colorkey
1397 put_xvmc_image(p_render_surface_visible,1);
1400 return VO_TRUE;
1401 case VOCTRL_GET_PANSCAN:
1402 if ( !vo_config_count || !vo_fs ) return VO_FALSE;
1403 return VO_TRUE;
1404 case VOCTRL_SET_EQUALIZER:
1406 struct voctrl_set_equalizer_args *args = data;
1407 return vo_xv_set_eq(xv_port, args->name, args->value);
1410 case VOCTRL_GET_EQUALIZER:
1412 struct voctrl_get_equalizer_args *args = data;
1413 return vo_xv_get_eq(xv_port, args->name, args->valueptr);
1415 case VOCTRL_UPDATE_SCREENINFO:
1416 update_xinerama_info();
1417 return VO_TRUE;
1419 return VO_NOTIMPL;