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