shithub: aacdec

Download patch

ref: faa53495f9a92232ccf8bf7631c8e05b0882b5cc
parent: 580f32e643ef52c2e7603fee6064917d332df61b
author: menno <menno>
date: Sat Jan 10 13:52:47 EST 2004

SBR bitstream reading moved to a more logical place (this may have broken the DRM decoder)
made bitstream reading error handling a bit simpler

--- a/libfaad/bits.c
+++ b/libfaad/bits.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: bits.c,v 1.30 2004/01/05 14:05:11 menno Exp $
+** $Id: bits.c,v 1.31 2004/01/10 18:52:47 menno Exp $
 **/
 
 #include "common.h"
@@ -99,8 +99,13 @@
     uint32_t tmp;
 
     ld->bufa = ld->bufb;
-    tmp = getdword(ld->tail);
-    ld->tail++;
+    if (ld->no_more_reading == 0)
+    {
+        tmp = getdword(ld->tail);
+        ld->tail++;
+    } else {
+        tmp = 0;
+    }
     ld->bufb = tmp;
     ld->bits_left += (32 - bits);
     ld->bytes_used += 4;
--- 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.29 2004/01/05 14:05:11 menno Exp $
+** $Id: bits.h,v 1.30 2004/01/10 18:52:47 menno Exp $
 **/
 
 #ifndef __BITS_H__
@@ -126,8 +126,8 @@
 static INLINE void faad_flushbits(bitfile *ld, uint32_t bits)
 {
     /* do nothing if error */
-    if (ld->error != 0)
-        return;
+//    if (ld->error != 0)
+//        return;
 
     if (bits < ld->bits_left)
     {
@@ -142,8 +142,8 @@
 {
     uint32_t ret;
 
-    if (ld->no_more_reading || n == 0)
-        return 0;
+//    if (ld->no_more_reading || n == 0)
+//        return 0;
 
     ret = faad_showbits(ld, n);
     faad_flushbits(ld, n);
@@ -160,12 +160,16 @@
 {
     uint8_t r;
 
-    if (ld->bits_left == 0)
-        return (uint8_t)faad_getbits(ld, 1 DEBUGVAR(print,var,dbg));
+    if (ld->bits_left > 0)
+    {
+        ld->bits_left--;
+        r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
+        return r;
+    }
 
-    ld->bits_left--;
-    r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
-
+    /* bits_left == 0 */
+    r = (uint8_t)(ld->bufb >> 31);
+    faad_flushbits_ex(ld, 1);
     return r;
 }
 
--- a/libfaad/common.h
+++ b/libfaad/common.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: common.h,v 1.42 2004/01/05 14:05:11 menno Exp $
+** $Id: common.h,v 1.43 2004/01/10 18:52:47 menno Exp $
 **/
 
 #ifndef __COMMON_H__
@@ -100,6 +100,7 @@
 
 #define SBR_DEC
 //#define SBR_LOW_POWER
+//#define PS_DEC
 
 /* FIXED POINT: No MAIN decoding, forced SBR Low Power decoder */
 #ifdef FIXED_POINT
--- 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.85 2004/01/06 11:59:48 menno Exp $
+** $Id: decoder.c,v 1.86 2004/01/10 18:52:47 menno Exp $
 **/
 
 #include "common.h"
@@ -36,8 +36,11 @@
 #include "syntax.h"
 #include "error.h"
 #include "output.h"
+#include "filtbank.h"
+#include "drc.h"
 #ifdef SBR_DEC
 #include "sbr_dec.h"
+#include "sbr_syntax.h"
 #endif
 #ifdef SSR_DEC
 #include "ssr.h"
@@ -850,7 +853,9 @@
 #ifdef SBR_DEC
     if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
     {
-        int32_t i;
+        uint32_t i;
+        uint16_t count = 0;
+        bitfile ld_sbr = {0};
 
         if (bitsconsumed + 8 > buffer_size*8)
         {
@@ -871,11 +876,9 @@
             *prevbufstart++ = tabFlipbits[*pbufend--];
 
         /* Set SBR data */
-        hDecoder->sbr[0]->data = revbuffer;
+        faad_initbits(&ld_sbr, revbuffer, buffer_size);
         /* consider 8 bits from AAC-CRC */
-        hDecoder->sbr[0]->data_size_bits = buffer_size*8 - bitsconsumed - 8;
-        hDecoder->sbr[0]->data_size =
-            bit2byte(hDecoder->sbr[0]->data_size_bits + 8);
+        count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed - 8);
 
         hDecoder->sbr[0]->lcstereo_flag = hDecoder->lcstereo_flag;
 
@@ -883,6 +886,17 @@
         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);
+
+        /* check CRC */
+        /* no need to check it if there was already an error */
+        if (hDecoder->sbr[0]->ret == 0)
+            hDecoder->sbr[0]->ret = faad_check_CRC(&ld_sbr, faad_get_processed_bits(&ld_sbr) - 8);
+
+        faad_endbits(&ld_sbr);
     }
 #endif
 #endif
@@ -964,8 +978,6 @@
                     , 0
 #endif
                     );
-                hDecoder->sbr[i]->data = NULL;
-                hDecoder->sbr[i]->data_size = 0;
                 hDecoder->sbr[i]->id_aac = hDecoder->element_id[i];
             }
 
--- 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.19 2004/01/05 14:05:12 menno Exp $
+** $Id: sbr_dec.c,v 1.20 2004/01/10 18:52:47 menno Exp $
 **/
 
 
@@ -150,7 +150,7 @@
     int16_t i, k, l;
 
     uint8_t dont_process = 0;
-    uint8_t ch, channels, ret;
+    uint8_t ch, channels;
     real_t *ch_buf;
 
     ALIGN qmf_t X[MAX_NTSR][64];
@@ -158,48 +158,15 @@
     ALIGN real_t deg[64];
 #endif
 
-    bitfile *ld = NULL;
-
     channels = (sbr->id_aac == ID_SCE) ? 1 : 2;
 
-    if (sbr->data == NULL || sbr->data_size == 0)
+    if (sbr->ret || (sbr->header_count == 0))
     {
-        ret = 1;
-    } else {
-        ld = (bitfile*)faad_malloc(sizeof(bitfile));
-
-        /* initialise and read the bitstream */
-        faad_initbits(ld, sbr->data, sbr->data_size);
-
-#ifdef DRM
-        if (sbr->Is_DRM_SBR)
-            faad_getbits(ld, 8); /* Skip 8-bit CRC */
-#endif
-
-        ret = sbr_extension_data(ld, sbr);
-
-#ifdef DRM
-        /* Check CRC */
-        if (sbr->Is_DRM_SBR)
-            ld->error = faad_check_CRC(ld, faad_get_processed_bits(ld) - 8);
-#endif
-
-        ret = ld->error ? ld->error : ret;
-        faad_endbits(ld);
-        if (ld) faad_free(ld);
-        ld = NULL;
-    }
-
-    if (sbr->data) faad_free(sbr->data);
-    sbr->data = NULL;
-
-    if (ret || (sbr->header_count == 0))
-    {
         /* don't process just upsample */
         dont_process = 1;
 
         /* Re-activate reset for next frame */
-        if (ret && sbr->Reset)
+        if (sbr->ret && sbr->Reset)
             sbr->bs_start_freq_prev = -1;
     }
 
--- 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.14 2004/01/05 14:05:12 menno Exp $
+** $Id: sbr_dec.h,v 1.15 2004/01/10 18:52:47 menno Exp $
 **/
 
 #ifndef __SBR_DEC_H__
@@ -32,6 +32,9 @@
 extern "C" {
 #endif
 
+#ifdef PS_DEC
+#include "ps_dec.h"
+#endif
 
 /* MAX_NTSRHFG: maximum of number_time_slots * rate + HFGen. 16*2+8 */
 #define MAX_NTSRHFG 40
@@ -58,6 +61,7 @@
 
     uint8_t rate;
     uint8_t just_seeked;
+    uint8_t ret;
 
     uint8_t amp_res[2];
 
@@ -140,9 +144,6 @@
     uint32_t frame;
     uint32_t header_count;
 
-    uint8_t *data;
-    uint16_t data_size;
-
     uint8_t id_aac;
     qmfa_info *qmfa[2];
     qmfs_info *qmfs[2];
@@ -153,7 +154,6 @@
 #ifdef DRM
 	int8_t lcstereo_flag;
 	uint8_t bs_dataextra;
-	uint16_t data_size_bits;
     uint8_t Is_DRM_SBR;
 #endif
 
@@ -161,6 +161,10 @@
 	uint8_t numTimeSlots;
 	uint8_t tHFGen;
 	uint8_t tHFAdj;
+
+#ifdef PS_DEC
+    ps_info ps;
+#endif
 
     /* to get it compiling */
     /* we'll see during the coding of all the tools, whether
--- a/libfaad/sbr_syntax.c
+++ b/libfaad/sbr_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: sbr_syntax.c,v 1.18 2004/01/05 14:05:12 menno Exp $
+** $Id: sbr_syntax.c,v 1.19 2004/01/10 18:52:47 menno Exp $
 **/
 
 #include "common.h"
@@ -37,11 +37,16 @@
 #include "sbr_tf_grid.h"
 #include "sbr_e_nf.h"
 #include "bits.h"
+#ifdef PS_DEC
+#include "ps_dec.h"
+#endif
 #include "analysis.h"
 
 /* static function declarations */
 static void sbr_header(bitfile *ld, sbr_info *sbr);
 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr);
+static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
+                              uint8_t bs_extension_id, uint16_t num_bits_left);
 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr);
 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr);
 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch);
@@ -98,9 +103,11 @@
 }
 
 /* table 2 */
-uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr)
+uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt)
 {
     uint8_t result;
+    uint16_t num_align_bits = 0;
+    uint16_t num_sbr_bits = (uint16_t)faad_get_processed_bits(ld);
 
 #ifdef DRM
     if (!sbr->Is_DRM_SBR)
@@ -122,52 +129,65 @@
     if (sbr->bs_header_flag)
         sbr_header(ld, sbr);
 
-    /* TODO: Reset? */
+    /* Reset? */
     sbr_reset(sbr);
 
     /* first frame should have a header */
-    if (sbr->frame == 0 && sbr->bs_header_flag == 0)
-        return 1;
-
-
-    if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))
+    //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0))
+    if (sbr->header_count != 0)
     {
-        uint8_t k2;
+        if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))
+        {
+            uint8_t k2;
 
-        /* calculate the Master Frequency Table */
-        sbr->k0 = qmf_start_channel(sbr->bs_start_freq, sbr->bs_samplerate_mode,
-            sbr->sample_rate);
-        k2 = qmf_stop_channel(sbr->bs_stop_freq, sbr->sample_rate, sbr->k0);
+            /* calculate the Master Frequency Table */
+            sbr->k0 = qmf_start_channel(sbr->bs_start_freq, sbr->bs_samplerate_mode,
+                sbr->sample_rate);
+            k2 = qmf_stop_channel(sbr->bs_stop_freq, sbr->sample_rate, sbr->k0);
 
-        /* check k0 and k2 */
-        if (sbr->sample_rate >= 48000)
-        {
-            if ((k2 - sbr->k0) > 32)
-                return 1;
-        } else if (sbr->sample_rate <= 32000) {
-            if ((k2 - sbr->k0) > 48)
-                return 1;
-        } else { /* (sbr->sample_rate == 44100) */
-            if ((k2 - sbr->k0) > 45)
-                return 1;
-        }
+            /* check k0 and k2 */
+            if (sbr->sample_rate >= 48000)
+            {
+                if ((k2 - sbr->k0) > 32)
+                    return 1;
+            } else if (sbr->sample_rate <= 32000) {
+                if ((k2 - sbr->k0) > 48)
+                    return 1;
+            } else { /* (sbr->sample_rate == 44100) */
+                if ((k2 - sbr->k0) > 45)
+                    return 1;
+            }
 
-        if (sbr->bs_freq_scale == 0)
-        {
-            master_frequency_table_fs0(sbr, sbr->k0, k2, sbr->bs_alter_scale);
-        } else {
-            master_frequency_table(sbr, sbr->k0, k2, sbr->bs_freq_scale,
-                sbr->bs_alter_scale);
+            if (sbr->bs_freq_scale == 0)
+            {
+                master_frequency_table_fs0(sbr, sbr->k0, k2, sbr->bs_alter_scale);
+            } else {
+                master_frequency_table(sbr, sbr->k0, k2, sbr->bs_freq_scale,
+                    sbr->bs_alter_scale);
+            }
+            if ((result = derived_frequency_table(sbr, sbr->bs_xover_band, k2)) > 0)
+                return result;
         }
-        if ((result = derived_frequency_table(sbr, sbr->bs_xover_band, k2)) > 0)
-            return result;
+
+        result = sbr_data(ld, sbr);
+    } else {
+        result = 1;
     }
 
-    if ((result = sbr_data(ld, sbr)) > 0)
-        return result;
+    num_sbr_bits = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits;
+    /* -4 does not apply, bs_extension_type is re-read in this function */
+    num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits;
 
-    /* no error */
-    return 0;
+    while (num_align_bits > 7)
+    {
+        faad_getbits(ld, 8
+            DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
+        num_align_bits -= 8;
+    }
+    faad_getbits(ld, num_align_bits
+        DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
+
+    return result;
 }
 
 /* table 3 */
@@ -330,19 +350,15 @@
             sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
                 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
             nr_bits_left -= 2;
-            /* sbr_extension(ld, sbr, 0, nr_bits_left); */
-#ifdef DRM
-            if (!sbr->Is_DRM_SBR)
-#endif
-            {
-                sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6
-                    DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
-            }
+            nr_bits_left -= sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
         }
 
         /* Corrigendum */
-        faad_getbits(ld, nr_bits_left
-            DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
+        if (nr_bits_left > 0)
+        {
+            faad_getbits(ld, nr_bits_left
+                DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
+        }
     }
 
     return 0;
@@ -461,19 +477,15 @@
             sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
                 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
             nr_bits_left -= 2;
-            /* sbr_extension(ld, sbr, 0, nr_bits_left); */
-#ifdef DRM
-            if (!sbr->Is_DRM_SBR)
-#endif
-            {
-                sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6
-                    DEBUGVAR(1,280,"sbr_channel_pair_element(): bs_extension_data"));
-            }
+            sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
         }
 
         /* Corrigendum */
-        faad_getbits(ld, nr_bits_left
-            DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
+        if (nr_bits_left > 0)
+        {
+            faad_getbits(ld, nr_bits_left
+                DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
+        }
     }
 
     return 0;
@@ -657,6 +669,27 @@
     {
         sbr->bs_invf_mode[ch][n] = (uint8_t)faad_getbits(ld, 2
             DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
+    }
+}
+
+static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
+                              uint8_t bs_extension_id, uint16_t num_bits_left)
+{
+    switch (bs_extension_id)
+    {
+#ifdef PS_DEC
+    case EXTENSION_ID_PS:
+        return ps_data(&(sbr->ps), ld);
+#endif
+    default:
+#ifdef DRM
+        if (!sbr->Is_DRM_SBR)
+#endif
+        {
+            sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6
+                DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
+            return 6;
+        }
     }
 }
 
--- a/libfaad/sbr_syntax.h
+++ b/libfaad/sbr_syntax.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_syntax.h,v 1.14 2004/01/05 14:05:12 menno Exp $
+** $Id: sbr_syntax.h,v 1.15 2004/01/10 18:52:47 menno Exp $
 **/
 
 #ifndef __SBR_SYNTAX_H__
@@ -55,7 +55,7 @@
 #define NOISE_FLOOR_OFFSET 6
 
 
-uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr);
+uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt);
 
 #ifdef __cplusplus
 }
--- 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.64 2004/01/05 14:05:12 menno Exp $
+** $Id: syntax.c,v 1.65 2004/01/10 18:52:47 menno Exp $
 **/
 
 /*
@@ -443,14 +443,6 @@
 #endif
                     )) > 0)
                     return;
-#ifdef SBR_DEC
-                if (hDecoder->sbr_used[ch_ele-1])
-                {
-                    hDecoder->sbr_present_flag = 1;
-                    hDecoder->sbr[ch_ele-1]->sample_rate = get_sample_rate(hDecoder->sf_index);
-                    hDecoder->sbr[ch_ele-1]->sample_rate *= 2;
-                }
-#endif
                 break;
             }
         }
@@ -958,11 +950,14 @@
                     );
             }
 
-            /* read in all the SBR data for processing later on */
-            hDecoder->sbr[sbr_ele]->data = (uint8_t*)faad_getbitbuffer(ld, count*8);
-            hDecoder->sbr[sbr_ele]->data_size = count;
+            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