48 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
50 #define PTS_PROP L"PtsProp"
88 return AMF_SURFACE_UNKNOWN;
92 const wchar_t *scope,
const wchar_t *
message)
112 AMFQueryVersion_Fn version_fun;
116 if (!
ctx->delayed_frame) {
121 if (!
ctx->timestamp_list) {
127 ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
131 init_fun = (AMFInit_Fn)dlsym(
ctx->library, AMF_INIT_FUNCTION_NAME);
134 version_fun = (AMFQueryVersion_Fn)dlsym(
ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
137 res = version_fun(&
ctx->version);
139 res = init_fun(AMF_FULL_VERSION, &
ctx->factory);
141 res =
ctx->factory->pVtbl->GetTrace(
ctx->factory, &
ctx->trace);
154 res =
ctx->context->pVtbl->InitDX11(
ctx->context, hwctx->
device, AMF_DX11_1);
156 if (res == AMF_NOT_SUPPORTED)
159 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given D3D11 device: %d.\n", res);
171 HANDLE device_handle;
172 IDirect3DDevice9 *device;
177 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->
devmgr, &device_handle);
179 av_log(avctx,
AV_LOG_ERROR,
"Failed to open device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
183 hr = IDirect3DDeviceManager9_LockDevice(hwctx->
devmgr, device_handle, &device, FALSE);
185 IDirect3DDeviceManager9_UnlockDevice(hwctx->
devmgr, device_handle, FALSE);
188 av_log(avctx,
AV_LOG_ERROR,
"Failed to lock device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
192 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->
devmgr, device_handle);
197 res =
ctx->context->pVtbl->InitDX9(
ctx->context, device);
199 IDirect3DDevice9_Release(device);
202 if (res == AMF_NOT_SUPPORTED)
205 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on given D3D9 device: %d.\n", res);
216 AMFContext1 *context1 =
NULL;
220 ctx->hwsurfaces_in_queue = 0;
221 ctx->hwsurfaces_in_queue_max = 16;
225 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
227 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
228 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
229 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
233 ctx->tracer.avctx = avctx;
237 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
245 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
266 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
272 if (!
ctx->hw_frames_ctx)
281 switch (device_ctx->
type) {
284 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
291 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
297 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
303 if (!
ctx->hw_device_ctx)
307 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
311 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
315 AMFGuid guid = IID_AMFContext1();
316 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
319 res = context1->pVtbl->InitVulkan(context1,
NULL);
320 context1->pVtbl->Release(context1);
322 if (res == AMF_NOT_SUPPORTED)
323 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
325 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
354 if (
ctx->hw_frames_ctx)
363 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
373 if (
ctx->delayed_surface) {
374 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
379 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
380 ctx->encoder->pVtbl->Release(
ctx->encoder);
385 ctx->context->pVtbl->Terminate(
ctx->context);
386 ctx->context->pVtbl->Release(
ctx->context);
396 dlclose(
ctx->library);
403 ctx->delayed_drain = 0;
419 planes = surface->pVtbl->GetPlanesCount(surface);
423 plane = surface->pVtbl->GetPlaneAt(surface,
i);
424 dst_data[
i] = plane->pVtbl->GetNative(plane);
425 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
450 AMFVariantStruct var = {0};
461 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
462 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
467 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
468 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
478 pkt->
pts = var.int64Value;
489 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
493 (
av_fifo_size(
ctx->timestamp_list) /
sizeof(timestamp) - 1) *
sizeof(timestamp_last),
494 sizeof(timestamp_last),
499 ctx->dts_delay = timestamp_last - timestamp;
524 AMFVariantStruct var;
525 res = AMFVariantInit(&var);
527 AMFGuid guid_AMFInterface = IID_AMFInterface();
528 AMFInterface *amf_interface;
529 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
532 res = AMFVariantAssignInterface(&var, amf_interface);
533 amf_interface->pVtbl->Release(amf_interface);
536 res =
object->pVtbl->SetProperty(
object,
name, var);
538 AMFVariantClear(&var);
546 AMFVariantStruct var;
547 res = AMFVariantInit(&var);
549 res =
object->pVtbl->GetProperty(
object,
name, &var);
551 if (var.type == AMF_VARIANT_INTERFACE) {
552 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
553 AMFInterface *amf_interface = AMFVariantInterface(&var);
554 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
556 res = AMF_INVALID_DATA_TYPE;
559 AMFVariantClear(&var);
567 AMFBuffer *frame_ref_storage_buffer =
NULL;
570 res =
context->pVtbl->AllocBuffer(
context, AMF_MEMORY_HOST,
sizeof(frame_ref), &frame_ref_storage_buffer);
574 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref,
sizeof(frame_ref));
576 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
577 frame_ref_storage_buffer =
NULL;
580 return frame_ref_storage_buffer;
586 memcpy(&frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer),
sizeof(frame_ref));
588 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
603 if (
ctx->delayed_surface !=
NULL) {
604 ctx->delayed_drain = 1;
605 }
else if(!
ctx->delayed_drain) {
606 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
607 if (res == AMF_INPUT_FULL) {
608 ctx->delayed_drain = 1;
622 if (
ctx->delayed_surface !=
NULL) {
626 switch (
frame->format) {
630 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
631 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
635 frame->hw_frames_ctx->data ==
ctx->hw_frames_ctx->data);
637 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
639 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
649 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
651 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
660 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
668 AMFBuffer *frame_ref_storage_buffer;
671 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
678 ctx->hwsurfaces_in_queue++;
679 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
682 surface->pVtbl->SetPts(surface,
frame->pts);
683 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
687 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
690 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
698 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
699 if (res == AMF_INPUT_FULL) {
701 ctx->delayed_surface = surface;
702 if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_DX11) {
706 surface->pVtbl->Release(surface);
721 AMF_RESULT res_query;
732 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
736 AMFGuid guid = IID_AMFBuffer();
742 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
743 AMFBuffer *frame_ref_storage_buffer;
747 ctx->hwsurfaces_in_queue--;
754 if (
ctx->delayed_surface !=
NULL) {
755 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
756 if (res != AMF_INPUT_FULL) {
757 int64_t
pts =
ctx->delayed_surface->pVtbl->GetPts(
ctx->delayed_surface);
758 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
767 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
769 }
else if (
ctx->delayed_drain) {
770 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
771 if (res != AMF_INPUT_FULL) {
772 ctx->delayed_drain = 0;
776 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
779 }
else if (
ctx->delayed_surface !=
NULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
783 }
while (block_and_wait);
785 if (res_query == AMF_EOF) {