diff options
author | Miguel Freitas <miguelfreitas@users.sourceforge.net> | 2002-12-16 18:59:50 +0000 |
---|---|---|
committer | Miguel Freitas <miguelfreitas@users.sourceforge.net> | 2002-12-16 18:59:50 +0000 |
commit | 94ef6649dd5f4e95337af00dcede2337ea7cfb49 (patch) | |
tree | 07d679ce92b4e4517815abc42394480eebf44904 /src/libfaad/syntax.c | |
parent | 48f4c5809db11a6df4a5e7285d5e60a2ed924e2a (diff) | |
download | xine-lib-94ef6649dd5f4e95337af00dcede2337ea7cfb49.tar.gz xine-lib-94ef6649dd5f4e95337af00dcede2337ea7cfb49.tar.bz2 |
updated libfaad
CVS patchset: 3560
CVS date: 2002/12/16 18:59:50
Diffstat (limited to 'src/libfaad/syntax.c')
-rw-r--r-- | src/libfaad/syntax.c | 763 |
1 files changed, 528 insertions, 235 deletions
diff --git a/src/libfaad/syntax.c b/src/libfaad/syntax.c index d8aa34fef..27c68029b 100644 --- a/src/libfaad/syntax.c +++ b/src/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.2 2002/08/09 22:36:36 miguelfreitas Exp $ +** $Id: syntax.c,v 1.3 2002/12/16 19:02:04 miguelfreitas Exp $ **/ /* @@ -27,9 +27,13 @@ an ADTS header)). */ -#include <stdlib.h> -#include <memory.h> #include "common.h" +#include "structs.h" + +#include <stdlib.h> +#include <string.h> + +#include "decoder.h" #include "syntax.h" #include "specrec.h" #include "huffman.h" @@ -37,20 +41,20 @@ #include "data.h" #include "pulse.h" #include "analysis.h" -#ifdef SBR -#include "sbr_syntax.h" -#endif +#include "drc.h" #ifdef ERROR_RESILIENCE -#include "rvlc_scale_factors.h" +#include "rvlc.h" #endif /* Table 4.4.1 */ int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration, uint8_t object_type, +#ifdef ERROR_RESILIENCE uint8_t *aacSectionDataResilienceFlag, uint8_t *aacScalefactorDataResilienceFlag, uint8_t *aacSpectralDataResilienceFlag, +#endif uint8_t *frameLengthFlag) { uint8_t dependsOnCoreCoder, extensionFlag; @@ -79,6 +83,7 @@ int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration, return -3; } +#ifdef ERROR_RESILIENCE if (extensionFlag == 1) { /* Error resilience not supported yet */ @@ -94,6 +99,7 @@ int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration, /* 1 bit: extensionFlag3 */ } } +#endif return 0; } @@ -233,53 +239,304 @@ uint8_t program_config_element(program_config *pce, bitfile *ld) return 0; } -/* Table 4.4.4 and */ -/* Table 4.4.9 */ -uint8_t single_lfe_channel_element(element *sce, bitfile *ld, int16_t *spec_data, - uint8_t sf_index, uint8_t object_type, - uint16_t frame_len +element *decode_sce_lfe(faacDecHandle hDecoder, + faacDecFrameInfo *hInfo, bitfile *ld, + int16_t **spec_data, real_t **spec_coef, + uint8_t id_syn_ele) +{ + element *ele; + uint8_t channels = hDecoder->fr_channels; + + if (channels+1 >= MAX_CHANNELS) + { + hInfo->error = 12; + return NULL; + } + if (hDecoder->fr_ch_ele+1 >= MAX_SYNTAX_ELEMENTS) + { + hInfo->error = 13; + return NULL; + } + + spec_data[channels] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t)); + spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t)); + + ele = (element*)malloc(sizeof(element)); + memset(ele, 0, sizeof(element)); + ele->ele_id = id_syn_ele; + ele->channel = channels; + ele->paired_channel = -1; + + hInfo->error = single_lfe_channel_element(hDecoder, ele, + ld, spec_data[channels]); + + hDecoder->fr_channels++; + hDecoder->fr_ch_ele++; + + return ele; +} + +element *decode_cpe(faacDecHandle hDecoder, + faacDecFrameInfo *hInfo, bitfile *ld, + int16_t **spec_data, real_t **spec_coef, + uint8_t id_syn_ele) +{ + element *ele; + uint8_t channels = hDecoder->fr_channels; + + if (channels+2 >= MAX_CHANNELS) + { + hInfo->error = 12; + return NULL; + } + if (hDecoder->fr_ch_ele+1 >= MAX_SYNTAX_ELEMENTS) + { + hInfo->error = 13; + return NULL; + } + + spec_data[channels] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t)); + spec_data[channels+1] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t)); + spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t)); + spec_coef[channels+1] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t)); + + ele = (element*)malloc(sizeof(element)); + memset(ele, 0, sizeof(element)); + ele->ele_id = id_syn_ele; + ele->channel = channels; + ele->paired_channel = channels+1; + + hInfo->error = channel_pair_element(hDecoder, ele, + ld, spec_data[channels], spec_data[channels+1]); + + hDecoder->fr_channels += 2; + hDecoder->fr_ch_ele++; + + return ele; +} + +element **raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, + bitfile *ld, element **elements, + int16_t **spec_data, real_t **spec_coef, + program_config *pce, drc_info *drc) +{ + uint8_t id_syn_ele; + uint8_t ch_ele = 0; + + hDecoder->fr_channels = 0; + hDecoder->fr_ch_ele = 0; + #ifdef ERROR_RESILIENCE - ,uint8_t aacSectionDataResilienceFlag, - uint8_t aacScalefactorDataResilienceFlag, - uint8_t aacSpectralDataResilienceFlag + if (hDecoder->object_type < ER_OBJECT_START) + { +#endif + /* Table 4.4.3: raw_data_block() */ + while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID + DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END) + { + switch (id_syn_ele) { + case ID_SCE: + case ID_LFE: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, id_syn_ele); + if (hInfo->error > 0) + return elements; + break; + case ID_CPE: + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, id_syn_ele); + if (hInfo->error > 0) + return elements; + break; + case ID_CCE: /* not implemented yet */ + hInfo->error = 6; + return elements; + case ID_DSE: + data_stream_element(ld); + break; + case ID_PCE: + if ((hInfo->error = program_config_element(pce, ld)) > 0) + return elements; + break; + case ID_FIL: + if ((hInfo->error = fill_element(ld, drc)) > 0) + return elements; + break; + } + } +#ifdef ERROR_RESILIENCE + } else { + /* Table 262: er_raw_data_block() */ + switch (hDecoder->channelConfiguration) + { + case 1: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + if (hInfo->error > 0) + return elements; + break; + case 2: + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + if (hInfo->error > 0) + return elements; + break; + case 3: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + if (hInfo->error > 0) + return elements; + break; + case 4: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + if (hInfo->error > 0) + return elements; + break; + case 5: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + if (hInfo->error > 0) + return elements; + break; + case 6: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_LFE); + if (hInfo->error > 0) + return elements; + break; + case 7: + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_SCE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_cpe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_CPE); + elements[ch_ele++] = decode_sce_lfe(hDecoder, + hInfo, ld, spec_data, spec_coef, ID_LFE); + if (hInfo->error > 0) + return elements; + break; + default: + hInfo->error = 7; + return elements; + } +#if 0 + cnt = bits_to_decode() / 8; + while (cnt >= 1) + { + cnt -= extension_payload(cnt); + } #endif - ) + } +#endif + + return elements; +} + +#ifdef DRM +static uint8_t faad_check_CRC(bitfile *ld) { - ic_stream *ics = &(sce->ics1); + uint16_t len = faad_get_processed_bits(ld) - 8; + uint8_t CRC; + uint16_t r=255; /* Initialize to all ones */ + + /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */ +#define GPOLY 0435 + faad_rewindbits(ld); + + CRC = ~faad_getbits(ld, 8 + DEBUGVAR(1,999,"faad_check_CRC(): CRC")); /* CRC is stored inverted */ + + for (; len>0; len--) + { + r = ( (r << 1) ^ (( ( faad_get1bit(ld + DEBUGVAR(1,998,"")) & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF; + } + if (r != CRC) + { + return 8; + } else { + return 0; + } +} +#endif + +/* Table 4.4.4 and */ +/* Table 4.4.9 */ +static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, + element *sce, bitfile *ld, + int16_t *spec_data) +{ + ic_stream *ics = &(sce->ics1); #ifdef DRM - if (object_type != DRM_ER_LC) + uint8_t result; + + if (hDecoder->object_type != DRM_ER_LC) #endif sce->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag")); - return individual_channel_stream(sce, ld, ics, 0, spec_data, sf_index, - object_type, frame_len -#ifdef ERROR_RESILIENCE - ,aacSectionDataResilienceFlag, - aacScalefactorDataResilienceFlag, - aacSpectralDataResilienceFlag +#ifdef DRM + if (hDecoder->object_type == DRM_ER_LC) + { + individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data); + + if (ics->tns_data_present) + tns_data(ics, &(ics->tns), ld); + + if ((result = faad_check_CRC( ld )) > 0) + return result; + + /* error resilient spectral data decoding */ + if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0) + return result; + + /* pulse coding reconstruction */ + if (ics->pulse_data_present) + { + if (ics->window_sequence != EIGHT_SHORT_SEQUENCE) + pulse_decode(ics, spec_data); + else + return 2; /* pulse coding not allowed for short blocks */ + } + return 0; + } else #endif - ); + + return individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data); } /* Table 4.4.5 */ -uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1, - int16_t *spec_data2, uint8_t sf_index, uint8_t object_type, - uint16_t frame_len -#ifdef ERROR_RESILIENCE - ,uint8_t aacSectionDataResilienceFlag, - uint8_t aacScalefactorDataResilienceFlag, - uint8_t aacSpectralDataResilienceFlag -#endif - ) +static uint8_t channel_pair_element(faacDecHandle hDecoder, element *cpe, + bitfile *ld, int16_t *spec_data1, + int16_t *spec_data2) { uint8_t result; ic_stream *ics1 = &(cpe->ics1); ic_stream *ics2 = &(cpe->ics2); #ifdef DRM - if (object_type != DRM_ER_LC) + if (hDecoder->object_type != DRM_ER_LC) #endif cpe->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag")); @@ -288,8 +545,7 @@ uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1, DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1) { /* both channels have common ics information */ - if ((result = ics_info(ics1, ld, cpe->common_window, sf_index, - object_type, frame_len)) > 0) + if ((result = ics_info(hDecoder, ics1, ld, cpe->common_window)) > 0) return result; ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2 @@ -307,36 +563,90 @@ uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1, } } +#ifdef ERROR_RESILIENCE + if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present)) + { + if ((ics1->ltp.data_present = faad_get1bit(ld + DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) + { + ltp_data(hDecoder, ics1, &(ics1->ltp), ld); + } + } +#endif + memcpy(ics2, ics1, sizeof(ic_stream)); } else { ics1->ms_mask_present = 0; } - if ((result = individual_channel_stream(cpe, ld, ics1, 0, spec_data1, - sf_index, object_type, frame_len -#ifdef ERROR_RESILIENCE - ,aacSectionDataResilienceFlag, - aacScalefactorDataResilienceFlag, - aacSpectralDataResilienceFlag -#endif - )) > 0) + if ((result = individual_channel_stream(hDecoder, cpe, ld, ics1, + 0, spec_data1)) > 0) + { return result; - if ((result = individual_channel_stream(cpe, ld, ics2, 0, spec_data2, - sf_index, object_type, frame_len + } + #ifdef ERROR_RESILIENCE - ,aacSectionDataResilienceFlag, - aacScalefactorDataResilienceFlag, - aacSpectralDataResilienceFlag + if (cpe->common_window && (hDecoder->object_type >= ER_OBJECT_START) && + (ics1->predictor_data_present)) + { + if ((ics1->ltp2.data_present = faad_get1bit(ld + DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) + { + ltp_data(hDecoder, ics1, &(ics1->ltp2), ld); + } + } #endif - )) > 0) + + if ((result = individual_channel_stream(hDecoder, cpe, ld, ics2, + 0, spec_data2)) > 0) + { return result; + } + +#ifdef DRM + if (hDecoder->object_type == DRM_ER_LC) + { + if (ics1->tns_data_present) + tns_data(ics1, &(ics1->tns), ld); + + if (ics1->tns_data_present) + tns_data(ics2, &(ics2->tns), ld); + + if ((result = faad_check_CRC( ld )) > 0) + { + printf("CRC wrong!\n"); + return result; + } + /* error resilient spectral data decoding */ + if ((result = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0) + return result; + if ((result = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0) + return result; + /* pulse coding reconstruction */ + if (ics1->pulse_data_present) + { + if (ics1->window_sequence != EIGHT_SHORT_SEQUENCE) + pulse_decode(ics1, spec_data1); + else + return 2; /* pulse coding not allowed for short blocks */ + } + if (ics2->pulse_data_present) + { + if (ics2->window_sequence != EIGHT_SHORT_SEQUENCE) + pulse_decode(ics2, spec_data2); + else + return 2; /* pulse coding not allowed for short blocks */ + } + return 0; + } else +#endif return 0; } /* Table 4.4.6 */ -static uint8_t ics_info(ic_stream *ics, bitfile *ld, uint8_t common_window, - uint8_t sf_index, uint8_t object_type, uint16_t frame_len) +static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, + uint8_t common_window) { /* ics->ics_reserved_bit = */ faad_get1bit(ld DEBUGVAR(1,43,"ics_info(): ics_reserved_bit")); @@ -358,11 +668,11 @@ static uint8_t ics_info(ic_stream *ics, bitfile *ld, uint8_t common_window, if ((ics->predictor_data_present = faad_get1bit(ld DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1) { - if (object_type == MAIN) /* MPEG2 style AAC predictor */ + if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */ { uint8_t sfb; - ics->pred.limit = min(ics->max_sfb, pred_sfb_max[sf_index]); + ics->pred.limit = min(ics->max_sfb, pred_sfb_max[hDecoder->sf_index]); if ((ics->pred.predictor_reset = faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1) @@ -379,26 +689,39 @@ static uint8_t ics_info(ic_stream *ics, bitfile *ld, uint8_t common_window, } #ifdef LTP_DEC else { /* Long Term Prediction */ - if ((ics->ltp.data_present = faad_get1bit(ld - DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) + if (hDecoder->object_type < ER_OBJECT_START) { - ltp_data(ics, &(ics->ltp), ld, object_type); + if ((ics->ltp.data_present = faad_get1bit(ld + DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) + { + ltp_data(hDecoder, ics, &(ics->ltp), ld); + } + if (common_window) + { + if ((ics->ltp2.data_present = faad_get1bit(ld + DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1) + { + ltp_data(hDecoder, ics, &(ics->ltp2), ld); + } + } } - if (common_window) +#ifdef ERROR_RESILIENCE + if (!common_window && (hDecoder->object_type >= ER_OBJECT_START)) { - if ((ics->ltp2.data_present = faad_get1bit(ld - DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1) + if ((ics->ltp.data_present = faad_get1bit(ld + DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) { - ltp_data(ics, &(ics->ltp2), ld, object_type); + ltp_data(hDecoder, ics, &(ics->ltp), ld); } } +#endif } #endif } } /* get the grouping information */ - return window_grouping_info(ics, sf_index, object_type, frame_len); + return window_grouping_info(hDecoder, ics); } /* Table 4.4.7 */ @@ -411,7 +734,8 @@ static void pulse_data(pulse_info *pul, bitfile *ld) pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6 DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb")); - for (i = 0; i < pul->number_pulse+1; i++) { + for (i = 0; i < pul->number_pulse+1; i++) + { pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,58,"pulse_data(): pulse_offset")); pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4 @@ -420,7 +744,7 @@ static void pulse_data(pulse_info *pul, bitfile *ld) } /* Table 4.4.10 */ -uint16_t data_stream_element(bitfile *ld) +static uint16_t data_stream_element(bitfile *ld) { uint8_t byte_aligned; uint16_t i, count; @@ -429,11 +753,11 @@ uint16_t data_stream_element(bitfile *ld) DEBUGVAR(1,60,"data_stream_element(): element_instance_tag")); byte_aligned = faad_get1bit(ld DEBUGVAR(1,61,"data_stream_element(): byte_aligned")); - count = faad_getbits(ld, 8 + count = (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,62,"data_stream_element(): count")); if (count == 255) { - count += faad_getbits(ld, 8 + count += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,63,"data_stream_element(): extra count")); } if (byte_aligned) @@ -449,17 +773,9 @@ uint16_t data_stream_element(bitfile *ld) } /* Table 4.4.11 */ -uint8_t fill_element(bitfile *ld, drc_info *drc -#ifdef SBR - ,uint8_t next_ele_id -#endif - ) +static uint8_t fill_element(bitfile *ld, drc_info *drc) { uint16_t count; -#ifdef SBR - uint8_t bs_extension_type; - uint32_t btot; -#endif count = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,65,"fill_element(): count")); @@ -469,135 +785,101 @@ uint8_t fill_element(bitfile *ld, drc_info *drc DEBUGVAR(1,66,"fill_element(): extra count")) - 1; } -#ifdef SBR - bs_extension_type = (uint8_t)faad_showbits(ld, 4); - - if (bs_extension_type == SBR_HDR || bs_extension_type == SBR_STD) + while (count > 0) { - uint16_t i; - uint16_t bytes, bits; - - /* flush the extension type and the fill nibble */ - faad_flushbits(ld, 8); - - btot = faad_get_processed_bits(ld); - - /* SBR bitstream reading function */ - sbr_bitstream(next_ele_id, bs_extension_type); - - btot = faad_get_processed_bits(ld) - btot; - - /* there might still be some fill bits left to read */ - bits = (8*(count-1) - btot) % 8; - bytes = ((8*(count-1) - btot) - bits) / 8; - - if (bits > 0) - faad_flushbits(ld, bits); - for (i = 0; i < bytes; i++) - { - faad_flushbits(ld, 8); - } - } else { -#endif - while (count > 0) - { - count -= extension_payload(ld, drc, count); - } -#ifdef SBR + count -= extension_payload(ld, drc, count); } -#endif return 0; } /* Table 4.4.12 */ +#ifdef SSR_DEC static void gain_control_data(bitfile *ld, ic_stream *ics) { uint8_t bd, wd, ad; - uint8_t adjust_num[4][8]; - uint8_t alevcode[4][8][8]; - uint8_t aloccode[4][8][8]; + ssr_info *ssr = &(ics->ssr); - uint8_t max_band = (uint8_t)faad_getbits(ld, 2 + ssr->max_band = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,1000,"gain_control_data(): max_band")); if (ics->window_sequence == ONLY_LONG_SEQUENCE) { - for (bd = 1; bd <= max_band; bd++) + for (bd = 1; bd <= ssr->max_band; bd++) { - for (wd=0; wd<1; wd++) + for (wd = 0; wd < 1; wd++) { - adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 + ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); - for (ad = 0; ad < adjust_num[bd][wd]; ad++) + for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) { - alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } } } } else if (ics->window_sequence == LONG_START_SEQUENCE) { - for (bd = 1; bd <= max_band; bd++) + for (bd = 1; bd <= ssr->max_band; bd++) { for (wd = 0; wd < 2; wd++) { - adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 + ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); - for (ad = 0; ad < adjust_num[bd][wd]; ad++) + for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) { - alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); if (wd == 0) { - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } else { - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } } } } } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) { - for (bd = 1; bd <= max_band; bd++) + for (bd = 1; bd <= ssr->max_band; bd++) { - for(wd=0; wd<8; wd++) + for (wd = 0; wd < 8; wd++) { - adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 + ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); - for (ad = 0; ad < adjust_num[bd][wd]; ad++) + for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) { - alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } } } } else if (ics->window_sequence == LONG_STOP_SEQUENCE) { - for (bd = 1; bd <= max_band; bd++) + for (bd = 1; bd <= ssr->max_band; bd++) { for (wd = 0; wd < 2; wd++) { - adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 + ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); - for (ad = 0; ad < adjust_num[bd][wd]; ad++) + for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) { - alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); if (wd == 0) { - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } else { - aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 + ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); } } @@ -605,18 +887,12 @@ static void gain_control_data(bitfile *ld, ic_stream *ics) } } } +#endif /* Table 4.4.24 */ -static uint8_t individual_channel_stream(element *ele, bitfile *ld, - ic_stream *ics, uint8_t scal_flag, - int16_t *spec_data, uint8_t sf_index, - uint8_t object_type, uint16_t frame_len -#ifdef ERROR_RESILIENCE - ,uint8_t aacSectionDataResilienceFlag, - uint8_t aacScalefactorDataResilienceFlag, - uint8_t aacSpectralDataResilienceFlag -#endif - ) +static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele, + bitfile *ld, ic_stream *ics, uint8_t scal_flag, + int16_t *spec_data) { uint8_t result; @@ -625,20 +901,11 @@ static uint8_t individual_channel_stream(element *ele, bitfile *ld, if (!ele->common_window && !scal_flag) { - if ((result = ics_info(ics, ld, ele->common_window, sf_index, - object_type, frame_len)) > 0) + if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0) return result; } - section_data(ics, ld -#ifdef ERROR_RESILIENCE - ,aacSectionDataResilienceFlag -#endif - ); - if ((result = scale_factor_data(ics, ld -#ifdef ERROR_RESILIENCE - ,aacScalefactorDataResilienceFlag -#endif - )) > 0) + section_data(hDecoder, ics, ld); + if ((result = scale_factor_data(hDecoder, ics, ld)) > 0) return result; if (!scal_flag) @@ -660,7 +927,7 @@ static uint8_t individual_channel_stream(element *ele, bitfile *ld, DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1) { #ifdef ERROR_RESILIENCE - if (object_type < ER_OBJECT_START) + if (hDecoder->object_type < ER_OBJECT_START) #endif tns_data(ics, &(ics->tns), ld); } @@ -669,17 +936,30 @@ static uint8_t individual_channel_stream(element *ele, bitfile *ld, if ((ics->gain_control_data_present = faad_get1bit(ld DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1) { -#if 1 - return 1; +#ifdef SSR_DEC + if (hDecoder->object_type != SSR) + return 1; + else + gain_control_data(ld, ics); #else - gain_control_data(ld, ics); + return 1; #endif } } #ifdef ERROR_RESILIENCE - if (aacSpectralDataResilienceFlag) + if (hDecoder->aacSpectralDataResilienceFlag) { +#if 0 + if (hDecoder->channelConfiguration == 2) + { + if (ics->length_of_reordered_spectral_data > 6144) + ics->length_of_reordered_spectral_data = 6144; + } else { + if (ics->length_of_reordered_spectral_data > 12288) + ics->length_of_reordered_spectral_data = 12288; + } +#endif ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14 DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data")); /* TODO: test for >6144/12288, see page 143 */ @@ -690,30 +970,34 @@ static uint8_t individual_channel_stream(element *ele, bitfile *ld, } /* RVLC spectral data is put here */ - if (aacScalefactorDataResilienceFlag) + if (hDecoder->aacScalefactorDataResilienceFlag) { if ((result = rvlc_decode_scale_factors(ics, ld)) > 0) return result; } - if (object_type >= ER_OBJECT_START) +#ifdef DRM + if (hDecoder->object_type == DRM_ER_LC) + return 0; +#endif + + if (hDecoder->object_type >= ER_OBJECT_START) { if (ics->tns_data_present) tns_data(ics, &(ics->tns), ld); } - if (aacSpectralDataResilienceFlag) + if (hDecoder->aacSpectralDataResilienceFlag) { /* error resilient spectral data decoding */ - if ((result = reordered_spectral_data(ics, ld, spec_data, frame_len, - aacSectionDataResilienceFlag)) > 0) + if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0) { return result; } } else { #endif /* decode the spectral data */ - if ((result = spectral_data(ics, ld, spec_data, frame_len)) > 0) + if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0) { return result; } @@ -734,11 +1018,7 @@ static uint8_t individual_channel_stream(element *ele, bitfile *ld, } /* Table 4.4.25 */ -static void section_data(ic_stream *ics, bitfile *ld -#ifdef ERROR_RESILIENCE - ,uint8_t aacSectionDataResilienceFlag -#endif - ) +static void section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld) { uint8_t g; uint8_t sect_esc_val, sect_bits; @@ -749,20 +1029,28 @@ static void section_data(ic_stream *ics, bitfile *ld sect_bits = 5; sect_esc_val = (1<<sect_bits) - 1; +#if 0 + printf("\ntotal sfb %d\n", ics->max_sfb); + printf(" sect top cb\n"); +#endif + for (g = 0; g < ics->num_window_groups; g++) { - uint16_t k = 0; + uint8_t k = 0; uint8_t i = 0; while (k < ics->max_sfb) { +#ifdef ERROR_RESILIENCE + uint8_t vcb11 = 0; +#endif uint8_t sfb; uint8_t sect_len_incr; uint16_t sect_len = 0; uint8_t sect_cb_bits = 4; #ifdef ERROR_RESILIENCE - if (aacSectionDataResilienceFlag) + if (hDecoder->aacSectionDataResilienceFlag) sect_cb_bits = 5; #endif @@ -773,21 +1061,31 @@ static void section_data(ic_stream *ics, bitfile *ld ics->noise_used = 1; #ifdef ERROR_RESILIENCE - if (!aacSectionDataResilienceFlag || - (ics->sect_cb[g][i] < 11) || - (ics->sect_cb[g][i] > 11 && ics->sect_cb[g][i] < 16)) + if (hDecoder->aacSectionDataResilienceFlag) { -#endif - while ((sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits - DEBUGVAR(1,72,"section_data(): sect_len_incr"))) == sect_esc_val) + if ((ics->sect_cb[g][i] == 11) || + ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32))) { - sect_len += sect_esc_val; + vcb11 = 1; } -#ifdef ERROR_RESILIENCE - } else { + } + if (vcb11) + { sect_len_incr = 1; + } else { +#endif + sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits + DEBUGVAR(1,72,"section_data(): sect_len_incr")); +#ifdef ERROR_RESILIENCE } #endif + while ((sect_len_incr == sect_esc_val) /* && + (k+sect_len < ics->max_sfb)*/) + { + sect_len += sect_len_incr; + sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits + DEBUGVAR(1,72,"section_data(): sect_len_incr")); + } sect_len += sect_len_incr; @@ -797,11 +1095,22 @@ static void section_data(ic_stream *ics, bitfile *ld for (sfb = k; sfb < k + sect_len; sfb++) ics->sfb_cb[g][sfb] = ics->sect_cb[g][i]; +#if 0 + printf(" %6d %6d %6d\n", + i, + ics->sect_end[g][i], + ics->sect_cb[g][i]); +#endif + k += sect_len; i++; } ics->num_sec[g] = i; } + +#if 0 + printf("\n"); +#endif } /* @@ -818,7 +1127,7 @@ static void section_data(ic_stream *ics, bitfile *ld static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld) { uint8_t g, sfb; - int8_t t; + int16_t t; int8_t noise_pcm_flag = 1; int16_t scale_factor = ics->global_gain; @@ -851,7 +1160,7 @@ static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld) if (noise_pcm_flag) { noise_pcm_flag = 0; - t = faad_getbits(ld, 9 + t = (int16_t)faad_getbits(ld, 9 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256; } else { t = huffman_scale_factor(ld); @@ -885,14 +1194,10 @@ static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld) } /* Table 4.4.26 */ -static uint8_t scale_factor_data(ic_stream *ics, bitfile *ld -#ifdef ERROR_RESILIENCE - ,uint8_t aacScalefactorDataResilienceFlag -#endif - ) +static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld) { #ifdef ERROR_RESILIENCE - if (!aacScalefactorDataResilienceFlag) + if (!hDecoder->aacScalefactorDataResilienceFlag) { #endif return decode_scale_factors(ics, ld); @@ -968,13 +1273,12 @@ static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld) The limit MAX_LTP_SFB is not defined in 14496-3, this is a bug in the document and will be corrected in one of the corrigenda. */ -static void ltp_data(ic_stream *ics, ltp_info *ltp, bitfile *ld, - uint8_t object_type) +static void ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld) { uint8_t sfb, w; #ifdef LD_DEC - if (object_type == LD) + if (hDecoder->object_type == LD) { ltp->lag_update = (uint8_t)faad_getbits(ld, 1 DEBUGVAR(1,142,"ltp_data(): lag_update")); @@ -1022,33 +1326,21 @@ static void ltp_data(ic_stream *ics, ltp_info *ltp, bitfile *ld, } #endif -/* defines whether a huffman codebook is unsigned or not */ -/* Table 4.6.2 */ -static uint8_t unsigned_cb[] = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, - /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 -}; - /* Table 4.4.29 */ -static uint8_t spectral_data(ic_stream *ics, bitfile *ld, int16_t *spectral_data, - uint16_t frame_len) +static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, + int16_t *spectral_data) { int8_t i; - uint8_t g, inc; + uint8_t g; int16_t *sp; uint16_t k, p = 0; uint8_t groups = 0; uint8_t sect_cb; uint8_t result; - uint16_t nshort = frame_len/8; + uint16_t nshort = hDecoder->frameLength/8; sp = spectral_data; - for (i = frame_len/16-1; i >= 0; --i) - { - *sp++ = 0; *sp++ = 0; *sp++ = 0; *sp++ = 0; - *sp++ = 0; *sp++ = 0; *sp++ = 0; *sp++ = 0; - *sp++ = 0; *sp++ = 0; *sp++ = 0; *sp++ = 0; - *sp++ = 0; *sp++ = 0; *sp++ = 0; *sp++ = 0; - } + memset(sp, 0, hDecoder->frameLength*sizeof(int16_t)); for(g = 0; g < ics->num_window_groups; g++) { @@ -1058,33 +1350,34 @@ static uint8_t spectral_data(ic_stream *ics, bitfile *ld, int16_t *spectral_data { sect_cb = ics->sect_cb[g][i]; - if ((sect_cb == ZERO_HCB) || - (sect_cb == NOISE_HCB) || - (sect_cb == INTENSITY_HCB) || - (sect_cb == INTENSITY_HCB2)) + switch (sect_cb) { + 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]]); - } else { + break; + default: for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; - k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; ) + k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += 4) { sp = spectral_data + p; - inc = (sect_cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN; - - 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; - p += inc; - if ((sect_cb == ESC_HCB) || (sect_cb >= 16)) + if (sect_cb < FIRST_PAIR_HCB) { - sp[0] = huffman_getescape(ld, sp[0]); - sp[1] = huffman_getescape(ld, sp[1]); + if ((result = huffman_spectral_data(sect_cb, ld, sp)) > 0) + return result; + } else { + if ((result = huffman_spectral_data(sect_cb, ld, sp)) > 0) + return result; + if ((result = huffman_spectral_data(sect_cb, ld, sp+2)) > 0) + return result; } + p += 4; } + break; } } groups += ics->window_group_length[g]; @@ -1097,7 +1390,7 @@ static uint8_t spectral_data(ic_stream *ics, bitfile *ld, int16_t *spectral_data static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count) { uint16_t i, n; - uint8_t extension_type = faad_getbits(ld, 4 + uint8_t extension_type = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,87,"extension_payload(): extension_type")); switch (extension_type) @@ -1155,7 +1448,7 @@ static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc) if (faad_get1bit(ld DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1) { - band_incr = faad_getbits(ld, 4 + band_incr = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,95,"dynamic_range_info(): band_incr")); /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4 DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits")); |