]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegaudiodec.c
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless...
[ffmpeg] / libavcodec / mpegaudiodec.c
index 75bc858f8923d00b5a159dd0579248d31de5f842..9a066c905bd9762a3ba792f79a077405e5be8acd 100644 (file)
@@ -301,7 +301,7 @@ static int int_pow(int i, int *exp_ptr)
 static int decode_init(AVCodecContext * avctx)
 {
     MPADecodeContext *s = avctx->priv_data;
-    static int init;
+    static int init=0;
     int i, j, k;
 
     if(!init) {
@@ -379,17 +379,13 @@ static int decode_init(AVCodecContext * avctx)
             band_index_long[i][22] = k;
         }
 
-        /* compute n ^ (4/3) and store it in mantissa/exp format */
-        table_4_3_exp = av_mallocz(TABLE_4_3_SIZE * 
-                                   sizeof(table_4_3_exp[0]));
-        if (!table_4_3_exp)
+       /* compute n ^ (4/3) and store it in mantissa/exp format */
+       if (!av_mallocz_static(&table_4_3_exp,
+                              TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])))
+           return -1;
+       if (!av_mallocz_static(&table_4_3_value,
+                              TABLE_4_3_SIZE * sizeof(table_4_3_value[0])))
             return -1;
-        table_4_3_value = av_mallocz(TABLE_4_3_SIZE * 
-                                     sizeof(table_4_3_value[0]));
-        if (!table_4_3_value) {
-            av_free(table_4_3_exp);
-            return -1;
-        }
         
         int_pow_init();
         for(i=1;i<TABLE_4_3_SIZE;i++) {
@@ -511,7 +507,7 @@ static int decode_init(AVCodecContext * avctx)
     return 0;
 }
 
-/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
+/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
 
 /* cos(i*pi/64) */
 
@@ -1457,17 +1453,14 @@ static void seek_to_maindata(MPADecodeContext *s, long backstep)
     UINT8 *ptr;
 
     /* compute current position in stream */
-#ifdef ALT_BITSTREAM_READER
-    ptr = s->gb.buffer + (s->gb.index>>3);
-#else
-    ptr = s->gb.buf_ptr - (s->gb.bit_cnt >> 3);
-#endif    
+    ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
+
     /* copy old data before current one */
     ptr -= backstep;
     memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
            BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
     /* init get bits again */
-    init_get_bits(&s->gb, ptr, s->frame_size + backstep);
+    init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
 
     /* prepare next buffer */
     s->inbuf_index ^= 1;
@@ -1547,9 +1540,7 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
 {
     int s_index;
     int linbits, code, x, y, l, v, i, j, k, pos;
-    UINT8 *last_buf_ptr;
-    UINT32 last_bit_buf;
-    int last_bit_cnt;
+    GetBitContext last_gb;
     VLC *vlc;
     UINT8 *code_table;
 
@@ -1608,36 +1599,20 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
             
     /* high frequencies */
     vlc = &huff_quad_vlc[g->count1table_select];
-    last_buf_ptr = NULL;
-    last_bit_buf = 0;
-    last_bit_cnt = 0;
+    last_gb.buffer = NULL;
     while (s_index <= 572) {
         pos = get_bits_count(&s->gb);
         if (pos >= end_pos) {
-            if (pos > end_pos && last_buf_ptr != NULL) {
+            if (pos > end_pos && last_gb.buffer != NULL) {
                 /* some encoders generate an incorrect size for this
                    part. We must go back into the data */
                 s_index -= 4;
-#ifdef ALT_BITSTREAM_READER
-                s->gb.buffer = last_buf_ptr;
-                s->gb.index = last_bit_cnt;
-#else
-                s->gb.buf_ptr = last_buf_ptr;
-                s->gb.bit_buf = last_bit_buf;
-                s->gb.bit_cnt = last_bit_cnt;
-#endif            
+                s->gb = last_gb;
             }
             break;
         }
-#ifdef ALT_BITSTREAM_READER
-        last_buf_ptr = s->gb.buffer;
-        last_bit_cnt = s->gb.index;
-#else
-        last_buf_ptr = s->gb.buf_ptr;
-        last_bit_buf = s->gb.bit_buf;
-        last_bit_cnt = s->gb.bit_cnt;
-#endif
-        
+        last_gb= s->gb;
+
         code = get_vlc(&s->gb, vlc);
         dprintf("t=%d code=%d\n", g->count1table_select, code);
         if (code < 0)
@@ -2305,7 +2280,7 @@ static int mp_decode_frame(MPADecodeContext *s,
     short *samples_ptr;
 
     init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
-                  s->inbuf_ptr - s->inbuf - HEADER_SIZE);
+                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
     
     /* skip error protection field */
     if (s->error_protection)
@@ -2418,8 +2393,10 @@ static int decode_frame(AVCodecContext * avctx,
            if (len > buf_size)
                len = buf_size;
             if (len == 0) {
-                /* frame too long: resync */
+               /* frame too long: resync */
                 s->frame_size = 0;
+               memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
+               s->inbuf_ptr--;
             } else {
                 UINT8 *p, *pend;
                 UINT32 header1;
@@ -2481,6 +2458,7 @@ static int decode_frame(AVCodecContext * avctx,
            break;
        }
     next_data:
+       ;
     }
     return buf_ptr - buf;
 }