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
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_fourcc.h>
32 #include <libavcodec/avcodec.h>
38 #ifdef HAVE_AVCODEC_VAAPI
42 #include <libavcodec/vaapi.h>
45 #include <va/va_x11.h>
62 Display *p_display_x11;
65 VAConfigID i_config_id;
66 VAContextID i_context_id;
68 struct vaapi_context hw_ctx;
76 unsigned int i_surface_order;
79 vlc_fourcc_t i_surface_chroma;
81 vlc_va_surface_t *p_surface;
84 copy_cache_t image_cache;
88 static vlc_va_vaapi_t *vlc_va_vaapi_Get( void *p_va )
94 static int Open( vlc_va_vaapi_t *p_va, int i_codec_id )
96 VAProfile i_profile, *p_profiles_list;
97 bool b_supported_profile = false;
98 int i_profiles_nb = 0;
104 case CODEC_ID_MPEG1VIDEO:
105 case CODEC_ID_MPEG2VIDEO:
106 i_profile = VAProfileMPEG2Main;
107 i_surface_count = 2+1;
110 i_profile = VAProfileMPEG4AdvancedSimple;
111 i_surface_count = 2+1;
114 i_profile = VAProfileVC1Main;
115 i_surface_count = 2+1;
118 i_profile = VAProfileVC1Advanced;
119 i_surface_count = 2+1;
122 i_profile = VAProfileH264High;
123 i_surface_count = 16+1;
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;
134 /* Create a VA display */
135 p_va->p_display_x11 = XOpenDisplay(NULL);
136 if( !p_va->p_display_x11 )
138 msg_Err( p_va->log, "Could not connect to X server" );
142 p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
143 if( !p_va->p_display )
145 msg_Err( p_va->log, "Could not get a VAAPI device" );
149 if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
151 msg_Err( p_va->log, "Failed to initialize the VAAPI device" );
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 )
161 VAStatus i_status = vaQueryConfigProfiles( p_va->p_display, p_profiles_list, &i_profiles_nb );
162 if ( i_status == VA_STATUS_SUCCESS )
164 for( int i = 0; i < i_profiles_nb; i++ )
166 if ( p_profiles_list[i] == i_profile )
168 b_supported_profile = true;
173 free( p_profiles_list );
174 if ( !b_supported_profile )
176 msg_Dbg( p_va->log, "Codec and profile not supported by the hardware" );
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 ) )
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 )
191 if( vaCreateConfig( p_va->p_display,
192 i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
194 p_va->i_config_id = VA_INVALID_ID;
198 p_va->i_surface_count = i_surface_count;
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;
210 static void DestroySurfaces( vlc_va_vaapi_t *p_va )
212 if( p_va->image.image_id != VA_INVALID_ID )
214 CopyCleanCache( &p_va->image_cache );
215 vaDestroyImage( p_va->p_display, p_va->image.image_id );
218 if( p_va->i_context_id != VA_INVALID_ID )
219 vaDestroyContext( p_va->p_display, p_va->i_context_id );
221 for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
223 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
225 if( p_surface->i_id != VA_INVALID_SURFACE )
226 vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
228 free( p_va->p_surface );
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;
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 )
240 assert( i_width > 0 && i_height > 0 );
243 p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
244 if( !p_va->p_surface )
246 p_va->image.image_id = VA_INVALID_ID;
247 p_va->i_context_id = VA_INVALID_ID;
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 ) )
254 for( int i = 0; i < p_va->i_surface_count; i++ )
255 p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
259 for( int i = 0; i < p_va->i_surface_count; i++ )
261 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
263 p_surface->i_id = pi_surface_id[i];
264 p_surface->i_refcount = 0;
265 p_surface->i_order = 0;
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 ) )
273 p_va->i_context_id = VA_INVALID_ID;
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) );
283 if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
289 vlc_fourcc_t i_chroma = 0;
291 for( int i = 0; i < i_fmt_count; i++ )
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' ) )
297 if( vaCreateImage( p_va->p_display, &p_fmt[i], i_width, i_height, &p_va->image ) )
299 p_va->image.image_id = VA_INVALID_ID;
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) )
307 vaDestroyImage( p_va->p_display, p_va->image.image_id );
308 p_va->image.image_id = VA_INVALID_ID;
312 i_chroma = VLC_CODEC_YV12;
320 *pi_chroma = i_chroma;
322 CopyInitCache( &p_va->image_cache, i_width );
324 /* Setup the ffmpeg hardware context */
325 *pp_hw_ctx = &p_va->hw_ctx;
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;
333 p_va->i_surface_chroma = i_chroma;
334 p_va->i_surface_width = i_width;
335 p_va->i_surface_height = i_height;
339 DestroySurfaces( p_va );
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 )
346 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
348 if( p_va->i_surface_width == i_width &&
349 p_va->i_surface_height == i_height )
351 *pp_hw_ctx = &p_va->hw_ctx;
352 *pi_chroma = p_va->i_surface_chroma;
358 if( p_va->i_surface_width || p_va->i_surface_height )
359 DestroySurfaces( p_va );
361 if( i_width > 0 && i_height > 0 )
362 return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
366 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
368 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
370 if( !p_va->image_cache.buffer )
373 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
375 #if VA_CHECK_VERSION(0,31,0)
376 if( vaSyncSurface( p_va->p_display, i_surface_id ) )
378 if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
382 /* XXX vaDeriveImage may be better but it is not supported by
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) )
392 if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
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') )
399 bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
400 uint8_t *pp_plane[3];
403 for( int i = 0; i < 3; i++ )
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];
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 );
416 assert( i_fourcc == VA_FOURCC('N','V','1','2') );
417 uint8_t *pp_plane[2];
420 for( int i = 0; i < 2; i++ )
422 pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
423 pi_pitch[i] = p_va->image.pitches[i];
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 );
431 if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
436 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
438 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
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++ )
446 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
448 if( !p_surface->i_refcount )
451 if( p_surface->i_order < p_va->p_surface[i_old].i_order )
454 if( i >= p_va->i_surface_count )
457 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
459 p_surface->i_refcount = 1;
460 p_surface->i_order = p_va->i_surface_order++;
463 for( int i = 0; i < 4; i++ )
465 p_ff->data[i] = NULL;
466 p_ff->linesize[i] = 0;
468 if( i == 0 || i == 3 )
469 p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
473 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
475 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
477 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
479 for( int i = 0; i < p_va->i_surface_count; i++ )
481 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
483 if( p_surface->i_id == i_surface_id )
484 p_surface->i_refcount--;
488 static void Close( vlc_va_vaapi_t *p_va )
490 if( p_va->i_surface_width || p_va->i_surface_height )
491 DestroySurfaces( p_va );
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 );
500 static void Delete( vlc_va_t *p_external )
502 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
504 free( p_va->va.description );
509 vlc_va_t *vlc_va_NewVaapi( vlc_object_t *obj, int i_codec_id )
511 if( !vlc_xlib_init( obj ) )
514 vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
520 if( Open( p_va, i_codec_id ) )
527 p_va->va.setup = Setup;
529 p_va->va.release = Release;
530 p_va->va.extract = Extract;
531 p_va->va.close = Delete;
535 vlc_va_t *vlc_va_NewVaapi( vlc_object_t *obj, int i_codec_id )
538 VLC_UNUSED( i_codec_id );