20 #include "config_components.h"
43 #define AMF_AV_FRAME_REF L"av_frame_ref"
56 const unsigned int luma_den = 10000;
57 hdrmeta->maxMasteringLuminance =
59 hdrmeta->minMasteringLuminance =
63 const unsigned int chroma_den = 50000;
64 hdrmeta->redPrimary[0] =
66 hdrmeta->redPrimary[1] =
68 hdrmeta->greenPrimary[0] =
70 hdrmeta->greenPrimary[1] =
72 hdrmeta->bluePrimary[0] =
74 hdrmeta->bluePrimary[1] =
76 hdrmeta->whitePoint[0] =
78 hdrmeta->whitePoint[1] =
86 hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->
MaxCLL;
87 hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->
MaxFALL;
105 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
107 #define PTS_PROP L"PtsProp"
153 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");
160 res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context,
codec_id, &
ctx->encoder);
163 ctx->submitted_frame = 0;
164 ctx->encoded_frame = 0;
175 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
176 ctx->encoder->pVtbl->Release(
ctx->encoder);
190 uint8_t *dst_data[4] = {0};
191 int dst_linesize[4] = {0};
195 planes = (int)surface->pVtbl->GetPlanesCount(surface);
199 plane = surface->pVtbl->GetPlaneAt(surface,
i);
200 dst_data[
i] = plane->pVtbl->GetNative(plane);
201 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
214 AMFVariantStruct var = {0};
225 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
226 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
231 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
232 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
237 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
238 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
247 pkt->
pts = var.int64Value;
253 if ((
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false)) &&
ctx->dts_delay == 0) {
258 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
263 ctx->dts_delay = timestamp_last - timestamp;
279 if (!
ctx->timestamp_list) {
284 ctx->hwsurfaces_in_queue = 0;
324 AMFVariantStruct var;
325 res = AMFVariantInit(&var);
327 AMFGuid guid_AMFInterface = IID_AMFInterface();
328 AMFInterface *amf_interface;
329 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
332 res = AMFVariantAssignInterface(&var, amf_interface);
333 amf_interface->pVtbl->Release(amf_interface);
336 res =
object->pVtbl->SetProperty(
object,
name, var);
338 AMFVariantClear(&var);
345 AMF_RESULT res = AMF_FAIL;
357 AMFVariantStruct var = {0};
359 if(res == AMF_OK && var.int64Value){
375 AMF_RESULT res_query;
382 int max_b_frames =
ctx->max_b_frames < 0 ? 0 :
ctx->max_b_frames;
393 if(
ctx->submitted_frame <=
ctx->encoded_frame + max_b_frames + 1)
399 if (!
frame->buf[0]) {
401 if(!
ctx->delayed_drain) {
402 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
403 if (res == AMF_INPUT_FULL) {
404 ctx->delayed_drain = 1;
416 switch (
frame->format) {
420 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
421 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->data[0];
427 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
429 res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX11Native(amf_device_ctx->context, texture, &surface,
NULL);
439 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->data[3];
441 res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX9Native(amf_device_ctx->context, texture, &surface,
NULL);
450 surface = (AMFSurface*)
frame->data[0];
451 surface->pVtbl->Acquire(surface);
457 res = amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
466 ctx->hwsurfaces_in_queue++;
468 surface->pVtbl->SetCrop(surface, 0, 0,
frame->width,
frame->height);
473 AMFBuffer * hdrmeta_buffer =
NULL;
474 res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST,
sizeof(AMFHDRMetadata), &hdrmeta_buffer);
476 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
480 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
482 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
484 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer);
break;
489 hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
493 surface->pVtbl->SetPts(surface,
frame->pts);
494 AMF_ASSIGN_PROPERTY_INT64(res, surface,
PTS_PROP,
frame->pts);
498 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
499 switch (
frame->pict_type) {
501 if (
ctx->forced_idr) {
502 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
503 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
504 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
506 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
510 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
513 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
518 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
519 switch (
frame->pict_type) {
521 if (
ctx->forced_idr) {
522 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
523 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
525 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
529 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
535 if (
ctx->forced_idr) {
536 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
537 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
539 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
548 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
551 if (res == AMF_INPUT_FULL) {
555 surface->pVtbl->Release(surface);
558 ctx->submitted_frame++;
563 if(
ctx->submitted_frame <=
ctx->encoded_frame + max_b_frames + 1)
574 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
578 AMFGuid guid = IID_AMFBuffer();
582 ctx->hwsurfaces_in_queue--;
584 ctx->encoded_frame++;
590 if (
ctx->delayed_drain) {
591 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
592 if (res != AMF_INPUT_FULL) {
593 ctx->delayed_drain = 0;
597 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
600 }
else if (
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max) || input_full) {
604 if (!
ctx->query_timeout_supported || avpkt->
data || avpkt->
buf) {
608 }
while (block_and_wait);
610 if (res_query == AMF_EOF) {
617 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
618 surface->pVtbl->Release(surface);
619 if (res == AMF_INPUT_FULL) {
620 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed SubmitInput returned AMF_INPUT_FULL- should not happen\n");
626 ctx->submitted_frame++;
639 amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
644 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
647 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
651 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
658 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
661 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
665 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
669 return color_profile;