45 #define AMF_VIDEO_DECODER_OUTPUT_FORMAT L"OutputDecodeFormat"
62 AMFSurface *surface = (AMFSurface*)(
data);
63 surface->pVtbl->Release(surface);
68 if( AMF_GET_MAJOR_VERSION(amf_device_ctx->
version) <= 1 &&
69 AMF_GET_MINOR_VERSION(amf_device_ctx->
version) <= 4 &&
70 AMF_GET_SUBMINOR_VERSION(amf_device_ctx->
version) < 36)
83 amf_int64 color_profile;
90 ctx->resolution_changed = 0;
94 codec_id = AMFVideoDecoderUVD_H264_AVC;
97 codec_id = AMFVideoDecoderHW_H265_HEVC;
98 if(no_bitness_detect){
100 codec_id = AMFVideoDecoderHW_H265_MAIN10;
105 if(no_bitness_detect){
107 codec_id = AMFVideoDecoderHW_VP9_10BIT;
118 res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context,
codec_id, &
ctx->decoder);
124 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 1);
126 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 0);
128 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
132 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
134 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
139 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
141 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
147 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
149 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
153 if (color_profile != AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN)
154 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PROFILE, color_profile);
156 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->
color_trc);
159 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PRIMARIES, (amf_int64)avctx->
color_primaries);
161 if (
ctx->timestamp_mode != -1)
162 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_TIMESTAMP_MODE,
ctx->timestamp_mode);
163 if (
ctx->decoder_mode != -1)
164 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_REORDER_MODE,
ctx->decoder_mode);
165 if (
ctx->dpb_size != -1)
166 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_DPB_SIZE,
ctx->dpb_size);
167 if (
ctx->lowlatency != -1)
168 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
ctx->lowlatency);
169 if (
ctx->smart_access_video != -1) {
170 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_ENABLE_SMART_ACCESS_VIDEO,
ctx->smart_access_video != 0);
172 av_log(avctx,
AV_LOG_ERROR,
"The Smart Access Video is not supported by AMF decoder.\n");
177 if (
ctx->smart_access_video != 0) {
178 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
true);
179 av_log(avctx,
AV_LOG_INFO,
"The Smart Access Video set low latency mode for decoder.\n");
183 if (
ctx->skip_transfer_sav != -1)
184 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SKIP_TRANSFER_SMART_ACCESS_VIDEO,
ctx->skip_transfer_sav);
186 if (
ctx->copy_output != -1)
187 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_COPY,
ctx->copy_output);
190 const uint8_t *extradata;
193 res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST, extradata_size, &
buffer);
195 memcpy(
buffer->pVtbl->GetNative(
buffer), extradata, extradata_size);
196 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->decoder, AMF_VIDEO_DECODER_EXTRADATA,
buffer);
201 if (
ctx->surface_pool_size == -1) {
202 ctx->surface_pool_size = pool_size;
211 if (
ctx->surface_pool_size > 100)
212 ctx->surface_pool_size = 100;
214 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_POOL_SIZE,
ctx->surface_pool_size);
215 res =
ctx->decoder->pVtbl->Init(
ctx->decoder, AMF_SURFACE_UNKNOWN, avctx->
width, avctx->
height);
228 ctx->decoder->pVtbl->Terminate(
ctx->decoder);
229 ctx->decoder->pVtbl->Release(
ctx->decoder);
254 hwframes_ctx->
width = new_width;
255 hwframes_ctx->
height = new_height;
309 AMFVariantStruct format_var = {0};
342 AMFVariantStruct var;
343 res = AMFVariantInit(&var);
345 res =
object->pVtbl->GetProperty(
object,
name, &var);
347 if (var.type == AMF_VARIANT_INTERFACE) {
348 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
349 AMFInterface *amf_interface = AMFVariantInterface(&var);
350 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
352 res = AMF_INVALID_DATA_TYPE;
355 AMFVariantClear(&var);
362 AMFVariantStruct var = {0};
389 frame->data[0] = (uint8_t *)surface;
391 format_amf = surface->pVtbl->GetFormat(surface);
395 ret = surface->pVtbl->Convert(surface, AMF_MEMORY_HOST);
398 for (
i = 0;
i < surface->pVtbl->GetPlanesCount(surface);
i++) {
399 plane = surface->pVtbl->GetPlaneAt(surface,
i);
400 frame->data[
i] = plane->pVtbl->GetNative(plane);
401 frame->linesize[
i] = plane->pVtbl->GetHPitch(plane);
409 format_amf = surface->pVtbl->GetFormat(surface);
416 frame->pts = surface->pVtbl->GetPts(surface);
418 surface->pVtbl->GetProperty(surface,
L"FFMPEG:dts", &var);
419 frame->pkt_dts = var.int64Value;
421 frame->duration = surface->pVtbl->GetDuration(surface);
422 if (
frame->duration < 0)
431 AMFBuffer * hdrmeta_buffer =
NULL;
433 if (hdrmeta_buffer !=
NULL) {
434 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
437 if (hdrmeta !=
NULL) {
439 const int chroma_den = 50000;
440 const int luma_den = 10000;
462 if (hdrmeta->maxContentLightLevel) {
468 light->
MaxCLL = hdrmeta->maxContentLightLevel;
469 light->
MaxFALL = hdrmeta->maxFrameAverageLightLevel;
480 AMF_RESULT
ret = AMF_OK;
481 AMFSurface *surface =
NULL;
482 AMFData *data_out =
NULL;
484 ret =
ctx->decoder->pVtbl->QueryOutput(
ctx->decoder, &data_out);
485 if (
ret != AMF_OK &&
ret != AMF_REPEAT) {
488 if (data_out ==
NULL) {
493 AMFGuid guid = IID_AMFSurface();
494 data_out->pVtbl->QueryInterface(data_out, &guid, (
void**)&surface);
495 data_out->pVtbl->Release(data_out);
505 surface->pVtbl->Release(surface);
519 AMF_ASSIGN_PROPERTY_INT64(res,
buffer,
L"FFMPEG:dts",
pkt->
dts);
530 AMFContext *ctxt = amf_device_ctx->context;
533 AMFBuffer *buf =
NULL;
541 err = buf->pVtbl->SetSize(buf,
pkt->
size);
542 AMF_RETURN_IF_FALSE(ctxt, err == AMF_OK, err,
"amf_buffer_from_packet() - SetSize failed");
544 mem = buf->pVtbl->GetNative(buf);
567 if(
ctx->resolution_changed)
568 ctx->resolution_changed = 0;
577 ctx->decoder->pVtbl->Drain(
ctx->decoder);
588 res =
ctx->decoder->pVtbl->SubmitInput(
ctx->decoder, (AMFData*) buf);
589 if(res == AMF_DECODER_NO_FREE_SURFACES)
593 }
while (res == AMF_DECODER_NO_FREE_SURFACES);
595 buf->pVtbl->Release(buf);
597 if(res == AMF_DECODER_NO_FREE_SURFACES) {
599 av_log(avctx,
AV_LOG_VERBOSE,
"SubmitInput() returned NO_FREE_SURFACES and came out of loop - should never happen\n");
601 }
else if (res == AMF_RESOLUTION_CHANGED) {
603 ctx->decoder->pVtbl->Drain(
ctx->decoder);
606 ctx->resolution_changed = 1;
608 }
else if (res != AMF_OK && res != AMF_NEED_MORE_INPUT && res != AMF_REPEAT) {
617 else if (res == AMF_REPEAT)
620 else if (res == AMF_EOF) {
623 if(
ctx->resolution_changed){
625 AMFVariantStruct size_var = {0};
626 AMFVariantStruct format_var = {0};
627 res =
ctx->decoder->pVtbl->GetProperty(
ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
632 avctx->
width = size_var.sizeValue.width;
633 avctx->
height = size_var.sizeValue.height;
653 return got_frame ? 0 :
AVERROR(EAGAIN);
659 ctx->decoder->pVtbl->Flush(
ctx->decoder);
662 #define OFFSET(x) offsetof(AMFDecoderContext, x)
663 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
667 {
"decoder_mode",
"Decoder mode",
OFFSET(decoder_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_VIDEO_DECODER_MODE_LOW_LATENCY,
VD,
"decoder_mode" },
668 {
"regular",
"DPB delay is based on number of reference frames + 1", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_REGULAR }, 0, 0,
VD,
"decoder_mode" },
669 {
"compliant",
"DPB delay is based on profile - up to 16", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_COMPLIANT }, 0, 0,
VD,
"decoder_mode" },
670 {
"low_latency",
"DPB delay is 0", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_LOW_LATENCY }, 0, 0,
VD,
"decoder_mode" },
673 {
"timestamp_mode",
"Timestamp mode",
OFFSET(timestamp_mode),
AV_OPT_TYPE_INT, { .i64 = AMF_TS_SORT }, -1, AMF_TS_DECODE,
VD,
"timestamp_mode" },
674 {
"presentation",
"Preserve timestamps from input to output", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_PRESENTATION }, 0, 0,
VD,
"timestamp_mode" },
675 {
"sort",
"Resort PTS list", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_SORT }, 0, 0,
VD,
"timestamp_mode" },
676 {
"decode",
"Decode order", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_DECODE }, 0, 0,
VD,
"timestamp_mode" },
679 {
"surface_pool_size",
"Number of surfaces in the decode pool",
OFFSET(surface_pool_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VD,
NULL },
684 {
"skip_transfer_sav",
"Skip transfer on another GPU when SAV enabled",
OFFSET(skip_transfer_sav),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VD,
NULL },
697 #define DEFINE_AMF_DECODER(x, X, bsf_name) \
698 const FFCodec ff_##x##_amf_decoder = { \
699 .p.name = #x "_amf", \
700 CODEC_LONG_NAME(#X " AMD AMF video decoder"), \
701 .priv_data_size = sizeof(AMFDecoderContext), \
702 .p.type = AVMEDIA_TYPE_VIDEO, \
703 .p.id = AV_CODEC_ID_##X, \
704 .init = amf_decode_init, \
705 FF_CODEC_RECEIVE_FRAME_CB(amf_decode_frame), \
706 .flush = amf_decode_flush, \
707 .close = amf_decode_close, \
709 .p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
710 .p.priv_class = &amf_decode_class, \
711 .hw_configs = amf_hw_configs, \
712 .p.wrapper_name = "amf", \
713 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \