shithub: aacdec

Download patch

ref: 9d43d85c34011019a5d7d8e8e3f68158b0656acb
parent: c3ec6d5ff3050628f6e2120ae9e9e7521e7c4191
author: menno <menno>
date: Sat Jan 19 04:39:41 EST 2002

- decodes mp4 files correctly
- bug fix in tns_data()
- added bitstream analysis

--- a/frontend/main.c
+++ b/frontend/main.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: main.c,v 1.7 2002/01/16 08:18:30 menno Exp $
+** $Id: main.c,v 1.8 2002/01/19 09:39:00 menno Exp $
 **/
 
 #ifdef _WIN32
@@ -169,6 +169,7 @@
 
     int first_time = 1;
 
+
     /* declare variables for buffering */
     DEC_BUFF_VARS
 
@@ -210,10 +211,8 @@
         fclose(infile);
         return 1;
     }
+    buffer_index += bytesconsumed;
 
-    /* update buffer */
-    UPDATE_BUFF_READ
-
     do
     {
         /* update buffer */
@@ -268,7 +267,7 @@
             write_audio_file(aufile, sample_buffer, frameInfo.samples);
         }
 
-        if (IS_FILE_END)
+        if (buffer_index >= fileread)
             sample_buffer = NULL; /* to make sure it stops now */
 
     } while (sample_buffer != NULL);
@@ -285,8 +284,6 @@
     return frameInfo.error;
 }
 
-int use_ltp;
-
 int GetAACTrack(MP4FileHandle *infile)
 {
     /* find AAC track */
@@ -305,16 +302,9 @@
             switch (type)
             {
             case MP4_MPEG4_AUDIO_TYPE:
-                /* could be LTP */
-                use_ltp = 1;
-
-                return trackId;
             case MP4_MPEG2_AAC_MAIN_AUDIO_TYPE:
             case MP4_MPEG2_AAC_LC_AUDIO_TYPE:
             case MP4_MPEG2_AAC_SSR_AUDIO_TYPE:
-                /* MPEG2: no LTP */
-                use_ltp = 0;
-
                 return trackId;
             }
         }
@@ -333,7 +323,7 @@
 int decodeMP4file(char *mp4file, char *sndfile, int to_stdout,
                   int outputFormat, int fileType)
 {
-    int track, srate;
+    int track;
     unsigned long samplerate, channels;
     void *sample_buffer;
 
@@ -354,6 +344,8 @@
 
     int first_time = 1;
 
+    hDecoder = faacDecOpen();
+
 	infile = MP4Read(mp4file, 0);
 	if (!infile)
     {
@@ -362,8 +354,6 @@
         return 1;
 	}
 
-    use_ltp = 0;
-
     if ((track = GetAACTrack(infile)) < 0)
     {
         fprintf(stderr, "Unable to find correct AAC sound track in the MP4 file.\n");
@@ -370,23 +360,13 @@
         MP4Close(infile);
         return 1;
     }
-    srate = MP4GetTrackTimeScale(infile, track);
 
-    hDecoder = faacDecOpen();
+    buffer = NULL;
+    buffer_size = 0;
+    MP4GetTrackESConfiguration(infile, track, &buffer, &buffer_size);
 
-    /* Set the default object type and samplerate */
-    /* This is useful for RAW AAC files */
-    config = faacDecGetCurrentConfiguration(hDecoder);
-    config->defSampleRate = (srate > 10)?srate:srates[srate];
-    if (use_ltp)
-        config->defObjectType = LTP;
-    config->outputFormat = outputFormat;
-
-    faacDecSetConfiguration(hDecoder, config);
-
-
-    if(faacDecInit(hDecoder, NULL, &samplerate,
-        &channels) < 0)
+    if(faacDecInit2(hDecoder, buffer, buffer_size,
+                    &samplerate, &channels) < 0)
     {
         /* If some error initializing occured, skip the file */
         fprintf(stderr, "Error initializing decoder library.\n");
@@ -394,6 +374,7 @@
         MP4Close(infile);
         return 1;
     }
+    if (buffer) free(buffer);
 
     numSamples = MP4GetTrackNumberOfSamples(infile, track);
 
@@ -435,11 +416,11 @@
         {
             if(!to_stdout)
             {
-                aufile = open_audio_file(sndfile, (srate > 10)?srate:srates[srate], frameInfo.channels,
+                aufile = open_audio_file(sndfile, samplerate, frameInfo.channels,
                     outputFormat, fileType);
             } else {
                 setmode(fileno(stdout), O_BINARY);
-                aufile = open_audio_file("-", (srate > 10)?srate:srates[srate], frameInfo.channels,
+                aufile = open_audio_file("-", samplerate, frameInfo.channels,
                     outputFormat, fileType);
             }
             if (aufile == NULL)
--- a/include/faad.h
+++ b/include/faad.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: faad.h,v 1.1 2002/01/14 19:15:55 menno Exp $
+** $Id: faad.h,v 1.2 2002/01/19 09:39:11 menno Exp $
 **/
 
 #ifndef __AACDEC_H__
@@ -80,10 +80,16 @@
 int FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
                                     faacDecConfigurationPtr config);
 
+/* Init the library based on info from the AAC file (ADTS/ADIF) */
 int FAADAPI faacDecInit(faacDecHandle hDecoder,
                         unsigned char *buffer,
                         unsigned long *samplerate,
                         unsigned long *channels);
+
+/* Init the library using a DecoderSpecificInfo */
+int FAADAPI faacDecInit2(faacDecHandle hDecoder, unsigned char *pBuffer,
+                         unsigned long SizeOfDecoderSpecificInfo,
+                         unsigned long *samplerate, unsigned long *channels);
 
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
--- a/libfaad/bits.c
+++ b/libfaad/bits.c
@@ -16,11 +16,12 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: bits.c,v 1.1 2002/01/14 19:15:55 menno Exp $
+** $Id: bits.c,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #include "bits.h"
 
+
 /* initialize buffer, call once before first getbits or showbits */
 void faad_initbits(bitfile *ld, unsigned char *buffer)
 {
@@ -41,7 +42,7 @@
 
     while (ld->framebits%8 != 0)
     {
-        faad_get1bit(ld);
+        faad_get1bit(ld DEBUGVAR(1,0,"faad_byte_align(): get bit until aligned"));
         i++;
     }
 
--- a/libfaad/bits.h
+++ b/libfaad/bits.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: bits.h,v 1.1 2002/01/14 19:15:55 menno Exp $
+** $Id: bits.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __BITS_H__
@@ -26,6 +26,11 @@
 extern "C" {
 #endif
 
+#include "debug.h"
+#ifdef ANALYSIS
+#include <stdio.h>
+#endif
+
 #define BYTE_NUMBIT 8
 #define bit2byte(a) ((a)/BYTE_NUMBIT)
 
@@ -109,7 +114,7 @@
 }
 
 /* return next n bits (right adjusted) */
-static bits_inline unsigned int faad_getbits(bitfile *ld, int n)
+static bits_inline unsigned int faad_getbits(bitfile *ld, int n DEBUGDEC)
 {
     long l;
 
@@ -116,10 +121,15 @@
     l = faad_showbits(ld, n);
     faad_flushbits(ld, n);
 
+#ifdef ANALYSIS
+    if (print)
+        fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, l, var, dbg);
+#endif
+
     return l;
 }
 
-static bits_inline unsigned int faad_get1bit(bitfile *ld)
+static bits_inline unsigned int faad_get1bit(bitfile *ld DEBUGDEC)
 {
     unsigned char l;
 
@@ -129,6 +139,11 @@
     ld->framebits++;
     ld->rdptr += (ld->bitcnt>>3);
     ld->bitcnt &= 7;
+
+#ifdef ANALYSIS
+    if (print)
+        fprintf(stdout, "%4d  1 bits, val: %4d, variable: %d %s\n", dbg_count++, l>>7, var, dbg);
+#endif
 
     return l>>7;
 }
--- /dev/null
+++ b/libfaad/debug.h
@@ -1,0 +1,43 @@
+/*
+** FAAD - Freeware Advanced Audio Decoder
+** Copyright (C) 2002 M. Bakker
+**  
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+** 
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+** 
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software 
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+**
+** $Id: debug.h,v 1.1 2002/01/19 09:39:41 menno Exp $
+**/
+
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#ifdef ANALYSIS
+#define DEBUGDEC        ,int print,int var,char *dbg
+#define DEBUGVAR(A,B,C) ,A,B,C
+extern int dbg_count;
+#else
+#define DEBUGDEC
+#define DEBUGVAR(A,B,C)
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--- a/libfaad/decoder.c
+++ b/libfaad/decoder.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: decoder.c,v 1.2 2002/01/15 12:58:56 menno Exp $
+** $Id: decoder.c,v 1.3 2002/01/19 09:39:41 menno Exp $
 **/
 
 #include <stdlib.h>
@@ -35,6 +35,9 @@
 #include "error.h"
 #include "output.h"
 
+#ifdef ANALYSIS
+int dbg_count;
+#endif
 
 char* FAADAPI faacDecGetErrorMessage(int errcode)
 {
@@ -171,6 +174,85 @@
     return 0;
 }
 
+
+static unsigned long ObjectTypesTable[32] = {0, 1, 1, 1, 1, };
+
+int parse_audio_decoder_specific_info(faacDecHandle hDecoder,
+                                      unsigned char *pBuffer,
+                                      unsigned long *samplerate,
+                                      unsigned long *channels)
+{
+    bitfile ld;
+    unsigned long ObjectTypeIndex, SamplingFrequencyIndex,
+        ChannelsConfiguration;
+
+    faad_initbits(&ld, pBuffer);
+    faad_byte_align(&ld);
+
+    ObjectTypeIndex = faad_getbits(&ld, 5
+        DEBUGVAR(1,0,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
+
+    SamplingFrequencyIndex = faad_getbits(&ld, 4
+        DEBUGVAR(1,0,"parse_audio_decoder_specific_info(): SamplingFrequencyIndex"));
+
+    ChannelsConfiguration = faad_getbits(&ld, 4
+        DEBUGVAR(1,0,"parse_audio_decoder_specific_info(): ChannelsConfiguration"));
+
+    if (ObjectTypesTable[ObjectTypeIndex] != 1)
+    {
+        return -1;
+    }
+
+    *samplerate = sample_rates[SamplingFrequencyIndex];
+    if (*samplerate == 0)
+    {
+        return -2;
+    }
+
+    *channels = ChannelsConfiguration;
+
+    hDecoder->sf_index = SamplingFrequencyIndex;
+    hDecoder->object_type = ObjectTypeIndex - 1;
+
+    if(ChannelsConfiguration > 7)
+    {
+        return -3;
+    }
+
+    /* get GASpecificConfig */
+
+    return 0;
+}
+
+/* Init the library using a DecoderSpecificInfo */
+int FAADAPI faacDecInit2(faacDecHandle hDecoder, unsigned char *pBuffer,
+                         unsigned long SizeOfDecoderSpecificInfo,
+                         unsigned long *samplerate, unsigned long *channels)
+{
+    int rc;
+
+    hDecoder->adif_header_present = 0;
+    hDecoder->adts_header_present = 0;
+
+    if((hDecoder == NULL)
+        || (pBuffer == NULL)
+        || (SizeOfDecoderSpecificInfo < 2)
+        || (samplerate == NULL)
+        || (channels == NULL))
+    {
+        return -1;
+    }
+
+    rc = parse_audio_decoder_specific_info(hDecoder, pBuffer,
+        samplerate, channels);
+    if (rc != 0)
+    {
+        return rc;
+    }
+
+    return 0;
+}
+
 void FAADAPI faacDecClose(faacDecHandle hDecoder)
 {
     int i;
@@ -248,8 +330,13 @@
     channels = 0;
     ch_ele = 0;
 
+#ifdef ANALYSIS
+    dbg_count = 0;
+#endif
+
     /* Table 4.4.3: raw_data_block() */
-    while ((id_syn_ele = faad_getbits(ld, LEN_SE_ID)) != ID_END)
+    while ((id_syn_ele = faad_getbits(ld, LEN_SE_ID
+        DEBUGVAR(1,0,"faacDecDecode(): id_syn_ele"))) != ID_END)
     {
         switch (id_syn_ele) {
         case ID_SCE:
@@ -487,6 +574,10 @@
     {
         free(syntax_elements[i]);
     }
+
+#ifdef ANALYSIS
+    fflush(stdout);
+#endif
 
     return sample_buffer;
 }
--- a/libfaad/decoder.h
+++ b/libfaad/decoder.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: decoder.h,v 1.1 2002/01/14 19:15:55 menno Exp $
+** $Id: decoder.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __DECODER_H__
@@ -109,8 +109,16 @@
 int FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
                                     faacDecConfigurationPtr config);
 
-int FAADAPI faacDecInit(faacDecHandle hDecoder, unsigned char *buffer,
-                        unsigned long *samplerate, unsigned long *channels);
+/* Init the library based on info from the AAC file (ADTS/ADIF) */
+int FAADAPI faacDecInit(faacDecHandle hDecoder,
+                        unsigned char *buffer,
+                        unsigned long *samplerate,
+                        unsigned long *channels);
+
+/* Init the library using a DecoderSpecificInfo */
+int FAADAPI faacDecInit2(faacDecHandle hDecoder, unsigned char *pBuffer,
+                         unsigned long SizeOfDecoderSpecificInfo,
+                         unsigned long *samplerate, unsigned long *channels);
 
 void FAADAPI faacDecClose(faacDecHandle hDecoder);
 
--- a/libfaad/huffman.c
+++ b/libfaad/huffman.c
@@ -16,9 +16,10 @@
 ** 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.c,v 1.1 2002/01/14 19:15:56 menno Exp $
+** $Id: huffman.c,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
+#include "syntax.h"
 #include "huffman.h"
 
 extern codebook book1[] = {
--- 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.1 2002/01/14 19:15:56 menno Exp $
+** $Id: huffman.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __HUFFMAN_H__
@@ -26,18 +26,12 @@
 extern "C" {
 #endif
 
+#include <stdlib.h>
+#ifdef ANALYSIS
+#include <stdio.h>
+#endif
 #include "bits.h"
 
-#define ZERO_HCB       0
-#define FIRST_PAIR_HCB 5
-#define ESC_HCB        11
-#define QUAD_LEN       4
-#define PAIR_LEN       2
-#define NSPECBOOKS     ESC_HCB + 1
-#define BOOKSCL        NSPECBOOKS
-#define NOISE_HCB      13
-#define INTENSITY_HCB2 14
-#define INTENSITY_HCB  15
 
 typedef struct
 {
@@ -109,17 +103,25 @@
         }
     }
 
+#ifdef ANALYSIS
+    fprintf(stdout, "%4d %2d bits, huffman_scale_factor(): huffman codeword (scalefactor)\n",
+        dbg_count++, h->len);
+#endif
+
     return h->scl;
 }
 
+static int codebookN[] = { 0, 7, 7, 9 };
+
 static huff_inline void huffman_spectral_data(int cb, bitfile *ld, short *sp)
 {
     int i, j;
     unsigned long cw;
-    codebook *h = book_table[cb];
+    codebook *h;
 
+    h = book_table[cb];
     i = h->len;
-    cw = faad_getbits(ld, i);
+    cw = faad_getbits(ld, i DEBUGVAR(0,0,""));
 
     while (cw != h->cw)
     {
@@ -128,10 +130,15 @@
         i = h->len;
         if (j!=0) {
             while (j--)
-                cw = (cw<<1) | faad_get1bit(ld);
+                cw = (cw<<1) | faad_get1bit(ld DEBUGVAR(0,0,""));
         }
     }
 
+#ifdef ANALYSIS
+    fprintf(stdout, "%4d %2d bits, huffman_spectral_data(): huffman codeword\n",
+        dbg_count++, h->len);
+#endif
+
     if(cb < FIRST_PAIR_HCB)
     {
         sp[0] = h->x;
@@ -149,9 +156,16 @@
     int i;
 
     for(i = 0; i < len; i++)
+    {
         if(sp[i])
-            if(faad_get1bit(ld) & 1)
+        {
+            if(faad_get1bit(ld
+                DEBUGVAR(1,0,"huffman_sign_bits(): sign bit")) & 1)
+            {
                 sp[i] = -sp[i];
+            }
+        }
+    }
 }
 
 static huff_inline short huffman_getescape(bitfile *ld, short sp)
@@ -169,15 +183,18 @@
     }
 
     for (i = 4; ; i++){
-        if (faad_get1bit(ld) == 0)
+        if (faad_get1bit(ld DEBUGVAR(1,0,"huffman_getescape(): escape size")) == 0)
             break;
     }
 
     if (i > 16) {
-        off = faad_getbits(ld, i-16) << 16;
-        off |= faad_getbits(ld, 16);
+        off = faad_getbits(ld, i-16
+            DEBUGVAR(1,0,"huffman_getescape(): escape, first part")) << 16;
+        off |= faad_getbits(ld, 16
+            DEBUGVAR(1,0,"huffman_getescape(): escape, second part"));
     } else {
-        off = faad_getbits(ld, i);
+        off = faad_getbits(ld, i
+            DEBUGVAR(1,0,"huffman_getescape(): escape"));
     }
 
     i = off + (1<<i);
--- a/libfaad/is.h
+++ b/libfaad/is.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: is.h,v 1.1 2002/01/14 19:15:56 menno Exp $
+** $Id: is.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __IS_H__
@@ -26,7 +26,7 @@
 extern "C" {
 #endif
 
-#include "huffman.h"
+#include "syntax.h"
 
 void is_decode(ic_stream *ics, ic_stream *icsr, float *l_spec, float *r_spec);
 
--- a/libfaad/libfaad.dsp
+++ b/libfaad/libfaad.dsp
@@ -169,6 +169,10 @@
 # End Source File
 # Begin Source File
 
+SOURCE=.\debug.h
+# End Source File
+# Begin Source File
+
 SOURCE=.\decoder.h
 # End Source File
 # Begin Source File
--- a/libfaad/pns.h
+++ b/libfaad/pns.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: pns.h,v 1.1 2002/01/14 19:15:56 menno Exp $
+** $Id: pns.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __PNS_H__
@@ -26,7 +26,6 @@
 extern "C" {
 #endif
 
-#include "huffman.h"
 #include "syntax.h"
 
 #define NOISE_OFFSET 90
--- 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.1 2002/01/14 19:15:57 menno Exp $
+** $Id: syntax.c,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 /*
@@ -35,7 +35,9 @@
 #include "bits.h"
 #include "data.h"
 #include "pulse.h"
+#include "debug.h"
 
+
 /* Table 4.4.2 */
 /* An MPEG-4 Audio decoder is only required to follow the Program
    Configuration Element in GASpecificConfig(). The decoder shall ignore
@@ -49,81 +51,121 @@
 
     pce->channels = 0;
 
-    pce->element_instance_tag = faad_getbits(ld, 4);
+    pce->element_instance_tag = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): element_instance_tag"));
 
-    pce->object_type = faad_getbits(ld, 2);
-    pce->sf_index = faad_getbits(ld, 4);
-    pce->num_front_channel_elements = faad_getbits(ld, 4);
-    pce->num_side_channel_elements = faad_getbits(ld, 4);
-    pce->num_back_channel_elements = faad_getbits(ld, 4);
-    pce->num_lfe_channel_elements = faad_getbits(ld, 2);
-    pce->num_assoc_data_elements = faad_getbits(ld, 3);
-    pce->num_valid_cc_elements = faad_getbits(ld, 4);
+    pce->object_type = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"program_config_element(): object_type"));
+    pce->sf_index = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): sf_index"));
+    pce->num_front_channel_elements = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): num_front_channel_elements"));
+    pce->num_side_channel_elements = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): num_side_channel_elements"));
+    pce->num_back_channel_elements = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): num_back_channel_elements"));
+    pce->num_lfe_channel_elements = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"program_config_element(): num_lfe_channel_elements"));
+    pce->num_assoc_data_elements = faad_getbits(ld, 3
+        DEBUGVAR(1,0,"program_config_element(): num_assoc_data_elements"));
+    pce->num_valid_cc_elements = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): num_valid_cc_elements"));
 
-    pce->mono_mixdown_present = faad_get1bit(ld);
+    pce->mono_mixdown_present = faad_get1bit(ld
+        DEBUGVAR(1,0,"program_config_element(): mono_mixdown_present"));
     if (pce->mono_mixdown_present == 1)
-        pce->mono_mixdown_element_number = faad_getbits(ld, 4);
+    {
+        pce->mono_mixdown_element_number = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): mono_mixdown_element_number"));
+    }
 
-    pce->stereo_mixdown_present = faad_get1bit(ld);
+    pce->stereo_mixdown_present = faad_get1bit(ld
+        DEBUGVAR(1,0,"program_config_element(): stereo_mixdown_present"));
     if (pce->stereo_mixdown_present == 1)
-        pce->stereo_mixdown_element_number = faad_getbits(ld, 4);
+    {
+        pce->stereo_mixdown_element_number = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): stereo_mixdown_element_number"));
+    }
 
-    pce->matrix_mixdown_idx_present = faad_get1bit(ld);
+    pce->matrix_mixdown_idx_present = faad_get1bit(ld
+        DEBUGVAR(1,0,"program_config_element(): matrix_mixdown_idx_present"));
     if (pce->matrix_mixdown_idx_present == 1)
     {
-        pce->matrix_mixdown_idx = faad_getbits(ld, 2);
-        pce->pseudo_surround_enable = faad_get1bit(ld);
+        pce->matrix_mixdown_idx = faad_getbits(ld, 2
+            DEBUGVAR(1,0,"program_config_element(): matrix_mixdown_idx"));
+        pce->pseudo_surround_enable = faad_get1bit(ld
+            DEBUGVAR(1,0,"program_config_element(): pseudo_surround_enable"));
     }
 
     for (i = 0; i < pce->num_front_channel_elements; i++)
     {
-        if ((pce->front_element_is_cpe[i] = faad_get1bit(ld)) & 1)
+        if ((pce->front_element_is_cpe[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"program_config_element(): front_element_is_cpe"))) & 1)
+        {
             pce->channels += 2;
-        else
+        } else {
             pce->channels++;
-        pce->front_element_tag_select[i] = faad_getbits(ld, 4);
+        }
+        pce->front_element_tag_select[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): front_element_tag_select"));
     }
 
     for (i = 0; i < pce->num_side_channel_elements; i++)
     {
-        if ((pce->side_element_is_cpe[i] = faad_get1bit(ld)) & 1)
+        if ((pce->side_element_is_cpe[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"program_config_element(): side_element_is_cpe"))) & 1)
+        {
             pce->channels += 2;
-        else
+        } else {
             pce->channels++;
-        pce->side_element_tag_select[i] = faad_getbits(ld, 4);
+        }
+        pce->side_element_tag_select[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): side_element_tag_select"));
     }
 
     for (i = 0; i < pce->num_back_channel_elements; i++)
     {
-        if ((pce->back_element_is_cpe[i] = faad_get1bit(ld)) & 1)
+        if ((pce->back_element_is_cpe[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"program_config_element(): back_element_is_cpe"))) & 1)
+        {
             pce->channels += 2;
-        else
+        } else {
             pce->channels++;
-        pce->back_element_tag_select[i] = faad_getbits(ld, 4);
+        }
+        pce->back_element_tag_select[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): back_element_tag_select"));
     }
 
     for (i = 0; i < pce->num_lfe_channel_elements; i++)
     {
         pce->channels++;
-        pce->lfe_element_tag_select[i] = faad_getbits(ld, 4);
+        pce->lfe_element_tag_select[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): lfe_element_tag_select"));
     }
 
     for (i = 0; i < pce->num_assoc_data_elements; i++)
-        pce->assoc_data_element_tag_select[i] = faad_getbits(ld, 4);
+        pce->assoc_data_element_tag_select[i] = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"program_config_element(): assoc_data_element_tag_select"));
 
     for (i = 0; i < pce->num_valid_cc_elements; i++)
     {
         /* have to count these as channels too?? */
-        pce->cc_element_is_ind_sw[i] = faad_get1bit(ld);
-        pce->valid_cc_element_tag_select[i] = faad_getbits(ld, 4);
+        pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"program_config_element(): cc_element_is_ind_sw"));
+        pce->valid_cc_element_tag_select[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"program_config_element(): valid_cc_element_tag_select"));
     }
 
     faad_byte_align(ld);
 
-    pce->comment_field_bytes = faad_getbits(ld, 8);
+    pce->comment_field_bytes = faad_getbits(ld, 8
+        DEBUGVAR(1,0,"program_config_element(): comment_field_bytes"));
     i = 0;
     for (i = 0; i < pce->comment_field_bytes; i++)
-        pce->comment_field_data[i] = faad_getbits(ld, 8);
+    {
+        pce->comment_field_data[i] = faad_getbits(ld, 8
+            DEBUGVAR(1,0,"program_config_element(): comment_field_data"));
+    }
     pce->comment_field_data[i] = 0;
 
     return 0;
@@ -136,7 +178,8 @@
 {
     ic_stream *ics = &sce->ics1;
 
-    sce->element_instance_tag = faad_getbits(ld, LEN_TAG);
+    sce->element_instance_tag = faad_getbits(ld, LEN_TAG
+        DEBUGVAR(1,0,"single_lfe_channel_element(): element_instance_tag"));
 
     return individual_channel_stream(sce, ld, ics, 0, spec_data, sf_index,
         object_type);
@@ -151,9 +194,11 @@
     ic_stream *ics1 = &cpe->ics1;
     ic_stream *ics2 = &cpe->ics2;
 
-    cpe->element_instance_tag = faad_getbits(ld, LEN_TAG);
+    cpe->element_instance_tag = faad_getbits(ld, LEN_TAG
+        DEBUGVAR(1,0,"channel_pair_element(): element_instance_tag"));
 
-    if ((cpe->common_window = faad_get1bit(ld)) & 1)
+    if ((cpe->common_window = faad_get1bit(ld
+        DEBUGVAR(1,0,"channel_pair_element(): common_window"))) & 1)
     {
         /* both channels have common ics information */
         if ((result = ics_info(ics1, ld, cpe->common_window, sf_index,
@@ -160,7 +205,8 @@
             object_type)) > 0)
             return result;
 
-        ics1->ms_mask_present = faad_getbits(ld, 2);
+        ics1->ms_mask_present = faad_getbits(ld, 2
+            DEBUGVAR(1,0,"channel_pair_element(): ms_mask_present"));
         if (ics1->ms_mask_present == 1)
         {
             int g, sfb;
@@ -168,7 +214,8 @@
             {
                 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
                 {
-                    ics1->ms_used[g][sfb] = faad_get1bit(ld);
+                    ics1->ms_used[g][sfb] = faad_get1bit(ld
+                        DEBUGVAR(1,0,"channel_pair_element(): faad_get1bit"));
                 }
             }
         }
@@ -192,38 +239,61 @@
 static int ics_info(ic_stream *ics, bitfile *ld, int common_window,
                     int sf_index, int object_type)
 {
-    /* ics->ics_reserved_bit = */ faad_get1bit(ld);
-    ics->window_sequence = faad_getbits(ld, 2);
-    ics->window_shape = faad_get1bit(ld);
+    /* ics->ics_reserved_bit = */ faad_get1bit(ld
+        DEBUGVAR(1,0,"ics_info(): ics_reserved_bit"));
+    ics->window_sequence = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"ics_info(): window_sequence"));
+    ics->window_shape = faad_get1bit(ld
+        DEBUGVAR(1,0,"ics_info(): window_shape"));
 
     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
     {
-        ics->max_sfb = faad_getbits(ld, 4);
-        ics->scale_factor_grouping = faad_getbits(ld, 7);
+        ics->max_sfb = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"ics_info(): max_sfb (short)"));
+        ics->scale_factor_grouping = faad_getbits(ld, 7
+            DEBUGVAR(1,0,"ics_info(): scale_factor_grouping"));
     } else {
-        ics->max_sfb = faad_getbits(ld, 6);
+        ics->max_sfb = faad_getbits(ld, 6
+            DEBUGVAR(1,0,"ics_info(): max_sfb (long)"));
 
         if (object_type == LTP)
         {
-            if ((ics->predictor_data_present = faad_get1bit(ld)) & 1)
+            if ((ics->predictor_data_present = faad_get1bit(ld
+                DEBUGVAR(1,0,"ics_info(): predictor_data_present"))) & 1)
             {
-                if ((ics->ltp.data_present = faad_get1bit(ld)) & 1)
+                if ((ics->ltp.data_present = faad_get1bit(ld
+                    DEBUGVAR(1,0,"ics_info(): ltp.data_present"))) & 1)
+                {
                     ltp_data(ics, &ics->ltp, ld);
+                }
                 if (common_window)
-                    if ((ics->ltp2.data_present = faad_get1bit(ld)) & 1)
+                {
+                    if ((ics->ltp2.data_present = faad_get1bit(ld
+                        DEBUGVAR(1,0,"ics_info(): ltp2.data_present"))) & 1)
+                    {
                         ltp_data(ics, &ics->ltp2, ld);
+                    }
+                }
             }
         } else { /* MPEG2 style AAC predictor */
-            if ((ics->predictor_data_present = faad_get1bit(ld)) & 1)
+            if ((ics->predictor_data_present = faad_get1bit(ld
+                DEBUGVAR(1,0,"ics_info(): predictor_data_present"))) & 1)
             {
                 int sfb;
                 ics->pred.limit = min(ics->max_sfb, pred_sfb_max[sf_index]);
 
-                if ((ics->pred.predictor_reset = faad_get1bit(ld)) & 1)
-                    ics->pred.predictor_reset_group_number = faad_getbits(ld, 5);
+                if ((ics->pred.predictor_reset = faad_get1bit(ld
+                    DEBUGVAR(1,0,"ics_info(): pred.predictor_reset"))) & 1)
+                {
+                    ics->pred.predictor_reset_group_number = faad_getbits(ld, 5
+                        DEBUGVAR(1,0,"ics_info(): pred.predictor_reset_group_number"));
+                }
 
                 for (sfb = 0; sfb < ics->pred.limit; sfb++)
-                    ics->pred.prediction_used[sfb] = faad_get1bit(ld);
+                {
+                    ics->pred.prediction_used[sfb] = faad_get1bit(ld
+                        DEBUGVAR(1,0,"ics_info(): pred.prediction_used"));
+                }
             }
         }
     }
@@ -237,12 +307,16 @@
 {
     int i;
 
-    pul->number_pulse = faad_getbits(ld, 2);
-    pul->pulse_start_sfb = faad_getbits(ld, 6);
+    pul->number_pulse = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"pulse_data(): number_pulse"));
+    pul->pulse_start_sfb = faad_getbits(ld, 6
+        DEBUGVAR(1,0,"pulse_data(): pulse_start_sfb"));
 
     for (i = 0; i < pul->number_pulse+1; i++) {
-        pul->pulse_offset[i] = faad_getbits(ld, 5);
-        pul->pulse_amp[i] = faad_getbits(ld, 4);
+        pul->pulse_offset[i] = faad_getbits(ld, 5
+            DEBUGVAR(1,0,"pulse_data(): pulse_offset"));
+        pul->pulse_amp[i] = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"pulse_data(): pulse_amp"));
     }
 }
 
@@ -252,16 +326,25 @@
     int i, byte_aligned, count;
     char data_stream_byte;
 
-    /* element_instance_tag = */ faad_getbits(ld, LEN_TAG);
-    byte_aligned = faad_get1bit(ld);
-    count = faad_getbits(ld, 8);
+    /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
+        DEBUGVAR(1,0,"data_stream_element(): element_instance_tag"));
+    byte_aligned = faad_get1bit(ld
+        DEBUGVAR(1,0,"data_stream_element(): byte_aligned"));
+    count = faad_getbits(ld, 8
+        DEBUGVAR(1,0,"data_stream_element(): count"));
     if (count == 255)
-        count += faad_getbits(ld, 8);
+    {
+        count += faad_getbits(ld, 8
+            DEBUGVAR(1,0,"data_stream_element(): extra count"));
+    }
     if (byte_aligned)
         faad_byte_align(ld);
 
     for (i = 0; i < count; i++)
-        data_stream_byte = faad_getbits(ld, LEN_BYTE);
+    {
+        data_stream_byte = faad_getbits(ld, LEN_BYTE
+            DEBUGVAR(1,0,"data_stream_element(): data_stream_byte"));
+    }
 
     return count;
 }
@@ -271,9 +354,13 @@
 {
     int count;
 
-    count = faad_getbits(ld, 4);
+    count = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"fill_element(): count"));
     if (count == 15)
-        count += faad_getbits(ld, 8) - 1;
+    {
+        count += faad_getbits(ld, 8
+            DEBUGVAR(1,0,"fill_element(): extra count")) - 1;
+    }
 
     while (count > 0)
     {
@@ -291,7 +378,8 @@
 {
     int result;
 
-    ics->global_gain = faad_getbits(ld, 8);
+    ics->global_gain = faad_getbits(ld, 8
+        DEBUGVAR(1,0,"individual_channel_stream(): global_gain"));
 
     if (!ele->common_window && !scal_flag)
     {
@@ -306,16 +394,25 @@
     if (!scal_flag)
     {
         /* get pulse data */
-        if ((ics->pulse_data_present = faad_get1bit(ld)) & 1)
+        if ((ics->pulse_data_present = faad_get1bit(ld
+            DEBUGVAR(1,0,"individual_channel_stream(): pulse_data_present"))) & 1)
+        {
             pulse_data(&ics->pul, ld);
+        }
 
         /* get tns data */
-        if ((ics->tns_data_present = faad_get1bit(ld)) & 1)
+        if ((ics->tns_data_present = faad_get1bit(ld
+            DEBUGVAR(1,0,"individual_channel_stream(): tns_data_present"))) & 1)
+        {
             tns_data(ics, &ics->tns, ld);
+        }
 
         /* get gain control data */
-        if ((ics->gain_control_data_present = faad_get1bit(ld)) & 1)
+        if ((ics->gain_control_data_present = faad_get1bit(ld
+            DEBUGVAR(1,0,"individual_channel_stream(): gain_control_data_present"))) & 1)
+        {
             return 1;
+        }
     }
 
     /* decode the spectral data */
@@ -357,10 +454,14 @@
             int sect_len_incr;
             int sect_len = 0;
 
-            ics->sect_cb[g][i] = faad_getbits(ld, 4);
+            ics->sect_cb[g][i] = faad_getbits(ld, 4
+                DEBUGVAR(1,0,"section_data(): sect_cb"));
 
-            while ((sect_len_incr = faad_getbits(ld, sect_bits)) == sect_esc_val)
+            while ((sect_len_incr = faad_getbits(ld, sect_bits
+                DEBUGVAR(1,0,"section_data(): sect_len_incr"))) == sect_esc_val)
+            {
                 sect_len += sect_esc_val;
+            }
             sect_len += sect_len_incr;
 
             ics->sect_start[g][i] = k;
@@ -413,11 +514,14 @@
             case NOISE_HCB: /* noise books */
 
                 /* decode noise energy */
-                if (noise_pcm_flag) {
+                if (noise_pcm_flag)
+                {
                     noise_pcm_flag = 0;
-                    t = faad_getbits(ld, 9) - 256;
-                } else
+                    t = faad_getbits(ld, 9
+                        DEBUGVAR(1,0,"scale_factor_data(): first noise")) - 256;
+                } else {
                     t = huffman_scale_factor(ld) - 60;
+                }
                 noise_energy += t;
                 ics->scale_factors[g][sfb] = noise_energy;
 
@@ -444,7 +548,7 @@
 /* Table 4.4.27 */
 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
 {
-    int w, filt, i, coef_bits;
+    int w, filt, i, start_coef_bits, coef_bits;
     int n_filt_bits = 2;
     int length_bits = 6;
     int order_bits = 5;
@@ -458,29 +562,38 @@
 
     for (w = 0; w < ics->num_windows; w++)
     {
-        tns->n_filt[w] = faad_getbits(ld, n_filt_bits);
+        tns->n_filt[w] = faad_getbits(ld, n_filt_bits
+            DEBUGVAR(1,0,"tns_data(): n_filt"));
 
         if (tns->n_filt[w])
         {
-            if ((tns->coef_res[w] = faad_get1bit(ld)) & 1)
-                coef_bits = 4;
-            else
-                coef_bits = 3;
+            if ((tns->coef_res[w] = faad_get1bit(ld
+                DEBUGVAR(1,0,"tns_data(): coef_res"))) & 1)
+            {
+                start_coef_bits = 4;
+            } else {
+                start_coef_bits = 3;
+            }
         }
 
         for (filt = 0; filt < tns->n_filt[w]; filt++)
         {
-            tns->length[w][filt] = faad_getbits(ld, length_bits);
-            tns->order[w][filt]  = faad_getbits(ld, order_bits);
+            tns->length[w][filt] = faad_getbits(ld, length_bits
+                DEBUGVAR(1,0,"tns_data(): length"));
+            tns->order[w][filt]  = faad_getbits(ld, order_bits
+                DEBUGVAR(1,0,"tns_data(): order"));
             if (tns->order[w][filt])
             {
-                tns->direction[w][filt] = faad_get1bit(ld);
-                tns->coef_compress[w][filt] = faad_get1bit(ld);
+                tns->direction[w][filt] = faad_get1bit(ld
+                    DEBUGVAR(1,0,"tns_data(): direction"));
+                tns->coef_compress[w][filt] = faad_get1bit(ld
+                    DEBUGVAR(1,0,"tns_data(): coef_compress"));
 
-                coef_bits -= tns->coef_compress[w][filt];
+                coef_bits = start_coef_bits - tns->coef_compress[w][filt];
                 for (i = 0; i < tns->order[w][filt]; i++)
                 {
-                    tns->coef[w][filt][i] = faad_getbits(ld, coef_bits);
+                    tns->coef[w][filt][i] = faad_getbits(ld, coef_bits
+                        DEBUGVAR(1,0,"tns_data(): coef"));
                 }
             }
         }
@@ -496,18 +609,25 @@
 {
     int sfb, w;
 
-    ltp->lag = faad_getbits(ld, 11);
-    ltp->coef = faad_getbits(ld, 3);
+    ltp->lag = faad_getbits(ld, 11
+        DEBUGVAR(1,0,"ltp_data(): lag"));
+    ltp->coef = faad_getbits(ld, 3
+        DEBUGVAR(1,0,"ltp_data(): coef"));
 
     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
     {
         for (w = 0; w < ics->num_windows; w++)
         {
-            if ((ltp->short_used[w] = faad_get1bit(ld)) & 1)
+            if ((ltp->short_used[w] = faad_get1bit(ld
+                DEBUGVAR(1,0,"ltp_data(): short_used"))) & 1)
             {
-                ltp->short_lag_present[w] = faad_get1bit(ld);
+                ltp->short_lag_present[w] = faad_get1bit(ld
+                    DEBUGVAR(1,0,"ltp_data(): short_lag_present"));
                 if (ltp->short_lag_present[w])
-                    ltp->short_lag[w] = faad_getbits(ld, 4);
+                {
+                    ltp->short_lag[w] = faad_getbits(ld, 4
+                        DEBUGVAR(1,0,"ltp_data(): short_lag"));
+                }
 			}
         }
     } else {
@@ -514,7 +634,10 @@
         ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
 
         for (sfb = 0; sfb < ltp->last_band; sfb++)
-            ltp->long_used[sfb] = faad_get1bit(ld);
+        {
+            ltp->long_used[sfb] = faad_get1bit(ld
+                DEBUGVAR(1,0,"ltp_data(): long_used"));
+        }
     }
 }
 
@@ -529,6 +652,7 @@
     short *sp;
     int p = 0;
     int groups = 0;
+    int sect_cb;
 
     sp = spectral_data;
     for (i = 1024/16-1; i >= 0; --i)
@@ -545,35 +669,34 @@
 
         for (i = 0; i < ics->num_sec[g]; i++)
         {
-            switch (ics->sect_cb[g][i])
+            sect_cb = ics->sect_cb[g][i];
+
+            if ((sect_cb == ZERO_HCB) ||
+                (sect_cb == NOISE_HCB) ||
+                (sect_cb == INTENSITY_HCB) ||
+                (sect_cb == INTENSITY_HCB2))
             {
-            case ZERO_HCB:
-            case NOISE_HCB:
-            case INTENSITY_HCB:
-            case INTENSITY_HCB2:
                 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
                     ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
-                break;
-            default:
+            } else {
                 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
                      k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; )
                 {
                     sp = spectral_data + p;
 
-                    inc = (ics->sect_cb[g][i] < FIRST_PAIR_HCB)?QUAD_LEN:PAIR_LEN;
+                    inc = (sect_cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
 
-                    huffman_spectral_data(ics->sect_cb[g][i], ld, sp);
-                    if (unsigned_cb[ics->sect_cb[g][i]])
+                    huffman_spectral_data(sect_cb, ld, sp);
+                    if (unsigned_cb[sect_cb])
                         huffman_sign_bits(ld, sp, inc);
                     k += inc;
                     p += inc;
-                    if (ics->sect_cb[g][i] == ESC_HCB)
+                    if (sect_cb == ESC_HCB)
                     {
                         sp[0] = huffman_getescape(ld, sp[0]);
                         sp[1] = huffman_getescape(ld, sp[1]);
                     }
                 }
-                break;
             }
         }
         groups += ics->window_group_length[g];
@@ -586,7 +709,8 @@
 static int extension_payload(bitfile *ld, drc_info *drc, int count)
 {
     int i, n;
-    int extension_type = faad_getbits(ld, 4);
+    int extension_type = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"extension_payload(): extension_type"));
 
     switch (extension_type)
     {
@@ -595,13 +719,22 @@
         n = dynamic_range_info(ld, drc);
         return n;
     case EXT_FILL_DATA:
-        /* fill_nibble = */ faad_getbits(ld, 4); /* must be �0000� */
+        /* fill_nibble = */ faad_getbits(ld, 4
+            DEBUGVAR(1,0,"extension_payload(): fill_nibble")); /* must be �0000� */
         for (i = 0; i < count-1; i++)
-            /* fill_byte[i] = */ faad_getbits(ld, 8); /* must be �10100101� */
+        {
+            /* fill_byte[i] = */ faad_getbits(ld, 8
+                DEBUGVAR(1,0,"extension_payload(): fill_byte")); /* must be �10100101� */
+        }
         return count;
     default:
-        for (i = 0; i < 8*(count-1)+4; i++)
-            /* other_bits[i] = */ faad_get1bit(ld);
+        faad_getbits(ld, 4
+            DEBUGVAR(1,0,"extension_payload(): fill_nibble"));
+        for (i = 0; i < count-1; i++)
+        {
+            /* other_bits[i] = */ faad_getbits(ld, 8
+               DEBUGVAR(1,0,"extension_payload(): fill_byte"));
+        }
         return count;
     }
 }
@@ -614,44 +747,57 @@
 
     drc->num_bands = 1;
 
-    if (faad_get1bit(ld) & 1)
+    if (faad_get1bit(ld
+        DEBUGVAR(1,0,"dynamic_range_info(): has instance_tag")) & 1)
     {
-        drc->pce_instance_tag = faad_getbits(ld, 4);
-        /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4);
+        drc->pce_instance_tag = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"dynamic_range_info(): pce_instance_tag"));
+        /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
+            DEBUGVAR(1,0,"dynamic_range_info(): drc_tag_reserved_bits"));
         n++;
     }
 
-    drc->excluded_chns_present = faad_get1bit(ld);
+    drc->excluded_chns_present = faad_get1bit(ld
+        DEBUGVAR(1,0,"dynamic_range_info(): excluded_chns_present"));
     if (drc->excluded_chns_present == 1)
     {
         n += excluded_channels(ld, drc);
     }
 
-    if (faad_get1bit(ld) & 1)
+    if (faad_get1bit(ld
+        DEBUGVAR(1,0,"dynamic_range_info(): has bands data")) & 1)
     {
-        band_incr = faad_getbits(ld, 4);
-        /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4);
+        band_incr = faad_getbits(ld, 4
+            DEBUGVAR(1,0,"dynamic_range_info(): band_incr"));
+        /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
+            DEBUGVAR(1,0,"dynamic_range_info(): drc_bands_reserved_bits"));
         n++;
         drc->num_bands += band_incr;
 
         for (i = 0; i < drc->num_bands; i++);
         {
-            drc->band_top[i] = faad_getbits(ld, 8);
+            drc->band_top[i] = faad_getbits(ld, 8
+                DEBUGVAR(1,0,"dynamic_range_info(): band_top"));
             n++;
         }
     }
 
-    if (faad_get1bit(ld) & 1)
+    if (faad_get1bit(ld
+        DEBUGVAR(1,0,"dynamic_range_info(): has prog_ref_level")) & 1)
     {
-        drc->prog_ref_level = faad_getbits(ld, 7);
-        /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld);
+        drc->prog_ref_level = faad_getbits(ld, 7
+            DEBUGVAR(1,0,"dynamic_range_info(): prog_ref_level"));
+        /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
+            DEBUGVAR(1,0,"dynamic_range_info(): prog_ref_level_reserved_bits"));
         n++;
     }
 
     for (i = 0; i < drc->num_bands; i++)
     {
-        drc->dyn_rng_sgn[i] = faad_get1bit(ld);
-        drc->dyn_rng_ctl[i] = faad_getbits(ld, 7);
+        drc->dyn_rng_sgn[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"dynamic_range_info(): dyn_rng_sgn"));
+        drc->dyn_rng_ctl[i] = faad_getbits(ld, 7
+            DEBUGVAR(1,0,"dynamic_range_info(): dyn_rng_ctl"));
         n++;
     }
 
@@ -665,13 +811,20 @@
     int num_excl_chan = 7;
 
     for (i = 0; i < 7; i++)
-        drc->exclude_mask[i] = faad_get1bit(ld);
+    {
+        drc->exclude_mask[i] = faad_get1bit(ld
+            DEBUGVAR(1,0,"excluded_channels(): exclude_mask"));
+    }
     n++;
 
-    while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld)) == 1)
+    while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
+        DEBUGVAR(1,0,"excluded_channels(): additional_excluded_chns"))) == 1)
     {
         for (i = num_excl_chan; i < num_excl_chan+7; i++)
-            drc->exclude_mask[i] = faad_get1bit(ld);
+        {
+            drc->exclude_mask[i] = faad_get1bit(ld
+                DEBUGVAR(1,0,"excluded_channels(): exclude_mask"));
+        }
         n++;
         num_excl_chan += 7;
     }
@@ -686,29 +839,45 @@
 {
     int i;
 
-    /* adif_id[0] = */ faad_getbits(ld, 8);
-    /* adif_id[1] = */ faad_getbits(ld, 8);
-    /* adif_id[2] = */ faad_getbits(ld, 8);
-    /* adif_id[3] = */ faad_getbits(ld, 8);
-    adif->copyright_id_present = faad_get1bit(ld);
+    /* adif_id[0] = */ faad_getbits(ld, 8
+        DEBUGVAR(1,0,"get_adif_header(): adif_id[0]"));
+    /* adif_id[1] = */ faad_getbits(ld, 8
+        DEBUGVAR(1,0,"get_adif_header(): adif_id[1]"));
+    /* adif_id[2] = */ faad_getbits(ld, 8
+        DEBUGVAR(1,0,"get_adif_header(): adif_id[2]"));
+    /* adif_id[3] = */ faad_getbits(ld, 8
+        DEBUGVAR(1,0,"get_adif_header(): adif_id[3]"));
+    adif->copyright_id_present = faad_get1bit(ld
+        DEBUGVAR(1,0,"get_adif_header(): copyright_id_present"));
     if(adif->copyright_id_present)
     {
         for (i = 0; i < 72/8; i++)
-            adif->copyright_id[i] = faad_getbits(ld, 8);
+        {
+            adif->copyright_id[i] = faad_getbits(ld, 8
+                DEBUGVAR(1,0,"get_adif_header(): copyright_id"));
+        }
         adif->copyright_id[i] = 0;
     }
-    adif->original_copy  = faad_get1bit(ld);
-    adif->home = faad_get1bit(ld);
-    adif->bitstream_type = faad_get1bit(ld);
-    adif->bitrate = faad_getbits(ld, 23);
-    adif->num_program_config_elements = faad_getbits(ld, 4);
+    adif->original_copy  = faad_get1bit(ld
+        DEBUGVAR(1,0,"get_adif_header(): original_copy"));
+    adif->home = faad_get1bit(ld
+        DEBUGVAR(1,0,"get_adif_header(): home"));
+    adif->bitstream_type = faad_get1bit(ld
+        DEBUGVAR(1,0,"get_adif_header(): bitstream_type"));
+    adif->bitrate = faad_getbits(ld, 23
+        DEBUGVAR(1,0,"get_adif_header(): bitrate"));
+    adif->num_program_config_elements = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"get_adif_header(): num_program_config_elements"));
 
     for (i = 0; i < adif->num_program_config_elements + 1; i++)
     {
         if(adif->bitstream_type == 0)
-            adif->adif_buffer_fullness = faad_getbits(ld, 20);
-        else
+        {
+            adif->adif_buffer_fullness = faad_getbits(ld, 20
+                DEBUGVAR(1,0,"get_adif_header(): adif_buffer_fullness"));
+        } else {
             adif->adif_buffer_fullness = 0;
+        }
 
         program_config_element(&adif->pce, ld);
     }
@@ -729,20 +898,34 @@
 /* Table 1.A.6 */
 static int adts_fixed_header(adts_header *adts, bitfile *ld)
 {
-    adts->syncword = faad_getbits(ld, 12);
+    adts->syncword = faad_getbits(ld, 12
+        DEBUGVAR(1,0,"adts_fixed_header(): syncword"));
     if (adts->syncword != 0xFFF)
         return 5;
-    adts->id = faad_get1bit(ld);
-    adts->layer = faad_getbits(ld, 2);
-    adts->protection_absent = faad_get1bit(ld);
-    adts->profile = faad_getbits(ld, 2);
-    adts->sf_index = faad_getbits(ld, 4);
-    adts->private_bit = faad_get1bit(ld);
-    adts->channel_configuration = faad_getbits(ld, 3);
-    adts->original = faad_get1bit(ld);
-    adts->home = faad_get1bit(ld);
+
+    adts->id = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_fixed_header(): id"));
+    adts->layer = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"adts_fixed_header(): layer"));
+    adts->protection_absent = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_fixed_header(): protection_absent"));
+    adts->profile = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"adts_fixed_header(): profile"));
+    adts->sf_index = faad_getbits(ld, 4
+        DEBUGVAR(1,0,"adts_fixed_header(): sf_index"));
+    adts->private_bit = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_fixed_header(): private_bit"));
+    adts->channel_configuration = faad_getbits(ld, 3
+        DEBUGVAR(1,0,"adts_fixed_header(): channel_configuration"));
+    adts->original = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_fixed_header(): original"));
+    adts->home = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_fixed_header(): home"));
     if (adts->id == 0)
-        adts->emphasis = faad_getbits(ld, 2);
+    {
+        adts->emphasis = faad_getbits(ld, 2
+            DEBUGVAR(1,0,"adts_fixed_header(): emphasis"));
+    }
 
     return 0;
 }
@@ -750,11 +933,16 @@
 /* Table 1.A.7 */
 static void adts_variable_header(adts_header *adts, bitfile *ld)
 {
-    adts->copyright_identification_bit = faad_get1bit(ld);
-    adts->copyright_identification_start = faad_get1bit(ld);
-    adts->aac_frame_length = faad_getbits(ld, 13);
-    adts->adts_buffer_fullness = faad_getbits(ld, 11);
-    adts->no_raw_data_blocks_in_frame = faad_getbits(ld, 2);
+    adts->copyright_identification_bit = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_variable_header(): copyright_identification_bit"));
+    adts->copyright_identification_start = faad_get1bit(ld
+        DEBUGVAR(1,0,"adts_variable_header(): copyright_identification_start"));
+    adts->aac_frame_length = faad_getbits(ld, 13
+        DEBUGVAR(1,0,"adts_variable_header(): aac_frame_length"));
+    adts->adts_buffer_fullness = faad_getbits(ld, 11
+        DEBUGVAR(1,0,"adts_variable_header(): adts_buffer_fullness"));
+    adts->no_raw_data_blocks_in_frame = faad_getbits(ld, 2
+        DEBUGVAR(1,0,"adts_variable_header(): no_raw_data_blocks_in_frame"));
 }
 
 /* Table 1.A.8 */
@@ -761,5 +949,8 @@
 static void adts_error_check(adts_header *adts, bitfile *ld)
 {
     if (adts->protection_absent == 0)
-        adts->crc_check = faad_getbits(ld, 16);
+    {
+        adts->crc_check = faad_getbits(ld, 16
+            DEBUGVAR(1,0,"adts_error_check(): crc_check"));
+    }
 }
--- a/libfaad/syntax.h
+++ b/libfaad/syntax.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: syntax.h,v 1.1 2002/01/14 19:15:57 menno Exp $
+** $Id: syntax.h,v 1.2 2002/01/19 09:39:41 menno Exp $
 **/
 
 #ifndef __SYNTAX_H__
@@ -64,6 +64,17 @@
 #define LONG_START_SEQUENCE  0x1
 #define EIGHT_SHORT_SEQUENCE 0x2
 #define LONG_STOP_SEQUENCE   0x3
+
+#define ZERO_HCB       0
+#define FIRST_PAIR_HCB 5
+#define ESC_HCB        11
+#define QUAD_LEN       4
+#define PAIR_LEN       2
+#define NSPECBOOKS     ESC_HCB + 1
+#define BOOKSCL        NSPECBOOKS
+#define NOISE_HCB      13
+#define INTENSITY_HCB2 14
+#define INTENSITY_HCB  15
 
 
 typedef struct