Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v2] avcodec/aacenc: add strict bit rate control option
@ 2023-04-25  6:09 Jeremy Wu
  2023-05-19  4:04 ` Jeremy Wu
  0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Wu @ 2023-04-25  6:09 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Jeremy Wu, Jeremy Wu

From: Jeremy Wu <jrwu@google.com>

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>
---
 libavcodec/aacenc.c        | 12 ++++++++++++
 libavcodec/options_table.h |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)

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;
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index f331ce2861..716787aa59 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -50,7 +50,7 @@ static const AVOption avcodec_options[] = {
 {"bt", "Set video bitrate tolerance (in bits/s). In 1-pass mode, bitrate tolerance specifies how far "
        "ratecontrol is willing to deviate from the target average bitrate value. This is not related "
        "to minimum/maximum bitrate. Lowering tolerance too much has an adverse effect on quality.",
-       OFFSET(bit_rate_tolerance), AV_OPT_TYPE_INT, {.i64 = AV_CODEC_DEFAULT_BITRATE*20 }, 1, INT_MAX, V|E},
+       OFFSET(bit_rate_tolerance), AV_OPT_TYPE_INT, {.i64 = AV_CODEC_DEFAULT_BITRATE*20 }, 0, INT_MAX, A|V|E},
 {"flags", NULL, OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT }, 0, UINT_MAX, V|A|S|E|D, "flags"},
 {"unaligned", "allow decoders to produce unaligned output", 0, AV_OPT_TYPE_CONST, { .i64 = AV_CODEC_FLAG_UNALIGNED }, INT_MIN, INT_MAX, V | D, "flags" },
 {"mv4", "use four motion vectors per macroblock (MPEG-4)", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_4MV }, INT_MIN, INT_MAX, V|E, "flags"},
-- 
2.40.0.634.g4ca3ef3211-goog

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2] avcodec/aacenc: add strict bit rate control option
  2023-04-25  6:09 [FFmpeg-devel] [PATCH v2] avcodec/aacenc: add strict bit rate control option Jeremy Wu
@ 2023-05-19  4:04 ` Jeremy Wu
  2023-06-04  1:42   ` Lynne
  0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Wu @ 2023-05-19  4:04 UTC (permalink / raw)
  To: ffmpeg-devel

Friendly ping for review, thanks!

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2] avcodec/aacenc: add strict bit rate control option
  2023-05-19  4:04 ` Jeremy Wu
@ 2023-06-04  1:42   ` Lynne
  0 siblings, 0 replies; 3+ messages in thread
From: Lynne @ 2023-06-04  1:42 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

May 19, 2023, 06:05 by jrwu@chromium.org:

> Friendly ping for review, thanks!
>

Patch applied, sorry for the delay.
Had my open tab for the patch buried very deep.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-04  1:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-25  6:09 [FFmpeg-devel] [PATCH v2] avcodec/aacenc: add strict bit rate control option Jeremy Wu
2023-05-19  4:04 ` Jeremy Wu
2023-06-04  1:42   ` Lynne

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