[FFmpeg-devel] [PATCH 3/4] lavf/mp3enc: write encoder delay/padding upon closing

Jon Toohill jtoohill at google.com
Wed Sep 28 21:29:02 EEST 2016


trailing_padding is not known before encoding.
---
 libavformat/mp3enc.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index de63401..37608f1 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -247,12 +247,7 @@ static int mp3_write_xing(AVFormatContext *s)
     ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
     avio_w8(dyn_ctx, 0);      // unknown encoding flags
     avio_w8(dyn_ctx, 0);      // unknown abr/minimal bitrate
-
-    // encoder delay
-    if (par->initial_padding - 528 - 1 >= 1 << 12) {
-        av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
-    }
-    avio_wb24(dyn_ctx, FFMAX(par->initial_padding - 528 - 1, 0)<<12);
+    avio_wb24(dyn_ctx, 0);    // empty encoder delay/padding
 
     avio_w8(dyn_ctx,   0); // misc
     avio_w8(dyn_ctx,   0); // mp3gain
@@ -381,7 +376,7 @@ static void mp3_update_xing(AVFormatContext *s)
     AVReplayGain *rg;
     uint16_t tag_crc;
     uint8_t *toc;
-    int i, rg_size;
+    int i, rg_size, delay, padding;
 
     /* replace "Xing" identification string with "Info" for CBR files. */
     if (!mp3->has_variable_bitrate)
@@ -422,6 +417,17 @@ static void mp3_update_xing(AVFormatContext *s)
         }
     }
 
+    /* write encoder delay/padding */
+    delay = FFMAX(s->streams[0]->codecpar->initial_padding - 528 - 1, 0);
+    padding = s->streams[0]->codecpar->trailing_padding;
+    if (delay >= 1 << 12) {
+        av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
+    }
+    if (padding >= 1 << 12) {
+        av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
+    }
+    AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (delay << 12) + padding);
+
     AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
     AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
 
-- 
2.8.0.rc3.226.g39d4020



More information about the ffmpeg-devel mailing list