20 #include "config_components.h"
43 #define AMF_AV_FRAME_REF L"av_frame_ref"
44 #define PTS_PROP L"PtsProp"
57 const unsigned int luma_den = 10000;
58 hdrmeta->maxMasteringLuminance =
60 hdrmeta->minMasteringLuminance =
64 const unsigned int chroma_den = 50000;
65 hdrmeta->redPrimary[0] =
67 hdrmeta->redPrimary[1] =
69 hdrmeta->greenPrimary[0] =
71 hdrmeta->greenPrimary[1] =
73 hdrmeta->bluePrimary[0] =
75 hdrmeta->bluePrimary[1] =
77 hdrmeta->whitePoint[0] =
79 hdrmeta->whitePoint[1] =
87 hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->
MaxCLL;
88 hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->
MaxFALL;
106 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
146 if(!
ctx->pts_property_name)
150 ctx->av_frame_property_name =
av_memdup(
name, alloc_size *
sizeof(
wchar_t));
151 if(!
ctx->av_frame_property_name)
177 AMF_RETURN_IF_FALSE(
ctx, amf_device_ctx->version >= AMF_MAKE_FULL_VERSION(1, 4, 32, 0),
AVERROR_UNKNOWN,
"10-bit encoder is not supported by AMD GPU drivers versions lower than 23.30.\n");
184 res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context,
codec_id, &
ctx->encoder);
187 ctx->submitted_frame = 0;
188 ctx->encoded_frame = 0;
199 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
200 ctx->encoder->pVtbl->Release(
ctx->encoder);
207 if (
ctx->output_list) {
227 uint8_t *dst_data[4] = {0};
228 int dst_linesize[4] = {0};
232 planes = (int)surface->pVtbl->GetPlanesCount(surface);
236 plane = surface->pVtbl->GetPlaneAt(surface,
i);
237 dst_data[
i] = plane->pVtbl->GetNative(plane);
238 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
251 AMFVariantStruct var = {0};
262 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
263 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
268 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
269 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
274 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
275 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
284 pkt->
pts = var.int64Value;
290 if ((
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false)) &&
ctx->dts_delay == 0) {
295 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
300 ctx->dts_delay = timestamp_last - timestamp;
316 if (!
ctx->timestamp_list) {
320 if (!
ctx->output_list)
325 ctx->hwsurfaces_in_queue = 0;
354 if (
ctx->pa_lookahead_buffer_depth >=
ctx->hwsurfaces_in_queue_max) {
356 "async_depth (%d) too small for lookahead (%d), increasing to (%d)\n",
357 ctx->hwsurfaces_in_queue_max,
358 ctx->pa_lookahead_buffer_depth,
359 ctx->pa_lookahead_buffer_depth + 1);
360 ctx->hwsurfaces_in_queue_max =
ctx->pa_lookahead_buffer_depth + 1;
374 AMFVariantStruct var;
375 res = AMFVariantInit(&var);
377 AMFGuid guid_AMFInterface = IID_AMFInterface();
378 AMFInterface *amf_interface;
379 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
382 res = AMFVariantAssignInterface(&var, amf_interface);
383 amf_interface->pVtbl->Release(amf_interface);
386 res =
object->pVtbl->SetProperty(
object,
name, var);
388 AMFVariantClear(&var);
395 AMF_RESULT res = AMF_FAIL;
400 AMF_ASSIGN_PROPERTY_INT64(res, surface,
ctx->av_frame_property_name,
data);
407 AMFVariantStruct var = {0};
408 AMF_RESULT res =
buffer->pVtbl->GetProperty(
buffer,
ctx->av_frame_property_name, &var);
409 if(res == AMF_OK && var.int64Value){
429 switch (
frame->format) {
433 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
434 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
438 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
439 res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX11Native(amf_device_ctx->context, texture, &surface,
NULL);
448 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
449 res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX9Native(amf_device_ctx->context, texture, &surface,
NULL);
457 surface = (AMFSurface*)
frame->data[0];
458 surface->pVtbl->Acquire(surface);
464 res = amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
472 ctx->hwsurfaces_in_queue++;
474 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
478 AMFBuffer * hdrmeta_buffer =
NULL;
479 res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST,
sizeof(AMFHDRMetadata), &hdrmeta_buffer);
481 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
485 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
487 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
489 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
494 hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
497 surface->pVtbl->SetPts(surface,
frame->pts);
499 AMF_ASSIGN_PROPERTY_INT64(res, surface,
ctx->pts_property_name,
frame->pts);
503 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
504 switch (
frame->pict_type) {
506 if (
ctx->forced_idr) {
507 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
508 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
509 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
511 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
515 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
518 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
523 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
524 switch (
frame->pict_type) {
526 if (
ctx->forced_idr) {
527 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
528 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
530 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
534 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
540 if (
ctx->forced_idr) {
541 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
542 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
544 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
552 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
553 if (res == AMF_INPUT_FULL) {
555 *surface_resubmit = surface;
557 surface->pVtbl->Release(surface);
559 ctx->submitted_frame++;
563 if(
ctx->submitted_frame <=
ctx->encoded_frame + output_delay)
576 if (amf_device_ctx->lock)
577 amf_device_ctx->lock(amf_device_ctx->lock_ctx);
579 if (amf_device_ctx->unlock)
580 amf_device_ctx->unlock(amf_device_ctx->lock_ctx);
588 AMF_RESULT
ret =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
591 AMFGuid guid = IID_AMFBuffer();
595 ctx->hwsurfaces_in_queue--;
596 ctx->encoded_frame++;
604 AMFSurface *surface =
NULL;
607 AMF_RESULT res_query;
631 if(
ctx->submitted_frame <=
ctx->encoded_frame + output_delay)
637 if (!
frame->buf[0]) {
639 if(!
ctx->delayed_drain) {
640 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
641 if (res == AMF_INPUT_FULL) {
642 ctx->delayed_drain = 1;
672 if (
ctx->delayed_drain) {
673 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
674 if (res != AMF_INPUT_FULL) {
675 ctx->delayed_drain = 0;
679 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
682 }
else if (
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max) || surface) {
686 if (!
ctx->query_timeout_supported || avpkt->
data || avpkt->
buf) {
690 }
while (block_and_wait);
692 if (res_query == AMF_EOF) {
700 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
701 if (res != AMF_INPUT_FULL)
704 if (!
ctx->query_timeout_supported)
715 }
while(res == AMF_INPUT_FULL);
717 surface->pVtbl->Release(surface);
718 if (res == AMF_INPUT_FULL) {
719 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed SubmitInput returned AMF_INPUT_FULL- should not happen\n");
725 ctx->submitted_frame++;
738 amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
743 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
746 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
750 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
757 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
760 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
764 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
768 return color_profile;