]> git.sesse.net Git - vlc/blob - modules/video_output/x11/xvmc.c
cosmetic: remove nullity test on free() and delete
[vlc] / modules / video_output / x11 / xvmc.c
1 /*****************************************************************************
2  * xvmc.c : XVMC plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Shane Harper <shanegh@optusnet.com.au>
8  *          Vincent Seguin <seguin@via.ecp.fr>
9  *          Samuel Hocevar <sam@zoy.org>
10  *          David Kennedy <dkennedy@tinytoad.com>
11  *          Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_interface.h>
39 #include <vlc_vout.h>
40 #include <vlc_keys.h>
41
42 #ifdef HAVE_MACHINE_PARAM_H
43     /* BSD */
44 #   include <machine/param.h>
45 #   include <sys/types.h>                                  /* typedef ushort */
46 #   include <sys/ipc.h>
47 #endif
48
49 #ifndef WIN32
50 #   include <netinet/in.h>                            /* BSD: struct in_addr */
51 #endif
52
53 #ifdef HAVE_SYS_SHM_H
54 #   include <sys/shm.h>                                /* shmget(), shmctl() */
55 #endif
56
57 #include <X11/Xlib.h>
58 #include <X11/Xmd.h>
59 #include <X11/Xutil.h>
60 #include <X11/keysym.h>
61 #ifdef HAVE_SYS_SHM_H
62 #   include <X11/extensions/XShm.h>
63 #endif
64 #ifdef DPMSINFO_IN_DPMS_H
65 #   include <X11/extensions/dpms.h>
66 #endif
67
68 #include <X11/extensions/Xv.h>
69 #include <X11/extensions/Xvlib.h>
70 #include <X11/extensions/vldXvMC.h>
71
72 #include "../../codec/xvmc/accel_xvmc.h"
73 #include "xcommon.h"
74 #include "../../codec/spudec/spudec.h"
75 #include <unistd.h>
76
77 /* picture structure */
78 #define TOP_FIELD 1
79 #define BOTTOM_FIELD 2
80 #define FRAME_PICTURE 3
81
82 /* picture coding type */
83 #define I_TYPE 1
84 #define P_TYPE 2
85 #define B_TYPE 3
86 #define D_TYPE 4
87
88 /*****************************************************************************
89  * Exported prototypes
90  *****************************************************************************/
91 extern int  Activate   ( vlc_object_t * );
92 extern void Deactivate ( vlc_object_t * );
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 #define ADAPTOR_TEXT N_("XVMC adaptor number")
98 #define ADAPTOR_LONGTEXT N_( \
99     "If you graphics card provides several adaptors, this option allows you " \
100     "to choose which one will be used (you shouldn't have to change this).")
101
102 #define ALT_FS_TEXT N_("Alternate fullscreen method")
103 #define ALT_FS_LONGTEXT N_( \
104     "There are two ways to make a fullscreen window, unfortunately each one " \
105     "has its drawbacks.\n" \
106     "1) Let the window manager handle your fullscreen window (default), but " \
107     "things like taskbars will likely show on top of the video.\n" \
108     "2) Completely bypass the window manager, but then nothing will be able " \
109     "to show on top of the video.")
110
111 #define DISPLAY_TEXT N_("X11 display name")
112 #define DISPLAY_LONGTEXT N_( \
113     "Specify the X11 hardware display you want to use. By default VLC will " \
114     "use the value of the DISPLAY environment variable.")
115
116 #define CHROMA_TEXT N_("XVimage chroma format")
117 #define CHROMA_LONGTEXT N_( \
118     "Force the XVideo renderer to use a specific chroma format instead of " \
119     "trying to improve performances by using the most efficient one.")
120
121 #define SHM_TEXT N_("Use shared memory")
122 #define SHM_LONGTEXT N_( \
123     "Use shared memory to communicate between VLC and the X server.")
124
125 #define SCREEN_TEXT N_("Screen to be used for fullscreen mode.")
126 #define SCREEN_LONGTEXT N_( \
127     "Choose the screen you want to use in fullscreen mode. For instance " \
128     "set it to 0 for first screen, 1 for the second.")
129
130 #define MODE_TEXT N_("Deinterlace mode")
131 #define MODE_LONGTEXT N_("You can choose the default deinterlace mode")
132
133 #define CROP_TEXT N_("Crop")
134 #define CROP_LONGTEXT N_("You can choose the crop style to apply.")
135
136 vlc_module_begin ()
137     set_shortname( "XVMC" )
138     add_string( "xvmc-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
139     add_integer( "xvmc-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true )
140     add_bool( "xvmc-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, true )
141     add_string( "xvmc-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
142 #ifdef HAVE_SYS_SHM_H
143     add_bool( "xvmc-shm", 1, NULL, SHM_TEXT, SHM_LONGTEXT, true )
144 #endif
145 #ifdef HAVE_XINERAMA
146     add_integer ( "xvmc-xineramascreen", -1, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, true )
147 #endif
148     add_string( "xvmc-deinterlace-mode", "bob", NULL, MODE_TEXT, MODE_LONGTEXT, false )
149     add_string( "xvmc-crop-style", "eq", NULL, CROP_TEXT, CROP_LONGTEXT, false )
150
151     set_description( N_("XVMC extension video output") )
152     set_capability( "video output", 10 )
153     set_callbacks( Activate, Deactivate )
154 vlc_module_end ()
155
156 /* following functions are local */
157
158 static const unsigned accel_priority[] = {
159     VLC_XVMC_ACCEL_VLD,
160 };
161
162 #define NUM_ACCEL_PRIORITY (sizeof(accel_priority)/sizeof(accel_priority[0]))
163
164 /*
165  * Additional thread safety, since the plugin may decide to destroy a context
166  * while it's surfaces are still active in the video-out loop.
167  * When / If XvMC libs are reasonably thread-safe, the locks can be made
168  * more efficient by allowing multiple threads in that do not destroy
169  * the context or surfaces that may be active in other threads.
170  */
171
172 static void init_context_lock( context_lock_t *c )
173 {
174     pthread_cond_init(&c->cond,NULL);
175     pthread_mutex_init(&c->mutex,NULL);
176     c->num_readers = 0;
177 }
178
179 void free_context_lock( context_lock_t *c )
180 {
181     pthread_mutex_destroy(&c->mutex);
182     pthread_cond_destroy(&c->cond);
183 }
184
185 void xvmc_context_reader_lock( context_lock_t *c )
186 {
187     pthread_mutex_lock(&c->mutex);
188     c->num_readers++;
189     pthread_mutex_unlock(&c->mutex);
190 }
191
192 void xvmc_context_reader_unlock( context_lock_t *c )
193 {
194     pthread_mutex_lock(&c->mutex);
195     if (c->num_readers > 0) {
196         if (--(c->num_readers) == 0) {
197         pthread_cond_broadcast(&c->cond);
198         }
199     }
200     pthread_mutex_unlock(&c->mutex);
201 }
202
203 void xvmc_context_writer_lock( context_lock_t *c )
204 {
205     pthread_mutex_lock(&c->mutex);
206     while(c->num_readers) {
207         pthread_cond_wait( &c->cond, &c->mutex );
208     }
209 }
210
211 void xvmc_context_writer_unlock( context_lock_t *c )
212 {
213     pthread_mutex_unlock( &c->mutex );
214 }
215
216 void clear_xx44_palette( xx44_palette_t *p )
217 {
218     int i;
219     uint32_t *cluts = p->cluts;
220     int *ids = p->lookup_cache;
221
222     i= p->size;
223     while(i--)
224         *cluts++ = 0;
225     i = 2*OVL_PALETTE_SIZE;
226     while(i--)
227         *ids++ = -1;
228     p->max_used=1;
229 }
230
231 static void init_xx44_palette( xx44_palette_t *p, unsigned num_entries )
232 {
233     p->size = (num_entries > XX44_PALETTE_SIZE) ?
234                     XX44_PALETTE_SIZE : num_entries;
235 }
236
237 static void dispose_xx44_palette(xx44_palette_t *p)
238 {
239     /* Nothing to do */
240 }
241
242 static void colorToPalette( const uint32_t *icolor, unsigned char *palette_p,
243                             unsigned num_xvmc_components, char *xvmc_components )
244 {
245     const clut_t *color = (const clut_t *) icolor;
246     unsigned int i;
247
248     for (i=0; i<num_xvmc_components; ++i)
249     {
250         switch(xvmc_components[i])
251         {
252             case 'V': *palette_p = color->cr; break;
253             case 'U': *palette_p = color->cb; break;
254             case 'Y':
255             default:  *palette_p = color->y; break;
256         }
257         *palette_p++;
258     }
259 }
260
261
262 void xx44_to_xvmc_palette( const xx44_palette_t *p,unsigned char *xvmc_palette,
263                            unsigned first_xx44_entry, unsigned num_xx44_entries,
264                            unsigned num_xvmc_components, char *xvmc_components )
265 {
266     unsigned int i;
267     const uint32_t *cluts = p->cluts + first_xx44_entry;
268
269     for( i=0; i<num_xx44_entries; ++i )
270     {
271         if( (cluts - p->cluts) < p->size )
272         {
273             colorToPalette( cluts++, xvmc_palette,
274                             num_xvmc_components, xvmc_components );
275             xvmc_palette += num_xvmc_components;
276         }
277     }
278 }
279
280 static int xx44_paletteIndex( xx44_palette_t *p, int color, uint32_t clut )
281 {
282     unsigned int i;
283     uint32_t *cluts = p->cluts;
284     int tmp;
285
286     if( (tmp = p->lookup_cache[color]) >= 0 )
287     {
288         if (cluts[tmp] == clut)
289             return tmp;
290     }
291     for (i=0; i<p->max_used; ++i)
292     {
293         if (*cluts++ == clut) {
294             p->lookup_cache[color] = i;
295             return p->lookup_cache[color];
296         }
297     }
298
299     if( p->max_used == (p->size -1) )
300     {
301         //printf("video_out: Warning! Out of xx44 palette colors!\n");
302         return 1;
303     }
304     p->cluts[p->max_used] = clut;
305     p->lookup_cache[color] = p->max_used++;
306     return p->lookup_cache[color];
307 }
308
309 static void memblend_xx44( uint8_t *mem, uint8_t val,
310                            size_t size, uint8_t mask )
311 {
312     uint8_t masked_val = val & mask;
313
314 /* size_t is unsigned, therefore always positive
315    if (size < 0)
316         return;*/
317
318     while(size--)
319     {
320         if( (*mem & mask) <= masked_val )
321             *mem = val;
322         mem++;
323     }
324 }
325
326 void blend_xx44( uint8_t *dst_img, subpicture_t *sub_img,
327                  int dst_width, int dst_height, int dst_pitch,
328                  xx44_palette_t *palette, int ia44 )
329 {
330     int src_width;
331     int src_height;
332     int mask;
333     int x_off;
334     int y_off;
335     int x, y;
336     uint8_t norm_pixel,clip_pixel;
337     uint8_t *dst_y;
338     uint8_t *dst;
339     uint8_t alphamask;
340     int clip_right;
341     int i_len, i_color;
342     uint16_t *p_source = NULL;
343 #if 0
344     if (!sub_img)
345         return;
346
347     src_width  = sub_img->i_width;
348     src_height = sub_img->i_height;
349     x_off = sub_img->i_x;
350     y_off = sub_img->i_y;
351     alphamask = (ia44) ? 0x0F : 0xF0;
352     p_source = (uint16_t *)sub_img->p_sys->p_data;
353
354     dst_y = dst_img + dst_pitch*y_off + x_off;
355
356     if( (x_off + sub_img->i_width) <= dst_width )
357         clip_right = sub_img->i_width;
358     else
359         clip_right = dst_width - x_off;
360
361     if ((src_height + y_off) > dst_height)
362         src_height = dst_height - y_off;
363
364     for (y = 0; y < src_height; y++)
365     {
366         mask = !( (y < sub_img->p_sys->i_y_start) ||
367                   (y >= sub_img->p_sys->i_y_end) );
368         dst = dst_y;
369
370         for (x = 0; x < src_width;)
371         {
372             i_color = *p_source & 0x3;
373             i_len = *p_source++ >> 2;
374
375             if( (i_len > 0) && ((x+i_len) <= src_width) )
376             {
377                 /* Get the RLE part, then draw the line */
378                 uint32_t color = (sub_img->p_sys->pi_yuv[i_color][0] << 16) |
379                                  (sub_img->p_sys->pi_yuv[i_color][1] << 0) |
380                                  (sub_img->p_sys->pi_yuv[i_color][2] << 8);
381
382                 norm_pixel = (uint8_t)(
383                             (xx44_paletteIndex( palette,i_color, color ) << 4) |
384                             (sub_img->p_sys->pi_alpha[i_color] & 0x0F) );
385                 clip_pixel = (uint8_t)(
386                             (xx44_paletteIndex( palette,i_color + OVL_PALETTE_SIZE,
387                                                 sub_img->p_sys->pi_yuv[i_color][0] ) << 4) |
388                             (sub_img->p_sys->pi_alpha[i_color] & 0x0F));
389
390                 if( !ia44 )
391                 {
392                     norm_pixel = ((norm_pixel & 0x0F) << 4) | ((norm_pixel & 0xF0) >> 4);
393                     clip_pixel = ((clip_pixel & 0x0F) << 4) | ((clip_pixel & 0xF0) >> 4);
394                 }
395                 if( mask )
396                 {
397                     if( x < sub_img->p_sys->i_x_start )
398                     {
399                         if( (x + i_len) <= sub_img->p_sys->i_x_start )
400                         {
401                             memblend_xx44( dst, norm_pixel, i_len, alphamask );
402                             dst += i_len;
403                         }
404                         else
405                         {
406                             memblend_xx44( dst, norm_pixel,
407                                            sub_img->p_sys->i_x_start - x,
408                                            alphamask );
409                             dst += sub_img->p_sys->i_x_start - x;
410                             i_len -= sub_img->p_sys->i_x_start - x;
411                             if( i_len <= (sub_img->p_sys->i_x_end -
412                                           sub_img->p_sys->i_x_start) )
413                             {
414                                 memblend_xx44( dst, clip_pixel,
415                                                i_len, alphamask);
416                                 dst += i_len;
417                             }
418                             else
419                             {
420                                 memblend_xx44( dst, clip_pixel,
421                                                sub_img->p_sys->i_x_end -
422                                                     sub_img->p_sys->i_x_start,
423                                                alphamask );
424                                 dst += (sub_img->p_sys->i_x_end -
425                                         sub_img->p_sys->i_x_start);
426                                 i_len -= (sub_img->p_sys->i_x_end -
427                                           sub_img->p_sys->i_x_start);
428                                 memblend_xx44( dst, norm_pixel,
429                                                i_len, alphamask );
430                                 dst += i_len;
431                             }
432                         }
433                     }
434                     else if( x < sub_img->p_sys->i_x_end )
435                     {
436                         if( i_len <= (sub_img->p_sys->i_x_end - x) )
437                         {
438                             memblend_xx44( dst, clip_pixel, i_len, alphamask);
439                             dst += i_len;
440                         }
441                         else
442                         {
443                             memblend_xx44( dst, clip_pixel,
444                                            sub_img->p_sys->i_x_end - x,
445                                            alphamask);
446                             dst += (sub_img->p_sys->i_x_end - x);
447                             i_len -= (sub_img->p_sys->i_x_end - x);
448                             memblend_xx44( dst, norm_pixel, i_len, alphamask);
449                             dst += i_len;
450                         }
451                     }
452                     else
453                     {
454                         memblend_xx44( dst, norm_pixel, i_len, alphamask );
455                         dst += i_len;
456                     }
457                 }
458                 else
459                 {
460                     memblend_xx44( dst, norm_pixel, i_len, alphamask );
461                     dst += i_len;
462                 }
463             }
464             else
465             {
466                 return;
467             }
468             x += i_len;
469         }
470         dst_y += dst_pitch;
471     }
472 #endif
473 }
474
475 int xxmc_xvmc_surface_valid( vout_thread_t *p_vout, XvMCSurface *surf )
476 {
477     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
478     unsigned long index = surf - handler->surfaces;
479     int ret;
480
481     if( index >= XVMC_MAX_SURFACES )
482         return 0;
483     pthread_mutex_lock(&handler->mutex);
484     ret = handler->surfValid[index];
485     pthread_mutex_unlock(&handler->mutex);
486     return ret;
487 }
488
489 static void xxmc_xvmc_dump_subpictures( vout_thread_t *p_vout )
490 {
491     int i;
492     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
493
494     for( i=0; i < XVMC_MAX_SUBPICTURES; ++i )
495     {
496         msg_Dbg( p_vout, "handler in use %d, valid %d",
497                          handler->subInUse[i],
498                          handler->subValid[i]);
499     }
500 }
501
502 XvMCSubpicture *xxmc_xvmc_alloc_subpicture( vout_thread_t *p_vout,
503                     XvMCContext *context, unsigned short width,
504                     unsigned short height, int xvimage_id )
505 {
506     int i;
507     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
508     int status;
509
510     pthread_mutex_lock(&handler->mutex);
511     /* xxmc_xvmc_dump_subpictures(p_vout); */
512     for( i=0; i<XVMC_MAX_SUBPICTURES; ++i )
513     {
514         if( handler->subValid[i] && !handler->subInUse[i] )
515         {
516             XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
517             if( XvMCGetSubpictureStatus( p_vout->p_sys->p_display,
518                                          handler->subpictures + i,
519                                          &status ) )
520             {
521                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
522                 continue;
523             }
524             XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
525             if( status & XVMC_DISPLAYING )
526                 continue;
527             handler->subInUse[i] = 1;
528             /* xxmc_xvmc_dump_subpictures(p_vout); */
529             pthread_mutex_unlock(&handler->mutex);
530             return (handler->subpictures + i);
531         }
532     }
533     for (i=0; i<XVMC_MAX_SUBPICTURES; ++i)
534     {
535         if( !handler->subInUse[i] )
536         {
537             XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
538             if( Success != XvMCCreateSubpicture( p_vout->p_sys->p_display,
539                                                  context,
540                                                  handler->subpictures + i,
541                                                  width, height, xvimage_id ) )
542             {
543                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
544                 pthread_mutex_unlock( &handler->mutex );
545                 return NULL;
546             }
547             XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
548             msg_Dbg( p_vout, "video_out_xxmc: created subpicture %d", i );
549             handler->subInUse[i] = 1;
550             handler->subValid[i] = 1;
551             pthread_mutex_unlock( &handler->mutex );
552             return (handler->subpictures + i);
553         }
554     }
555     pthread_mutex_unlock( &handler->mutex );
556     return NULL;
557 }
558
559 void xxmc_xvmc_free_subpicture( vout_thread_t *p_vout, XvMCSubpicture *sub )
560 {
561     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
562     unsigned int index = sub - handler->subpictures;
563
564     if( index >= XVMC_MAX_SUBPICTURES )
565         return;
566
567     pthread_mutex_lock( &handler->mutex );
568     handler->subInUse[index] = 0;
569     /* xxmc_xvmc_dump_subpictures(p_vout); */
570     pthread_mutex_unlock( &handler->mutex );
571 }
572
573 static void xxmc_xvmc_surface_handler_construct( vout_thread_t *p_vout )
574 {
575     int i;
576     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
577  
578     pthread_mutex_init( &handler->mutex, NULL );
579     for( i=0; i<XVMC_MAX_SURFACES; ++i )
580     {
581         handler->surfInUse[i] = 0;
582         handler->surfValid[i] = 0;
583     }
584     for( i=0; i<XVMC_MAX_SUBPICTURES; ++i )
585     {
586         handler->subInUse[i] = 0;
587         handler->subValid[i] = 0;
588     }
589 }
590
591 static void xxmc_xvmc_dump_surfaces( vout_thread_t *p_vout )
592 {
593     int i;
594     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
595
596     for (i=0; i<XVMC_MAX_SURFACES; ++i)
597     {
598         msg_Dbg(p_vout, "surfaces in use %d, valid %d;",
599                         handler->surfInUse[i],
600                         handler->surfValid[i]);
601     }
602 }
603
604 void xxmc_xvmc_free_surface( vout_thread_t *p_vout, XvMCSurface *surf )
605 {
606     xvmc_surface_handler_t *handler = &p_vout->p_sys->xvmc_surf_handler;
607     unsigned int index = 0;
608
609     index = (surf - handler->surfaces);
610
611     if (index < XVMC_MAX_SURFACES)
612     {
613         pthread_mutex_lock(&handler->mutex);
614         msg_Dbg( p_vout,"free surface %d",index );
615         handler->surfInUse[index]--;
616         xxmc_xvmc_dump_surfaces(p_vout);
617         pthread_mutex_unlock(&handler->mutex);
618     }
619 }
620
621 int checkXvMCCap( vout_thread_t *p_vout )
622 {
623     int i_xvport = 0;
624     int numSurf = 0;
625     int numSub = 0;
626     int i,j;
627     XvMCSurfaceInfo     *surfaceInfo =NULL;
628     XvMCSurfaceInfo     *curInfo = NULL;
629     XvMCContext         c;
630     xvmc_capabilities_t *curCap = NULL;
631     XvImageFormatValues *formatValues = NULL;
632
633     i_xvport = p_vout->p_sys->i_xvport;
634     p_vout->p_sys->xvmc_cap = 0;
635
636     init_context_lock( &p_vout->p_sys->xvmc_lock );
637     xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock );
638
639     p_vout->p_sys->old_subpic = NULL;
640     p_vout->p_sys->new_subpic = NULL;
641     p_vout->p_sys->contextActive = 0;
642     p_vout->p_sys->subImage = NULL;
643     p_vout->p_sys->hwSubpictures = 0;
644     p_vout->p_sys->xvmc_palette = NULL;
645
646     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
647
648     if( !XvMCQueryExtension( p_vout->p_sys->p_display,
649                              &p_vout->p_sys->xvmc_eventbase,
650                              &p_vout->p_sys->xvmc_errbase ) )
651     {
652         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
653         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
654         return VLC_EGENERIC;
655     }
656     msg_Dbg( p_vout,"XvMC extension found" );
657
658     surfaceInfo = XvMCListSurfaceTypes(p_vout->p_sys->p_display, i_xvport, &numSurf);
659     if( !surfaceInfo )
660     {
661         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
662         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
663         return VLC_EGENERIC;
664     }
665
666     p_vout->p_sys->xvmc_cap =
667             (xvmc_capabilities_t *) malloc( numSurf *
668                                             sizeof(xvmc_capabilities_t) );
669     if( !p_vout->p_sys->xvmc_cap )
670     {
671         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
672         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
673         return VLC_EGENERIC;
674     }
675
676     p_vout->p_sys->xvmc_num_cap = numSurf;
677     curInfo = surfaceInfo;
678     curCap = p_vout->p_sys->xvmc_cap;
679
680     msg_Dbg( p_vout,"found %d XvMC surface types", numSurf );
681
682     for( i=0; i< numSurf; ++i )
683     {
684         curCap->mpeg_flags = 0;
685         curCap->accel_flags = 0;
686         if( curInfo->chroma_format == XVMC_CHROMA_FORMAT_420 )
687         {
688             curCap->mpeg_flags |= ((curInfo->mc_type & XVMC_MPEG_1) ?
689                                                 VLC_XVMC_MPEG_1 : 0);
690             curCap->mpeg_flags |= ((curInfo->mc_type & XVMC_MPEG_2) ?
691                                                 VLC_XVMC_MPEG_2 : 0);
692             curCap->mpeg_flags |= ((curInfo->mc_type & XVMC_MPEG_4) ?
693                                                 VLC_XVMC_MPEG_4 : 0);
694             curCap->accel_flags |= ((curInfo->mc_type & XVMC_VLD) ?
695                                               VLC_XVMC_ACCEL_VLD : 0);
696             curCap->accel_flags |= ((curInfo->mc_type & XVMC_IDCT) ?
697                                              VLC_XVMC_ACCEL_IDCT : 0);
698             curCap->accel_flags |= ((curInfo->mc_type & (XVMC_VLD | XVMC_IDCT)) ?
699                                             0 : VLC_XVMC_ACCEL_MOCOMP);
700             curCap->max_width = curInfo->max_width;
701             curCap->max_height = curInfo->max_height;
702             curCap->sub_max_width = curInfo->subpicture_max_width;
703             curCap->sub_max_height = curInfo->subpicture_max_height;
704             curCap->flags = curInfo->flags;
705
706             msg_Dbg (p_vout, "surface type %d: Max size: %d %d.",
707                             i, curCap->max_width, curCap->max_height);
708             msg_Dbg (p_vout, "surface subtype %d: Max subpic size: %d %d.",
709                             i, curCap->sub_max_width, curCap->sub_max_height);
710
711             curCap->type_id = curInfo->surface_type_id;
712             formatValues = XvMCListSubpictureTypes( p_vout->p_sys->p_display,
713                                                     i_xvport,
714                                                     curCap->type_id,
715                                                     &numSub );
716             curCap->subPicType.id = 0;
717             if( formatValues )
718             {
719                 msg_Dbg( p_vout, "surface type %d: found %d XvMC subpicture types",
720                                 i, numSub);
721                 for( j = 0; j<numSub; ++j )
722                 {
723                     if( formatValues[j].id == FOURCC_IA44 )
724                     {
725                         curCap->subPicType = formatValues[j];
726                         msg_Dbg( p_vout,
727                                     "surface type %d: detected and using "
728                                     "IA44 subpicture type.", i );
729                         /* Prefer IA44 */
730                         break;
731                     }
732                     else if( formatValues[j].id == FOURCC_AI44 )
733                     {
734                         curCap->subPicType = formatValues[j];
735                         msg_Dbg( p_vout,
736                                  "surface type %d: detected AI44 "
737                                  "subpicture type.", i );
738                     }
739                 }
740             }
741             XFree(formatValues);
742             curInfo++;
743             curCap++;
744         }
745     }
746     XFree(surfaceInfo);
747
748     /*
749      * Try to create a direct rendering context. This will fail if we are not
750      * on the displaying computer or an indirect context is not available.
751      */
752     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
753     curCap = p_vout->p_sys->xvmc_cap;
754     if( Success == XvMCCreateContext( p_vout->p_sys->p_display, i_xvport,
755                                       curCap->type_id,
756                                       curCap->max_width,
757                                       curCap->max_height,
758                                       XVMC_DIRECT, &c ) )
759     {
760         msg_Dbg( p_vout, "using direct XVMC rendering context" );
761         p_vout->p_sys->context_flags = XVMC_DIRECT;
762     }
763     else if( Success == XvMCCreateContext( p_vout->p_sys->p_display, i_xvport,
764                                            curCap->type_id,
765                                            curCap->max_width,
766                                            curCap->max_height,
767                                            0, &c ) )
768     {
769         msg_Dbg( p_vout, "using default XVMC rendering context" );
770         p_vout->p_sys->context_flags = 0;
771     }
772     else
773     {
774         free( p_vout->p_sys->xvmc_cap );
775         p_vout->p_sys->xvmc_cap = NULL;
776         msg_Err( p_vout, "use of direct XvMC context on a remote display failed"
777                          " falling back to XV." );
778         xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
779         return VLC_SUCCESS;
780     }
781     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
782     XvMCDestroyContext( p_vout->p_sys->p_display, &c );
783     xxmc_xvmc_surface_handler_construct( p_vout );
784     /*  p_vout->p_sys->capabilities |= VO_CAP_XXMC; */
785     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
786     init_xx44_palette( &p_vout->p_sys->palette , 0 );
787     p_vout->p_sys->last_accel_request = 0xFFFFFFFF;
788     xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock );
789     return VLC_SUCCESS;
790 }
791
792 static int xxmc_setup_subpictures( vout_thread_t *p_vout,
793         unsigned int width, unsigned int height )
794 {
795     xvmc_capabilities_t *curCap = NULL;
796     XvMCSubpicture *sp = NULL;
797
798     if( p_vout->p_sys->contextActive )
799     {
800         curCap = p_vout->p_sys->xvmc_cap + p_vout->p_sys->xvmc_cur_cap;
801
802         if( (width > curCap->sub_max_width) ||
803             (height > curCap->sub_max_height) )
804             return VLC_EGENERIC;
805
806         if( (p_vout->p_sys->xvmc_backend_subpic =
807                 (curCap->flags & XVMC_BACKEND_SUBPICTURE)) )
808             msg_Dbg( p_vout, "using backend subpictures." );
809
810         if (!p_vout->p_sys->subImage)
811         {
812             XLockDisplay( p_vout->p_sys->p_display );
813             msg_Dbg(p_vout, "xxmc_setup_subpictures");
814 #ifdef HAVE_SYS_SHM_H
815             if( p_vout->p_sys->i_shm_opcode )
816             {
817                 /* Create image using XShm extension */
818                 p_vout->p_sys->subImage = CreateShmImage( p_vout,
819                                             p_vout->p_sys->p_display,
820                                             p_vout->p_sys->i_xvport,
821                                             curCap->subPicType.id,
822                                             /* VLC2X11_FOURCC( p_vout->output. i_chroma ), */
823                                             &p_vout->p_sys->subShmInfo,
824                                             p_vout->output.i_width,
825                                             p_vout->output.i_height );
826             }
827 #endif /* HAVE_SYS_SHM_H */
828             XUnlockDisplay( p_vout->p_sys->p_display );
829             if( !p_vout->p_sys->subImage )
830             {
831                 msg_Dbg(p_vout, "failed allocating XvImage for supbictures" );
832                 return VLC_EGENERIC;
833             }
834         }
835
836         sp = xxmc_xvmc_alloc_subpicture( p_vout, &p_vout->p_sys->context,
837                                          width, height,
838                                          curCap->subPicType.id );
839         if( sp )
840         {
841             init_xx44_palette( &p_vout->p_sys->palette, sp->num_palette_entries );
842             p_vout->p_sys->xvmc_palette = (char *) malloc( sp->num_palette_entries
843                     * sp->entry_bytes );
844             xxmc_xvmc_free_subpicture( p_vout, sp);
845             if( !p_vout->p_sys->xvmc_palette )
846                 return VLC_EGENERIC;
847             p_vout->p_sys->hwSubpictures = 1;
848         }
849     }
850     return VLC_SUCCESS;
851 }
852
853 static void xvmc_check_colorkey_properties( vout_thread_t *p_vout )
854 {
855     int num,i;
856     XvAttribute *xvmc_attributes = NULL;
857     Atom ap;
858
859     /*
860     * Determine if the context is of "Overlay" type. If so,
861     * check whether we can autopaint.
862     */
863     p_vout->p_sys->have_xvmc_autopaint = 0;
864     if( p_vout->p_sys->context_flags & XVMC_OVERLAID_SURFACE )
865     {
866         msg_Dbg( p_vout, "check colorkey properties" );
867         XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
868         xvmc_attributes = XvMCQueryAttributes( p_vout->p_sys->p_display,
869                                                &p_vout->p_sys->context,
870                                                &num );
871         if( xvmc_attributes )
872         {
873             for( i = 0; i < num; ++i )
874             {
875                 if( strncmp( "XV_AUTOPAINT_COLORKEY",
876                              xvmc_attributes[i].name,
877                              21) == 0)
878                 {
879                     ap = XInternAtom( p_vout->p_sys->p_display,
880                                       "XV_AUTOPAINT_COLORKEY",
881                                        False );
882                     XvMCSetAttribute( p_vout->p_sys->p_display,
883                                       &p_vout->p_sys->context,
884                                       ap,
885                                       1 ); /* p_vout->p_sys->props[VO_PROP_AUTOPAINT_COLORKEY].value */
886                     p_vout->p_sys->have_xvmc_autopaint = 1;
887                     msg_Dbg( p_vout, "has xvmc autopaint" );
888                 }
889             }
890         }
891         XFree( xvmc_attributes );
892         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
893         /* p_vout->p_sys->xvmc_xoverlay_type = X11OSD_COLORKEY; */
894     }
895 #if 0
896     else
897     {
898         p_vout->p_sys->xvmc_xoverlay_type = X11OSD_SHAPED;
899     }
900 #endif
901 }
902
903 static void xxmc_xvmc_destroy_surfaces( vout_thread_t *p_vout )
904 {
905     int i;
906     xvmc_surface_handler_t *handler = NULL;
907
908     handler = &p_vout->p_sys->xvmc_surf_handler;
909
910     pthread_mutex_lock( &handler->mutex );
911     for( i = 0; i < XVMC_MAX_SURFACES; ++i )
912     {
913         XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
914         if( handler->surfValid[i] )
915         {
916             XvMCFlushSurface( p_vout->p_sys->p_display , handler->surfaces+i);
917             XvMCSyncSurface( p_vout->p_sys->p_display, handler->surfaces+i );
918             XvMCHideSurface( p_vout->p_sys->p_display, handler->surfaces+i );
919             XvMCDestroySurface( p_vout->p_sys->p_display, handler->surfaces+i );
920         }
921         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
922         handler->surfValid[i] = 0;
923     }
924     pthread_mutex_unlock( &handler->mutex );
925 }
926
927 static void xxmc_xvmc_destroy_subpictures( vout_thread_t *p_vout )
928 {
929     int i;
930     xvmc_surface_handler_t *handler = NULL;
931
932     handler = &p_vout->p_sys->xvmc_surf_handler;
933
934     pthread_mutex_lock( &handler->mutex );
935     for( i = 0; i < XVMC_MAX_SUBPICTURES; ++i )
936     {
937         XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
938         if( handler->subValid[i] )
939         {
940             XvMCFlushSubpicture( p_vout->p_sys->p_display , handler->subpictures+i);
941             XvMCSyncSubpicture( p_vout->p_sys->p_display, handler->subpictures+i );
942             XvMCDestroySubpicture( p_vout->p_sys->p_display, handler->subpictures+i );
943         }
944         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
945         handler->subValid[i] = 0;
946     }
947     pthread_mutex_unlock( &handler->mutex );
948 }
949
950 static XvMCSurface *xxmc_xvmc_alloc_surface( vout_thread_t *p_vout,
951         XvMCContext *context )
952 {
953     xvmc_surface_handler_t *handler = NULL;
954     int i;
955
956     handler = &p_vout->p_sys->xvmc_surf_handler;
957
958     pthread_mutex_lock( &handler->mutex );
959     xxmc_xvmc_dump_surfaces( p_vout );
960     for( i = 0; i < XVMC_MAX_SURFACES; ++i )
961     {
962         if( handler->surfValid[i] && !handler->surfInUse[i] )
963         {
964             handler->surfInUse[i] = 1;
965             msg_Dbg( p_vout, "reusing surface %d", i );
966             xxmc_xvmc_dump_surfaces( p_vout );
967             pthread_mutex_unlock( &handler->mutex );
968             return (handler->surfaces + i);
969         }
970     }
971     for( i = 0; i < XVMC_MAX_SURFACES; ++i )
972     {
973         if( !handler->surfInUse[i] )
974         {
975             XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
976             if( Success != XvMCCreateSurface( p_vout->p_sys->p_display,
977                                               context,
978                                               handler->surfaces + i) )
979             {
980                 XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
981                 pthread_mutex_unlock( &handler->mutex );
982                 return NULL;
983             }
984             XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
985
986             msg_Dbg( p_vout, "created surface %d", i );
987             handler->surfInUse[i] = 1;
988             handler->surfValid[i] = 1;
989             pthread_mutex_unlock( &handler->mutex );
990             return (handler->surfaces + i);
991         }
992     }
993     pthread_mutex_unlock( &handler->mutex );
994     return NULL;
995 }
996
997 void xxmc_dispose_context( vout_thread_t *p_vout )
998 {
999     if( p_vout->p_sys->contextActive )
1000     {
1001         if( p_vout->p_sys->xvmc_accel &
1002             (VLC_XVMC_ACCEL_MOCOMP | VLC_XVMC_ACCEL_IDCT) )
1003         {
1004             xvmc_macroblocks_t *macroblocks = NULL;
1005
1006             macroblocks = &p_vout->p_sys->macroblocks;
1007             XvMCDestroyMacroBlocks( p_vout->p_sys->p_display,
1008                                     &macroblocks->macro_blocks );
1009             XvMCDestroyBlocks( p_vout->p_sys->p_display,
1010                                &macroblocks->blocks );
1011         }
1012
1013         msg_Dbg( p_vout, "freeing up XvMC surfaces and subpictures" );
1014         free( p_vout->p_sys->xvmc_palette );
1015         dispose_xx44_palette( &p_vout->p_sys->palette );
1016         xxmc_xvmc_destroy_subpictures( p_vout );
1017         xxmc_xvmc_destroy_surfaces( p_vout );
1018
1019         msg_Dbg(p_vout, "freeing up XvMC Context.");
1020         XLockDisplay( p_vout->p_sys->p_display );
1021         if( p_vout->p_sys->subImage )
1022         {
1023             XFree( p_vout->p_sys->subImage );
1024             p_vout->p_sys->subImage = NULL;
1025         }
1026         p_vout->p_sys->subImage = NULL;
1027         XUnlockDisplay( p_vout->p_sys->p_display );
1028         XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1029         XvMCDestroyContext( p_vout->p_sys->p_display,
1030                             &p_vout->p_sys->context );
1031         XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1032         p_vout->p_sys->contextActive = 0;
1033         p_vout->p_sys->hwSubpictures = 0;
1034         p_vout->p_sys->xvmc_accel = 0;
1035     }
1036 }
1037
1038 static int xxmc_find_context( vout_thread_t *p_vout, vlc_xxmc_t *xxmc,
1039         unsigned int width, unsigned int height )
1040 {
1041     unsigned int i, k;
1042     bool found = false;
1043     xvmc_capabilities_t *curCap = NULL;
1044     unsigned int request_mpeg_flags, request_accel_flags;
1045
1046     request_mpeg_flags = xxmc->mpeg;
1047     for( k = 0; k < NUM_ACCEL_PRIORITY; ++k )
1048     {
1049         request_accel_flags = xxmc->acceleration & accel_priority[k];
1050         if( !request_accel_flags )
1051             continue;
1052
1053         curCap = p_vout->p_sys->xvmc_cap;
1054         for( i =0; i < p_vout->p_sys->xvmc_num_cap; ++i )
1055         {
1056             msg_Dbg( p_vout, "surface type %d, capabilities 0x%8x 0x%8x",
1057                              i,
1058                              curCap->mpeg_flags,
1059                              curCap->accel_flags );
1060             msg_Dbg( p_vout, "fequests: 0x%8x 0x%8x",
1061                              request_mpeg_flags,
1062                              request_accel_flags );
1063             if( ( (curCap->mpeg_flags & request_mpeg_flags) == request_mpeg_flags) &&
1064                   (curCap->accel_flags & request_accel_flags) &&
1065                   (width <= curCap->max_width) &&
1066                   (height <= curCap->max_height) )
1067             {
1068                 found = true;
1069                 break;
1070             }
1071             curCap++;
1072         }
1073         if( found )
1074         {
1075             p_vout->p_sys->xvmc_cur_cap = i;
1076             break;
1077         }
1078     }
1079     if( found )
1080     {
1081         p_vout->p_sys->xvmc_accel = request_accel_flags;
1082         p_vout->p_sys->unsigned_intra = (curCap->flags & XVMC_INTRA_UNSIGNED);
1083         return 1;
1084     }
1085     p_vout->p_sys->xvmc_accel = 0;
1086     return 0;
1087 }
1088
1089 static int xxmc_create_context( vout_thread_t *p_vout,
1090         unsigned int width, unsigned int height )
1091 {
1092     xvmc_capabilities_t *curCap = NULL;
1093
1094     curCap = p_vout->p_sys->xvmc_cap + p_vout->p_sys->xvmc_cur_cap;
1095
1096     msg_Dbg( p_vout, "creating new XvMC context %d", curCap->type_id );
1097
1098     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1099     if( Success == XvMCCreateContext( p_vout->p_sys->p_display,
1100                                       p_vout->p_sys->i_xvport,
1101                                       curCap->type_id,
1102                                       width,
1103                                       height,
1104                                       p_vout->p_sys->context_flags,
1105                                       &p_vout->p_sys->context ) )
1106     {
1107         p_vout->p_sys->xvmc_mpeg = curCap->mpeg_flags;
1108         p_vout->p_sys->xvmc_width = width;
1109         p_vout->p_sys->xvmc_height = height;
1110         p_vout->p_sys->contextActive = 1;
1111     }
1112     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1113     return p_vout->p_sys->contextActive;
1114 }
1115
1116 static void xvmc_flushsync(picture_t *picture)
1117 {
1118     vout_thread_t *p_vout = picture->p_sys->p_vout;
1119
1120     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1121
1122     if( !xxmc_xvmc_surface_valid( p_vout, picture->p_sys->xvmc_surf ) )
1123     {
1124         msg_Dbg(p_vout, "xvmc_flushsync 1 : %d", picture->p_sys->xxmc_data.result );
1125         picture->p_sys->xxmc_data.result = 128;
1126         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1127         return;
1128     }
1129
1130     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1131     picture->p_sys->xxmc_data.result =
1132             XvMCFlushSurface( p_vout->p_sys->p_display,
1133                               picture->p_sys->xvmc_surf );
1134     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1135     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1136 }
1137
1138 static void xvmc_flush(picture_t *picture)
1139 {
1140     vout_thread_t *p_vout = picture->p_sys->p_vout;
1141
1142     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1143
1144     if ( !xxmc_xvmc_surface_valid( p_vout, picture->p_sys->xvmc_surf ) )
1145     {
1146         msg_Dbg(p_vout, "xvmc flush 1 : %d", picture->p_sys->xxmc_data.result );
1147         picture->p_sys->xxmc_data.result = 128;
1148         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1149         return;
1150     }
1151
1152     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1153     picture->p_sys->xxmc_data.result =
1154             XvMCFlushSurface( p_vout->p_sys->p_display,
1155                               picture->p_sys->xvmc_surf );
1156     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1157     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1158 }
1159
1160 static int xxmc_frame_updates( vout_thread_t *p_vout, picture_t *picture )
1161 {
1162     vlc_xxmc_t *xxmc = &picture->p_sys->xxmc_data;
1163
1164     /*
1165      * If we have changed context since the surface was updated, xvmc_surf
1166      * is either NULL or invalid. If it is invalid. Set it to NULL.
1167      * Also if there are other users of this surface, deregister our use of
1168      * it and later try to allocate a new, fresh one.
1169      */
1170
1171     if( picture->p_sys->xvmc_surf )
1172     {
1173         if( !xxmc_xvmc_surface_valid( p_vout, picture->p_sys->xvmc_surf ) )
1174         {
1175             xxmc_xvmc_free_surface( p_vout , picture->p_sys->xvmc_surf );
1176             picture->p_sys->xvmc_surf = NULL;
1177         }
1178     }
1179 #if 0
1180     if( picture->p_sys->p_image )
1181     {
1182         memset( picture->p_sys->p_image->data, 0,
1183                 picture->p_sys->p_image->width
1184                     * picture->p_sys->p_image->height );
1185     }
1186 #endif
1187     /*
1188      * If it is NULL create a new surface.
1189      */
1190     if( !picture->p_sys->xvmc_surf )
1191     {
1192         picture->p_sys->xvmc_surf = xxmc_xvmc_alloc_surface( p_vout,
1193                                                     &p_vout->p_sys->context );
1194         if( !picture->p_sys->xvmc_surf )
1195         {
1196             msg_Err( p_vout, "accelerated surface allocation failed.\n"
1197                             " You are probably out of framebuffer memory.\n"
1198                             " Falling back to software decoding." );
1199             p_vout->p_sys->xvmc_accel = 0;
1200             xxmc_dispose_context( p_vout );
1201             return VLC_EGENERIC;
1202         }
1203     }
1204     xxmc->acceleration = p_vout->p_sys->xvmc_accel;
1205
1206     xxmc->proc_xxmc_flush = xvmc_flush;
1207     xxmc->proc_xxmc_flushsync = xvmc_flushsync;
1208     xxmc->xvmc.proc_macro_block = NULL;
1209 #if 0
1210     frame->vo_frame.proc_duplicate_frame_data = xxmc_duplicate_frame_data;
1211 #endif
1212     xxmc->proc_xxmc_begin = xvmc_vld_frame;
1213     xxmc->proc_xxmc_slice = xvmc_vld_slice;
1214     return VLC_SUCCESS;
1215 }
1216
1217 static int xxmc_xvmc_update_context( vout_thread_t *p_vout,
1218     picture_t *picture, uint32_t width, uint32_t height )
1219 {
1220     vlc_xxmc_t *xxmc = &picture->p_sys->xxmc_data;
1221
1222     /*
1223      * Are we at all capable of doing XvMC ?
1224      */
1225     if( p_vout->p_sys->xvmc_cap == 0 )
1226         return VLC_EGENERIC;
1227
1228     msg_Dbg( p_vout, "new format: need to change XvMC context. "
1229                      "width: %d height: %d mpeg: %d acceleration: %d",
1230                      width, height,
1231                      xxmc->mpeg, xxmc->acceleration );
1232
1233     if( picture->p_sys->xvmc_surf )
1234         xxmc_xvmc_free_surface( p_vout , picture->p_sys->xvmc_surf );
1235     picture->p_sys->xvmc_surf = NULL;
1236
1237     xxmc_dispose_context( p_vout );
1238
1239     if( xxmc_find_context( p_vout, xxmc, width, height ) )
1240     {
1241         xxmc_create_context( p_vout, width, height);
1242         xvmc_check_colorkey_properties( p_vout );
1243         xxmc_setup_subpictures(p_vout, width, height);
1244     }
1245
1246     if( !p_vout->p_sys->contextActive )
1247     {
1248         msg_Dbg( p_vout, "using software decoding for this stream" );
1249         p_vout->p_sys->xvmc_accel = 0;
1250     }
1251     else
1252     {
1253         msg_Dbg(p_vout, "using hardware decoding for this stream." );
1254     }
1255
1256     p_vout->p_sys->xvmc_mpeg = xxmc->mpeg;
1257     p_vout->p_sys->xvmc_width = width;
1258     p_vout->p_sys->xvmc_height = height;
1259     return p_vout->p_sys->contextActive;
1260 }
1261
1262
1263 void xxmc_do_update_frame( picture_t *picture, uint32_t width, uint32_t height,
1264         double ratio, int format, int flags)
1265 {
1266     vout_thread_t *p_vout = picture->p_sys->p_vout;
1267     int indextime = 0;
1268     int status = 0;
1269
1270     picture->p_sys->xxmc_data.decoded = 0;
1271     picture->p_sys->nb_display = 0;
1272     picture->b_force = 0;
1273     vlc_xxmc_t *xxmc = &picture->p_sys->xxmc_data;
1274
1275     xvmc_context_writer_lock( &p_vout->p_sys->xvmc_lock);
1276     if( (p_vout->p_sys->last_accel_request != xxmc->acceleration) ||
1277         (p_vout->p_sys->xvmc_mpeg != xxmc->mpeg) ||
1278         (p_vout->p_sys->xvmc_width != width) ||
1279         (p_vout->p_sys->xvmc_height != height))
1280     {
1281         p_vout->p_sys->last_accel_request = xxmc->acceleration;
1282         xxmc_xvmc_update_context( p_vout, picture, width, height );
1283     }
1284
1285     if( p_vout->p_sys->contextActive )
1286         xxmc_frame_updates( p_vout, picture );
1287
1288     if( !p_vout->p_sys->contextActive )
1289     {
1290         xxmc->acceleration = 0;
1291         xxmc->xvmc.macroblocks = 0;
1292     }
1293     else
1294     {
1295         picture->format.i_chroma = format;
1296     }
1297     xvmc_context_writer_unlock( &p_vout->p_sys->xvmc_lock);
1298
1299     XvMCGetSurfaceStatus( p_vout->p_sys->p_display,
1300                           picture->p_sys->xvmc_surf,
1301                           &status );
1302     /* Wait a little till frame is being displayed */
1303     while( status & XVMC_DISPLAYING )
1304     {
1305         /* msleep(1); */
1306
1307         XvMCGetSurfaceStatus( p_vout->p_sys->p_display,
1308                               picture->p_sys->xvmc_surf,
1309                               &status );
1310
1311         indextime++;
1312         if( indextime > 4 )
1313             break;
1314     }
1315 }
1316
1317 #if 0
1318 /* called xlocked */
1319 static void dispose_ximage( vout_thread_t *p_vout, XShmSegmentInfo *shminfo,
1320                 XvImage *myimage )
1321 {
1322 # ifdef HAVE_SYS_SHM_H
1323     if( p_vout->p_sys->i_shm_opcode )
1324     {
1325         XShmDetach( p_vout->p_sys->p_display, shminfo );
1326         XFree( myimage );
1327         shmdt( shminfo->shmaddr );
1328         if( shminfo->shmid >= 0 )
1329         {
1330             shmctl( shminfo->shmid, IPC_RMID, 0 );
1331             shminfo->shmid = -1;
1332         }
1333     }
1334     else
1335 #endif
1336     {
1337         if( myimage->data )
1338             free(myimage->data);
1339         XFree (myimage);
1340     }
1341 }
1342 #endif
1343
1344 void xvmc_vld_frame( picture_t *picture )
1345 {
1346     picture_sys_t *p_sys  = picture->p_sys;
1347     vout_thread_t *p_vout = p_sys->p_vout;
1348     vlc_vld_frame_t *vft  = &(p_sys->xxmc_data.vld_frame);
1349     picture_t *ff         = (picture_t *) vft->forward_reference_picture;
1350     picture_t *bf         = (picture_t *) vft->backward_reference_picture;
1351     XvMCMpegControl ctl;
1352     XvMCSurface *fs=0, *bs=0;
1353     XvMCQMatrix qmx;
1354
1355     ctl.BHMV_range = vft->mv_ranges[0][0];
1356     ctl.BVMV_range = vft->mv_ranges[0][1];
1357     ctl.FHMV_range = vft->mv_ranges[1][0];
1358     ctl.FVMV_range = vft->mv_ranges[1][1];
1359     ctl.picture_structure = vft->picture_structure;
1360     ctl.intra_dc_precision = vft->intra_dc_precision;
1361     ctl.picture_coding_type = vft->picture_coding_type;
1362     ctl.mpeg_coding = (vft->mpeg_coding == 0) ? XVMC_MPEG_1 : XVMC_MPEG_2;
1363     ctl.flags = 0;
1364     ctl.flags |= (vft->progressive_sequence) ? XVMC_PROGRESSIVE_SEQUENCE : 0;
1365     ctl.flags |= (vft->scan) ? XVMC_ALTERNATE_SCAN : XVMC_ZIG_ZAG_SCAN;
1366     ctl.flags |= (vft->pred_dct_frame) ?
1367                     XVMC_PRED_DCT_FRAME : XVMC_PRED_DCT_FIELD;
1368     ctl.flags |= (picture->b_top_field_first) ?
1369                     XVMC_TOP_FIELD_FIRST : XVMC_BOTTOM_FIELD_FIRST;
1370     ctl.flags |= (vft->concealment_motion_vectors) ?
1371                     XVMC_CONCEALMENT_MOTION_VECTORS : 0;
1372     ctl.flags |= (vft->q_scale_type) ? XVMC_Q_SCALE_TYPE : 0;
1373     ctl.flags |= (vft->intra_vlc_format) ? XVMC_INTRA_VLC_FORMAT : 0;
1374     ctl.flags |= (vft->second_field) ? XVMC_SECOND_FIELD : 0;
1375
1376     if( ff )
1377         fs = ff->p_sys->xvmc_surf;
1378     if( bf )
1379         bs = bf->p_sys->xvmc_surf;
1380
1381     /*
1382      * Below is for interlaced streams and second_field.
1383      */
1384     if( ctl.picture_coding_type == P_TYPE ) /* XVMC_P_PICTURE) */
1385         bs = picture->p_sys->xvmc_surf;
1386
1387     if( (qmx.load_intra_quantiser_matrix = vft->load_intra_quantizer_matrix) )
1388     {
1389         memcpy( qmx.intra_quantiser_matrix, vft->intra_quantizer_matrix,
1390                 sizeof(qmx.intra_quantiser_matrix) );
1391     }
1392     if( (qmx.load_non_intra_quantiser_matrix =
1393                 vft->load_non_intra_quantizer_matrix) )
1394     {
1395         memcpy( qmx.non_intra_quantiser_matrix, vft->non_intra_quantizer_matrix,
1396                sizeof(qmx.non_intra_quantiser_matrix) );
1397     }
1398     qmx.load_chroma_intra_quantiser_matrix = 0;
1399     qmx.load_chroma_non_intra_quantiser_matrix = 0;
1400     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1401
1402     if( !xxmc_xvmc_surface_valid( p_vout, picture->p_sys->xvmc_surf ) )
1403     {
1404         picture->p_sys->xxmc_data.result = 128;
1405         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1406         return;
1407     }
1408
1409     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1410     XvMCLoadQMatrix( p_vout->p_sys->p_display, &p_vout->p_sys->context, &qmx );
1411     do {
1412         picture->p_sys->xxmc_data.result =
1413                 XvMCBeginSurface( p_vout->p_sys->p_display,
1414                                   &p_vout->p_sys->context,
1415                                   picture->p_sys->xvmc_surf,
1416                                   fs, bs, &ctl );
1417     } while( !picture->p_sys->xxmc_data.result );
1418     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1419     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1420 }
1421
1422 void xvmc_vld_slice( picture_t *picture )
1423 {
1424     picture_sys_t *p_sys  = picture->p_sys;
1425     vout_thread_t *p_vout = p_sys->p_vout;
1426
1427     xvmc_context_reader_lock( &p_vout->p_sys->xvmc_lock );
1428     if( !xxmc_xvmc_surface_valid( p_vout, picture->p_sys->xvmc_surf ) )
1429     {
1430         picture->p_sys->xxmc_data.result = 128;
1431         xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1432         msg_Err(p_vout, "vld slice error" );
1433         return;
1434     }
1435
1436     XVMCLOCKDISPLAY( p_vout->p_sys->p_display );
1437     picture->p_sys->xxmc_data.result =
1438             XvMCPutSlice2( p_vout->p_sys->p_display,
1439                            &p_vout->p_sys->context,
1440                             (char *)picture->p_sys->xxmc_data.slice_data,
1441                             picture->p_sys->xxmc_data.slice_data_size,
1442                             picture->p_sys->xxmc_data.slice_code );
1443
1444     if( picture->p_sys->xxmc_data.result != 0 )
1445         msg_Err( p_vout, "vlc slice error %d",
1446                  picture->p_sys->xxmc_data.result );
1447     /*
1448      * If CPU-saving mode is enabled, sleep after every xxmc->sleep slice. This will free
1449      * up the cpu while the decoder is working on the slice. The value of xxmc->sleep is calculated
1450      * so that the decoder thread sleeps at most 50% of the frame delay,
1451      * assuming a 2.6 kernel clock of 1000 Hz.
1452      */
1453     XVMCUNLOCKDISPLAY( p_vout->p_sys->p_display );
1454     xvmc_context_reader_unlock( &p_vout->p_sys->xvmc_lock );
1455 #if 0
1456     if( p_vout->p_sys->cpu_save_enabled )
1457     {
1458         p_vout->p_sys->cpu_saver += 1.;
1459         if( p_vout->p_sys->cpu_saver >= picture->p_sys->xxmc_data.sleep )
1460         {
1461             usleep(1);
1462             p_vout->p_sys->cpu_saver -= picture->p_sys->xxmc_data.sleep;
1463         }
1464     }
1465 #endif
1466 }