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
22
28
29
35 {
37 VAStatus vas;
39
41
42 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
44 if (vas != VA_STATUS_SUCCESS) {
46 "buffer (type %d): %d (%s).\n",
47 type, vas, vaErrorStr(vas));
49 }
50
52
55 return 0;
56 }
57
58
61 const void *params_data,
62 size_t params_size,
63 const void *slice_data,
64 size_t slice_size)
65 {
67 VAStatus vas;
69
74 else
76
83 }
85
87
88 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
89 VASliceParameterBufferType,
90 params_size, 1, (void*)params_data,
92 if (vas != VA_STATUS_SUCCESS) {
94 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
96 }
97
101
102 vas = vaCreateBuffer(
ctx->hwctx->display,
ctx->va_context,
103 VASliceDataBufferType,
104 slice_size, 1, (void*)slice_data,
106 if (vas != VA_STATUS_SUCCESS) {
108 "data buffer (size %zu): %d (%s).\n",
109 slice_size, vas, vaErrorStr(vas));
110 vaDestroyBuffer(
ctx->hwctx->display,
113 }
114
118
120 return 0;
121 }
122
125 {
127 VAStatus vas;
129
131 vas = vaDestroyBuffer(
ctx->hwctx->display,
133 if (vas != VA_STATUS_SUCCESS) {
135 "parameter buffer %#x: %d (%s).\n",
137 }
138 }
139
141 vas = vaDestroyBuffer(
ctx->hwctx->display,
143 if (vas != VA_STATUS_SUCCESS) {
145 "slice buffer %#x: %d (%s).\n",
147 }
148 }
149 }
150
153 {
155 VAStatus vas;
156 int err;
157
160
161 vas = vaBeginPicture(
ctx->hwctx->display,
ctx->va_context,
163 if (vas != VA_STATUS_SUCCESS) {
165 "issue: %d (%s).\n", vas, vaErrorStr(vas));
167 goto fail_with_picture;
168 }
169
170 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
172 if (vas != VA_STATUS_SUCCESS) {
174 "parameters: %d (%s).\n", vas, vaErrorStr(vas));
176 goto fail_with_picture;
177 }
178
179 vas = vaRenderPicture(
ctx->hwctx->display,
ctx->va_context,
181 if (vas != VA_STATUS_SUCCESS) {
183 "%d (%s).\n", vas, vaErrorStr(vas));
185 goto fail_with_picture;
186 }
187
188 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
189 if (vas != VA_STATUS_SUCCESS) {
191 "issue: %d (%s).\n", vas, vaErrorStr(vas));
193 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
196 else
197 goto fail_at_end;
198 }
199
200 if (CONFIG_VAAPI_1 ||
ctx->hwctx->driver_quirks &
203
204 err = 0;
205 goto exit;
206
207 fail_with_picture:
208 vas = vaEndPicture(
ctx->hwctx->display,
ctx->va_context);
209 if (vas != VA_STATUS_SUCCESS) {
211 "after error: %d (%s).\n", vas, vaErrorStr(vas));
212 }
215 fail_at_end:
216 exit:
221
222 return err;
223 }
224
227 {
229
234
235 return 0;
236 }
237
238 static const struct {
242 #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
243 // 4:0:0
245 // 4:2:0
249 #ifdef VA_FOURCC_I420
251 #endif
253 // 4:1:1
255 // 4:2:2
257 #ifdef VA_FOURCC_YV16
259 #endif
261 #ifdef VA_FOURCC_Y210
263 #endif
264 // 4:4:0
266 // 4:4:4
268 // 4:2:0 10-bit
269 #ifdef VA_FOURCC_P010
271 #endif
272 #ifdef VA_FOURCC_I010
273 MAP(I010, YUV420P10),
274 #endif
275 #undef MAP
276 };
277
280 VAConfigID config_id,
282 {
284 VAStatus vas;
285 VASurfaceAttrib *attr;
287 uint32_t best_fourcc,
fourcc;
289
292
293 vas = vaQuerySurfaceAttributes(hwctx->
display, config_id,
295 if (vas != VA_STATUS_SUCCESS) {
297 "%d (%s).\n", vas, vaErrorStr(vas));
299 }
300
302 if (!attr)
304
305 vas = vaQuerySurfaceAttributes(hwctx->
display, config_id,
306 attr, &nb_attr);
307 if (vas != VA_STATUS_SUCCESS) {
309 "%d (%s).\n", vas, vaErrorStr(vas));
312 }
313
315
316 for (
i = 0;
i < nb_attr;
i++) {
317 if (attr[
i].
type != VASurfaceAttribPixelFormat)
318 continue;
319
320 fourcc = attr[
i].value.value.i;
323 break;
324 }
328 continue;
329 }
333
335 source_format, 0,
NULL);
336 if (
format == best_format)
338 }
339
341
345 }
346
350
351 frames->sw_format = best_format;
355
356 ctx->pixel_format_attribute = (VASurfaceAttrib) {
357 .type = VASurfaceAttribPixelFormat,
358 .value.value.i = best_fourcc,
359 };
360
363 }
364
365 return 0;
366 }
367
368 static const struct {
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 ),
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,
390 HEVCMain ),
391 #endif
392 #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL
393 MAP(HEVC, HEVC_REXT, None,
395 #endif
396 MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
397 JPEGBaseline),
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 ),
407 #if VA_CHECK_VERSION(0, 38, 0)
408 MAP(VP9, VP9_0, VP9Profile0 ),
409 #endif
410 #if VA_CHECK_VERSION(0, 39, 0)
411 MAP(VP9, VP9_2, VP9Profile2 ),
412 #endif
413 #if VA_CHECK_VERSION(1, 8, 0)
414 MAP(AV1, AV1_MAIN, AV1Profile0),
415 MAP(AV1, AV1_HIGH, AV1Profile1),
416 #endif
417
418 #undef MAP
419 };
420
421 /*
422 * Set *va_config and the frames_ref fields from the current codec parameters
423 * in avctx.
424 */
427 VAConfigID *va_config,
429 {
432 VAStatus vas;
436 int profile_count, exact_match, matched_ff_profile,
codec_profile;
437
440
442 if (!codec_desc) {
445 }
446
447 profile_count = vaMaxNumProfiles(hwctx->display);
449 sizeof(VAProfile));
450 if (!profile_list) {
453 }
454
455 vas = vaQueryConfigProfiles(hwctx->display,
456 profile_list, &profile_count);
457 if (vas != VA_STATUS_SUCCESS) {
459 "%d (%s).\n", vas, vaErrorStr(vas));
462 }
463
464 matched_va_profile = VAProfileNone;
465 exact_match = 0;
466
468 int profile_match = 0;
470 continue;
473 profile_match = 1;
474
479
480 for (j = 0; j < profile_count; j++) {
482 exact_match = profile_match;
483 break;
484 }
485 }
486 if (j < profile_count) {
489 if (exact_match)
490 break;
491 }
492 }
494
495 if (matched_va_profile == VAProfileNone) {
497 "profile %d.\n", codec_desc->
name, avctx->
profile);
500 }
501 if (!exact_match) {
505 "supported for hardware decode.\n",
508 "incompatible profile %d instead.\n",
509 matched_ff_profile);
510 } else {
512 "supported for hardware decode.\n",
516 }
517 }
518
519 vas = vaCreateConfig(hwctx->display, matched_va_profile,
520 VAEntrypointVLD,
NULL, 0,
521 va_config);
522 if (vas != VA_STATUS_SUCCESS) {
524 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
527 }
528
530 if (!hwconfig) {
533 }
535
536 constraints =
538 if (!constraints) {
541 }
542
548 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
554 }
558 "usable surface formats.\n");
561 }
562
563 if (frames_ref) {
565
569
572 if (err < 0)
574
575 frames->initial_pool_size = 1;
576 // Add per-codec number of surfaces used for storing reference frames.
581 frames->initial_pool_size += 16;
582 break;
584 frames->initial_pool_size += 8;
585 break;
587 frames->initial_pool_size += 3;
588 break;
589 default:
590 frames->initial_pool_size += 2;
591 }
592 }
593
596
597 return 0;
598
602 if (*va_config != VA_INVALID_ID) {
603 vaDestroyConfig(hwctx->display, *va_config);
604 *va_config = VA_INVALID_ID;
605 }
607 return err;
608 }
609
612 {
616 VAConfigID va_config = VA_INVALID_ID;
617 int err;
618
621 hwctx = device_ctx->hwctx;
622
624 hw_frames_ctx);
625 if (err)
626 return err;
627
628 if (va_config != VA_INVALID_ID)
629 vaDestroyConfig(hwctx->display, va_config);
630
631 return 0;
632 }
633
635 {
637 VAStatus vas;
638 int err;
639
640 ctx->va_config = VA_INVALID_ID;
641 ctx->va_context = VA_INVALID_ID;
642
644 if (err < 0)
646
648 ctx->hwfc =
ctx->frames->hwctx;
649 ctx->device =
ctx->frames->device_ctx;
650 ctx->hwctx =
ctx->device->hwctx;
651
654 if (err)
656
657 vas = vaCreateContext(
ctx->hwctx->display,
ctx->va_config,
659 VA_PROGRESSIVE,
660 ctx->hwfc->surface_ids,
661 ctx->hwfc->nb_surfaces,
663 if (vas != VA_STATUS_SUCCESS) {
665 "context: %d (%s).\n", vas, vaErrorStr(vas));
668 }
669
671 "%#x/%#x.\n",
ctx->va_config,
ctx->va_context);
672
673 return 0;
674
677 return err;
678 }
679
681 {
683 VAStatus vas;
684
685 if (
ctx->va_context != VA_INVALID_ID) {
686 vas = vaDestroyContext(
ctx->hwctx->display,
ctx->va_context);
687 if (vas != VA_STATUS_SUCCESS) {
689 "context %#x: %d (%s).\n",
690 ctx->va_context, vas, vaErrorStr(vas));
691 }
692 }
693 if (
ctx->va_config != VA_INVALID_ID) {
694 vas = vaDestroyConfig(
ctx->hwctx->display,
ctx->va_config);
695 if (vas != VA_STATUS_SUCCESS) {
697 "configuration %#x: %d (%s).\n",
698 ctx->va_config, vas, vaErrorStr(vas));
699 }
700 }
701
702 return 0;
703 }