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"
29 int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
30 VAAPIDecodePicture *pic,
35 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
39 av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
41 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
42 type, size, 1, (void*)data, &buffer);
43 if (vas != VA_STATUS_SUCCESS) {
44 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
45 "buffer (type %d): %d (%s).\n",
46 type, vas, vaErrorStr(vas));
50 pic->param_buffers[pic->nb_param_buffers++] = buffer;
52 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
53 "is %#x.\n", type, size, buffer);
58 int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
59 VAAPIDecodePicture *pic,
60 const void *params_data,
62 const void *slice_data,
65 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
69 av_assert0(pic->nb_slices <= pic->slices_allocated);
70 if (pic->nb_slices == pic->slices_allocated) {
71 if (pic->slices_allocated > 0)
72 pic->slices_allocated *= 2;
74 pic->slices_allocated = 64;
77 av_realloc_array(pic->slice_buffers,
78 pic->slices_allocated,
79 2 * sizeof(*pic->slice_buffers));
80 if (!pic->slice_buffers)
81 return AVERROR(ENOMEM);
83 av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
85 index = 2 * pic->nb_slices;
87 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
88 VASliceParameterBufferType,
89 params_size, 1, (void*)params_data,
90 &pic->slice_buffers[index]);
91 if (vas != VA_STATUS_SUCCESS) {
92 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
93 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
97 av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
98 "is %#x.\n", pic->nb_slices, params_size,
99 pic->slice_buffers[index]);
101 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
102 VASliceDataBufferType,
103 slice_size, 1, (void*)slice_data,
104 &pic->slice_buffers[index + 1]);
105 if (vas != VA_STATUS_SUCCESS) {
106 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
107 "data buffer (size %zu): %d (%s).\n",
108 slice_size, vas, vaErrorStr(vas));
109 vaDestroyBuffer(ctx->hwctx->display,
110 pic->slice_buffers[index]);
114 av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
115 "is %#x.\n", pic->nb_slices, slice_size,
116 pic->slice_buffers[index + 1]);
122 static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
123 VAAPIDecodePicture *pic)
125 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
129 for (i = 0; i < pic->nb_param_buffers; i++) {
130 vas = vaDestroyBuffer(ctx->hwctx->display,
131 pic->param_buffers[i]);
132 if (vas != VA_STATUS_SUCCESS) {
133 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
134 "parameter buffer %#x: %d (%s).\n",
135 pic->param_buffers[i], vas, vaErrorStr(vas));
139 for (i = 0; i < 2 * pic->nb_slices; i++) {
140 vas = vaDestroyBuffer(ctx->hwctx->display,
141 pic->slice_buffers[i]);
142 if (vas != VA_STATUS_SUCCESS) {
143 av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
144 "slice buffer %#x: %d (%s).\n",
145 pic->slice_buffers[i], vas, vaErrorStr(vas));
150 int ff_vaapi_decode_issue(AVCodecContext *avctx,
151 VAAPIDecodePicture *pic)
153 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
157 av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
158 pic->output_surface);
160 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
161 pic->output_surface);
162 if (vas != VA_STATUS_SUCCESS) {
163 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
164 "issue: %d (%s).\n", vas, vaErrorStr(vas));
166 goto fail_with_picture;
169 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
170 pic->param_buffers, pic->nb_param_buffers);
171 if (vas != VA_STATUS_SUCCESS) {
172 av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
173 "parameters: %d (%s).\n", vas, vaErrorStr(vas));
175 goto fail_with_picture;
178 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
179 pic->slice_buffers, 2 * pic->nb_slices);
180 if (vas != VA_STATUS_SUCCESS) {
181 av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
182 "%d (%s).\n", vas, vaErrorStr(vas));
184 goto fail_with_picture;
187 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
188 if (vas != VA_STATUS_SUCCESS) {
189 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
190 "issue: %d (%s).\n", vas, vaErrorStr(vas));
192 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
193 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
199 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
200 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
201 ff_vaapi_decode_destroy_buffers(avctx, pic);
203 pic->nb_param_buffers = 0;
205 pic->slices_allocated = 0;
206 av_freep(&pic->slice_buffers);
211 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
212 if (vas != VA_STATUS_SUCCESS) {
213 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
214 "after error: %d (%s).\n", vas, vaErrorStr(vas));
217 ff_vaapi_decode_destroy_buffers(avctx, pic);
222 int ff_vaapi_decode_cancel(AVCodecContext *avctx,
223 VAAPIDecodePicture *pic)
225 ff_vaapi_decode_destroy_buffers(avctx, pic);
227 pic->nb_param_buffers = 0;
229 pic->slices_allocated = 0;
230 av_freep(&pic->slice_buffers);
235 static const struct {
237 enum AVPixelFormat pix_fmt;
238 } vaapi_format_map[] = {
239 #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
246 #ifdef VA_FOURCC_I420
254 #ifdef VA_FOURCC_YV16
262 #ifdef VA_FOURCC_P010
265 #ifdef VA_FOURCC_I010
266 MAP(I010, YUV420P10),
271 static int vaapi_decode_find_best_format(AVCodecContext *avctx,
272 AVHWDeviceContext *device,
273 VAConfigID config_id,
274 AVHWFramesContext *frames)
276 AVVAAPIDeviceContext *hwctx = device->hwctx;
278 VASurfaceAttrib *attr;
279 enum AVPixelFormat source_format, best_format, format;
280 uint32_t best_fourcc, fourcc;
283 source_format = avctx->sw_pix_fmt;
284 av_assert0(source_format != AV_PIX_FMT_NONE);
286 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
288 if (vas != VA_STATUS_SUCCESS) {
289 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
290 "%d (%s).\n", vas, vaErrorStr(vas));
291 return AVERROR(ENOSYS);
294 attr = av_malloc_array(nb_attr, sizeof(*attr));
296 return AVERROR(ENOMEM);
298 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
300 if (vas != VA_STATUS_SUCCESS) {
301 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
302 "%d (%s).\n", vas, vaErrorStr(vas));
304 return AVERROR(ENOSYS);
307 best_format = AV_PIX_FMT_NONE;
309 for (i = 0; i < nb_attr; i++) {
310 if (attr[i].type != VASurfaceAttribPixelFormat)
313 fourcc = attr[i].value.value.i;
314 for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
315 if (fourcc == vaapi_format_map[j].fourcc)
318 if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
319 av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
323 format = vaapi_format_map[j].pix_fmt;
324 av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
325 fourcc, av_get_pix_fmt_name(format));
327 best_format = av_find_best_pix_fmt_of_2(format, best_format,
328 source_format, 0, NULL);
329 if (format == best_format)
330 best_fourcc = fourcc;
335 if (best_format == AV_PIX_FMT_NONE) {
336 av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
337 return AVERROR(EINVAL);
340 av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
341 av_get_pix_fmt_name(best_format), best_fourcc,
342 av_get_pix_fmt_name(source_format));
344 frames->sw_format = best_format;
345 if (avctx->internal->hwaccel_priv_data) {
346 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
347 AVVAAPIFramesContext *avfc = frames->hwctx;
349 ctx->pixel_format_attribute = (VASurfaceAttrib) {
350 .type = VASurfaceAttribPixelFormat,
351 .value.value.i = best_fourcc,
354 avfc->attributes = &ctx->pixel_format_attribute;
355 avfc->nb_attributes = 1;
361 static const struct {
362 enum AVCodecID codec_id;
364 VAProfile va_profile;
365 } vaapi_profile_map[] = {
366 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
367 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
368 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
369 MAP(H263, UNKNOWN, H263Baseline),
370 MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
371 MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
372 MPEG4AdvancedSimple),
373 MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
374 MAP(H264, H264_CONSTRAINED_BASELINE,
375 H264ConstrainedBaseline),
376 MAP(H264, H264_MAIN, H264Main ),
377 MAP(H264, H264_HIGH, H264High ),
378 #if VA_CHECK_VERSION(0, 37, 0)
379 MAP(HEVC, HEVC_MAIN, HEVCMain ),
380 MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
382 MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
384 MAP(WMV3, VC1_SIMPLE, VC1Simple ),
385 MAP(WMV3, VC1_MAIN, VC1Main ),
386 MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
387 MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
388 MAP(VC1, VC1_SIMPLE, VC1Simple ),
389 MAP(VC1, VC1_MAIN, VC1Main ),
390 MAP(VC1, VC1_COMPLEX, VC1Advanced ),
391 MAP(VC1, VC1_ADVANCED, VC1Advanced ),
392 #if VA_CHECK_VERSION(0, 35, 0)
393 MAP(VP8, UNKNOWN, VP8Version0_3 ),
395 #if VA_CHECK_VERSION(0, 38, 0)
396 MAP(VP9, VP9_0, VP9Profile0 ),
398 #if VA_CHECK_VERSION(0, 39, 0)
399 MAP(VP9, VP9_2, VP9Profile2 ),
405 * Set *va_config and the frames_ref fields from the current codec parameters
408 static int vaapi_decode_make_config(AVCodecContext *avctx,
409 AVBufferRef *device_ref,
410 VAConfigID *va_config,
411 AVBufferRef *frames_ref)
413 AVVAAPIHWConfig *hwconfig = NULL;
414 AVHWFramesConstraints *constraints = NULL;
417 const AVCodecDescriptor *codec_desc;
418 VAProfile *profile_list = NULL, matched_va_profile;
419 int profile_count, exact_match, matched_ff_profile;
421 AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
422 AVVAAPIDeviceContext *hwctx = device->hwctx;
424 codec_desc = avcodec_descriptor_get(avctx->codec_id);
426 err = AVERROR(EINVAL);
430 profile_count = vaMaxNumProfiles(hwctx->display);
431 profile_list = av_malloc_array(profile_count,
434 err = AVERROR(ENOMEM);
438 vas = vaQueryConfigProfiles(hwctx->display,
439 profile_list, &profile_count);
440 if (vas != VA_STATUS_SUCCESS) {
441 av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
442 "%d (%s).\n", vas, vaErrorStr(vas));
443 err = AVERROR(ENOSYS);
447 matched_va_profile = VAProfileNone;
450 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
451 int profile_match = 0;
452 if (avctx->codec_id != vaapi_profile_map[i].codec_id)
454 if (avctx->profile == vaapi_profile_map[i].codec_profile ||
455 vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
457 for (j = 0; j < profile_count; j++) {
458 if (vaapi_profile_map[i].va_profile == profile_list[j]) {
459 exact_match = profile_match;
463 if (j < profile_count) {
464 matched_va_profile = vaapi_profile_map[i].va_profile;
465 matched_ff_profile = vaapi_profile_map[i].codec_profile;
470 av_freep(&profile_list);
472 if (matched_va_profile == VAProfileNone) {
473 av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
474 "profile %d.\n", codec_desc->name, avctx->profile);
475 err = AVERROR(ENOSYS);
479 if (avctx->hwaccel_flags &
480 AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
481 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
482 "supported for hardware decode.\n",
483 codec_desc->name, avctx->profile);
484 av_log(avctx, AV_LOG_WARNING, "Using possibly-"
485 "incompatible profile %d instead.\n",
488 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
489 "supported for hardware decode.\n",
490 codec_desc->name, avctx->profile);
491 err = AVERROR(EINVAL);
496 vas = vaCreateConfig(hwctx->display, matched_va_profile,
497 VAEntrypointVLD, NULL, 0,
499 if (vas != VA_STATUS_SUCCESS) {
500 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
501 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
506 hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
508 err = AVERROR(ENOMEM);
511 hwconfig->config_id = *va_config;
514 av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
516 err = AVERROR(ENOMEM);
520 if (avctx->coded_width < constraints->min_width ||
521 avctx->coded_height < constraints->min_height ||
522 avctx->coded_width > constraints->max_width ||
523 avctx->coded_height > constraints->max_height) {
524 av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
525 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
526 avctx->coded_width, avctx->coded_height,
527 constraints->min_width, constraints->max_width,
528 constraints->min_height, constraints->max_height);
529 err = AVERROR(EINVAL);
532 if (!constraints->valid_sw_formats ||
533 constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
534 av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
535 "usable surface formats.\n");
536 err = AVERROR(EINVAL);
541 AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
543 frames->format = AV_PIX_FMT_VAAPI;
544 frames->width = avctx->coded_width;
545 frames->height = avctx->coded_height;
547 err = vaapi_decode_find_best_format(avctx, device,
552 frames->initial_pool_size = 1;
553 // Add per-codec number of surfaces used for storing reference frames.
554 switch (avctx->codec_id) {
555 case AV_CODEC_ID_H264:
556 case AV_CODEC_ID_HEVC:
557 frames->initial_pool_size += 16;
559 case AV_CODEC_ID_VP9:
560 frames->initial_pool_size += 8;
562 case AV_CODEC_ID_VP8:
563 frames->initial_pool_size += 3;
566 frames->initial_pool_size += 2;
570 av_hwframe_constraints_free(&constraints);
576 av_hwframe_constraints_free(&constraints);
578 if (*va_config != VA_INVALID_ID) {
579 vaDestroyConfig(hwctx->display, *va_config);
580 *va_config = VA_INVALID_ID;
582 av_freep(&profile_list);
586 int ff_vaapi_common_frame_params(AVCodecContext *avctx,
587 AVBufferRef *hw_frames_ctx)
589 AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
590 AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
591 AVVAAPIDeviceContext *hwctx;
592 VAConfigID va_config = VA_INVALID_ID;
595 if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
596 return AVERROR(EINVAL);
597 hwctx = device_ctx->hwctx;
599 err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
604 if (va_config != VA_INVALID_ID)
605 vaDestroyConfig(hwctx->display, va_config);
610 int ff_vaapi_decode_init(AVCodecContext *avctx)
612 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
616 ctx->va_config = VA_INVALID_ID;
617 ctx->va_context = VA_INVALID_ID;
619 #if FF_API_STRUCT_VAAPI_CONTEXT
620 if (avctx->hwaccel_context) {
621 av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
622 "vaapi_context in decode.\n");
624 ctx->have_old_context = 1;
625 ctx->old_context = avctx->hwaccel_context;
627 // Really we only want the VAAPI device context, but this
628 // allocates a whole generic device context because we don't
629 // have any other way to determine how big it should be.
631 av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
632 if (!ctx->device_ref) {
633 err = AVERROR(ENOMEM);
636 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
637 ctx->hwctx = ctx->device->hwctx;
639 ctx->hwctx->display = ctx->old_context->display;
641 // The old VAAPI decode setup assumed this quirk was always
642 // present, so set it here to avoid the behaviour changing.
643 ctx->hwctx->driver_quirks =
644 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
649 #if FF_API_STRUCT_VAAPI_CONTEXT
650 if (ctx->have_old_context) {
651 ctx->va_config = ctx->old_context->config_id;
652 ctx->va_context = ctx->old_context->context_id;
654 av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
655 "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
659 err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
663 ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
664 ctx->hwfc = ctx->frames->hwctx;
665 ctx->device = ctx->frames->device_ctx;
666 ctx->hwctx = ctx->device->hwctx;
668 err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
669 &ctx->va_config, avctx->hw_frames_ctx);
673 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
674 avctx->coded_width, avctx->coded_height,
676 ctx->hwfc->surface_ids,
677 ctx->hwfc->nb_surfaces,
679 if (vas != VA_STATUS_SUCCESS) {
680 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
681 "context: %d (%s).\n", vas, vaErrorStr(vas));
686 av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
687 "%#x/%#x.\n", ctx->va_config, ctx->va_context);
688 #if FF_API_STRUCT_VAAPI_CONTEXT
695 ff_vaapi_decode_uninit(avctx);
699 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
701 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
704 #if FF_API_STRUCT_VAAPI_CONTEXT
705 if (ctx->have_old_context) {
706 av_buffer_unref(&ctx->device_ref);
710 if (ctx->va_context != VA_INVALID_ID) {
711 vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
712 if (vas != VA_STATUS_SUCCESS) {
713 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
714 "context %#x: %d (%s).\n",
715 ctx->va_context, vas, vaErrorStr(vas));
718 if (ctx->va_config != VA_INVALID_ID) {
719 vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
720 if (vas != VA_STATUS_SUCCESS) {
721 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
722 "configuration %#x: %d (%s).\n",
723 ctx->va_config, vas, vaErrorStr(vas));
727 #if FF_API_STRUCT_VAAPI_CONTEXT