From 12dd81530dff95de4d5c44c2095f6a7df5f2642e Mon Sep 17 00:00:00 2001
From: Oliver Collyer <ovcollyer@mac.com>
Date: Sat, 9 Mar 2019 12:50:54 +0000
Subject: [PATCH 1/1] avcodec/aacenc: Validate and log bitrate changes made
 during encoding

---
 libavcodec/aacenc.c | 55 ++++++++++++++++++++++++++++++++-------------
 libavcodec/aacenc.h |  2 ++
 2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 4d0abb107f..bc15b99cbc 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -553,6 +553,31 @@ static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
     }
 }
 
+/**
+ * Validate bit rate.
+ */
+static void validate_bit_rate(AVCodecContext *avctx)
+{
+    AACEncContext *s = avctx->priv_data;
+
+    /* Bitrate default */
+    if (!avctx->bit_rate) {
+        for (int i = 1; i <= s->chan_map[0]; i++) {
+            avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
+                s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE  */
+                69000; /* SCE  */
+        }
+    }
+
+    /* Bitrate limiting */
+    WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
+        "Too many bits %f > %d per frame requested, clamping to max\n",
+        1024.0 * avctx->bit_rate / avctx->sample_rate,
+        6144 * s->channels);
+    avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
+        avctx->bit_rate);
+}
+
 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                             const AVFrame *frame, int *got_packet_ptr)
 {
@@ -567,6 +592,18 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     int chan_el_counter[4];
     FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
 
+    /* check for bit rate change */
+    if (avctx->bit_rate != s->last_bit_rate) {
+        validate_bit_rate(avctx);
+        if (avctx->bit_rate != s->last_bit_rate) {
+            av_log(avctx, AV_LOG_VERBOSE,
+                "bitrate change: %"PRIi64" -> %"PRIi64"\n",
+                s->last_bit_rate,
+                avctx->bit_rate);
+            s->last_bit_rate = avctx->bit_rate;
+        }
+    }
+
     /* add current frame to queue */
     if (frame) {
         if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
@@ -997,14 +1034,6 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
         s->chan_map = aac_chan_configs[s->channels - 1];
     }
 
-    if (!avctx->bit_rate) {
-        for (i = 1; i <= s->chan_map[0]; i++) {
-            avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
-                               s->chan_map[i] == TYPE_LFE ? 16000  : /* LFE  */
-                                                            69000  ; /* SCE  */
-        }
-    }
-
     /* Samplerate */
     for (i = 0; i < 16; i++)
         if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
@@ -1015,13 +1044,9 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
              s->samplerate_index >= ff_aac_swb_size_128_len,
              "Unsupported sample rate %d\n", avctx->sample_rate);
 
-    /* Bitrate limiting */
-    WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
-             "Too many bits %f > %d per frame requested, clamping to max\n",
-             1024.0 * avctx->bit_rate / avctx->sample_rate,
-             6144 * s->channels);
-    avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
-                                     avctx->bit_rate);
+    /* Bitrate */
+    validate_bit_rate(avctx);
+    s->last_bit_rate = avctx->bit_rate;
 
     /* Profile and option setting */
     avctx->profile = avctx->profile == FF_PROFILE_UNKNOWN ? FF_PROFILE_AAC_LOW :
diff --git a/libavcodec/aacenc.h b/libavcodec/aacenc.h
index 5a015ca92e..d53e24049f 100644
--- a/libavcodec/aacenc.h
+++ b/libavcodec/aacenc.h
@@ -418,6 +418,8 @@ typedef struct AACEncContext {
     struct {
         float *samples;
     } buffer;
+
+    int64_t last_bit_rate;
 } AACEncContext;
 
 void ff_aac_dsp_init_x86(AACEncContext *s);
-- 
2.20.1.windows.1

