syntax.c

00001 /*
00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
00004 **  
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 ** 
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 ** 
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software 
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 **
00019 ** Any non-GPL usage of this software or parts of this software is strictly
00020 ** forbidden.
00021 **
00022 ** Software using this code must display the following message visibly in the
00023 ** software:
00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
00025 ** in, for example, the about-box or help/startup screen.
00026 **
00027 ** Commercial non-GPL licensing of this software is possible.
00028 ** For more info contact Ahead Software through [email protected].
00029 **
00030 ** $Id: syntax.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 /*
00034    Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
00035 */
00036 
00037 #include "common.h"
00038 #include "structs.h"
00039 
00040 #include <stdlib.h>
00041 #include <string.h>
00042 
00043 #include "decoder.h"
00044 #include "syntax.h"
00045 #include "specrec.h"
00046 #include "huffman.h"
00047 #include "bits.h"
00048 #include "pulse.h"
00049 #include "analysis.h"
00050 #include "drc.h"
00051 #ifdef ERROR_RESILIENCE
00052 #include "rvlc.h"
00053 #endif
00054 #ifdef SBR_DEC
00055 #include "sbr_syntax.h"
00056 #endif
00057 
00058 
00059 /* static function declarations */
00060 static void decode_sce_lfe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
00061                            uint8_t id_syn_ele);
00062 static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
00063                        uint8_t id_syn_ele);
00064 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
00065                                           uint8_t channel, uint8_t *tag);
00066 static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
00067                                     uint8_t channel, uint8_t *tag);
00068 #ifdef COUPLING_DEC
00069 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld);
00070 #endif
00071 static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld);
00072 static uint8_t program_config_element(program_config *pce, bitfile *ld);
00073 static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
00074 #ifdef SBR_DEC
00075                             ,uint8_t sbr_ele
00076 #endif
00077                             );
00078 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
00079                                          bitfile *ld, ic_stream *ics, uint8_t scal_flag,
00080                                          int16_t *spec_data);
00081 static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
00082                         uint8_t common_window);
00083 static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
00084 static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
00085 #ifdef SSR_DEC
00086 static void gain_control_data(bitfile *ld, ic_stream *ics);
00087 #endif
00088 static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
00089                              int16_t *spectral_data);
00090 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
00091 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
00092 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
00093 #ifdef LTP_DEC
00094 static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
00095 #endif
00096 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
00097 static void adts_variable_header(adts_header *adts, bitfile *ld);
00098 static void adts_error_check(adts_header *adts, bitfile *ld);
00099 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
00100 static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
00101 static uint8_t side_info(NeAACDecHandle hDecoder, element *ele,
00102                          bitfile *ld, ic_stream *ics, uint8_t scal_flag);
00103 #ifdef DRM
00104 static int8_t DRM_aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
00105                                            bitfile *ld, uint8_t this_layer_stereo);
00106 #endif
00107 
00108 
00109 /* Table 4.4.1 */
00110 int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
00111                         program_config *pce_out)
00112 {
00113     program_config pce;
00114 
00115     /* 1024 or 960 */
00116     mp4ASC->frameLengthFlag = faad_get1bit(ld
00117         DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
00118 #ifndef ALLOW_SMALL_FRAMELENGTH
00119     if (mp4ASC->frameLengthFlag == 1)
00120         return -3;
00121 #endif
00122 
00123     mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
00124         DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
00125     if (mp4ASC->dependsOnCoreCoder == 1)
00126     {
00127         mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
00128             DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
00129     }
00130 
00131     mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
00132     if (mp4ASC->channelsConfiguration == 0)
00133     {
00134         if (program_config_element(&pce, ld))
00135             return -3;
00136         //mp4ASC->channelsConfiguration = pce.channels;
00137 
00138         if (pce_out != NULL)
00139             memcpy(pce_out, &pce, sizeof(program_config));
00140 
00141         /*
00142         if (pce.num_valid_cc_elements)
00143             return -3;
00144         */
00145     }
00146 
00147 #ifdef ERROR_RESILIENCE
00148     if (mp4ASC->extensionFlag == 1)
00149     {
00150         /* Error resilience not supported yet */
00151         if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
00152         {
00153             mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
00154                 DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
00155             mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
00156                 DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
00157             mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
00158                 DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
00159 
00160             /* 1 bit: extensionFlag3 */
00161         }
00162     }
00163 #endif
00164 
00165     return 0;
00166 }
00167 
00168 /* Table 4.4.2 */
00169 /* An MPEG-4 Audio decoder is only required to follow the Program
00170    Configuration Element in GASpecificConfig(). The decoder shall ignore
00171    any Program Configuration Elements that may occur in raw data blocks.
00172    PCEs transmitted in raw data blocks cannot be used to convey decoder
00173    configuration information.
00174 */
00175 static uint8_t program_config_element(program_config *pce, bitfile *ld)
00176 {
00177     uint8_t i;
00178 
00179     memset(pce, 0, sizeof(program_config));
00180 
00181     pce->channels = 0;
00182 
00183     pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
00184         DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
00185 
00186     pce->object_type = (uint8_t)faad_getbits(ld, 2
00187         DEBUGVAR(1,11,"program_config_element(): object_type"));
00188     pce->sf_index = (uint8_t)faad_getbits(ld, 4
00189         DEBUGVAR(1,12,"program_config_element(): sf_index"));
00190     pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
00191         DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
00192     pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
00193         DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
00194     pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
00195         DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
00196     pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
00197         DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
00198     pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
00199         DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
00200     pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
00201         DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
00202 
00203     pce->mono_mixdown_present = faad_get1bit(ld
00204         DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
00205     if (pce->mono_mixdown_present == 1)
00206     {
00207         pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
00208             DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
00209     }
00210 
00211     pce->stereo_mixdown_present = faad_get1bit(ld
00212         DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
00213     if (pce->stereo_mixdown_present == 1)
00214     {
00215         pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
00216             DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
00217     }
00218 
00219     pce->matrix_mixdown_idx_present = faad_get1bit(ld
00220         DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
00221     if (pce->matrix_mixdown_idx_present == 1)
00222     {
00223         pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
00224             DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
00225         pce->pseudo_surround_enable = faad_get1bit(ld
00226             DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
00227     }
00228 
00229     for (i = 0; i < pce->num_front_channel_elements; i++)
00230     {
00231         pce->front_element_is_cpe[i] = faad_get1bit(ld
00232             DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
00233         pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00234             DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
00235 
00236         if (pce->front_element_is_cpe[i] & 1)
00237         {
00238             pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
00239             pce->num_front_channels += 2;
00240             pce->channels += 2;
00241         } else {
00242             pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
00243             pce->num_front_channels++;
00244             pce->channels++;
00245         }
00246     }
00247 
00248     for (i = 0; i < pce->num_side_channel_elements; i++)
00249     {
00250         pce->side_element_is_cpe[i] = faad_get1bit(ld
00251             DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
00252         pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00253             DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
00254 
00255         if (pce->side_element_is_cpe[i] & 1)
00256         {
00257             pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
00258             pce->num_side_channels += 2;
00259             pce->channels += 2;
00260         } else {
00261             pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
00262             pce->num_side_channels++;
00263             pce->channels++;
00264         }
00265     }
00266 
00267     for (i = 0; i < pce->num_back_channel_elements; i++)
00268     {
00269         pce->back_element_is_cpe[i] = faad_get1bit(ld
00270             DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
00271         pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00272             DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
00273 
00274         if (pce->back_element_is_cpe[i] & 1)
00275         {
00276             pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
00277             pce->channels += 2;
00278             pce->num_back_channels += 2;
00279         } else {
00280             pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
00281             pce->num_back_channels++;
00282             pce->channels++;
00283         }
00284     }
00285 
00286     for (i = 0; i < pce->num_lfe_channel_elements; i++)
00287     {
00288         pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00289             DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
00290 
00291         pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
00292         pce->num_lfe_channels++;
00293         pce->channels++;
00294     }
00295 
00296     for (i = 0; i < pce->num_assoc_data_elements; i++)
00297         pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00298         DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
00299 
00300     for (i = 0; i < pce->num_valid_cc_elements; i++)
00301     {
00302         pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
00303             DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
00304         pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
00305             DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
00306     }
00307 
00308     faad_byte_align(ld);
00309 
00310     pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
00311         DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
00312 
00313     for (i = 0; i < pce->comment_field_bytes; i++)
00314     {
00315         pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
00316             DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
00317     }
00318     pce->comment_field_data[i] = 0;
00319 
00320     if (pce->channels > MAX_CHANNELS)
00321         return 22;
00322 
00323     return 0;
00324 }
00325 
00326 static void decode_sce_lfe(NeAACDecHandle hDecoder,
00327                            NeAACDecFrameInfo *hInfo, bitfile *ld,
00328                            uint8_t id_syn_ele)
00329 {
00330     uint8_t channels = hDecoder->fr_channels;
00331     uint8_t tag = 0;
00332 
00333     if (channels+1 > MAX_CHANNELS)
00334     {
00335         hInfo->error = 12;
00336         return;
00337     }
00338     if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
00339     {
00340         hInfo->error = 13;
00341         return;
00342     }
00343 
00344     /* for SCE hDecoder->element_output_channels[] is not set here because this
00345        can become 2 when some form of Parametric Stereo coding is used
00346     */
00347 
00348     /* save the syntax element id */
00349     hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
00350 
00351     /* decode the element */
00352     hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
00353 
00354     /* map output channels position to internal data channels */
00355     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
00356     {
00357         /* this might be faulty when pce_set is true */
00358         hDecoder->internal_channel[channels] = channels;
00359         hDecoder->internal_channel[channels+1] = channels+1;
00360     } else {
00361         if (hDecoder->pce_set)
00362             hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
00363         else
00364             hDecoder->internal_channel[channels] = channels;
00365     }
00366 
00367     hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
00368     hDecoder->fr_ch_ele++;
00369 }
00370 
00371 static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
00372                        uint8_t id_syn_ele)
00373 {
00374     uint8_t channels = hDecoder->fr_channels;
00375     uint8_t tag = 0;
00376 
00377     if (channels+2 > MAX_CHANNELS)
00378     {
00379         hInfo->error = 12;
00380         return;
00381     }
00382     if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
00383     {
00384         hInfo->error = 13;
00385         return;
00386     }
00387 
00388     /* for CPE the number of output channels is always 2 */
00389     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
00390     {
00391         /* element_output_channels not set yet */
00392         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
00393     } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
00394         /* element inconsistency */
00395         hInfo->error = 21;
00396         return;
00397     }
00398 
00399     /* save the syntax element id */
00400     hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
00401 
00402     /* decode the element */
00403     hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
00404 
00405     /* map output channel position to internal data channels */
00406     if (hDecoder->pce_set)
00407     {
00408         hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
00409         hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
00410     } else {
00411         hDecoder->internal_channel[channels] = channels;
00412         hDecoder->internal_channel[channels+1] = channels+1;
00413     }
00414 
00415     hDecoder->fr_channels += 2;
00416     hDecoder->fr_ch_ele++;
00417 }
00418 
00419 void raw_data_block(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
00420                     bitfile *ld, program_config *pce, drc_info *drc)
00421 {
00422     uint8_t id_syn_ele;
00423     uint8_t ele_this_frame = 0;
00424 
00425     hDecoder->fr_channels = 0;
00426     hDecoder->fr_ch_ele = 0;
00427     hDecoder->first_syn_ele = 25;
00428     hDecoder->has_lfe = 0;
00429 
00430 #ifdef ERROR_RESILIENCE
00431     if (hDecoder->object_type < ER_OBJECT_START)
00432     {
00433 #endif
00434         /* Table 4.4.3: raw_data_block() */
00435         while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
00436             DEBUGVAR(1,4,"NeAACDecDecode(): id_syn_ele"))) != ID_END)
00437         {
00438             switch (id_syn_ele) {
00439             case ID_SCE:
00440                 ele_this_frame++;
00441                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
00442                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
00443                 if (hInfo->error > 0)
00444                     return;
00445                 break;
00446             case ID_CPE:
00447                 ele_this_frame++;
00448                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
00449                 decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
00450                 if (hInfo->error > 0)
00451                     return;
00452                 break;
00453             case ID_LFE:
00454 #ifdef DRM
00455                 hInfo->error = 32;
00456 #else
00457                 ele_this_frame++;
00458                 hDecoder->has_lfe++;
00459                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
00460 #endif
00461                 if (hInfo->error > 0)
00462                     return;
00463                 break;
00464             case ID_CCE: /* not implemented yet, but skip the bits */
00465 #ifdef DRM
00466                 hInfo->error = 32;
00467 #else
00468                 ele_this_frame++;
00469 #ifdef COUPLING_DEC
00470                 hInfo->error = coupling_channel_element(hDecoder, ld);
00471 #else
00472                 hInfo->error = 6;
00473 #endif
00474 #endif
00475                 if (hInfo->error > 0)
00476                     return;
00477                 break;
00478             case ID_DSE:
00479                 ele_this_frame++;
00480                 data_stream_element(hDecoder, ld);
00481                 break;
00482             case ID_PCE:
00483                 if (ele_this_frame != 0)
00484                 {
00485                     hInfo->error = 31;
00486                     return;
00487                 }
00488                 ele_this_frame++;
00489                 /* 14496-4: 5.6.4.1.2.1.3: */
00490                 /* program_configuration_element()'s in access units shall be ignored */
00491                 program_config_element(pce, ld);
00492                 //if ((hInfo->error = program_config_element(pce, ld)) > 0)
00493                 //    return;
00494                 //hDecoder->pce_set = 1;
00495                 break;
00496             case ID_FIL:
00497                 ele_this_frame++;
00498                 /* one sbr_info describes a channel_element not a channel! */
00499                 /* if we encounter SBR data here: error */
00500                 /* SBR data will be read directly in the SCE/LFE/CPE element */
00501                 if ((hInfo->error = fill_element(hDecoder, ld, drc
00502 #ifdef SBR_DEC
00503                     , INVALID_SBR_ELEMENT
00504 #endif
00505                     )) > 0)
00506                     return;
00507                 break;
00508             }
00509         }
00510 #ifdef ERROR_RESILIENCE
00511     } else {
00512         /* Table 262: er_raw_data_block() */
00513         switch (hDecoder->channelConfiguration)
00514         {
00515         case 1:
00516             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00517             if (hInfo->error > 0)
00518                 return;
00519             break;
00520         case 2:
00521             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00522             if (hInfo->error > 0)
00523                 return;
00524             break;
00525         case 3:
00526             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00527             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00528             if (hInfo->error > 0)
00529                 return;
00530             break;
00531         case 4:
00532             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00533             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00534             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00535             if (hInfo->error > 0)
00536                 return;
00537             break;
00538         case 5:
00539             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00540             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00541             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00542             if (hInfo->error > 0)
00543                 return;
00544             break;
00545         case 6:
00546             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00547             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00548             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00549             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
00550             if (hInfo->error > 0)
00551                 return;
00552             break;
00553         case 7: /* 8 channels */
00554             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
00555             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00556             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00557             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
00558             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
00559             if (hInfo->error > 0)
00560                 return;
00561             break;
00562         default:
00563             hInfo->error = 7;
00564             return;
00565         }
00566 #if 0
00567         cnt = bits_to_decode() / 8;
00568         while (cnt >= 1)
00569         {
00570             cnt -= extension_payload(cnt);
00571         }
00572 #endif
00573     }
00574 #endif
00575 
00576     /* new in corrigendum 14496-3:2002 */
00577 #ifdef DRM
00578     if (hDecoder->object_type != DRM_ER_LC)
00579 #endif
00580     {
00581         faad_byte_align(ld);
00582     }
00583 
00584     return;
00585 }
00586 
00587 /* Table 4.4.4 and */
00588 /* Table 4.4.9 */
00589 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
00590                                           uint8_t channel, uint8_t *tag)
00591 {
00592     uint8_t retval = 0;
00593     element sce = {0};
00594     ic_stream *ics = &(sce.ics1);
00595     ALIGN int16_t spec_data[1024] = {0};
00596 
00597     sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
00598         DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
00599 
00600     *tag = sce.element_instance_tag;
00601     sce.channel = channel;
00602     sce.paired_channel = -1;
00603 
00604     retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
00605     if (retval > 0)
00606         return retval;
00607 
00608     /* IS not allowed in single channel */
00609     if (ics->is_used)
00610         return 32;
00611 
00612 #ifdef SBR_DEC
00613     /* check if next bitstream element is a fill element */
00614     /* if so, read it now so SBR decoding can be done in case of a file with SBR */
00615     if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
00616     {
00617         faad_flushbits(ld, LEN_SE_ID);
00618 
00619         /* one sbr_info describes a channel_element not a channel! */
00620         if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
00621         {
00622             return retval;
00623         }
00624     }
00625 #endif
00626 
00627     /* noiseless coding is done, spectral reconstruction is done now */
00628     retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
00629     if (retval > 0)
00630         return retval;
00631 
00632     return 0;
00633 }
00634 
00635 /* Table 4.4.5 */
00636 static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
00637                                     uint8_t channels, uint8_t *tag)
00638 {
00639     ALIGN int16_t spec_data1[1024] = {0};
00640     ALIGN int16_t spec_data2[1024] = {0};
00641     element cpe = {0};
00642     ic_stream *ics1 = &(cpe.ics1);
00643     ic_stream *ics2 = &(cpe.ics2);
00644     uint8_t result;
00645 
00646     cpe.channel        = channels;
00647     cpe.paired_channel = channels+1;
00648 
00649     cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
00650         DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
00651     *tag = cpe.element_instance_tag;
00652 
00653     if ((cpe.common_window = faad_get1bit(ld
00654         DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
00655     {
00656         /* both channels have common ics information */
00657         if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
00658             return result;
00659 
00660         ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
00661             DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
00662         if (ics1->ms_mask_present == 3)
00663         {
00664             /* bitstream error */
00665             return 32;
00666         }
00667         if (ics1->ms_mask_present == 1)
00668         {
00669             uint8_t g, sfb;
00670             for (g = 0; g < ics1->num_window_groups; g++)
00671             {
00672                 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
00673                 {
00674                     ics1->ms_used[g][sfb] = faad_get1bit(ld
00675                         DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
00676                 }
00677             }
00678         }
00679 
00680 #ifdef ERROR_RESILIENCE
00681         if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
00682         {
00683             if ((
00684 #ifdef LTP_DEC
00685                 ics1->ltp.data_present =
00686 #endif
00687                 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
00688             {
00689 #ifdef LTP_DEC
00690                 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
00691                 {
00692                     return result;
00693                 }
00694 #else
00695                 return 26;
00696 #endif
00697             }
00698         }
00699 #endif
00700 
00701         memcpy(ics2, ics1, sizeof(ic_stream));
00702     } else {
00703         ics1->ms_mask_present = 0;
00704     }
00705 
00706     if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
00707         0, spec_data1)) > 0)
00708     {
00709         return result;
00710     }
00711 
00712 #ifdef ERROR_RESILIENCE
00713     if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
00714         (ics1->predictor_data_present))
00715     {
00716         if ((
00717 #ifdef LTP_DEC
00718             ics1->ltp2.data_present =
00719 #endif
00720             faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
00721         {
00722 #ifdef LTP_DEC
00723             if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
00724             {
00725                 return result;
00726             }
00727 #else
00728             return 26;
00729 #endif
00730         }
00731     }
00732 #endif
00733 
00734     if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
00735         0, spec_data2)) > 0)
00736     {
00737         return result;
00738     }
00739 
00740 #ifdef SBR_DEC
00741     /* check if next bitstream element is a fill element */
00742     /* if so, read it now so SBR decoding can be done in case of a file with SBR */
00743     if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
00744     {
00745         faad_flushbits(ld, LEN_SE_ID);
00746 
00747         /* one sbr_info describes a channel_element not a channel! */
00748         if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
00749         {
00750             return result;
00751         }
00752     }
00753 #endif
00754 
00755     /* noiseless coding is done, spectral reconstruction is done now */
00756     if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
00757         spec_data1, spec_data2)) > 0)
00758     {
00759         return result;
00760     }
00761 
00762     return 0;
00763 }
00764 
00765 /* Table 4.4.6 */
00766 static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
00767                         uint8_t common_window)
00768 {
00769     uint8_t retval = 0;
00770     uint8_t ics_reserved_bit;
00771 
00772     ics_reserved_bit = faad_get1bit(ld
00773         DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
00774     if (ics_reserved_bit != 0)
00775         return 32;
00776     ics->window_sequence = (uint8_t)faad_getbits(ld, 2
00777         DEBUGVAR(1,44,"ics_info(): window_sequence"));
00778     ics->window_shape = faad_get1bit(ld
00779         DEBUGVAR(1,45,"ics_info(): window_shape"));
00780 
00781 #ifdef LD_DEC
00782     /* No block switching in LD */
00783     if ((hDecoder->object_type == LD) && (ics->window_sequence != ONLY_LONG_SEQUENCE))
00784         return 32;
00785 #endif
00786 
00787     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
00788     {
00789         ics->max_sfb = (uint8_t)faad_getbits(ld, 4
00790             DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
00791         ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
00792             DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
00793     } else {
00794         ics->max_sfb = (uint8_t)faad_getbits(ld, 6
00795             DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
00796     }
00797 
00798     /* get the grouping information */
00799     if ((retval = window_grouping_info(hDecoder, ics)) > 0)
00800         return retval;
00801 
00802 
00803     /* should be an error */
00804     /* check the range of max_sfb */
00805     if (ics->max_sfb > ics->num_swb)
00806         return 16;
00807 
00808     if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
00809     {
00810         if ((ics->predictor_data_present = faad_get1bit(ld
00811             DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
00812         {
00813             if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
00814             {
00815                 uint8_t sfb;
00816 
00817                 uint8_t limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
00818 #ifdef MAIN_DEC
00819                 ics->pred.limit = limit;
00820 #endif
00821 
00822                 if ((
00823 #ifdef MAIN_DEC
00824                     ics->pred.predictor_reset =
00825 #endif
00826                     faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
00827                 {
00828 #ifdef MAIN_DEC
00829                     ics->pred.predictor_reset_group_number =
00830 #endif
00831                         (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
00832                 }
00833 
00834                 for (sfb = 0; sfb < limit; sfb++)
00835                 {
00836 #ifdef MAIN_DEC
00837                     ics->pred.prediction_used[sfb] =
00838 #endif
00839                         faad_get1bit(ld DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
00840                 }
00841             }
00842 #ifdef LTP_DEC
00843             else { /* Long Term Prediction */
00844                 if (hDecoder->object_type < ER_OBJECT_START)
00845                 {
00846                     if ((ics->ltp.data_present = faad_get1bit(ld
00847                         DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
00848                     {
00849                         if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
00850                         {
00851                             return retval;
00852                         }
00853                     }
00854                     if (common_window)
00855                     {
00856                         if ((ics->ltp2.data_present = faad_get1bit(ld
00857                             DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
00858                         {
00859                             if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
00860                             {
00861                                 return retval;
00862                             }
00863                         }
00864                     }
00865                 }
00866 #ifdef ERROR_RESILIENCE
00867                 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
00868                 {
00869                     if ((ics->ltp.data_present = faad_get1bit(ld
00870                         DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
00871                     {
00872                         ltp_data(hDecoder, ics, &(ics->ltp), ld);
00873                     }
00874                 }
00875 #endif
00876             }
00877 #endif
00878         }
00879     }
00880 
00881     return retval;
00882 }
00883 
00884 /* Table 4.4.7 */
00885 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
00886 {
00887     uint8_t i;
00888 
00889     pul->number_pulse = (uint8_t)faad_getbits(ld, 2
00890         DEBUGVAR(1,56,"pulse_data(): number_pulse"));
00891     pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
00892         DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
00893 
00894     /* check the range of pulse_start_sfb */
00895     if (pul->pulse_start_sfb > ics->num_swb)
00896         return 16;
00897 
00898     for (i = 0; i < pul->number_pulse+1; i++)
00899     {
00900         pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
00901             DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
00902 #if 0
00903         printf("%d\n", pul->pulse_offset[i]);
00904 #endif
00905         pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
00906             DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
00907 #if 0
00908         printf("%d\n", pul->pulse_amp[i]);
00909 #endif
00910     }
00911 
00912     return 0;
00913 }
00914 
00915 #ifdef COUPLING_DEC
00916 /* Table 4.4.8: Currently just for skipping the bits... */
00917 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld)
00918 {
00919     uint8_t c, result = 0;
00920     uint8_t ind_sw_cce_flag = 0;
00921     uint8_t num_gain_element_lists = 0;
00922     uint8_t num_coupled_elements = 0;
00923 
00924     element el_empty = {0};
00925     ic_stream ics_empty = {0};
00926     int16_t sh_data[1024];
00927 
00928     c = faad_getbits(ld, LEN_TAG
00929         DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
00930 
00931     ind_sw_cce_flag = faad_get1bit(ld
00932         DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
00933     num_coupled_elements = faad_getbits(ld, 3
00934         DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
00935 
00936     for (c = 0; c < num_coupled_elements + 1; c++)
00937     {
00938         uint8_t cc_target_is_cpe, cc_target_tag_select;
00939 
00940         num_gain_element_lists++;
00941 
00942         cc_target_is_cpe = faad_get1bit(ld
00943             DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
00944         cc_target_tag_select = faad_getbits(ld, 4
00945             DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
00946 
00947         if (cc_target_is_cpe)
00948         {
00949             uint8_t cc_l = faad_get1bit(ld
00950                 DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
00951             uint8_t cc_r = faad_get1bit(ld
00952                 DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
00953 
00954             if (cc_l && cc_r)
00955                 num_gain_element_lists++;
00956         }
00957     }
00958 
00959     faad_get1bit(ld
00960         DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
00961     faad_get1bit(ld
00962         DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
00963     faad_getbits(ld, 2
00964         DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
00965 
00966     if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
00967         0, sh_data)) > 0)
00968     {
00969         return result;
00970     }
00971 
00972     /* IS not allowed in single channel */
00973     if (ics->is_used)
00974         return 32;
00975 
00976     for (c = 1; c < num_gain_element_lists; c++)
00977     {
00978         uint8_t cge;
00979 
00980         if (ind_sw_cce_flag)
00981         {
00982             cge = 1;
00983         } else {
00984             cge = faad_get1bit(ld
00985                 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
00986         }
00987 
00988         if (cge)
00989         {
00990             huffman_scale_factor(ld);
00991         } else {
00992             uint8_t g, sfb;
00993 
00994             for (g = 0; g < ics_empty.num_window_groups; g++)
00995             {
00996                 for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
00997                 {
00998                     if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
00999                         huffman_scale_factor(ld);
01000                 }
01001             }
01002         }
01003     }
01004 
01005     return 0;
01006 }
01007 #endif
01008 
01009 /* Table 4.4.10 */
01010 static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld)
01011 {
01012     uint8_t byte_aligned;
01013     uint16_t i, count;
01014 
01015     /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
01016         DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
01017     byte_aligned = faad_get1bit(ld
01018         DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
01019     count = (uint16_t)faad_getbits(ld, 8
01020         DEBUGVAR(1,62,"data_stream_element(): count"));
01021     if (count == 255)
01022     {
01023         count += (uint16_t)faad_getbits(ld, 8
01024             DEBUGVAR(1,63,"data_stream_element(): extra count"));
01025     }
01026     if (byte_aligned)
01027         faad_byte_align(ld);
01028 
01029     for (i = 0; i < count; i++)
01030     {
01031         faad_getbits(ld, LEN_BYTE
01032             DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
01033     }
01034 
01035     return count;
01036 }
01037 
01038 /* Table 4.4.11 */
01039 static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
01040 #ifdef SBR_DEC
01041                             ,uint8_t sbr_ele
01042 #endif
01043                             )
01044 {
01045     uint16_t count;
01046 #ifdef SBR_DEC
01047     uint8_t bs_extension_type;
01048 #endif
01049 
01050     count = (uint16_t)faad_getbits(ld, 4
01051         DEBUGVAR(1,65,"fill_element(): count"));
01052     if (count == 15)
01053     {
01054         count += (uint16_t)faad_getbits(ld, 8
01055             DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
01056     }
01057 
01058     if (count > 0)
01059     {
01060 #ifdef SBR_DEC
01061         bs_extension_type = (uint8_t)faad_showbits(ld, 4);
01062 
01063         if ((bs_extension_type == EXT_SBR_DATA) ||
01064             (bs_extension_type == EXT_SBR_DATA_CRC))
01065         {
01066             if (sbr_ele == INVALID_SBR_ELEMENT)
01067                 return 24;
01068 
01069             if (!hDecoder->sbr[sbr_ele])
01070             {
01071                 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
01072                     hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index),
01073                     hDecoder->downSampledSBR
01074 #ifdef DRM
01075                     , 0
01076 #endif
01077                     );
01078             }
01079 
01080             hDecoder->sbr_present_flag = 1;
01081 
01082             /* parse the SBR data */
01083             hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
01084 
01085 #if 0
01086             if (hDecoder->sbr[sbr_ele]->ret > 0)
01087             {
01088                 printf("%s\n", NeAACDecGetErrorMessage(hDecoder->sbr[sbr_ele]->ret));
01089             }
01090 #endif
01091 
01092 #if (defined(PS_DEC) || defined(DRM_PS))
01093             if (hDecoder->sbr[sbr_ele]->ps_used)
01094             {
01095                 hDecoder->ps_used[sbr_ele] = 1;
01096 
01097                 /* set element independent flag to 1 as well */
01098                 hDecoder->ps_used_global = 1;
01099             }
01100 #endif
01101         } else {
01102 #endif
01103 #ifndef DRM
01104             while (count > 0)
01105             {
01106                 count -= extension_payload(ld, drc, count);
01107             }
01108 #else
01109             return 30;
01110 #endif
01111 #ifdef SBR_DEC
01112         }
01113 #endif
01114     }
01115 
01116     return 0;
01117 }
01118 
01119 /* Table 4.4.12 */
01120 #ifdef SSR_DEC
01121 static void gain_control_data(bitfile *ld, ic_stream *ics)
01122 {
01123     uint8_t bd, wd, ad;
01124     ssr_info *ssr = &(ics->ssr);
01125 
01126     ssr->max_band = (uint8_t)faad_getbits(ld, 2
01127         DEBUGVAR(1,1000,"gain_control_data(): max_band"));
01128 
01129     if (ics->window_sequence == ONLY_LONG_SEQUENCE)
01130     {
01131         for (bd = 1; bd <= ssr->max_band; bd++)
01132         {
01133             for (wd = 0; wd < 1; wd++)
01134             {
01135                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
01136                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
01137 
01138                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
01139                 {
01140                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01141                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
01142                     ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
01143                         DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01144                 }
01145             }
01146         }
01147     } else if (ics->window_sequence == LONG_START_SEQUENCE) {
01148         for (bd = 1; bd <= ssr->max_band; bd++)
01149         {
01150             for (wd = 0; wd < 2; wd++)
01151             {
01152                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
01153                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
01154 
01155                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
01156                 {
01157                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01158                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
01159                     if (wd == 0)
01160                     {
01161                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01162                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01163                     } else {
01164                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
01165                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01166                     }
01167                 }
01168             }
01169         }
01170     } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
01171         for (bd = 1; bd <= ssr->max_band; bd++)
01172         {
01173             for (wd = 0; wd < 8; wd++)
01174             {
01175                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
01176                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
01177 
01178                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
01179                 {
01180                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01181                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
01182                     ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
01183                         DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01184                 }
01185             }
01186         }
01187     } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
01188         for (bd = 1; bd <= ssr->max_band; bd++)
01189         {
01190             for (wd = 0; wd < 2; wd++)
01191             {
01192                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
01193                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
01194 
01195                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
01196                 {
01197                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01198                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
01199 
01200                     if (wd == 0)
01201                     {
01202                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
01203                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01204                     } else {
01205                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
01206                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
01207                     }
01208                 }
01209             }
01210         }
01211     }
01212 }
01213 #endif
01214 
01215 #ifdef DRM
01216 /* Table 4.4.13 ASME */
01217 void DRM_aac_scalable_main_element(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
01218                                    bitfile *ld, program_config *pce, drc_info *drc)
01219 {
01220     uint8_t retval = 0;
01221     uint8_t channels = hDecoder->fr_channels = 0;
01222     uint8_t ch;
01223     uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
01224     element cpe = {0};
01225     ic_stream *ics1 = &(cpe.ics1);
01226     ic_stream *ics2 = &(cpe.ics2);
01227     int16_t *spec_data;
01228     ALIGN int16_t spec_data1[1024] = {0};
01229     ALIGN int16_t spec_data2[1024] = {0};
01230 
01231     hDecoder->fr_ch_ele = 0;
01232 
01233     hInfo->error = DRM_aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
01234     if (hInfo->error > 0)
01235         return;
01236 
01237     cpe.common_window = 1;
01238     if (this_layer_stereo)
01239     {
01240         hDecoder->element_id[0] = ID_CPE;
01241         if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
01242             hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
01243     } else {
01244         hDecoder->element_id[0] = ID_SCE;
01245     }
01246 
01247     if (this_layer_stereo)
01248     {
01249         cpe.channel        = 0;
01250         cpe.paired_channel = 1;
01251     }
01252 
01253 
01254     /* Stereo2 / Mono1 */
01255     ics1->tns_data_present = faad_get1bit(ld);
01256 
01257 #if defined(LTP_DEC)
01258     ics1->ltp.data_present = faad_get1bit(ld);
01259 #elif defined (DRM)
01260     if(faad_get1bit(ld)) {
01261          hInfo->error = 26;
01262          return;
01263     }
01264 #else
01265     faad_get1bit(ld);
01266 #endif    
01267 
01268     hInfo->error = side_info(hDecoder, &cpe, ld, ics1, 1);
01269     if (hInfo->error > 0)
01270         return;
01271     if (this_layer_stereo)
01272     {
01273         /* Stereo3 */
01274         ics2->tns_data_present = faad_get1bit(ld);
01275 #ifdef LTP_DEC
01276         ics1->ltp.data_present =
01277 #endif
01278             faad_get1bit(ld);
01279         hInfo->error = side_info(hDecoder, &cpe, ld, ics2, 1);
01280         if (hInfo->error > 0)
01281             return;
01282     }
01283     /* Stereo4 / Mono2 */
01284     if (ics1->tns_data_present)
01285         tns_data(ics1, &(ics1->tns), ld);
01286     if (this_layer_stereo)
01287     {
01288         /* Stereo5 */
01289         if (ics2->tns_data_present)
01290             tns_data(ics2, &(ics2->tns), ld);
01291     }
01292 
01293 #ifdef DRM
01294     /* CRC check */
01295     if (hDecoder->object_type == DRM_ER_LC)
01296     {
01297         if ((hInfo->error = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
01298             return;
01299     }
01300 #endif
01301 
01302     /* Stereo6 / Mono3 */
01303     /* error resilient spectral data decoding */
01304     if ((hInfo->error = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0)
01305     {
01306         return;
01307     }
01308     if (this_layer_stereo)
01309     {
01310         /* Stereo7 */
01311         /* error resilient spectral data decoding */
01312         if ((hInfo->error = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0)
01313         {
01314             return;
01315         }
01316     }
01317 
01318 
01319 #ifdef DRM
01320 #ifdef SBR_DEC
01321     /* In case of DRM we need to read the SBR info before channel reconstruction */
01322     if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
01323     {
01324         bitfile ld_sbr = {0};
01325         uint32_t i;
01326         uint16_t count = 0;
01327         uint8_t *revbuffer;
01328         uint8_t *prevbufstart;
01329         uint8_t *pbufend;
01330 
01331         /* all forward bitreading should be finished at this point */
01332         uint32_t bitsconsumed = faad_get_processed_bits(ld);
01333         uint32_t buffer_size = faad_origbitbuffer_size(ld);
01334         uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
01335 
01336         if (bitsconsumed + 8 > buffer_size*8)
01337         {
01338             hInfo->error = 14;
01339             return;
01340         }
01341 
01342         if (!hDecoder->sbr[0])
01343         {
01344             hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0],
01345                 2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1);
01346         }
01347 
01348         /* Reverse bit reading of SBR data in DRM audio frame */
01349         revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
01350         prevbufstart = revbuffer;
01351         pbufend = &buffer[buffer_size - 1];
01352         for (i = 0; i < buffer_size; i++)
01353             *prevbufstart++ = tabFlipbits[*pbufend--];
01354 
01355         /* Set SBR data */
01356         /* consider 8 bits from AAC-CRC */
01357         /* SBR buffer size is original buffer size minus AAC buffer size */
01358         count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
01359         faad_initbits(&ld_sbr, revbuffer, count);
01360 
01361         hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
01362         hDecoder->sbr[0]->sample_rate *= 2;
01363 
01364         faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
01365 
01366         hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
01367 #if (defined(PS_DEC) || defined(DRM_PS))
01368         if (hDecoder->sbr[0]->ps_used)
01369         {
01370             hDecoder->ps_used[0] = 1;
01371             hDecoder->ps_used_global = 1;
01372         }
01373 #endif
01374 
01375         if (ld_sbr.error)
01376         {
01377             hDecoder->sbr[0]->ret = 1;
01378         }
01379 
01380         /* check CRC */
01381         /* no need to check it if there was already an error */
01382         if (hDecoder->sbr[0]->ret == 0)
01383             hDecoder->sbr[0]->ret = (uint8_t)faad_check_CRC(&ld_sbr, (uint16_t)faad_get_processed_bits(&ld_sbr) - 8);
01384 
01385         /* SBR data was corrupted, disable it until the next header */
01386         if (hDecoder->sbr[0]->ret != 0)
01387         {
01388             hDecoder->sbr[0]->header_count = 0;
01389         }
01390 
01391         faad_endbits(&ld_sbr);
01392 
01393         if (revbuffer)
01394             faad_free(revbuffer);
01395     }
01396 #endif
01397 #endif
01398 
01399     if (this_layer_stereo)
01400     {
01401         hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
01402         if (hInfo->error > 0)
01403             return;
01404     } else {
01405         hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
01406         if (hInfo->error > 0)
01407             return;
01408     }
01409 
01410     /* map output channels position to internal data channels */
01411     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
01412     {
01413         /* this might be faulty when pce_set is true */
01414         hDecoder->internal_channel[channels] = channels;
01415         hDecoder->internal_channel[channels+1] = channels+1;
01416     } else {
01417         hDecoder->internal_channel[channels] = channels;
01418     }
01419 
01420     hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
01421     hDecoder->fr_ch_ele++;
01422 
01423     return;
01424 }
01425 
01426 /* Table 4.4.15 */
01427 static int8_t DRM_aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
01428                                            bitfile *ld, uint8_t this_layer_stereo)
01429 {
01430     uint8_t retval = 0;
01431     uint8_t ch;
01432     ic_stream *ics;
01433     uint8_t ics_reserved_bit;
01434 
01435     ics_reserved_bit = faad_get1bit(ld
01436         DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
01437     if (ics_reserved_bit != 0)
01438         return 32;
01439     ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
01440         DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
01441     ics1->window_shape = faad_get1bit(ld
01442         DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
01443 
01444     if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
01445     {
01446         ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
01447             DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
01448         ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
01449             DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
01450     } else {
01451         ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
01452             DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
01453     }
01454 
01455     /* get the grouping information */
01456     if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
01457         return retval;
01458 
01459     /* should be an error */
01460     /* check the range of max_sfb */
01461     if (ics1->max_sfb > ics1->num_swb)
01462         return 16;
01463 
01464     if (this_layer_stereo)
01465     {
01466         ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
01467             DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
01468         if (ics1->ms_mask_present == 3)
01469         {
01470             /* bitstream error */
01471             return 32;
01472         }
01473         if (ics1->ms_mask_present == 1)
01474         {
01475             uint8_t g, sfb;
01476             for (g = 0; g < ics1->num_window_groups; g++)
01477             {
01478                 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
01479                 {
01480                     ics1->ms_used[g][sfb] = faad_get1bit(ld
01481                         DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
01482                 }
01483             }
01484         }
01485 
01486         memcpy(ics2, ics1, sizeof(ic_stream));
01487     } else {
01488         ics1->ms_mask_present = 0;
01489     }
01490 
01491     return 0;
01492 }
01493 #endif
01494 
01495 static uint8_t side_info(NeAACDecHandle hDecoder, element *ele,
01496                          bitfile *ld, ic_stream *ics, uint8_t scal_flag)
01497 {
01498     uint8_t result;
01499 
01500     ics->global_gain = (uint8_t)faad_getbits(ld, 8
01501         DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
01502 
01503     if (!ele->common_window && !scal_flag)
01504     {
01505         if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
01506             return result;
01507     }
01508 
01509     if ((result = section_data(hDecoder, ics, ld)) > 0)
01510         return result;
01511 
01512     if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
01513         return result;
01514 
01515     if (!scal_flag)
01516     {
01522         /* get pulse data */
01523         if ((ics->pulse_data_present = faad_get1bit(ld
01524             DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
01525         {
01526             if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
01527                 return result;
01528         }
01529 
01530         /* get tns data */
01531         if ((ics->tns_data_present = faad_get1bit(ld
01532             DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
01533         {
01534 #ifdef ERROR_RESILIENCE
01535             if (hDecoder->object_type < ER_OBJECT_START)
01536 #endif
01537                 tns_data(ics, &(ics->tns), ld);
01538         }
01539 
01540         /* get gain control data */
01541         if ((ics->gain_control_data_present = faad_get1bit(ld
01542             DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
01543         {
01544 #ifdef SSR_DEC
01545             if (hDecoder->object_type != SSR)
01546                 return 1;
01547             else
01548                 gain_control_data(ld, ics);
01549 #else
01550             return 1;
01551 #endif
01552         }
01553     }
01554 
01555 #ifdef ERROR_RESILIENCE
01556     if (hDecoder->aacSpectralDataResilienceFlag)
01557     {
01558         ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
01559             DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
01560 
01561         if (hDecoder->channelConfiguration == 2)
01562         {
01563             if (ics->length_of_reordered_spectral_data > 6144)
01564                 ics->length_of_reordered_spectral_data = 6144;
01565         } else {
01566             if (ics->length_of_reordered_spectral_data > 12288)
01567                 ics->length_of_reordered_spectral_data = 12288;
01568         }
01569 
01570         ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
01571             DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
01572         if (ics->length_of_longest_codeword >= 49)
01573             ics->length_of_longest_codeword = 49;
01574     }
01575 
01576     /* RVLC spectral data is put here */
01577     if (hDecoder->aacScalefactorDataResilienceFlag)
01578     {
01579         if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
01580             return result;
01581     }
01582 #endif
01583 
01584     return 0;
01585 }
01586 
01587 /* Table 4.4.24 */
01588 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
01589                                          bitfile *ld, ic_stream *ics, uint8_t scal_flag,
01590                                          int16_t *spec_data)
01591 {
01592     uint8_t result;
01593 
01594     result = side_info(hDecoder, ele, ld, ics, scal_flag);
01595     if (result > 0)
01596         return result;
01597 
01598     if (hDecoder->object_type >= ER_OBJECT_START) 
01599     {
01600         if (ics->tns_data_present)
01601             tns_data(ics, &(ics->tns), ld);
01602     }
01603 
01604 #ifdef DRM
01605     /* CRC check */
01606     if (hDecoder->object_type == DRM_ER_LC)
01607     {
01608         if ((result = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
01609             return result;
01610     }
01611 #endif
01612 
01613 #ifdef ERROR_RESILIENCE
01614     if (hDecoder->aacSpectralDataResilienceFlag)
01615     {
01616         /* error resilient spectral data decoding */
01617         if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
01618         {
01619             return result;
01620         }
01621     } else {
01622 #endif
01623         /* decode the spectral data */
01624         if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
01625         {
01626             return result;
01627         }
01628 #ifdef ERROR_RESILIENCE
01629     }
01630 #endif
01631 
01632     /* pulse coding reconstruction */
01633     if (ics->pulse_data_present)
01634     {
01635         if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
01636         {
01637             if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
01638                 return result;
01639         } else {
01640             return 2; /* pulse coding not allowed for short blocks */
01641         }
01642     }
01643 
01644     return 0;
01645 }
01646 
01647 /* Table 4.4.25 */
01648 static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
01649 {
01650     uint8_t g;
01651     uint8_t sect_esc_val, sect_bits;
01652 
01653     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
01654         sect_bits = 3;
01655     else
01656         sect_bits = 5;
01657     sect_esc_val = (1<<sect_bits) - 1;
01658 
01659 #if 0
01660     printf("\ntotal sfb %d\n", ics->max_sfb);
01661     printf("   sect    top     cb\n");
01662 #endif
01663 
01664     for (g = 0; g < ics->num_window_groups; g++)
01665     {
01666         uint8_t k = 0;
01667         uint8_t i = 0;
01668 
01669         while (k < ics->max_sfb)
01670         {
01671 #ifdef ERROR_RESILIENCE
01672             uint8_t vcb11 = 0;
01673 #endif
01674             uint8_t sfb;
01675             uint8_t sect_len_incr;
01676             uint16_t sect_len = 0;
01677             uint8_t sect_cb_bits = 4;
01678 
01679             /* if "faad_getbits" detects error and returns "0", "k" is never
01680                incremented and we cannot leave the while loop */
01681             if ((ld->error != 0) || (ld->no_more_reading))
01682                 return 14;
01683 
01684 #ifdef ERROR_RESILIENCE
01685             if (hDecoder->aacSectionDataResilienceFlag)
01686                 sect_cb_bits = 5;
01687 #endif
01688 
01689             ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
01690                 DEBUGVAR(1,71,"section_data(): sect_cb"));
01691 
01692             if (ics->sect_cb[g][i] == 12)
01693                 return 32;
01694 
01695 #if 0
01696             printf("%d\n", ics->sect_cb[g][i]);
01697 #endif
01698 
01699 #ifndef DRM
01700             if (ics->sect_cb[g][i] == NOISE_HCB)
01701                 ics->noise_used = 1;
01702 #else
01703             /* PNS not allowed in DRM */
01704             if (ics->sect_cb[g][i] == NOISE_HCB)
01705                 return 29;
01706 #endif
01707             if (ics->sect_cb[g][i] == INTENSITY_HCB2 || ics->sect_cb[g][i] == INTENSITY_HCB)
01708                 ics->is_used = 1;
01709 
01710 #ifdef ERROR_RESILIENCE
01711             if (hDecoder->aacSectionDataResilienceFlag)
01712             {
01713                 if ((ics->sect_cb[g][i] == 11) ||
01714                     ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
01715                 {
01716                     vcb11 = 1;
01717                 }
01718             }
01719             if (vcb11)
01720             {
01721                 sect_len_incr = 1;
01722             } else {
01723 #endif
01724                 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
01725                     DEBUGVAR(1,72,"section_data(): sect_len_incr"));
01726 #ifdef ERROR_RESILIENCE
01727             }
01728 #endif
01729             while ((sect_len_incr == sect_esc_val) /* &&
01730                 (k+sect_len < ics->max_sfb)*/)
01731             {
01732                 sect_len += sect_len_incr;
01733                 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
01734                     DEBUGVAR(1,72,"section_data(): sect_len_incr"));
01735             }
01736 
01737             sect_len += sect_len_incr;
01738 
01739             ics->sect_start[g][i] = k;
01740             ics->sect_end[g][i] = k + sect_len;
01741 
01742 #if 0
01743             printf("%d\n", ics->sect_start[g][i]);
01744 #endif
01745 #if 0
01746             printf("%d\n", ics->sect_end[g][i]);
01747 #endif
01748 
01749             if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
01750             {
01751                 if (k + sect_len >= 8*15)
01752                     return 15;
01753                 if (i >= 8*15)
01754                     return 15;
01755             } else {
01756                 if (k + sect_len >= MAX_SFB)
01757                     return 15;
01758                 if (i >= MAX_SFB)
01759                     return 15;
01760             }
01761 
01762             for (sfb = k; sfb < k + sect_len; sfb++)
01763             {
01764                 ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
01765 #if 0
01766                 printf("%d\n", ics->sfb_cb[g][sfb]);
01767 #endif
01768             }
01769 
01770 #if 0
01771             printf(" %6d %6d %6d\n",
01772                 i,
01773                 ics->sect_end[g][i],
01774                 ics->sect_cb[g][i]);
01775 #endif
01776 
01777             k += sect_len;
01778             i++;
01779         }
01780         ics->num_sec[g] = i;
01781 
01782         /* the sum of all sect_len_incr elements for a given window
01783          * group shall equal max_sfb */
01784         if (k != ics->max_sfb)
01785         {
01786             return 32;
01787         }
01788 #if 0
01789         printf("%d\n", ics->num_sec[g]);
01790 #endif
01791     }
01792 
01793 #if 0
01794     printf("\n");
01795 #endif
01796 
01797     return 0;
01798 }
01799 
01800 /*
01801  *  decode_scale_factors()
01802  *   decodes the scalefactors from the bitstream
01803  */
01804 /*
01805  * All scalefactors (and also the stereo positions and pns energies) are
01806  * transmitted using Huffman coded DPCM relative to the previous active
01807  * scalefactor (respectively previous stereo position or previous pns energy,
01808  * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
01809  * differentially coded relative to the global gain.
01810  */
01811 static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
01812 {
01813     uint8_t g, sfb;
01814     int16_t t;
01815     int8_t noise_pcm_flag = 1;
01816 
01817     int16_t scale_factor = ics->global_gain;
01818     int16_t is_position = 0;
01819     int16_t noise_energy = ics->global_gain - 90;
01820 
01821     for (g = 0; g < ics->num_window_groups; g++)
01822     {
01823         for (sfb = 0; sfb < ics->max_sfb; sfb++)
01824         {
01825             switch (ics->sfb_cb[g][sfb])
01826             {
01827             case ZERO_HCB: /* zero book */
01828                 ics->scale_factors[g][sfb] = 0;
01829 //#define SF_PRINT
01830 #ifdef SF_PRINT
01831                 printf("%d\n", ics->scale_factors[g][sfb]);
01832 #endif
01833                 break;
01834             case INTENSITY_HCB: /* intensity books */
01835             case INTENSITY_HCB2:
01836 
01837                 /* decode intensity position */
01838                 t = huffman_scale_factor(ld);
01839                 is_position += (t - 60);
01840                 ics->scale_factors[g][sfb] = is_position;
01841 #ifdef SF_PRINT
01842                 printf("%d\n", ics->scale_factors[g][sfb]);
01843 #endif
01844 
01845                 break;
01846             case NOISE_HCB: /* noise books */
01847 
01848 #ifndef DRM
01849                 /* decode noise energy */
01850                 if (noise_pcm_flag)
01851                 {
01852                     noise_pcm_flag = 0;
01853                     t = (int16_t)faad_getbits(ld, 9
01854                         DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
01855                 } else {
01856                     t = huffman_scale_factor(ld);
01857                     t -= 60;
01858                 }
01859                 noise_energy += t;
01860                 ics->scale_factors[g][sfb] = noise_energy;
01861 #ifdef SF_PRINT
01862                 printf("%d\n", ics->scale_factors[g][sfb]);
01863 #endif
01864 #else
01865                 /* PNS not allowed in DRM */
01866                 return 29;
01867 #endif
01868 
01869                 break;
01870             default: /* spectral books */
01871 
01872                 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
01873 
01874                 ics->scale_factors[g][sfb] = 0;
01875 
01876                 /* decode scale factor */
01877                 t = huffman_scale_factor(ld);
01878                 scale_factor += (t - 60);
01879                 if (scale_factor < 0 || scale_factor > 255)
01880                     return 4;
01881                 ics->scale_factors[g][sfb] = scale_factor;
01882 #ifdef SF_PRINT
01883                 printf("%d\n", ics->scale_factors[g][sfb]);
01884 #endif
01885 
01886                 break;
01887             }
01888         }
01889     }
01890 
01891     return 0;
01892 }
01893 
01894 /* Table 4.4.26 */
01895 static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
01896 {
01897     uint8_t ret = 0;
01898 #ifdef PROFILE
01899     int64_t count = faad_get_ts();
01900 #endif
01901 
01902 #ifdef ERROR_RESILIENCE
01903     if (!hDecoder->aacScalefactorDataResilienceFlag)
01904     {
01905 #endif
01906         ret = decode_scale_factors(ics, ld);
01907 #ifdef ERROR_RESILIENCE
01908     } else {
01909         /* In ER AAC the parameters for RVLC are seperated from the actual
01910            data that holds the scale_factors.
01911            Strangely enough, 2 parameters for HCR are put inbetween them.
01912         */
01913         ret = rvlc_scale_factor_data(ics, ld);
01914     }
01915 #endif
01916 
01917 #ifdef PROFILE
01918     count = faad_get_ts() - count;
01919     hDecoder->scalefac_cycles += count;
01920 #endif
01921 
01922     return ret;
01923 }
01924 
01925 /* Table 4.4.27 */
01926 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
01927 {
01928     uint8_t w, filt, i, start_coef_bits, coef_bits;
01929     uint8_t n_filt_bits = 2;
01930     uint8_t length_bits = 6;
01931     uint8_t order_bits = 5;
01932 
01933     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
01934     {
01935         n_filt_bits = 1;
01936         length_bits = 4;
01937         order_bits = 3;
01938     }
01939 
01940     for (w = 0; w < ics->num_windows; w++)
01941     {
01942         tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
01943             DEBUGVAR(1,74,"tns_data(): n_filt"));
01944 #if 0
01945         printf("%d\n", tns->n_filt[w]);
01946 #endif
01947 
01948         if (tns->n_filt[w])
01949         {
01950             if ((tns->coef_res[w] = faad_get1bit(ld
01951                 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
01952             {
01953                 start_coef_bits = 4;
01954             } else {
01955                 start_coef_bits = 3;
01956             }
01957 #if 0
01958             printf("%d\n", tns->coef_res[w]);
01959 #endif
01960         }
01961 
01962         for (filt = 0; filt < tns->n_filt[w]; filt++)
01963         {
01964             tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
01965                 DEBUGVAR(1,76,"tns_data(): length"));
01966 #if 0
01967             printf("%d\n", tns->length[w][filt]);
01968 #endif
01969             tns->order[w][filt]  = (uint8_t)faad_getbits(ld, order_bits
01970                 DEBUGVAR(1,77,"tns_data(): order"));
01971 #if 0
01972             printf("%d\n", tns->order[w][filt]);
01973 #endif
01974             if (tns->order[w][filt])
01975             {
01976                 tns->direction[w][filt] = faad_get1bit(ld
01977                     DEBUGVAR(1,78,"tns_data(): direction"));
01978 #if 0
01979                 printf("%d\n", tns->direction[w][filt]);
01980 #endif
01981                 tns->coef_compress[w][filt] = faad_get1bit(ld
01982                     DEBUGVAR(1,79,"tns_data(): coef_compress"));
01983 #if 0
01984                 printf("%d\n", tns->coef_compress[w][filt]);
01985 #endif
01986 
01987                 coef_bits = start_coef_bits - tns->coef_compress[w][filt];
01988                 for (i = 0; i < tns->order[w][filt]; i++)
01989                 {
01990                     tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
01991                         DEBUGVAR(1,80,"tns_data(): coef"));
01992 #if 0
01993                     printf("%d\n", tns->coef[w][filt][i]);
01994 #endif
01995                 }
01996             }
01997         }
01998     }
01999 }
02000 
02001 #ifdef LTP_DEC
02002 /* Table 4.4.28 */
02003 static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
02004 {
02005     uint8_t sfb, w;
02006 
02007     ltp->lag = 0;
02008 
02009 #ifdef LD_DEC
02010     if (hDecoder->object_type == LD)
02011     {
02012         ltp->lag_update = (uint8_t)faad_getbits(ld, 1
02013             DEBUGVAR(1,142,"ltp_data(): lag_update"));
02014 
02015         if (ltp->lag_update)
02016         {
02017             ltp->lag = (uint16_t)faad_getbits(ld, 10
02018                 DEBUGVAR(1,81,"ltp_data(): lag"));
02019         }
02020     } else {
02021 #endif
02022         ltp->lag = (uint16_t)faad_getbits(ld, 11
02023             DEBUGVAR(1,81,"ltp_data(): lag"));
02024 #ifdef LD_DEC
02025     }
02026 #endif
02027 
02028     /* Check length of lag */
02029     if (ltp->lag > (hDecoder->frameLength << 1))
02030         return 18;
02031 
02032     ltp->coef = (uint8_t)faad_getbits(ld, 3
02033         DEBUGVAR(1,82,"ltp_data(): coef"));
02034 
02035     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
02036     {
02037         for (w = 0; w < ics->num_windows; w++)
02038         {
02039             if ((ltp->short_used[w] = faad_get1bit(ld
02040                 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
02041             {
02042                 ltp->short_lag_present[w] = faad_get1bit(ld
02043                     DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
02044                 if (ltp->short_lag_present[w])
02045                 {
02046                     ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
02047                         DEBUGVAR(1,85,"ltp_data(): short_lag"));
02048                 }
02049             }
02050         }
02051     } else {
02052         ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
02053 
02054         for (sfb = 0; sfb < ltp->last_band; sfb++)
02055         {
02056             ltp->long_used[sfb] = faad_get1bit(ld
02057                 DEBUGVAR(1,86,"ltp_data(): long_used"));
02058         }
02059     }
02060 
02061     return 0;
02062 }
02063 #endif
02064 
02065 /* Table 4.4.29 */
02066 static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
02067                              int16_t *spectral_data)
02068 {
02069     int8_t i;
02070     uint8_t g;
02071     uint16_t inc, k, p = 0;
02072     uint8_t groups = 0;
02073     uint8_t sect_cb;
02074     uint8_t result;
02075     uint16_t nshort = hDecoder->frameLength/8;
02076 
02077 #ifdef PROFILE
02078     int64_t count = faad_get_ts();
02079 #endif
02080 
02081     for(g = 0; g < ics->num_window_groups; g++)
02082     {
02083         p = groups*nshort;
02084 
02085         for (i = 0; i < ics->num_sec[g]; i++)
02086         {
02087             sect_cb = ics->sect_cb[g][i];
02088 
02089             inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
02090 
02091             switch (sect_cb)
02092             {
02093             case ZERO_HCB:
02094             case NOISE_HCB:
02095             case INTENSITY_HCB:
02096             case INTENSITY_HCB2:
02097 //#define SD_PRINT
02098 #ifdef SD_PRINT
02099                 {
02100                     int j;
02101                     for (j = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; j < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; j++)
02102                     {
02103                         printf("%d\n", 0);
02104                     }
02105                 }
02106 #endif
02107 //#define SFBO_PRINT
02108 #ifdef SFBO_PRINT
02109                 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
02110 #endif
02111                 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
02112                     ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
02113                 break;
02114             default:
02115 #ifdef SFBO_PRINT
02116                 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
02117 #endif
02118                 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
02119                      k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
02120                 {
02121                     if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
02122                         return result;
02123 #ifdef SD_PRINT
02124                     {
02125                         int j;
02126                         for (j = p; j < p+inc; j++)
02127                         {
02128                             printf("%d\n", spectral_data[j]);
02129                         }
02130                     }
02131 #endif
02132                     p += inc;
02133                 }
02134                 break;
02135             }
02136         }
02137         groups += ics->window_group_length[g];
02138     }
02139 
02140 #ifdef PROFILE
02141     count = faad_get_ts() - count;
02142     hDecoder->spectral_cycles += count;
02143 #endif
02144 
02145     return 0;
02146 }
02147 
02148 /* Table 4.4.30 */
02149 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
02150 {
02151     uint16_t i, n, dataElementLength;
02152     uint8_t dataElementLengthPart;
02153     uint8_t align = 4, data_element_version, loopCounter;
02154 
02155     uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
02156         DEBUGVAR(1,87,"extension_payload(): extension_type"));
02157 
02158     switch (extension_type)
02159     {
02160     case EXT_DYNAMIC_RANGE:
02161         drc->present = 1;
02162         n = dynamic_range_info(ld, drc);
02163         return n;
02164     case EXT_FILL_DATA:
02165         /* fill_nibble = */ faad_getbits(ld, 4
02166             DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be ‘0000’ */
02167         for (i = 0; i < count-1; i++)
02168         {
02169             /* fill_byte[i] = */ faad_getbits(ld, 8
02170                 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */
02171         }
02172         return count;
02173     case EXT_DATA_ELEMENT:
02174         data_element_version = (uint8_t)faad_getbits(ld, 4
02175             DEBUGVAR(1,400,"extension_payload(): data_element_version"));
02176         switch (data_element_version)
02177         {
02178         case ANC_DATA:
02179             loopCounter = 0;
02180             dataElementLength = 0;
02181             do {
02182                 dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
02183                     DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
02184                 dataElementLength += dataElementLengthPart;
02185                 loopCounter++;
02186             } while (dataElementLengthPart == 255);
02187 
02188             for (i = 0; i < dataElementLength; i++)
02189             {
02190                 /* data_element_byte[i] = */ faad_getbits(ld, 8
02191                     DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
02192                 return (dataElementLength+loopCounter+1);
02193             }
02194         default:
02195             align = 0;
02196         }
02197     case EXT_FIL:
02198     default:
02199         faad_getbits(ld, align
02200             DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
02201         for (i = 0; i < count-1; i++)
02202         {
02203             /* other_bits[i] = */ faad_getbits(ld, 8
02204                DEBUGVAR(1,89,"extension_payload(): fill_bit"));
02205         }
02206         return count;
02207     }
02208 }
02209 
02210 /* Table 4.4.31 */
02211 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
02212 {
02213     uint8_t i, n = 1;
02214     uint8_t band_incr;
02215 
02216     drc->num_bands = 1;
02217 
02218     if (faad_get1bit(ld
02219         DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
02220     {
02221         drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4
02222             DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
02223         /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
02224             DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
02225         n++;
02226     }
02227 
02228     drc->excluded_chns_present = faad_get1bit(ld
02229         DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
02230     if (drc->excluded_chns_present == 1)
02231     {
02232         n += excluded_channels(ld, drc);
02233     }
02234 
02235     if (faad_get1bit(ld
02236         DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
02237     {
02238         band_incr = (uint8_t)faad_getbits(ld, 4
02239             DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
02240         /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
02241             DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
02242         n++;
02243         drc->num_bands += band_incr;
02244 
02245         for (i = 0; i < drc->num_bands; i++);
02246         {
02247             drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
02248                 DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
02249             n++;
02250         }
02251     }
02252 
02253     if (faad_get1bit(ld
02254         DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
02255     {
02256         drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7
02257             DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
02258         /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
02259             DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
02260         n++;
02261     }
02262 
02263     for (i = 0; i < drc->num_bands; i++)
02264     {
02265         drc->dyn_rng_sgn[i] = faad_get1bit(ld
02266             DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
02267         drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7
02268             DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
02269         n++;
02270     }
02271 
02272     return n;
02273 }
02274 
02275 /* Table 4.4.32 */
02276 static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
02277 {
02278     uint8_t i, n = 0;
02279     uint8_t num_excl_chan = 7;
02280 
02281     for (i = 0; i < 7; i++)
02282     {
02283         drc->exclude_mask[i] = faad_get1bit(ld
02284             DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
02285     }
02286     n++;
02287 
02288     while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
02289         DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
02290     {
02291         for (i = num_excl_chan; i < num_excl_chan+7; i++)
02292         {
02293             drc->exclude_mask[i] = faad_get1bit(ld
02294                 DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
02295         }
02296         n++;
02297         num_excl_chan += 7;
02298     }
02299 
02300     return n;
02301 }
02302 
02303 /* Annex A: Audio Interchange Formats */
02304 
02305 /* Table 1.A.2 */
02306 void get_adif_header(adif_header *adif, bitfile *ld)
02307 {
02308     uint8_t i;
02309 
02310     /* adif_id[0] = */ faad_getbits(ld, 8
02311         DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
02312     /* adif_id[1] = */ faad_getbits(ld, 8
02313         DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
02314     /* adif_id[2] = */ faad_getbits(ld, 8
02315         DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
02316     /* adif_id[3] = */ faad_getbits(ld, 8
02317         DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
02318     adif->copyright_id_present = faad_get1bit(ld
02319         DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
02320     if(adif->copyright_id_present)
02321     {
02322         for (i = 0; i < 72/8; i++)
02323         {
02324             adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
02325                 DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
02326         }
02327         adif->copyright_id[i] = 0;
02328     }
02329     adif->original_copy  = faad_get1bit(ld
02330         DEBUGVAR(1,112,"get_adif_header(): original_copy"));
02331     adif->home = faad_get1bit(ld
02332         DEBUGVAR(1,113,"get_adif_header(): home"));
02333     adif->bitstream_type = faad_get1bit(ld
02334         DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
02335     adif->bitrate = faad_getbits(ld, 23
02336         DEBUGVAR(1,115,"get_adif_header(): bitrate"));
02337     adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4
02338         DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
02339 
02340     for (i = 0; i < adif->num_program_config_elements + 1; i++)
02341     {
02342         if(adif->bitstream_type == 0)
02343         {
02344             adif->adif_buffer_fullness = faad_getbits(ld, 20
02345                 DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
02346         } else {
02347             adif->adif_buffer_fullness = 0;
02348         }
02349 
02350         program_config_element(&adif->pce[i], ld);
02351     }
02352 }
02353 
02354 /* Table 1.A.5 */
02355 uint8_t adts_frame(adts_header *adts, bitfile *ld)
02356 {
02357     /* faad_byte_align(ld); */
02358     if (adts_fixed_header(adts, ld))
02359         return 5;
02360     adts_variable_header(adts, ld);
02361     adts_error_check(adts, ld);
02362 
02363     return 0;
02364 }
02365 
02366 /* Table 1.A.6 */
02367 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
02368 {
02369     uint16_t i;
02370     uint8_t sync_err = 1;
02371 
02372     /* try to recover from sync errors */
02373     for (i = 0; i < 768; i++)
02374     {
02375         adts->syncword = (uint16_t)faad_showbits(ld, 12);
02376         if (adts->syncword != 0xFFF)
02377         {
02378             faad_getbits(ld, 8
02379                 DEBUGVAR(0,0,""));
02380         } else {
02381             sync_err = 0;
02382             faad_getbits(ld, 12
02383                 DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
02384             break;
02385         }
02386     }
02387     if (sync_err)
02388         return 5;
02389 
02390     adts->id = faad_get1bit(ld
02391         DEBUGVAR(1,119,"adts_fixed_header(): id"));
02392     adts->layer = (uint8_t)faad_getbits(ld, 2
02393         DEBUGVAR(1,120,"adts_fixed_header(): layer"));
02394     adts->protection_absent = faad_get1bit(ld
02395         DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
02396     adts->profile = (uint8_t)faad_getbits(ld, 2
02397         DEBUGVAR(1,122,"adts_fixed_header(): profile"));
02398     adts->sf_index = (uint8_t)faad_getbits(ld, 4
02399         DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
02400     adts->private_bit = faad_get1bit(ld
02401         DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
02402     adts->channel_configuration = (uint8_t)faad_getbits(ld, 3
02403         DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
02404     adts->original = faad_get1bit(ld
02405         DEBUGVAR(1,126,"adts_fixed_header(): original"));
02406     adts->home = faad_get1bit(ld
02407         DEBUGVAR(1,127,"adts_fixed_header(): home"));
02408 
02409     if (adts->old_format == 1)
02410     {
02411         /* Removed in corrigendum 14496-3:2002 */
02412         if (adts->id == 0)
02413         {
02414             adts->emphasis = (uint8_t)faad_getbits(ld, 2
02415                 DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
02416         }
02417     }
02418 
02419     return 0;
02420 }
02421 
02422 /* Table 1.A.7 */
02423 static void adts_variable_header(adts_header *adts, bitfile *ld)
02424 {
02425     adts->copyright_identification_bit = faad_get1bit(ld
02426         DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
02427     adts->copyright_identification_start = faad_get1bit(ld
02428         DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
02429     adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13
02430         DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
02431     adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11
02432         DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
02433     adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2
02434         DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
02435 }
02436 
02437 /* Table 1.A.8 */
02438 static void adts_error_check(adts_header *adts, bitfile *ld)
02439 {
02440     if (adts->protection_absent == 0)
02441     {
02442         adts->crc_check = (uint16_t)faad_getbits(ld, 16
02443             DEBUGVAR(1,134,"adts_error_check(): crc_check"));
02444     }
02445 }

Generated on Tue Dec 13 14:47:47 2005 for guliverkli by  doxygen 1.4.5