20 #include "config_components.h"
50 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
52 #define PTS_PROP L"PtsProp"
90 return AMF_SURFACE_UNKNOWN;
94 const wchar_t *scope,
const wchar_t *
message)
114 AMFQueryVersion_Fn version_fun;
118 if (!
ctx->delayed_frame) {
124 if (!
ctx->timestamp_list) {
130 ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
134 init_fun = (AMFInit_Fn)dlsym(
ctx->library, AMF_INIT_FUNCTION_NAME);
137 version_fun = (AMFQueryVersion_Fn)dlsym(
ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
140 res = version_fun(&
ctx->version);
142 res = init_fun(AMF_FULL_VERSION, &
ctx->factory);
144 res =
ctx->factory->pVtbl->GetTrace(
ctx->factory, &
ctx->trace);
157 res =
ctx->context->pVtbl->InitDX11(
ctx->context, hwctx->
device, AMF_DX11_1);
159 if (res == AMF_NOT_SUPPORTED)
162 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given D3D11 device: %d.\n", res);
174 HANDLE device_handle;
175 IDirect3DDevice9 *device;
180 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->
devmgr, &device_handle);
182 av_log(avctx,
AV_LOG_ERROR,
"Failed to open device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
186 hr = IDirect3DDeviceManager9_LockDevice(hwctx->
devmgr, device_handle, &device, FALSE);
188 IDirect3DDeviceManager9_UnlockDevice(hwctx->
devmgr, device_handle, FALSE);
191 av_log(avctx,
AV_LOG_ERROR,
"Failed to lock device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
195 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->
devmgr, device_handle);
200 res =
ctx->context->pVtbl->InitDX9(
ctx->context, device);
202 IDirect3DDevice9_Release(device);
205 if (res == AMF_NOT_SUPPORTED)
208 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on given D3D9 device: %d.\n", res);
219 AMFContext1 *context1 =
NULL;
223 ctx->hwsurfaces_in_queue = 0;
224 ctx->hwsurfaces_in_queue_max = 16;
228 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
230 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
231 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
232 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
236 ctx->tracer.avctx = avctx;
240 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
248 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
269 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
275 if (!
ctx->hw_frames_ctx)
284 switch (device_ctx->
type) {
287 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
294 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
300 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
306 if (!
ctx->hw_device_ctx)
310 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
314 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
318 AMFGuid guid = IID_AMFContext1();
319 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
322 res = context1->pVtbl->InitVulkan(context1,
NULL);
323 context1->pVtbl->Release(context1);
325 if (res == AMF_NOT_SUPPORTED)
326 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
328 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
360 if (
ctx->hw_frames_ctx)
369 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
379 if (
ctx->delayed_surface) {
380 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
385 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
386 ctx->encoder->pVtbl->Release(
ctx->encoder);
391 ctx->context->pVtbl->Terminate(
ctx->context);
392 ctx->context->pVtbl->Release(
ctx->context);
402 dlclose(
ctx->library);
409 ctx->delayed_drain = 0;
420 uint8_t *dst_data[4];
425 planes = surface->pVtbl->GetPlanesCount(surface);
429 plane = surface->pVtbl->GetPlaneAt(surface,
i);
430 dst_data[
i] = plane->pVtbl->GetNative(plane);
431 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
444 AMFVariantStruct var = {0};
455 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
456 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
461 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
462 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
467 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
468 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
477 pkt->
pts = var.int64Value;
484 if ((
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false)) &&
ctx->dts_delay == 0) {
489 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
494 ctx->dts_delay = timestamp_last - timestamp;
519 AMFVariantStruct var;
520 res = AMFVariantInit(&var);
522 AMFGuid guid_AMFInterface = IID_AMFInterface();
523 AMFInterface *amf_interface;
524 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
527 res = AMFVariantAssignInterface(&var, amf_interface);
528 amf_interface->pVtbl->Release(amf_interface);
531 res =
object->pVtbl->SetProperty(
object,
name, var);
533 AMFVariantClear(&var);
541 AMFVariantStruct var;
542 res = AMFVariantInit(&var);
544 res =
object->pVtbl->GetProperty(
object,
name, &var);
546 if (var.type == AMF_VARIANT_INTERFACE) {
547 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
548 AMFInterface *amf_interface = AMFVariantInterface(&var);
549 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
551 res = AMF_INVALID_DATA_TYPE;
554 AMFVariantClear(&var);
562 AMFBuffer *frame_ref_storage_buffer =
NULL;
565 res =
context->pVtbl->AllocBuffer(
context, AMF_MEMORY_HOST,
sizeof(frame_ref), &frame_ref_storage_buffer);
569 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref,
sizeof(frame_ref));
571 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
572 frame_ref_storage_buffer =
NULL;
575 return frame_ref_storage_buffer;
581 memcpy(&frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer),
sizeof(frame_ref));
583 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
592 AMF_RESULT res_query;
596 int query_output_data_flag = 0;
597 AMF_RESULT res_resubmit;
610 if (
ctx->delayed_surface !=
NULL) {
611 ctx->delayed_drain = 1;
612 }
else if(!
ctx->delayed_drain) {
613 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
614 if (res == AMF_INPUT_FULL) {
615 ctx->delayed_drain = 1;
624 }
else if (!
ctx->delayed_surface) {
632 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
633 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->
data[0];
639 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
641 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
651 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->
data[3];
653 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
662 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
670 AMFBuffer *frame_ref_storage_buffer;
680 ctx->hwsurfaces_in_queue++;
681 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
684 surface->pVtbl->SetPts(surface,
frame->
pts);
689 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
692 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
700 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
701 if (res == AMF_INPUT_FULL) {
703 ctx->delayed_surface = surface;
706 surface->pVtbl->Release(surface);
720 if (!avpkt->
data && !avpkt->
buf) {
721 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
725 AMFGuid guid = IID_AMFBuffer();
726 query_output_data_flag = 1;
732 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
733 AMFBuffer* frame_ref_storage_buffer;
737 ctx->hwsurfaces_in_queue--;
745 res_resubmit = AMF_OK;
746 if (
ctx->delayed_surface !=
NULL) {
747 res_resubmit =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
748 if (res_resubmit != AMF_INPUT_FULL) {
750 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
759 }
else if (
ctx->delayed_drain) {
760 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
761 if (res != AMF_INPUT_FULL) {
762 ctx->delayed_drain = 0;
766 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
770 if (query_output_data_flag == 0) {
771 if (res_resubmit == AMF_INPUT_FULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
776 }
while (block_and_wait);
778 if (res_query == AMF_EOF) {