]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/vaapi.c
Used VA_INVALID_ID when appropriate.
[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     p_va->i_config_id  = VA_INVALID_ID;
132     p_va->i_context_id = VA_INVALID_ID;
133
134     /* Create a VA display */
135     if( !XInitThreads() )
136         return VLC_EGENERIC;
137
138     p_va->p_display_x11 = XOpenDisplay(NULL);
139     if( !p_va->p_display_x11 )
140         goto error;
141
142     p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
143     if( !p_va->p_display )
144         goto error;
145
146     if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
147         goto error;
148
149     /* Create a VA configuration */
150     VAConfigAttrib attrib;
151     memset( &attrib, 0, sizeof(attrib) );
152     attrib.type = VAConfigAttribRTFormat;
153     if( vaGetConfigAttributes( p_va->p_display,
154                                i_profile, VAEntrypointVLD, &attrib, 1 ) )
155         goto error;
156
157     /* Not sure what to do if not, I don't have a way to test */
158     if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
159         goto error;
160     if( vaCreateConfig( p_va->p_display,
161                         i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
162     {
163         p_va->i_config_id = VA_INVALID_ID;
164         goto error;
165     }
166
167     p_va->i_surface_count = i_surface_count;
168
169     if( asprintf( &p_va->va.description, "VA API version %d.%d",
170                   p_va->i_version_major, p_va->i_version_minor ) < 0 )
171         p_va->va.description = NULL;
172
173     return VLC_SUCCESS;
174
175 error:
176     return VLC_EGENERIC;
177 }
178
179 static void DestroySurfaces( vlc_va_vaapi_t *p_va )
180 {
181     if( p_va->image.image_id != VA_INVALID_SURFACE )
182     {
183         CopyCleanCache( &p_va->image_cache );
184         vaDestroyImage( p_va->p_display, p_va->image.image_id );
185     }
186
187     if( p_va->i_context_id != VA_INVALID_ID )
188         vaDestroyContext( p_va->p_display, p_va->i_context_id );
189
190     for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
191     {
192         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
193
194         if( p_surface->i_id != VA_INVALID_SURFACE )
195             vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
196     }
197     free( p_va->p_surface );
198
199     /* */
200     p_va->image.image_id = VA_INVALID_SURFACE;
201     p_va->i_context_id = VA_INVALID_ID;
202     p_va->p_surface = NULL;
203     p_va->i_surface_width = 0;
204     p_va->i_surface_height = 0;
205 }
206 static int CreateSurfaces( vlc_va_vaapi_t *p_va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
207                            int i_width, int i_height )
208 {
209     assert( i_width > 0 && i_height > 0 );
210
211     /* */
212     p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
213     if( !p_va->p_surface )
214         return VLC_EGENERIC;
215     p_va->image.image_id = VA_INVALID_SURFACE;
216     p_va->i_context_id   = VA_INVALID_ID;
217
218     /* Create surfaces */
219     VASurfaceID pi_surface_id[p_va->i_surface_count];
220     if( vaCreateSurfaces( p_va->p_display, i_width, i_height, VA_RT_FORMAT_YUV420,
221                           p_va->i_surface_count, pi_surface_id ) )
222     {
223         for( int i = 0; i < p_va->i_surface_count; i++ )
224             p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
225         goto error;
226     }
227
228     for( int i = 0; i < p_va->i_surface_count; i++ )
229     {
230         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
231
232         p_surface->i_id = pi_surface_id[i];
233         p_surface->i_refcount = 0;
234         p_surface->i_order = 0;
235     }
236
237     /* Create a context */
238     if( vaCreateContext( p_va->p_display, p_va->i_config_id,
239                          i_width, i_height, VA_PROGRESSIVE,
240                          pi_surface_id, p_va->i_surface_count, &p_va->i_context_id ) )
241     {
242         p_va->i_context_id = VA_INVALID_ID;
243         goto error;
244     }
245
246     /* Find and create a supported image chroma */
247     int i_fmt_count = vaMaxNumImageFormats( p_va->p_display );
248     VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
249     if( !p_fmt )
250         goto error;
251
252     if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
253     {
254         free( p_fmt );
255         goto error;
256     }
257
258     vlc_fourcc_t  i_chroma = 0;
259     VAImageFormat fmt;
260     for( int i = 0; i < i_fmt_count; i++ )
261     {
262         if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
263             p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
264             p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
265         {
266             if( vaCreateImage(  p_va->p_display, &p_fmt[i], i_width, i_height, &p_va->image ) )
267             {
268                 p_va->image.image_id = VA_INVALID_SURFACE;
269                 continue;
270             }
271             /* Validate that vaGetImage works with this format */
272             if( vaGetImage( p_va->p_display, pi_surface_id[0],
273                             0, 0, i_width, i_height,
274                             p_va->image.image_id) )
275             {
276                 vaDestroyImage( p_va->p_display, p_va->image.image_id );
277                 p_va->image.image_id = VA_INVALID_SURFACE;
278                 continue;
279             }
280
281             i_chroma = VLC_CODEC_YV12;
282             fmt = p_fmt[i];
283             break;
284         }
285     }
286     free( p_fmt );
287     if( !i_chroma )
288         goto error;
289     *pi_chroma = i_chroma;
290
291     CopyInitCache( &p_va->image_cache, i_width );
292
293     /* Setup the ffmpeg hardware context */
294     *pp_hw_ctx = &p_va->hw_ctx;
295
296     memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
297     p_va->hw_ctx.display    = p_va->p_display;
298     p_va->hw_ctx.config_id  = p_va->i_config_id;
299     p_va->hw_ctx.context_id = p_va->i_context_id;
300
301     /* */
302     p_va->i_surface_chroma = i_chroma;
303     p_va->i_surface_width = i_width;
304     p_va->i_surface_height = i_height;
305     return VLC_SUCCESS;
306
307 error:
308     DestroySurfaces( p_va );
309     return VLC_EGENERIC;
310 }
311
312 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
313                   int i_width, int i_height )
314 {
315     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
316
317     if( p_va->i_surface_width == i_width &&
318         p_va->i_surface_height == i_height )
319     {
320         *pp_hw_ctx = &p_va->hw_ctx;
321         *pi_chroma = p_va->i_surface_chroma;
322         return VLC_SUCCESS;
323     }
324
325     *pp_hw_ctx = NULL;
326     *pi_chroma = 0;
327     if( p_va->i_surface_width || p_va->i_surface_height )
328         DestroySurfaces( p_va );
329
330     if( i_width > 0 && i_height > 0 )
331         return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
332
333     return VLC_EGENERIC;
334 }
335 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
336 {
337     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
338
339     if( !p_va->image_cache.buffer )
340         return VLC_EGENERIC;
341
342     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
343
344 #if VA_CHECK_VERSION(0,31,0)
345     if( vaSyncSurface( p_va->p_display, i_surface_id ) )
346 #else
347     if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
348 #endif
349         return VLC_EGENERIC;
350
351     /* XXX vaDeriveImage may be better but it is not supported by
352      * my setup.
353      */
354
355     if( vaGetImage( p_va->p_display, i_surface_id,
356                     0, 0, p_va->i_surface_width, p_va->i_surface_height,
357                     p_va->image.image_id) )
358         return VLC_EGENERIC;
359
360     void *p_base;
361     if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
362         return VLC_EGENERIC;
363
364     const uint32_t i_fourcc = p_va->image.format.fourcc;
365     if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
366         i_fourcc == VA_FOURCC('I','4','2','0') )
367     {
368         bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
369         uint8_t *pp_plane[3];
370         size_t  pi_pitch[3];
371
372         for( int i = 0; i < 3; i++ )
373         {
374             const int i_src_plane = (b_swap_uv && i != 0) ?  (3 - i) : i;
375             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i_src_plane];
376             pi_pitch[i] = p_va->image.pitches[i_src_plane];
377         }
378         CopyFromYv12( p_picture, pp_plane, pi_pitch,
379                       p_va->i_surface_width,
380                       p_va->i_surface_height,
381                       &p_va->image_cache );
382     }
383     else
384     {
385         assert( i_fourcc == VA_FOURCC('N','V','1','2') );
386         uint8_t *pp_plane[2];
387         size_t  pi_pitch[2];
388
389         for( int i = 0; i < 2; i++ )
390         {
391             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
392             pi_pitch[i] = p_va->image.pitches[i];
393         }
394         CopyFromNv12( p_picture, pp_plane, pi_pitch,
395                       p_va->i_surface_width,
396                       p_va->i_surface_height,
397                       &p_va->image_cache );
398     }
399
400     if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
401         return VLC_EGENERIC;
402
403     return VLC_SUCCESS;
404 }
405 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
406 {
407     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
408     int i_old;
409     int i;
410
411     /* Grab an unused surface, in case none are, try the oldest
412      * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
413     for( i = 0, i_old = 0; i < p_va->i_surface_count; i++ )
414     {
415         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
416
417         if( !p_surface->i_refcount )
418             break;
419
420         if( p_surface->i_order < p_va->p_surface[i_old].i_order )
421             i_old = i;
422     }
423     if( i >= p_va->i_surface_count )
424         i = i_old;
425
426     vlc_va_surface_t *p_surface = &p_va->p_surface[i];
427
428     p_surface->i_refcount = 1;
429     p_surface->i_order = p_va->i_surface_order++;
430
431     /* */
432     for( int i = 0; i < 4; i++ )
433     {
434         p_ff->data[i] = NULL;
435         p_ff->linesize[i] = 0;
436
437         if( i == 0 || i == 3 )
438             p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
439     }
440     return VLC_SUCCESS;
441 }
442 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
443 {
444     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
445
446     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
447
448     for( int i = 0; i < p_va->i_surface_count; i++ )
449     {
450         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
451
452         if( p_surface->i_id == i_surface_id )
453             p_surface->i_refcount--;
454     }
455 }
456
457 static void Close( vlc_va_vaapi_t *p_va )
458 {
459     if( p_va->i_surface_width || p_va->i_surface_height )
460         DestroySurfaces( p_va );
461
462     if( p_va->i_config_id != VA_INVALID_ID )
463         vaDestroyConfig( p_va->p_display, p_va->i_config_id );
464     if( p_va->p_display )
465         vaTerminate( p_va->p_display );
466     if( p_va->p_display_x11 )
467         XCloseDisplay( p_va->p_display_x11 );
468 }
469 static void Delete( vlc_va_t *p_external )
470 {
471     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
472     Close( p_va );
473     free( p_va->va.description );
474     free( p_va );
475 }
476
477 /* */
478 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
479 {
480     bool fail;
481
482     vlc_global_lock( VLC_XLIB_MUTEX );
483     fail = !XInitThreads();
484     vlc_global_unlock( VLC_XLIB_MUTEX );
485     if( unlikely(fail) )
486         return NULL;
487
488     vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
489     if( !p_va )
490         return NULL;
491     if( Open( p_va, i_codec_id ) )
492     {
493         free( p_va );
494         return NULL;
495     }
496
497     /* */
498     p_va->va.setup = Setup;
499     p_va->va.get = Get;
500     p_va->va.release = Release;
501     p_va->va.extract = Extract;
502     p_va->va.close = Delete;
503     return &p_va->va;
504 }
505 #else
506 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
507 {
508     VLC_UNUSED( i_codec_id );
509     return NULL;
510 }
511 #endif