FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vaapi_decode.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 314 0.0%
Functions: 0 10 0.0%
Branches: 0 138 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config_components.h"
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/pixdesc.h"
24
25 #include "avcodec.h"
26 #include "decode.h"
27 #include "internal.h"
28 #include "vaapi_decode.h"
29 #include "vaapi_hevc.h"
30
31
32 int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
33 VAAPIDecodePicture *pic,
34 int type,
35 const void *data,
36 size_t size)
37 {
38 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
39 VAStatus vas;
40 VABufferID buffer;
41
42 av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
43
44 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
45 type, size, 1, (void*)data, &buffer);
46 if (vas != VA_STATUS_SUCCESS) {
47 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
48 "buffer (type %d): %d (%s).\n",
49 type, vas, vaErrorStr(vas));
50 return AVERROR(EIO);
51 }
52
53 pic->param_buffers[pic->nb_param_buffers++] = buffer;
54
55 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
56 "is %#x.\n", type, size, buffer);
57 return 0;
58 }
59
60
61 int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
62 VAAPIDecodePicture *pic,
63 const void *params_data,
64 size_t params_size,
65 const void *slice_data,
66 size_t slice_size)
67 {
68 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
69 VAStatus vas;
70 int index;
71
72 av_assert0(pic->nb_slices <= pic->slices_allocated);
73 if (pic->nb_slices == pic->slices_allocated) {
74 if (pic->slices_allocated > 0)
75 pic->slices_allocated *= 2;
76 else
77 pic->slices_allocated = 64;
78
79 pic->slice_buffers =
80 av_realloc_array(pic->slice_buffers,
81 pic->slices_allocated,
82 2 * sizeof(*pic->slice_buffers));
83 if (!pic->slice_buffers)
84 return AVERROR(ENOMEM);
85 }
86 av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
87
88 index = 2 * pic->nb_slices;
89
90 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
91 VASliceParameterBufferType,
92 params_size, 1, (void*)params_data,
93 &pic->slice_buffers[index]);
94 if (vas != VA_STATUS_SUCCESS) {
95 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
96 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
97 return AVERROR(EIO);
98 }
99
100 av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
101 "is %#x.\n", pic->nb_slices, params_size,
102 pic->slice_buffers[index]);
103
104 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
105 VASliceDataBufferType,
106 slice_size, 1, (void*)slice_data,
107 &pic->slice_buffers[index + 1]);
108 if (vas != VA_STATUS_SUCCESS) {
109 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
110 "data buffer (size %zu): %d (%s).\n",
111 slice_size, vas, vaErrorStr(vas));
112 vaDestroyBuffer(ctx->hwctx->display,
113 pic->slice_buffers[index]);
114 return AVERROR(EIO);
115 }
116
117 av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
118 "is %#x.\n", pic->nb_slices, slice_size,
119 pic->slice_buffers[index + 1]);
120
121 ++pic->nb_slices;
122 return 0;
123 }
124
125 static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
126 VAAPIDecodePicture *pic)
127 {
128 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
129 VAStatus vas;
130 int i;
131
132 for (i = 0; i < pic->nb_param_buffers; i++) {
133 vas = vaDestroyBuffer(ctx->hwctx->display,
134 pic->param_buffers[i]);
135 if (vas != VA_STATUS_SUCCESS) {
136 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
137 "parameter buffer %#x: %d (%s).\n",
138 pic->param_buffers[i], vas, vaErrorStr(vas));
139 }
140 }
141
142 for (i = 0; i < 2 * pic->nb_slices; i++) {
143 vas = vaDestroyBuffer(ctx->hwctx->display,
144 pic->slice_buffers[i]);
145 if (vas != VA_STATUS_SUCCESS) {
146 av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
147 "slice buffer %#x: %d (%s).\n",
148 pic->slice_buffers[i], vas, vaErrorStr(vas));
149 }
150 }
151 }
152
153 int ff_vaapi_decode_issue(AVCodecContext *avctx,
154 VAAPIDecodePicture *pic)
155 {
156 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
157 VAStatus vas;
158 int err;
159
160 av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
161 pic->output_surface);
162
163 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
164 pic->output_surface);
165 if (vas != VA_STATUS_SUCCESS) {
166 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
167 "issue: %d (%s).\n", vas, vaErrorStr(vas));
168 err = AVERROR(EIO);
169 goto fail_with_picture;
170 }
171
172 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
173 pic->param_buffers, pic->nb_param_buffers);
174 if (vas != VA_STATUS_SUCCESS) {
175 av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
176 "parameters: %d (%s).\n", vas, vaErrorStr(vas));
177 err = AVERROR(EIO);
178 goto fail_with_picture;
179 }
180
181 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
182 pic->slice_buffers, 2 * pic->nb_slices);
183 if (vas != VA_STATUS_SUCCESS) {
184 av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
185 "%d (%s).\n", vas, vaErrorStr(vas));
186 err = AVERROR(EIO);
187 goto fail_with_picture;
188 }
189
190 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
191 if (vas != VA_STATUS_SUCCESS) {
192 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
193 "issue: %d (%s).\n", vas, vaErrorStr(vas));
194 err = AVERROR(EIO);
195 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
196 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
197 goto fail;
198 else
199 goto fail_at_end;
200 }
201
202 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
203 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
204 ff_vaapi_decode_destroy_buffers(avctx, pic);
205
206 err = 0;
207 goto exit;
208
209 fail_with_picture:
210 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
211 if (vas != VA_STATUS_SUCCESS) {
212 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
213 "after error: %d (%s).\n", vas, vaErrorStr(vas));
214 }
215 fail:
216 ff_vaapi_decode_destroy_buffers(avctx, pic);
217 fail_at_end:
218 exit:
219 pic->nb_param_buffers = 0;
220 pic->nb_slices = 0;
221 pic->slices_allocated = 0;
222 av_freep(&pic->slice_buffers);
223
224 return err;
225 }
226
227 int ff_vaapi_decode_cancel(AVCodecContext *avctx,
228 VAAPIDecodePicture *pic)
229 {
230 ff_vaapi_decode_destroy_buffers(avctx, pic);
231
232 pic->nb_param_buffers = 0;
233 pic->nb_slices = 0;
234 pic->slices_allocated = 0;
235 av_freep(&pic->slice_buffers);
236
237 return 0;
238 }
239
240 static const struct {
241 uint32_t fourcc;
242 enum AVPixelFormat pix_fmt;
243 } vaapi_format_map[] = {
244 #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
245 // 4:0:0
246 MAP(Y800, GRAY8),
247 // 4:2:0
248 MAP(NV12, NV12),
249 MAP(YV12, YUV420P),
250 MAP(IYUV, YUV420P),
251 #ifdef VA_FOURCC_I420
252 MAP(I420, YUV420P),
253 #endif
254 MAP(IMC3, YUV420P),
255 // 4:1:1
256 MAP(411P, YUV411P),
257 // 4:2:2
258 MAP(422H, YUV422P),
259 #ifdef VA_FOURCC_YV16
260 MAP(YV16, YUV422P),
261 #endif
262 MAP(YUY2, YUYV422),
263 #ifdef VA_FOURCC_Y210
264 MAP(Y210, Y210),
265 #endif
266 #ifdef VA_FOURCC_Y212
267 MAP(Y212, Y212),
268 #endif
269 // 4:4:0
270 MAP(422V, YUV440P),
271 // 4:4:4
272 MAP(444P, YUV444P),
273 #ifdef VA_FOURCC_XYUV
274 MAP(XYUV, VUYX),
275 #endif
276 #ifdef VA_FOURCC_Y410
277 MAP(Y410, XV30),
278 #endif
279 #ifdef VA_FOURCC_Y412
280 MAP(Y412, XV36),
281 #endif
282 // 4:2:0 10-bit
283 #ifdef VA_FOURCC_P010
284 MAP(P010, P010),
285 #endif
286 #ifdef VA_FOURCC_P012
287 MAP(P012, P012),
288 #endif
289 #ifdef VA_FOURCC_I010
290 MAP(I010, YUV420P10),
291 #endif
292 #undef MAP
293 };
294
295 static int vaapi_decode_find_best_format(AVCodecContext *avctx,
296 AVHWDeviceContext *device,
297 VAConfigID config_id,
298 AVHWFramesContext *frames)
299 {
300 AVVAAPIDeviceContext *hwctx = device->hwctx;
301 VAStatus vas;
302 VASurfaceAttrib *attr;
303 enum AVPixelFormat source_format, best_format, format;
304 uint32_t best_fourcc, fourcc;
305 int i, j, nb_attr;
306
307 source_format = avctx->sw_pix_fmt;
308 av_assert0(source_format != AV_PIX_FMT_NONE);
309
310 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
311 NULL, &nb_attr);
312 if (vas != VA_STATUS_SUCCESS) {
313 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
314 "%d (%s).\n", vas, vaErrorStr(vas));
315 return AVERROR(ENOSYS);
316 }
317
318 attr = av_malloc_array(nb_attr, sizeof(*attr));
319 if (!attr)
320 return AVERROR(ENOMEM);
321
322 vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
323 attr, &nb_attr);
324 if (vas != VA_STATUS_SUCCESS) {
325 av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
326 "%d (%s).\n", vas, vaErrorStr(vas));
327 av_freep(&attr);
328 return AVERROR(ENOSYS);
329 }
330
331 best_format = AV_PIX_FMT_NONE;
332
333 for (i = 0; i < nb_attr; i++) {
334 if (attr[i].type != VASurfaceAttribPixelFormat)
335 continue;
336
337 fourcc = attr[i].value.value.i;
338 for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
339 if (fourcc == vaapi_format_map[j].fourcc)
340 break;
341 }
342 if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
343 av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
344 fourcc);
345 continue;
346 }
347 format = vaapi_format_map[j].pix_fmt;
348 av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
349 fourcc, av_get_pix_fmt_name(format));
350
351 best_format = av_find_best_pix_fmt_of_2(format, best_format,
352 source_format, 0, NULL);
353 if (format == best_format)
354 best_fourcc = fourcc;
355 }
356
357 av_freep(&attr);
358
359 if (best_format == AV_PIX_FMT_NONE) {
360 av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
361 return AVERROR(EINVAL);
362 }
363
364 av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
365 av_get_pix_fmt_name(best_format), best_fourcc,
366 av_get_pix_fmt_name(source_format));
367
368 frames->sw_format = best_format;
369 if (avctx->internal->hwaccel_priv_data) {
370 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
371 AVVAAPIFramesContext *avfc = frames->hwctx;
372
373 ctx->pixel_format_attribute = (VASurfaceAttrib) {
374 .type = VASurfaceAttribPixelFormat,
375 .flags = VA_SURFACE_ATTRIB_SETTABLE,
376 .value.type = VAGenericValueTypeInteger,
377 .value.value.i = best_fourcc,
378 };
379
380 avfc->attributes = &ctx->pixel_format_attribute;
381 avfc->nb_attributes = 1;
382 }
383
384 return 0;
385 }
386
387 static const struct {
388 enum AVCodecID codec_id;
389 int codec_profile;
390 VAProfile va_profile;
391 VAProfile (*profile_parser)(AVCodecContext *avctx);
392 } vaapi_profile_map[] = {
393 #define MAP(c, p, v, ...) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v, __VA_ARGS__ }
394 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
395 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
396 MAP(H263, UNKNOWN, H263Baseline),
397 MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
398 MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
399 MPEG4AdvancedSimple),
400 MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
401 MAP(H264, H264_CONSTRAINED_BASELINE,
402 H264ConstrainedBaseline),
403 MAP(H264, H264_MAIN, H264Main ),
404 MAP(H264, H264_HIGH, H264High ),
405 #if VA_CHECK_VERSION(0, 37, 0)
406 MAP(HEVC, HEVC_MAIN, HEVCMain ),
407 MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
408 MAP(HEVC, HEVC_MAIN_STILL_PICTURE,
409 HEVCMain ),
410 #endif
411 #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL
412 MAP(HEVC, HEVC_REXT, None,
413 ff_vaapi_parse_hevc_rext_profile ),
414 #endif
415 MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
416 JPEGBaseline),
417 MAP(WMV3, VC1_SIMPLE, VC1Simple ),
418 MAP(WMV3, VC1_MAIN, VC1Main ),
419 MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
420 MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
421 MAP(VC1, VC1_SIMPLE, VC1Simple ),
422 MAP(VC1, VC1_MAIN, VC1Main ),
423 MAP(VC1, VC1_COMPLEX, VC1Advanced ),
424 MAP(VC1, VC1_ADVANCED, VC1Advanced ),
425 MAP(VP8, UNKNOWN, VP8Version0_3 ),
426 #if VA_CHECK_VERSION(0, 38, 0)
427 MAP(VP9, VP9_0, VP9Profile0 ),
428 #endif
429 #if VA_CHECK_VERSION(0, 39, 0)
430 MAP(VP9, VP9_1, VP9Profile1 ),
431 MAP(VP9, VP9_2, VP9Profile2 ),
432 MAP(VP9, VP9_3, VP9Profile3 ),
433 #endif
434 #if VA_CHECK_VERSION(1, 8, 0)
435 MAP(AV1, AV1_MAIN, AV1Profile0),
436 MAP(AV1, AV1_HIGH, AV1Profile1),
437 #endif
438
439 #undef MAP
440 };
441
442 /*
443 * Set *va_config and the frames_ref fields from the current codec parameters
444 * in avctx.
445 */
446 static int vaapi_decode_make_config(AVCodecContext *avctx,
447 AVBufferRef *device_ref,
448 VAConfigID *va_config,
449 AVBufferRef *frames_ref)
450 {
451 AVVAAPIHWConfig *hwconfig = NULL;
452 AVHWFramesConstraints *constraints = NULL;
453 VAStatus vas;
454 int err, i, j;
455 const AVCodecDescriptor *codec_desc;
456 VAProfile *profile_list = NULL, matched_va_profile, va_profile;
457 int profile_count, exact_match, matched_ff_profile, codec_profile;
458
459 AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
460 AVVAAPIDeviceContext *hwctx = device->hwctx;
461
462 codec_desc = avcodec_descriptor_get(avctx->codec_id);
463 if (!codec_desc) {
464 err = AVERROR(EINVAL);
465 goto fail;
466 }
467
468 profile_count = vaMaxNumProfiles(hwctx->display);
469 profile_list = av_malloc_array(profile_count,
470 sizeof(VAProfile));
471 if (!profile_list) {
472 err = AVERROR(ENOMEM);
473 goto fail;
474 }
475
476 vas = vaQueryConfigProfiles(hwctx->display,
477 profile_list, &profile_count);
478 if (vas != VA_STATUS_SUCCESS) {
479 av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
480 "%d (%s).\n", vas, vaErrorStr(vas));
481 err = AVERROR(ENOSYS);
482 goto fail;
483 }
484
485 matched_va_profile = VAProfileNone;
486 exact_match = 0;
487
488 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
489 int profile_match = 0;
490 if (avctx->codec_id != vaapi_profile_map[i].codec_id)
491 continue;
492 if (avctx->profile == vaapi_profile_map[i].codec_profile ||
493 vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
494 profile_match = 1;
495
496 va_profile = vaapi_profile_map[i].profile_parser ?
497 vaapi_profile_map[i].profile_parser(avctx) :
498 vaapi_profile_map[i].va_profile;
499 codec_profile = vaapi_profile_map[i].codec_profile;
500
501 for (j = 0; j < profile_count; j++) {
502 if (va_profile == profile_list[j]) {
503 exact_match = profile_match;
504 break;
505 }
506 }
507 if (j < profile_count) {
508 matched_va_profile = va_profile;
509 matched_ff_profile = codec_profile;
510 if (exact_match)
511 break;
512 }
513 }
514 av_freep(&profile_list);
515
516 if (matched_va_profile == VAProfileNone) {
517 av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
518 "profile %d.\n", codec_desc->name, avctx->profile);
519 err = AVERROR(ENOSYS);
520 goto fail;
521 }
522 if (!exact_match) {
523 if (avctx->hwaccel_flags &
524 AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
525 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
526 "supported for hardware decode.\n",
527 codec_desc->name, avctx->profile);
528 av_log(avctx, AV_LOG_WARNING, "Using possibly-"
529 "incompatible profile %d instead.\n",
530 matched_ff_profile);
531 } else {
532 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
533 "supported for hardware decode.\n",
534 codec_desc->name, avctx->profile);
535 err = AVERROR(EINVAL);
536 goto fail;
537 }
538 }
539
540 vas = vaCreateConfig(hwctx->display, matched_va_profile,
541 VAEntrypointVLD, NULL, 0,
542 va_config);
543 if (vas != VA_STATUS_SUCCESS) {
544 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
545 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
546 err = AVERROR(EIO);
547 goto fail;
548 }
549
550 hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
551 if (!hwconfig) {
552 err = AVERROR(ENOMEM);
553 goto fail;
554 }
555 hwconfig->config_id = *va_config;
556
557 constraints =
558 av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
559 if (!constraints) {
560 err = AVERROR(ENOMEM);
561 goto fail;
562 }
563
564 if (avctx->coded_width < constraints->min_width ||
565 avctx->coded_height < constraints->min_height ||
566 avctx->coded_width > constraints->max_width ||
567 avctx->coded_height > constraints->max_height) {
568 av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
569 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
570 avctx->coded_width, avctx->coded_height,
571 constraints->min_width, constraints->max_width,
572 constraints->min_height, constraints->max_height);
573 err = AVERROR(EINVAL);
574 goto fail;
575 }
576 if (!constraints->valid_sw_formats ||
577 constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
578 av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
579 "usable surface formats.\n");
580 err = AVERROR(EINVAL);
581 goto fail;
582 }
583
584 if (frames_ref) {
585 AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
586
587 frames->format = AV_PIX_FMT_VAAPI;
588 frames->width = avctx->coded_width;
589 frames->height = avctx->coded_height;
590
591 err = vaapi_decode_find_best_format(avctx, device,
592 *va_config, frames);
593 if (err < 0)
594 goto fail;
595
596 frames->initial_pool_size = 1;
597 // Add per-codec number of surfaces used for storing reference frames.
598 switch (avctx->codec_id) {
599 case AV_CODEC_ID_H264:
600 case AV_CODEC_ID_HEVC:
601 case AV_CODEC_ID_AV1:
602 frames->initial_pool_size += 16;
603 break;
604 case AV_CODEC_ID_VP9:
605 frames->initial_pool_size += 8;
606 break;
607 case AV_CODEC_ID_VP8:
608 frames->initial_pool_size += 3;
609 break;
610 default:
611 frames->initial_pool_size += 2;
612 }
613 }
614
615 av_hwframe_constraints_free(&constraints);
616 av_freep(&hwconfig);
617
618 return 0;
619
620 fail:
621 av_hwframe_constraints_free(&constraints);
622 av_freep(&hwconfig);
623 if (*va_config != VA_INVALID_ID) {
624 vaDestroyConfig(hwctx->display, *va_config);
625 *va_config = VA_INVALID_ID;
626 }
627 av_freep(&profile_list);
628 return err;
629 }
630
631 int ff_vaapi_common_frame_params(AVCodecContext *avctx,
632 AVBufferRef *hw_frames_ctx)
633 {
634 AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
635 AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
636 AVVAAPIDeviceContext *hwctx;
637 VAConfigID va_config = VA_INVALID_ID;
638 int err;
639
640 if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
641 return AVERROR(EINVAL);
642 hwctx = device_ctx->hwctx;
643
644 err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
645 hw_frames_ctx);
646 if (err)
647 return err;
648
649 if (va_config != VA_INVALID_ID)
650 vaDestroyConfig(hwctx->display, va_config);
651
652 return 0;
653 }
654
655 int ff_vaapi_decode_init(AVCodecContext *avctx)
656 {
657 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
658 VAStatus vas;
659 int err;
660
661 ctx->va_config = VA_INVALID_ID;
662 ctx->va_context = VA_INVALID_ID;
663
664 err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
665 if (err < 0)
666 goto fail;
667
668 ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
669 ctx->hwfc = ctx->frames->hwctx;
670 ctx->device = ctx->frames->device_ctx;
671 ctx->hwctx = ctx->device->hwctx;
672
673 err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
674 &ctx->va_config, NULL);
675 if (err)
676 goto fail;
677
678 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
679 avctx->coded_width, avctx->coded_height,
680 VA_PROGRESSIVE,
681 ctx->hwfc->surface_ids,
682 ctx->hwfc->nb_surfaces,
683 &ctx->va_context);
684 if (vas != VA_STATUS_SUCCESS) {
685 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
686 "context: %d (%s).\n", vas, vaErrorStr(vas));
687 err = AVERROR(EIO);
688 goto fail;
689 }
690
691 av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
692 "%#x/%#x.\n", ctx->va_config, ctx->va_context);
693
694 return 0;
695
696 fail:
697 ff_vaapi_decode_uninit(avctx);
698 return err;
699 }
700
701 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
702 {
703 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
704 VAStatus vas;
705
706 if (ctx->va_context != VA_INVALID_ID) {
707 vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
708 if (vas != VA_STATUS_SUCCESS) {
709 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
710 "context %#x: %d (%s).\n",
711 ctx->va_context, vas, vaErrorStr(vas));
712 }
713 }
714 if (ctx->va_config != VA_INVALID_ID) {
715 vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
716 if (vas != VA_STATUS_SUCCESS) {
717 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
718 "configuration %#x: %d (%s).\n",
719 ctx->va_config, vas, vaErrorStr(vas));
720 }
721 }
722
723 return 0;
724 }
725