1 /*****************************************************************************
2 * vaapi.c: VAAPI helpers for the libavcodec decoder
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir_AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_fourcc.h>
35 #include <libavcodec/avcodec.h>
36 #include <libavcodec/vaapi.h>
38 #include <va/va_x11.h>
44 static int Create( vlc_va_t *, int, const es_format_t * );
45 static void Delete( vlc_va_t * );
48 set_description( N_("Video Acceleration (VA) API") )
49 set_capability( "hw decoder", 50 )
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_VCODEC )
52 set_callbacks( Create, Delete )
65 Display *p_display_x11;
68 VAConfigID i_config_id;
69 VAContextID i_context_id;
71 struct vaapi_context hw_ctx;
79 unsigned int i_surface_order;
82 vlc_fourcc_t i_surface_chroma;
84 vlc_va_surface_t *p_surface;
87 copy_cache_t image_cache;
89 bool b_supports_derive;
93 static int Open( vlc_va_t *p_external, int i_codec_id )
95 vlc_va_sys_t *p_va = calloc( 1, sizeof(*p_va) );
96 if ( unlikely(p_va == NULL) )
99 VAProfile i_profile, *p_profiles_list;
100 bool b_supported_profile = false;
101 int i_profiles_nb = 0;
107 case CODEC_ID_MPEG1VIDEO:
108 case CODEC_ID_MPEG2VIDEO:
109 i_profile = VAProfileMPEG2Main;
110 i_surface_count = 2+1;
113 i_profile = VAProfileMPEG4AdvancedSimple;
114 i_surface_count = 2+1;
117 i_profile = VAProfileVC1Main;
118 i_surface_count = 2+1;
121 i_profile = VAProfileVC1Advanced;
122 i_surface_count = 2+1;
125 i_profile = VAProfileH264High;
126 i_surface_count = 16+1;
133 p_va->i_config_id = VA_INVALID_ID;
134 p_va->i_context_id = VA_INVALID_ID;
135 p_va->image.image_id = VA_INVALID_ID;
137 /* Create a VA display */
138 p_va->p_display_x11 = XOpenDisplay(NULL);
139 if( !p_va->p_display_x11 )
141 msg_Err( p_external, "Could not connect to X server" );
145 p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
146 if( !p_va->p_display )
148 msg_Err( p_external, "Could not get a VAAPI device" );
152 if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
154 msg_Err( p_external, "Failed to initialize the VAAPI device" );
158 /* Check if the selected profile is supported */
159 i_profiles_nb = vaMaxNumProfiles( p_va->p_display );
160 p_profiles_list = calloc( i_profiles_nb, sizeof( VAProfile ) );
161 if( !p_profiles_list )
164 VAStatus i_status = vaQueryConfigProfiles( p_va->p_display, p_profiles_list, &i_profiles_nb );
165 if ( i_status == VA_STATUS_SUCCESS )
167 for( int i = 0; i < i_profiles_nb; i++ )
169 if ( p_profiles_list[i] == i_profile )
171 b_supported_profile = true;
176 free( p_profiles_list );
177 if ( !b_supported_profile )
179 msg_Dbg( p_external, "Codec and profile not supported by the hardware" );
183 /* Create a VA configuration */
184 VAConfigAttrib attrib;
185 memset( &attrib, 0, sizeof(attrib) );
186 attrib.type = VAConfigAttribRTFormat;
187 if( vaGetConfigAttributes( p_va->p_display,
188 i_profile, VAEntrypointVLD, &attrib, 1 ) )
191 /* Not sure what to do if not, I don't have a way to test */
192 if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
194 if( vaCreateConfig( p_va->p_display,
195 i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
197 p_va->i_config_id = VA_INVALID_ID;
201 p_va->i_surface_count = i_surface_count;
203 p_va->b_supports_derive = false;
205 if( asprintf( &p_external->description, "VA API version %d.%d",
206 p_va->i_version_major, p_va->i_version_minor ) < 0 )
207 p_external->description = NULL;
209 p_external->sys = p_va;
216 static void DestroySurfaces( vlc_va_sys_t *p_va )
218 if( p_va->image.image_id != VA_INVALID_ID )
220 CopyCleanCache( &p_va->image_cache );
221 vaDestroyImage( p_va->p_display, p_va->image.image_id );
223 else if(p_va->b_supports_derive)
225 CopyCleanCache( &p_va->image_cache );
228 if( p_va->i_context_id != VA_INVALID_ID )
229 vaDestroyContext( p_va->p_display, p_va->i_context_id );
231 for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
233 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
235 if( p_surface->i_id != VA_INVALID_SURFACE )
236 vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
238 free( p_va->p_surface );
241 p_va->image.image_id = VA_INVALID_ID;
242 p_va->i_context_id = VA_INVALID_ID;
243 p_va->p_surface = NULL;
244 p_va->i_surface_width = 0;
245 p_va->i_surface_height = 0;
247 static int CreateSurfaces( vlc_va_sys_t *p_va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
248 int i_width, int i_height )
250 assert( i_width > 0 && i_height > 0 );
253 p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
254 if( !p_va->p_surface )
256 p_va->image.image_id = VA_INVALID_ID;
257 p_va->i_context_id = VA_INVALID_ID;
259 /* Create surfaces */
260 VASurfaceID pi_surface_id[p_va->i_surface_count];
261 if( vaCreateSurfaces( p_va->p_display, i_width, i_height, VA_RT_FORMAT_YUV420,
262 p_va->i_surface_count, pi_surface_id ) )
264 for( int i = 0; i < p_va->i_surface_count; i++ )
265 p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
269 for( int i = 0; i < p_va->i_surface_count; i++ )
271 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
273 p_surface->i_id = pi_surface_id[i];
274 p_surface->i_refcount = 0;
275 p_surface->i_order = 0;
278 /* Create a context */
279 if( vaCreateContext( p_va->p_display, p_va->i_config_id,
280 i_width, i_height, VA_PROGRESSIVE,
281 pi_surface_id, p_va->i_surface_count, &p_va->i_context_id ) )
283 p_va->i_context_id = VA_INVALID_ID;
287 /* Find and create a supported image chroma */
288 int i_fmt_count = vaMaxNumImageFormats( p_va->p_display );
289 VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
293 if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
300 if(vaDeriveImage(p_va->p_display, pi_surface_id[0], &test_image) == VA_STATUS_SUCCESS)
302 p_va->b_supports_derive = true;
303 vaDestroyImage(p_va->p_display, test_image.image_id);
306 vlc_fourcc_t i_chroma = 0;
308 for( int i = 0; i < i_fmt_count; i++ )
310 if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
311 p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
312 p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
314 if( vaCreateImage( p_va->p_display, &p_fmt[i], i_width, i_height, &p_va->image ) )
316 p_va->image.image_id = VA_INVALID_ID;
319 /* Validate that vaGetImage works with this format */
320 if( vaGetImage( p_va->p_display, pi_surface_id[0],
321 0, 0, i_width, i_height,
322 p_va->image.image_id) )
324 vaDestroyImage( p_va->p_display, p_va->image.image_id );
325 p_va->image.image_id = VA_INVALID_ID;
329 i_chroma = VLC_CODEC_YV12;
337 *pi_chroma = i_chroma;
339 if(p_va->b_supports_derive)
341 vaDestroyImage( p_va->p_display, p_va->image.image_id );
342 p_va->image.image_id = VA_INVALID_ID;
345 if( unlikely(CopyInitCache( &p_va->image_cache, i_width )) )
348 /* Setup the ffmpeg hardware context */
349 *pp_hw_ctx = &p_va->hw_ctx;
351 memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
352 p_va->hw_ctx.display = p_va->p_display;
353 p_va->hw_ctx.config_id = p_va->i_config_id;
354 p_va->hw_ctx.context_id = p_va->i_context_id;
357 p_va->i_surface_chroma = i_chroma;
358 p_va->i_surface_width = i_width;
359 p_va->i_surface_height = i_height;
363 DestroySurfaces( p_va );
367 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
368 int i_width, int i_height )
370 vlc_va_sys_t *p_va = p_external->sys;
372 if( p_va->i_surface_width == i_width &&
373 p_va->i_surface_height == i_height )
375 *pp_hw_ctx = &p_va->hw_ctx;
376 *pi_chroma = p_va->i_surface_chroma;
382 if( p_va->i_surface_width || p_va->i_surface_height )
383 DestroySurfaces( p_va );
385 if( i_width > 0 && i_height > 0 )
386 return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
390 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
392 vlc_va_sys_t *p_va = p_external->sys;
394 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
396 #if VA_CHECK_VERSION(0,31,0)
397 if( vaSyncSurface( p_va->p_display, i_surface_id ) )
399 if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
403 if(p_va->b_supports_derive)
405 if(vaDeriveImage(p_va->p_display, i_surface_id, &(p_va->image)) != VA_STATUS_SUCCESS)
410 if( vaGetImage( p_va->p_display, i_surface_id,
411 0, 0, p_va->i_surface_width, p_va->i_surface_height,
412 p_va->image.image_id) )
417 if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
420 const uint32_t i_fourcc = p_va->image.format.fourcc;
421 if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
422 i_fourcc == VA_FOURCC('I','4','2','0') )
424 bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
425 uint8_t *pp_plane[3];
428 for( int i = 0; i < 3; i++ )
430 const int i_src_plane = (b_swap_uv && i != 0) ? (3 - i) : i;
431 pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i_src_plane];
432 pi_pitch[i] = p_va->image.pitches[i_src_plane];
434 CopyFromYv12( p_picture, pp_plane, pi_pitch,
435 p_va->i_surface_width,
436 p_va->i_surface_height,
437 &p_va->image_cache );
441 assert( i_fourcc == VA_FOURCC('N','V','1','2') );
442 uint8_t *pp_plane[2];
445 for( int i = 0; i < 2; i++ )
447 pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
448 pi_pitch[i] = p_va->image.pitches[i];
450 CopyFromNv12( p_picture, pp_plane, pi_pitch,
451 p_va->i_surface_width,
452 p_va->i_surface_height,
453 &p_va->image_cache );
456 if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
459 if(p_va->b_supports_derive)
461 vaDestroyImage( p_va->p_display, p_va->image.image_id );
462 p_va->image.image_id = VA_INVALID_ID;
467 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
469 vlc_va_sys_t *p_va = p_external->sys;
473 /* Grab an unused surface, in case none are, try the oldest
474 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
475 for( i = 0, i_old = 0; i < p_va->i_surface_count; i++ )
477 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
479 if( !p_surface->i_refcount )
482 if( p_surface->i_order < p_va->p_surface[i_old].i_order )
485 if( i >= p_va->i_surface_count )
488 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
490 p_surface->i_refcount = 1;
491 p_surface->i_order = p_va->i_surface_order++;
494 for( int i = 0; i < 4; i++ )
496 p_ff->data[i] = NULL;
497 p_ff->linesize[i] = 0;
499 if( i == 0 || i == 3 )
500 p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
504 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
506 vlc_va_sys_t *p_va = p_external->sys;
508 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
510 for( int i = 0; i < p_va->i_surface_count; i++ )
512 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
514 if( p_surface->i_id == i_surface_id )
515 p_surface->i_refcount--;
519 static void Close( vlc_va_sys_t *p_va )
521 if( p_va->i_surface_width || p_va->i_surface_height )
522 DestroySurfaces( p_va );
524 if( p_va->i_config_id != VA_INVALID_ID )
525 vaDestroyConfig( p_va->p_display, p_va->i_config_id );
526 if( p_va->p_display )
527 vaTerminate( p_va->p_display );
528 if( p_va->p_display_x11 )
529 XCloseDisplay( p_va->p_display_x11 );
532 static void Delete( vlc_va_t *p_external )
534 vlc_va_sys_t *p_va = p_external->sys;
536 free( p_external->description );
540 static int Create( vlc_va_t *p_va, int i_codec_id, const es_format_t *fmt )
542 if( !vlc_xlib_init( VLC_OBJECT(p_va) ) )
544 msg_Warn( p_va, "Ignoring VA API" );
550 int err = Open( p_va, i_codec_id );
554 /* Only VLD supported */
555 p_va->pix_fmt = PIX_FMT_VAAPI_VLD;
558 p_va->release = Release;
559 p_va->extract = Extract;