1 /*****************************************************************************
2 * vaapi.c: VAAPI helpers for the ffmpeg 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 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
33 # include <libavcodec/avcodec.h>
34 #elif defined(HAVE_FFMPEG_AVCODEC_H)
35 # include <ffmpeg/avcodec.h>
44 #ifdef HAVE_AVCODEC_VAAPI
46 #include <libavcodec/vaapi.h>
49 #include <va/va_x11.h>
64 Display *p_display_x11;
67 VAConfigID i_config_id;
68 VAContextID i_context_id;
70 struct vaapi_context hw_ctx;
78 unsigned int i_surface_order;
81 vlc_fourcc_t i_surface_chroma;
83 vlc_va_surface_t *p_surface;
86 copy_cache_t image_cache;
90 static vlc_va_vaapi_t *vlc_va_vaapi_Get( void *p_va )
96 static int Open( vlc_va_vaapi_t *p_va, int i_codec_id )
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 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;
135 /* Create a VA display */
136 p_va->p_display_x11 = XOpenDisplay(NULL);
137 if( !p_va->p_display_x11 )
140 p_va->p_display = vaGetDisplay( p_va->p_display_x11 );
141 if( !p_va->p_display )
144 if( vaInitialize( p_va->p_display, &p_va->i_version_major, &p_va->i_version_minor ) )
147 /* Create a VA configuration */
148 VAConfigAttrib attrib;
149 memset( &attrib, 0, sizeof(attrib) );
150 attrib.type = VAConfigAttribRTFormat;
151 if( vaGetConfigAttributes( p_va->p_display,
152 i_profile, VAEntrypointVLD, &attrib, 1 ) )
155 /* Not sure what to do if not, I don't have a way to test */
156 if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
158 if( vaCreateConfig( p_va->p_display,
159 i_profile, VAEntrypointVLD, &attrib, 1, &p_va->i_config_id ) )
161 p_va->i_config_id = VA_INVALID_ID;
165 p_va->i_surface_count = i_surface_count;
167 if( asprintf( &p_va->va.description, "VA API version %d.%d",
168 p_va->i_version_major, p_va->i_version_minor ) < 0 )
169 p_va->va.description = NULL;
177 static void DestroySurfaces( vlc_va_vaapi_t *p_va )
179 if( p_va->image.image_id != VA_INVALID_ID )
181 CopyCleanCache( &p_va->image_cache );
182 vaDestroyImage( p_va->p_display, p_va->image.image_id );
185 if( p_va->i_context_id != VA_INVALID_ID )
186 vaDestroyContext( p_va->p_display, p_va->i_context_id );
188 for( int i = 0; i < p_va->i_surface_count && p_va->p_surface; i++ )
190 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
192 if( p_surface->i_id != VA_INVALID_SURFACE )
193 vaDestroySurfaces( p_va->p_display, &p_surface->i_id, 1 );
195 free( p_va->p_surface );
198 p_va->image.image_id = VA_INVALID_ID;
199 p_va->i_context_id = VA_INVALID_ID;
200 p_va->p_surface = NULL;
201 p_va->i_surface_width = 0;
202 p_va->i_surface_height = 0;
204 static int CreateSurfaces( vlc_va_vaapi_t *p_va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
205 int i_width, int i_height )
207 assert( i_width > 0 && i_height > 0 );
210 p_va->p_surface = calloc( p_va->i_surface_count, sizeof(*p_va->p_surface) );
211 if( !p_va->p_surface )
213 p_va->image.image_id = VA_INVALID_ID;
214 p_va->i_context_id = VA_INVALID_ID;
216 /* Create surfaces */
217 VASurfaceID pi_surface_id[p_va->i_surface_count];
218 if( vaCreateSurfaces( p_va->p_display, i_width, i_height, VA_RT_FORMAT_YUV420,
219 p_va->i_surface_count, pi_surface_id ) )
221 for( int i = 0; i < p_va->i_surface_count; i++ )
222 p_va->p_surface[i].i_id = VA_INVALID_SURFACE;
226 for( int i = 0; i < p_va->i_surface_count; i++ )
228 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
230 p_surface->i_id = pi_surface_id[i];
231 p_surface->i_refcount = 0;
232 p_surface->i_order = 0;
235 /* Create a context */
236 if( vaCreateContext( p_va->p_display, p_va->i_config_id,
237 i_width, i_height, VA_PROGRESSIVE,
238 pi_surface_id, p_va->i_surface_count, &p_va->i_context_id ) )
240 p_va->i_context_id = VA_INVALID_ID;
244 /* Find and create a supported image chroma */
245 int i_fmt_count = vaMaxNumImageFormats( p_va->p_display );
246 VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
250 if( vaQueryImageFormats( p_va->p_display, p_fmt, &i_fmt_count ) )
256 vlc_fourcc_t i_chroma = 0;
258 for( int i = 0; i < i_fmt_count; i++ )
260 if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
261 p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
262 p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
264 if( vaCreateImage( p_va->p_display, &p_fmt[i], i_width, i_height, &p_va->image ) )
266 p_va->image.image_id = VA_INVALID_ID;
269 /* Validate that vaGetImage works with this format */
270 if( vaGetImage( p_va->p_display, pi_surface_id[0],
271 0, 0, i_width, i_height,
272 p_va->image.image_id) )
274 vaDestroyImage( p_va->p_display, p_va->image.image_id );
275 p_va->image.image_id = VA_INVALID_ID;
279 i_chroma = VLC_CODEC_YV12;
287 *pi_chroma = i_chroma;
289 CopyInitCache( &p_va->image_cache, i_width );
291 /* Setup the ffmpeg hardware context */
292 *pp_hw_ctx = &p_va->hw_ctx;
294 memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
295 p_va->hw_ctx.display = p_va->p_display;
296 p_va->hw_ctx.config_id = p_va->i_config_id;
297 p_va->hw_ctx.context_id = p_va->i_context_id;
300 p_va->i_surface_chroma = i_chroma;
301 p_va->i_surface_width = i_width;
302 p_va->i_surface_height = i_height;
306 DestroySurfaces( p_va );
310 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
311 int i_width, int i_height )
313 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
315 if( p_va->i_surface_width == i_width &&
316 p_va->i_surface_height == i_height )
318 *pp_hw_ctx = &p_va->hw_ctx;
319 *pi_chroma = p_va->i_surface_chroma;
325 if( p_va->i_surface_width || p_va->i_surface_height )
326 DestroySurfaces( p_va );
328 if( i_width > 0 && i_height > 0 )
329 return CreateSurfaces( p_va, pp_hw_ctx, pi_chroma, i_width, i_height );
333 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
335 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
337 if( !p_va->image_cache.buffer )
340 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
342 #if VA_CHECK_VERSION(0,31,0)
343 if( vaSyncSurface( p_va->p_display, i_surface_id ) )
345 if( vaSyncSurface( p_va->p_display, p_va->i_context_id, i_surface_id ) )
349 /* XXX vaDeriveImage may be better but it is not supported by
353 if( vaGetImage( p_va->p_display, i_surface_id,
354 0, 0, p_va->i_surface_width, p_va->i_surface_height,
355 p_va->image.image_id) )
359 if( vaMapBuffer( p_va->p_display, p_va->image.buf, &p_base ) )
362 const uint32_t i_fourcc = p_va->image.format.fourcc;
363 if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
364 i_fourcc == VA_FOURCC('I','4','2','0') )
366 bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
367 uint8_t *pp_plane[3];
370 for( int i = 0; i < 3; i++ )
372 const int i_src_plane = (b_swap_uv && i != 0) ? (3 - i) : i;
373 pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i_src_plane];
374 pi_pitch[i] = p_va->image.pitches[i_src_plane];
376 CopyFromYv12( p_picture, pp_plane, pi_pitch,
377 p_va->i_surface_width,
378 p_va->i_surface_height,
379 &p_va->image_cache );
383 assert( i_fourcc == VA_FOURCC('N','V','1','2') );
384 uint8_t *pp_plane[2];
387 for( int i = 0; i < 2; i++ )
389 pp_plane[i] = (uint8_t*)p_base + p_va->image.offsets[i];
390 pi_pitch[i] = p_va->image.pitches[i];
392 CopyFromNv12( p_picture, pp_plane, pi_pitch,
393 p_va->i_surface_width,
394 p_va->i_surface_height,
395 &p_va->image_cache );
398 if( vaUnmapBuffer( p_va->p_display, p_va->image.buf ) )
403 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
405 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
409 /* Grab an unused surface, in case none are, try the oldest
410 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
411 for( i = 0, i_old = 0; i < p_va->i_surface_count; i++ )
413 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
415 if( !p_surface->i_refcount )
418 if( p_surface->i_order < p_va->p_surface[i_old].i_order )
421 if( i >= p_va->i_surface_count )
424 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
426 p_surface->i_refcount = 1;
427 p_surface->i_order = p_va->i_surface_order++;
430 for( int i = 0; i < 4; i++ )
432 p_ff->data[i] = NULL;
433 p_ff->linesize[i] = 0;
435 if( i == 0 || i == 3 )
436 p_ff->data[i] = (void*)(uintptr_t)p_surface->i_id;/* Yummie */
440 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
442 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
444 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)p_ff->data[3];
446 for( int i = 0; i < p_va->i_surface_count; i++ )
448 vlc_va_surface_t *p_surface = &p_va->p_surface[i];
450 if( p_surface->i_id == i_surface_id )
451 p_surface->i_refcount--;
455 static void Close( vlc_va_vaapi_t *p_va )
457 if( p_va->i_surface_width || p_va->i_surface_height )
458 DestroySurfaces( p_va );
460 if( p_va->i_config_id != VA_INVALID_ID )
461 vaDestroyConfig( p_va->p_display, p_va->i_config_id );
462 if( p_va->p_display )
463 vaTerminate( p_va->p_display );
464 if( p_va->p_display_x11 )
465 XCloseDisplay( p_va->p_display_x11 );
467 static void Delete( vlc_va_t *p_external )
469 vlc_va_vaapi_t *p_va = vlc_va_vaapi_Get(p_external);
471 free( p_va->va.description );
476 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
480 vlc_global_lock( VLC_XLIB_MUTEX );
481 fail = !XInitThreads();
482 vlc_global_unlock( VLC_XLIB_MUTEX );
486 vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
490 if( Open( p_va, i_codec_id ) )
497 p_va->va.setup = Setup;
499 p_va->va.release = Release;
500 p_va->va.extract = Extract;
501 p_va->va.close = Delete;
505 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
507 VLC_UNUSED( i_codec_id );