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