1 /*****************************************************************************
2 * vaapi.c: VAAPI helpers for the libavcodec decoder
3 *****************************************************************************
4 * Copyright (C) 2009-2010 Laurent Aimar
5 * Copyright (C) 2012-2014 RĂ©mi Denis-Courmont
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>
34 #ifdef VLC_VA_BACKEND_XLIB
35 # include <vlc_xlib.h>
36 # include <va/va_x11.h>
38 #ifdef VLC_VA_BACKEND_DRM
39 # include <sys/types.h>
40 # include <sys/stat.h>
43 # include <va/va_drm.h>
45 #include <libavcodec/avcodec.h>
46 #include <libavcodec/vaapi.h>
50 #include "../../video_chroma/copy.h"
52 #ifndef VA_SURFACE_ATTRIB_SETTABLE
53 #define vaCreateSurfaces(d, f, w, h, s, ns, a, na) \
54 vaCreateSurfaces(d, w, h, f, ns, s)
57 static int Create( vlc_va_t *, AVCodecContext *, const es_format_t * );
58 static void Delete( vlc_va_t * );
61 #if defined (VLC_VA_BACKEND_XLIB)
62 set_description( N_("Video Acceleration (VA) API / X11") )
63 #elif defined (VLC_VA_BACKEND_DRM)
64 set_description( N_("Video Acceleration (VA) API / DRM") )
66 set_capability( "hw decoder", 0 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_VCODEC )
69 set_callbacks( Create, Delete )
70 add_shortcut( "vaapi" )
83 #ifdef VLC_VA_BACKEND_XLIB
84 Display *p_display_x11;
86 #ifdef VLC_VA_BACKEND_DRM
91 VAConfigID i_config_id;
92 VAContextID i_context_id;
94 struct vaapi_context hw_ctx;
99 unsigned int i_surface_order;
101 int i_surface_height;
102 vlc_fourcc_t i_surface_chroma;
104 vlc_va_surface_t *p_surface;
107 copy_cache_t image_cache;
109 bool b_supports_derive;
113 static int Open( vlc_va_t *va, int i_codec_id, int i_thread_count )
115 vlc_va_sys_t *sys = calloc( 1, sizeof(*sys) );
116 if ( unlikely(sys == NULL) )
119 VAProfile i_profile, *p_profiles_list;
120 bool b_supported_profile = false;
121 int i_profiles_nb = 0;
127 case AV_CODEC_ID_MPEG1VIDEO:
128 case AV_CODEC_ID_MPEG2VIDEO:
129 i_profile = VAProfileMPEG2Main;
130 i_surface_count = 2 + 2;
132 case AV_CODEC_ID_MPEG4:
133 i_profile = VAProfileMPEG4AdvancedSimple;
134 i_surface_count = 2+1;
136 case AV_CODEC_ID_WMV3:
137 i_profile = VAProfileVC1Main;
138 i_surface_count = 2+1;
140 case AV_CODEC_ID_VC1:
141 i_profile = VAProfileVC1Advanced;
142 i_surface_count = 2+1;
144 case AV_CODEC_ID_H264:
145 i_profile = VAProfileH264High;
146 i_surface_count = 16 + i_thread_count + 2;
153 sys->i_config_id = VA_INVALID_ID;
154 sys->i_context_id = VA_INVALID_ID;
155 sys->image.image_id = VA_INVALID_ID;
157 /* Create a VA display */
158 #ifdef VLC_VA_BACKEND_XLIB
159 sys->p_display_x11 = XOpenDisplay(NULL);
160 if( !sys->p_display_x11 )
162 msg_Err( va, "Could not connect to X server" );
166 sys->p_display = vaGetDisplay( sys->p_display_x11 );
168 #ifdef VLC_VA_BACKEND_DRM
169 sys->drm_fd = vlc_open("/dev/dri/card0", O_RDWR);
170 if( sys->drm_fd == -1 )
172 msg_Err( va, "Could not access rendering device: %m" );
176 sys->p_display = vaGetDisplayDRM( sys->drm_fd );
178 if( !sys->p_display )
180 msg_Err( va, "Could not get a VAAPI device" );
186 if( vaInitialize( sys->p_display, &major, &minor ) )
188 msg_Err( va, "Failed to initialize the VAAPI device" );
192 /* Check if the selected profile is supported */
193 i_profiles_nb = vaMaxNumProfiles( sys->p_display );
194 p_profiles_list = calloc( i_profiles_nb, sizeof( VAProfile ) );
195 if( !p_profiles_list )
198 VAStatus i_status = vaQueryConfigProfiles( sys->p_display, p_profiles_list, &i_profiles_nb );
199 if ( i_status == VA_STATUS_SUCCESS )
201 for( int i = 0; i < i_profiles_nb; i++ )
203 if ( p_profiles_list[i] == i_profile )
205 b_supported_profile = true;
210 free( p_profiles_list );
211 if ( !b_supported_profile )
213 msg_Dbg( va, "Codec and profile not supported by the hardware" );
217 /* Create a VA configuration */
218 VAConfigAttrib attrib;
219 memset( &attrib, 0, sizeof(attrib) );
220 attrib.type = VAConfigAttribRTFormat;
221 if( vaGetConfigAttributes( sys->p_display,
222 i_profile, VAEntrypointVLD, &attrib, 1 ) )
225 /* Not sure what to do if not, I don't have a way to test */
226 if( (attrib.value & VA_RT_FORMAT_YUV420) == 0 )
228 if( vaCreateConfig( sys->p_display,
229 i_profile, VAEntrypointVLD, &attrib, 1, &sys->i_config_id ) )
231 sys->i_config_id = VA_INVALID_ID;
235 sys->i_surface_count = i_surface_count;
237 sys->b_supports_derive = false;
239 vlc_mutex_init(&sys->lock);
242 va->description = (char *)vaQueryVendorString( sys->p_display );
246 if( sys->p_display != NULL )
247 vaTerminate( sys->p_display );
248 #ifdef VLC_VA_BACKEND_XLIB
249 if( sys->p_display_x11 != NULL )
250 XCloseDisplay( sys->p_display_x11 );
252 #ifdef VLC_VA_BACKEND_DRM
253 if( sys->drm_fd != -1 )
254 close( sys->drm_fd );
260 static void DestroySurfaces( vlc_va_sys_t *sys )
262 if( sys->image.image_id != VA_INVALID_ID )
264 CopyCleanCache( &sys->image_cache );
265 vaDestroyImage( sys->p_display, sys->image.image_id );
267 else if(sys->b_supports_derive)
269 CopyCleanCache( &sys->image_cache );
272 if( sys->i_context_id != VA_INVALID_ID )
273 vaDestroyContext( sys->p_display, sys->i_context_id );
275 for( int i = 0; i < sys->i_surface_count && sys->p_surface; i++ )
277 vlc_va_surface_t *p_surface = &sys->p_surface[i];
279 if( p_surface->i_id != VA_INVALID_SURFACE )
280 vaDestroySurfaces( sys->p_display, &p_surface->i_id, 1 );
282 free( sys->p_surface );
285 sys->image.image_id = VA_INVALID_ID;
286 sys->i_context_id = VA_INVALID_ID;
287 sys->p_surface = NULL;
288 sys->i_surface_width = 0;
289 sys->i_surface_height = 0;
290 vlc_mutex_destroy(&sys->lock);
293 static int CreateSurfaces( vlc_va_sys_t *sys, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
294 int i_width, int i_height )
296 assert( i_width > 0 && i_height > 0 );
299 sys->p_surface = calloc( sys->i_surface_count, sizeof(*sys->p_surface) );
300 if( !sys->p_surface )
302 sys->image.image_id = VA_INVALID_ID;
303 sys->i_context_id = VA_INVALID_ID;
305 /* Create surfaces */
306 VASurfaceID pi_surface_id[sys->i_surface_count];
307 if( vaCreateSurfaces( sys->p_display, VA_RT_FORMAT_YUV420, i_width, i_height,
308 pi_surface_id, sys->i_surface_count, NULL, 0 ) )
310 for( int i = 0; i < sys->i_surface_count; i++ )
311 sys->p_surface[i].i_id = VA_INVALID_SURFACE;
315 for( int i = 0; i < sys->i_surface_count; i++ )
317 vlc_va_surface_t *p_surface = &sys->p_surface[i];
319 p_surface->i_id = pi_surface_id[i];
320 p_surface->i_refcount = 0;
321 p_surface->i_order = 0;
322 p_surface->p_lock = &sys->lock;
325 /* Create a context */
326 if( vaCreateContext( sys->p_display, sys->i_config_id,
327 i_width, i_height, VA_PROGRESSIVE,
328 pi_surface_id, sys->i_surface_count, &sys->i_context_id ) )
330 sys->i_context_id = VA_INVALID_ID;
334 /* Find and create a supported image chroma */
335 int i_fmt_count = vaMaxNumImageFormats( sys->p_display );
336 VAImageFormat *p_fmt = calloc( i_fmt_count, sizeof(*p_fmt) );
340 if( vaQueryImageFormats( sys->p_display, p_fmt, &i_fmt_count ) )
347 if(vaDeriveImage(sys->p_display, pi_surface_id[0], &test_image) == VA_STATUS_SUCCESS)
349 sys->b_supports_derive = true;
350 vaDestroyImage(sys->p_display, test_image.image_id);
353 vlc_fourcc_t i_chroma = 0;
355 for( int i = 0; i < i_fmt_count; i++ )
357 if( p_fmt[i].fourcc == VA_FOURCC( 'Y', 'V', '1', '2' ) ||
358 p_fmt[i].fourcc == VA_FOURCC( 'I', '4', '2', '0' ) ||
359 p_fmt[i].fourcc == VA_FOURCC( 'N', 'V', '1', '2' ) )
361 if( vaCreateImage( sys->p_display, &p_fmt[i], i_width, i_height, &sys->image ) )
363 sys->image.image_id = VA_INVALID_ID;
366 /* Validate that vaGetImage works with this format */
367 if( vaGetImage( sys->p_display, pi_surface_id[0],
368 0, 0, i_width, i_height,
369 sys->image.image_id) )
371 vaDestroyImage( sys->p_display, sys->image.image_id );
372 sys->image.image_id = VA_INVALID_ID;
376 i_chroma = VLC_CODEC_YV12;
384 *pi_chroma = i_chroma;
386 if(sys->b_supports_derive)
388 vaDestroyImage( sys->p_display, sys->image.image_id );
389 sys->image.image_id = VA_INVALID_ID;
392 if( unlikely(CopyInitCache( &sys->image_cache, i_width )) )
395 /* Setup the ffmpeg hardware context */
396 *pp_hw_ctx = &sys->hw_ctx;
398 memset( &sys->hw_ctx, 0, sizeof(sys->hw_ctx) );
399 sys->hw_ctx.display = sys->p_display;
400 sys->hw_ctx.config_id = sys->i_config_id;
401 sys->hw_ctx.context_id = sys->i_context_id;
404 sys->i_surface_chroma = i_chroma;
405 sys->i_surface_width = i_width;
406 sys->i_surface_height = i_height;
410 DestroySurfaces( sys );
414 static int Setup( vlc_va_t *va, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
415 int i_width, int i_height )
417 vlc_va_sys_t *sys = va->sys;
419 if( sys->i_surface_width == i_width &&
420 sys->i_surface_height == i_height )
422 *pp_hw_ctx = &sys->hw_ctx;
423 *pi_chroma = sys->i_surface_chroma;
429 if( sys->i_surface_width || sys->i_surface_height )
430 DestroySurfaces( sys );
432 if( i_width > 0 && i_height > 0 )
433 return CreateSurfaces( sys, pp_hw_ctx, pi_chroma, i_width, i_height );
438 static int Extract( vlc_va_t *va, picture_t *p_picture, void *opaque,
441 vlc_va_sys_t *sys = va->sys;
442 VASurfaceID i_surface_id = (VASurfaceID)(uintptr_t)data;
444 #if VA_CHECK_VERSION(0,31,0)
445 if( vaSyncSurface( sys->p_display, i_surface_id ) )
447 if( vaSyncSurface( sys->p_display, sys->i_context_id, i_surface_id ) )
451 if(sys->b_supports_derive)
453 if(vaDeriveImage(sys->p_display, i_surface_id, &(sys->image)) != VA_STATUS_SUCCESS)
458 if( vaGetImage( sys->p_display, i_surface_id,
459 0, 0, sys->i_surface_width, sys->i_surface_height,
460 sys->image.image_id) )
465 if( vaMapBuffer( sys->p_display, sys->image.buf, &p_base ) )
468 const uint32_t i_fourcc = sys->image.format.fourcc;
469 if( i_fourcc == VA_FOURCC('Y','V','1','2') ||
470 i_fourcc == VA_FOURCC('I','4','2','0') )
472 bool b_swap_uv = i_fourcc == VA_FOURCC('I','4','2','0');
473 uint8_t *pp_plane[3];
476 for( int i = 0; i < 3; i++ )
478 const int i_src_plane = (b_swap_uv && i != 0) ? (3 - i) : i;
479 pp_plane[i] = (uint8_t*)p_base + sys->image.offsets[i_src_plane];
480 pi_pitch[i] = sys->image.pitches[i_src_plane];
482 CopyFromYv12( p_picture, pp_plane, pi_pitch,
483 sys->i_surface_width,
484 sys->i_surface_height,
489 assert( i_fourcc == VA_FOURCC('N','V','1','2') );
490 uint8_t *pp_plane[2];
493 for( int i = 0; i < 2; i++ )
495 pp_plane[i] = (uint8_t*)p_base + sys->image.offsets[i];
496 pi_pitch[i] = sys->image.pitches[i];
498 CopyFromNv12( p_picture, pp_plane, pi_pitch,
499 sys->i_surface_width,
500 sys->i_surface_height,
504 if( vaUnmapBuffer( sys->p_display, sys->image.buf ) )
507 if(sys->b_supports_derive)
509 vaDestroyImage( sys->p_display, sys->image.image_id );
510 sys->image.image_id = VA_INVALID_ID;
516 static int Get( vlc_va_t *va, void **opaque, uint8_t **data )
518 vlc_va_sys_t *sys = va->sys;
522 vlc_mutex_lock( &sys->lock );
523 /* Grab an unused surface, in case none are, try the oldest
524 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
525 for( i = 0, i_old = 0; i < sys->i_surface_count; i++ )
527 vlc_va_surface_t *p_surface = &sys->p_surface[i];
529 if( !p_surface->i_refcount )
532 if( p_surface->i_order < sys->p_surface[i_old].i_order )
535 if( i >= sys->i_surface_count )
537 vlc_mutex_unlock( &sys->lock );
539 vlc_va_surface_t *p_surface = &sys->p_surface[i];
541 p_surface->i_refcount = 1;
542 p_surface->i_order = sys->i_surface_order++;
543 *data = (void *)(uintptr_t)p_surface->i_id;
548 static void Release( void *opaque, uint8_t *data )
550 vlc_va_surface_t *p_surface = opaque;
552 vlc_mutex_lock( p_surface->p_lock );
553 p_surface->i_refcount--;
554 vlc_mutex_unlock( p_surface->p_lock );
558 static void Close( vlc_va_sys_t *sys )
560 if( sys->i_surface_width || sys->i_surface_height )
561 DestroySurfaces( sys );
563 if( sys->i_config_id != VA_INVALID_ID )
564 vaDestroyConfig( sys->p_display, sys->i_config_id );
565 vaTerminate( sys->p_display );
566 #ifdef VLC_VA_BACKEND_XLIB
567 XCloseDisplay( sys->p_display_x11 );
569 #ifdef VLC_VA_BACKEND_DRM
570 close( sys->drm_fd );
574 static void Delete( vlc_va_t *va )
576 vlc_va_sys_t *sys = va->sys;
581 static int Create( vlc_va_t *p_va, AVCodecContext *ctx,
582 const es_format_t *fmt )
584 #ifdef VLC_VA_BACKEND_XLIB
585 if( !vlc_xlib_init( VLC_OBJECT(p_va) ) )
587 msg_Warn( p_va, "Ignoring VA API" );
594 int err = Open( p_va, ctx->codec_id, ctx->thread_count );
598 /* Only VLD supported */
599 p_va->pix_fmt = PIX_FMT_VAAPI_VLD;
602 p_va->release = Release;
603 p_va->extract = Extract;