[FFmpeg-devel] [PATCH 13/36] avcodec/mp3_header_decompress_bsf: Improve readability

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Sat May 30 19:05:18 EEST 2020


Up until now, the mp3_header_decompress bsf used a single variable for
two purposes in a bitfield-like manner: To contain the bitrate index
as well as whether the padding_bit is set. The former was contained in
bits 1-4, while the latter was given by bit 0. This made the code hard
to understand.

This commit changes this by using dedicated variables for the
bitrate index as well as for the offset of the old data in the new
buffer. The latter implicitly contains whether the padding_bit is set.

Also add const to the pointer to the source data; it is of course not
modified.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mp3_header_decompress_bsf.c | 35 ++++++++++++++------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/libavcodec/mp3_header_decompress_bsf.c b/libavcodec/mp3_header_decompress_bsf.c
index fe021deed3..3d357dd27e 100644
--- a/libavcodec/mp3_header_decompress_bsf.c
+++ b/libavcodec/mp3_header_decompress_bsf.c
@@ -32,8 +32,8 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
     uint32_t header;
     int sample_rate= ctx->par_in->sample_rate;
     int sample_rate_index=0;
-    int lsf, mpeg25, bitrate_index, frame_size, ret;
-    uint8_t *buf;
+    int lsf, mpeg25, bitrate_index, frame_size, offset, ret;
+    const uint8_t *buf;
     int buf_size;
 
     ret = ff_bsf_get_packet(ctx, &in);
@@ -69,23 +69,25 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
 
     sample_rate= avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
 
-    for(bitrate_index=2; bitrate_index<30; bitrate_index++){
-        frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index>>1];
-        frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
-        if(frame_size == buf_size + 4)
-            break;
-        if(frame_size == buf_size + 6)
+    for (bitrate_index = 1; bitrate_index < 15; bitrate_index++) {
+        frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index];
+        frame_size = (frame_size * 144000) / (sample_rate << lsf);
+        offset = frame_size - buf_size;
+        if (3 <= offset && offset <= 6)
             break;
     }
-    if(bitrate_index == 30){
+    if (bitrate_index == 15) {
         av_log(ctx, AV_LOG_ERROR, "Could not find bitrate_index.\n");
         ret = AVERROR(EINVAL);
         goto fail;
     }
 
-    header |= (bitrate_index&1)<<9;
-    header |= (bitrate_index>>1)<<12;
-    header |= (frame_size == buf_size + 4)<<16;
+    header |= bitrate_index << 12;
+    if (offset & 1) {
+        header |= 1 << 9; // padding_bit
+        frame_size++;
+        offset++;
+    }
 
     ret = av_new_packet(out, frame_size);
     if (ret < 0)
@@ -95,10 +97,10 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
         av_packet_unref(out);
         goto fail;
     }
-    memcpy(out->data + frame_size - buf_size, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
+    memcpy(out->data + offset, buf, buf_size);
 
     if(ctx->par_in->channels==2){
-        uint8_t *p= out->data + frame_size - buf_size;
+        uint8_t *p = out->data + offset;
         if(lsf){
             FFSWAP(int, p[1], p[2]);
             header |= (p[1] & 0xC0)>>2;
@@ -109,10 +111,11 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
         }
     }
 
-    if (frame_size == buf_size + 6) {
+    if (offset == 6) {
         //FIXME actually set a correct crc instead of 0
         AV_WN16(out->data + 4, 0);
-    }
+    } else
+        header |= 1 << 16; // protection_bit
     AV_WB32(out->data, header);
 
     ret = 0;
-- 
2.20.1



More information about the ffmpeg-devel mailing list