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 for (i
= 0; i
< pic
->nb_param_buffers
; i
++)
159 vaUnmapBuffer(ctx
->hwctx
->display
, pic
->param_buffers
[i
]);
161 vas
= vaBeginPicture(ctx
->hwctx
->display
, ctx
->va_context
,
162 pic
->output_surface
);
163 if (vas
!= VA_STATUS_SUCCESS
) {
164 av_log(avctx
, AV_LOG_ERROR
, "Failed to begin picture decode "
165 "issue: %d (%s).\n", vas
, vaErrorStr(vas
));
167 goto fail_with_picture
;
170 vas
= vaRenderPicture(ctx
->hwctx
->display
, ctx
->va_context
,
171 pic
->param_buffers
, pic
->nb_param_buffers
);
172 if (vas
!= VA_STATUS_SUCCESS
) {
173 av_log(avctx
, AV_LOG_ERROR
, "Failed to upload decode "
174 "parameters: %d (%s).\n", vas
, vaErrorStr(vas
));
176 goto fail_with_picture
;
179 vas
= vaRenderPicture(ctx
->hwctx
->display
, ctx
->va_context
,
180 pic
->slice_buffers
, 2 * pic
->nb_slices
);
181 if (vas
!= VA_STATUS_SUCCESS
) {
182 av_log(avctx
, AV_LOG_ERROR
, "Failed to upload slices: "
183 "%d (%s).\n", vas
, vaErrorStr(vas
));
185 goto fail_with_picture
;
188 vas
= vaEndPicture(ctx
->hwctx
->display
, ctx
->va_context
);
189 if (vas
!= VA_STATUS_SUCCESS
) {
190 av_log(avctx
, AV_LOG_ERROR
, "Failed to end picture decode "
191 "issue: %d (%s).\n", vas
, vaErrorStr(vas
));
193 if (ctx
->hwctx
->driver_quirks
&
194 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
)
200 if (ctx
->hwctx
->driver_quirks
&
201 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
)
202 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
204 pic
->nb_param_buffers
= 0;
206 pic
->slices_allocated
= 0;
207 av_freep(&pic
->slice_buffers
);
212 vas
= vaEndPicture(ctx
->hwctx
->display
, ctx
->va_context
);
213 if (vas
!= VA_STATUS_SUCCESS
) {
214 av_log(avctx
, AV_LOG_ERROR
, "Failed to end picture decode "
215 "after error: %d (%s).\n", vas
, vaErrorStr(vas
));
218 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
223 int ff_vaapi_decode_cancel(AVCodecContext
*avctx
,
224 VAAPIDecodePicture
*pic
)
226 ff_vaapi_decode_destroy_buffers(avctx
, pic
);
228 pic
->nb_param_buffers
= 0;
230 pic
->slices_allocated
= 0;
231 av_freep(&pic
->slice_buffers
);
236 static const struct {
237 enum AVCodecID codec_id
;
239 VAProfile va_profile
;
240 } vaapi_profile_map
[] = {
241 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
242 MAP(MPEG2VIDEO
, MPEG2_SIMPLE
, MPEG2Simple
),
243 MAP(MPEG2VIDEO
, MPEG2_MAIN
, MPEG2Main
),
244 MAP(H263
, UNKNOWN
, H263Baseline
),
245 MAP(MPEG4
, MPEG4_SIMPLE
, MPEG4Simple
),
246 MAP(MPEG4
, MPEG4_ADVANCED_SIMPLE
,
247 MPEG4AdvancedSimple
),
248 MAP(MPEG4
, MPEG4_MAIN
, MPEG4Main
),
249 MAP(H264
, H264_CONSTRAINED_BASELINE
,
250 H264ConstrainedBaseline
),
251 MAP(H264
, H264_BASELINE
, H264Baseline
),
252 MAP(H264
, H264_MAIN
, H264Main
),
253 MAP(H264
, H264_HIGH
, H264High
),
254 #if VA_CHECK_VERSION(0, 37, 0)
255 MAP(HEVC
, HEVC_MAIN
, HEVCMain
),
257 MAP(WMV3
, VC1_SIMPLE
, VC1Simple
),
258 MAP(WMV3
, VC1_MAIN
, VC1Main
),
259 MAP(WMV3
, VC1_COMPLEX
, VC1Advanced
),
260 MAP(WMV3
, VC1_ADVANCED
, VC1Advanced
),
261 MAP(VC1
, VC1_SIMPLE
, VC1Simple
),
262 MAP(VC1
, VC1_MAIN
, VC1Main
),
263 MAP(VC1
, VC1_COMPLEX
, VC1Advanced
),
264 MAP(VC1
, VC1_ADVANCED
, VC1Advanced
),
265 #if VA_CHECK_VERSION(0, 35, 0)
266 MAP(VP8
, UNKNOWN
, VP8Version0_3
),
268 #if VA_CHECK_VERSION(0, 38, 0)
269 MAP(VP9
, VP9_0
, VP9Profile0
),
274 static int vaapi_decode_make_config(AVCodecContext
*avctx
)
276 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
278 AVVAAPIHWConfig
*hwconfig
= NULL
;
279 AVHWFramesConstraints
*constraints
= NULL
;
282 const AVCodecDescriptor
*codec_desc
;
283 VAProfile profile
, *profile_list
= NULL
;
284 int profile_count
, exact_match
, alt_profile
;
286 // Allowing a profile mismatch can be useful because streams may
287 // over-declare their required capabilities - in particular, many
288 // H.264 baseline profile streams (notably some of those in FATE)
289 // only use the feature set of constrained baseline. This flag
290 // would have to be be set by some external means in order to
291 // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
292 int allow_profile_mismatch
= 0;
294 codec_desc
= avcodec_descriptor_get(avctx
->codec_id
);
296 err
= AVERROR(EINVAL
);
300 profile_count
= vaMaxNumProfiles(ctx
->hwctx
->display
);
301 profile_list
= av_malloc_array(profile_count
,
304 err
= AVERROR(ENOMEM
);
308 vas
= vaQueryConfigProfiles(ctx
->hwctx
->display
,
309 profile_list
, &profile_count
);
310 if (vas
!= VA_STATUS_SUCCESS
) {
311 av_log(ctx
, AV_LOG_ERROR
, "Failed to query profiles: "
312 "%d (%s).\n", vas
, vaErrorStr(vas
));
313 err
= AVERROR(ENOSYS
);
317 profile
= VAProfileNone
;
320 for (i
= 0; i
< FF_ARRAY_ELEMS(vaapi_profile_map
); i
++) {
321 int profile_match
= 0;
322 if (avctx
->codec_id
!= vaapi_profile_map
[i
].codec_id
)
324 if (avctx
->profile
== vaapi_profile_map
[i
].codec_profile
||
325 vaapi_profile_map
[i
].codec_profile
== FF_PROFILE_UNKNOWN
)
327 profile
= vaapi_profile_map
[i
].va_profile
;
328 for (j
= 0; j
< profile_count
; j
++) {
329 if (profile
== profile_list
[j
]) {
330 exact_match
= profile_match
;
334 if (j
< profile_count
) {
337 alt_profile
= vaapi_profile_map
[i
].codec_profile
;
340 av_freep(&profile_list
);
342 if (profile
== VAProfileNone
) {
343 av_log(ctx
, AV_LOG_ERROR
, "No support for codec %s "
344 "profile %d.\n", codec_desc
->name
, avctx
->profile
);
345 err
= AVERROR(ENOSYS
);
349 if (allow_profile_mismatch
) {
350 av_log(avctx
, AV_LOG_VERBOSE
, "Codec %s profile %d not "
351 "supported for hardware decode.\n",
352 codec_desc
->name
, avctx
->profile
);
353 av_log(avctx
, AV_LOG_WARNING
, "Using possibly-"
354 "incompatible profile %d instead.\n",
357 av_log(avctx
, AV_LOG_VERBOSE
, "Codec %s profile %d not "
358 "supported for hardware decode.\n",
359 codec_desc
->name
, avctx
->profile
);
360 err
= AVERROR(EINVAL
);
365 ctx
->va_profile
= profile
;
366 ctx
->va_entrypoint
= VAEntrypointVLD
;
368 vas
= vaCreateConfig(ctx
->hwctx
->display
, ctx
->va_profile
,
369 ctx
->va_entrypoint
, NULL
, 0,
371 if (vas
!= VA_STATUS_SUCCESS
) {
372 av_log(avctx
, AV_LOG_ERROR
, "Failed to create decode "
373 "configuration: %d (%s).\n", vas
, vaErrorStr(vas
));
378 hwconfig
= av_hwdevice_hwconfig_alloc(ctx
->frames
->device_ref
);
380 err
= AVERROR(ENOMEM
);
383 hwconfig
->config_id
= ctx
->va_config
;
386 av_hwdevice_get_hwframe_constraints(ctx
->frames
->device_ref
,
391 if (avctx
->coded_width
< constraints
->min_width
||
392 avctx
->coded_height
< constraints
->min_height
||
393 avctx
->coded_width
> constraints
->max_width
||
394 avctx
->coded_height
> constraints
->max_height
) {
395 av_log(ctx
, AV_LOG_ERROR
, "Hardware does not support image "
396 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
397 avctx
->coded_width
, avctx
->coded_height
,
398 constraints
->min_width
, constraints
->max_width
,
399 constraints
->min_height
, constraints
->max_height
);
400 err
= AVERROR(EINVAL
);
405 av_hwframe_constraints_free(&constraints
);
411 av_hwframe_constraints_free(&constraints
);
413 if (ctx
->va_config
!= VA_INVALID_ID
) {
414 vaDestroyConfig(ctx
->hwctx
->display
, ctx
->va_config
);
415 ctx
->va_config
= VA_INVALID_ID
;
417 av_freep(&profile_list
);
421 int ff_vaapi_decode_init(AVCodecContext
*avctx
)
423 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
427 ctx
->va_config
= VA_INVALID_ID
;
428 ctx
->va_context
= VA_INVALID_ID
;
430 #if FF_API_VAAPI_CONTEXT
431 if (avctx
->hwaccel_context
) {
432 av_log(avctx
, AV_LOG_WARNING
, "Using deprecated struct "
433 "vaapi_context in decode.\n");
435 ctx
->have_old_context
= 1;
436 ctx
->old_context
= avctx
->hwaccel_context
;
438 // Really we only want the VAAPI device context, but this
439 // allocates a whole generic device context because we don't
440 // have any other way to determine how big it should be.
442 av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI
);
443 if (!ctx
->device_ref
) {
444 err
= AVERROR(ENOMEM
);
447 ctx
->device
= (AVHWDeviceContext
*)ctx
->device_ref
->data
;
448 ctx
->hwctx
= ctx
->device
->hwctx
;
450 ctx
->hwctx
->display
= ctx
->old_context
->display
;
452 // The old VAAPI decode setup assumed this quirk was always
453 // present, so set it here to avoid the behaviour changing.
454 ctx
->hwctx
->driver_quirks
=
455 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
;
459 if (avctx
->hw_frames_ctx
) {
460 // This structure has a shorter lifetime than the enclosing
461 // AVCodecContext, so we inherit the references from there
462 // and do not need to make separate ones.
464 ctx
->frames
= (AVHWFramesContext
*)avctx
->hw_frames_ctx
->data
;
465 ctx
->hwfc
= ctx
->frames
->hwctx
;
467 ctx
->device
= ctx
->frames
->device_ctx
;
468 ctx
->hwctx
= ctx
->device
->hwctx
;
471 av_log(avctx
, AV_LOG_ERROR
, "A hardware frames context is "
472 "required for VAAPI decoding.\n");
473 err
= AVERROR(EINVAL
);
477 #if FF_API_VAAPI_CONTEXT
478 if (ctx
->have_old_context
) {
479 ctx
->va_config
= ctx
->old_context
->config_id
;
480 ctx
->va_context
= ctx
->old_context
->context_id
;
482 av_log(avctx
, AV_LOG_DEBUG
, "Using user-supplied decoder "
483 "context: %#x/%#x.\n", ctx
->va_config
, ctx
->va_context
);
487 err
= vaapi_decode_make_config(avctx
);
491 vas
= vaCreateContext(ctx
->hwctx
->display
, ctx
->va_config
,
492 avctx
->coded_width
, avctx
->coded_height
,
494 ctx
->hwfc
->surface_ids
,
495 ctx
->hwfc
->nb_surfaces
,
497 if (vas
!= VA_STATUS_SUCCESS
) {
498 av_log(avctx
, AV_LOG_ERROR
, "Failed to create decode "
499 "context: %d (%s).\n", vas
, vaErrorStr(vas
));
504 av_log(avctx
, AV_LOG_DEBUG
, "Decode context initialised: "
505 "%#x/%#x.\n", ctx
->va_config
, ctx
->va_context
);
506 #if FF_API_VAAPI_CONTEXT
513 ff_vaapi_decode_uninit(avctx
);
517 int ff_vaapi_decode_uninit(AVCodecContext
*avctx
)
519 VAAPIDecodeContext
*ctx
= avctx
->internal
->hwaccel_priv_data
;
522 #if FF_API_VAAPI_CONTEXT
523 if (ctx
->have_old_context
) {
524 av_buffer_unref(&ctx
->device_ref
);
528 if (ctx
->va_context
!= VA_INVALID_ID
) {
529 vas
= vaDestroyContext(ctx
->hwctx
->display
, ctx
->va_context
);
530 if (vas
!= VA_STATUS_SUCCESS
) {
531 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy decode "
532 "context %#x: %d (%s).\n",
533 ctx
->va_context
, vas
, vaErrorStr(vas
));
536 if (ctx
->va_config
!= VA_INVALID_ID
) {
537 vas
= vaDestroyConfig(ctx
->hwctx
->display
, ctx
->va_config
);
538 if (vas
!= VA_STATUS_SUCCESS
) {
539 av_log(avctx
, AV_LOG_ERROR
, "Failed to destroy decode "
540 "configuration %#x: %d (%s).\n",
541 ctx
->va_config
, vas
, vaErrorStr(vas
));
545 #if FF_API_VAAPI_CONTEXT