shithub: aacenc

Download patch

ref: aa1eaee4022d8cc79b9d83bd3aae64769b433cc9
parent: 58f5370af92b2bed578093db57e1868a6bc80c39
author: Krzysztof Nikiel <knik@users.sourceforge.net>
date: Thu Nov 2 15:21:03 EDT 2017

calculate max bitrate according to the specs

--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+	* fixed max MP4 bitrate calculation
 1.29.9:
 	* initial version of PNS coding
 	* Intensity Stereo coding
--- a/frontend/main.c
+++ b/frontend/main.c
@@ -1205,11 +1205,11 @@
 
         if (verbose >= 2)
         {
-        fprintf(stderr, "%u frames\n", mp4config.frame.ents);
-        fprintf(stderr, "%u output samples\n", mp4config.samples);
-        fprintf(stderr, "max bitrate: %u\n", mp4config.bitratemax);
-        fprintf(stderr, "avg bitrate: %u\n", mp4config.bitrateavg);
-        fprintf(stderr, "max frame size: %u\n", mp4config.buffersize);
+            fprintf(stderr, "%u frames\n", mp4config.frame.ents);
+            fprintf(stderr, "%u output samples\n", mp4config.samples);
+            fprintf(stderr, "max bitrate: %u\n", mp4config.bitrate.max);
+            fprintf(stderr, "avg bitrate: %u\n", mp4config.bitrate.avg);
+            fprintf(stderr, "max frame size: %u\n", mp4config.buffersize);
         }
     }
     else
--- a/frontend/mp4write.c
+++ b/frontend/mp4write.c
@@ -413,8 +413,8 @@
     size += u8out(0);
 #endif
     // bitrate
-    size += u32out(mp4config.bitratemax);
-    size += u32out(mp4config.bitrateavg);
+    size += u32out(mp4config.bitrate.max);
+    size += u32out(mp4config.bitrate.avg);
 
     size += u8out(TAG_DSI);
     size += u8out(0x80);
@@ -794,11 +794,21 @@
 {
     if (mp4config.framesamples <= samples)
     {
-        int bitrate = 8.0 * size * mp4config.samplerate / samples;
+        int bitrate;
 
-        if (mp4config.bitratemax < bitrate)
-            mp4config.bitratemax = bitrate;
+        mp4config.bitrate.samples += samples;
+        mp4config.bitrate.size += size;
 
+        if (mp4config.bitrate.samples >= mp4config.samplerate)
+        {
+            bitrate = 8.0 * mp4config.bitrate.size * mp4config.samplerate
+                / mp4config.bitrate.samples;
+            mp4config.bitrate.size = 0;
+            mp4config.bitrate.samples = 0;
+
+            if (mp4config.bitrate.max < bitrate)
+                mp4config.bitrate.max = bitrate;
+        }
         mp4config.framesamples = samples;
     }
     if (mp4config.buffersize < size)
@@ -870,8 +880,10 @@
 
 int mp4atom_tail(void)
 {
-    mp4config.bitrateavg = 8.0 * mp4config.mdatsize
+    mp4config.bitrate.avg = 8.0 * mp4config.mdatsize
         * mp4config.samplerate / mp4config.samples;
+    if (!mp4config.bitrate.max)
+        mp4config.bitrate.max = mp4config.bitrate.avg;
 
     g_atom = g_tail;
     while (g_atom->opcode != ATOM_STOP)
--- a/frontend/mp4write.h
+++ b/frontend/mp4write.h
@@ -31,8 +31,12 @@
     uint32_t bits;
     // buffer config
     uint16_t buffersize;
-    uint32_t bitratemax;
-    uint32_t bitrateavg;
+    struct {
+        uint32_t max;
+        uint32_t avg;
+        int size;
+        int samples;
+    } bitrate;
     uint32_t framesamples;
     struct
     {