2 * This file is part of FFmpeg.
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
21 #include "libavutil/pixdesc.h"
26 #include "vaapi_decode.h"
27 #include "vaapi_hevc.h"
30 int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
31 VAAPIDecodePicture *pic,
36 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
40 av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
42 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
43 type, size, 1, (void*)data, &buffer);
44 if (vas != VA_STATUS_SUCCESS) {
45 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
46 "buffer (type %d): %d (%s).\n",
47 type, vas, vaErrorStr(vas));
51 pic->param_buffers[pic->nb_param_buffers++] = buffer;
53 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
54 "is %#x.\n", type, size, buffer);
59 int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
60 VAAPIDecodePicture *pic,
61 const void *params_data,
63 const void *slice_data,
66 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
70 av_assert0(pic->nb_slices <= pic->slices_allocated);
71 if (pic->nb_slices == pic->slices_allocated) {
72 if (pic->slices_allocated > 0)
73 pic->slices_allocated *= 2;
75 pic->slices_allocated = 64;
78 av_realloc_array(pic->slice_buffers,
79 pic->slices_allocated,
80 2 * sizeof(*pic->slice_buffers));
81 if (!pic->slice_buffers)
82 return AVERROR(ENOMEM);
84 av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
86 index = 2 * pic->nb_slices;
88 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
89 VASliceParameterBufferType,
90 params_size, 1, (void*)params_data,
91 &pic->slice_buffers[index]);
92 if (vas != VA_STATUS_SUCCESS) {
93 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
94 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
98 av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
99 "is %#x.\n", pic->nb_slices, params_size,
100 pic->slice_buffers[index]);
102 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
103 VASliceDataBufferType,
104 slice_size, 1, (void*)slice_data,
105 &pic->slice_buffers[index + 1]);
106 if (vas != VA_STATUS_SUCCESS) {
107 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
108 "data buffer (size %zu): %d (%s).\n",
109 slice_size, vas, vaErrorStr(vas));
110 vaDestroyBuffer(ctx->hwctx->display,
111 pic->slice_buffers[index]);
115 av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
116 "is %#x.\n", pic->nb_slices, slice_size,
117 pic->slice_buffers[index + 1]);
123 static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
124 VAAPIDecodePicture *pic)
126 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
130 for (i = 0; i < pic->nb_param_buffers; i++) {
131 vas = vaDestroyBuffer(ctx->hwctx->display,
132 pic->param_buffers[i]);
133 if (vas != VA_STATUS_SUCCESS) {
134 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
135 "parameter buffer %#x: %d (%s).\n",
136 pic->param_buffers[i], vas, vaErrorStr(vas));
140 for (i = 0; i < 2 * pic->nb_slices; i++) {
141 vas = vaDestroyBuffer(ctx->hwctx->display,
142 pic->slice_buffers[i]);
143 if (vas != VA_STATUS_SUCCESS) {
144 av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
145 "slice buffer %#x: %d (%s).\n",
146 pic->slice_buffers[i], vas, vaErrorStr(vas));
151 int ff_vaapi_decode_issue(AVCodecContext *avctx,
152 VAAPIDecodePicture *pic)
154 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
158 av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
159 pic->output_surface);
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 (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
194 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
200 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
201 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
202 ff_vaapi_decode_destroy_buffers(avctx, pic);
208 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
209 if (vas != VA_STATUS_SUCCESS) {
210 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
211 "after error: %d (%s).\n", vas, vaErrorStr(vas));
214 ff_vaapi_decode_destroy_buffers(avctx, pic);
217 pic->nb_param_buffers = 0;
219 pic->slices_allocated = 0;
220 av_freep(&pic->slice_buffers);
225 int ff_vaapi_decode_cancel(AVCodecContext *avctx,
226 VAAPIDecodePicture *pic)
228 ff_vaapi_decode_destroy_buffers(avctx, pic);
230 pic->nb_param_buffers = 0;
232 pic->slices_allocated = 0;
233 av_freep(&pic->slice_buffers);
238 static const struct {
240 enum AVPixelFormat pix_fmt;
241 } vaapi_format_map[] = {
242 #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
249 #ifdef VA_FOURCC_I420
257 #ifdef VA_FOURCC_YV16
261 #ifdef VA_FOURCC_Y210
269 #ifdef VA_FOURCC_P010
272 #ifdef VA_FOURCC_I010
273 MAP(I010, YUV420P10),
278 static int vaapi_decode_find_best_format(AVCodecContext *avctx,
279 AVHWDeviceContext *device,
280 VAConfigID config_id,
281 AVHWFramesContext *frames)
283 AVVAAPIDeviceContext *hwctx = device->hwctx;
285 VASurfaceAttrib *attr;
286 enum AVPixelFormat source_format, best_format, format;
287 uint32_t best_fourcc, fourcc;
290 source_format = avctx->sw_pix_fmt;
291 av_assert0(source_format != AV_PIX_FMT_NONE);
293 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
295 if (vas != VA_STATUS_SUCCESS) {
296 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
297 "%d (%s).\n", vas, vaErrorStr(vas));
298 return AVERROR(ENOSYS);
301 attr = av_malloc_array(nb_attr, sizeof(*attr));
303 return AVERROR(ENOMEM);
305 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
307 if (vas != VA_STATUS_SUCCESS) {
308 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
309 "%d (%s).\n", vas, vaErrorStr(vas));
311 return AVERROR(ENOSYS);
314 best_format = AV_PIX_FMT_NONE;
316 for (i = 0; i < nb_attr; i++) {
317 if (attr[i].type != VASurfaceAttribPixelFormat)
320 fourcc = attr[i].value.value.i;
321 for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
322 if (fourcc == vaapi_format_map[j].fourcc)
325 if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
326 av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
330 format = vaapi_format_map[j].pix_fmt;
331 av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
332 fourcc, av_get_pix_fmt_name(format));
334 best_format = av_find_best_pix_fmt_of_2(format, best_format,
335 source_format, 0, NULL);
336 if (format == best_format)
337 best_fourcc = fourcc;
342 if (best_format == AV_PIX_FMT_NONE) {
343 av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
344 return AVERROR(EINVAL);
347 av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
348 av_get_pix_fmt_name(best_format), best_fourcc,
349 av_get_pix_fmt_name(source_format));
351 frames->sw_format = best_format;
352 if (avctx->internal->hwaccel_priv_data) {
353 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
354 AVVAAPIFramesContext *avfc = frames->hwctx;
356 ctx->pixel_format_attribute = (VASurfaceAttrib) {
357 .type = VASurfaceAttribPixelFormat,
358 .value.value.i = best_fourcc,
361 avfc->attributes = &ctx->pixel_format_attribute;
362 avfc->nb_attributes = 1;
368 static const struct {
369 enum AVCodecID codec_id;
371 VAProfile va_profile;
372 VAProfile (*profile_parser)(AVCodecContext *avctx);
373 } vaapi_profile_map[] = {
374 #define MAP(c, p, v, ...) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v, __VA_ARGS__ }
375 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
376 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
377 MAP(H263, UNKNOWN, H263Baseline),
378 MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
379 MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
380 MPEG4AdvancedSimple),
381 MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
382 MAP(H264, H264_CONSTRAINED_BASELINE,
383 H264ConstrainedBaseline),
384 MAP(H264, H264_MAIN, H264Main ),
385 MAP(H264, H264_HIGH, H264High ),
386 #if VA_CHECK_VERSION(0, 37, 0)
387 MAP(HEVC, HEVC_MAIN, HEVCMain ),
388 MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
389 MAP(HEVC, HEVC_MAIN_STILL_PICTURE,
392 #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL
393 MAP(HEVC, HEVC_REXT, None,
394 ff_vaapi_parse_hevc_rext_profile ),
396 MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
398 MAP(WMV3, VC1_SIMPLE, VC1Simple ),
399 MAP(WMV3, VC1_MAIN, VC1Main ),
400 MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
401 MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
402 MAP(VC1, VC1_SIMPLE, VC1Simple ),
403 MAP(VC1, VC1_MAIN, VC1Main ),
404 MAP(VC1, VC1_COMPLEX, VC1Advanced ),
405 MAP(VC1, VC1_ADVANCED, VC1Advanced ),
406 MAP(VP8, UNKNOWN, VP8Version0_3 ),
407 #if VA_CHECK_VERSION(0, 38, 0)
408 MAP(VP9, VP9_0, VP9Profile0 ),
410 #if VA_CHECK_VERSION(0, 39, 0)
411 MAP(VP9, VP9_2, VP9Profile2 ),
413 #if VA_CHECK_VERSION(1, 8, 0)
414 MAP(AV1, AV1_MAIN, AV1Profile0),
415 MAP(AV1, AV1_HIGH, AV1Profile1),
422 * Set *va_config and the frames_ref fields from the current codec parameters
425 static int vaapi_decode_make_config(AVCodecContext *avctx,
426 AVBufferRef *device_ref,
427 VAConfigID *va_config,
428 AVBufferRef *frames_ref)
430 AVVAAPIHWConfig *hwconfig = NULL;
431 AVHWFramesConstraints *constraints = NULL;
434 const AVCodecDescriptor *codec_desc;
435 VAProfile *profile_list = NULL, matched_va_profile, va_profile;
436 int profile_count, exact_match, matched_ff_profile, codec_profile;
438 AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
439 AVVAAPIDeviceContext *hwctx = device->hwctx;
441 codec_desc = avcodec_descriptor_get(avctx->codec_id);
443 err = AVERROR(EINVAL);
447 profile_count = vaMaxNumProfiles(hwctx->display);
448 profile_list = av_malloc_array(profile_count,
451 err = AVERROR(ENOMEM);
455 vas = vaQueryConfigProfiles(hwctx->display,
456 profile_list, &profile_count);
457 if (vas != VA_STATUS_SUCCESS) {
458 av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
459 "%d (%s).\n", vas, vaErrorStr(vas));
460 err = AVERROR(ENOSYS);
464 matched_va_profile = VAProfileNone;
467 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
468 int profile_match = 0;
469 if (avctx->codec_id != vaapi_profile_map[i].codec_id)
471 if (avctx->profile == vaapi_profile_map[i].codec_profile ||
472 vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
475 va_profile = vaapi_profile_map[i].profile_parser ?
476 vaapi_profile_map[i].profile_parser(avctx) :
477 vaapi_profile_map[i].va_profile;
478 codec_profile = vaapi_profile_map[i].codec_profile;
480 for (j = 0; j < profile_count; j++) {
481 if (va_profile == profile_list[j]) {
482 exact_match = profile_match;
486 if (j < profile_count) {
487 matched_va_profile = va_profile;
488 matched_ff_profile = codec_profile;
493 av_freep(&profile_list);
495 if (matched_va_profile == VAProfileNone) {
496 av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
497 "profile %d.\n", codec_desc->name, avctx->profile);
498 err = AVERROR(ENOSYS);
502 if (avctx->hwaccel_flags &
503 AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
504 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
505 "supported for hardware decode.\n",
506 codec_desc->name, avctx->profile);
507 av_log(avctx, AV_LOG_WARNING, "Using possibly-"
508 "incompatible profile %d instead.\n",
511 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
512 "supported for hardware decode.\n",
513 codec_desc->name, avctx->profile);
514 err = AVERROR(EINVAL);
519 vas = vaCreateConfig(hwctx->display, matched_va_profile,
520 VAEntrypointVLD, NULL, 0,
522 if (vas != VA_STATUS_SUCCESS) {
523 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
524 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
529 hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
531 err = AVERROR(ENOMEM);
534 hwconfig->config_id = *va_config;
537 av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
539 err = AVERROR(ENOMEM);
543 if (avctx->coded_width < constraints->min_width ||
544 avctx->coded_height < constraints->min_height ||
545 avctx->coded_width > constraints->max_width ||
546 avctx->coded_height > constraints->max_height) {
547 av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
548 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
549 avctx->coded_width, avctx->coded_height,
550 constraints->min_width, constraints->max_width,
551 constraints->min_height, constraints->max_height);
552 err = AVERROR(EINVAL);
555 if (!constraints->valid_sw_formats ||
556 constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
557 av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
558 "usable surface formats.\n");
559 err = AVERROR(EINVAL);
564 AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
566 frames->format = AV_PIX_FMT_VAAPI;
567 frames->width = avctx->coded_width;
568 frames->height = avctx->coded_height;
570 err = vaapi_decode_find_best_format(avctx, device,
575 frames->initial_pool_size = 1;
576 // Add per-codec number of surfaces used for storing reference frames.
577 switch (avctx->codec_id) {
578 case AV_CODEC_ID_H264:
579 case AV_CODEC_ID_HEVC:
580 frames->initial_pool_size += 16;
582 case AV_CODEC_ID_VP9:
583 case AV_CODEC_ID_AV1:
584 frames->initial_pool_size += 8;
586 case AV_CODEC_ID_VP8:
587 frames->initial_pool_size += 3;
590 frames->initial_pool_size += 2;
594 av_hwframe_constraints_free(&constraints);
600 av_hwframe_constraints_free(&constraints);
602 if (*va_config != VA_INVALID_ID) {
603 vaDestroyConfig(hwctx->display, *va_config);
604 *va_config = VA_INVALID_ID;
606 av_freep(&profile_list);
610 int ff_vaapi_common_frame_params(AVCodecContext *avctx,
611 AVBufferRef *hw_frames_ctx)
613 AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
614 AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
615 AVVAAPIDeviceContext *hwctx;
616 VAConfigID va_config = VA_INVALID_ID;
619 if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
620 return AVERROR(EINVAL);
621 hwctx = device_ctx->hwctx;
623 err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
628 if (va_config != VA_INVALID_ID)
629 vaDestroyConfig(hwctx->display, va_config);
634 int ff_vaapi_decode_init(AVCodecContext *avctx)
636 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
640 ctx->va_config = VA_INVALID_ID;
641 ctx->va_context = VA_INVALID_ID;
643 err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
647 ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
648 ctx->hwfc = ctx->frames->hwctx;
649 ctx->device = ctx->frames->device_ctx;
650 ctx->hwctx = ctx->device->hwctx;
652 err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
653 &ctx->va_config, avctx->hw_frames_ctx);
657 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
658 avctx->coded_width, avctx->coded_height,
660 ctx->hwfc->surface_ids,
661 ctx->hwfc->nb_surfaces,
663 if (vas != VA_STATUS_SUCCESS) {
664 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
665 "context: %d (%s).\n", vas, vaErrorStr(vas));
670 av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
671 "%#x/%#x.\n", ctx->va_config, ctx->va_context);
676 ff_vaapi_decode_uninit(avctx);
680 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
682 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
685 if (ctx->va_context != VA_INVALID_ID) {
686 vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
687 if (vas != VA_STATUS_SUCCESS) {
688 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
689 "context %#x: %d (%s).\n",
690 ctx->va_context, vas, vaErrorStr(vas));
693 if (ctx->va_config != VA_INVALID_ID) {
694 vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
695 if (vas != VA_STATUS_SUCCESS) {
696 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
697 "configuration %#x: %d (%s).\n",
698 ctx->va_config, vas, vaErrorStr(vas));