From 4d0d20d96ad458cfec0444b9be0182ca6085ee0c Mon Sep 17 00:00:00 2001 From: 21pages Date: Sat, 24 Feb 2024 16:02:44 +0800 Subject: [PATCH 2/2] libavcodec/amfenc: reconfig when bitrate change Signed-off-by: 21pages --- libavcodec/amfenc.c | 20 ++++++++++++++++++++ libavcodec/amfenc.h | 1 + 2 files changed, 21 insertions(+) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index a033e1220e..3eab01a903 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -222,6 +222,7 @@ static int amf_init_context(AVCodecContext *avctx) ctx->hwsurfaces_in_queue = 0; ctx->hwsurfaces_in_queue_max = 16; + ctx->av_bitrate = avctx->bit_rate; // configure AMF logger // the return of these functions indicates old state and do not affect behaviour @@ -575,6 +576,23 @@ static void amf_release_buffer_with_frame_ref(AMFBuffer *frame_ref_storage_buffe frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer); } +static int reconfig_encoder(AVCodecContext *avctx) +{ + AmfContext *ctx = avctx->priv_data; + AMF_RESULT res = AMF_OK; + + if (ctx->av_bitrate != avctx->bit_rate) { + av_log(ctx, AV_LOG_INFO, "change bitrate from %d to %d\n", ctx->av_bitrate, avctx->bit_rate); + ctx->av_bitrate = avctx->bit_rate; + if (avctx->codec->id == AV_CODEC_ID_H264) { + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_TARGET_BITRATE, avctx->bit_rate); + } else if (avctx->codec->id == AV_CODEC_ID_HEVC) { + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, avctx->bit_rate); + } + } + return 0; +} + int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) { AmfContext *ctx = avctx->priv_data; @@ -586,6 +604,8 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) AVFrame *frame = ctx->delayed_frame; int block_and_wait; + reconfig_encoder(avctx); + if (!ctx->encoder) return AVERROR(EINVAL); diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h index e92120ea39..31172645f2 100644 --- a/libavcodec/amfenc.h +++ b/libavcodec/amfenc.h @@ -107,6 +107,7 @@ typedef struct AmfContext { int me_half_pel; int me_quarter_pel; int aud; + int64_t av_bitrate; // HEVC - specific options -- 2.43.0.windows.1