2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/avassert.h"
20 #include "libavutil/common.h"
24 #include "vaapi_decode.h"
27 int ff_vaapi_decode_make_param_buffer(AVCodecContext
*avctx
,
28 VAAPIDecodePicture
*pic
,
33 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
37 av_assert0(pic
->nb_param_buffers
+ 1 <= MAX_PARAM_BUFFERS
);
39 vas
= vaCreateBuffer(ctx
->hwctx
->display
, ctx
->va_context
,
40 type
, size
, 1, (void*)data
, &buffer
);
41 if (vas
!= VA_STATUS_SUCCESS
) {
42 av_log(avctx
, AV_LOG_ERROR
, "Failed to create parameter "
43 "buffer (type %d): %d (%s).\n",
44 type
, vas
, vaErrorStr(vas
));
48 pic
->param_buffers
[pic
->nb_param_buffers
++] = buffer
;
50 av_log(avctx
, AV_LOG_DEBUG
, "Param buffer (type %d, %zu bytes) "
51 "is %#x.\n", type
, size
, buffer
);
56 int ff_vaapi_decode_make_slice_buffer(AVCodecContext
*avctx
,
57 VAAPIDecodePicture
*pic
,
58 const void *params_data
,
60 const void *slice_data
,
63 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
67 av_assert0(pic
->nb_slices
<= pic
->slices_allocated
);
68 if (pic
->nb_slices
== pic
->slices_allocated
) {
69 if (pic
->slices_allocated
> 0)
70 pic
->slices_allocated
*= 2;
72 pic
->slices_allocated
= 64;
75 av_realloc_array(pic
->slice_buffers
,
76 pic
->slices_allocated
,
77 2 * sizeof(*pic
->slice_buffers
));
78 if (!pic
->slice_buffers
)
79 return AVERROR(ENOMEM
);
81 av_assert0(pic
->nb_slices
+ 1 <= pic
->slices_allocated
);
83 index
= 2 * pic
->nb_slices
;
85 vas
= vaCreateBuffer(ctx
->hwctx
->display
, ctx
->va_context
,
86 VASliceParameterBufferType
,
87 params_size
, 1, (void*)params_data
,
88 &pic
->slice_buffers
[index
]);
89 if (vas
!= VA_STATUS_SUCCESS
) {
90 av_log(avctx
, AV_LOG_ERROR
, "Failed to create slice "
91 "parameter buffer: %d (%s).\n", vas
, vaErrorStr(vas
));
95 av_log(avctx
, AV_LOG_DEBUG
, "Slice %d param buffer (%zu bytes) "
96 "is %#x.\n", pic
->nb_slices
, params_size
,
97 pic
->slice_buffers
[index
]);
99 vas
= vaCreateBuffer(ctx
->hwctx
->display
, ctx
->va_context
,
100 VASliceDataBufferType
,
101 slice_size
, 1, (void*)slice_data
,
102 &pic
->slice_buffers
[index
+ 1]);
103 if (vas
!= VA_STATUS_SUCCESS
) {
104 av_log(avctx
, AV_LOG_ERROR
, "Failed to create slice "
105 "data buffer (size %zu): %d (%s).\n",
106 slice_size
, vas
, vaErrorStr(vas
));
107 vaDestroyBuffer(ctx
->hwctx
->display
,
108 pic
->slice_buffers
[index
]);
112 av_log(avctx
, AV_LOG_DEBUG
, "Slice %d data buffer (%zu bytes) "
113 "is %#x.\n", pic
->nb_slices
, slice_size
,
114 pic
->slice_buffers
[index
+ 1]);
120 static void ff_vaapi_decode_destroy_buffers(AVCodecContext
*avctx
,
121 VAAPIDecodePicture
*pic
)
123 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
127 for (i
= 0; i
< pic
->nb_param_buffers
; i
++) {
128 vas
= vaDestroyBuffer(ctx
->hwctx
->display
,
129 pic
->param_buffers
[i
]);
130 if (vas
!= VA_STATUS_SUCCESS
) {
131 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy "
132 "parameter buffer %#x: %d (%s).\n",
133 pic
->param_buffers
[i
], vas
, vaErrorStr(vas
));
137 for (i
= 0; i
< 2 * pic
->nb_slices
; i
++) {
138 vas
= vaDestroyBuffer(ctx
->hwctx
->display
,
139 pic
->slice_buffers
[i
]);
140 if (vas
!= VA_STATUS_SUCCESS
) {
141 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy slice "
142 "slice buffer %#x: %d (%s).\n",
143 pic
->slice_buffers
[i
], vas
, vaErrorStr(vas
));
148 int ff_vaapi_decode_issue(AVCodecContext
*avctx
,
149 VAAPIDecodePicture
*pic
)
151 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
155 av_log(avctx
, AV_LOG_DEBUG
, "Decode to surface %#x.\n",
156 pic
->output_surface
);
158 vas
= vaBeginPicture(ctx
->hwctx
->display
, ctx
->va_context
,
159 pic
->output_surface
);
160 if (vas
!= VA_STATUS_SUCCESS
) {
161 av_log(avctx
, AV_LOG_ERROR
, "Failed to begin picture decode "
162 "issue: %d (%s).\n", vas
, vaErrorStr(vas
));
164 goto fail_with_picture
;
167 vas
= vaRenderPicture(ctx
->hwctx
->display
, ctx
->va_context
,
168 pic
->param_buffers
, pic
->nb_param_buffers
);
169 if (vas
!= VA_STATUS_SUCCESS
) {
170 av_log(avctx
, AV_LOG_ERROR
, "Failed to upload decode "
171 "parameters: %d (%s).\n", vas
, vaErrorStr(vas
));
173 goto fail_with_picture
;
176 vas
= vaRenderPicture(ctx
->hwctx
->display
, ctx
->va_context
,
177 pic
->slice_buffers
, 2 * pic
->nb_slices
);
178 if (vas
!= VA_STATUS_SUCCESS
) {
179 av_log(avctx
, AV_LOG_ERROR
, "Failed to upload slices: "
180 "%d (%s).\n", vas
, vaErrorStr(vas
));
182 goto fail_with_picture
;
185 vas
= vaEndPicture(ctx
->hwctx
->display
, ctx
->va_context
);
186 if (vas
!= VA_STATUS_SUCCESS
) {
187 av_log(avctx
, AV_LOG_ERROR
, "Failed to end picture decode "
188 "issue: %d (%s).\n", vas
, vaErrorStr(vas
));
190 if (ctx
->hwctx
->driver_quirks
&
191 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
)
197 if (ctx
->hwctx
->driver_quirks
&
198 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
)
199 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
201 pic
->nb_param_buffers
= 0;
203 pic
->slices_allocated
= 0;
204 av_freep(&pic
->slice_buffers
);
209 vas
= vaEndPicture(ctx
->hwctx
->display
, ctx
->va_context
);
210 if (vas
!= VA_STATUS_SUCCESS
) {
211 av_log(avctx
, AV_LOG_ERROR
, "Failed to end picture decode "
212 "after error: %d (%s).\n", vas
, vaErrorStr(vas
));
215 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
220 int ff_vaapi_decode_cancel(AVCodecContext
*avctx
,
221 VAAPIDecodePicture
*pic
)
223 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
225 pic
->nb_param_buffers
= 0;
227 pic
->slices_allocated
= 0;
228 av_freep(&pic
->slice_buffers
);
233 static const struct {
234 enum AVCodecID codec_id
;
236 VAProfile va_profile
;
237 } vaapi_profile_map
[] = {
238 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
239 MAP(MPEG2VIDEO
, MPEG2_SIMPLE
, MPEG2Simple
),
240 MAP(MPEG2VIDEO
, MPEG2_MAIN
, MPEG2Main
),
241 MAP(H263
, UNKNOWN
, H263Baseline
),
242 MAP(MPEG4
, MPEG4_SIMPLE
, MPEG4Simple
),
243 MAP(MPEG4
, MPEG4_ADVANCED_SIMPLE
,
244 MPEG4AdvancedSimple
),
245 MAP(MPEG4
, MPEG4_MAIN
, MPEG4Main
),
246 MAP(H264
, H264_CONSTRAINED_BASELINE
,
247 H264ConstrainedBaseline
),
248 MAP(H264
, H264_BASELINE
, H264Baseline
),
249 MAP(H264
, H264_MAIN
, H264Main
),
250 MAP(H264
, H264_HIGH
, H264High
),
251 #if VA_CHECK_VERSION(0, 37, 0)
252 MAP(HEVC
, HEVC_MAIN
, HEVCMain
),
253 MAP(HEVC
, HEVC_MAIN_10
, HEVCMain10
),
255 MAP(WMV3
, VC1_SIMPLE
, VC1Simple
),
256 MAP(WMV3
, VC1_MAIN
, VC1Main
),
257 MAP(WMV3
, VC1_COMPLEX
, VC1Advanced
),
258 MAP(WMV3
, VC1_ADVANCED
, VC1Advanced
),
259 MAP(VC1
, VC1_SIMPLE
, VC1Simple
),
260 MAP(VC1
, VC1_MAIN
, VC1Main
),
261 MAP(VC1
, VC1_COMPLEX
, VC1Advanced
),
262 MAP(VC1
, VC1_ADVANCED
, VC1Advanced
),
263 #if VA_CHECK_VERSION(0, 35, 0)
264 MAP(VP8
, UNKNOWN
, VP8Version0_3
),
266 #if VA_CHECK_VERSION(0, 38, 0)
267 MAP(VP9
, VP9_0
, VP9Profile0
),
272 static int vaapi_decode_make_config(AVCodecContext
*avctx
)
274 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
276 AVVAAPIHWConfig
*hwconfig
= NULL
;
277 AVHWFramesConstraints
*constraints
= NULL
;
280 const AVCodecDescriptor
*codec_desc
;
281 VAProfile profile
, *profile_list
= NULL
;
282 int profile_count
, exact_match
, alt_profile
;
284 // Allowing a profile mismatch can be useful because streams may
285 // over-declare their required capabilities - in particular, many
286 // H.264 baseline profile streams (notably some of those in FATE)
287 // only use the feature set of constrained baseline. This flag
288 // would have to be be set by some external means in order to
289 // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
290 int allow_profile_mismatch
= 0;
292 codec_desc
= avcodec_descriptor_get(avctx
->codec_id
);
294 err
= AVERROR(EINVAL
);
298 profile_count
= vaMaxNumProfiles(ctx
->hwctx
->display
);
299 profile_list
= av_malloc_array(profile_count
,
302 err
= AVERROR(ENOMEM
);
306 vas
= vaQueryConfigProfiles(ctx
->hwctx
->display
,
307 profile_list
, &profile_count
);
308 if (vas
!= VA_STATUS_SUCCESS
) {
309 av_log(avctx
, AV_LOG_ERROR
, "Failed to query profiles: "
310 "%d (%s).\n", vas
, vaErrorStr(vas
));
311 err
= AVERROR(ENOSYS
);
315 profile
= VAProfileNone
;
318 for (i
= 0; i
< FF_ARRAY_ELEMS(vaapi_profile_map
); i
++) {
319 int profile_match
= 0;
320 if (avctx
->codec_id
!= vaapi_profile_map
[i
].codec_id
)
322 if (avctx
->profile
== vaapi_profile_map
[i
].codec_profile
||
323 vaapi_profile_map
[i
].codec_profile
== FF_PROFILE_UNKNOWN
)
325 profile
= vaapi_profile_map
[i
].va_profile
;
326 for (j
= 0; j
< profile_count
; j
++) {
327 if (profile
== profile_list
[j
]) {
328 exact_match
= profile_match
;
332 if (j
< profile_count
) {
335 alt_profile
= vaapi_profile_map
[i
].codec_profile
;
338 av_freep(&profile_list
);
340 if (profile
== VAProfileNone
) {
341 av_log(avctx
, AV_LOG_ERROR
, "No support for codec %s "
342 "profile %d.\n", codec_desc
->name
, avctx
->profile
);
343 err
= AVERROR(ENOSYS
);
347 if (allow_profile_mismatch
) {
348 av_log(avctx
, AV_LOG_VERBOSE
, "Codec %s profile %d not "
349 "supported for hardware decode.\n",
350 codec_desc
->name
, avctx
->profile
);
351 av_log(avctx
, AV_LOG_WARNING
, "Using possibly-"
352 "incompatible profile %d instead.\n",
355 av_log(avctx
, AV_LOG_VERBOSE
, "Codec %s profile %d not "
356 "supported for hardware decode.\n",
357 codec_desc
->name
, avctx
->profile
);
358 err
= AVERROR(EINVAL
);
363 ctx
->va_profile
= profile
;
364 ctx
->va_entrypoint
= VAEntrypointVLD
;
366 vas
= vaCreateConfig(ctx
->hwctx
->display
, ctx
->va_profile
,
367 ctx
->va_entrypoint
, NULL
, 0,
369 if (vas
!= VA_STATUS_SUCCESS
) {
370 av_log(avctx
, AV_LOG_ERROR
, "Failed to create decode "
371 "configuration: %d (%s).\n", vas
, vaErrorStr(vas
));
376 hwconfig
= av_hwdevice_hwconfig_alloc(ctx
->frames
->device_ref
);
378 err
= AVERROR(ENOMEM
);
381 hwconfig
->config_id
= ctx
->va_config
;
384 av_hwdevice_get_hwframe_constraints(ctx
->frames
->device_ref
,
389 if (avctx
->coded_width
< constraints
->min_width
||
390 avctx
->coded_height
< constraints
->min_height
||
391 avctx
->coded_width
> constraints
->max_width
||
392 avctx
->coded_height
> constraints
->max_height
) {
393 av_log(avctx
, AV_LOG_ERROR
, "Hardware does not support image "
394 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
395 avctx
->coded_width
, avctx
->coded_height
,
396 constraints
->min_width
, constraints
->max_width
,
397 constraints
->min_height
, constraints
->max_height
);
398 err
= AVERROR(EINVAL
);
403 av_hwframe_constraints_free(&constraints
);
409 av_hwframe_constraints_free(&constraints
);
411 if (ctx
->va_config
!= VA_INVALID_ID
) {
412 vaDestroyConfig(ctx
->hwctx
->display
, ctx
->va_config
);
413 ctx
->va_config
= VA_INVALID_ID
;
415 av_freep(&profile_list
);
419 int ff_vaapi_decode_init(AVCodecContext
*avctx
)
421 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
425 ctx
->va_config
= VA_INVALID_ID
;
426 ctx
->va_context
= VA_INVALID_ID
;
428 #if FF_API_VAAPI_CONTEXT
429 if (avctx
->hwaccel_context
) {
430 av_log(avctx
, AV_LOG_WARNING
, "Using deprecated struct "
431 "vaapi_context in decode.\n");
433 ctx
->have_old_context
= 1;
434 ctx
->old_context
= avctx
->hwaccel_context
;
436 // Really we only want the VAAPI device context, but this
437 // allocates a whole generic device context because we don't
438 // have any other way to determine how big it should be.
440 av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI
);
441 if (!ctx
->device_ref
) {
442 err
= AVERROR(ENOMEM
);
445 ctx
->device
= (AVHWDeviceContext
*)ctx
->device_ref
->data
;
446 ctx
->hwctx
= ctx
->device
->hwctx
;
448 ctx
->hwctx
->display
= ctx
->old_context
->display
;
450 // The old VAAPI decode setup assumed this quirk was always
451 // present, so set it here to avoid the behaviour changing.
452 ctx
->hwctx
->driver_quirks
=
453 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
;
457 if (avctx
->hw_frames_ctx
) {
458 // This structure has a shorter lifetime than the enclosing
459 // AVCodecContext, so we inherit the references from there
460 // and do not need to make separate ones.
462 ctx
->frames
= (AVHWFramesContext
*)avctx
->hw_frames_ctx
->data
;
463 ctx
->hwfc
= ctx
->frames
->hwctx
;
465 ctx
->device
= ctx
->frames
->device_ctx
;
466 ctx
->hwctx
= ctx
->device
->hwctx
;
469 av_log(avctx
, AV_LOG_ERROR
, "A hardware frames context is "
470 "required for VAAPI decoding.\n");
471 err
= AVERROR(EINVAL
);
475 #if FF_API_VAAPI_CONTEXT
476 if (ctx
->have_old_context
) {
477 ctx
->va_config
= ctx
->old_context
->config_id
;
478 ctx
->va_context
= ctx
->old_context
->context_id
;
480 av_log(avctx
, AV_LOG_DEBUG
, "Using user-supplied decoder "
481 "context: %#x/%#x.\n", ctx
->va_config
, ctx
->va_context
);
485 err
= vaapi_decode_make_config(avctx
);
489 vas
= vaCreateContext(ctx
->hwctx
->display
, ctx
->va_config
,
490 avctx
->coded_width
, avctx
->coded_height
,
492 ctx
->hwfc
->surface_ids
,
493 ctx
->hwfc
->nb_surfaces
,
495 if (vas
!= VA_STATUS_SUCCESS
) {
496 av_log(avctx
, AV_LOG_ERROR
, "Failed to create decode "
497 "context: %d (%s).\n", vas
, vaErrorStr(vas
));
502 av_log(avctx
, AV_LOG_DEBUG
, "Decode context initialised: "
503 "%#x/%#x.\n", ctx
->va_config
, ctx
->va_context
);
504 #if FF_API_VAAPI_CONTEXT
511 ff_vaapi_decode_uninit(avctx
);
515 int ff_vaapi_decode_uninit(AVCodecContext
*avctx
)
517 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
520 #if FF_API_VAAPI_CONTEXT
521 if (ctx
->have_old_context
) {
522 av_buffer_unref(&ctx
->device_ref
);
526 if (ctx
->va_context
!= VA_INVALID_ID
) {
527 vas
= vaDestroyContext(ctx
->hwctx
->display
, ctx
->va_context
);
528 if (vas
!= VA_STATUS_SUCCESS
) {
529 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy decode "
530 "context %#x: %d (%s).\n",
531 ctx
->va_context
, vas
, vaErrorStr(vas
));
534 if (ctx
->va_config
!= VA_INVALID_ID
) {
535 vas
= vaDestroyConfig(ctx
->hwctx
->display
, ctx
->va_config
);
536 if (vas
!= VA_STATUS_SUCCESS
) {
537 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy decode "
538 "configuration %#x: %d (%s).\n",
539 ctx
->va_config
, vas
, vaErrorStr(vas
));
543 #if FF_API_VAAPI_CONTEXT