shithub: aacdec

Download patch

ref: 9ea647b768d3a5633b8ce8858e4d7d92b06b3577
parent: d59a88f783e2630fc4e6953907231ded85d39445
author: menno <menno>
date: Wed Jan 14 15:36:22 EST 2004

Stability fixes

--- a/libfaad/bits.h
+++ b/libfaad/bits.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: bits.h,v 1.32 2004/01/14 20:32:30 menno Exp $
+** $Id: bits.h,v 1.33 2004/01/14 20:36:22 menno Exp $
 **/
 
 #ifndef __BITS_H__
@@ -280,12 +280,12 @@
             DEBUGVAR(1,998,""))  & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF;
     }
 
-//    if (r != CRC)
-//    {
-//        return 8;
-//    } else {
+    if (r != CRC)
+    {
+        return 8;
+    } else {
         return 0;
-//    }
+    }
 }
 
 static uint8_t tabFlipbits[256] = {
--- a/libfaad/decoder.c
+++ b/libfaad/decoder.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: decoder.c,v 1.90 2004/01/14 09:09:13 menno Exp $
+** $Id: decoder.c,v 1.91 2004/01/14 20:32:30 menno Exp $
 **/
 
 #include "common.h"
@@ -408,8 +408,8 @@
     hDecoder->sbr[0] = NULL;
 #endif
 
-    /* must be done before frameLength is divided by 2 for LD */
-    hDecoder->fb = filter_bank_init(hDecoder->frameLength);
+    if (hDecoder->fb) filter_bank_end(hDecoder->fb);
+    hDecoder->fb = NULL;
 
     /* Take care of buffers */
     if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
@@ -448,6 +448,8 @@
     }
 #endif
 
+    hDecoder->fb = filter_bank_init(hDecoder->frameLength);
+
     return 0;
 }
 #endif
@@ -929,6 +931,9 @@
     return sample_buffer;
 
 error:
+
+    faad_endbits(&ld);
+
     /* cleanup */
 #ifdef ANALYSIS
     fflush(stdout);
--- a/libfaad/huffman.c
+++ b/libfaad/huffman.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: huffman.c,v 1.12 2004/01/13 14:24:10 menno Exp $
+** $Id: huffman.c,v 1.13 2004/01/14 20:32:30 menno Exp $
 **/
 
 #include "common.h"
@@ -168,8 +168,12 @@
     {
         /* we know for sure it's more than hcbN[cb] bits long */
         faad_flushbits(ld, hcbN[cb]);
+#if 0
         offset += (uint16_t)faad_showbits(ld, extra_bits);
         faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]);
+#else
+        offset += (uint16_t)faad_getbits(ld, extra_bits);
+#endif
     } else {
         faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits);
     }
@@ -211,8 +215,12 @@
     {
         /* we know for sure it's more than hcbN[cb] bits long */
         faad_flushbits(ld, hcbN[cb]);
+#if 0
         offset += (uint16_t)faad_showbits(ld, extra_bits);
         faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]);
+#else
+        offset += (uint16_t)faad_getbits(ld, extra_bits);
+#endif
     } else {
         faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits);
     }
@@ -333,7 +341,7 @@
     case 10:
         return huffman_2step_pair_sign(cb, ld, sp);
     case 12: {
-        uint8_t err = huffman_2step_quad(1, ld, sp);
+        uint8_t err = huffman_2step_pair(11, ld, sp);
         sp[0] = huffman_codebook(0); sp[1] = huffman_codebook(1); 
         return err; }
     case 11:
--- a/libfaad/sbr_dec.c
+++ b/libfaad/sbr_dec.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: sbr_dec.c,v 1.23 2004/01/13 20:35:14 menno Exp $
+** $Id: sbr_dec.c,v 1.24 2004/01/14 20:32:30 menno Exp $
 **/
 
 
@@ -44,7 +44,8 @@
 /* static function declarations */
 static void sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
 
-sbr_info *sbrDecodeInit(uint16_t framelength
+sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
+                        uint32_t sample_rate
 #ifdef DRM
 						, uint8_t IsDRM
 #endif
@@ -53,6 +54,10 @@
     sbr_info *sbr = faad_malloc(sizeof(sbr_info));
     memset(sbr, 0, sizeof(sbr_info));
 
+    /* save id of the parent element */
+    sbr->id_aac = id_aac;
+    sbr->sample_rate = sample_rate;
+
     sbr->bs_freq_scale = 2;
     sbr->bs_alter_scale = 1;
     sbr->bs_noise_bands = 2;
@@ -124,11 +129,22 @@
 
     sbr->L_E_prev[ch] = sbr->L_E[ch];
 
-    sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
-    for (i = 0; i < 64; i++)
+    /* sbr->L_E[ch] can become 0 on files with bit errors */
+    if (sbr->L_E[ch] > 0)
     {
-        sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
-        sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
+        sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
+        for (i = 0; i < 64; i++)
+        {
+            sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
+            sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
+        }
+    } else {
+        sbr->f_prev[ch] = 0;
+        for (i = 0; i < 64; i++)
+        {
+            sbr->E_prev[ch][i] = 0;
+            sbr->Q_prev[ch][i] = 0;
+        }
     }
 
     for (i = 0; i < 64; i++)
@@ -236,6 +252,10 @@
             else
                 xover_band = sbr->kx;
 
+            /* kx can be > 32 in cases of bit errors */
+            /* TODO: should be checked somewhere else */
+            xover_band = min(xover_band, 32);
+
             for (k = 0; k < xover_band; k++)
             {
                 QMF_RE(X[l][k]) = QMF_RE(sbr->Xcodec[ch][l + sbr->tHFAdj][k]);
@@ -275,6 +295,13 @@
 {
     uint8_t dont_process = 0;
 
+    if (sbr == NULL)
+        return;
+
+    /* case can occur due to bit errors */
+    if (sbr->id_aac != ID_CPE)
+        return;
+
     if (sbr->ret || (sbr->header_count == 0))
     {
         /* don't process just upsample */
@@ -311,6 +338,13 @@
                           const uint8_t just_seeked, const uint8_t upsample_only)
 {
     uint8_t dont_process = 0;
+
+    if (sbr == NULL)
+        return;
+
+    /* case can occur due to bit errors */
+    if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
+        return;
 
     if (sbr->ret || (sbr->header_count == 0))
     {
--- a/libfaad/sbr_dec.h
+++ b/libfaad/sbr_dec.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: sbr_dec.h,v 1.17 2004/01/13 20:35:14 menno Exp $
+** $Id: sbr_dec.h,v 1.18 2004/01/14 20:32:30 menno Exp $
 **/
 
 #ifndef __SBR_DEC_H__
@@ -205,7 +205,8 @@
     uint8_t bs_df_noise[2][3];
 } sbr_info;
 
-sbr_info *sbrDecodeInit(uint16_t framelength
+sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
+                        uint32_t sample_rate
 #ifdef DRM
 						, uint8_t IsDRM
 #endif
--- a/libfaad/specrec.c
+++ b/libfaad/specrec.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: specrec.c,v 1.39 2004/01/13 20:35:14 menno Exp $
+** $Id: specrec.c,v 1.40 2004/01/14 20:32:30 menno Exp $
 **/
 
 /*
@@ -588,8 +588,16 @@
         {
             top = ics->sect_sfb_offset[g][sfb+1];
 
-            exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
-            frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
+            /* this could be scalefactor for IS or PNS, those could be negative or bigger then 255??? */
+            if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255)
+            {
+                exp = 0;
+                frac = 0;
+            } else {
+                /* ics->scale_factors[g][sfb] must be between 0 and 255 */
+                exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
+                frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
+            }
 
 #ifdef FIXED_POINT
             exp -= 25;
@@ -777,17 +785,19 @@
             drc_decode(hDecoder->drc, spec_coef);
     }
 
-    mul = 1;
-#ifdef SBR_DEC
-    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
-    {
-        /* SBR requires 2 times as much output data */
-        mul = 2;
-    }
-#endif
 
     if (hDecoder->time_out[sce->channel] == NULL)
     {
+        mul = 1;
+#ifdef SBR_DEC
+        hDecoder->sbr_alloced = 0;
+        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        {
+            /* SBR requires 2 times as much output data */
+            mul = 2;
+            hDecoder->sbr_alloced = 1;
+        }
+#endif
         hDecoder->time_out[sce->channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
         memset(hDecoder->time_out[sce->channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
     }
@@ -847,7 +857,8 @@
 #endif
 
 #ifdef SBR_DEC
-    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+    if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        && hDecoder->sbr_alloced)
     {
         uint8_t ele = hDecoder->fr_ch_ele;
         uint8_t ch = sce->channel;
@@ -855,16 +866,19 @@
         /* following case can happen when forceUpSampling == 1 */
         if (hDecoder->sbr[ele] == NULL)
         {
-            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength
+            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
+                sce->ele_id, 2*get_sample_rate(hDecoder->sf_index)
 #ifdef DRM
                 , 0
 #endif
                 );
-            hDecoder->sbr[ele]->id_aac = sce->ele_id;
         }
 
         sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
             hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
+    } else {
+        hDecoder->sbr_present_flag = 0;
+        hDecoder->forceUpSampling = 0;
     }
 #endif
 
@@ -1018,17 +1032,18 @@
             drc_decode(hDecoder->drc, spec_coef2);
     }
 
-    mul = 1;
-#ifdef SBR_DEC
-    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
-    {
-        /* SBR requires 2 times as much output data */
-        mul = 2;
-    }
-#endif
-
     if (hDecoder->time_out[cpe->channel] == NULL)
     {
+        mul = 1;
+#ifdef SBR_DEC
+        hDecoder->sbr_alloced = 0;
+        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        {
+            /* SBR requires 2 times as much output data */
+            mul = 2;
+            hDecoder->sbr_alloced = 1;
+        }
+#endif
         hDecoder->time_out[cpe->channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
         memset(hDecoder->time_out[cpe->channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
     }
@@ -1124,7 +1139,8 @@
 #endif
 
 #ifdef SBR_DEC
-    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+    if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        && hDecoder->sbr_alloced)
     {
         uint8_t ele = hDecoder->fr_ch_ele;
         uint8_t ch0 = cpe->channel;
@@ -1133,17 +1149,20 @@
         /* following case can happen when forceUpSampling == 1 */
         if (hDecoder->sbr[ele] == NULL)
         {
-            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength
+            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
+                cpe->ele_id, 2*get_sample_rate(hDecoder->sf_index)
 #ifdef DRM
                 , 0
 #endif
                 );
-            hDecoder->sbr[ele]->id_aac = cpe->ele_id;
         }
 
         sbrDecodeCoupleFrame(hDecoder->sbr[ele],
             hDecoder->time_out[ch0], hDecoder->time_out[ch1],
             hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
+    } else {
+        hDecoder->sbr_present_flag = 0;
+        hDecoder->forceUpSampling = 0;
     }
 #endif
 
--- a/libfaad/structs.h
+++ b/libfaad/structs.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: structs.h,v 1.27 2004/01/13 20:35:14 menno Exp $
+** $Id: structs.h,v 1.28 2004/01/14 20:32:30 menno Exp $
 **/
 
 #ifndef __STRUCTS_H__
@@ -399,8 +399,7 @@
 #ifdef SBR_DEC
     int8_t sbr_present_flag;
     int8_t forceUpSampling;
-
-    uint8_t sbr_used[32];
+    uint8_t sbr_alloced;
 
     sbr_info *sbr[32];
 #ifdef DRM
--- a/libfaad/syntax.c
+++ b/libfaad/syntax.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: syntax.c,v 1.66 2004/01/13 14:24:10 menno Exp $
+** $Id: syntax.c,v 1.67 2004/01/14 20:32:30 menno Exp $
 **/
 
 /*
@@ -439,7 +439,7 @@
                 /* one sbr_info describes a channel_element not a channel! */
                 if ((hInfo->error = fill_element(hDecoder, ld, drc
 #ifdef SBR_DEC
-                    , (ch_ele-1)
+                    , (ch_ele == 0) ? 0 : (ch_ele-1)
 #endif
                     )) > 0)
                     return;
@@ -963,17 +963,15 @@
     if (count > 0)
     {
 #ifdef SBR_DEC
-        hDecoder->sbr_used[sbr_ele] = 0;
         bs_extension_type = (uint8_t)faad_showbits(ld, 4);
 
         if ((bs_extension_type == EXT_SBR_DATA) ||
             (bs_extension_type == EXT_SBR_DATA_CRC))
         {
-            hDecoder->sbr_used[sbr_ele] = 1;
-
             if (!hDecoder->sbr[sbr_ele])
             {
-                hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength
+                hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
+                    hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index)
 #ifdef DRM
                     , 0
 #endif
@@ -981,15 +979,10 @@
             }
 
             hDecoder->sbr_present_flag = 1;
-            hDecoder->sbr[sbr_ele]->sample_rate = get_sample_rate(hDecoder->sf_index);
-            hDecoder->sbr[sbr_ele]->sample_rate *= 2;
-            /* save id of previous element, this sbr object belongs to that element */
-            hDecoder->sbr[sbr_ele]->id_aac = hDecoder->element_id[sbr_ele];
 
             /* parse the SBR data */
             hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
         } else {
-            hDecoder->sbr_used[sbr_ele] = 0;
 #endif
             while (count > 0)
             {
@@ -1170,10 +1163,11 @@
             return;
         }
 
-        hDecoder->sbr_used[0] = 1;
-
         if (!hDecoder->sbr[0])
-            hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, 1);
+        {
+            hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, cpe.ele_id,
+                2*get_sample_rate(hDecoder->sf_index), 1);
+        }
 
         /* Reverse bit reading of SBR data in DRM audio frame */
         revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
@@ -1192,8 +1186,6 @@
         hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
         hDecoder->sbr[0]->sample_rate *= 2;
 
-        hDecoder->sbr[0]->id_aac = hDecoder->element_id[0];
-
         faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
 
         hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
@@ -1204,6 +1196,9 @@
             hDecoder->sbr[0]->ret = faad_check_CRC(&ld_sbr, faad_get_processed_bits(&ld_sbr) - 8);
 
         faad_endbits(&ld_sbr);
+
+        if (revbuffer)
+            faad_free(revbuffer);
     }
 #endif
 #endif
@@ -1641,6 +1636,10 @@
 
                 break;
             default: /* spectral books */
+
+                /* ics->scale_factors[g][sfb] must be between 0 and 255 */
+
+                ics->scale_factors[g][sfb] = 0;
 
                 /* decode scale factor */
                 t = huffman_scale_factor(ld);