From: Dmitrii Ovchinnikov <ovchinnikov.dmitrii@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Dmitrii Ovchinnikov <ovchinnikov.dmitrii@gmail.com> Subject: [FFmpeg-devel] [PATCH] avcodec/amfenc_h264: improve B-frame usability and simplify options Date: Fri, 16 May 2025 13:29:14 +0200 Message-ID: <20250516112914.2038-1-ovchinnikov.dmitrii@gmail.com> (raw) - Query GPU caps for B-frame support and warn if unsupported. - Make `-max_b_frames` optional - Drop explicit `-pa_lookahead_buffer_depth` requirement in adaptive mode. --- libavcodec/amfenc_h264.c | 69 ++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/libavcodec/amfenc_h264.c b/libavcodec/amfenc_h264.c index cfcc5482f0..260139f14f 100644 --- a/libavcodec/amfenc_h264.c +++ b/libavcodec/amfenc_h264.c @@ -468,26 +468,61 @@ static av_cold int amf_encode_init_h264(AVCodecContext *avctx) } // B-Frames - if (ctx->max_consecutive_b_frames != -1) { - AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_MAX_CONSECUTIVE_BPICTURES, ctx->max_consecutive_b_frames); - if (ctx->max_b_frames != -1) { - AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_PATTERN, ctx->max_b_frames); - if (res != AMF_OK) { - res = ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_PATTERN, &var); - av_log(ctx, AV_LOG_WARNING, "B-frames=%d is not supported by this GPU, switched to %d\n", - ctx->max_b_frames, (int)var.int64Value); - ctx->max_b_frames = (int)var.int64Value; + AMFVariantStruct is_adaptive_b_frames = { 0 }; + res = ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_ADAPTIVE_MINIGOP, &is_adaptive_b_frames); + if (ctx->max_consecutive_b_frames != -1 || ctx->max_b_frames != -1 || is_adaptive_b_frames.boolValue == true) { + + //Get the capability of encoder + AMFCaps *encoder_caps = NULL; + ctx->encoder->pVtbl->GetCaps(ctx->encoder, &encoder_caps); + if (encoder_caps != NULL) + { + res = encoder_caps->pVtbl->GetProperty(encoder_caps, AMF_VIDEO_ENCODER_CAP_BFRAMES, &var); + if (res == AMF_OK) { + + //encoder supports H.264 B-frame + if(var.boolValue == true){ + //adaptive b-frames is higher priority than max_b_frames + if (is_adaptive_b_frames.boolValue == true) + { + //force AMF_VIDEO_ENCODER_MAX_CONSECUTIVE_BPICTURES to 3 + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_MAX_CONSECUTIVE_BPICTURES, 3); + + if(ctx->pa_lookahead_buffer_depth < 1) + { + //force AMF_PA_LOOKAHEAD_BUFFER_DEPTH to 1 if not set or smaller than 1 + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_PA_LOOKAHEAD_BUFFER_DEPTH, 1); + } + } + else { + if (ctx->max_b_frames != -1) { + //in case user sets B-frames + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_PATTERN, ctx->max_b_frames); + if (res != AMF_OK) { + res = ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_PATTERN, &var); + av_log(ctx, AV_LOG_WARNING, "B-frames=%d is not supported by this GPU, switched to %d\n", ctx->max_b_frames, (int)var.int64Value); + ctx->max_b_frames = (int)var.int64Value; + } + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_MAX_CONSECUTIVE_BPICTURES, ctx->max_b_frames); + } + } + + } + //encoder doesn't support H.264 B-frame + else { + av_log(ctx, AV_LOG_WARNING, "The current GPU in use does not support H.264 B-frame encoding, there will be no B-frame in bitstream.\n"); + } + } else { + //Can't get the capability of encoder + av_log(ctx, AV_LOG_WARNING, "Unable to get H.264 B-frame capability.\n"); + av_log(ctx, AV_LOG_WARNING, "There will be no B-frame in bitstream.\n"); } - if (ctx->max_consecutive_b_frames < ctx->max_b_frames) { - av_log(ctx, AVERROR_BUG, "Maxium B frames needs to be greater than the specified B frame count.\n"); - } - } - } - else { - if (ctx->max_b_frames != -1) { - av_log(ctx, AVERROR_BUG, "Maxium number of B frames needs to be specified.\n"); + + encoder_caps->pVtbl->Release(encoder_caps); + encoder_caps = NULL; } } + res = ctx->encoder->pVtbl->GetProperty(ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_PATTERN, &var); if ((int)var.int64Value) { AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_B_PIC_DELTA_QP, ctx->b_frame_delta_qp); -- 2.47.1.windows.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
reply other threads:[~2025-05-16 11:29 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250516112914.2038-1-ovchinnikov.dmitrii@gmail.com \ --to=ovchinnikov.dmitrii@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git