aboutsummaryrefslogtreecommitdiffhomepage
path: root/libvo/vo_xvmc.c
blob: ac06f668ad7b5db32ccf734eb3b89043be30e9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
/*
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "config.h"
#include "mp_msg.h"
#include "video_out.h"
#include "video_out_internal.h"
#include "osdep/timer.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#ifdef HAVE_SHM
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#endif

#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XvMClib.h>

#include "x11_common.h"
#include "libavcodec/xvmc.h"

#include "sub.h"
#include "aspect.h"

#include "subopt-helper.h"
#include "gui/interface.h"

#include "libavutil/common.h"

//no chance for xinerama to be supported in the near future
#undef CONFIG_XINERAMA

#undef NDEBUG
#include <assert.h>


#define UNUSED(x) ((void)(x))

#include "libavcodec/avcodec.h"
#if LIBAVCODEC_BUILD < ((51<<16)+(40<<8)+2)
#error You need at least libavcodecs v51.40.2
#endif


static int benchmark;
static int use_sleep;
static int first_frame;//draw colorkey on first frame
static int use_queue;
static int xv_port_request = 0;
static int xv_adaptor = -1;
static int bob_deinterlace;
static int top_field_first;

static int image_width,image_height;
static int image_format;

#define NO_SUBPICTURE      0
#define OVERLAY_SUBPICTURE 1
#define BLEND_SUBPICTURE   2
#define BACKEND_SUBPICTURE 3

static int subpicture_mode;
static int subpicture_alloc;
static XvMCSubpicture subpicture;
static XvImageFormatValues subpicture_info;
static int subpicture_clear_color;//transparent color for the subpicture or color key for overlay

static XvMCSurfaceInfo surface_info;
static XvMCContext ctx;
static XvMCBlockArray data_blocks;
static XvMCMacroBlockArray mv_blocks;

#define MAX_SURFACES 8
static int number_of_surfaces=0;
static XvMCSurface surface_array[MAX_SURFACES];
static struct xvmc_pix_fmt *surface_render;

static struct xvmc_pix_fmt *p_render_surface_to_show = NULL;
static struct xvmc_pix_fmt *p_render_surface_visible = NULL;

//display queue, kinda render ahead
static struct xvmc_pix_fmt *show_queue[MAX_SURFACES];
static int free_element;


static void (*draw_osd_fnc)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
static void (*clear_osd_fnc)(int x0,int y0, int w,int h);
static void (*init_osd_fnc)(void);

static void   draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
static void   draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
static void   clear_osd_subpic(int x0,int y0, int w,int h);
static void   init_osd_yuv_pal(void);


static const struct{
   int id;//id as xvimages or as mplayer RGB|{8,15,16,24,32}
   void (* init_func_ptr)();
   void (* draw_func_ptr)();
   void (* clear_func_ptr)();
   } osd_render[]={
                     {0x34344149,init_osd_yuv_pal,draw_osd_AI44,clear_osd_subpic},
                     {0x34344941,init_osd_yuv_pal,draw_osd_IA44,clear_osd_subpic},
                     {0,NULL,NULL,NULL}
                  };

static void xvmc_free(void);
static void xvmc_clean_surfaces(void);
static int count_free_surfaces(void);
static struct xvmc_pix_fmt *find_free_surface(void);

static const vo_info_t info = {
  "XVideo Motion Compensation",
  "xvmc",
  "Ivan Kalvachev <iive@users.sf.net>",
  ""
};

const LIBVO_EXTERN(xvmc);

//shm stuff from vo_xv
#ifdef HAVE_SHM
static XShmSegmentInfo Shminfo;
static int Shmem_Flag;
#endif
XvImage * xvimage;


static void allocate_xvimage(int xvimage_width,int xvimage_height,int xv_format)
{
 /*
  * allocate XvImages.  FIXME: no error checking, without
  * mit-shm this will bomb... trzing to fix ::atmos
  */
#ifdef HAVE_SHM
   if ( mLocalDisplay && XShmQueryExtension( mDisplay ) ) Shmem_Flag = 1;
   else
   {
      Shmem_Flag = 0;
      mp_msg(MSGT_VO,MSGL_INFO, "Shared memory not supported\nReverting to normal Xv\n" );
   }
   if ( Shmem_Flag )
   {
      xvimage = (XvImage *) XvShmCreateImage(mDisplay, xv_port, xv_format,
                             NULL, xvimage_width, xvimage_height, &Shminfo);

      Shminfo.shmid    = shmget(IPC_PRIVATE, xvimage->data_size, IPC_CREAT | 0777);
      Shminfo.shmaddr  = (char *) shmat(Shminfo.shmid, 0, 0);
      Shminfo.readOnly = False;

      xvimage->data = Shminfo.shmaddr;
      XShmAttach(mDisplay, &Shminfo);
      XSync(mDisplay, False);
      shmctl(Shminfo.shmid, IPC_RMID, 0);
   }
   else
#endif
   {
      xvimage = (XvImage *) XvCreateImage(mDisplay, xv_port, xv_format, NULL, xvimage_width, xvimage_height);
      xvimage->data = malloc(xvimage->data_size);
      XSync(mDisplay,False);
   }
// memset(xvimage->data,128,xvimage->data_size);
   return;
}

static void deallocate_xvimage(void)
{
#ifdef HAVE_SHM
   if ( Shmem_Flag )
   {
      XShmDetach( mDisplay,&Shminfo );
      shmdt( Shminfo.shmaddr );
   }
   else
#endif
   {
      free(xvimage->data);
   }
   XFree(xvimage);

   XSync(mDisplay, False);
   return;
}
//end of vo_xv shm/xvimage code

static int xvmc_check_surface_format(uint32_t format, XvMCSurfaceInfo * surf_info){
   if ( format == IMGFMT_XVMC_IDCT_MPEG2 ){
      if( surf_info->mc_type != (XVMC_IDCT|XVMC_MPEG_2) ) return -1;
      if( surf_info->chroma_format != XVMC_CHROMA_FORMAT_420 ) return -1;
      return 0;
   }
   if ( format == IMGFMT_XVMC_MOCO_MPEG2 ){
      if(surf_info->mc_type != XVMC_MPEG_2) return -1;
      if(surf_info->chroma_format != XVMC_CHROMA_FORMAT_420) return -1;
      return 0;
   }
return -1;//fail
}

//print all info needed to add new format
static void print_xvimage_format_values(XvImageFormatValues *xifv){
int i;
   printf("Format_ID = 0x%X\n",xifv->id);

   printf("  type = ");
   if(xifv->type == XvRGB) printf("RGB\n");
   else if(xifv->type == XvYUV) printf("YUV\n");
   else printf("Unknown\n");

   printf("  byte_order = ");
   if(xifv->byte_order == LSBFirst) printf("LSB First\n");
   else if(xifv->type == MSBFirst) printf("MSB First\n");
   else printf("Unknown\n");//yes Linux support other types too

   printf("  guid = ");
   for(i=0;i<16;i++)
      printf("%02X ",(unsigned char)xifv->guid[i]);
   printf("\n");

   printf("  bits_per_pixel = %d\n",xifv->bits_per_pixel);

   printf("  format = ");
   if(xifv->format == XvPacked) printf("XvPacked\n");
   else if(xifv->format == XvPlanar) printf("XvPlanar\n");
   else printf("Unknown\n");

   printf("  num_planes = %d\n",xifv->num_planes);

   if(xifv->type == XvRGB){
      printf("  red_mask = %0X\n",  xifv->red_mask);
      printf("  green_mask = %0X\n",xifv->green_mask);
      printf("  blue_mask = %0X\n", xifv->blue_mask);
   }
   if(xifv->type == XvYUV){
      printf("  y_sample_bits = %d\n  u_sample_bits = %d\n  v_sample_bits = %d\n",
             xifv->y_sample_bits,xifv->u_sample_bits,xifv->v_sample_bits);
      printf("  horz_y_period = %d\n  horz_u_period = %d\n  horz_v_period = %d\n",
            xifv->horz_y_period,xifv->horz_u_period,xifv->horz_v_period);
      printf("  vert_y_period = %d\n  vert_u_period = %d\n  vert_v_period = %d\n",
            xifv->vert_y_period,xifv->vert_u_period,xifv->vert_v_period);

      printf("  component_order = ");
      for(i=0;i<32;i++)
         if(xifv->component_order[i]>=32)
            printf("%c",xifv->component_order[i]);
      printf("\n");

      printf("  scanline = ");
      if(xifv->scanline_order == XvTopToBottom) printf("XvTopToBottom\n");
      else if(xifv->scanline_order == XvBottomToTop) printf("XvBottomToTop\n");
      else printf("Unknown\n");
   }
   printf("\n");
}

// WARNING This function may changes xv_port and surface_info!
static int xvmc_find_surface_by_format(int format,int width,int height,
                                  XvMCSurfaceInfo * surf_info,int query){
int rez;
XvAdaptorInfo * ai;
int num_adaptors,i;
unsigned long p;
int s,mc_surf_num;
XvMCSurfaceInfo * mc_surf_list;

   rez = XvQueryAdaptors(mDisplay,DefaultRootWindow(mDisplay),&num_adaptors,&ai);
   if( rez != Success ) return -1;
   if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
      printf("vo_xvmc: Querying %d adaptors\n",num_adaptors); }
   for(i=0; i<num_adaptors; i++)
   {
      /* check if adaptor number has been specified */
      if (xv_adaptor != -1 && xv_adaptor != i)
         continue;
      if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
         printf("vo_xvmc: Quering adaptor #%d\n",i); }
      if( ai[i].type == 0 ) continue;// we need at least dummy type!
//probing ports
      for(p=ai[i].base_id; p<ai[i].base_id+ai[i].num_ports; p++)
      {
         if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
            printf("vo_xvmc: probing port #%ld\n",p); }
         mc_surf_list = XvMCListSurfaceTypes(mDisplay,p,&mc_surf_num);
         if( mc_surf_list == NULL || mc_surf_num == 0){
            if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
               printf("vo_xvmc: No XvMC supported. \n"); }
            continue;
         }
         if( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
            printf("vo_xvmc: XvMC list have %d surfaces\n",mc_surf_num); }
//we have XvMC list!
         for(s=0; s<mc_surf_num; s++)
         {
            if( width > mc_surf_list[s].max_width ) continue;
            if( height > mc_surf_list[s].max_height ) continue;
            if( xvmc_check_surface_format(format,&mc_surf_list[s])<0 ) continue;
//we have match!
            /* respect the users wish */
            if ( xv_port_request != 0 && xv_port_request != p )
            {
               continue;
            }

            if(!query){
               rez = XvGrabPort(mDisplay,p,CurrentTime);
               if(rez != Success){
                  if ( mp_msg_test(MSGT_VO,MSGL_DBG3) ) {
                     printf("vo_xvmc: Fail to grab port %ld\n",p); }
                  continue;
               }
               printf("vo_xvmc: Using Xv Adaptor #%d (%s)\n", i, ai[i].name);
               printf("vo_xvmc: Port %ld grabed\n",p);
               xv_port = p;
            }
            goto surface_found;
         }//for mc surf
         XFree(mc_surf_list);//if mc_surf_num==0 is list==NULL ?
      }//for ports
   }//for adaptors
   XvFreeAdaptorInfo(ai);

   if(!query) printf("vo_xvmc: Could not find free matching surface. Sorry.\n");
   return 0;

// somebody know cleaner way to escape from 3 internal loops?
surface_found:
   XvFreeAdaptorInfo(ai);

   memcpy(surf_info,&mc_surf_list[s],sizeof(XvMCSurfaceInfo));
   if( mp_msg_test(MSGT_VO,MSGL_DBG3) || !query)
      printf("vo_xvmc: Found matching surface with id=%X on %ld port at %d adapter\n",
             mc_surf_list[s].surface_type_id,p,i);
   return mc_surf_list[s].surface_type_id;
}

static uint32_t xvmc_draw_image(mp_image_t *mpi){
   struct xvmc_pix_fmt *rndr;

   assert(mpi!=NULL);
   assert(mpi->flags &MP_IMGFLAG_DIRECT);
//   assert(mpi->flags &MP_IMGFLAGS_DRAWBACK);

   rndr = (struct xvmc_pix_fmt*)mpi->priv; //there is copy in plane[2]
   assert( rndr != NULL );
   assert( rndr->xvmc_id == AV_XVMC_ID );
   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
       printf("vo_xvmc: draw_image(show rndr=%p)\n",rndr);
// the surface have passed vf system without been skiped, it will be displayed
   rndr->state |= AV_XVMC_STATE_DISPLAY_PENDING;
   p_render_surface_to_show = rndr;
   top_field_first = mpi->fields & MP_IMGFIELD_TOP_FIRST;
   return VO_TRUE;
}

static int preinit(const char *arg){
int xv_version,xv_release,xv_request_base,xv_event_base,xv_error_base;
int mc_eventBase,mc_errorBase;
int mc_ver,mc_rev;
strarg_t ck_src_arg = { 0, NULL };
strarg_t ck_method_arg = { 0, NULL };
opt_t subopts [] =
{
  /* name         arg type      arg var           test */
  {  "port",      OPT_ARG_INT,  &xv_port_request, (opt_test_f)int_pos },
  {  "adaptor",   OPT_ARG_INT,  &xv_adaptor,      (opt_test_f)int_non_neg },
  {  "ck",        OPT_ARG_STR,  &ck_src_arg,      xv_test_ck },
  {  "ck-method", OPT_ARG_STR,  &ck_method_arg,   xv_test_ckm },
  {  "benchmark", OPT_ARG_BOOL, &benchmark,       NULL },
  {  "sleep",     OPT_ARG_BOOL, &use_sleep,       NULL },
  {  "queue",     OPT_ARG_BOOL, &use_queue,       NULL },
  {  "bobdeint",  OPT_ARG_BOOL, &bob_deinterlace, NULL },
  {  NULL }
};

   //Obtain display handler
   if (!vo_init()) return -1;//vo_xv

   //XvMC is subdivision of XVideo
   if (Success != XvQueryExtension(mDisplay,&xv_version,&xv_release,&xv_request_base,
                         &xv_event_base,&xv_error_base) ){
      mp_msg(MSGT_VO,MSGL_ERR,"Sorry, Xv(MC) not supported by this X11 version/driver\n");
      mp_msg(MSGT_VO,MSGL_ERR,"********** Try with  -vo x11  or  -vo sdl  ***********\n");
      return -1;
   }
   printf("vo_xvmc: X-Video extension %d.%d\n",xv_version,xv_release);

   if( True != XvMCQueryExtension(mDisplay,&mc_eventBase,&mc_errorBase) ){
      printf("vo_xvmc: No X-Video MotionCompensation Extension on %s\n",
              XDisplayName(NULL));
      return -1;
   }

   if(Success == XvMCQueryVersion(mDisplay, &mc_ver, &mc_rev) ){
      printf("vo_xvmc: X-Video MotionCompensation Extension version %i.%i\n",
                mc_ver,mc_rev);
   }
   else{
      printf("vo_xvmc: Error querying version info!\n");
      return -1;
   }
   surface_render = NULL;
   xv_port = 0;
   number_of_surfaces = 0;
   subpicture_alloc = 0;

   benchmark = 0; //disable PutImageto allow faster display than screen refresh
   use_sleep = 0;
   use_queue = 0;
   bob_deinterlace = 0;

   /* parse suboptions */
   if ( subopt_parse( arg, subopts ) != 0 )
   {
     return -1;
   }

   xv_setup_colorkeyhandling( ck_method_arg.str, ck_src_arg.str );

   return 0;
}

static int config(uint32_t width, uint32_t height,
                       uint32_t d_width, uint32_t d_height,
                       uint32_t flags, char *title, uint32_t format){
int i,mode_id,rez;
int numblocks,blocks_per_macroblock;//bpmb we have 6,8,12

//from vo_xv
XVisualInfo vinfo;
XSetWindowAttributes xswa;
XWindowAttributes attribs;
unsigned long xswamask;
int depth;
#ifdef CONFIG_XF86VM
int vm = flags & VOFLAG_MODESWITCHING;
#endif
//end of vo_xv

   if( !IMGFMT_IS_XVMC(format) )
   {
      assert(0);//should never happen, abort on debug or
      return 1;//return error on relese
   }

// Find free port that supports MC, by querying adaptors
   if( xv_port != 0 || number_of_surfaces != 0 ){
      if( height==image_height && width==image_width && image_format==format){
         xvmc_clean_surfaces();
         goto skip_surface_allocation;
      }
      xvmc_free();
   };
   numblocks=((width+15)/16)*((height+15)/16);
// Find Supported Surface Type
   mode_id = xvmc_find_surface_by_format(format,width,height,&surface_info,0);//false=1 to grab port, not query
   if ( mode_id == 0 )
   {
      return -1;
   }

   rez = XvMCCreateContext(mDisplay, xv_port,mode_id,width,height,XVMC_DIRECT,&ctx);
   if( rez != Success ){
      printf("vo_xvmc: XvMCCreateContext failed with error %d\n",rez);
      return -1;
   }
   if( ctx.flags & XVMC_DIRECT ){
      printf("vo_xvmc: Allocated Direct Context\n");
   }else{
      printf("vo_xvmc: Allocated Indirect Context!\n");
   }


   blocks_per_macroblock = 6;
   if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_422)
      blocks_per_macroblock = 8;
   if(surface_info.chroma_format == XVMC_CHROMA_FORMAT_444)
      blocks_per_macroblock = 12;

   rez = XvMCCreateBlocks(mDisplay,&ctx,numblocks*blocks_per_macroblock,&data_blocks);
   if( rez != Success ){
      XvMCDestroyContext(mDisplay,&ctx);
      return -1;
   }
   printf("vo_xvmc: data_blocks allocated\n");

   rez = XvMCCreateMacroBlocks(mDisplay,&ctx,numblocks,&mv_blocks);
   if( rez != Success ){
      XvMCDestroyBlocks(mDisplay,&data_blocks);
      XvMCDestroyContext(mDisplay,&ctx);
      return -1;
   }
   printf("vo_xvmc: mv_blocks allocated\n");

   if(surface_render==NULL)
      surface_render = malloc(MAX_SURFACES * sizeof(struct xvmc_pix_fmt)); //easy mem debug
   memset(surface_render, 0, MAX_SURFACES * sizeof(struct xvmc_pix_fmt));

   for(i=0; i<MAX_SURFACES; i++){
      rez=XvMCCreateSurface(mDisplay,&ctx,&surface_array[i]);
      if( rez != Success )
         break;
      surface_render[i].xvmc_id = AV_XVMC_ID;
      surface_render[i].data_blocks = data_blocks.blocks;
      surface_render[i].mv_blocks = mv_blocks.macro_blocks;
      surface_render[i].allocated_mv_blocks = numblocks;
      surface_render[i].allocated_data_blocks = numblocks*blocks_per_macroblock;
      surface_render[i].idct = (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT;
      surface_render[i].unsigned_intra = (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED;
      surface_render[i].p_surface = &surface_array[i];
      if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
          printf("vo_xvmc: surface[%d] = %p .rndr=%p\n",i,&surface_array[i], &surface_render[i]);
   }
   number_of_surfaces = i;
   if( number_of_surfaces < 4 ){// +2 I or P and +2 for B (to avoid visible motion drawing)
      printf("vo_xvmc: Unable to allocate at least 4 Surfaces\n");
      uninit();
      return -1;
   }
   printf("vo_xvmc: Motion Compensation context allocated - %d surfaces\n",
          number_of_surfaces);

  //debug
   printf("vo_xvmc: idct=%d unsigned_intra=%d\n",
           (surface_info.mc_type & XVMC_IDCT) == XVMC_IDCT,
           (surface_info.flags & XVMC_INTRA_UNSIGNED) == XVMC_INTRA_UNSIGNED);

// Find way to display OSD & subtitle
   printf("vo_xvmc: looking for OSD support\n");
   subpicture_mode = NO_SUBPICTURE;
   if(surface_info.flags & XVMC_OVERLAID_SURFACE)
      subpicture_mode = OVERLAY_SUBPICTURE;

   if(surface_info.subpicture_max_width  != 0 &&
      surface_info.subpicture_max_height != 0  ){
      int s,k,num_subpic;

      XvImageFormatValues * xvfmv;
      xvfmv = XvMCListSubpictureTypes(mDisplay, xv_port,
                      surface_info.surface_type_id, &num_subpic);

      if(num_subpic != 0 && xvfmv != NULL){
         if( mp_msg_test(MSGT_VO,MSGL_DBG4) ){//Print all subpicture types for debug
            for(s=0;s<num_subpic;s++)
               print_xvimage_format_values(&xvfmv[s]);
         }

         for(s=0;s<num_subpic;s++){
            for(k=0;osd_render[k].draw_func_ptr!=NULL;k++){
               if(xvfmv[s].id == osd_render[k].id)
               {
                  init_osd_fnc  = osd_render[k].init_func_ptr;
                  draw_osd_fnc  = osd_render[k].draw_func_ptr;
                  clear_osd_fnc = osd_render[k].clear_func_ptr;

                  subpicture_mode = BLEND_SUBPICTURE;
                  subpicture_info = xvfmv[s];
                  printf("    Subpicture id 0x%08X\n",subpicture_info.id);
                  goto found_subpic;
               }
            }
         }
found_subpic:
         XFree(xvfmv);
      }
      //Blend2 supicture is always possible, blend1 only at backend
      if( (subpicture_mode == BLEND_SUBPICTURE) &&
          (surface_info.flags & XVMC_BACKEND_SUBPICTURE) )
      {
         subpicture_mode = BACKEND_SUBPICTURE;
      }

   }

   switch(subpicture_mode){
      case NO_SUBPICTURE:
         printf("vo_xvmc: No OSD support for this mode\n");
         break;
      case OVERLAY_SUBPICTURE:
         printf("vo_xvmc: OSD support via color key tricks\n");
         printf("vo_xvmc: not yet implemented:(\n");
         break;
      case BLEND_SUBPICTURE:
         printf("vo_xvmc: OSD support by additional frontend rendering\n");
         break;
      case BACKEND_SUBPICTURE:
         printf("vo_xvmc: OSD support by backend rendering (fast)\n");
         printf("vo_xvmc: Please send feedback to confirm that it works,otherwise send bugreport!\n");
         break;
   }

//take keycolor value and choose method for handling it
   if ( !vo_xv_init_colorkey() )
   {
     return -1; // bail out, colorkey setup failed
   }

   vo_xv_enable_vsync();//it won't break anything

//taken from vo_xv
   image_height = height;
   image_width = width;

skip_surface_allocation:

#ifdef CONFIG_GUI
   if(use_gui)
      guiGetEvent( guiSetShVideo,0 ); // let the GUI to setup/resize our window
   else
#endif
   {
#ifdef CONFIG_XF86VM
      if ( vm )
      {
         vo_vm_switch();
      }
      else
#endif
   XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
   depth=attribs.depth;
   if (depth != 15 && depth != 16 && depth != 24 && depth != 32) depth = 24;
   XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);

   xswa.background_pixel = 0;
   if (xv_ck_info.method == CK_METHOD_BACKGROUND)
      xswa.background_pixel = xv_colorkey;
   xswa.border_pixel     = 0;
   xswamask = CWBackPixel | CWBorderPixel;

      vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height, flags,
              CopyFromParent, "xvmc", title);
      XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);

#ifdef CONFIG_XF86VM
      if ( vm )
      {
      /* Grab the mouse pointer in our window */
         if(vo_grabpointer)
         XGrabPointer(mDisplay, vo_window, True, 0,
                      GrabModeAsync, GrabModeAsync,
                      vo_window, None, CurrentTime );
         XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
      }
#endif
   }

   if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0) vo_fs = 1;

//end vo_xv

   /* store image dimesions for displaying */
   p_render_surface_visible = NULL;
   p_render_surface_to_show = NULL;

   free_element = 0;
   first_frame = 1;

   image_format=format;
   return 0;
}

static int draw_frame(uint8_t *srcp[]){
   UNUSED(srcp);
   assert(0);
}

static void init_osd_yuv_pal(void) {
   char * palette;
   int rez;
   int i,j;
   int snum,seb;
   int Y,U,V;

   subpicture_clear_color = 0;

   if(subpicture.num_palette_entries > 0){

      snum = subpicture.num_palette_entries;
      seb = subpicture.entry_bytes;
      palette = malloc(snum*seb);//check fail
      if(palette == NULL) return;
      for(i=0; i<snum; i++){
         // 0-black max-white the other are gradients
         Y = i*(1 << subpicture_info.y_sample_bits)/snum;//snum=2;->(0),(1*(1<<1)/2)
         U = 1 << (subpicture_info.u_sample_bits - 1);
         V = 1 << (subpicture_info.v_sample_bits - 1);
         for(j=0; j<seb; j++)
            switch(subpicture.component_order[j]){
               case 'U': palette[i*seb+j] = U; break;
               case 'V': palette[i*seb+j] = V; break;
               case 'Y':
               default:
                         palette[i*seb+j] = Y; break;
         }
      }
      rez = XvMCSetSubpicturePalette(mDisplay, &subpicture, palette);
      if(rez!=Success){
         printf("vo_xvmc: Setting palette failed.\n");
      }
      free(palette);
   }
}

static void clear_osd_subpic(int x0, int y0, int w, int h){
int rez;
   rez=XvMCClearSubpicture(mDisplay, &subpicture,
                       x0, y0, w,h,
                       subpicture_clear_color);
   if(rez != Success)
      printf("vo_xvmc: XvMCClearSubpicture failed!\n");
}

static void OSD_init(void) {
unsigned short osd_height, osd_width;
int rez;

   if(subpicture_alloc){
      if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
         printf("vo_xvmc: destroying subpicture\n");
      XvMCDestroySubpicture(mDisplay,&subpicture);
      deallocate_xvimage();
      subpicture_alloc = 0;
   }

/*   if(surface_info.flags & XVMC_SUBPICTURE_INDEPENDENT_SCALING){
      osd_width = vo_dwidth;
      osd_height = vo_dheight;
   }else*/
   {
      osd_width = image_width;
      osd_height = image_height;
   }

   if(osd_width > surface_info.subpicture_max_width)
      osd_width = surface_info.subpicture_max_width;
   if(osd_height > surface_info.subpicture_max_height)
      osd_height = surface_info.subpicture_max_height;
   if(osd_width == 0 || osd_height == 0)
      return;//if called before window size is known

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: creating subpicture (%d,%d) format %X\n",
              osd_width,osd_height,subpicture_info.id);

   rez = XvMCCreateSubpicture(mDisplay,&ctx,&subpicture,
                           osd_width,osd_height,subpicture_info.id);
   if(rez != Success){
      subpicture_mode = NO_SUBPICTURE;
      printf("vo_xvmc: Create Subpicture failed, OSD disabled\n");
      return;
   }
   if( mp_msg_test(MSGT_VO,MSGL_DBG4) ){
   int i;
      printf("vo_xvmc: Created Subpicture:\n");
      printf("         xvimage_id=0x%X\n",subpicture.xvimage_id);
      printf("         width=%d\n",subpicture.width);
      printf("         height=%d\n",subpicture.height);
      printf("         num_palette_entries=0x%X\n",subpicture.num_palette_entries);
      printf("         entry_bytes=0x%X\n",subpicture.entry_bytes);

      printf("         component_order=\"");
      for(i=0; i<4; i++)
         if(subpicture.component_order[i] >= 32)
            printf("%c", subpicture.component_order[i]);
      printf("\"\n");
   }

   //call init for the surface type
   init_osd_fnc();//init palete,clear color etc ...
   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: clearing subpicture\n");
   clear_osd_fnc(0, 0, subpicture.width, subpicture.height);

   allocate_xvimage(subpicture.width, subpicture.height, subpicture_info.id);
   subpicture_alloc = 1;
}

static void draw_osd_IA44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
int ox,oy;
int rez;

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);

   for(ox=0; ox<w; ox++){
      for(oy=0; oy<h; oy++){
         xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]>>4) | ((0-srca[oy*stride+ox])&0xf0);
      }
   }
   rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
                           w,h,x0,y0);
   if(rez != Success){
      printf("vo_xvmc: composite subpicture failed\n");
      assert(0);
   }
}

static void draw_osd_AI44(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
int ox,oy;
int rez;
   if(  mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc:composite AI44 subpicture (%d,%d - %d,%d)\n",x0,y0,w,h);

   for(ox=0; ox<w; ox++){
      for(oy=0; oy<h; oy++){
         xvimage->data[oy*xvimage->width+ox] = (src[oy*stride+ox]&0xf0) | (((0-srca[oy*stride+ox])>>4)&0xf);
      }
   }
   rez = XvMCCompositeSubpicture(mDisplay, &subpicture, xvimage, 0, 0,
                           w,h,x0,y0);
   if(rez != Success){
      printf("vo_xvmc: composite subpicture failed\n");
      assert(0);
   }
}

static void draw_osd(void){
struct xvmc_pix_fmt *osd_rndr;
int osd_has_changed;
int have_osd_to_draw;
int rez;

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: draw_osd ,OSD_mode=%d, surface_to_show=%p\n",
             subpicture_mode,p_render_surface_to_show);

   if(subpicture_mode == BLEND_SUBPICTURE ||
      subpicture_mode == BACKEND_SUBPICTURE ){

      if(!subpicture_alloc) //allocate subpicture when dimensions are known
         OSD_init();
      if(!subpicture_alloc)
         return;//dimensions still unknown.

      osd_has_changed = vo_update_osd(subpicture.width, subpicture.height);
      have_osd_to_draw = vo_osd_check_range_update(0, 0, subpicture.width,
                                                         subpicture.height);

      if(!have_osd_to_draw)
         return;//nothing to draw,no subpic, no blend

      if(osd_has_changed){
         //vo_remove_text(subpicture.width, subpicture.height,clear_osd_fnc)
         clear_osd_fnc(0,0,subpicture.width,subpicture.height);
         vo_draw_text(subpicture.width, subpicture.height, draw_osd_fnc);
      }
      XvMCSyncSubpicture(mDisplay,&subpicture);//todo usleeep wait!

      if(subpicture_mode == BLEND_SUBPICTURE){
         osd_rndr = find_free_surface();
         if(osd_rndr == NULL)
            return;// no free surface to draw OSD in

         rez = XvMCBlendSubpicture2(mDisplay,
                       p_render_surface_to_show->p_surface, osd_rndr->p_surface,
                       &subpicture,
                       0, 0, subpicture.width, subpicture.height,
                       0, 0, image_width, image_height);
         if(rez!=Success){
            printf("vo_xvmc: BlendSubpicture failed rez=%d\n",rez);
            assert(0);
            return;
         }
//       XvMCFlushSurface(mDisplay,osd_rndr->p_surface);//fixme- should I?

         //When replaceing the surface with osd one, save the flags too!
         osd_rndr->picture_structure = p_render_surface_to_show->picture_structure;
//add more if needed    osd_rndr-> = p_render_surface_to_show->;

         p_render_surface_to_show->state &= ~AV_XVMC_STATE_DISPLAY_PENDING;
         p_render_surface_to_show->state |= AV_XVMC_STATE_OSD_SOURCE;
         p_render_surface_to_show->p_osd_target_surface_render = osd_rndr;

         p_render_surface_to_show = osd_rndr;
         p_render_surface_to_show->state = AV_XVMC_STATE_DISPLAY_PENDING;

         if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
            printf("vo_xvmc:draw_osd: surface_to_show changed to %p\n",osd_rndr);
      }//endof if(BLEND)
      if(subpicture_mode == BACKEND_SUBPICTURE){
         rez = XvMCBlendSubpicture(mDisplay,
                       p_render_surface_to_show->p_surface,
                       &subpicture,
                       0, 0, subpicture.width, subpicture.height,
                       0, 0, image_width, image_height);

      }

   }//if(BLEND||BACKEND)
}

static void xvmc_sync_surface(XvMCSurface * srf){
int status,rez;
   rez = XvMCGetSurfaceStatus(mDisplay,srf,&status);
   assert(rez==Success);
   if((status & XVMC_RENDERING) == 0)
      return;//surface is already complete
   if(use_sleep){
      rez = XvMCFlushSurface(mDisplay, srf);
      assert(rez==Success);

      do{
         usec_sleep(1000);//1ms (may be 20ms on linux)
         XvMCGetSurfaceStatus(mDisplay,srf,&status);
      } while (status & XVMC_RENDERING);
      return;//done
   }

   XvMCSyncSurface(mDisplay, srf);
}

static void put_xvmc_image(struct xvmc_pix_fmt *p_render_surface,
                           int draw_ck){
int rez;
struct vo_rect src_rect, dst_rect;
int i;

   if(p_render_surface == NULL)
      return;

   calc_src_dst_rects(image_width, image_height, &src_rect, &dst_rect, NULL);

   if(draw_ck)
      vo_xv_draw_colorkey(dst_rect.left, dst_rect.top, dst_rect.width, dst_rect.height);

   if(benchmark)
      return;

   for (i = 1; i <= bob_deinterlace + 1; i++) {
   int field = top_field_first ? i : i ^ 3;
   rez = XvMCPutSurface(mDisplay, p_render_surface->p_surface,
                        vo_window,
                        src_rect.left, src_rect.top, src_rect.width, src_rect.height,
                        dst_rect.left, dst_rect.top, dst_rect.width, dst_rect.height,
                        bob_deinterlace ? field : 3);
   if(rez != Success){
      printf("vo_xvmc: PutSurface failer, critical error %d!\n",rez);
      assert(0);
   }
   }
   XFlush(mDisplay);
}

static void flip_page(void){
int i,cfs;


   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: flip_page  show(rndr=%p)\n\n",p_render_surface_to_show);

   if(p_render_surface_to_show == NULL) return;
   assert( p_render_surface_to_show->xvmc_id == AV_XVMC_ID );
//fixme   assert( p_render_surface_to_show != p_render_surface_visible);

   if(use_queue){
      // fill the queue until only n free surfaces remain
      // after that start displaying
      cfs = count_free_surfaces();
      show_queue[free_element++] = p_render_surface_to_show;
      if(cfs > 3){//well have 3 free surfaces after add queue
         if(free_element > 1)//a little voodoo magic
            xvmc_sync_surface(show_queue[0]->p_surface);
         return;
      }
      p_render_surface_to_show=show_queue[0];
      if( mp_msg_test(MSGT_VO,MSGL_DBG5) )
         printf("vo_xvmc: flip_queue free_element=%d\n",free_element);
      free_element--;
      for(i=0; i<free_element; i++){
         show_queue[i] = show_queue[i+1];
      }
      show_queue[free_element] = NULL;
   }

// make sure the rendering is done
   xvmc_sync_surface(p_render_surface_to_show->p_surface);

//the visible surface won't be displayed anymore, mark it as free
   if(p_render_surface_visible != NULL)
      p_render_surface_visible->state &= ~AV_XVMC_STATE_DISPLAY_PENDING;

//!!fixme   assert(p_render_surface_to_show->state & AV_XVMC_STATE_DISPLAY_PENDING);

   //show it, displaying is always vsynced, so skip it for benchmark
   put_xvmc_image(p_render_surface_to_show,first_frame);
   first_frame=0;//make sure we won't draw it anymore

   p_render_surface_visible = p_render_surface_to_show;
   p_render_surface_to_show = NULL;
}

static void check_events(void){
int e=vo_x11_check_events(mDisplay);
   if(e&VO_EVENT_RESIZE)
   {
      e |= VO_EVENT_EXPOSE;
   }
   if ( e & VO_EVENT_EXPOSE )
   {
      put_xvmc_image(p_render_surface_visible,1);
   }
}

static void xvmc_free(void){
int i;
   if( subpicture_alloc ){

      XvMCDestroySubpicture(mDisplay,&subpicture);
      deallocate_xvimage();

      subpicture_alloc = 0;

      if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
         printf("vo_xvmc: subpicture destroyed\n");
   }

   if( number_of_surfaces ){

      XvMCDestroyMacroBlocks(mDisplay,&mv_blocks);
      XvMCDestroyBlocks(mDisplay,&data_blocks);

      for(i=0; i<number_of_surfaces; i++)
      {
         XvMCHideSurface(mDisplay,&surface_array[i]);//it doesn't hurt, I hope
         XvMCDestroySurface(mDisplay,&surface_array[i]);

         if( (surface_render[i].state != 0) &&
             (p_render_surface_visible != &surface_render[i]) )
            printf("vo_xvmc::uninit surface_render[%d].status=%d\n",i,
                    surface_render[i].state);
      }

      memset(surface_render, 0, MAX_SURFACES * sizeof(struct xvmc_pix_fmt)); //for debugging
      free(surface_render);surface_render=NULL;

      XvMCDestroyContext(mDisplay,&ctx);
      number_of_surfaces = 0;

      if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
         printf("vo_xvmc: Context sucessfuly freed\n"); }
   }


   if( xv_port !=0 ){
      XvUngrabPort(mDisplay,xv_port,CurrentTime);
      xv_port = 0;
      if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
         printf("vo_xvmc: xv_port sucessfuly ungrabed\n"); }
   }
}

static void uninit(void){
   if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) {
      printf("vo_xvmc: uninit called\n"); }
   xvmc_free();
 //from vo_xv
#ifdef CONFIG_XF86VM
   vo_vm_close();
#endif
   vo_x11_uninit();
}

static int query_format(uint32_t format){
uint32_t flags;
XvMCSurfaceInfo qsurface_info;
int mode_id;

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: query_format=%X\n",format);

   if(!IMGFMT_IS_XVMC(format)) return 0;// no caps supported
   mode_id = xvmc_find_surface_by_format(format, 16, 16, &qsurface_info, 1);//true=1 - quering

   if( mode_id == 0 ) return 0;

   flags = VFCAP_CSP_SUPPORTED |
           VFCAP_CSP_SUPPORTED_BY_HW |
           VFCAP_ACCEPT_STRIDE;

   if( (qsurface_info.subpicture_max_width  != 0) &&
       (qsurface_info.subpicture_max_height != 0) )
      flags|=VFCAP_OSD;
   return flags;
}


static int draw_slice(uint8_t *image[], int stride[],
                           int w, int h, int x, int y){
struct xvmc_pix_fmt *rndr;
int rez;

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: draw_slice y=%d\n",y);

   rndr = (struct xvmc_pix_fmt*)image[2]; //this is copy of priv-ate
   assert( rndr != NULL );
   assert( rndr->xvmc_id == AV_XVMC_ID );

   rez = XvMCRenderSurface(mDisplay,&ctx,rndr->picture_structure,
                           rndr->p_surface,
                           rndr->p_past_surface,
                           rndr->p_future_surface,
                           rndr->flags,
                           rndr->filled_mv_blocks_num,rndr->start_mv_blocks_num,
                           &mv_blocks,&data_blocks);
#if 1
   if(rez != Success)
   {
   int i;
      printf("vo_xvmc::slice: RenderSirface returned %d\n",rez);

      printf("vo_xvmc::slice: pict=%d,flags=%x,start_blocks=%d,num_blocks=%d\n",
             rndr->picture_structure,rndr->flags,rndr->start_mv_blocks_num,
             rndr->filled_mv_blocks_num);
      printf("vo_xvmc::slice: this_surf=%p, past_surf=%p, future_surf=%p\n",
             rndr->p_surface,rndr->p_past_surface,rndr->p_future_surface);

      for(i=0; i<rndr->filled_mv_blocks_num; i++){
       XvMCMacroBlock* testblock;
         testblock = &mv_blocks.macro_blocks[i];

         printf("vo_xvmc::slice: mv_block - x=%d,y=%d,mb_type=0x%x,mv_type=0x%x,mv_field_select=%d\n",
                testblock->x,testblock->y,testblock->macroblock_type,
                testblock->motion_type,testblock->motion_vertical_field_select);
         printf("vo_xvmc::slice: dct_type=%d,data_index=0x%x,cbp=%d,pad0=%d\n",
                 testblock->dct_type,testblock->index,testblock->coded_block_pattern,
                 testblock->pad0);
         printf("vo_xvmc::slice: PMV[0][0][0/1]=(%d,%d)\n",
                 testblock->PMV[0][0][0],testblock->PMV[0][0][1]);
       }
   }
#endif
   assert(rez==Success);
   if( mp_msg_test(MSGT_VO,MSGL_DBG4) ) printf("vo_xvmc: flush surface\n");
   rez = XvMCFlushSurface(mDisplay, rndr->p_surface);
   assert(rez==Success);

//   rndr->start_mv_blocks_num += rndr->filled_mv_blocks_num;
   rndr->start_mv_blocks_num = 0;
   rndr->filled_mv_blocks_num = 0;

   rndr->next_free_data_block_num = 0;

   return VO_TRUE;
}

//XvMCHide hides the surface on next retrace, so
//check if the surface is not still displaying
static void check_osd_source(struct xvmc_pix_fmt *src_rndr) {
struct xvmc_pix_fmt *osd_rndr;
int stat;
      //If this is source surface, check does the OSD rendering is compleate
      if(src_rndr->state & AV_XVMC_STATE_OSD_SOURCE){
         if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
            printf("vo_xvmc: OSD surface=%p quering\n",src_rndr);
         osd_rndr = src_rndr->p_osd_target_surface_render;
         XvMCGetSurfaceStatus(mDisplay, osd_rndr->p_surface, &stat);
         if(!(stat & XVMC_RENDERING))
            src_rndr->state &= ~AV_XVMC_STATE_OSD_SOURCE;
      }
}
static int count_free_surfaces(void) {
int i,num;
   num=0;
   for(i=0; i<number_of_surfaces; i++){
      check_osd_source(&surface_render[i]);
      if(surface_render[i].state == 0)
        num++;
   }
   return num;
}

static struct xvmc_pix_fmt *find_free_surface(void) {
int i,t;
int stat;
struct xvmc_pix_fmt *visible_rndr;

   visible_rndr = NULL;
   for(i=0; i<number_of_surfaces; i++){

      check_osd_source(&surface_render[i]);
      if( surface_render[i].state == 0){
         XvMCGetSurfaceStatus(mDisplay, surface_render[i].p_surface,&stat);
         if( (stat & XVMC_DISPLAYING) == 0 )
            return &surface_render[i];
         visible_rndr = &surface_render[i];// remember it, use as last resort
      }
   }

   //all surfaces are busy, but there is one that will be free
   //on next monitor retrace, we just have to wait
   if(visible_rndr != NULL){
      printf("vo_xvmc: waiting retrace\n");
      for(t=0;t<1000;t++){
         usec_sleep(1000);//1ms
         XvMCGetSurfaceStatus(mDisplay, visible_rndr->p_surface,&stat);
         if( (stat & XVMC_DISPLAYING) == 0 )
            return visible_rndr;
      }
   }
//todo remove when stable
   printf("vo_xvmc: no free surfaces, this should not happen in g1\n");
   for(i=0;i<number_of_surfaces;i++)
      printf("vo_xvmc: surface[%d].state=%d\n",i,surface_render[i].state);
   return NULL;
}

static void xvmc_clean_surfaces(void){
int i;

  for(i=0; i<number_of_surfaces; i++){

      surface_render[i].state&=!( AV_XVMC_STATE_DISPLAY_PENDING |
                                  AV_XVMC_STATE_OSD_SOURCE |
                                  0);
      surface_render[i].p_osd_target_surface_render=NULL;
      if(surface_render[i].state != 0){
         mp_msg(MSGT_VO,MSGL_WARN,"vo_xvmc: surface[%d].state=%d\n",
                                   i,surface_render[i].state);
      }
   }
   free_element=0;//clean up the queue
}

static uint32_t get_image(mp_image_t *mpi){
   struct xvmc_pix_fmt *rndr;

   rndr = find_free_surface();

   if(rndr == NULL){
      printf("vo_xvmc: get_image failed\n");
      return VO_FALSE;
   }

assert(rndr->start_mv_blocks_num == 0);
assert(rndr->filled_mv_blocks_num == 0);
assert(rndr->next_free_data_block_num == 0);

   mpi->flags |= MP_IMGFLAG_DIRECT;
//keep strides 0 to avoid field manipulations
   mpi->stride[0] = 0;
   mpi->stride[1] = 0;
   mpi->stride[2] = 0;

// these are shared!! so watch out
// do call RenderSurface before overwriting
   mpi->planes[0] = (char*)data_blocks.blocks;
   mpi->planes[1] = (char*)mv_blocks.macro_blocks;
   mpi->priv =
   mpi->planes[2] = (char*)rndr;

   rndr->picture_structure = 0;
   rndr->flags = 0;
   rndr->state = 0;
   rndr->start_mv_blocks_num = 0;
   rndr->filled_mv_blocks_num = 0;
   rndr->next_free_data_block_num = 0;

   if( mp_msg_test(MSGT_VO,MSGL_DBG4) )
      printf("vo_xvmc: get_image: rndr=%p (surface=%p) \n",
             rndr,rndr->p_surface);
return VO_TRUE;
}

static int control(uint32_t request, void *data, ... )
{
   switch (request){
      case VOCTRL_GET_DEINTERLACE:
        *(int*)data = bob_deinterlace;
        return VO_TRUE;
      case VOCTRL_SET_DEINTERLACE:
        bob_deinterlace = *(int*)data;
        return VO_TRUE;
      case VOCTRL_QUERY_FORMAT:
         return query_format(*((uint32_t*)data));
      case VOCTRL_DRAW_IMAGE:
         return xvmc_draw_image((mp_image_t *)data);
      case VOCTRL_GET_IMAGE:
         return get_image((mp_image_t *)data);
      //vo_xv
      case VOCTRL_GUISUPPORT:
         return VO_TRUE;
      case VOCTRL_ONTOP:
         vo_x11_ontop();
         return VO_TRUE;
      case VOCTRL_FULLSCREEN:
         vo_x11_fullscreen();
      // indended, fallthrough to update panscan on fullscreen/windowed switch
      case VOCTRL_SET_PANSCAN:
         if ( ( vo_fs && ( vo_panscan != vo_panscan_amount ) ) || ( !vo_fs && vo_panscan_amount ) )
         {
            int old_y = vo_panscan_y;
            panscan_calc();

            if(old_y != vo_panscan_y)
            {
               //this also draws the colorkey
               put_xvmc_image(p_render_surface_visible,1);
            }
         }
         return VO_TRUE;
      case VOCTRL_GET_PANSCAN:
         if ( !vo_config_count || !vo_fs ) return VO_FALSE;
         return VO_TRUE;
      case VOCTRL_SET_EQUALIZER:
      {
      va_list ap;
      int value;

         va_start(ap, data);
         value = va_arg(ap, int);
         va_end(ap);

         return vo_xv_set_eq(xv_port, data, value);
      }

      case VOCTRL_GET_EQUALIZER:
      {
      va_list ap;
      int *value;

         va_start(ap, data);
         value = va_arg(ap, int*);
         va_end(ap);

         return vo_xv_get_eq(xv_port, data, value);
      }
      case VOCTRL_UPDATE_SCREENINFO:
         update_xinerama_info();
         return VO_TRUE;
   }
return VO_NOTIMPL;
}