summaryrefslogtreecommitdiff
path: root/libavcodec/aacenc.c
diff options
context:
space:
mode:
authorJeremy Wu <jrwu@google.com>2023-04-25 06:09:52 +0000
committerLynne <dev@lynne.ee>2023-06-04 03:36:10 +0200
commitb92af7b64e7acd015aea3ae1da2228c5a7e677bf (patch)
treee6a0f579e41f53aa784a466bf4a726bd346137a0 /libavcodec/aacenc.c
parent378fb4028294bcf27df09d145a1f6ab1d014924e (diff)
avcodec/aacenc: add strict bit rate control option
In certain use cases, controlling the maximum frame size is critical. An example is when transmitting AAC packets over Bluetooth A2DP. While the spec allows the packets to be fragmented (but UNRECOMMENDED), in practice most headsets do not recognize nor reassemble such packets. In this patch, we allow setting `bit_rate_tolerance` to 0 to indicate that the specified bit rate should be treated as an upper bound up to frame level. Signed-off-by: Jeremy Wu <jrwu@chromium.org>
Diffstat (limited to 'libavcodec/aacenc.c')
-rw-r--r--libavcodec/aacenc.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index ed036209e9..f48f057022 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -1106,6 +1106,18 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
+ /* When strict bit-rate control is demanded */
+ if (avctx->bit_rate_tolerance == 0) {
+ if (rate_bits < frame_bits) {
+ float ratio = ((float)rate_bits) / frame_bits;
+ s->lambda *= FFMIN(0.9f, ratio);
+ continue;
+ }
+ /* reset lambda when solution is found */
+ s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
+ break;
+ }
+
/* When using ABR, be strict (but only for increasing) */
too_few_bits = too_few_bits - too_few_bits/8;
too_many_bits = too_many_bits + too_many_bits/2;