syntax.c
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:78k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*
  2. ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
  3. ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
  4. **  
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. ** 
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. ** 
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software 
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. **
  19. ** Any non-GPL usage of this software or parts of this software is strictly
  20. ** forbidden.
  21. **
  22. ** Software using this code must display the following message visibly in the
  23. ** software:
  24. ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
  25. ** in, for example, the about-box or help/startup screen.
  26. **
  27. ** Commercial non-GPL licensing of this software is possible.
  28. ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
  29. **
  30. ** $Id: syntax.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
  31. **/
  32. /*
  33.    Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
  34. */
  35. #include "common.h"
  36. #include "structs.h"
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "decoder.h"
  40. #include "syntax.h"
  41. #include "specrec.h"
  42. #include "huffman.h"
  43. #include "bits.h"
  44. #include "pulse.h"
  45. #include "analysis.h"
  46. #include "drc.h"
  47. #ifdef ERROR_RESILIENCE
  48. #include "rvlc.h"
  49. #endif
  50. #ifdef SBR_DEC
  51. #include "sbr_syntax.h"
  52. #endif
  53. /* static function declarations */
  54. static void decode_sce_lfe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
  55.                            uint8_t id_syn_ele);
  56. static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
  57.                        uint8_t id_syn_ele);
  58. static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
  59.                                           uint8_t channel, uint8_t *tag);
  60. static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
  61.                                     uint8_t channel, uint8_t *tag);
  62. #ifdef COUPLING_DEC
  63. static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld);
  64. #endif
  65. static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld);
  66. static uint8_t program_config_element(program_config *pce, bitfile *ld);
  67. static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
  68. #ifdef SBR_DEC
  69.                             ,uint8_t sbr_ele
  70. #endif
  71.                             );
  72. static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
  73.                                          bitfile *ld, ic_stream *ics, uint8_t scal_flag,
  74.                                          int16_t *spec_data);
  75. static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
  76.                         uint8_t common_window);
  77. static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
  78. static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
  79. #ifdef SSR_DEC
  80. static void gain_control_data(bitfile *ld, ic_stream *ics);
  81. #endif
  82. static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
  83.                              int16_t *spectral_data);
  84. static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
  85. static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
  86. static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
  87. #ifdef LTP_DEC
  88. static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
  89. #endif
  90. static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
  91. static void adts_variable_header(adts_header *adts, bitfile *ld);
  92. static void adts_error_check(adts_header *adts, bitfile *ld);
  93. static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
  94. static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
  95. static uint8_t side_info(NeAACDecHandle hDecoder, element *ele,
  96.                          bitfile *ld, ic_stream *ics, uint8_t scal_flag);
  97. #ifdef DRM
  98. static int8_t DRM_aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
  99.                                            bitfile *ld, uint8_t this_layer_stereo);
  100. #endif
  101. /* Table 4.4.1 */
  102. int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
  103.                         program_config *pce_out)
  104. {
  105.     program_config pce;
  106.     /* 1024 or 960 */
  107.     mp4ASC->frameLengthFlag = faad_get1bit(ld
  108.         DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
  109. #ifndef ALLOW_SMALL_FRAMELENGTH
  110.     if (mp4ASC->frameLengthFlag == 1)
  111.         return -3;
  112. #endif
  113.     mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
  114.         DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
  115.     if (mp4ASC->dependsOnCoreCoder == 1)
  116.     {
  117.         mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
  118.             DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
  119.     }
  120.     mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
  121.     if (mp4ASC->channelsConfiguration == 0)
  122.     {
  123.         if (program_config_element(&pce, ld))
  124.             return -3;
  125.         //mp4ASC->channelsConfiguration = pce.channels;
  126.         if (pce_out != NULL)
  127.             memcpy(pce_out, &pce, sizeof(program_config));
  128.         /*
  129.         if (pce.num_valid_cc_elements)
  130.             return -3;
  131.         */
  132.     }
  133. #ifdef ERROR_RESILIENCE
  134.     if (mp4ASC->extensionFlag == 1)
  135.     {
  136.         /* Error resilience not supported yet */
  137.         if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
  138.         {
  139.             mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
  140.                 DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
  141.             mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
  142.                 DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
  143.             mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
  144.                 DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
  145.             /* 1 bit: extensionFlag3 */
  146.         }
  147.     }
  148. #endif
  149.     return 0;
  150. }
  151. /* Table 4.4.2 */
  152. /* An MPEG-4 Audio decoder is only required to follow the Program
  153.    Configuration Element in GASpecificConfig(). The decoder shall ignore
  154.    any Program Configuration Elements that may occur in raw data blocks.
  155.    PCEs transmitted in raw data blocks cannot be used to convey decoder
  156.    configuration information.
  157. */
  158. static uint8_t program_config_element(program_config *pce, bitfile *ld)
  159. {
  160.     uint8_t i;
  161.     memset(pce, 0, sizeof(program_config));
  162.     pce->channels = 0;
  163.     pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
  164.         DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
  165.     pce->object_type = (uint8_t)faad_getbits(ld, 2
  166.         DEBUGVAR(1,11,"program_config_element(): object_type"));
  167.     pce->sf_index = (uint8_t)faad_getbits(ld, 4
  168.         DEBUGVAR(1,12,"program_config_element(): sf_index"));
  169.     pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
  170.         DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
  171.     pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
  172.         DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
  173.     pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
  174.         DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
  175.     pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
  176.         DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
  177.     pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
  178.         DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
  179.     pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
  180.         DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
  181.     pce->mono_mixdown_present = faad_get1bit(ld
  182.         DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
  183.     if (pce->mono_mixdown_present == 1)
  184.     {
  185.         pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
  186.             DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
  187.     }
  188.     pce->stereo_mixdown_present = faad_get1bit(ld
  189.         DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
  190.     if (pce->stereo_mixdown_present == 1)
  191.     {
  192.         pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
  193.             DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
  194.     }
  195.     pce->matrix_mixdown_idx_present = faad_get1bit(ld
  196.         DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
  197.     if (pce->matrix_mixdown_idx_present == 1)
  198.     {
  199.         pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
  200.             DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
  201.         pce->pseudo_surround_enable = faad_get1bit(ld
  202.             DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
  203.     }
  204.     for (i = 0; i < pce->num_front_channel_elements; i++)
  205.     {
  206.         pce->front_element_is_cpe[i] = faad_get1bit(ld
  207.             DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
  208.         pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  209.             DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
  210.         if (pce->front_element_is_cpe[i] & 1)
  211.         {
  212.             pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
  213.             pce->num_front_channels += 2;
  214.             pce->channels += 2;
  215.         } else {
  216.             pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
  217.             pce->num_front_channels++;
  218.             pce->channels++;
  219.         }
  220.     }
  221.     for (i = 0; i < pce->num_side_channel_elements; i++)
  222.     {
  223.         pce->side_element_is_cpe[i] = faad_get1bit(ld
  224.             DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
  225.         pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  226.             DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
  227.         if (pce->side_element_is_cpe[i] & 1)
  228.         {
  229.             pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
  230.             pce->num_side_channels += 2;
  231.             pce->channels += 2;
  232.         } else {
  233.             pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
  234.             pce->num_side_channels++;
  235.             pce->channels++;
  236.         }
  237.     }
  238.     for (i = 0; i < pce->num_back_channel_elements; i++)
  239.     {
  240.         pce->back_element_is_cpe[i] = faad_get1bit(ld
  241.             DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
  242.         pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  243.             DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
  244.         if (pce->back_element_is_cpe[i] & 1)
  245.         {
  246.             pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
  247.             pce->channels += 2;
  248.             pce->num_back_channels += 2;
  249.         } else {
  250.             pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
  251.             pce->num_back_channels++;
  252.             pce->channels++;
  253.         }
  254.     }
  255.     for (i = 0; i < pce->num_lfe_channel_elements; i++)
  256.     {
  257.         pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  258.             DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
  259.         pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
  260.         pce->num_lfe_channels++;
  261.         pce->channels++;
  262.     }
  263.     for (i = 0; i < pce->num_assoc_data_elements; i++)
  264.         pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  265.         DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
  266.     for (i = 0; i < pce->num_valid_cc_elements; i++)
  267.     {
  268.         pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
  269.             DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
  270.         pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
  271.             DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
  272.     }
  273.     faad_byte_align(ld);
  274.     pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
  275.         DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
  276.     for (i = 0; i < pce->comment_field_bytes; i++)
  277.     {
  278.         pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
  279.             DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
  280.     }
  281.     pce->comment_field_data[i] = 0;
  282.     if (pce->channels > MAX_CHANNELS)
  283.         return 22;
  284.     return 0;
  285. }
  286. static void decode_sce_lfe(NeAACDecHandle hDecoder,
  287.                            NeAACDecFrameInfo *hInfo, bitfile *ld,
  288.                            uint8_t id_syn_ele)
  289. {
  290.     uint8_t channels = hDecoder->fr_channels;
  291.     uint8_t tag = 0;
  292.     if (channels+1 > MAX_CHANNELS)
  293.     {
  294.         hInfo->error = 12;
  295.         return;
  296.     }
  297.     if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
  298.     {
  299.         hInfo->error = 13;
  300.         return;
  301.     }
  302.     /* for SCE hDecoder->element_output_channels[] is not set here because this
  303.        can become 2 when some form of Parametric Stereo coding is used
  304.     */
  305.     /* save the syntax element id */
  306.     hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
  307.     /* decode the element */
  308.     hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
  309.     /* map output channels position to internal data channels */
  310.     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
  311.     {
  312.         /* this might be faulty when pce_set is true */
  313.         hDecoder->internal_channel[channels] = channels;
  314.         hDecoder->internal_channel[channels+1] = channels+1;
  315.     } else {
  316.         if (hDecoder->pce_set)
  317.             hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
  318.         else
  319.             hDecoder->internal_channel[channels] = channels;
  320.     }
  321.     hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
  322.     hDecoder->fr_ch_ele++;
  323. }
  324. static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
  325.                        uint8_t id_syn_ele)
  326. {
  327.     uint8_t channels = hDecoder->fr_channels;
  328.     uint8_t tag = 0;
  329.     if (channels+2 > MAX_CHANNELS)
  330.     {
  331.         hInfo->error = 12;
  332.         return;
  333.     }
  334.     if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
  335.     {
  336.         hInfo->error = 13;
  337.         return;
  338.     }
  339.     /* for CPE the number of output channels is always 2 */
  340.     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
  341.     {
  342.         /* element_output_channels not set yet */
  343.         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
  344.     } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
  345.         /* element inconsistency */
  346.         hInfo->error = 21;
  347.         return;
  348.     }
  349.     /* save the syntax element id */
  350.     hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
  351.     /* decode the element */
  352.     hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
  353.     /* map output channel position to internal data channels */
  354.     if (hDecoder->pce_set)
  355.     {
  356.         hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
  357.         hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
  358.     } else {
  359.         hDecoder->internal_channel[channels] = channels;
  360.         hDecoder->internal_channel[channels+1] = channels+1;
  361.     }
  362.     hDecoder->fr_channels += 2;
  363.     hDecoder->fr_ch_ele++;
  364. }
  365. void raw_data_block(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
  366.                     bitfile *ld, program_config *pce, drc_info *drc)
  367. {
  368.     uint8_t id_syn_ele;
  369.     uint8_t ele_this_frame = 0;
  370.     hDecoder->fr_channels = 0;
  371.     hDecoder->fr_ch_ele = 0;
  372.     hDecoder->first_syn_ele = 25;
  373.     hDecoder->has_lfe = 0;
  374. #ifdef ERROR_RESILIENCE
  375.     if (hDecoder->object_type < ER_OBJECT_START)
  376.     {
  377. #endif
  378.         /* Table 4.4.3: raw_data_block() */
  379.         while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
  380.             DEBUGVAR(1,4,"NeAACDecDecode(): id_syn_ele"))) != ID_END)
  381.         {
  382.             switch (id_syn_ele) {
  383.             case ID_SCE:
  384.                 ele_this_frame++;
  385.                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
  386.                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
  387.                 if (hInfo->error > 0)
  388.                     return;
  389.                 break;
  390.             case ID_CPE:
  391.                 ele_this_frame++;
  392.                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
  393.                 decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
  394.                 if (hInfo->error > 0)
  395.                     return;
  396.                 break;
  397.             case ID_LFE:
  398. #ifdef DRM
  399.                 hInfo->error = 32;
  400. #else
  401.                 ele_this_frame++;
  402.                 hDecoder->has_lfe++;
  403.                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
  404. #endif
  405.                 if (hInfo->error > 0)
  406.                     return;
  407.                 break;
  408.             case ID_CCE: /* not implemented yet, but skip the bits */
  409. #ifdef DRM
  410.                 hInfo->error = 32;
  411. #else
  412.                 ele_this_frame++;
  413. #ifdef COUPLING_DEC
  414.                 hInfo->error = coupling_channel_element(hDecoder, ld);
  415. #else
  416.                 hInfo->error = 6;
  417. #endif
  418. #endif
  419.                 if (hInfo->error > 0)
  420.                     return;
  421.                 break;
  422.             case ID_DSE:
  423.                 ele_this_frame++;
  424.                 data_stream_element(hDecoder, ld);
  425.                 break;
  426.             case ID_PCE:
  427.                 if (ele_this_frame != 0)
  428.                 {
  429.                     hInfo->error = 31;
  430.                     return;
  431.                 }
  432.                 ele_this_frame++;
  433.                 /* 14496-4: 5.6.4.1.2.1.3: */
  434.                 /* program_configuration_element()'s in access units shall be ignored */
  435.                 program_config_element(pce, ld);
  436.                 //if ((hInfo->error = program_config_element(pce, ld)) > 0)
  437.                 //    return;
  438.                 //hDecoder->pce_set = 1;
  439.                 break;
  440.             case ID_FIL:
  441.                 ele_this_frame++;
  442.                 /* one sbr_info describes a channel_element not a channel! */
  443.                 /* if we encounter SBR data here: error */
  444.                 /* SBR data will be read directly in the SCE/LFE/CPE element */
  445.                 if ((hInfo->error = fill_element(hDecoder, ld, drc
  446. #ifdef SBR_DEC
  447.                     , INVALID_SBR_ELEMENT
  448. #endif
  449.                     )) > 0)
  450.                     return;
  451.                 break;
  452.             }
  453.         }
  454. #ifdef ERROR_RESILIENCE
  455.     } else {
  456.         /* Table 262: er_raw_data_block() */
  457.         switch (hDecoder->channelConfiguration)
  458.         {
  459.         case 1:
  460.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  461.             if (hInfo->error > 0)
  462.                 return;
  463.             break;
  464.         case 2:
  465.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  466.             if (hInfo->error > 0)
  467.                 return;
  468.             break;
  469.         case 3:
  470.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  471.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  472.             if (hInfo->error > 0)
  473.                 return;
  474.             break;
  475.         case 4:
  476.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  477.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  478.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  479.             if (hInfo->error > 0)
  480.                 return;
  481.             break;
  482.         case 5:
  483.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  484.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  485.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  486.             if (hInfo->error > 0)
  487.                 return;
  488.             break;
  489.         case 6:
  490.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  491.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  492.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  493.             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
  494.             if (hInfo->error > 0)
  495.                 return;
  496.             break;
  497.         case 7: /* 8 channels */
  498.             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
  499.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  500.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  501.             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
  502.             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
  503.             if (hInfo->error > 0)
  504.                 return;
  505.             break;
  506.         default:
  507.             hInfo->error = 7;
  508.             return;
  509.         }
  510. #if 0
  511.         cnt = bits_to_decode() / 8;
  512.         while (cnt >= 1)
  513.         {
  514.             cnt -= extension_payload(cnt);
  515.         }
  516. #endif
  517.     }
  518. #endif
  519.     /* new in corrigendum 14496-3:2002 */
  520. #ifdef DRM
  521.     if (hDecoder->object_type != DRM_ER_LC)
  522. #endif
  523.     {
  524.         faad_byte_align(ld);
  525.     }
  526.     return;
  527. }
  528. /* Table 4.4.4 and */
  529. /* Table 4.4.9 */
  530. static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
  531.                                           uint8_t channel, uint8_t *tag)
  532. {
  533.     uint8_t retval = 0;
  534.     element sce = {0};
  535.     ic_stream *ics = &(sce.ics1);
  536.     ALIGN int16_t spec_data[1024] = {0};
  537.     sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
  538.         DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
  539.     *tag = sce.element_instance_tag;
  540.     sce.channel = channel;
  541.     sce.paired_channel = -1;
  542.     retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
  543.     if (retval > 0)
  544.         return retval;
  545.     /* IS not allowed in single channel */
  546.     if (ics->is_used)
  547.         return 32;
  548. #ifdef SBR_DEC
  549.     /* check if next bitstream element is a fill element */
  550.     /* if so, read it now so SBR decoding can be done in case of a file with SBR */
  551.     if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
  552.     {
  553.         faad_flushbits(ld, LEN_SE_ID);
  554.         /* one sbr_info describes a channel_element not a channel! */
  555.         if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
  556.         {
  557.             return retval;
  558.         }
  559.     }
  560. #endif
  561.     /* noiseless coding is done, spectral reconstruction is done now */
  562.     retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
  563.     if (retval > 0)
  564.         return retval;
  565.     return 0;
  566. }
  567. /* Table 4.4.5 */
  568. static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
  569.                                     uint8_t channels, uint8_t *tag)
  570. {
  571.     ALIGN int16_t spec_data1[1024] = {0};
  572.     ALIGN int16_t spec_data2[1024] = {0};
  573.     element cpe = {0};
  574.     ic_stream *ics1 = &(cpe.ics1);
  575.     ic_stream *ics2 = &(cpe.ics2);
  576.     uint8_t result;
  577.     cpe.channel        = channels;
  578.     cpe.paired_channel = channels+1;
  579.     cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
  580.         DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
  581.     *tag = cpe.element_instance_tag;
  582.     if ((cpe.common_window = faad_get1bit(ld
  583.         DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
  584.     {
  585.         /* both channels have common ics information */
  586.         if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
  587.             return result;
  588.         ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
  589.             DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
  590.         if (ics1->ms_mask_present == 3)
  591.         {
  592.             /* bitstream error */
  593.             return 32;
  594.         }
  595.         if (ics1->ms_mask_present == 1)
  596.         {
  597.             uint8_t g, sfb;
  598.             for (g = 0; g < ics1->num_window_groups; g++)
  599.             {
  600.                 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
  601.                 {
  602.                     ics1->ms_used[g][sfb] = faad_get1bit(ld
  603.                         DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
  604.                 }
  605.             }
  606.         }
  607. #ifdef ERROR_RESILIENCE
  608.         if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
  609.         {
  610.             if ((
  611. #ifdef LTP_DEC
  612.                 ics1->ltp.data_present =
  613. #endif
  614.                 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
  615.             {
  616. #ifdef LTP_DEC
  617.                 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
  618.                 {
  619.                     return result;
  620.                 }
  621. #else
  622.                 return 26;
  623. #endif
  624.             }
  625.         }
  626. #endif
  627.         memcpy(ics2, ics1, sizeof(ic_stream));
  628.     } else {
  629.         ics1->ms_mask_present = 0;
  630.     }
  631.     if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
  632.         0, spec_data1)) > 0)
  633.     {
  634.         return result;
  635.     }
  636. #ifdef ERROR_RESILIENCE
  637.     if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
  638.         (ics1->predictor_data_present))
  639.     {
  640.         if ((
  641. #ifdef LTP_DEC
  642.             ics1->ltp2.data_present =
  643. #endif
  644.             faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
  645.         {
  646. #ifdef LTP_DEC
  647.             if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
  648.             {
  649.                 return result;
  650.             }
  651. #else
  652.             return 26;
  653. #endif
  654.         }
  655.     }
  656. #endif
  657.     if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
  658.         0, spec_data2)) > 0)
  659.     {
  660.         return result;
  661.     }
  662. #ifdef SBR_DEC
  663.     /* check if next bitstream element is a fill element */
  664.     /* if so, read it now so SBR decoding can be done in case of a file with SBR */
  665.     if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
  666.     {
  667.         faad_flushbits(ld, LEN_SE_ID);
  668.         /* one sbr_info describes a channel_element not a channel! */
  669.         if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
  670.         {
  671.             return result;
  672.         }
  673.     }
  674. #endif
  675.     /* noiseless coding is done, spectral reconstruction is done now */
  676.     if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
  677.         spec_data1, spec_data2)) > 0)
  678.     {
  679.         return result;
  680.     }
  681.     return 0;
  682. }
  683. /* Table 4.4.6 */
  684. static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
  685.                         uint8_t common_window)
  686. {
  687.     uint8_t retval = 0;
  688.     uint8_t ics_reserved_bit;
  689.     ics_reserved_bit = faad_get1bit(ld
  690.         DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
  691.     if (ics_reserved_bit != 0)
  692.         return 32;
  693.     ics->window_sequence = (uint8_t)faad_getbits(ld, 2
  694.         DEBUGVAR(1,44,"ics_info(): window_sequence"));
  695.     ics->window_shape = faad_get1bit(ld
  696.         DEBUGVAR(1,45,"ics_info(): window_shape"));
  697. #ifdef LD_DEC
  698.     /* No block switching in LD */
  699.     if ((hDecoder->object_type == LD) && (ics->window_sequence != ONLY_LONG_SEQUENCE))
  700.         return 32;
  701. #endif
  702.     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
  703.     {
  704.         ics->max_sfb = (uint8_t)faad_getbits(ld, 4
  705.             DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
  706.         ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
  707.             DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
  708.     } else {
  709.         ics->max_sfb = (uint8_t)faad_getbits(ld, 6
  710.             DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
  711.     }
  712.     /* get the grouping information */
  713.     if ((retval = window_grouping_info(hDecoder, ics)) > 0)
  714.         return retval;
  715.     /* should be an error */
  716.     /* check the range of max_sfb */
  717.     if (ics->max_sfb > ics->num_swb)
  718.         return 16;
  719.     if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
  720.     {
  721.         if ((ics->predictor_data_present = faad_get1bit(ld
  722.             DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
  723.         {
  724.             if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
  725.             {
  726.                 uint8_t sfb;
  727.                 uint8_t limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
  728. #ifdef MAIN_DEC
  729.                 ics->pred.limit = limit;
  730. #endif
  731.                 if ((
  732. #ifdef MAIN_DEC
  733.                     ics->pred.predictor_reset =
  734. #endif
  735.                     faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
  736.                 {
  737. #ifdef MAIN_DEC
  738.                     ics->pred.predictor_reset_group_number =
  739. #endif
  740.                         (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
  741.                 }
  742.                 for (sfb = 0; sfb < limit; sfb++)
  743.                 {
  744. #ifdef MAIN_DEC
  745.                     ics->pred.prediction_used[sfb] =
  746. #endif
  747.                         faad_get1bit(ld DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
  748.                 }
  749.             }
  750. #ifdef LTP_DEC
  751.             else { /* Long Term Prediction */
  752.                 if (hDecoder->object_type < ER_OBJECT_START)
  753.                 {
  754.                     if ((ics->ltp.data_present = faad_get1bit(ld
  755.                         DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
  756.                     {
  757.                         if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
  758.                         {
  759.                             return retval;
  760.                         }
  761.                     }
  762.                     if (common_window)
  763.                     {
  764.                         if ((ics->ltp2.data_present = faad_get1bit(ld
  765.                             DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
  766.                         {
  767.                             if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
  768.                             {
  769.                                 return retval;
  770.                             }
  771.                         }
  772.                     }
  773.                 }
  774. #ifdef ERROR_RESILIENCE
  775.                 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
  776.                 {
  777.                     if ((ics->ltp.data_present = faad_get1bit(ld
  778.                         DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
  779.                     {
  780.                         ltp_data(hDecoder, ics, &(ics->ltp), ld);
  781.                     }
  782.                 }
  783. #endif
  784.             }
  785. #endif
  786.         }
  787.     }
  788.     return retval;
  789. }
  790. /* Table 4.4.7 */
  791. static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
  792. {
  793.     uint8_t i;
  794.     pul->number_pulse = (uint8_t)faad_getbits(ld, 2
  795.         DEBUGVAR(1,56,"pulse_data(): number_pulse"));
  796.     pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
  797.         DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
  798.     /* check the range of pulse_start_sfb */
  799.     if (pul->pulse_start_sfb > ics->num_swb)
  800.         return 16;
  801.     for (i = 0; i < pul->number_pulse+1; i++)
  802.     {
  803.         pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
  804.             DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
  805. #if 0
  806.         printf("%dn", pul->pulse_offset[i]);
  807. #endif
  808.         pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
  809.             DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
  810. #if 0
  811.         printf("%dn", pul->pulse_amp[i]);
  812. #endif
  813.     }
  814.     return 0;
  815. }
  816. #ifdef COUPLING_DEC
  817. /* Table 4.4.8: Currently just for skipping the bits... */
  818. static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld)
  819. {
  820.     uint8_t c, result = 0;
  821.     uint8_t ind_sw_cce_flag = 0;
  822.     uint8_t num_gain_element_lists = 0;
  823.     uint8_t num_coupled_elements = 0;
  824.     element el_empty = {0};
  825.     ic_stream ics_empty = {0};
  826.     int16_t sh_data[1024];
  827.     c = faad_getbits(ld, LEN_TAG
  828.         DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
  829.     ind_sw_cce_flag = faad_get1bit(ld
  830.         DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
  831.     num_coupled_elements = faad_getbits(ld, 3
  832.         DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
  833.     for (c = 0; c < num_coupled_elements + 1; c++)
  834.     {
  835.         uint8_t cc_target_is_cpe, cc_target_tag_select;
  836.         num_gain_element_lists++;
  837.         cc_target_is_cpe = faad_get1bit(ld
  838.             DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
  839.         cc_target_tag_select = faad_getbits(ld, 4
  840.             DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
  841.         if (cc_target_is_cpe)
  842.         {
  843.             uint8_t cc_l = faad_get1bit(ld
  844.                 DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
  845.             uint8_t cc_r = faad_get1bit(ld
  846.                 DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
  847.             if (cc_l && cc_r)
  848.                 num_gain_element_lists++;
  849.         }
  850.     }
  851.     faad_get1bit(ld
  852.         DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
  853.     faad_get1bit(ld
  854.         DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
  855.     faad_getbits(ld, 2
  856.         DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
  857.     if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
  858.         0, sh_data)) > 0)
  859.     {
  860.         return result;
  861.     }
  862.     /* IS not allowed in single channel */
  863.     if (ics->is_used)
  864.         return 32;
  865.     for (c = 1; c < num_gain_element_lists; c++)
  866.     {
  867.         uint8_t cge;
  868.         if (ind_sw_cce_flag)
  869.         {
  870.             cge = 1;
  871.         } else {
  872.             cge = faad_get1bit(ld
  873.                 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
  874.         }
  875.         if (cge)
  876.         {
  877.             huffman_scale_factor(ld);
  878.         } else {
  879.             uint8_t g, sfb;
  880.             for (g = 0; g < ics_empty.num_window_groups; g++)
  881.             {
  882.                 for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
  883.                 {
  884.                     if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
  885.                         huffman_scale_factor(ld);
  886.                 }
  887.             }
  888.         }
  889.     }
  890.     return 0;
  891. }
  892. #endif
  893. /* Table 4.4.10 */
  894. static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld)
  895. {
  896.     uint8_t byte_aligned;
  897.     uint16_t i, count;
  898.     /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
  899.         DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
  900.     byte_aligned = faad_get1bit(ld
  901.         DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
  902.     count = (uint16_t)faad_getbits(ld, 8
  903.         DEBUGVAR(1,62,"data_stream_element(): count"));
  904.     if (count == 255)
  905.     {
  906.         count += (uint16_t)faad_getbits(ld, 8
  907.             DEBUGVAR(1,63,"data_stream_element(): extra count"));
  908.     }
  909.     if (byte_aligned)
  910.         faad_byte_align(ld);
  911.     for (i = 0; i < count; i++)
  912.     {
  913.         faad_getbits(ld, LEN_BYTE
  914.             DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
  915.     }
  916.     return count;
  917. }
  918. /* Table 4.4.11 */
  919. static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
  920. #ifdef SBR_DEC
  921.                             ,uint8_t sbr_ele
  922. #endif
  923.                             )
  924. {
  925.     uint16_t count;
  926. #ifdef SBR_DEC
  927.     uint8_t bs_extension_type;
  928. #endif
  929.     count = (uint16_t)faad_getbits(ld, 4
  930.         DEBUGVAR(1,65,"fill_element(): count"));
  931.     if (count == 15)
  932.     {
  933.         count += (uint16_t)faad_getbits(ld, 8
  934.             DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
  935.     }
  936.     if (count > 0)
  937.     {
  938. #ifdef SBR_DEC
  939.         bs_extension_type = (uint8_t)faad_showbits(ld, 4);
  940.         if ((bs_extension_type == EXT_SBR_DATA) ||
  941.             (bs_extension_type == EXT_SBR_DATA_CRC))
  942.         {
  943.             if (sbr_ele == INVALID_SBR_ELEMENT)
  944.                 return 24;
  945.             if (!hDecoder->sbr[sbr_ele])
  946.             {
  947.                 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
  948.                     hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index),
  949.                     hDecoder->downSampledSBR
  950. #ifdef DRM
  951.                     , 0
  952. #endif
  953.                     );
  954.             }
  955.             hDecoder->sbr_present_flag = 1;
  956.             /* parse the SBR data */
  957.             hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
  958. #if 0
  959.             if (hDecoder->sbr[sbr_ele]->ret > 0)
  960.             {
  961.                 printf("%sn", NeAACDecGetErrorMessage(hDecoder->sbr[sbr_ele]->ret));
  962.             }
  963. #endif
  964. #if (defined(PS_DEC) || defined(DRM_PS))
  965.             if (hDecoder->sbr[sbr_ele]->ps_used)
  966.             {
  967.                 hDecoder->ps_used[sbr_ele] = 1;
  968.                 /* set element independent flag to 1 as well */
  969.                 hDecoder->ps_used_global = 1;
  970.             }
  971. #endif
  972.         } else {
  973. #endif
  974. #ifndef DRM
  975.             while (count > 0)
  976.             {
  977.                 count -= extension_payload(ld, drc, count);
  978.             }
  979. #else
  980.             return 30;
  981. #endif
  982. #ifdef SBR_DEC
  983.         }
  984. #endif
  985.     }
  986.     return 0;
  987. }
  988. /* Table 4.4.12 */
  989. #ifdef SSR_DEC
  990. static void gain_control_data(bitfile *ld, ic_stream *ics)
  991. {
  992.     uint8_t bd, wd, ad;
  993.     ssr_info *ssr = &(ics->ssr);
  994.     ssr->max_band = (uint8_t)faad_getbits(ld, 2
  995.         DEBUGVAR(1,1000,"gain_control_data(): max_band"));
  996.     if (ics->window_sequence == ONLY_LONG_SEQUENCE)
  997.     {
  998.         for (bd = 1; bd <= ssr->max_band; bd++)
  999.         {
  1000.             for (wd = 0; wd < 1; wd++)
  1001.             {
  1002.                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
  1003.                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
  1004.                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
  1005.                 {
  1006.                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1007.                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
  1008.                     ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
  1009.                         DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1010.                 }
  1011.             }
  1012.         }
  1013.     } else if (ics->window_sequence == LONG_START_SEQUENCE) {
  1014.         for (bd = 1; bd <= ssr->max_band; bd++)
  1015.         {
  1016.             for (wd = 0; wd < 2; wd++)
  1017.             {
  1018.                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
  1019.                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
  1020.                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
  1021.                 {
  1022.                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1023.                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
  1024.                     if (wd == 0)
  1025.                     {
  1026.                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1027.                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1028.                     } else {
  1029.                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
  1030.                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1031.                     }
  1032.                 }
  1033.             }
  1034.         }
  1035.     } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
  1036.         for (bd = 1; bd <= ssr->max_band; bd++)
  1037.         {
  1038.             for (wd = 0; wd < 8; wd++)
  1039.             {
  1040.                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
  1041.                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
  1042.                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
  1043.                 {
  1044.                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1045.                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
  1046.                     ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
  1047.                         DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1048.                 }
  1049.             }
  1050.         }
  1051.     } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
  1052.         for (bd = 1; bd <= ssr->max_band; bd++)
  1053.         {
  1054.             for (wd = 0; wd < 2; wd++)
  1055.             {
  1056.                 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
  1057.                     DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
  1058.                 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
  1059.                 {
  1060.                     ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1061.                         DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
  1062.                     if (wd == 0)
  1063.                     {
  1064.                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
  1065.                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1066.                     } else {
  1067.                         ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
  1068.                             DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
  1069.                     }
  1070.                 }
  1071.             }
  1072.         }
  1073.     }
  1074. }
  1075. #endif
  1076. #ifdef DRM
  1077. /* Table 4.4.13 ASME */
  1078. void DRM_aac_scalable_main_element(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
  1079.                                    bitfile *ld, program_config *pce, drc_info *drc)
  1080. {
  1081.     uint8_t retval = 0;
  1082.     uint8_t channels = hDecoder->fr_channels = 0;
  1083.     uint8_t ch;
  1084.     uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
  1085.     element cpe = {0};
  1086.     ic_stream *ics1 = &(cpe.ics1);
  1087.     ic_stream *ics2 = &(cpe.ics2);
  1088.     int16_t *spec_data;
  1089.     ALIGN int16_t spec_data1[1024] = {0};
  1090.     ALIGN int16_t spec_data2[1024] = {0};
  1091.     hDecoder->fr_ch_ele = 0;
  1092.     hInfo->error = DRM_aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
  1093.     if (hInfo->error > 0)
  1094.         return;
  1095.     cpe.common_window = 1;
  1096.     if (this_layer_stereo)
  1097.     {
  1098.         hDecoder->element_id[0] = ID_CPE;
  1099.         if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
  1100.             hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
  1101.     } else {
  1102.         hDecoder->element_id[0] = ID_SCE;
  1103.     }
  1104.     if (this_layer_stereo)
  1105.     {
  1106.         cpe.channel        = 0;
  1107.         cpe.paired_channel = 1;
  1108.     }
  1109.     /* Stereo2 / Mono1 */
  1110.     ics1->tns_data_present = faad_get1bit(ld);
  1111. #if defined(LTP_DEC)
  1112.     ics1->ltp.data_present = faad_get1bit(ld);
  1113. #elif defined (DRM)
  1114.     if(faad_get1bit(ld)) {
  1115.          hInfo->error = 26;
  1116.          return;
  1117.     }
  1118. #else
  1119.     faad_get1bit(ld);
  1120. #endif    
  1121.     hInfo->error = side_info(hDecoder, &cpe, ld, ics1, 1);
  1122.     if (hInfo->error > 0)
  1123.         return;
  1124.     if (this_layer_stereo)
  1125.     {
  1126.         /* Stereo3 */
  1127.         ics2->tns_data_present = faad_get1bit(ld);
  1128. #ifdef LTP_DEC
  1129.         ics1->ltp.data_present =
  1130. #endif
  1131.             faad_get1bit(ld);
  1132.         hInfo->error = side_info(hDecoder, &cpe, ld, ics2, 1);
  1133.         if (hInfo->error > 0)
  1134.             return;
  1135.     }
  1136.     /* Stereo4 / Mono2 */
  1137.     if (ics1->tns_data_present)
  1138.         tns_data(ics1, &(ics1->tns), ld);
  1139.     if (this_layer_stereo)
  1140.     {
  1141.         /* Stereo5 */
  1142.         if (ics2->tns_data_present)
  1143.             tns_data(ics2, &(ics2->tns), ld);
  1144.     }
  1145. #ifdef DRM
  1146.     /* CRC check */
  1147.     if (hDecoder->object_type == DRM_ER_LC)
  1148.     {
  1149.         if ((hInfo->error = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
  1150.             return;
  1151.     }
  1152. #endif
  1153.     /* Stereo6 / Mono3 */
  1154.     /* error resilient spectral data decoding */
  1155.     if ((hInfo->error = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0)
  1156.     {
  1157.         return;
  1158.     }
  1159.     if (this_layer_stereo)
  1160.     {
  1161.         /* Stereo7 */
  1162.         /* error resilient spectral data decoding */
  1163.         if ((hInfo->error = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0)
  1164.         {
  1165.             return;
  1166.         }
  1167.     }
  1168. #ifdef DRM
  1169. #ifdef SBR_DEC
  1170.     /* In case of DRM we need to read the SBR info before channel reconstruction */
  1171.     if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
  1172.     {
  1173.         bitfile ld_sbr = {0};
  1174.         uint32_t i;
  1175.         uint16_t count = 0;
  1176.         uint8_t *revbuffer;
  1177.         uint8_t *prevbufstart;
  1178.         uint8_t *pbufend;
  1179.         /* all forward bitreading should be finished at this point */
  1180.         uint32_t bitsconsumed = faad_get_processed_bits(ld);
  1181.         uint32_t buffer_size = faad_origbitbuffer_size(ld);
  1182.         uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
  1183.         if (bitsconsumed + 8 > buffer_size*8)
  1184.         {
  1185.             hInfo->error = 14;
  1186.             return;
  1187.         }
  1188.         if (!hDecoder->sbr[0])
  1189.         {
  1190.             hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0],
  1191.                 2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1);
  1192.         }
  1193.         /* Reverse bit reading of SBR data in DRM audio frame */
  1194.         revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
  1195.         prevbufstart = revbuffer;
  1196.         pbufend = &buffer[buffer_size - 1];
  1197.         for (i = 0; i < buffer_size; i++)
  1198.             *prevbufstart++ = tabFlipbits[*pbufend--];
  1199.         /* Set SBR data */
  1200.         /* consider 8 bits from AAC-CRC */
  1201.         /* SBR buffer size is original buffer size minus AAC buffer size */
  1202.         count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
  1203.         faad_initbits(&ld_sbr, revbuffer, count);
  1204.         hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
  1205.         hDecoder->sbr[0]->sample_rate *= 2;
  1206.         faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
  1207.         hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
  1208. #if (defined(PS_DEC) || defined(DRM_PS))
  1209.         if (hDecoder->sbr[0]->ps_used)
  1210.         {
  1211.             hDecoder->ps_used[0] = 1;
  1212.             hDecoder->ps_used_global = 1;
  1213.         }
  1214. #endif
  1215.         if (ld_sbr.error)
  1216.         {
  1217.             hDecoder->sbr[0]->ret = 1;
  1218.         }
  1219.         /* check CRC */
  1220.         /* no need to check it if there was already an error */
  1221.         if (hDecoder->sbr[0]->ret == 0)
  1222.             hDecoder->sbr[0]->ret = (uint8_t)faad_check_CRC(&ld_sbr, (uint16_t)faad_get_processed_bits(&ld_sbr) - 8);
  1223.         /* SBR data was corrupted, disable it until the next header */
  1224.         if (hDecoder->sbr[0]->ret != 0)
  1225.         {
  1226.             hDecoder->sbr[0]->header_count = 0;
  1227.         }
  1228.         faad_endbits(&ld_sbr);
  1229.         if (revbuffer)
  1230.             faad_free(revbuffer);
  1231.     }
  1232. #endif
  1233. #endif
  1234.     if (this_layer_stereo)
  1235.     {
  1236.         hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
  1237.         if (hInfo->error > 0)
  1238.             return;
  1239.     } else {
  1240.         hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
  1241.         if (hInfo->error > 0)
  1242.             return;
  1243.     }
  1244.     /* map output channels position to internal data channels */
  1245.     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
  1246.     {
  1247.         /* this might be faulty when pce_set is true */
  1248.         hDecoder->internal_channel[channels] = channels;
  1249.         hDecoder->internal_channel[channels+1] = channels+1;
  1250.     } else {
  1251.         hDecoder->internal_channel[channels] = channels;
  1252.     }
  1253.     hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
  1254.     hDecoder->fr_ch_ele++;
  1255.     return;
  1256. }
  1257. /* Table 4.4.15 */
  1258. static int8_t DRM_aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
  1259.                                            bitfile *ld, uint8_t this_layer_stereo)
  1260. {
  1261.     uint8_t retval = 0;
  1262.     uint8_t ch;
  1263.     ic_stream *ics;
  1264.     uint8_t ics_reserved_bit;
  1265.     ics_reserved_bit = faad_get1bit(ld
  1266.         DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
  1267.     if (ics_reserved_bit != 0)
  1268.         return 32;
  1269.     ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
  1270.         DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
  1271.     ics1->window_shape = faad_get1bit(ld
  1272.         DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
  1273.     if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
  1274.     {
  1275.         ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
  1276.             DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
  1277.         ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
  1278.             DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
  1279.     } else {
  1280.         ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
  1281.             DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
  1282.     }
  1283.     /* get the grouping information */
  1284.     if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
  1285.         return retval;
  1286.     /* should be an error */
  1287.     /* check the range of max_sfb */
  1288.     if (ics1->max_sfb > ics1->num_swb)
  1289.         return 16;
  1290.     if (this_layer_stereo)
  1291.     {
  1292.         ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
  1293.             DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
  1294.         if (ics1->ms_mask_present == 3)
  1295.         {
  1296.             /* bitstream error */
  1297.             return 32;
  1298.         }
  1299.         if (ics1->ms_mask_present == 1)
  1300.         {
  1301.             uint8_t g, sfb;
  1302.             for (g = 0; g < ics1->num_window_groups; g++)
  1303.             {
  1304.                 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
  1305.                 {
  1306.                     ics1->ms_used[g][sfb] = faad_get1bit(ld
  1307.                         DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
  1308.                 }
  1309.             }
  1310.         }
  1311.         memcpy(ics2, ics1, sizeof(ic_stream));
  1312.     } else {
  1313.         ics1->ms_mask_present = 0;
  1314.     }
  1315.     return 0;
  1316. }
  1317. #endif
  1318. static uint8_t side_info(NeAACDecHandle hDecoder, element *ele,
  1319.                          bitfile *ld, ic_stream *ics, uint8_t scal_flag)
  1320. {
  1321.     uint8_t result;
  1322.     ics->global_gain = (uint8_t)faad_getbits(ld, 8
  1323.         DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
  1324.     if (!ele->common_window && !scal_flag)
  1325.     {
  1326.         if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
  1327.             return result;
  1328.     }
  1329.     if ((result = section_data(hDecoder, ics, ld)) > 0)
  1330.         return result;
  1331.     if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
  1332.         return result;
  1333.     if (!scal_flag)
  1334.     {
  1335.         /**
  1336.          **  NOTE: It could be that pulse data is available in scalable AAC too,
  1337.          **        as said in Amendment 1, this could be only the case for ER AAC,
  1338.          **        though. (have to check this out later)
  1339.          **/
  1340.         /* get pulse data */
  1341.         if ((ics->pulse_data_present = faad_get1bit(ld
  1342.             DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
  1343.         {
  1344.             if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
  1345.                 return result;
  1346.         }
  1347.         /* get tns data */
  1348.         if ((ics->tns_data_present = faad_get1bit(ld
  1349.             DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
  1350.         {
  1351. #ifdef ERROR_RESILIENCE
  1352.             if (hDecoder->object_type < ER_OBJECT_START)
  1353. #endif
  1354.                 tns_data(ics, &(ics->tns), ld);
  1355.         }
  1356.         /* get gain control data */
  1357.         if ((ics->gain_control_data_present = faad_get1bit(ld
  1358.             DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
  1359.         {
  1360. #ifdef SSR_DEC
  1361.             if (hDecoder->object_type != SSR)
  1362.                 return 1;
  1363.             else
  1364.                 gain_control_data(ld, ics);
  1365. #else
  1366.             return 1;
  1367. #endif
  1368.         }
  1369.     }
  1370. #ifdef ERROR_RESILIENCE
  1371.     if (hDecoder->aacSpectralDataResilienceFlag)
  1372.     {
  1373.         ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
  1374.             DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
  1375.         if (hDecoder->channelConfiguration == 2)
  1376.         {
  1377.             if (ics->length_of_reordered_spectral_data > 6144)
  1378.                 ics->length_of_reordered_spectral_data = 6144;
  1379.         } else {
  1380.             if (ics->length_of_reordered_spectral_data > 12288)
  1381.                 ics->length_of_reordered_spectral_data = 12288;
  1382.         }
  1383.         ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
  1384.             DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
  1385.         if (ics->length_of_longest_codeword >= 49)
  1386.             ics->length_of_longest_codeword = 49;
  1387.     }
  1388.     /* RVLC spectral data is put here */
  1389.     if (hDecoder->aacScalefactorDataResilienceFlag)
  1390.     {
  1391.         if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
  1392.             return result;
  1393.     }
  1394. #endif
  1395.     return 0;
  1396. }
  1397. /* Table 4.4.24 */
  1398. static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
  1399.                                          bitfile *ld, ic_stream *ics, uint8_t scal_flag,
  1400.                                          int16_t *spec_data)
  1401. {
  1402.     uint8_t result;
  1403.     result = side_info(hDecoder, ele, ld, ics, scal_flag);
  1404.     if (result > 0)
  1405.         return result;
  1406.     if (hDecoder->object_type >= ER_OBJECT_START) 
  1407.     {
  1408.         if (ics->tns_data_present)
  1409.             tns_data(ics, &(ics->tns), ld);
  1410.     }
  1411. #ifdef DRM
  1412.     /* CRC check */
  1413.     if (hDecoder->object_type == DRM_ER_LC)
  1414.     {
  1415.         if ((result = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
  1416.             return result;
  1417.     }
  1418. #endif
  1419. #ifdef ERROR_RESILIENCE
  1420.     if (hDecoder->aacSpectralDataResilienceFlag)
  1421.     {
  1422.         /* error resilient spectral data decoding */
  1423.         if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
  1424.         {
  1425.             return result;
  1426.         }
  1427.     } else {
  1428. #endif
  1429.         /* decode the spectral data */
  1430.         if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
  1431.         {
  1432.             return result;
  1433.         }
  1434. #ifdef ERROR_RESILIENCE
  1435.     }
  1436. #endif
  1437.     /* pulse coding reconstruction */
  1438.     if (ics->pulse_data_present)
  1439.     {
  1440.         if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
  1441.         {
  1442.             if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
  1443.                 return result;
  1444.         } else {
  1445.             return 2; /* pulse coding not allowed for short blocks */
  1446.         }
  1447.     }
  1448.     return 0;
  1449. }
  1450. /* Table 4.4.25 */
  1451. static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
  1452. {
  1453.     uint8_t g;
  1454.     uint8_t sect_esc_val, sect_bits;
  1455.     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
  1456.         sect_bits = 3;
  1457.     else
  1458.         sect_bits = 5;
  1459.     sect_esc_val = (1<<sect_bits) - 1;
  1460. #if 0
  1461.     printf("ntotal sfb %dn", ics->max_sfb);
  1462.     printf("   sect    top     cbn");
  1463. #endif
  1464.     for (g = 0; g < ics->num_window_groups; g++)
  1465.     {
  1466.         uint8_t k = 0;
  1467.         uint8_t i = 0;
  1468.         while (k < ics->max_sfb)
  1469.         {
  1470. #ifdef ERROR_RESILIENCE
  1471.             uint8_t vcb11 = 0;
  1472. #endif
  1473.             uint8_t sfb;
  1474.             uint8_t sect_len_incr;
  1475.             uint16_t sect_len = 0;
  1476.             uint8_t sect_cb_bits = 4;
  1477.             /* if "faad_getbits" detects error and returns "0", "k" is never
  1478.                incremented and we cannot leave the while loop */
  1479.             if ((ld->error != 0) || (ld->no_more_reading))
  1480.                 return 14;
  1481. #ifdef ERROR_RESILIENCE
  1482.             if (hDecoder->aacSectionDataResilienceFlag)
  1483.                 sect_cb_bits = 5;
  1484. #endif
  1485.             ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
  1486.                 DEBUGVAR(1,71,"section_data(): sect_cb"));
  1487.             if (ics->sect_cb[g][i] == 12)
  1488.                 return 32;
  1489. #if 0
  1490.             printf("%dn", ics->sect_cb[g][i]);
  1491. #endif
  1492. #ifndef DRM
  1493.             if (ics->sect_cb[g][i] == NOISE_HCB)
  1494.                 ics->noise_used = 1;
  1495. #else
  1496.             /* PNS not allowed in DRM */
  1497.             if (ics->sect_cb[g][i] == NOISE_HCB)
  1498.                 return 29;
  1499. #endif
  1500.             if (ics->sect_cb[g][i] == INTENSITY_HCB2 || ics->sect_cb[g][i] == INTENSITY_HCB)
  1501.                 ics->is_used = 1;
  1502. #ifdef ERROR_RESILIENCE
  1503.             if (hDecoder->aacSectionDataResilienceFlag)
  1504.             {
  1505.                 if ((ics->sect_cb[g][i] == 11) ||
  1506.                     ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
  1507.                 {
  1508.                     vcb11 = 1;
  1509.                 }
  1510.             }
  1511.             if (vcb11)
  1512.             {
  1513.                 sect_len_incr = 1;
  1514.             } else {
  1515. #endif
  1516.                 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
  1517.                     DEBUGVAR(1,72,"section_data(): sect_len_incr"));
  1518. #ifdef ERROR_RESILIENCE
  1519.             }
  1520. #endif
  1521.             while ((sect_len_incr == sect_esc_val) /* &&
  1522.                 (k+sect_len < ics->max_sfb)*/)
  1523.             {
  1524.                 sect_len += sect_len_incr;
  1525.                 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
  1526.                     DEBUGVAR(1,72,"section_data(): sect_len_incr"));
  1527.             }
  1528.             sect_len += sect_len_incr;
  1529.             ics->sect_start[g][i] = k;
  1530.             ics->sect_end[g][i] = k + sect_len;
  1531. #if 0
  1532.             printf("%dn", ics->sect_start[g][i]);
  1533. #endif
  1534. #if 0
  1535.             printf("%dn", ics->sect_end[g][i]);
  1536. #endif
  1537.             if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
  1538.             {
  1539.                 if (k + sect_len >= 8*15)
  1540.                     return 15;
  1541.                 if (i >= 8*15)
  1542.                     return 15;
  1543.             } else {
  1544.                 if (k + sect_len >= MAX_SFB)
  1545.                     return 15;
  1546.                 if (i >= MAX_SFB)
  1547.                     return 15;
  1548.             }
  1549.             for (sfb = k; sfb < k + sect_len; sfb++)
  1550.             {
  1551.                 ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
  1552. #if 0
  1553.                 printf("%dn", ics->sfb_cb[g][sfb]);
  1554. #endif
  1555.             }
  1556. #if 0
  1557.             printf(" %6d %6d %6dn",
  1558.                 i,
  1559.                 ics->sect_end[g][i],
  1560.                 ics->sect_cb[g][i]);
  1561. #endif
  1562.             k += sect_len;
  1563.             i++;
  1564.         }
  1565.         ics->num_sec[g] = i;
  1566.         /* the sum of all sect_len_incr elements for a given window
  1567.          * group shall equal max_sfb */
  1568.         if (k != ics->max_sfb)
  1569.         {
  1570.             return 32;
  1571.         }
  1572. #if 0
  1573.         printf("%dn", ics->num_sec[g]);
  1574. #endif
  1575.     }
  1576. #if 0
  1577.     printf("n");
  1578. #endif
  1579.     return 0;
  1580. }
  1581. /*
  1582.  *  decode_scale_factors()
  1583.  *   decodes the scalefactors from the bitstream
  1584.  */
  1585. /*
  1586.  * All scalefactors (and also the stereo positions and pns energies) are
  1587.  * transmitted using Huffman coded DPCM relative to the previous active
  1588.  * scalefactor (respectively previous stereo position or previous pns energy,
  1589.  * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
  1590.  * differentially coded relative to the global gain.
  1591.  */
  1592. static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
  1593. {
  1594.     uint8_t g, sfb;
  1595.     int16_t t;
  1596.     int8_t noise_pcm_flag = 1;
  1597.     int16_t scale_factor = ics->global_gain;
  1598.     int16_t is_position = 0;
  1599.     int16_t noise_energy = ics->global_gain - 90;
  1600.     for (g = 0; g < ics->num_window_groups; g++)
  1601.     {
  1602.         for (sfb = 0; sfb < ics->max_sfb; sfb++)
  1603.         {
  1604.             switch (ics->sfb_cb[g][sfb])
  1605.             {
  1606.             case ZERO_HCB: /* zero book */
  1607.                 ics->scale_factors[g][sfb] = 0;
  1608. //#define SF_PRINT
  1609. #ifdef SF_PRINT
  1610.                 printf("%dn", ics->scale_factors[g][sfb]);
  1611. #endif
  1612.                 break;
  1613.             case INTENSITY_HCB: /* intensity books */
  1614.             case INTENSITY_HCB2:
  1615.                 /* decode intensity position */
  1616.                 t = huffman_scale_factor(ld);
  1617.                 is_position += (t - 60);
  1618.                 ics->scale_factors[g][sfb] = is_position;
  1619. #ifdef SF_PRINT
  1620.                 printf("%dn", ics->scale_factors[g][sfb]);
  1621. #endif
  1622.                 break;
  1623.             case NOISE_HCB: /* noise books */
  1624. #ifndef DRM
  1625.                 /* decode noise energy */
  1626.                 if (noise_pcm_flag)
  1627.                 {
  1628.                     noise_pcm_flag = 0;
  1629.                     t = (int16_t)faad_getbits(ld, 9
  1630.                         DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
  1631.                 } else {
  1632.                     t = huffman_scale_factor(ld);
  1633.                     t -= 60;
  1634.                 }
  1635.                 noise_energy += t;
  1636.                 ics->scale_factors[g][sfb] = noise_energy;
  1637. #ifdef SF_PRINT
  1638.                 printf("%dn", ics->scale_factors[g][sfb]);
  1639. #endif
  1640. #else
  1641.                 /* PNS not allowed in DRM */
  1642.                 return 29;
  1643. #endif
  1644.                 break;
  1645.             default: /* spectral books */
  1646.                 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
  1647.                 ics->scale_factors[g][sfb] = 0;
  1648.                 /* decode scale factor */
  1649.                 t = huffman_scale_factor(ld);
  1650.                 scale_factor += (t - 60);
  1651.                 if (scale_factor < 0 || scale_factor > 255)
  1652.                     return 4;
  1653.                 ics->scale_factors[g][sfb] = scale_factor;
  1654. #ifdef SF_PRINT
  1655.                 printf("%dn", ics->scale_factors[g][sfb]);
  1656. #endif
  1657.                 break;
  1658.             }
  1659.         }
  1660.     }
  1661.     return 0;
  1662. }
  1663. /* Table 4.4.26 */
  1664. static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
  1665. {
  1666.     uint8_t ret = 0;
  1667. #ifdef PROFILE
  1668.     int64_t count = faad_get_ts();
  1669. #endif
  1670. #ifdef ERROR_RESILIENCE
  1671.     if (!hDecoder->aacScalefactorDataResilienceFlag)
  1672.     {
  1673. #endif
  1674.         ret = decode_scale_factors(ics, ld);
  1675. #ifdef ERROR_RESILIENCE
  1676.     } else {
  1677.         /* In ER AAC the parameters for RVLC are seperated from the actual
  1678.            data that holds the scale_factors.
  1679.            Strangely enough, 2 parameters for HCR are put inbetween them.
  1680.         */
  1681.         ret = rvlc_scale_factor_data(ics, ld);
  1682.     }
  1683. #endif
  1684. #ifdef PROFILE
  1685.     count = faad_get_ts() - count;
  1686.     hDecoder->scalefac_cycles += count;
  1687. #endif
  1688.     return ret;
  1689. }
  1690. /* Table 4.4.27 */
  1691. static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
  1692. {
  1693.     uint8_t w, filt, i, start_coef_bits, coef_bits;
  1694.     uint8_t n_filt_bits = 2;
  1695.     uint8_t length_bits = 6;
  1696.     uint8_t order_bits = 5;
  1697.     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
  1698.     {
  1699.         n_filt_bits = 1;
  1700.         length_bits = 4;
  1701.         order_bits = 3;
  1702.     }
  1703.     for (w = 0; w < ics->num_windows; w++)
  1704.     {
  1705.         tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
  1706.             DEBUGVAR(1,74,"tns_data(): n_filt"));
  1707. #if 0
  1708.         printf("%dn", tns->n_filt[w]);
  1709. #endif
  1710.         if (tns->n_filt[w])
  1711.         {
  1712.             if ((tns->coef_res[w] = faad_get1bit(ld
  1713.                 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
  1714.             {
  1715.                 start_coef_bits = 4;
  1716.             } else {
  1717.                 start_coef_bits = 3;
  1718.             }
  1719. #if 0
  1720.             printf("%dn", tns->coef_res[w]);
  1721. #endif
  1722.         }
  1723.         for (filt = 0; filt < tns->n_filt[w]; filt++)
  1724.         {
  1725.             tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
  1726.                 DEBUGVAR(1,76,"tns_data(): length"));
  1727. #if 0
  1728.             printf("%dn", tns->length[w][filt]);
  1729. #endif
  1730.             tns->order[w][filt]  = (uint8_t)faad_getbits(ld, order_bits
  1731.                 DEBUGVAR(1,77,"tns_data(): order"));
  1732. #if 0
  1733.             printf("%dn", tns->order[w][filt]);
  1734. #endif
  1735.             if (tns->order[w][filt])
  1736.             {
  1737.                 tns->direction[w][filt] = faad_get1bit(ld
  1738.                     DEBUGVAR(1,78,"tns_data(): direction"));
  1739. #if 0
  1740.                 printf("%dn", tns->direction[w][filt]);
  1741. #endif
  1742.                 tns->coef_compress[w][filt] = faad_get1bit(ld
  1743.                     DEBUGVAR(1,79,"tns_data(): coef_compress"));
  1744. #if 0
  1745.                 printf("%dn", tns->coef_compress[w][filt]);
  1746. #endif
  1747.                 coef_bits = start_coef_bits - tns->coef_compress[w][filt];
  1748.                 for (i = 0; i < tns->order[w][filt]; i++)
  1749.                 {
  1750.                     tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
  1751.                         DEBUGVAR(1,80,"tns_data(): coef"));
  1752. #if 0
  1753.                     printf("%dn", tns->coef[w][filt][i]);
  1754. #endif
  1755.                 }
  1756.             }
  1757.         }
  1758.     }
  1759. }
  1760. #ifdef LTP_DEC
  1761. /* Table 4.4.28 */
  1762. static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
  1763. {
  1764.     uint8_t sfb, w;
  1765.     ltp->lag = 0;
  1766. #ifdef LD_DEC
  1767.     if (hDecoder->object_type == LD)
  1768.     {
  1769.         ltp->lag_update = (uint8_t)faad_getbits(ld, 1
  1770.             DEBUGVAR(1,142,"ltp_data(): lag_update"));
  1771.         if (ltp->lag_update)
  1772.         {
  1773.             ltp->lag = (uint16_t)faad_getbits(ld, 10
  1774.                 DEBUGVAR(1,81,"ltp_data(): lag"));
  1775.         }
  1776.     } else {
  1777. #endif
  1778.         ltp->lag = (uint16_t)faad_getbits(ld, 11
  1779.             DEBUGVAR(1,81,"ltp_data(): lag"));
  1780. #ifdef LD_DEC
  1781.     }
  1782. #endif
  1783.     /* Check length of lag */
  1784.     if (ltp->lag > (hDecoder->frameLength << 1))
  1785.         return 18;
  1786.     ltp->coef = (uint8_t)faad_getbits(ld, 3
  1787.         DEBUGVAR(1,82,"ltp_data(): coef"));
  1788.     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
  1789.     {
  1790.         for (w = 0; w < ics->num_windows; w++)
  1791.         {
  1792.             if ((ltp->short_used[w] = faad_get1bit(ld
  1793.                 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
  1794.             {
  1795.                 ltp->short_lag_present[w] = faad_get1bit(ld
  1796.                     DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
  1797.                 if (ltp->short_lag_present[w])
  1798.                 {
  1799.                     ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
  1800.                         DEBUGVAR(1,85,"ltp_data(): short_lag"));
  1801.                 }
  1802.             }
  1803.         }
  1804.     } else {
  1805.         ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
  1806.         for (sfb = 0; sfb < ltp->last_band; sfb++)
  1807.         {
  1808.             ltp->long_used[sfb] = faad_get1bit(ld
  1809.                 DEBUGVAR(1,86,"ltp_data(): long_used"));
  1810.         }
  1811.     }
  1812.     return 0;
  1813. }
  1814. #endif
  1815. /* Table 4.4.29 */
  1816. static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
  1817.                              int16_t *spectral_data)
  1818. {
  1819.     int8_t i;
  1820.     uint8_t g;
  1821.     uint16_t inc, k, p = 0;
  1822.     uint8_t groups = 0;
  1823.     uint8_t sect_cb;
  1824.     uint8_t result;
  1825.     uint16_t nshort = hDecoder->frameLength/8;
  1826. #ifdef PROFILE
  1827.     int64_t count = faad_get_ts();
  1828. #endif
  1829.     for(g = 0; g < ics->num_window_groups; g++)
  1830.     {
  1831.         p = groups*nshort;
  1832.         for (i = 0; i < ics->num_sec[g]; i++)
  1833.         {
  1834.             sect_cb = ics->sect_cb[g][i];
  1835.             inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
  1836.             switch (sect_cb)
  1837.             {
  1838.             case ZERO_HCB:
  1839.             case NOISE_HCB:
  1840.             case INTENSITY_HCB:
  1841.             case INTENSITY_HCB2:
  1842. //#define SD_PRINT
  1843. #ifdef SD_PRINT
  1844.                 {
  1845.                     int j;
  1846.                     for (j = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; j < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; j++)
  1847.                     {
  1848.                         printf("%dn", 0);
  1849.                     }
  1850.                 }
  1851. #endif
  1852. //#define SFBO_PRINT
  1853. #ifdef SFBO_PRINT
  1854.                 printf("%dn", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
  1855. #endif
  1856.                 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
  1857.                     ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
  1858.                 break;
  1859.             default:
  1860. #ifdef SFBO_PRINT
  1861.                 printf("%dn", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
  1862. #endif
  1863.                 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
  1864.                      k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
  1865.                 {
  1866.                     if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
  1867.                         return result;
  1868. #ifdef SD_PRINT
  1869.                     {
  1870.                         int j;
  1871.                         for (j = p; j < p+inc; j++)
  1872.                         {
  1873.                             printf("%dn", spectral_data[j]);
  1874.                         }
  1875.                     }
  1876. #endif
  1877.                     p += inc;
  1878.                 }
  1879.                 break;
  1880.             }
  1881.         }
  1882.         groups += ics->window_group_length[g];
  1883.     }
  1884. #ifdef PROFILE
  1885.     count = faad_get_ts() - count;
  1886.     hDecoder->spectral_cycles += count;
  1887. #endif
  1888.     return 0;
  1889. }
  1890. /* Table 4.4.30 */
  1891. static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
  1892. {
  1893.     uint16_t i, n, dataElementLength;
  1894.     uint8_t dataElementLengthPart;
  1895.     uint8_t align = 4, data_element_version, loopCounter;
  1896.     uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
  1897.         DEBUGVAR(1,87,"extension_payload(): extension_type"));
  1898.     switch (extension_type)
  1899.     {
  1900.     case EXT_DYNAMIC_RANGE:
  1901.         drc->present = 1;
  1902.         n = dynamic_range_info(ld, drc);
  1903.         return n;
  1904.     case EXT_FILL_DATA:
  1905.         /* fill_nibble = */ faad_getbits(ld, 4
  1906.             DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be