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