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);
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);
219 ctx->hwsurfaces_in_queue = 0;
220 ctx->hwsurfaces_in_queue_max = 16;
224 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
226 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
227 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
228 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
232 ctx->tracer.avctx = avctx;
236 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
244 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
265 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
271 if (!
ctx->hw_frames_ctx)
280 switch (device_ctx->
type) {
283 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
290 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
296 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
302 if (!
ctx->hw_device_ctx)
306 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
310 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
341 if (
ctx->hw_frames_ctx)
350 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
360 if (
ctx->delayed_surface) {
361 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
366 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
367 ctx->encoder->pVtbl->Release(
ctx->encoder);
372 ctx->context->pVtbl->Terminate(
ctx->context);
373 ctx->context->pVtbl->Release(
ctx->context);
383 dlclose(
ctx->library);
390 ctx->delayed_drain = 0;
406 planes = surface->pVtbl->GetPlanesCount(surface);
410 plane = surface->pVtbl->GetPlaneAt(surface,
i);
437 AMFVariantStruct var = {0};
448 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
449 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
454 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
455 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
465 pkt->
pts = var.int64Value;
476 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
480 (
av_fifo_size(
ctx->timestamp_list) /
sizeof(timestamp) - 1) *
sizeof(timestamp_last),
481 sizeof(timestamp_last),
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);
590 if (
ctx->delayed_surface !=
NULL) {
591 ctx->delayed_drain = 1;
592 }
else if(!
ctx->delayed_drain) {
593 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
594 if (res == AMF_INPUT_FULL) {
595 ctx->delayed_drain = 1;
609 if (
ctx->delayed_surface !=
NULL) {
613 switch (
frame->format) {
617 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
618 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
622 frame->hw_frames_ctx->data ==
ctx->hw_frames_ctx->data);
624 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
626 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
636 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
638 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
647 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
655 AMFBuffer *frame_ref_storage_buffer;
658 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
665 ctx->hwsurfaces_in_queue++;
666 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
669 surface->pVtbl->SetPts(surface,
frame->pts);
670 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
674 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
677 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
685 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
686 if (res == AMF_INPUT_FULL) {
688 ctx->delayed_surface = surface;
689 if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_DX11) {
693 surface->pVtbl->Release(surface);
708 AMF_RESULT res_query;
719 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
723 AMFGuid guid = IID_AMFBuffer();
729 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
730 AMFBuffer *frame_ref_storage_buffer;
734 ctx->hwsurfaces_in_queue--;
741 if (
ctx->delayed_surface !=
NULL) {
742 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
743 if (res != AMF_INPUT_FULL) {
744 int64_t
pts =
ctx->delayed_surface->pVtbl->GetPts(
ctx->delayed_surface);
745 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
754 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
756 }
else if (
ctx->delayed_drain) {
757 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
758 if (res != AMF_INPUT_FULL) {
759 ctx->delayed_drain = 0;
763 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
766 }
else if (
ctx->delayed_surface !=
NULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
770 }
while (block_and_wait);
772 if (res_query == AMF_EOF) {