]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegaudiodec.c
remove invalid free() forgotten in r12300
[ffmpeg] / libavcodec / mpegaudiodec.c
index bfd54d535ab579337b4a4c2abe8187778f4e0372..98f7f3b5ec499a662543c8f9621e77f704e8415a 100644 (file)
@@ -46,9 +46,6 @@
 
 #include "mathops.h"
 
-#define FRAC_ONE    (1 << FRAC_BITS)
-
-#define FIX(a)   ((int)((a) * FRAC_ONE))
 /* WARNING: only correct for posititive numbers */
 #define FIXR(a)   ((int)((a) * FRAC_ONE + 0.5))
 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
@@ -88,16 +85,6 @@ typedef struct GranuleDef {
     int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
 } GranuleDef;
 
-#define MODE_EXT_MS_STEREO 2
-#define MODE_EXT_I_STEREO  1
-
-/* layer 3 huffman tables */
-typedef struct HuffTable {
-    int xsize;
-    const uint8_t *bits;
-    const uint16_t *codes;
-} HuffTable;
-
 #include "mpegaudiodata.h"
 #include "mpegaudiodectab.h"
 
@@ -139,6 +126,68 @@ static const int32_t scale_factor_mult2[3][3] = {
 
 static DECLARE_ALIGNED_16(MPA_INT, window[512]);
 
+/**
+ * Convert region offsets to region sizes and truncate
+ * size to big_values.
+ */
+void ff_region_offset2size(GranuleDef *g){
+    int i, k, j=0;
+    g->region_size[2] = (576 / 2);
+    for(i=0;i<3;i++) {
+        k = FFMIN(g->region_size[i], g->big_values);
+        g->region_size[i] = k - j;
+        j = k;
+    }
+}
+
+void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
+    if (g->block_type == 2)
+        g->region_size[0] = (36 / 2);
+    else {
+        if (s->sample_rate_index <= 2)
+            g->region_size[0] = (36 / 2);
+        else if (s->sample_rate_index != 8)
+            g->region_size[0] = (54 / 2);
+        else
+            g->region_size[0] = (108 / 2);
+    }
+    g->region_size[1] = (576 / 2);
+}
+
+void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
+    int l;
+    g->region_size[0] =
+        band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
+    /* should not overflow */
+    l = FFMIN(ra1 + ra2 + 2, 22);
+    g->region_size[1] =
+        band_index_long[s->sample_rate_index][l] >> 1;
+}
+
+void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
+    if (g->block_type == 2) {
+        if (g->switch_point) {
+            /* if switched mode, we handle the 36 first samples as
+                long blocks.  For 8000Hz, we handle the 48 first
+                exponents as long blocks (XXX: check this!) */
+            if (s->sample_rate_index <= 2)
+                g->long_end = 8;
+            else if (s->sample_rate_index != 8)
+                g->long_end = 6;
+            else
+                g->long_end = 4; /* 8000 Hz */
+
+            g->short_start = 2 + (s->sample_rate_index != 8);
+        } else {
+            g->long_end = 0;
+            g->short_start = 0;
+        }
+    } else {
+        g->short_start = 13;
+        g->long_end = 22;
+    }
+}
+
 /* layer 1 unscaling */
 /* n = number of bits of the mantissa minus 1 */
 static inline int l1_unscale(int n, int mant, int scale_factor)
@@ -832,10 +881,7 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
 #if FRAC_BITS <= 15
         /* NOTE: can cause a loss in precision if very high amplitude
            sound */
-        if (v > 32767)
-            v = 32767;
-        else if (v < -32768)
-            v = -32768;
+        v = av_clip_int16(v);
 #endif
         synth_buf[j] = v;
     }
@@ -2012,32 +2058,21 @@ static int mp_decode_layer3(MPADecodeContext *s)
                 g->scalefac_compress = get_bits(&s->gb, 9);
             else
                 g->scalefac_compress = get_bits(&s->gb, 4);
-            blocksplit_flag = get_bits(&s->gb, 1);
+            blocksplit_flag = get_bits1(&s->gb);
             if (blocksplit_flag) {
                 g->block_type = get_bits(&s->gb, 2);
                 if (g->block_type == 0){
                     av_log(NULL, AV_LOG_ERROR, "invalid block type\n");
                     return -1;
                 }
-                g->switch_point = get_bits(&s->gb, 1);
+                g->switch_point = get_bits1(&s->gb);
                 for(i=0;i<2;i++)
                     g->table_select[i] = get_bits(&s->gb, 5);
                 for(i=0;i<3;i++)
                     g->subblock_gain[i] = get_bits(&s->gb, 3);
-                /* compute huffman coded region sizes */
-                if (g->block_type == 2)
-                    g->region_size[0] = (36 / 2);
-                else {
-                    if (s->sample_rate_index <= 2)
-                        g->region_size[0] = (36 / 2);
-                    else if (s->sample_rate_index != 8)
-                        g->region_size[0] = (54 / 2);
-                    else
-                        g->region_size[0] = (108 / 2);
-                }
-                g->region_size[1] = (576 / 2);
+                ff_init_short_region(s, g);
             } else {
-                int region_address1, region_address2, l;
+                int region_address1, region_address2;
                 g->block_type = 0;
                 g->switch_point = 0;
                 for(i=0;i<3;i++)
@@ -2047,53 +2082,16 @@ static int mp_decode_layer3(MPADecodeContext *s)
                 region_address2 = get_bits(&s->gb, 3);
                 dprintf(s->avctx, "region1=%d region2=%d\n",
                         region_address1, region_address2);
-                g->region_size[0] =
-                    band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
-                l = region_address1 + region_address2 + 2;
-                /* should not overflow */
-                if (l > 22)
-                    l = 22;
-                g->region_size[1] =
-                    band_index_long[s->sample_rate_index][l] >> 1;
-            }
-            /* convert region offsets to region sizes and truncate
-               size to big_values */
-            g->region_size[2] = (576 / 2);
-            j = 0;
-            for(i=0;i<3;i++) {
-                k = FFMIN(g->region_size[i], g->big_values);
-                g->region_size[i] = k - j;
-                j = k;
-            }
-
-            /* compute band indexes */
-            if (g->block_type == 2) {
-                if (g->switch_point) {
-                    /* if switched mode, we handle the 36 first samples as
-                       long blocks.  For 8000Hz, we handle the 48 first
-                       exponents as long blocks (XXX: check this!) */
-                    if (s->sample_rate_index <= 2)
-                        g->long_end = 8;
-                    else if (s->sample_rate_index != 8)
-                        g->long_end = 6;
-                    else
-                        g->long_end = 4; /* 8000 Hz */
-
-                    g->short_start = 2 + (s->sample_rate_index != 8);
-                } else {
-                    g->long_end = 0;
-                    g->short_start = 0;
-                }
-            } else {
-                g->short_start = 13;
-                g->long_end = 22;
+                ff_init_long_region(s, g, region_address1, region_address2);
             }
+            ff_region_offset2size(g);
+            ff_compute_band_indexes(s, g);
 
             g->preflag = 0;
             if (!s->lsf)
-                g->preflag = get_bits(&s->gb, 1);
-            g->scalefac_scale = get_bits(&s->gb, 1);
-            g->count1table_select = get_bits(&s->gb, 1);
+                g->preflag = get_bits1(&s->gb);
+            g->scalefac_scale = get_bits1(&s->gb);
+            g->count1table_select = get_bits1(&s->gb);
             dprintf(s->avctx, "block_type=%d switch_point=%d\n",
                     g->block_type, g->switch_point);
         }
@@ -2298,7 +2296,7 @@ static int mp_decode_frame(MPADecodeContext *s,
 
     /* skip error protection field */
     if (s->error_protection)
-        get_bits(&s->gb, 16);
+        skip_bits(&s->gb, 16);
 
     dprintf(s->avctx, "frame %d:\n", s->frame_count);
     switch(s->layer) {
@@ -2369,7 +2367,7 @@ static int mp_decode_frame(MPADecodeContext *s,
 
 static int decode_frame(AVCodecContext * avctx,
                         void *data, int *data_size,
-                        uint8_t * buf, int buf_size)
+                        const uint8_t * buf, int buf_size)
 {
     MPADecodeContext *s = avctx->priv_data;
     uint32_t header;
@@ -2380,7 +2378,7 @@ retry:
     if(buf_size < HEADER_SIZE)
         return -1;
 
-    header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
+    header = AV_RB32(buf);
     if(ff_mpa_check_header(header) < 0){
         buf++;
 //        buf_size--;
@@ -2417,6 +2415,7 @@ retry:
         return -1;
     }else if(s->frame_size < buf_size){
         av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
+        buf_size= s->frame_size;
     }
 
     out_size = mp_decode_frame(s, out_samples, buf, buf_size);
@@ -2432,13 +2431,14 @@ retry:
 
 static void flush(AVCodecContext *avctx){
     MPADecodeContext *s = avctx->priv_data;
+    memset(s->synth_buf, 0, sizeof(s->synth_buf));
     s->last_buf_size= 0;
 }
 
 #ifdef CONFIG_MP3ADU_DECODER
 static int decode_frame_adu(AVCodecContext * avctx,
                         void *data, int *data_size,
-                        uint8_t * buf, int buf_size)
+                        const uint8_t * buf, int buf_size)
 {
     MPADecodeContext *s = avctx->priv_data;
     uint32_t header;
@@ -2458,7 +2458,7 @@ static int decode_frame_adu(AVCodecContext * avctx,
         len = MPA_MAX_CODED_FRAME_SIZE;
 
     // Get header and restore sync word
-    header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
+    header = AV_RB32(buf) | 0xffe00000;
 
     if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
         *data_size = 0;
@@ -2524,7 +2524,7 @@ static int decode_init_mp3on4(AVCodecContext * avctx)
     /* Init the first mp3 decoder in standard way, so that all tables get builded
      * We replace avctx->priv_data with the context of the first decoder so that
      * decode_init() does not have to be changed.
-     * Other decoders will be inited here copying data from the first context
+     * Other decoders will be initialized here copying data from the first context
      */
     // Allocate zeroed memory for the first decoder context
     s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
@@ -2564,7 +2564,7 @@ static int decode_close_mp3on4(AVCodecContext * avctx)
 
 static int decode_frame_mp3on4(AVCodecContext * avctx,
                         void *data, int *data_size,
-                        uint8_t * buf, int buf_size)
+                        const uint8_t * buf, int buf_size)
 {
     MP3On4DecodeContext *s = avctx->priv_data;
     MPADecodeContext *m;
@@ -2574,7 +2574,7 @@ static int decode_frame_mp3on4(AVCodecContext * avctx,
     OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
     OUT_INT *outptr, *bp;
     int fsize;
-    unsigned char *start2 = buf, *start;
+    const unsigned char *start2 = buf, *start;
     int fr, i, j, n;
     int off = avctx->channels;
     int *coff = chan_offset[s->chan_cfg];
@@ -2603,7 +2603,7 @@ static int decode_frame_mp3on4(AVCodecContext * avctx,
         assert (m != NULL);
 
         // Get header
-        header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
+        header = AV_RB32(start) | 0xfff00000;
 
         if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
             *data_size = 0;
@@ -2657,6 +2657,7 @@ AVCodec mp2_decoder =
     NULL,
     decode_frame,
     CODEC_CAP_PARSE_ONLY,
+    .flush= flush,
 };
 #endif
 #ifdef CONFIG_MP3_DECODER