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;
280 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
282 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
283 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
284 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
288 ctx->tracer.avctx = avctx;
292 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
300 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
321 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
327 if (!
ctx->hw_frames_ctx)
336 switch (device_ctx->
type) {
339 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
346 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
352 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
358 if (!
ctx->hw_device_ctx)
362 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
366 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
370 AMFGuid guid = IID_AMFContext1();
371 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
374 res = context1->pVtbl->InitVulkan(context1,
NULL);
375 context1->pVtbl->Release(context1);
377 if (res == AMF_NOT_SUPPORTED)
378 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
380 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
412 if (
ctx->hw_frames_ctx)
425 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
428 ctx->submitted_frame = 0;
437 if (
ctx->delayed_surface) {
438 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
443 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
444 ctx->encoder->pVtbl->Release(
ctx->encoder);
449 ctx->context->pVtbl->Terminate(
ctx->context);
450 ctx->context->pVtbl->Release(
ctx->context);
460 dlclose(
ctx->library);
467 ctx->delayed_drain = 0;
478 uint8_t *dst_data[4];
483 planes = surface->pVtbl->GetPlanesCount(surface);
487 plane = surface->pVtbl->GetPlaneAt(surface,
i);
488 dst_data[
i] = plane->pVtbl->GetNative(plane);
489 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
502 AMFVariantStruct var = {0};
513 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
514 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
519 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
520 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
525 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
526 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
535 pkt->
pts = var.int64Value;
542 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);
830 if (
ctx->submitted_frame == 0)
832 ctx->use_b_frame = (
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false));
834 ctx->submitted_frame++;
845 if (!avpkt->
data && !avpkt->
buf && (
ctx->use_b_frame ? (
ctx->submitted_frame >= 2) :
true) ) {
846 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
850 AMFGuid guid = IID_AMFBuffer();
851 query_output_data_flag = 1;
855 ctx->submitted_frame++;
858 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
859 AMFBuffer* frame_ref_storage_buffer;
863 ctx->hwsurfaces_in_queue--;
871 res_resubmit = AMF_OK;
872 if (
ctx->delayed_surface !=
NULL) {
873 if (
ctx->delayed_surface->pVtbl->HasProperty(
ctx->delayed_surface,
L"av_frame_hdrmeta")) {
874 AMFBuffer * hdrmeta_buffer =
NULL;
879 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
881 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
883 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
885 hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
887 res_resubmit =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
888 if (res_resubmit != AMF_INPUT_FULL) {
890 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
895 ctx->submitted_frame++;
900 }
else if (
ctx->delayed_drain) {
901 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
902 if (res != AMF_INPUT_FULL) {
903 ctx->delayed_drain = 0;
907 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
911 if (query_output_data_flag == 0) {
912 if (res_resubmit == AMF_INPUT_FULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
917 if (!
ctx->query_timeout_supported || avpkt->
data || avpkt->
buf) {
922 }
while (block_and_wait);
924 if (res_query == AMF_EOF) {
936 amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
941 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
944 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
948 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
955 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
958 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
962 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
966 return color_profile;