]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/vaapi.c
Compilation fix.
[vlc] / modules / codec / avcodec / vaapi.c
1 /*****************************************************************************
2  * vaapi.c: VAAPI helpers for the ffmpeg decoder
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir_AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_fourcc.h>
30 #include <assert.h>
31
32 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
33 #   include <libavcodec/avcodec.h>
34 #elif defined(HAVE_FFMPEG_AVCODEC_H)
35 #   include <ffmpeg/avcodec.h>
36 #else
37 #   include <avcodec.h>
38 #endif
39
40 #include "avcodec.h"
41 #include "va.h"
42 #include "copy.h"
43
44 #ifdef HAVE_AVCODEC_VAAPI
45
46 #include <libavcodec/vaapi.h>
47
48 #include <X11/Xlib.h>
49 #include <va/va_x11.h>
50
51 typedef struct
52 {
53     VASurfaceID  i_id;
54     int          i_refcount;
55     unsigned int i_order;
56
57 } vlc_va_surface_t;
58
59 typedef struct
60 {
61     vlc_va_t     va;
62
63     /* */
64     Display      *p_display_x11;
65     VADisplay     p_display;
66
67     VAConfigID    i_config_id;
68     VAContextID   i_context_id;
69
70     struct vaapi_context hw_ctx;
71
72     /* */
73     int i_version_major;
74     int i_version_minor;
75
76     /* */
77     int          i_surface_count;
78     unsigned int i_surface_order;
79     int          i_surface_width;
80     int          i_surface_height;
81     vlc_fourcc_t i_surface_chroma;
82
83     vlc_va_surface_t *p_surface;
84
85     VAImage      image;
86     copy_cache_t image_cache;
87
88 } vlc_va_vaapi_t;
89
90 static vlc_va_vaapi_t *vlc_va_vaapi_Get( void *p_va )
91 {
92     return p_va;
93 }
94
95 /* */
96 static int Open( vlc_va_vaapi_t *p_va, int i_codec_id )
97 {
98     VAProfile i_profile;
99     int i_surface_count;
100
101     /* */
102     switch( i_codec_id )
103     {
104     case CODEC_ID_MPEG1VIDEO:
105     case CODEC_ID_MPEG2VIDEO:
106         i_profile = VAProfileMPEG2Main;
107         i_surface_count = 2+1;
108         break;
109     case CODEC_ID_MPEG4:
110         i_profile = VAProfileMPEG4AdvancedSimple;
111         i_surface_count = 2+1;
112         break;
113     case CODEC_ID_WMV3:
114         i_profile = VAProfileVC1Main;
115         i_surface_count = 2+1;
116         break;
117     case CODEC_ID_VC1:
118         i_profile = VAProfileVC1Advanced;
119         i_surface_count = 2+1;
120         break;
121     case CODEC_ID_H264:
122         i_profile = VAProfileH264High;
123         i_surface_count = 16+1;
124         break;
125     default:
126         return VLC_EGENERIC;
127     }
128
129     /* */
130     memset( p_va, 0, sizeof(*p_va) );
131
132     /* Create a VA display */
133     if( !XInitThreads() )
134         return VLC_EGENERIC;
135
136     p_va->p_display_x11 = XOpenDisplay(NULL);
137     if( !p_va->p_display_x11 )
138         goto error;
139
140     p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
141     if( !p_va->p_display )
142         goto error;
143
144     if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
145         goto error;
146
147     /* Create a VA configuration */
148     VAConfigAttrib attrib;
149     memset( &attrib, 0, sizeof(attrib) );
150     attrib.type = VAConfigAttribRTFormat;
151     if( vaGetConfigAttributes( p_va->p_display,
152                                i_profile, VAEntrypointVLD, &attrib, 1 ) )
153         goto error;
154
155     /* Not sure what to do if not, I don't have a way to test */
156     if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
157         goto error;
158     if( vaCreateConfig( p_va->p_display,
159                         i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
160     {
161         p_va->i_config_id = 0;
162         goto error;
163     }
164
165     p_va->i_surface_count = i_surface_count;
166
167     if( asprintf( &p_va->va.description, "VA API version %d.%d",
168                   p_va->i_version_major, p_va->i_version_minor ) < 0 )
169         p_va->va.description = NULL;
170
171     return VLC_SUCCESS;
172
173 error:
174     return VLC_EGENERIC;
175 }
176
177 static void DestroySurfaces( vlc_va_vaapi_t *p_va )
178 {
179     if( p_va->image.image_id )
180     {
181         CopyCleanCache( &p_va->image_cache );
182         vaDestroyImage( p_va->p_display, p_va->image.image_id );
183     }
184
185     if( p_va->i_context_id )
186         vaDestroyContext( p_va->p_display, p_va->i_context_id );
187
188     for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
189     {
190         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
191
192         if( p_surface->i_id != VA_INVALID_SURFACE )
193             vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
194     }
195     free( p_va->p_surface );
196
197     /* */
198     p_va->image.image_id = 0;
199     p_va->i_context_id = 0;
200     p_va->p_surface = NULL;
201     p_va->i_surface_width = 0;
202     p_va->i_surface_height = 0;
203 }
204 static int CreateSurfaces( vlc_va_vaapi_t *p_va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
205                            int i_width, int i_height )
206 {
207     assert( i_width > 0 && i_height > 0 );
208
209     /* */
210     p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
211     if( !p_va->p_surface )
212         return VLC_EGENERIC;
213
214     /* Create surfaces */
215     VASurfaceID pi_surface_id[p_va->i_surface_count];
216     if( vaCreateSurfaces( p_va->p_display, i_width, i_height, VA_RT_FORMAT_YUV420,
217                           p_va->i_surface_count, pi_surface_id ) )
218     {
219         for( int i = 0; i < p_va->i_surface_count; i++ )
220             p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
221         goto error;
222     }
223
224     for( int i = 0; i < p_va->i_surface_count; i++ )
225     {
226         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
227
228         p_surface->i_id = pi_surface_id[i];
229         p_surface->i_refcount = 0;
230         p_surface->i_order = 0;
231     }
232
233     /* Create a context */
234     if( vaCreateContext( p_va->p_display, p_va->i_config_id,
235                          i_width, i_height, VA_PROGRESSIVE,
236                          pi_surface_id, p_va->i_surface_count, &p_va->i_context_id ) )
237     {
238         p_va->i_context_id = 0;
239         goto error;
240     }
241
242     /* Find a supported image chroma */
243     int i_fmt_count = vaMaxNumImageFormats( p_va->p_display );
244     VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
245     if( !p_fmt )
246         goto error;
247
248     if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
249     {
250         free( p_fmt );
251         goto error;
252     }
253
254     vlc_fourcc_t  i_chroma = 0;
255     VAImageFormat fmt;
256     for( int i = 0; i < i_fmt_count; i++ )
257     {
258         if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
259             p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
260             p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
261         {
262             i_chroma = VLC_CODEC_YV12;
263             fmt = p_fmt[i];
264             break;
265         }
266     }
267     free( p_fmt );
268     if( !i_chroma )
269         goto error;
270     *pi_chroma = i_chroma;
271
272     /* Create an image for surface extraction */
273     if( vaCreateImage(  p_va->p_display, &fmt, i_width, i_height, &p_va->image ) )
274     {
275         p_va->image.image_id = 0;
276         goto error;
277     }
278     CopyInitCache( &p_va->image_cache, i_width );
279
280     /* Setup the ffmpeg hardware context */
281     *pp_hw_ctx = &p_va->hw_ctx;
282
283     memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
284     p_va->hw_ctx.display    = p_va->p_display;
285     p_va->hw_ctx.config_id  = p_va->i_config_id;
286     p_va->hw_ctx.context_id = p_va->i_context_id;
287
288     /* */
289     p_va->i_surface_chroma = i_chroma;
290     p_va->i_surface_width = i_width;
291     p_va->i_surface_height = i_height;
292     return VLC_SUCCESS;
293
294 error:
295     DestroySurfaces( p_va );
296     return VLC_EGENERIC;
297 }
298
299 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
300                   int i_width, int i_height )
301 {
302     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
303
304     if( p_va->i_surface_width == i_width &&
305         p_va->i_surface_height == i_height )
306     {
307         *pp_hw_ctx = &p_va->hw_ctx;
308         *pi_chroma = p_va->i_surface_chroma;
309         return VLC_SUCCESS;
310     }
311
312     *pp_hw_ctx = NULL;
313     *pi_chroma = 0;
314     if( p_va->i_surface_width || p_va->i_surface_height )
315         DestroySurfaces( p_va );
316
317     if( i_width > 0 && i_height > 0 )
318         return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
319
320     return VLC_EGENERIC;
321 }
322 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
323 {
324     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
325
326     if( !p_va->image_cache.buffer )
327         return VLC_EGENERIC;
328
329     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
330
331 #if VA_CHECK_VERSION(0,31,0)
332     if( vaSyncSurface( p_va->p_display, i_surface_id ) )
333 #else
334     if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
335 #endif
336         return VLC_EGENERIC;
337
338     /* XXX vaDeriveImage may be better but it is not supported by
339      * my setup.
340      */
341
342     if( vaGetImage( p_va->p_display, i_surface_id,
343                     0, 0, p_va->i_surface_width, p_va->i_surface_height,
344                     p_va->image.image_id) )
345         return VLC_EGENERIC;
346
347     void *p_base;
348     if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
349         return VLC_EGENERIC;
350
351     const uint32_t i_fourcc = p_va->image.format.fourcc;
352     if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
353         i_fourcc == VA_FOURCC('I','4','2','0') )
354     {
355         bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
356         uint8_t *pp_plane[3];
357         size_t  pi_pitch[3];
358
359         for( int i = 0; i < 3; i++ )
360         {
361             const int i_src_plane = (b_swap_uv && i != 0) ?  (3 - i) : i;
362             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i_src_plane];
363             pi_pitch[i] = p_va->image.pitches[i_src_plane];
364         }
365         CopyFromYv12( p_picture, pp_plane, pi_pitch,
366                       p_va->i_surface_width,
367                       p_va->i_surface_height,
368                       &p_va->image_cache );
369     }
370     else
371     {
372         assert( i_fourcc == VA_FOURCC('N','V','1','2') );
373         uint8_t *pp_plane[2];
374         size_t  pi_pitch[2];
375
376         for( int i = 0; i < 2; i++ )
377         {
378             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
379             pi_pitch[i] = p_va->image.pitches[i];
380         }
381         CopyFromNv12( p_picture, pp_plane, pi_pitch,
382                       p_va->i_surface_width,
383                       p_va->i_surface_height,
384                       &p_va->image_cache );
385     }
386
387     if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
388         return VLC_EGENERIC;
389
390     return VLC_SUCCESS;
391 }
392 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
393 {
394     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
395     int i_old;
396     int i;
397
398     /* Grab an unused surface, in case none are, try the oldest
399      * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
400     for( i = 0, i_old = 0; i < p_va->i_surface_count; i++ )
401     {
402         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
403
404         if( !p_surface->i_refcount )
405             break;
406
407         if( p_surface->i_order < p_va->p_surface[i_old].i_order )
408             i_old = i;
409     }
410     if( i >= p_va->i_surface_count )
411         i = i_old;
412
413     vlc_va_surface_t *p_surface = &p_va->p_surface[i];
414
415     p_surface->i_refcount = 1;
416     p_surface->i_order = p_va->i_surface_order++;
417
418     /* */
419     for( int i = 0; i < 4; i++ )
420     {
421         p_ff->data[i] = NULL;
422         p_ff->linesize[i] = 0;
423
424         if( i == 0 || i == 3 )
425             p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
426     }
427     return VLC_SUCCESS;
428 }
429 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
430 {
431     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
432
433     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
434
435     for( int i = 0; i < p_va->i_surface_count; i++ )
436     {
437         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
438
439         if( p_surface->i_id == i_surface_id )
440             p_surface->i_refcount--;
441     }
442 }
443
444 static void Close( vlc_va_vaapi_t *p_va )
445 {
446     if( p_va->i_surface_width || p_va->i_surface_height )
447         DestroySurfaces( p_va );
448
449     if( p_va->i_config_id )
450         vaDestroyConfig( p_va->p_display, p_va->i_config_id );
451     if( p_va->p_display )
452         vaTerminate( p_va->p_display );
453     if( p_va->p_display_x11 )
454         XCloseDisplay( p_va->p_display_x11 );
455 }
456 static void Delete( vlc_va_t *p_external )
457 {
458     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
459     Close( p_va );
460     free( p_va->va.description );
461     free( p_va );
462 }
463
464 /* */
465 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
466 {
467     bool fail;
468
469     vlc_global_lock( VLC_XLIB_MUTEX );
470     fail = !XInitThreads();
471     vlc_global_unlock( VLC_XLIB_MUTEX );
472     if( unlikely(fail) )
473         return NULL;
474
475     vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
476     if( !p_va )
477         return NULL;
478     if( Open( p_va, i_codec_id ) )
479     {
480         free( p_va );
481         return NULL;
482     }
483
484     /* */
485     p_va->va.setup = Setup;
486     p_va->va.get = Get;
487     p_va->va.release = Release;
488     p_va->va.extract = Extract;
489     p_va->va.close = Delete;
490     return &p_va->va;
491 }
492 #else
493 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
494 {
495     VLC_UNUSED( i_codec_id );
496     return NULL;
497 }
498 #endif