49 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
51 #define PTS_PROP L"PtsProp"
89 return AMF_SURFACE_UNKNOWN;
93 const wchar_t *scope,
const wchar_t *
message)
113 AMFQueryVersion_Fn version_fun;
117 if (!
ctx->delayed_frame) {
122 if (!
ctx->timestamp_list) {
128 ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
132 init_fun = (AMFInit_Fn)dlsym(
ctx->library, AMF_INIT_FUNCTION_NAME);
135 version_fun = (AMFQueryVersion_Fn)dlsym(
ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
138 res = version_fun(&
ctx->version);
140 res = init_fun(AMF_FULL_VERSION, &
ctx->factory);
142 res =
ctx->factory->pVtbl->GetTrace(
ctx->factory, &
ctx->trace);
155 res =
ctx->context->pVtbl->InitDX11(
ctx->context, hwctx->
device, AMF_DX11_1);
157 if (res == AMF_NOT_SUPPORTED)
160 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given D3D11 device: %d.\n", res);
172 HANDLE device_handle;
173 IDirect3DDevice9 *device;
178 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->
devmgr, &device_handle);
180 av_log(avctx,
AV_LOG_ERROR,
"Failed to open device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
184 hr = IDirect3DDeviceManager9_LockDevice(hwctx->
devmgr, device_handle, &device, FALSE);
186 IDirect3DDeviceManager9_UnlockDevice(hwctx->
devmgr, device_handle, FALSE);
189 av_log(avctx,
AV_LOG_ERROR,
"Failed to lock device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
193 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->
devmgr, device_handle);
198 res =
ctx->context->pVtbl->InitDX9(
ctx->context, device);
200 IDirect3DDevice9_Release(device);
203 if (res == AMF_NOT_SUPPORTED)
206 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on given D3D9 device: %d.\n", res);
217 AMFContext1 *context1 =
NULL;
221 ctx->hwsurfaces_in_queue = 0;
222 ctx->hwsurfaces_in_queue_max = 16;
226 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
228 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
229 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
230 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
234 ctx->tracer.avctx = avctx;
238 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
246 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
267 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
273 if (!
ctx->hw_frames_ctx)
282 switch (device_ctx->
type) {
285 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
292 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
298 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
304 if (!
ctx->hw_device_ctx)
308 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
312 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
316 AMFGuid guid = IID_AMFContext1();
317 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
320 res = context1->pVtbl->InitVulkan(context1,
NULL);
321 context1->pVtbl->Release(context1);
323 if (res == AMF_NOT_SUPPORTED)
324 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
326 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
355 if (
ctx->hw_frames_ctx)
364 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
374 if (
ctx->delayed_surface) {
375 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
380 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
381 ctx->encoder->pVtbl->Release(
ctx->encoder);
386 ctx->context->pVtbl->Terminate(
ctx->context);
387 ctx->context->pVtbl->Release(
ctx->context);
397 dlclose(
ctx->library);
404 ctx->delayed_drain = 0;
415 uint8_t *dst_data[4];
420 planes = surface->pVtbl->GetPlanesCount(surface);
424 plane = surface->pVtbl->GetPlaneAt(surface,
i);
425 dst_data[
i] = plane->pVtbl->GetNative(plane);
426 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
451 AMFVariantStruct var = {0};
462 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
463 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
468 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
469 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
479 pkt->
pts = var.int64Value;
490 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
494 (
av_fifo_size(
ctx->timestamp_list) /
sizeof(timestamp) - 1) *
sizeof(timestamp_last),
495 sizeof(timestamp_last),
500 ctx->dts_delay = timestamp_last - timestamp;
525 AMFVariantStruct var;
526 res = AMFVariantInit(&var);
528 AMFGuid guid_AMFInterface = IID_AMFInterface();
529 AMFInterface *amf_interface;
530 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
533 res = AMFVariantAssignInterface(&var, amf_interface);
534 amf_interface->pVtbl->Release(amf_interface);
537 res =
object->pVtbl->SetProperty(
object,
name, var);
539 AMFVariantClear(&var);
547 AMFVariantStruct var;
548 res = AMFVariantInit(&var);
550 res =
object->pVtbl->GetProperty(
object,
name, &var);
552 if (var.type == AMF_VARIANT_INTERFACE) {
553 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
554 AMFInterface *amf_interface = AMFVariantInterface(&var);
555 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
557 res = AMF_INVALID_DATA_TYPE;
560 AMFVariantClear(&var);
568 AMFBuffer *frame_ref_storage_buffer =
NULL;
571 res =
context->pVtbl->AllocBuffer(
context, AMF_MEMORY_HOST,
sizeof(frame_ref), &frame_ref_storage_buffer);
575 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref,
sizeof(frame_ref));
577 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
578 frame_ref_storage_buffer =
NULL;
581 return frame_ref_storage_buffer;
587 memcpy(&frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer),
sizeof(frame_ref));
589 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
598 AMF_RESULT res_query;
606 if (!
frame->buf[0]) {
612 if (!
frame->buf[0]) {
614 if (
ctx->delayed_surface !=
NULL) {
615 ctx->delayed_drain = 1;
616 }
else if(!
ctx->delayed_drain) {
617 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
618 if (res == AMF_INPUT_FULL) {
619 ctx->delayed_drain = 1;
628 }
else if (!
ctx->delayed_surface) {
632 switch (
frame->format) {
636 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
637 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
641 frame->hw_frames_ctx->data ==
ctx->hw_frames_ctx->data);
643 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
645 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
655 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
657 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
666 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
674 AMFBuffer *frame_ref_storage_buffer;
677 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
684 ctx->hwsurfaces_in_queue++;
685 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
688 surface->pVtbl->SetPts(surface,
frame->pts);
689 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
693 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
696 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
703 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
704 if (res == AMF_INPUT_FULL) {
706 ctx->delayed_surface = surface;
709 surface->pVtbl->Release(surface);
723 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
727 AMFGuid guid = IID_AMFBuffer();
733 if (
data->pVtbl->HasProperty(
data,
L"av_frame_ref")) {
734 AMFBuffer *frame_ref_storage_buffer;
738 ctx->hwsurfaces_in_queue--;
745 if (
ctx->delayed_surface !=
NULL) {
746 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
747 if (res != AMF_INPUT_FULL) {
748 int64_t
pts =
ctx->delayed_surface->pVtbl->GetPts(
ctx->delayed_surface);
749 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
758 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
760 }
else if (
ctx->delayed_drain) {
761 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
762 if (res != AMF_INPUT_FULL) {
763 ctx->delayed_drain = 0;
767 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
770 }
else if (
ctx->delayed_surface !=
NULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
774 }
while (block_and_wait);
776 if (res_query == AMF_EOF) {