FFmpeg
Loading...
Searching...
No Matches
hwcontext_amf.c
Go to the documentation of this file.
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 "mem.h"
20#include "buffer.h"
21#include "pixfmt.h"
22#include "pixdesc.h"
23#include "imgutils.h"
24#include "hwcontext.h"
25#include "hwcontext_amf.h"
26#include "hwcontext_internal.h"
28#include "internal.h"
29
30#include "libavutil/thread.h"
31#include "libavutil/avassert.h"
32
33#include <AMF/core/Surface.h>
34#include <AMF/core/Trace.h>
35
36#if CONFIG_VULKAN
37#include <AMF/core/VulkanAMF.h>
38#include "hwcontext_vulkan.h"
39#endif
40#if CONFIG_D3D11VA
42#endif
43#if CONFIG_D3D12VA
45#endif
46#if CONFIG_DXVA2
47#define COBJMACROS
49#endif
50#ifdef _WIN32
51#include "compat/w32dlfcn.h"
52#else
53#include <dlfcn.h>
54#endif
55#define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
56
57typedef struct AMFDevicePriv {
59#if CONFIG_VULKAN
60 AMFVulkanDevice vk_dev;
61#endif
63
64static void amf_lock_default(void *opaque)
65{
66 ff_mutex_lock((AVMutex*)opaque);
67}
68
69static void amf_unlock_default(void *opaque)
70{
71 ff_mutex_unlock((AVMutex*)opaque);
72}
73
74typedef struct AmfTraceWriter {
75 AMFTraceWriterVtbl *vtblp;
76 void *avctx;
77 AMFTraceWriterVtbl vtbl;
79
80static void AMF_CDECL_CALL AMFTraceWriter_Write(AMFTraceWriter *pThis,
81 const wchar_t *scope, const wchar_t *message)
82{
83 AmfTraceWriter *tracer = (AmfTraceWriter*)pThis;
84 av_log(tracer->avctx, AV_LOG_DEBUG, "%ls: %ls", scope, message); // \n is provided from AMF
85}
86
87static void AMF_CDECL_CALL AMFTraceWriter_Flush(AMFTraceWriter *pThis)
88{
89}
90
91static AmfTraceWriter * amf_writer_alloc(void *avctx)
92{
93 AmfTraceWriter * writer = av_mallocz(sizeof(AmfTraceWriter));
94 if (!writer)
95 return NULL;
96
97 writer->vtblp = &writer->vtbl;
98 writer->vtblp->Write = AMFTraceWriter_Write;
99 writer->vtblp->Flush = AMFTraceWriter_Flush;
100 writer->avctx = avctx;
101
102 return writer;
103}
104
105static void amf_writer_free(void *opaque)
106{
107 AmfTraceWriter *writer = (AmfTraceWriter *)opaque;
108 av_freep(&writer);
109}
110
111/**
112 * We still need AVHWFramesContext to utilize our hardware memory
113 * otherwise, we will receive the error "HW format requires hw_frames_ctx to be non-NULL".
114 * (libavfilter\buffersrc.c function query_formats)
115*/
116typedef struct {
117 void *dummy;
119
120typedef struct AVAMFFormatMap {
122 enum AMF_SURFACE_FORMAT amf_format;
123} FormatMap;
124
126{
127 { AV_PIX_FMT_NONE, AMF_SURFACE_UNKNOWN },
128 { AV_PIX_FMT_NV12, AMF_SURFACE_NV12 },
129 { AV_PIX_FMT_BGR0, AMF_SURFACE_BGRA },
130 { AV_PIX_FMT_RGB0, AMF_SURFACE_RGBA },
131 { AV_PIX_FMT_BGRA, AMF_SURFACE_BGRA },
132 { AV_PIX_FMT_ARGB, AMF_SURFACE_ARGB },
133 { AV_PIX_FMT_RGBA, AMF_SURFACE_RGBA },
134 { AV_PIX_FMT_GRAY8, AMF_SURFACE_GRAY8 },
135 { AV_PIX_FMT_YUV420P, AMF_SURFACE_YUV420P },
136 { AV_PIX_FMT_YUYV422, AMF_SURFACE_YUY2 },
137 { AV_PIX_FMT_P010, AMF_SURFACE_P010 },
138 { AV_PIX_FMT_X2BGR10, AMF_SURFACE_R10G10B10A2 },
139 { AV_PIX_FMT_RGBAF16, AMF_SURFACE_RGBA_F16},
140};
141
142enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
143{
144 int i;
145 for (i = 0; i < amf_countof(format_map); i++) {
146 if (format_map[i].av_format == fmt) {
147 return format_map[i].amf_format;
148 }
149 }
150 return AMF_SURFACE_UNKNOWN;
151}
152
153enum AVPixelFormat av_amf_to_av_format(enum AMF_SURFACE_FORMAT fmt)
154{
155 int i;
156 for (i = 0; i < amf_countof(format_map); i++) {
157 if (format_map[i].amf_format == fmt) {
158 return format_map[i].av_format;
159 }
160 }
161 return AV_PIX_FMT_NONE;
162}
163
164enum AMF_VIDEO_CONVERTER_COLOR_PROFILE_ENUM av_amf_get_color_profile(enum AVColorRange color_range, enum AVColorSpace color_space)
165{
166 switch (color_space) {
169 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
170 } else {
171 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
172 }
173 break;
174 case AVCOL_SPC_BT709:
176 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
177 } else {
178 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
179 }
180 break;
184 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
185 } else {
186 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
187 }
188 break;
189
190 default:
191 return AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
192 }
193}
194
195int av_amf_display_mastering_meta_to_hdrmeta(const AVMasteringDisplayMetadata *display_meta, AMFHDRMetadata *hdrmeta)
196{
197 if (!display_meta || !hdrmeta)
198 return AVERROR(EINVAL);
199
200 if (display_meta->has_luminance) {
201 const unsigned int luma_den = 10000;
202 hdrmeta->maxMasteringLuminance =
203 (amf_uint32)(luma_den * av_q2d(display_meta->max_luminance));
204 hdrmeta->minMasteringLuminance =
205 FFMIN((amf_uint32)(luma_den * av_q2d(display_meta->min_luminance)), hdrmeta->maxMasteringLuminance);
206 }
207
208 if (display_meta->has_primaries) {
209 const unsigned int chroma_den = 50000;
210 hdrmeta->redPrimary[0] =
211 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][0])), chroma_den);
212 hdrmeta->redPrimary[1] =
213 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][1])), chroma_den);
214 hdrmeta->greenPrimary[0] =
215 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][0])), chroma_den);
216 hdrmeta->greenPrimary[1] =
217 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][1])), chroma_den);
218 hdrmeta->bluePrimary[0] =
219 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][0])), chroma_den);
220 hdrmeta->bluePrimary[1] =
221 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][1])), chroma_den);
222 hdrmeta->whitePoint[0] =
223 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[0])), chroma_den);
224 hdrmeta->whitePoint[1] =
225 FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[1])), chroma_den);
226 }
227
228 return 0;
229}
230
231int av_amf_light_metadata_to_hdrmeta(const AVContentLightMetadata *light_meta, AMFHDRMetadata *hdrmeta)
232{
233 if (!light_meta || !hdrmeta)
234 return AVERROR(EINVAL);
235
236 hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->MaxCLL;
237 hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->MaxFALL;
238
239 return 0;
240}
241
242int av_amf_extract_hdr_metadata(const AVFrame *frame, AMFHDRMetadata *hdrmeta)
243{
244 AVFrameSideData *sidedata;
245 AVContentLightMetadata *content_light = NULL;
246 AVMasteringDisplayMetadata *mastering_display = NULL;
247
248 if (!frame || !hdrmeta)
249 return AVERROR(EINVAL);
250
252 if (sidedata) {
253 mastering_display = (AVMasteringDisplayMetadata *)sidedata->data;
254 if (av_amf_display_mastering_meta_to_hdrmeta(mastering_display, hdrmeta) != 0)
255 mastering_display = NULL;
256 }
257
259 if (sidedata) {
260 content_light = (AVContentLightMetadata *)sidedata->data;
261 if (av_amf_light_metadata_to_hdrmeta(content_light, hdrmeta) != 0)
262 content_light = NULL;
263 }
264
265 if (!mastering_display && !content_light)
266 return AVERROR(ENODATA);
267
268 return 0;
269}
270
271int av_amf_attach_hdr_metadata(AVFrame *frame, const AMFHDRMetadata *hdrmeta) {
272 if (!hdrmeta || !frame)
273 return AVERROR(EINVAL);
274
275 AVMasteringDisplayMetadata *mastering =
277 const int chroma_den = 50000;
278 const int luma_den = 10000;
279
280 if (!mastering)
281 return AVERROR(ENOMEM);
282
283 mastering->display_primaries[0][0] =
284 av_make_q(hdrmeta->redPrimary[0], chroma_den);
285 mastering->display_primaries[0][1] =
286 av_make_q(hdrmeta->redPrimary[1], chroma_den);
287
288 mastering->display_primaries[1][0] =
289 av_make_q(hdrmeta->greenPrimary[0], chroma_den);
290 mastering->display_primaries[1][1] =
291 av_make_q(hdrmeta->greenPrimary[1], chroma_den);
292
293 mastering->display_primaries[2][0] =
294 av_make_q(hdrmeta->bluePrimary[0], chroma_den);
295 mastering->display_primaries[2][1] =
296 av_make_q(hdrmeta->bluePrimary[1], chroma_den);
297
298 mastering->white_point[0] = av_make_q(hdrmeta->whitePoint[0], chroma_den);
299 mastering->white_point[1] = av_make_q(hdrmeta->whitePoint[1], chroma_den);
300
301 mastering->max_luminance =
302 av_make_q(hdrmeta->maxMasteringLuminance, luma_den);
303 mastering->min_luminance =
304 av_make_q(hdrmeta->maxMasteringLuminance, luma_den);
305
306 mastering->has_luminance = 1;
307 mastering->has_primaries = 1;
308 if (hdrmeta->maxContentLightLevel) {
311
312 if (!light)
313 return AVERROR(ENOMEM);
314
315 light->MaxCLL = hdrmeta->maxContentLightLevel;
316 light->MaxFALL = hdrmeta->maxFrameAverageLightLevel;
317 }
318
319 return 0;
320}
321
322static const enum AVPixelFormat supported_formats[] = {
331#if CONFIG_D3D11VA
333#endif
334#if CONFIG_D3D12VA
336#endif
337#if CONFIG_DXVA2
339#endif
340};
341
352
354 const void *hwconfig,
355 AVHWFramesConstraints *constraints)
356{
357 int i;
358
360 sizeof(*constraints->valid_sw_formats));
361 if (!constraints->valid_sw_formats)
362 return AVERROR(ENOMEM);
363
364 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
365 constraints->valid_sw_formats[i] = supported_formats[i];
367
368 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
369 if (!constraints->valid_hw_formats)
370 return AVERROR(ENOMEM);
371
373 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
374
375 return 0;
376}
377
378static void amf_dummy_free(void *opaque, uint8_t *data)
379{
380
381}
382
383static AVBufferRef *amf_pool_alloc(void *opaque, size_t size)
384{
385 AVHWFramesContext *hwfc = (AVHWFramesContext *)opaque;
386 AVBufferRef *buf;
387
389 if (!buf) {
390 av_log(hwfc, AV_LOG_ERROR, "Failed to create buffer for AMF context.\n");
391 return NULL;
392 }
393 return buf;
394}
395
397{
398 int i;
399
400 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
401 if (ctx->sw_format == supported_formats[i])
402 break;
403 }
405 av_log(ctx, AV_LOG_ERROR, "Pixel format '%s' is not supported\n",
406 av_get_pix_fmt_name(ctx->sw_format));
407 return AVERROR(ENOSYS);
408 }
409
411 av_buffer_pool_init2(sizeof(AMFSurface), ctx,
413
414 return 0;
415}
416
417
419{
420 frame->buf[0] = av_buffer_pool_get(ctx->pool);
421 if (!frame->buf[0])
422 return AVERROR(ENOMEM);
423
424 frame->data[0] = frame->buf[0]->data;
426 frame->width = ctx->width;
427 frame->height = ctx->height;
428 return 0;
429}
430
433 enum AVPixelFormat **formats)
434{
435 enum AVPixelFormat *fmts;
436 int i;
437
439 if (!fmts)
440 return AVERROR(ENOMEM);
443
444 *formats = fmts;
445
446 return 0;
447}
448
449static void amf_free_amfsurface(void *opaque, uint8_t *data)
450{
451 if(!!data){
452 AMFSurface *surface = (AMFSurface*)(data);
453 surface->pVtbl->Release(surface);
454 }
455}
456
458 const AVFrame *src)
459{
460 AMFSurface* surface = (AMFSurface*)dst->data[0];
461 AMFPlane *plane;
462 uint8_t *dst_data[4];
463 int dst_linesize[4];
464 int planes;
465 int i;
466 int res;
467 int w = FFMIN(dst->width, src->width);
468 int h = FFMIN(dst->height, src->height);
469
470 if (dst->hw_frames_ctx->data != (uint8_t *)ctx || src->format != ctx->sw_format)
471 return AVERROR(EINVAL);
472
473 if (!surface) {
474 AVHWDeviceContext *hwdev_ctx = ctx->device_ctx;
475 AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hwdev_ctx->hwctx;
476 AMF_SURFACE_FORMAT format = av_av_to_amf_format(ctx->sw_format);
477 res = amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, AMF_MEMORY_HOST, format, dst->width, dst->height, &surface);
478 AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "AllocSurface() failed with error %d\n", res);
479 dst->data[0] = (uint8_t *)surface;
480 dst->buf[1] = av_buffer_create((uint8_t *)surface, sizeof(surface),
482 NULL,
484 AMF_RETURN_IF_FALSE(ctx, !!dst->buf[1], AVERROR(ENOMEM), "av_buffer_create for amf surface failed.");
485 }
486
487 planes = (int)surface->pVtbl->GetPlanesCount(surface);
488 av_assert0(planes < FF_ARRAY_ELEMS(dst_data));
489
490 for (i = 0; i < planes; i++) {
491 plane = surface->pVtbl->GetPlaneAt(surface, i);
492 dst_data[i] = plane->pVtbl->GetNative(plane);
493 dst_linesize[i] = plane->pVtbl->GetHPitch(plane);
494 }
495 av_image_copy2(dst_data, dst_linesize,
496 src->data, src->linesize, src->format,
497 w, h);
498
499 return 0;
500}
501
503 const AVFrame *src)
504{
505 AMFSurface* surface = (AMFSurface*)src->data[0];
506 AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
507 const AMFGuid guid = IID_AMFSurface();
508 AMFData *data;
509 AMFPlane *plane;
510 uint8_t *src_data[4];
511 int src_linesize[4];
512 int planes;
513 int i;
514 int w = FFMIN(dst->width, src->width);
515 int h = FFMIN(dst->height, src->height);
516 int ret;
517
518 if (src->hw_frames_ctx->data != (uint8_t *)ctx || dst->format != ctx->sw_format)
519 return AVERROR(EINVAL);
520
521 if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_HOST) {
522 surface->pVtbl->Acquire(surface);
523 } else {
524 amf_ctx->lock(amf_ctx->lock_ctx);
525 ret = surface->pVtbl->Duplicate(surface, AMF_MEMORY_HOST, &data);
526 amf_ctx->unlock(amf_ctx->lock_ctx);
527 AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, "Duplicate(AMF_MEMORY_HOST) failed with error %d\n", ret);
528 ret = data->pVtbl->QueryInterface(data, &guid, (void**)&surface);
529 data->pVtbl->Release(data);
530 AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, "QueryInterface(AMFSurface) failed with error %d\n", ret);
531 }
532
533 planes = (int)surface->pVtbl->GetPlanesCount(surface);
534 av_assert0(planes < FF_ARRAY_ELEMS(src_data));
535
536 for (i = 0; i < planes; i++) {
537 plane = surface->pVtbl->GetPlaneAt(surface, i);
538 src_data[i] = plane->pVtbl->GetNative(plane);
539 src_linesize[i] = plane->pVtbl->GetHPitch(plane);
540 }
541 av_image_copy2(dst->data, dst->linesize,
542 src_data, src_linesize, dst->format,
543 w, h);
544 surface->pVtbl->Release(surface);
545 return 0;
546}
547
548
549
550static void amf_device_uninit(AVHWDeviceContext *device_ctx)
551{
552 AVAMFDeviceContext *amf_ctx = device_ctx->hwctx;
553 AMF_RESULT res = AMF_NOT_INITIALIZED;
554 AMFTrace *trace;
555
556 if (amf_ctx->context) {
557 if (amf_ctx->lock)
558 amf_ctx->lock(amf_ctx->lock_ctx);
559 amf_ctx->context->pVtbl->Terminate(amf_ctx->context);
560 amf_ctx->context->pVtbl->Release(amf_ctx->context);
561 if (amf_ctx->unlock)
562 amf_ctx->unlock(amf_ctx->lock_ctx);
563 amf_ctx->context = NULL;
564 }
565
566 if (amf_ctx->factory)
567 res = amf_ctx->factory->pVtbl->GetTrace(amf_ctx->factory, &trace);
568
569 if (res == AMF_OK) {
570 trace->pVtbl->UnregisterWriter(trace, FFMPEG_AMF_WRITER_ID);
571 }
572
573 if(amf_ctx->library) {
574 dlclose(amf_ctx->library);
575 amf_ctx->library = NULL;
576 }
577 if (amf_ctx->trace_writer) {
579 }
580
581 if (amf_ctx->lock == amf_lock_default) {
583 av_freep(&amf_ctx->lock_ctx);
584 amf_ctx->lock = NULL;
585 amf_ctx->unlock = NULL;
586 }
587
588 amf_ctx->version = 0;
589}
590
591enum AMF_MEMORY_TYPE av_amf_get_memory_type(AVAMFDeviceContext *amf_ctx)
592{
593 AMFContext *context = amf_ctx->context;
594 AMFContext1 *context1 = NULL;
595 AMFGuid guid1 = IID_AMFContext1();
596 void * device = NULL;
597
598#ifdef _WIN32
599 // At the moment used only for DX12.
600 AMFContext2 *context2 = NULL;
601
602 /*
603 Work around the incorrect C Context2 IID in unpatched AMF 1.5.2 headers;
604 mostly pedantic fix as it works as it is in practice.
605 TODO: replace with AMFGuid guid2 = IID_AMFContext2()
606 once AMF 1.5.3 is available to public and marked as required version in ffmpeg.
607 */
608 static const AMFGuid guid2 = {
609 0x726241d3, 0xbd46, 0x4e90,
610 0x99, 0x68, 0x93, 0xe0, 0x7e, 0xa2, 0x98, 0x4d
611 };
612#endif
613
614 if (!amf_ctx)
615 return AMF_MEMORY_UNKNOWN;
616
617#ifdef _WIN32
618 if (AMF_IFACE_CALL(context, QueryInterface, &guid2, (void**)&context2) == AMF_OK)
619 {
620 device = AMF_IFACE_CALL(context2, GetDX12Device, AMF_DX12);
621 AMF_IFACE_CALL(context2, Release);
622
623 if (device)
624 return AMF_MEMORY_DX12;
625 }
626
627 if (AMF_IFACE_CALL(context, GetDX11Device, AMF_DX11_1))
628 return AMF_MEMORY_DX11;
629
630 if (AMF_IFACE_CALL(context, GetDX9Device, AMF_DX9))
631 return AMF_MEMORY_DX9;
632#endif
633
634 if (AMF_IFACE_CALL(context, QueryInterface, &guid1, (void**)&context1) != AMF_OK)
635 return AMF_MEMORY_UNKNOWN;
636
637 device = AMF_IFACE_CALL(context1, GetVulkanDevice);
638 AMF_IFACE_CALL(context1, Release);
639 if (device)
640 return AMF_MEMORY_VULKAN;
641
642 return AMF_MEMORY_UNKNOWN;
643}
644
646{
647 AVAMFDeviceContext *amf_ctx = ctx->hwctx;
648 AMFContext *context = amf_ctx->context;
649 AMFContext1 *context1 = NULL;
650 AMFGuid guid1 = IID_AMFContext1();
651 AMF_RESULT res;
652
653#ifdef _WIN32
654 AMFContext2 *context2 = NULL;
655 static const AMFGuid guid2 = {
656 0x726241d3, 0xbd46, 0x4e90,
657 0x99, 0x68, 0x93, 0xe0, 0x7e, 0xa2, 0x98, 0x4d
658 };
659#endif
660
661 if (!amf_ctx->lock) {
662 amf_ctx->lock_ctx = av_mallocz(sizeof(AVMutex));
663 if (!amf_ctx->lock_ctx) {
664 return AVERROR(ENOMEM);
665 }
666 ff_mutex_init((AVMutex*)amf_ctx->lock_ctx, NULL);
667 amf_ctx->lock = amf_lock_default;
668 amf_ctx->unlock = amf_unlock_default;
669 }
670
671 if (av_amf_get_memory_type(amf_ctx) != AMF_MEMORY_UNKNOWN) {
672 av_log(ctx, AV_LOG_VERBOSE, "AMF is already initialized, skipping init.\n");
673 return 0;
674 }
675
676#ifdef _WIN32
677 res = AMF_IFACE_CALL(context, InitDX11, NULL, AMF_DX11_1);
678 if (res == AMF_OK) {
679 av_log(ctx, AV_LOG_VERBOSE, "Successfully initialized AMF via D3D11.\n");
680 return 0;
681 }
682
683 res = AMF_IFACE_CALL(context, QueryInterface, &guid2, (void**)&context2);
684 if (res == AMF_OK) {
685 res = AMF_IFACE_CALL(context2, InitDX12, NULL, AMF_DX12);
686 AMF_IFACE_CALL(context2, Release);
687 if (res == AMF_OK) {
688 av_log(ctx, AV_LOG_VERBOSE, "Successfully initialized AMF via D3D12.\n");
689 return 0;
690 }
691 } else {
692 av_log(ctx, AV_LOG_VERBOSE, "QueryInterface(AMFContext2) failed with error %d, trying older D3D APIs...\n", res);
693 }
694
695 res = AMF_IFACE_CALL(context, InitDX9, NULL);
696 if (res == AMF_OK) {
697 av_log(ctx, AV_LOG_VERBOSE, "Successfully initialized AMF via D3D9.\n");
698 return 0;
699 }
700
701 av_log(ctx, AV_LOG_WARNING, "AMF failed to initialize with any of supported versions of DirectX, trying Vulkan instead...\n");
702#endif
703
704 res = AMF_IFACE_CALL(context, QueryInterface, &guid1, (void**)&context1);
705 if (res != AMF_OK) {
706 av_log(ctx, AV_LOG_ERROR, "QueryInterface(AMFContext1) failed with error %d\n", res);
707 return AVERROR_UNKNOWN;
708 }
709
710 res = AMF_IFACE_CALL(context1, InitVulkan, NULL);
711 AMF_IFACE_CALL(context1, Release);
712
713 if (res == AMF_OK) {
714 av_log(ctx, AV_LOG_VERBOSE, "Successfully initialized AMF via Vulkan.\n");
715 } else {
716 if (res == AMF_NOT_SUPPORTED)
717 av_log(ctx, AV_LOG_ERROR, "AMF via Vulkan is not supported on the given device.\n");
718 else
719 av_log(ctx, AV_LOG_ERROR, "Failed to initialize AMF via Vulkan, error %d\n", res);
720
721 return AVERROR(ENOSYS);
722 }
723
724 return 0;
725}
726
727static int amf_load_library(AVAMFDeviceContext* amf_ctx, void* avcl)
728{
729 AMFInit_Fn init_fun;
730 AMFQueryVersion_Fn version_fun;
731 AMF_RESULT res;
732
733 amf_ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
734 AMF_RETURN_IF_FALSE(avcl, amf_ctx->library != NULL,
735 AVERROR_UNKNOWN, "DLL %s failed to open\n", AMF_DLL_NAMEA);
736
737 init_fun = (AMFInit_Fn)dlsym(amf_ctx->library, AMF_INIT_FUNCTION_NAME);
738 AMF_RETURN_IF_FALSE(avcl, init_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_INIT_FUNCTION_NAME);
739
740 version_fun = (AMFQueryVersion_Fn)dlsym(amf_ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
741 AMF_RETURN_IF_FALSE(avcl, version_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_QUERY_VERSION_FUNCTION_NAME);
742
743 amf_uint64 version;
744 res = version_fun(&version);
745 AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_QUERY_VERSION_FUNCTION_NAME, res);
746 amf_ctx->version = version;
747 res = init_fun(AMF_FULL_VERSION, &amf_ctx->factory);
748 AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_INIT_FUNCTION_NAME, res);
749 return 0;
750}
751
753 const char *device,
754 AVDictionary *opts, int flags)
755{
756 AVAMFDeviceContext *ctx = device_ctx->hwctx;
757 AMFTrace *trace;
758 int ret;
759 if ((ret = amf_load_library(ctx, device_ctx)) == 0) {
760 ret = ctx->factory->pVtbl->GetTrace(ctx->factory, &trace);
761 if (ret == AMF_OK) {
762 int level_ff = av_log_get_level();
763 int level_amf = AMF_TRACE_TRACE;
764 amf_bool enable_log = true;
765 switch(level_ff)
766 {
767 case AV_LOG_QUIET:
768 level_amf = AMF_TRACE_ERROR;
769 enable_log = false;
770 break;
771 case AV_LOG_PANIC:
772 case AV_LOG_FATAL:
773 case AV_LOG_ERROR:
774 level_amf = AMF_TRACE_ERROR;
775 break;
776 case AV_LOG_WARNING:
777 case AV_LOG_INFO:
778 level_amf = AMF_TRACE_WARNING;
779 break;
780 case AV_LOG_VERBOSE:
781 level_amf = AMF_TRACE_INFO;
782 break;
783 case AV_LOG_DEBUG:
784 level_amf = AMF_TRACE_DEBUG;
785 break;
786 case AV_LOG_TRACE:
787 level_amf = AMF_TRACE_TRACE;
788 break;
789 }
790 if(ctx->version == AMF_MAKE_FULL_VERSION(1, 4, 35, 0)){// get around a bug in trace in AMF runtime driver 24.20
791 level_amf = AMF_TRACE_WARNING;
792 }
793
794 trace->pVtbl->EnableWriter(trace, AMF_TRACE_WRITER_CONSOLE, 0);
795 trace->pVtbl->SetGlobalLevel(trace, level_amf);
796
797 // connect AMF logger to av_log
798 ctx->trace_writer = amf_writer_alloc(device_ctx);
799 trace->pVtbl->RegisterWriter(trace, FFMPEG_AMF_WRITER_ID, (AMFTraceWriter*)ctx->trace_writer, 1);
800 trace->pVtbl->SetWriterLevel(trace, FFMPEG_AMF_WRITER_ID, level_amf);
801 trace->pVtbl->EnableWriter(trace, FFMPEG_AMF_WRITER_ID, enable_log);
802 trace->pVtbl->SetWriterLevel(trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, level_amf);
803 trace->pVtbl->EnableWriter(trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, enable_log);
804 }
805
806
807 ret = ctx->factory->pVtbl->CreateContext(ctx->factory, &ctx->context);
808 if (ret == AMF_OK) {
809 AMF_ASSIGN_PROPERTY_INT64(ret, ctx->context, L"DeviceSurfaceCacheSize", 50 );
810 return 0;
811 }
812 av_log(device_ctx, AV_LOG_ERROR, "CreateContext() failed with error %d.\n", ret);
813 }
814 amf_device_uninit(device_ctx);
815 return ret;
816}
817
818#if CONFIG_DXVA2
819static int amf_init_from_dxva2_device(AVAMFDeviceContext * amf_ctx, AVHWDeviceContext *child_device_ctx)
820{
821 AVDXVA2DeviceContext *hwctx = child_device_ctx->hwctx;
822 IDirect3DDevice9 *device;
823 HANDLE device_handle;
824 HRESULT hr;
825 AMF_RESULT res;
826 int ret;
827
828 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->devmgr, &device_handle);
829 if (FAILED(hr)) {
830 av_log(child_device_ctx, AV_LOG_ERROR, "Failed to open device handle for Direct3D9 device: %lx.\n", (unsigned long)hr);
831 return AVERROR_EXTERNAL;
832 }
833
834 hr = IDirect3DDeviceManager9_LockDevice(hwctx->devmgr, device_handle, &device, FALSE);
835 if (SUCCEEDED(hr)) {
836 IDirect3DDeviceManager9_UnlockDevice(hwctx->devmgr, device_handle, FALSE);
837 ret = 0;
838 } else {
839 av_log(child_device_ctx, AV_LOG_ERROR, "Failed to lock device handle for Direct3D9 device: %lx.\n", (unsigned long)hr);
841 }
842
843
844 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->devmgr, device_handle);
845
846 if (ret < 0)
847 return ret;
848
849 res = amf_ctx->context->pVtbl->InitDX9(amf_ctx->context, device);
850
851 IDirect3DDevice9_Release(device);
852
853 if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
854 if (res == AMF_NOT_SUPPORTED)
855 av_log(child_device_ctx, AV_LOG_ERROR, "AMF via D3D9 is not supported on the given device.\n");
856 else
857 av_log(child_device_ctx, AV_LOG_ERROR, "AMF failed to initialise on given D3D9 device: %d.\n", res);
858 return AVERROR(ENODEV);
859 }
860 av_log(child_device_ctx, AV_LOG_INFO, "AMF via DXVA2.\n");
861 return 0;
862}
863#endif
864
865#if CONFIG_D3D11VA
866static int amf_init_from_d3d11_device(AVAMFDeviceContext* amf_ctx, AVHWDeviceContext *child_device_ctx)
867{
868 AMF_RESULT res;
869 AVD3D11VADeviceContext *hwctx = child_device_ctx->hwctx;
870 res = amf_ctx->context->pVtbl->InitDX11(amf_ctx->context, hwctx->device, AMF_DX11_1);
871 if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
872 if (res == AMF_NOT_SUPPORTED)
873 av_log(child_device_ctx, AV_LOG_ERROR, "AMF via D3D11 is not supported on the given device.\n");
874 else
875 av_log(child_device_ctx, AV_LOG_ERROR, "AMF failed to initialise on the given D3D11 device: %d.\n", res);
876 return AVERROR(ENODEV);
877 }
878 av_log(child_device_ctx, AV_LOG_INFO, "AMF via D3D11.\n");
879 hwctx->BindFlags |= D3D11_BIND_SHADER_RESOURCE;
880 return 0;
881}
882#endif
883
884#if CONFIG_D3D12VA
885static int amf_init_from_d3d12_device(AVAMFDeviceContext* amf_ctx, AVHWDeviceContext *child_device_ctx)
886{
887 AVD3D12VADeviceContext *hwctx = child_device_ctx->hwctx;
888 AMF_RESULT res;
889 AMFContext2 *context2 = NULL;
890 AMFGuid guid = IID_AMFContext2();
891 res = amf_ctx->context->pVtbl->QueryInterface(amf_ctx->context, &guid, (void**)&context2);
892 AMF_RETURN_IF_FALSE(child_device_ctx, res == AMF_OK, AVERROR_UNKNOWN, "CreateContext2() failed with error %d\n", res);
893 res = context2->pVtbl->InitDX12(context2, hwctx->device, AMF_DX12);
894 context2->pVtbl->Release(context2);
895 if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
896 if (res == AMF_NOT_SUPPORTED)
897 av_log(child_device_ctx, AV_LOG_ERROR, "AMF via D3D12 is not supported on the given device.\n");
898 else
899 av_log(child_device_ctx, AV_LOG_ERROR, "AMF failed to initialise on the given D3D12 device: %d.\n", res);
900 return AVERROR(ENODEV);
901 }
902 av_log(child_device_ctx, AV_LOG_INFO, "AMF via D3D12.\n");
903 return 0;
904}
905#endif
906
907#if CONFIG_VULKAN
908static int amf_vk_ext_enabled(const char * const *exts, int nb_exts, const char *name)
909{
910 for (int i = 0; i < nb_exts; i++)
911 if (!strcmp(exts[i], name))
912 return 1;
913 return 0;
914}
915
916// AMF loads vkGetPhysicalDeviceProperties2KHR and the VK_KHR_swapchain entry points (swapchain depends on VK_KHR_surface).
917static void amf_vulkan_log_missing_exts(AVHWDeviceContext *child_device_ctx, const AVVulkanDeviceContext *hwctx)
918{
919 static const struct {
920 const char *name;
921 int dev;
922 } required_exts[] = {
923 { VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 0 },
924 { VK_KHR_SURFACE_EXTENSION_NAME, 0 },
925 { VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1 },
926 };
927 int missing = 0;
928
929 for (int i = 0; i < FF_ARRAY_ELEMS(required_exts); i++) {
930 int enabled = required_exts[i].dev ?
931 amf_vk_ext_enabled(hwctx->enabled_dev_extensions, hwctx->nb_enabled_dev_extensions, required_exts[i].name) :
932 amf_vk_ext_enabled(hwctx->enabled_inst_extensions, hwctx->nb_enabled_inst_extensions, required_exts[i].name);
933 if (!enabled) {
934 av_log(child_device_ctx, AV_LOG_ERROR, "Vulkan %s extension %s is not enabled.\n",
935 required_exts[i].dev ? "device" : "instance", required_exts[i].name);
936 missing = 1;
937 }
938 }
939
940 if (missing)
941 av_log(child_device_ctx, AV_LOG_ERROR, "AMF requires them, create the Vulkan device with instance_extensions="
942 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME "+" VK_KHR_SURFACE_EXTENSION_NAME
943 ",device_extensions=" VK_KHR_SWAPCHAIN_EXTENSION_NAME ".\n");
944}
945
946#if FF_API_VULKAN_SYNC_QUEUES
947// qf may list a family more than once and in any order. Queues are walked once each, in ascending family and
948// queue index order, so that queue locks shared with other users are always taken in the same order.
949static int amf_vk_nb_families(const AVVulkanDeviceContext *hwctx)
950{
951 int nb = 0;
952 for (int i = 0; i < hwctx->nb_qf; i++)
953 nb = FFMAX(nb, hwctx->qf[i].idx + 1);
954 return nb;
955}
956
957static int amf_vk_family_queues(const AVVulkanDeviceContext *hwctx, int family)
958{
959 int num = 0;
960 for (int i = 0; i < hwctx->nb_qf; i++)
961 if (hwctx->qf[i].idx == family)
962 num = FFMAX(num, hwctx->qf[i].num);
963 return num;
964}
965
966// AMF submits to the queues of the Vulkan device it was derived from, so its lock takes all of them.
967// The queue callbacks must allow holding several queue locks at once and need not be recursive, so no FFmpeg
968// Vulkan work may be submitted on this thread while they are held.
969static void amf_lock_vulkan(void *opaque)
970{
971 AVHWDeviceContext *vk_ctx = opaque;
972 AVVulkanDeviceContext *hwctx = vk_ctx->hwctx;
973 int nb_families = amf_vk_nb_families(hwctx);
974
976 for (int i = 0; i < nb_families; i++) {
977 int nb_queues = amf_vk_family_queues(hwctx, i);
978 for (int j = 0; j < nb_queues; j++)
979 hwctx->lock_queue(vk_ctx, i, j);
980 }
982}
983
984static void amf_unlock_vulkan(void *opaque)
985{
986 AVHWDeviceContext *vk_ctx = opaque;
987 AVVulkanDeviceContext *hwctx = vk_ctx->hwctx;
988 int nb_families = amf_vk_nb_families(hwctx);
989
991 for (int i = 0; i < nb_families; i++) {
992 int nb_queues = amf_vk_family_queues(hwctx, i);
993 for (int j = 0; j < nb_queues; j++)
994 hwctx->unlock_queue(vk_ctx, i, j);
995 }
997}
998#endif
999
1000static int amf_init_from_vulkan_device(AMFDevicePriv *priv, AVHWDeviceContext *child_device_ctx)
1001{
1002 AVVulkanDeviceContext *hwctx = child_device_ctx->hwctx;
1003 AMFVulkanDevice *vk_dev = &priv->vk_dev;
1004 AMFContext1 *context1 = NULL;
1005 AMFGuid guid = IID_AMFContext1();
1006 AMF_RESULT res;
1007
1008 res = AMF_IFACE_CALL(priv->p.context, QueryInterface, &guid, (void**)&context1);
1009 AMF_RETURN_IF_FALSE(child_device_ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface(AMFContext1) failed with error %d\n", res);
1010
1011 vk_dev->cbSizeof = sizeof(*vk_dev);
1012 vk_dev->pNext = NULL;
1013 vk_dev->hInstance = hwctx->inst;
1014 vk_dev->hPhysicalDevice = hwctx->phys_dev;
1015 vk_dev->hDevice = hwctx->act_dev;
1016
1017 AMF_ASSIGN_PROPERTY_BOOL(res, context1, AMF_CONTEXT_VULKAN_USE_TIMELINE_SEMAPHORES, 1);
1018 if (res != AMF_OK)
1019 av_log(child_device_ctx, AV_LOG_WARNING, "Failed to enable Vulkan timeline semaphores on the AMF context: %d.\n", res);
1020
1021#if FF_API_VULKAN_SYNC_QUEUES
1022 priv->p.lock = amf_lock_vulkan;
1023 priv->p.unlock = amf_unlock_vulkan;
1024 priv->p.lock_ctx = child_device_ctx;
1025#endif
1026 if (priv->p.lock)
1027 priv->p.lock(priv->p.lock_ctx);
1028 res = AMF_IFACE_CALL(context1, InitVulkan, vk_dev);
1029 if (priv->p.unlock)
1030 priv->p.unlock(priv->p.lock_ctx);
1031 AMF_IFACE_CALL(context1, Release);
1032 if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
1033 if (res == AMF_NOT_SUPPORTED)
1034 av_log(child_device_ctx, AV_LOG_ERROR, "AMF via Vulkan is not supported on the given device.\n");
1035 else
1036 av_log(child_device_ctx, AV_LOG_ERROR, "AMF failed to initialise on the given Vulkan device: %d.\n", res);
1037
1038 amf_vulkan_log_missing_exts(child_device_ctx, hwctx);
1039 if (hwctx->queue_flags)
1040 av_log(child_device_ctx, AV_LOG_ERROR, "The Vulkan queues were created with flags 0x%x (e.g. internally "
1041 "synchronized queues), which some AMF runtimes cannot use.\n", hwctx->queue_flags);
1042
1043 return AVERROR(ENODEV);
1044 }
1045 av_log(child_device_ctx, AV_LOG_INFO, "AMF via Vulkan.\n");
1046 return 0;
1047}
1048#endif
1049
1051 AVHWDeviceContext *child_device_ctx, AVDictionary *opts,
1052 int flags)
1053{
1054#if CONFIG_DXVA2 || CONFIG_D3D11VA || CONFIG_D3D12VA
1055 AVAMFDeviceContext *amf_ctx = device_ctx->hwctx;
1056#endif
1057 int ret;
1058
1059 ret = amf_device_create(device_ctx, "", opts, flags);
1060 if(ret < 0)
1061 return ret;
1062
1063 switch (child_device_ctx->type) {
1064#if CONFIG_DXVA2
1066 return amf_init_from_dxva2_device(amf_ctx, child_device_ctx);
1067#endif
1068#if CONFIG_D3D11VA
1070 return amf_init_from_d3d11_device(amf_ctx, child_device_ctx);
1071#endif
1072#if CONFIG_D3D12VA
1074 return amf_init_from_d3d12_device(amf_ctx, child_device_ctx);
1075#endif
1076#if CONFIG_VULKAN
1078 return amf_init_from_vulkan_device(device_ctx->hwctx, child_device_ctx);
1079#endif
1080 default:
1081 av_log(child_device_ctx, AV_LOG_ERROR, "AMF initialisation from a %s device is not supported.\n",
1082 av_hwdevice_get_type_name(child_device_ctx->type));
1083 return AVERROR(ENOSYS);
1084 }
1085}
1086
1088{
1089 AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
1090 AMFSurface1 *surface1 = hwmap->priv;
1091 amf_ctx->lock(amf_ctx->lock_ctx);
1092 AMF_IFACE_CALL(surface1, Unmap);
1093 amf_ctx->unlock(amf_ctx->lock_ctx);
1094 AMF_IFACE_CALL(surface1, Release);
1095}
1096
1097#if CONFIG_D3D11VA
1098static int amf_same_d3d11_device(ID3D11Device *a, ID3D11Device *b)
1099{
1100 static const GUID iid_unknown = { 0x00000000, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
1101 IUnknown *unk_a, *unk_b;
1102 int same;
1103
1104 if (a == b)
1105 return 1;
1106 if (!a || !b)
1107 return 0;
1108 if (FAILED(a->lpVtbl->QueryInterface(a, &iid_unknown, (void **)&unk_a)))
1109 return 0;
1110 if (FAILED(b->lpVtbl->QueryInterface(b, &iid_unknown, (void **)&unk_b))) {
1111 unk_a->lpVtbl->Release(unk_a);
1112 return 0;
1113 }
1114 same = unk_a == unk_b;
1115 unk_a->lpVtbl->Release(unk_a);
1116 unk_b->lpVtbl->Release(unk_b);
1117 return same;
1118}
1119
1120static int amf_map_to_d3d11(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
1121{
1122 AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
1123 AVHWFramesContext *dst_fc;
1124 AVD3D11VADeviceContext *d3d11_ctx;
1125 AMFSurface *surface = (AMFSurface *)src->data[0];
1126 D3D11_TEXTURE2D_DESC desc;
1127 ID3D11Texture2D *texture;
1128 AMFPlane *plane;
1129 int ret;
1130
1131 AMF_RETURN_IF_FALSE(ctx, surface, AVERROR(EINVAL), "Source surface is null\n");
1132 if (!dst->hw_frames_ctx)
1133 return AVERROR(EINVAL);
1134 dst_fc = (AVHWFramesContext *)dst->hw_frames_ctx->data;
1135 if (dst_fc->device_ctx->type != AV_HWDEVICE_TYPE_D3D11VA)
1136 return AVERROR(ENOSYS);
1137 d3d11_ctx = dst_fc->device_ctx->hwctx;
1138 if (dst_fc->sw_format != ctx->sw_format ||
1139 dst_fc->width != ctx->width || dst_fc->height != ctx->height)
1140 return AVERROR(ENOSYS);
1141 if (!amf_same_d3d11_device(AMF_IFACE_CALL(amf_ctx->context, GetDX11Device, AMF_DX11_0),
1142 d3d11_ctx->device)) {
1143 av_log(ctx, AV_LOG_DEBUG, "The AMF context does not use the D3D11 device of the target frames.\n");
1144 return AVERROR(ENOSYS);
1145 }
1146 if (AMF_IFACE_CALL(surface, GetMemoryType) != AMF_MEMORY_DX11)
1147 return AVERROR(ENOSYS);
1148
1149 plane = AMF_IFACE_CALL(surface, GetPlaneAt, 0);
1150 AMF_RETURN_IF_FALSE(ctx, plane, AVERROR(EINVAL), "Surface has no plane\n");
1151 texture = plane->pVtbl->GetNative(plane);
1152 AMF_RETURN_IF_FALSE(ctx, texture, AVERROR(EINVAL), "Surface has no D3D11 texture\n");
1153 for (int i = 1; i < AMF_IFACE_CALL(surface, GetPlanesCount); i++) {
1154 plane = AMF_IFACE_CALL(surface, GetPlaneAt, i);
1155 if (!plane || plane->pVtbl->GetNative(plane) != texture)
1156 return AVERROR(ENOSYS);
1157 }
1158 texture->lpVtbl->GetDesc(texture, &desc);
1159 if (desc.ArraySize > 1 ||
1160 desc.Width != dst_fc->width || desc.Height != dst_fc->height)
1161 return AVERROR(ENOSYS);
1162
1163 ret = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src, NULL, NULL);
1164 if (ret < 0)
1165 return ret;
1166
1167 dst->data[0] = (uint8_t *)texture;
1168 dst->data[1] = 0;
1169 dst->width = src->width;
1170 dst->height = src->height;
1171 return 0;
1172}
1173#endif
1174
1175static int amf_map_frame(AVHWFramesContext *device_ctx, AVFrame *dst, const AVFrame *src, int flags)
1176{
1177 AVAMFDeviceContext *amf_ctx = device_ctx->device_ctx->hwctx;
1178 AMFSurface *surface = (AMFSurface *)src->data[0];
1179 AMFSurface1 *surface1 = NULL;
1180 size_t *linesizes = NULL;
1181 uint8_t **map = NULL;
1182 int amf_mem_flags = AMF_MEMORY_CPU_NONE;
1183 const AMFGuid guid = IID_AMFSurface1();
1184 AMF_RESULT res = AMF_FAIL;
1185 size_t plane_count = 0;
1186 int ret = -1;
1187
1188#if CONFIG_D3D11VA
1189 if (dst->format == AV_PIX_FMT_D3D11)
1190 return amf_map_to_d3d11(device_ctx, dst, src);
1191#endif
1192 if (dst->format != AV_PIX_FMT_NONE &&
1194 return AVERROR(ENOSYS);
1195
1196 AMF_RETURN_IF_FALSE(device_ctx, surface != NULL, AVERROR(EINVAL) , "Source surface is null");
1197
1198 res = AMF_IFACE_CALL(surface, QueryInterface, &guid, (void**)&surface1);
1199 AMF_RETURN_IF_FALSE(device_ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface(AMFSurface1) failed with error %d\n", res);
1200
1202 amf_mem_flags |= AMF_MEMORY_CPU_READ;
1203
1205 amf_mem_flags |= AMF_MEMORY_CPU_WRITE;
1206
1207 plane_count = AMF_IFACE_CALL(surface, GetPlanesCount);
1208 linesizes = av_malloc_array(sizeof(size_t), plane_count);
1209 AMF_GOTO_FAIL_IF_FALSE(device_ctx, linesizes, AVERROR(ENOMEM), "Unable to allocate line sizes array\n");
1210
1211 map = av_malloc_array(sizeof(uint8_t *), plane_count);
1212 AMF_GOTO_FAIL_IF_FALSE(device_ctx, map, AVERROR(ENOMEM), "Unable to allocate mapping buffer\n");
1213
1214 dst->format = av_amf_to_av_format(AMF_IFACE_CALL(surface, GetFormat));
1215
1216 amf_ctx->lock(amf_ctx->lock_ctx);
1217 res = AMF_IFACE_CALL(surface1, Map, amf_mem_flags, plane_count, linesizes, (void**)map);
1218 amf_ctx->unlock(amf_ctx->lock_ctx);
1219 AMF_GOTO_FAIL_IF_FALSE(device_ctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFSurface1->Map() failed with error %d\n", res);
1220
1221 ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, &amf_unmap_frame, surface1);
1222 AMF_GOTO_FAIL_IF_FALSE(device_ctx, ret >= 0, ret, "ff_hwframe_map_create failed with error %d\n", ret);
1223
1224 dst->width = src->width;
1225 dst->height = src->height;
1227
1228 // The dst frame is no longer a hardware frame, from the perspective of any dst frame consumer.
1229 av_buffer_unref(&dst->hw_frames_ctx);
1230
1231 for (int plane = 0; plane < plane_count; ++plane)
1232 {
1233 dst->data[plane] = map[plane];
1234 dst->linesize[plane] = linesizes[plane];
1235 }
1236
1237fail:
1238 if (map)
1239 av_free(map);
1240
1241 if (linesizes)
1242 av_free(linesizes);
1243
1244 if (ret < 0 && surface1 != NULL)
1245 AMF_IFACE_CALL(surface1, Release);
1246
1247 return ret;
1248}
1249
1251 .type = AV_HWDEVICE_TYPE_AMF,
1252 .name = "AMF",
1253
1254 .device_hwctx_size = sizeof(AMFDevicePriv),
1255 .frames_hwctx_size = sizeof(AMFFramesContext),
1256
1257 .device_create = amf_device_create,
1258 .device_derive = amf_device_derive,
1259 .device_init = amf_device_init,
1260 .device_uninit = amf_device_uninit,
1261 .frames_get_constraints = amf_frames_get_constraints,
1262 .frames_init = amf_frames_init,
1263 .frames_get_buffer = amf_get_buffer,
1264 .transfer_get_formats = amf_transfer_get_formats,
1265 .transfer_data_to = amf_transfer_data_to,
1266 .transfer_data_from = amf_transfer_data_from,
1267 .map_from = amf_map_frame,
1268
1269 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_AMF_SURFACE, AV_PIX_FMT_NONE },
1270};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static const char *const format[]
Definition af_aiir.c:445
static void amf_free_amfsurface(void *opaque, uint8_t *data)
Definition amfdec.c:58
#define FFMPEG_AMF_WRITER_ID
Definition amfenc.c:54
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition amfenc.h:195
static AVFormatContext * ctx
static AVDictionary * opts
#define L(x)
Definition vpx_arith.h:36
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
refcounted data buffer API
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
static AVFrame * frame
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition buffer.h:114
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:56
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition buffer.c:396
AVBufferPool * av_buffer_pool_init2(size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition buffer.c:260
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:662
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition frame.h:137
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
Definition frame.h:120
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_PANIC
Something went really wrong and we will crash now.
Definition log.h:197
#define AV_LOG_QUIET
Print no output.
Definition log.h:192
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition log.h:204
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_log_get_level(void)
Get the current log level.
Definition log.c:477
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition imgutils.h:184
int a
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
Definition hwcontext.c:121
int ff_hwframe_map_create(AVBufferRef *hwframe_ref, AVFrame *dst, const AVFrame *src, void(*unmap)(AVHWFramesContext *ctx, HWMapDescriptor *hwmap), void *priv)
Definition hwcontext.c:799
@ AV_HWFRAME_MAP_READ
The mapping must be readable.
Definition hwcontext.h:530
@ AV_HWFRAME_MAP_WRITE
The mapping must be writeable.
Definition hwcontext.h:534
AVHWFrameTransferDirection
Definition hwcontext.h:421
@ AV_HWDEVICE_TYPE_D3D11VA
Definition hwcontext.h:35
@ AV_HWDEVICE_TYPE_AMF
Definition hwcontext.h:41
@ AV_HWDEVICE_TYPE_VULKAN
Definition hwcontext.h:39
@ AV_HWDEVICE_TYPE_D3D12VA
Definition hwcontext.h:40
@ AV_HWDEVICE_TYPE_DXVA2
Definition hwcontext.h:32
int av_amf_display_mastering_meta_to_hdrmeta(const AVMasteringDisplayMetadata *display_meta, AMFHDRMetadata *hdrmeta)
static int amf_device_create(AVHWDeviceContext *device_ctx, const char *device, AVDictionary *opts, int flags)
static int amf_device_init(AVHWDeviceContext *ctx)
static void amf_unlock_default(void *opaque)
static int amf_load_library(AVAMFDeviceContext *amf_ctx, void *avcl)
enum AMF_MEMORY_TYPE av_amf_get_memory_type(AVAMFDeviceContext *amf_ctx)
static int amf_map_frame(AVHWFramesContext *device_ctx, AVFrame *dst, const AVFrame *src, int flags)
int av_amf_attach_hdr_metadata(AVFrame *frame, const AMFHDRMetadata *hdrmeta)
static void AMF_CDECL_CALL AMFTraceWriter_Write(AMFTraceWriter *pThis, const wchar_t *scope, const wchar_t *message)
static int amf_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
static int amf_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
const FormatMap format_map[]
const HWContextType ff_hwcontext_type_amf
static void amf_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
int av_amf_light_metadata_to_hdrmeta(const AVContentLightMetadata *light_meta, AMFHDRMetadata *hdrmeta)
static void amf_device_uninit(AVHWDeviceContext *device_ctx)
static int amf_frames_init(AVHWFramesContext *ctx)
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
static void amf_writer_free(void *opaque)
static enum AVPixelFormat supported_transfer_formats[]
static AmfTraceWriter * amf_writer_alloc(void *avctx)
static int amf_device_derive(AVHWDeviceContext *device_ctx, AVHWDeviceContext *child_device_ctx, AVDictionary *opts, int flags)
static int amf_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
static void amf_free_amfsurface(void *opaque, uint8_t *data)
int av_amf_extract_hdr_metadata(const AVFrame *frame, AMFHDRMetadata *hdrmeta)
enum AVPixelFormat av_amf_to_av_format(enum AMF_SURFACE_FORMAT fmt)
static void amf_lock_default(void *opaque)
static int amf_transfer_get_formats(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
static void amf_dummy_free(void *opaque, uint8_t *data)
enum AMF_VIDEO_CONVERTER_COLOR_PROFILE_ENUM av_amf_get_color_profile(enum AVColorRange color_range, enum AVColorSpace color_space)
static int amf_frames_get_constraints(AVHWDeviceContext *ctx, const void *hwconfig, AVHWFramesConstraints *constraints)
static AVBufferRef * amf_pool_alloc(void *opaque, size_t size)
static void AMF_CDECL_CALL AMFTraceWriter_Flush(AMFTraceWriter *pThis)
#define AMF_GOTO_FAIL_IF_FALSE(avctx, exp, ret_value,...)
#define AMF_IFACE_CALL(this, function,...)
An API-specific header for AV_HWDEVICE_TYPE_D3D11VA.
An API-specific header for AV_HWDEVICE_TYPE_D3D12VA.
An API-specific header for AV_HWDEVICE_TYPE_DXVA2.
static FFHWFramesContext * ffhwframesctx(AVHWFramesContext *ctx)
const VDPAUPixFmtMap * map
API-specific header for AV_HWDEVICE_TYPE_VULKAN.
misc image utilities
#define b
Definition input.c:43
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition internal.h:71
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition internal.h:72
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
static int ff_mutex_destroy(AVMutex *mutex)
Definition thread.h:190
#define AVMutex
Definition thread.h:184
static int ff_mutex_init(AVMutex *mutex, const void *attr)
Definition thread.h:187
version
Definition libkvazaar.c:313
const char * desc
Definition libsvtav1.c:110
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
AVContentLightMetadata * av_content_light_metadata_create_side_data(AVFrame *frame)
Allocate a complete AVContentLightMetadata and add it to the frame.
AVMasteringDisplayMetadata * av_mastering_display_metadata_create_side_data(AVFrame *frame)
Allocate a complete AVMasteringDisplayMetadata and add it to the frame.
Memory handling functions.
const char data[16]
Definition mxf.c:155
void ID3D11Device
Definition nvenc.h:28
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
pixel format definitions
#define AV_PIX_FMT_RGBAF16
Definition pixfmt.h:635
AVColorRange
Visual content value range.
Definition pixfmt.h:757
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_P010
Definition pixfmt.h:613
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition pixfmt.h:440
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition pixfmt.h:134
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition pixfmt.h:263
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition pixfmt.h:74
@ AV_PIX_FMT_AMF_SURFACE
HW acceleration through AMF.
Definition pixfmt.h:477
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:625
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:713
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition pixfmt.h:723
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:722
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:718
const char * name
Definition qsvenc.c:142
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
AVAMFDeviceContext p
We still need AVHWFramesContext to utilize our hardware memory otherwise, we will receive the error "...
This struct is allocated as AVHWDeviceContext.hwctx.
void(* lock)(void *lock_ctx)
AMFContext * context
void(* unlock)(void *lock_ctx)
AMFFactory * factory
int64_t version
version of AMF runtime
A reference to a data buffer.
Definition buffer.h:82
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
unsigned MaxFALL
Max average light level per frame (cd/m^2).
unsigned MaxCLL
Max content light level (cd/m^2).
This struct is allocated as AVHWDeviceContext.hwctx.
UINT BindFlags
D3D11_TEXTURE2D_DESC.BindFlags to be applied to D3D11 resources allocated for frames using this devic...
ID3D11Device * device
Device used for texture creation and access.
This struct is allocated as AVHWDeviceContext.hwctx.
ID3D12Device * device
Device used for objects creation and access.
This struct is allocated as AVHWDeviceContext.hwctx.
IDirect3DDeviceManager9 * devmgr
Structure to hold side data for an AVFrame.
Definition frame.h:334
uint8_t * data
Definition frame.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition hwcontext.h:75
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition hwcontext.h:459
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:464
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:471
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition hwcontext.h:137
Mastering display metadata capable of representing the color volume of the display used to master the...
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
VkPhysicalDevice phys_dev
Physical device.
AVVulkanDeviceQueueFamily qf[64]
Queue families used.
const char *const * enabled_dev_extensions
Enabled device extensions.
VkDevice act_dev
Active device.
VkInstance inst
Vulkan instance.
VkDeviceQueueCreateFlags queue_flags
AMFTraceWriterVtbl vtbl
AMFTraceWriterVtbl * vtblp
AVBufferPool * pool_internal
enum AMF_SURFACE_FORMAT amf_format
enum AVPixelFormat av_format
void * priv
Hardware-specific private data associated with the mapping.
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
int size
static enum AVPixelFormat supported_formats[]
color_range