20 #include "config_components.h"
52 const unsigned int luma_den = 10000;
53 hdrmeta->maxMasteringLuminance =
55 hdrmeta->minMasteringLuminance =
59 const unsigned int chroma_den = 50000;
60 hdrmeta->redPrimary[0] =
62 hdrmeta->redPrimary[1] =
64 hdrmeta->greenPrimary[0] =
66 hdrmeta->greenPrimary[1] =
68 hdrmeta->bluePrimary[0] =
70 hdrmeta->bluePrimary[1] =
72 hdrmeta->whitePoint[0] =
74 hdrmeta->whitePoint[1] =
82 hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->
MaxCLL;
83 hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->
MaxFALL;
101 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
103 #define PTS_PROP L"PtsProp"
143 return AMF_SURFACE_UNKNOWN;
147 const wchar_t *scope,
const wchar_t *
message)
167 AMFQueryVersion_Fn version_fun;
171 if (!
ctx->delayed_frame) {
177 if (!
ctx->timestamp_list) {
183 ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
187 init_fun = (AMFInit_Fn)dlsym(
ctx->library, AMF_INIT_FUNCTION_NAME);
190 version_fun = (AMFQueryVersion_Fn)dlsym(
ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
193 res = version_fun(&
ctx->version);
195 res = init_fun(AMF_FULL_VERSION, &
ctx->factory);
197 res =
ctx->factory->pVtbl->GetTrace(
ctx->factory, &
ctx->trace);
210 res =
ctx->context->pVtbl->InitDX11(
ctx->context, hwctx->
device, AMF_DX11_1);
212 if (res == AMF_NOT_SUPPORTED)
215 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given D3D11 device: %d.\n", res);
227 HANDLE device_handle;
228 IDirect3DDevice9 *device;
233 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->
devmgr, &device_handle);
235 av_log(avctx,
AV_LOG_ERROR,
"Failed to open device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
239 hr = IDirect3DDeviceManager9_LockDevice(hwctx->
devmgr, device_handle, &device, FALSE);
241 IDirect3DDeviceManager9_UnlockDevice(hwctx->
devmgr, device_handle, FALSE);
244 av_log(avctx,
AV_LOG_ERROR,
"Failed to lock device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
248 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->
devmgr, device_handle);
253 res =
ctx->context->pVtbl->InitDX9(
ctx->context, device);
255 IDirect3DDevice9_Release(device);
258 if (res == AMF_NOT_SUPPORTED)
261 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on given D3D9 device: %d.\n", res);
272 AMFContext1 *context1 =
NULL;
276 ctx->hwsurfaces_in_queue = 0;
277 ctx->hwsurfaces_in_queue_max = 16;
281 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
283 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
284 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
285 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
289 ctx->tracer.avctx = avctx;
293 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
301 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
322 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
328 if (!
ctx->hw_frames_ctx)
337 switch (device_ctx->
type) {
340 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
347 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
353 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
359 if (!
ctx->hw_device_ctx)
363 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
367 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
371 AMFGuid guid = IID_AMFContext1();
372 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
375 res = context1->pVtbl->InitVulkan(context1,
NULL);
376 context1->pVtbl->Release(context1);
378 if (res == AMF_NOT_SUPPORTED)
379 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
381 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
413 if (
ctx->hw_frames_ctx)
426 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
436 if (
ctx->delayed_surface) {
437 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
442 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
443 ctx->encoder->pVtbl->Release(
ctx->encoder);
448 ctx->context->pVtbl->Terminate(
ctx->context);
449 ctx->context->pVtbl->Release(
ctx->context);
459 dlclose(
ctx->library);
466 ctx->delayed_drain = 0;
477 uint8_t *dst_data[4];
482 planes = surface->pVtbl->GetPlanesCount(surface);
486 plane = surface->pVtbl->GetPlaneAt(surface,
i);
487 dst_data[
i] = plane->pVtbl->GetNative(plane);
488 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
501 AMFVariantStruct var = {0};
512 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
513 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
518 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
519 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
524 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
525 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
534 pkt->
pts = var.int64Value;
541 if ((
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false)) &&
ctx->dts_delay == 0) {
546 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
551 ctx->dts_delay = timestamp_last - timestamp;
576 AMFVariantStruct var;
577 res = AMFVariantInit(&var);
579 AMFGuid guid_AMFInterface = IID_AMFInterface();
580 AMFInterface *amf_interface;
581 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
584 res = AMFVariantAssignInterface(&var, amf_interface);
585 amf_interface->pVtbl->Release(amf_interface);
588 res =
object->pVtbl->SetProperty(
object,
name, var);
590 AMFVariantClear(&var);
598 AMFVariantStruct var;
599 res = AMFVariantInit(&var);
601 res =
object->pVtbl->GetProperty(
object,
name, &var);
603 if (var.type == AMF_VARIANT_INTERFACE) {
604 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
605 AMFInterface *amf_interface = AMFVariantInterface(&var);
606 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
608 res = AMF_INVALID_DATA_TYPE;
611 AMFVariantClear(&var);
619 AMFBuffer *frame_ref_storage_buffer =
NULL;
626 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &
frame_ref,
sizeof(
frame_ref));
628 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
629 frame_ref_storage_buffer =
NULL;
632 return frame_ref_storage_buffer;
638 memcpy(&
frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer),
sizeof(
frame_ref));
640 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
649 AMF_RESULT res_query;
653 int query_output_data_flag = 0;
654 AMF_RESULT res_resubmit;
659 if (!
frame->buf[0]) {
665 if (!
frame->buf[0]) {
667 if (
ctx->delayed_surface !=
NULL) {
668 ctx->delayed_drain = 1;
669 }
else if(!
ctx->delayed_drain) {
670 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
671 if (res == AMF_INPUT_FULL) {
672 ctx->delayed_drain = 1;
681 }
else if (!
ctx->delayed_surface) {
685 switch (
frame->format) {
689 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
690 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
694 frame->hw_frames_ctx->data ==
ctx->hw_frames_ctx->data);
696 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
698 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
708 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
710 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
719 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
727 AMFBuffer *frame_ref_storage_buffer;
730 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
737 ctx->hwsurfaces_in_queue++;
738 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
743 AMFBuffer * hdrmeta_buffer =
NULL;
744 res =
ctx->context->pVtbl->AllocBuffer(
ctx->context, AMF_MEMORY_HOST,
sizeof(AMFHDRMetadata), &hdrmeta_buffer);
746 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
750 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
752 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
754 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
759 hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
763 surface->pVtbl->SetPts(surface,
frame->pts);
764 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
768 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
769 switch (
frame->pict_type) {
771 if (
ctx->forced_idr) {
772 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
773 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
774 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
776 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
780 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
783 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
788 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
789 switch (
frame->pict_type) {
791 if (
ctx->forced_idr) {
792 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
793 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
795 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
799 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
805 if (
ctx->forced_idr) {
806 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
807 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
809 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
818 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
819 if (res == AMF_INPUT_FULL) {
821 ctx->delayed_surface = surface;
824 surface->pVtbl->Release(surface);
838 if (!avpkt->
data && !avpkt->
buf) {
839 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
843 AMFGuid guid = IID_AMFBuffer();
844 query_output_data_flag = 1;
850 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
851 AMFBuffer* frame_ref_storage_buffer;
855 ctx->hwsurfaces_in_queue--;
863 res_resubmit = AMF_OK;
864 if (
ctx->delayed_surface !=
NULL) {
865 if (
ctx->delayed_surface->pVtbl->HasProperty(
ctx->delayed_surface,
L"av_frame_hdrmeta")) {
866 AMFBuffer * hdrmeta_buffer =
NULL;
871 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
873 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
875 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
877 hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
879 res_resubmit =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
880 if (res_resubmit != AMF_INPUT_FULL) {
882 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
891 }
else if (
ctx->delayed_drain) {
892 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
893 if (res != AMF_INPUT_FULL) {
894 ctx->delayed_drain = 0;
898 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
902 if (query_output_data_flag == 0) {
903 if (res_resubmit == AMF_INPUT_FULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
908 }
while (block_and_wait);
910 if (res_query == AMF_EOF) {
922 amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
927 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
930 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
934 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
941 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
944 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
948 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
952 return color_profile;