43 #define AMF_VIDEO_DECODER_OUTPUT_FORMAT L"OutputDecodeFormat"
60 AMFSurface *surface = (AMFSurface*)(
data);
61 surface->pVtbl->Release(surface);
66 if( AMF_GET_MAJOR_VERSION(amf_device_ctx->
version) <= 1 &&
67 AMF_GET_MINOR_VERSION(amf_device_ctx->
version) <= 4 &&
68 AMF_GET_SUBMINOR_VERSION(amf_device_ctx->
version) < 36)
81 amf_int64 color_profile;
88 ctx->resolution_changed = 0;
92 codec_id = AMFVideoDecoderUVD_H264_AVC;
95 codec_id = AMFVideoDecoderHW_H265_HEVC;
96 if(no_bitness_detect){
98 codec_id = AMFVideoDecoderHW_H265_MAIN10;
103 if(no_bitness_detect){
105 codec_id = AMFVideoDecoderHW_VP9_10BIT;
116 res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context,
codec_id, &
ctx->decoder);
122 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 1);
124 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 0);
127 if (color_profile != AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN)
128 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PROFILE, color_profile);
130 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->
color_trc);
133 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PRIMARIES, (amf_int64)avctx->
color_primaries);
135 if (
ctx->timestamp_mode != -1)
136 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_TIMESTAMP_MODE,
ctx->timestamp_mode);
137 if (
ctx->decoder_mode != -1)
138 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_REORDER_MODE,
ctx->decoder_mode);
139 if (
ctx->dpb_size != -1)
140 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_DPB_SIZE,
ctx->dpb_size);
141 if (
ctx->lowlatency != -1)
142 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
ctx->lowlatency);
143 if (
ctx->smart_access_video != -1) {
144 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_ENABLE_SMART_ACCESS_VIDEO,
ctx->smart_access_video != 0);
146 av_log(avctx,
AV_LOG_ERROR,
"The Smart Access Video is not supported by AMF decoder.\n");
151 if (
ctx->smart_access_video != 0) {
152 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
true);
153 av_log(avctx,
AV_LOG_INFO,
"The Smart Access Video set low latency mode for decoder.\n");
157 if (
ctx->skip_transfer_sav != -1)
158 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SKIP_TRANSFER_SMART_ACCESS_VIDEO,
ctx->skip_transfer_sav);
160 if (
ctx->copy_output != -1)
161 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_COPY,
ctx->copy_output);
164 const uint8_t *extradata;
167 res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST, extradata_size, &
buffer);
169 memcpy(
buffer->pVtbl->GetNative(
buffer), extradata, extradata_size);
170 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->decoder, AMF_VIDEO_DECODER_EXTRADATA,
buffer);
175 if (
ctx->surface_pool_size == -1) {
176 ctx->surface_pool_size = pool_size;
185 if (
ctx->surface_pool_size > 100)
186 ctx->surface_pool_size = 100;
188 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_POOL_SIZE,
ctx->surface_pool_size);
189 res =
ctx->decoder->pVtbl->Init(
ctx->decoder, AMF_SURFACE_UNKNOWN, avctx->
width, avctx->
height);
202 ctx->decoder->pVtbl->Terminate(
ctx->decoder);
203 ctx->decoder->pVtbl->Release(
ctx->decoder);
228 hwframes_ctx->
width = new_width;
229 hwframes_ctx->
height = new_height;
246 ctx->dimensions_initialized = 0;
285 AMFVariantStruct format_var = {0};
304 }
else if (avctx->
width > 0 && avctx->
height > 0) {
305 frames_w = avctx->
width;
327 AMFVariantStruct var;
328 res = AMFVariantInit(&var);
330 res =
object->pVtbl->GetProperty(
object,
name, &var);
332 if (var.type == AMF_VARIANT_INTERFACE) {
333 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
334 AMFInterface *amf_interface = AMFVariantInterface(&var);
335 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
337 res = AMF_INVALID_DATA_TYPE;
340 AMFVariantClear(&var);
347 AMFVariantStruct var = {0};
374 frame->data[0] = (uint8_t *)surface;
376 format_amf = surface->pVtbl->GetFormat(surface);
380 ret = surface->pVtbl->Convert(surface, AMF_MEMORY_HOST);
383 for (
i = 0;
i < surface->pVtbl->GetPlanesCount(surface);
i++) {
384 plane = surface->pVtbl->GetPlaneAt(surface,
i);
385 frame->data[
i] = plane->pVtbl->GetNative(plane);
386 frame->linesize[
i] = plane->pVtbl->GetHPitch(plane);
394 format_amf = surface->pVtbl->GetFormat(surface);
401 frame->pts = surface->pVtbl->GetPts(surface);
403 surface->pVtbl->GetProperty(surface,
L"FFMPEG:dts", &var);
404 frame->pkt_dts = var.int64Value;
406 frame->duration = surface->pVtbl->GetDuration(surface);
407 if (
frame->duration < 0)
416 AMFBuffer * hdrmeta_buffer =
NULL;
418 if (hdrmeta_buffer !=
NULL) {
419 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
434 AMF_RESULT
ret = AMF_OK;
435 AMFSurface *surface =
NULL;
436 AMFData *data_out =
NULL;
438 ret =
ctx->decoder->pVtbl->QueryOutput(
ctx->decoder, &data_out);
439 if (
ret != AMF_OK &&
ret != AMF_REPEAT) {
442 if (data_out ==
NULL) {
447 AMFGuid guid = IID_AMFSurface();
448 data_out->pVtbl->QueryInterface(data_out, &guid, (
void**)&surface);
449 data_out->pVtbl->Release(data_out);
459 surface->pVtbl->Release(surface);
473 AMF_ASSIGN_PROPERTY_INT64(res,
buffer,
L"FFMPEG:dts",
pkt->
dts);
484 AMFContext *ctxt = amf_device_ctx->context;
487 AMFBuffer *buf =
NULL;
495 err = buf->pVtbl->SetSize(buf,
pkt->
size);
496 AMF_RETURN_IF_FALSE(ctxt, err == AMF_OK, err,
"amf_buffer_from_packet() - SetSize failed");
498 mem = buf->pVtbl->GetNative(buf);
511 AMFVariantStruct size_var = {0};
512 AMF_RESULT res = AMF_OK;
514 res =
ctx->decoder->pVtbl->GetProperty(
ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
515 if (res == AMF_OK && size_var.sizeValue.width > 0 && size_var.sizeValue.height > 0) {
516 avctx->
width = size_var.sizeValue.width;
517 avctx->
height = size_var.sizeValue.height;
521 ctx->dimensions_initialized = 1;
540 if(
ctx->resolution_changed)
541 ctx->resolution_changed = 0;
550 ctx->decoder->pVtbl->Drain(
ctx->decoder);
561 res =
ctx->decoder->pVtbl->SubmitInput(
ctx->decoder, (AMFData*) buf);
562 if(res == AMF_DECODER_NO_FREE_SURFACES)
566 }
while (res == AMF_DECODER_NO_FREE_SURFACES);
568 buf->pVtbl->Release(buf);
570 if(res == AMF_DECODER_NO_FREE_SURFACES) {
572 av_log(avctx,
AV_LOG_VERBOSE,
"SubmitInput() returned NO_FREE_SURFACES and came out of loop - should never happen\n");
574 }
else if (res == AMF_RESOLUTION_CHANGED) {
576 ctx->decoder->pVtbl->Drain(
ctx->decoder);
579 ctx->resolution_changed = 1;
581 }
else if (res != AMF_OK && res != AMF_NEED_MORE_INPUT && res != AMF_REPEAT) {
590 if (!
ctx->dimensions_initialized)
592 }
else if (res == AMF_REPEAT)
595 else if (res == AMF_EOF) {
598 if(
ctx->resolution_changed){
600 AMFVariantStruct size_var = {0};
601 AMFVariantStruct format_var = {0};
602 res =
ctx->decoder->pVtbl->GetProperty(
ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
607 avctx->
width = size_var.sizeValue.width;
608 avctx->
height = size_var.sizeValue.height;
628 return got_frame ? 0 :
AVERROR(EAGAIN);
634 ctx->decoder->pVtbl->Flush(
ctx->decoder);
637 #define OFFSET(x) offsetof(AMFDecoderContext, x)
638 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
642 {
"decoder_mode",
"Decoder mode",
OFFSET(decoder_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_VIDEO_DECODER_MODE_LOW_LATENCY,
VD,
"decoder_mode" },
643 {
"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" },
644 {
"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" },
645 {
"low_latency",
"DPB delay is 0", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_LOW_LATENCY }, 0, 0,
VD,
"decoder_mode" },
648 {
"timestamp_mode",
"Timestamp mode",
OFFSET(timestamp_mode),
AV_OPT_TYPE_INT, { .i64 = AMF_TS_SORT }, -1, AMF_TS_DECODE,
VD,
"timestamp_mode" },
649 {
"presentation",
"Preserve timestamps from input to output", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_PRESENTATION }, 0, 0,
VD,
"timestamp_mode" },
650 {
"sort",
"Resort PTS list", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_SORT }, 0, 0,
VD,
"timestamp_mode" },
651 {
"decode",
"Decode order", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_DECODE }, 0, 0,
VD,
"timestamp_mode" },
654 {
"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 },
659 {
"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 },
672 #define DEFINE_AMF_DECODER(x, X, bsf_name) \
673 const FFCodec ff_##x##_amf_decoder = { \
674 .p.name = #x "_amf", \
675 CODEC_LONG_NAME(#X " AMD AMF video decoder"), \
676 .priv_data_size = sizeof(AMFDecoderContext), \
677 .p.type = AVMEDIA_TYPE_VIDEO, \
678 .p.id = AV_CODEC_ID_##X, \
679 .init = amf_decode_init, \
680 FF_CODEC_RECEIVE_FRAME_CB(amf_decode_frame), \
681 .flush = amf_decode_flush, \
682 .close = amf_decode_close, \
684 .p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
685 .p.priv_class = &amf_decode_class, \
686 .hw_configs = amf_hw_configs, \
687 .p.wrapper_name = "amf", \
688 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \