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);
357 if (
ctx->hw_frames_ctx)
366 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
376 if (
ctx->delayed_surface) {
377 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
382 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
383 ctx->encoder->pVtbl->Release(
ctx->encoder);
388 ctx->context->pVtbl->Terminate(
ctx->context);
389 ctx->context->pVtbl->Release(
ctx->context);
399 dlclose(
ctx->library);
406 ctx->delayed_drain = 0;
417 uint8_t *dst_data[4];
422 planes = surface->pVtbl->GetPlanesCount(surface);
426 plane = surface->pVtbl->GetPlaneAt(surface,
i);
427 dst_data[
i] = plane->pVtbl->GetNative(plane);
428 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
441 AMFVariantStruct var = {0};
452 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
453 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
458 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
459 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
469 pkt->
pts = var.int64Value;
481 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
486 ctx->dts_delay = timestamp_last - timestamp;
511 AMFVariantStruct var;
512 res = AMFVariantInit(&var);
514 AMFGuid guid_AMFInterface = IID_AMFInterface();
515 AMFInterface *amf_interface;
516 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
519 res = AMFVariantAssignInterface(&var, amf_interface);
520 amf_interface->pVtbl->Release(amf_interface);
523 res =
object->pVtbl->SetProperty(
object,
name, var);
525 AMFVariantClear(&var);
533 AMFVariantStruct var;
534 res = AMFVariantInit(&var);
536 res =
object->pVtbl->GetProperty(
object,
name, &var);
538 if (var.type == AMF_VARIANT_INTERFACE) {
539 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
540 AMFInterface *amf_interface = AMFVariantInterface(&var);
541 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
543 res = AMF_INVALID_DATA_TYPE;
546 AMFVariantClear(&var);
554 AMFBuffer *frame_ref_storage_buffer =
NULL;
557 res =
context->pVtbl->AllocBuffer(
context, AMF_MEMORY_HOST,
sizeof(frame_ref), &frame_ref_storage_buffer);
561 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref,
sizeof(frame_ref));
563 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
564 frame_ref_storage_buffer =
NULL;
567 return frame_ref_storage_buffer;
573 memcpy(&frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer),
sizeof(frame_ref));
575 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
584 AMF_RESULT res_query;
592 if (!
frame->buf[0]) {
598 if (!
frame->buf[0]) {
600 if (
ctx->delayed_surface !=
NULL) {
601 ctx->delayed_drain = 1;
602 }
else if(!
ctx->delayed_drain) {
603 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
604 if (res == AMF_INPUT_FULL) {
605 ctx->delayed_drain = 1;
614 }
else if (!
ctx->delayed_surface) {
618 switch (
frame->format) {
622 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
623 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
627 frame->hw_frames_ctx->data ==
ctx->hw_frames_ctx->data);
629 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
631 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
641 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
643 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
652 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
660 AMFBuffer *frame_ref_storage_buffer;
663 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
670 ctx->hwsurfaces_in_queue++;
671 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
674 surface->pVtbl->SetPts(surface,
frame->pts);
675 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
679 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
682 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
689 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
690 if (res == AMF_INPUT_FULL) {
692 ctx->delayed_surface = surface;
695 surface->pVtbl->Release(surface);
709 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
713 AMFGuid guid = IID_AMFBuffer();
719 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
720 AMFBuffer *frame_ref_storage_buffer;
724 ctx->hwsurfaces_in_queue--;
731 if (
ctx->delayed_surface !=
NULL) {
732 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
733 if (res != AMF_INPUT_FULL) {
734 int64_t
pts =
ctx->delayed_surface->pVtbl->GetPts(
ctx->delayed_surface);
735 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
744 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
746 }
else if (
ctx->delayed_drain) {
747 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
748 if (res != AMF_INPUT_FULL) {
749 ctx->delayed_drain = 0;
753 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
756 }
else if (
ctx->delayed_surface !=
NULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
760 }
while (block_and_wait);
762 if (res_query == AMF_EOF) {