]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/vaapi.c
avcodec: unify interface for video acceleration
[vlc] / modules / codec / avcodec / vaapi.c
1 /*****************************************************************************
2  * vaapi.c: VAAPI helpers for the libavcodec 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 #include <libavcodec/avcodec.h>
33
34 #include "avcodec.h"
35 #include "va.h"
36 #include "copy.h"
37
38 #ifdef HAVE_AVCODEC_VAAPI
39
40 #include <vlc_xlib.h>
41
42 #include <libavcodec/vaapi.h>
43
44 #include <X11/Xlib.h>
45 #include <va/va_x11.h>
46
47 typedef struct
48 {
49     VASurfaceID  i_id;
50     int          i_refcount;
51     unsigned int i_order;
52
53 } vlc_va_surface_t;
54
55 typedef struct
56 {
57     vlc_va_t     va;
58
59     vlc_object_t *log;
60
61     /* */
62     Display      *p_display_x11;
63     VADisplay     p_display;
64
65     VAConfigID    i_config_id;
66     VAContextID   i_context_id;
67
68     struct vaapi_context hw_ctx;
69
70     /* */
71     int i_version_major;
72     int i_version_minor;
73
74     /* */
75     int          i_surface_count;
76     unsigned int i_surface_order;
77     int          i_surface_width;
78     int          i_surface_height;
79     vlc_fourcc_t i_surface_chroma;
80
81     vlc_va_surface_t *p_surface;
82
83     VAImage      image;
84     copy_cache_t image_cache;
85
86 } vlc_va_vaapi_t;
87
88 static vlc_va_vaapi_t *vlc_va_vaapi_Get( void *p_va )
89 {
90     return p_va;
91 }
92
93 /* */
94 static int Open( vlc_va_vaapi_t *p_va, int i_codec_id )
95 {
96     VAProfile i_profile, *p_profiles_list;
97     bool b_supported_profile = false;
98     int i_profiles_nb = 0;
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     p_va->i_config_id  = VA_INVALID_ID;
131     p_va->i_context_id = VA_INVALID_ID;
132     p_va->image.image_id = VA_INVALID_ID;
133
134     /* Create a VA display */
135     p_va->p_display_x11 = XOpenDisplay(NULL);
136     if( !p_va->p_display_x11 )
137     {
138         msg_Err( p_va->log, "Could not connect to X server" );
139         goto error;
140     }
141
142     p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
143     if( !p_va->p_display )
144     {
145         msg_Err( p_va->log, "Could not get a VAAPI device" );
146         goto error;
147     }
148
149     if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
150     {
151         msg_Err( p_va->log, "Failed to initialize the VAAPI device" );
152         goto error;
153     }
154
155     /* Check if the selected profile is supported */
156     i_profiles_nb = vaMaxNumProfiles( p_va->p_display );
157     p_profiles_list = calloc( i_profiles_nb, sizeof( VAProfile ) );
158     if( !p_profiles_list )
159         goto error;
160
161     VAStatus i_status = vaQueryConfigProfiles( p_va->p_display, p_profiles_list, &i_profiles_nb );
162     if ( i_status == VA_STATUS_SUCCESS )
163     {
164         for( int i = 0; i < i_profiles_nb; i++ )
165         {
166             if ( p_profiles_list[i] == i_profile )
167             {
168                 b_supported_profile = true;
169                 break;
170             }
171         }
172     }
173     free( p_profiles_list );
174     if ( !b_supported_profile )
175     {
176         msg_Dbg( p_va->log, "Codec and profile not supported by the hardware" );
177         goto error;
178     }
179
180     /* Create a VA configuration */
181     VAConfigAttrib attrib;
182     memset( &attrib, 0, sizeof(attrib) );
183     attrib.type = VAConfigAttribRTFormat;
184     if( vaGetConfigAttributes( p_va->p_display,
185                                i_profile, VAEntrypointVLD, &attrib, 1 ) )
186         goto error;
187
188     /* Not sure what to do if not, I don't have a way to test */
189     if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
190         goto error;
191     if( vaCreateConfig( p_va->p_display,
192                         i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
193     {
194         p_va->i_config_id = VA_INVALID_ID;
195         goto error;
196     }
197
198     p_va->i_surface_count = i_surface_count;
199
200     if( asprintf( &p_va->va.description, "VA API version %d.%d",
201                   p_va->i_version_major, p_va->i_version_minor ) < 0 )
202         p_va->va.description = NULL;
203
204     return VLC_SUCCESS;
205
206 error:
207     return VLC_EGENERIC;
208 }
209
210 static void DestroySurfaces( vlc_va_vaapi_t *p_va )
211 {
212     if( p_va->image.image_id != VA_INVALID_ID )
213     {
214         CopyCleanCache( &p_va->image_cache );
215         vaDestroyImage( p_va->p_display, p_va->image.image_id );
216     }
217
218     if( p_va->i_context_id != VA_INVALID_ID )
219         vaDestroyContext( p_va->p_display, p_va->i_context_id );
220
221     for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
222     {
223         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
224
225         if( p_surface->i_id != VA_INVALID_SURFACE )
226             vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
227     }
228     free( p_va->p_surface );
229
230     /* */
231     p_va->image.image_id = VA_INVALID_ID;
232     p_va->i_context_id = VA_INVALID_ID;
233     p_va->p_surface = NULL;
234     p_va->i_surface_width = 0;
235     p_va->i_surface_height = 0;
236 }
237 static int CreateSurfaces( vlc_va_vaapi_t *p_va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
238                            int i_width, int i_height )
239 {
240     assert( i_width > 0 && i_height > 0 );
241
242     /* */
243     p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
244     if( !p_va->p_surface )
245         return VLC_EGENERIC;
246     p_va->image.image_id = VA_INVALID_ID;
247     p_va->i_context_id   = VA_INVALID_ID;
248
249     /* Create surfaces */
250     VASurfaceID pi_surface_id[p_va->i_surface_count];
251     if( vaCreateSurfaces( p_va->p_display, i_width, i_height, VA_RT_FORMAT_YUV420,
252                           p_va->i_surface_count, pi_surface_id ) )
253     {
254         for( int i = 0; i < p_va->i_surface_count; i++ )
255             p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
256         goto error;
257     }
258
259     for( int i = 0; i < p_va->i_surface_count; i++ )
260     {
261         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
262
263         p_surface->i_id = pi_surface_id[i];
264         p_surface->i_refcount = 0;
265         p_surface->i_order = 0;
266     }
267
268     /* Create a context */
269     if( vaCreateContext( p_va->p_display, p_va->i_config_id,
270                          i_width, i_height, VA_PROGRESSIVE,
271                          pi_surface_id, p_va->i_surface_count, &p_va->i_context_id ) )
272     {
273         p_va->i_context_id = VA_INVALID_ID;
274         goto error;
275     }
276
277     /* Find and create a supported image chroma */
278     int i_fmt_count = vaMaxNumImageFormats( p_va->p_display );
279     VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
280     if( !p_fmt )
281         goto error;
282
283     if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
284     {
285         free( p_fmt );
286         goto error;
287     }
288
289     vlc_fourcc_t  i_chroma = 0;
290     VAImageFormat fmt;
291     for( int i = 0; i < i_fmt_count; i++ )
292     {
293         if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
294             p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
295             p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
296         {
297             if( vaCreateImage(  p_va->p_display, &p_fmt[i], i_width, i_height, &p_va->image ) )
298             {
299                 p_va->image.image_id = VA_INVALID_ID;
300                 continue;
301             }
302             /* Validate that vaGetImage works with this format */
303             if( vaGetImage( p_va->p_display, pi_surface_id[0],
304                             0, 0, i_width, i_height,
305                             p_va->image.image_id) )
306             {
307                 vaDestroyImage( p_va->p_display, p_va->image.image_id );
308                 p_va->image.image_id = VA_INVALID_ID;
309                 continue;
310             }
311
312             i_chroma = VLC_CODEC_YV12;
313             fmt = p_fmt[i];
314             break;
315         }
316     }
317     free( p_fmt );
318     if( !i_chroma )
319         goto error;
320     *pi_chroma = i_chroma;
321
322     CopyInitCache( &p_va->image_cache, i_width );
323
324     /* Setup the ffmpeg hardware context */
325     *pp_hw_ctx = &p_va->hw_ctx;
326
327     memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
328     p_va->hw_ctx.display    = p_va->p_display;
329     p_va->hw_ctx.config_id  = p_va->i_config_id;
330     p_va->hw_ctx.context_id = p_va->i_context_id;
331
332     /* */
333     p_va->i_surface_chroma = i_chroma;
334     p_va->i_surface_width = i_width;
335     p_va->i_surface_height = i_height;
336     return VLC_SUCCESS;
337
338 error:
339     DestroySurfaces( p_va );
340     return VLC_EGENERIC;
341 }
342
343 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
344                   int i_width, int i_height )
345 {
346     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
347
348     if( p_va->i_surface_width == i_width &&
349         p_va->i_surface_height == i_height )
350     {
351         *pp_hw_ctx = &p_va->hw_ctx;
352         *pi_chroma = p_va->i_surface_chroma;
353         return VLC_SUCCESS;
354     }
355
356     *pp_hw_ctx = NULL;
357     *pi_chroma = 0;
358     if( p_va->i_surface_width || p_va->i_surface_height )
359         DestroySurfaces( p_va );
360
361     if( i_width > 0 && i_height > 0 )
362         return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
363
364     return VLC_EGENERIC;
365 }
366 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
367 {
368     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
369
370     if( !p_va->image_cache.buffer )
371         return VLC_EGENERIC;
372
373     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
374
375 #if VA_CHECK_VERSION(0,31,0)
376     if( vaSyncSurface( p_va->p_display, i_surface_id ) )
377 #else
378     if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
379 #endif
380         return VLC_EGENERIC;
381
382     /* XXX vaDeriveImage may be better but it is not supported by
383      * my setup.
384      */
385
386     if( vaGetImage( p_va->p_display, i_surface_id,
387                     0, 0, p_va->i_surface_width, p_va->i_surface_height,
388                     p_va->image.image_id) )
389         return VLC_EGENERIC;
390
391     void *p_base;
392     if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
393         return VLC_EGENERIC;
394
395     const uint32_t i_fourcc = p_va->image.format.fourcc;
396     if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
397         i_fourcc == VA_FOURCC('I','4','2','0') )
398     {
399         bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
400         uint8_t *pp_plane[3];
401         size_t  pi_pitch[3];
402
403         for( int i = 0; i < 3; i++ )
404         {
405             const int i_src_plane = (b_swap_uv && i != 0) ?  (3 - i) : i;
406             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i_src_plane];
407             pi_pitch[i] = p_va->image.pitches[i_src_plane];
408         }
409         CopyFromYv12( p_picture, pp_plane, pi_pitch,
410                       p_va->i_surface_width,
411                       p_va->i_surface_height,
412                       &p_va->image_cache );
413     }
414     else
415     {
416         assert( i_fourcc == VA_FOURCC('N','V','1','2') );
417         uint8_t *pp_plane[2];
418         size_t  pi_pitch[2];
419
420         for( int i = 0; i < 2; i++ )
421         {
422             pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
423             pi_pitch[i] = p_va->image.pitches[i];
424         }
425         CopyFromNv12( p_picture, pp_plane, pi_pitch,
426                       p_va->i_surface_width,
427                       p_va->i_surface_height,
428                       &p_va->image_cache );
429     }
430
431     if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
432         return VLC_EGENERIC;
433
434     return VLC_SUCCESS;
435 }
436 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
437 {
438     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
439     int i_old;
440     int i;
441
442     /* Grab an unused surface, in case none are, try the oldest
443      * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
444     for( i = 0, i_old = 0; i < p_va->i_surface_count; i++ )
445     {
446         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
447
448         if( !p_surface->i_refcount )
449             break;
450
451         if( p_surface->i_order < p_va->p_surface[i_old].i_order )
452             i_old = i;
453     }
454     if( i >= p_va->i_surface_count )
455         i = i_old;
456
457     vlc_va_surface_t *p_surface = &p_va->p_surface[i];
458
459     p_surface->i_refcount = 1;
460     p_surface->i_order = p_va->i_surface_order++;
461
462     /* */
463     for( int i = 0; i < 4; i++ )
464     {
465         p_ff->data[i] = NULL;
466         p_ff->linesize[i] = 0;
467
468         if( i == 0 || i == 3 )
469             p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
470     }
471     return VLC_SUCCESS;
472 }
473 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
474 {
475     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
476
477     VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
478
479     for( int i = 0; i < p_va->i_surface_count; i++ )
480     {
481         vlc_va_surface_t *p_surface = &p_va->p_surface[i];
482
483         if( p_surface->i_id == i_surface_id )
484             p_surface->i_refcount--;
485     }
486 }
487
488 static void Close( vlc_va_vaapi_t *p_va )
489 {
490     if( p_va->i_surface_width || p_va->i_surface_height )
491         DestroySurfaces( p_va );
492
493     if( p_va->i_config_id != VA_INVALID_ID )
494         vaDestroyConfig( p_va->p_display, p_va->i_config_id );
495     if( p_va->p_display )
496         vaTerminate( p_va->p_display );
497     if( p_va->p_display_x11 )
498         XCloseDisplay( p_va->p_display_x11 );
499 }
500 static void Delete( vlc_va_t *p_external )
501 {
502     vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
503     Close( p_va );
504     free( p_va->va.description );
505     free( p_va );
506 }
507
508 /* */
509 vlc_va_t *vlc_va_New( vlc_object_t *obj, int pixfmt, int i_codec_id,
510                       const es_format_t *fmt )
511 {
512     /* Only VLD supported */
513     if( pixfmt != PIX_FMT_VAAPI_VLD )
514         return NULL;
515
516     if( !vlc_xlib_init( obj ) )
517     {
518         msg_Warn( obj, "Ignoring VA API" );
519         return NULL;
520     }
521
522     vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
523     if( !p_va )
524         return NULL;
525
526     p_va->log = obj;
527     (void) fmt;
528
529     if( Open( p_va, i_codec_id ) )
530     {
531         free( p_va );
532         return NULL;
533     }
534
535     /* */
536     p_va->va.setup = Setup;
537     p_va->va.get = Get;
538     p_va->va.release = Release;
539     p_va->va.extract = Extract;
540     p_va->va.close = Delete;
541     return &p_va->va;
542 }
543 #endif