FFmpeg coverage


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