shithub: aacdec

Download patch

ref: 3e5c46259ac3a65720ff99cc546833e753e28f58
parent: 5857a18ba4498e9bd73e3893ea59d460c5abe8b5
author: menno <menno>
date: Sat Jun 15 11:38:22 EDT 2002

Better bitstream error handling

--- a/libfaad/error.c
+++ b/libfaad/error.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: error.c,v 1.4 2002/04/18 18:08:06 menno Exp $
+** $Id: error.c,v 1.5 2002/06/15 15:38:22 menno Exp $
 **/
 
 #include "common.h"
@@ -31,5 +31,8 @@
     "Unable to find ADTS syncword",
     "Channel coupling not yet implemented",
     "Channel configuration not allowed in error resilient frame",
-    "Bit error in error resilient scalefactor decoding"
+    "Bit error in error resilient scalefactor decoding",
+    "Error decoding huffman scalefactor (bitstream error)",
+    "Error decoding huffman codeword (bitstream error)",
+    "Non existent huffman codebook number found"
 };
\ No newline at end of file
--- a/libfaad/huffman.h
+++ b/libfaad/huffman.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: huffman.h,v 1.8 2002/06/15 15:10:47 menno Exp $
+** $Id: huffman.h,v 1.9 2002/06/15 15:38:22 menno Exp $
 **/
 
 #ifndef __HUFFMAN_H__
@@ -34,7 +34,7 @@
 #include "codebook/hcb.h"
 
 
-static INLINE uint8_t huffman_scale_factor(bitfile *ld)
+static INLINE int8_t huffman_scale_factor(bitfile *ld)
 {
     uint16_t offset = 0;
 
@@ -43,14 +43,13 @@
         uint8_t b = faad_get1bit(ld
             DEBUGVAR(1,255,"huffman_scale_factor()"));
         offset += hcb_sf[offset][b];
+
+        if (offset > 240)
+        {
+            /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
+            return -1;
+        }
     }
-#ifdef ANALYSIS
-    if (offset>240)
-    {
-        printf("ERROR: offset into hcb_sf = %d >240!\n", offset);
-        return 0;
-    }
-#endif
 
     return hcb_sf[offset][0];
 }
@@ -74,13 +73,11 @@
 
 static uint8_t hcbN[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
 
-#ifdef ANALYSIS
 static int hcb_2_quad_table_size[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
 static int hcb_2_pair_table_size[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
-static int hcb_bin_table_size[] = { 0, 0, 0, 0, 0, 161, 0, 127, 0, 337, 0, 0 };
-#endif
+static int hcb_bin_table_size[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
 
-static INLINE void huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp)
+static INLINE uint8_t huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp)
 {
     uint32_t cw;
     uint16_t offset = 0;
@@ -106,21 +103,20 @@
             faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits);
         }
 
-#ifdef ANALYSIS
         if (offset > hcb_2_quad_table_size[cb])
         {
-            printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
-                hcb_2_quad_table_size[cb]);
-            return;
+            /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
+                hcb_2_quad_table_size[cb]); */
+            return 10;
         }
-#endif
 
         sp[0] = hcb_2_quad_table[cb][offset].x;
         sp[1] = hcb_2_quad_table[cb][offset].y;
         sp[2] = hcb_2_quad_table[cb][offset].v;
         sp[3] = hcb_2_quad_table[cb][offset].w;
-        break;
 
+        return 0;
+
     case 6: /* 2-step method for data pairs */
     case 8:
     case 10:
@@ -148,19 +144,18 @@
             faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits);
         }
 
-#ifdef ANALYSIS
         if (offset > hcb_2_pair_table_size[cb])
         {
-            printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
-                hcb_2_pair_table_size[cb]);
-            return;
+            /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
+                hcb_2_pair_table_size[cb]); */
+            return 10;
         }
-#endif
 
         sp[0] = hcb_2_pair_table[cb][offset].x;
         sp[1] = hcb_2_pair_table[cb][offset].y;
-        break;
 
+        return 0;
+
     case 3: /* binary search for data quadruples */
 
         while (!hcb3[offset].is_leaf)
@@ -170,12 +165,19 @@
             offset += hcb3[offset].data[b];
         }
 
+        if (offset > hcb_bin_table_size[cb])
+        {
+            /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
+                hcb_bin_table_size[cb]); */
+            return 10;
+        }
+
         sp[0] = hcb3[offset].data[0];
         sp[1] = hcb3[offset].data[1];
         sp[2] = hcb3[offset].data[2];
         sp[3] = hcb3[offset].data[3];
 
-        break;
+        return 0;
 
     case 5: /* binary search for data pairs */
     case 7:
@@ -188,20 +190,24 @@
             offset += hcb_bin_table[cb][offset].data[b];
         }
 
-#ifdef ANALYSIS
         if (offset > hcb_bin_table_size[cb])
         {
-            printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
-                hcb_bin_table_size[cb]);
-            return;
+            /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
+                hcb_bin_table_size[cb]); */
+            return 10;
         }
-#endif
 
         sp[0] = hcb_bin_table[cb][offset].data[0];
         sp[1] = hcb_bin_table[cb][offset].data[1];
 
-        break;
+        return 0;
+
+    default:
+        /* Non existent codebook number, something went wrong */
+        return 11;
     }
+
+    return 0;
 }
 
 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len)
--- a/libfaad/syntax.c
+++ b/libfaad/syntax.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: syntax.c,v 1.19 2002/06/15 15:10:47 menno Exp $
+** $Id: syntax.c,v 1.20 2002/06/15 15:38:22 menno Exp $
 **/
 
 /*
@@ -837,8 +837,10 @@
             case INTENSITY_HCB2:
 
                 /* decode intensity position */
-                t = huffman_scale_factor(ld) - 60;
-                is_position += t;
+                t = huffman_scale_factor(ld);
+                if (t < 0)
+                    return 9;
+                is_position += (t - 60);
                 ics->scale_factors[g][sfb] = is_position;
 
                 break;
@@ -851,7 +853,10 @@
                     t = faad_getbits(ld, 9
                         DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
                 } else {
-                    t = huffman_scale_factor(ld) - 60;
+                    t = huffman_scale_factor(ld);
+                    if (t < 0)
+                        return 9;
+                    t -= 60;
                 }
                 noise_energy += t;
                 ics->scale_factors[g][sfb] = noise_energy;
@@ -862,8 +867,10 @@
             default: /* spectral books */
 
                 /* decode scale factor */
-                t = huffman_scale_factor(ld) - 60;
-                scale_factor += t;
+                t = huffman_scale_factor(ld);
+                if (t < 0)
+                    return 9;
+                scale_factor += (t - 60);
                 if (scale_factor < 0)
                     return 4;
                 ics->scale_factors[g][sfb] = scale_factor;
@@ -1100,6 +1107,7 @@
     uint16_t k, p = 0;
     uint8_t groups = 0;
     uint8_t sect_cb;
+    uint8_t result;
     uint16_t nshort = frame_len/8;
 
     sp = spectral_data;
@@ -1134,7 +1142,8 @@
 
                     inc = (sect_cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
 
-                    huffman_spectral_data(sect_cb, ld, sp);
+                    if ((result = huffman_spectral_data(sect_cb, ld, sp)) > 0)
+                        return result;
                     if (unsigned_cb[sect_cb])
                         huffman_sign_bits(ld, sp, inc);
                     k += inc;