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

多媒体编程

开发平台:

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: decoder.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
  31. **/
  32. #include "common.h"
  33. #include "structs.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "decoder.h"
  37. #include "mp4.h"
  38. #include "syntax.h"
  39. #include "error.h"
  40. #include "output.h"
  41. #include "filtbank.h"
  42. #include "drc.h"
  43. #ifdef SBR_DEC
  44. #include "sbr_dec.h"
  45. #include "sbr_syntax.h"
  46. #endif
  47. #ifdef SSR_DEC
  48. #include "ssr.h"
  49. #endif
  50. #ifdef ANALYSIS
  51. uint16_t dbg_count;
  52. #endif
  53. /* static function declarations */
  54. static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
  55.                               uint8_t *buffer, uint32_t buffer_size,
  56.                               void **sample_buffer, uint32_t sample_buffer_size);
  57. static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
  58. char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
  59. {
  60.     if (errcode >= NUM_ERROR_MESSAGES)
  61.         return NULL;
  62.     return err_msg[errcode];
  63. }
  64. uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
  65. {
  66.     uint32_t cap = 0;
  67.     /* can't do without it */
  68.     cap += LC_DEC_CAP;
  69. #ifdef MAIN_DEC
  70.     cap += MAIN_DEC_CAP;
  71. #endif
  72. #ifdef LTP_DEC
  73.     cap += LTP_DEC_CAP;
  74. #endif
  75. #ifdef LD_DEC
  76.     cap += LD_DEC_CAP;
  77. #endif
  78. #ifdef ERROR_RESILIENCE
  79.     cap += ERROR_RESILIENCE_CAP;
  80. #endif
  81. #ifdef FIXED_POINT
  82.     cap += FIXED_POINT_CAP;
  83. #endif
  84.     return cap;
  85. }
  86. NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
  87. {
  88.     uint8_t i;
  89.     NeAACDecHandle hDecoder = NULL;
  90.     if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
  91.         return NULL;
  92.     memset(hDecoder, 0, sizeof(NeAACDecStruct));
  93.     hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
  94.     hDecoder->config.defObjectType = MAIN;
  95.     hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
  96.     hDecoder->config.downMatrix = 0;
  97.     hDecoder->adts_header_present = 0;
  98.     hDecoder->adif_header_present = 0;
  99. #ifdef ERROR_RESILIENCE
  100.     hDecoder->aacSectionDataResilienceFlag = 0;
  101.     hDecoder->aacScalefactorDataResilienceFlag = 0;
  102.     hDecoder->aacSpectralDataResilienceFlag = 0;
  103. #endif
  104.     hDecoder->frameLength = 1024;
  105.     hDecoder->frame = 0;
  106.     hDecoder->sample_buffer = NULL;
  107.     for (i = 0; i < MAX_CHANNELS; i++)
  108.     {
  109.         hDecoder->window_shape_prev[i] = 0;
  110.         hDecoder->time_out[i] = NULL;
  111.         hDecoder->fb_intermed[i] = NULL;
  112. #ifdef SSR_DEC
  113.         hDecoder->ssr_overlap[i] = NULL;
  114.         hDecoder->prev_fmd[i] = NULL;
  115. #endif
  116. #ifdef MAIN_DEC
  117.         hDecoder->pred_stat[i] = NULL;
  118. #endif
  119. #ifdef LTP_DEC
  120.         hDecoder->ltp_lag[i] = 0;
  121.         hDecoder->lt_pred_stat[i] = NULL;
  122. #endif
  123.     }
  124. #ifdef SBR_DEC
  125.     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
  126.     {
  127.         hDecoder->sbr[i] = NULL;
  128.     }
  129. #endif
  130.     hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
  131.     return hDecoder;
  132. }
  133. NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
  134. {
  135.     if (hDecoder)
  136.     {
  137.         NeAACDecConfigurationPtr config = &(hDecoder->config);
  138.         return config;
  139.     }
  140.     return NULL;
  141. }
  142. uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
  143.                                              NeAACDecConfigurationPtr config)
  144. {
  145.     if (hDecoder && config)
  146.     {
  147.         /* check if we can decode this object type */
  148.         if (can_decode_ot(config->defObjectType) < 0)
  149.             return 0;
  150.         hDecoder->config.defObjectType = config->defObjectType;
  151.         /* samplerate: anything but 0 should be possible */
  152.         if (config->defSampleRate == 0)
  153.             return 0;
  154.         hDecoder->config.defSampleRate = config->defSampleRate;
  155.         /* check output format */
  156. #ifdef FIXED_POINT
  157.         if ((config->outputFormat < 1) || (config->outputFormat > 4))
  158.             return 0;
  159. #else
  160.         if ((config->outputFormat < 1) || (config->outputFormat > 5))
  161.             return 0;
  162. #endif
  163.         hDecoder->config.outputFormat = config->outputFormat;
  164.         if (config->downMatrix > 1)
  165.             return 0;
  166.         hDecoder->config.downMatrix = config->downMatrix;
  167.         /* OK */
  168.         return 1;
  169.     }
  170.     return 0;
  171. }
  172. int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
  173.                                  uint32_t buffer_size,
  174.                                  uint32_t *samplerate, uint8_t *channels)
  175. {
  176.     uint32_t bits = 0;
  177.     bitfile ld;
  178.     adif_header adif;
  179.     adts_header adts;
  180.     if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
  181.         return -1;
  182.     hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
  183.     hDecoder->object_type = hDecoder->config.defObjectType;
  184.     *samplerate = get_sample_rate(hDecoder->sf_index);
  185.     *channels = 1;
  186.     if (buffer != NULL)
  187.     {
  188.         faad_initbits(&ld, buffer, buffer_size);
  189.         /* Check if an ADIF header is present */
  190.         if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
  191.             (buffer[2] == 'I') && (buffer[3] == 'F'))
  192.         {
  193.             hDecoder->adif_header_present = 1;
  194.             get_adif_header(&adif, &ld);
  195.             faad_byte_align(&ld);
  196.             hDecoder->sf_index = adif.pce[0].sf_index;
  197.             hDecoder->object_type = adif.pce[0].object_type + 1;
  198.             *samplerate = get_sample_rate(hDecoder->sf_index);
  199.             *channels = adif.pce[0].channels;
  200.             memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
  201.             hDecoder->pce_set = 1;
  202.             bits = bit2byte(faad_get_processed_bits(&ld));
  203.         /* Check if an ADTS header is present */
  204.         } else if (faad_showbits(&ld, 12) == 0xfff) {
  205.             hDecoder->adts_header_present = 1;
  206.             adts.old_format = hDecoder->config.useOldADTSFormat;
  207.             adts_frame(&adts, &ld);
  208.             hDecoder->sf_index = adts.sf_index;
  209.             hDecoder->object_type = adts.profile + 1;
  210.             *samplerate = get_sample_rate(hDecoder->sf_index);
  211.             *channels = (adts.channel_configuration > 6) ?
  212.                 2 : adts.channel_configuration;
  213.         }
  214.         if (ld.error)
  215.         {
  216.             faad_endbits(&ld);
  217.             return -1;
  218.         }
  219.         faad_endbits(&ld);
  220.     }
  221.     hDecoder->channelConfiguration = *channels;
  222. #if (defined(PS_DEC) || defined(DRM_PS))
  223.     /* check if we have a mono file */
  224.     if (*channels == 1)
  225.     {
  226.         /* upMatrix to 2 channels for implicit signalling of PS */
  227.         *channels = 2;
  228.     }
  229. #endif
  230. #ifdef SBR_DEC
  231.     /* implicit signalling */
  232.     if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
  233.     {
  234.         *samplerate *= 2;
  235.         hDecoder->forceUpSampling = 1;
  236.     } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
  237.         hDecoder->downSampledSBR = 1;
  238.     }
  239. #endif
  240.     /* must be done before frameLength is divided by 2 for LD */
  241. #ifdef SSR_DEC
  242.     if (hDecoder->object_type == SSR)
  243.         hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
  244.     else
  245. #endif
  246.         hDecoder->fb = filter_bank_init(hDecoder->frameLength);
  247. #ifdef LD_DEC
  248.     if (hDecoder->object_type == LD)
  249.         hDecoder->frameLength >>= 1;
  250. #endif
  251.     if (can_decode_ot(hDecoder->object_type) < 0)
  252.         return -1;
  253.     return bits;
  254. }
  255. /* Init the library using a DecoderSpecificInfo */
  256. int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
  257.                                  uint32_t SizeOfDecoderSpecificInfo,
  258.                                  uint32_t *samplerate, uint8_t *channels)
  259. {
  260.     int8_t rc;
  261.     mp4AudioSpecificConfig mp4ASC;
  262.     if((hDecoder == NULL)
  263.         || (pBuffer == NULL)
  264.         || (SizeOfDecoderSpecificInfo < 2)
  265.         || (samplerate == NULL)
  266.         || (channels == NULL))
  267.     {
  268.         return -1;
  269.     }
  270.     hDecoder->adif_header_present = 0;
  271.     hDecoder->adts_header_present = 0;
  272.     /* decode the audio specific config */
  273.     rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
  274.         &(hDecoder->pce));
  275.     /* copy the relevant info to the decoder handle */
  276.     *samplerate = mp4ASC.samplingFrequency;
  277.     if (mp4ASC.channelsConfiguration)
  278.     {
  279.         *channels = mp4ASC.channelsConfiguration;
  280.     } else {
  281.         *channels = hDecoder->pce.channels;
  282.         hDecoder->pce_set = 1;
  283.     }
  284. #if (defined(PS_DEC) || defined(DRM_PS))
  285.     /* check if we have a mono file */
  286.     if (*channels == 1)
  287.     {
  288.         /* upMatrix to 2 channels for implicit signalling of PS */
  289.         *channels = 2;
  290.     }
  291. #endif
  292.     hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
  293.     hDecoder->object_type = mp4ASC.objectTypeIndex;
  294. #ifdef ERROR_RESILIENCE
  295.     hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
  296.     hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
  297.     hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
  298. #endif
  299. #ifdef SBR_DEC
  300.     hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
  301.     hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
  302.     if (hDecoder->config.dontUpSampleImplicitSBR == 0)
  303.         hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
  304.     else
  305.         hDecoder->forceUpSampling = 0;
  306.     /* AAC core decoder samplerate is 2 times as low */
  307.     if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
  308.     {
  309.         hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
  310.     }
  311. #endif
  312.     if (rc != 0)
  313.     {
  314.         return rc;
  315.     }
  316.     hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
  317.     if (mp4ASC.frameLengthFlag)
  318. #ifdef ALLOW_SMALL_FRAMELENGTH
  319.         hDecoder->frameLength = 960;
  320. #else
  321.         return -1;
  322. #endif
  323.     /* must be done before frameLength is divided by 2 for LD */
  324. #ifdef SSR_DEC
  325.     if (hDecoder->object_type == SSR)
  326.         hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
  327.     else
  328. #endif
  329.         hDecoder->fb = filter_bank_init(hDecoder->frameLength);
  330. #ifdef LD_DEC
  331.     if (hDecoder->object_type == LD)
  332.         hDecoder->frameLength >>= 1;
  333. #endif
  334.     return 0;
  335. }
  336. #ifdef DRM
  337. int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
  338.                                    uint8_t channels)
  339. {
  340.     if (hDecoder == NULL)
  341.         return 1; /* error */
  342.     NeAACDecClose(*hDecoder);
  343.     *hDecoder = NeAACDecOpen();
  344.     /* Special object type defined for DRM */
  345.     (*hDecoder)->config.defObjectType = DRM_ER_LC;
  346.     (*hDecoder)->config.defSampleRate = samplerate;
  347. #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
  348.     (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
  349.     (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
  350.     (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
  351. #endif
  352.     (*hDecoder)->frameLength = 960;
  353.     (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
  354.     (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
  355.     if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
  356.         (*hDecoder)->channelConfiguration = 2;
  357.     else
  358.         (*hDecoder)->channelConfiguration = 1;
  359. #ifdef SBR_DEC
  360.     if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
  361.         (*hDecoder)->sbr_present_flag = 0;
  362.     else
  363.         (*hDecoder)->sbr_present_flag = 1;    
  364. #endif        
  365.     (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
  366.     return 0;
  367. }
  368. #endif
  369. void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
  370. {
  371.     uint8_t i;
  372.     if (hDecoder == NULL)
  373.         return;
  374. #ifdef PROFILE
  375.     printf("AAC decoder total:  %I64d cyclesn", hDecoder->cycles);
  376.     printf("requant:            %I64d cyclesn", hDecoder->requant_cycles);
  377.     printf("spectral_data:      %I64d cyclesn", hDecoder->spectral_cycles);
  378.     printf("scalefactors:       %I64d cyclesn", hDecoder->scalefac_cycles);
  379.     printf("output:             %I64d cyclesn", hDecoder->output_cycles);
  380. #endif
  381.     for (i = 0; i < MAX_CHANNELS; i++)
  382.     {
  383.         if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
  384.         if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
  385. #ifdef SSR_DEC
  386.         if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
  387.         if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
  388. #endif
  389. #ifdef MAIN_DEC
  390.         if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
  391. #endif
  392. #ifdef LTP_DEC
  393.         if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
  394. #endif
  395.     }
  396. #ifdef SSR_DEC
  397.     if (hDecoder->object_type == SSR)
  398.         ssr_filter_bank_end(hDecoder->fb);
  399.     else
  400. #endif
  401.         filter_bank_end(hDecoder->fb);
  402.     drc_end(hDecoder->drc);
  403.     if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
  404. #ifdef SBR_DEC
  405.     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
  406.     {
  407.         if (hDecoder->sbr[i])
  408.             sbrDecodeEnd(hDecoder->sbr[i]);
  409.     }
  410. #endif
  411.     if (hDecoder) faad_free(hDecoder);
  412. }
  413. void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
  414. {
  415.     if (hDecoder)
  416.     {
  417.         hDecoder->postSeekResetFlag = 1;
  418.         if (frame != -1)
  419.             hDecoder->frame = frame;
  420.     }
  421. }
  422. static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
  423. {
  424.     hInfo->num_front_channels = 0;
  425.     hInfo->num_side_channels = 0;
  426.     hInfo->num_back_channels = 0;
  427.     hInfo->num_lfe_channels = 0;
  428.     memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
  429.     if (hDecoder->downMatrix)
  430.     {
  431.         hInfo->num_front_channels = 2;
  432.         hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
  433.         hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
  434.         return;
  435.     }
  436.     /* check if there is a PCE */
  437.     if (hDecoder->pce_set)
  438.     {
  439.         uint8_t i, chpos = 0;
  440.         uint8_t chdir, back_center = 0;
  441.         hInfo->num_front_channels = hDecoder->pce.num_front_channels;
  442.         hInfo->num_side_channels = hDecoder->pce.num_side_channels;
  443.         hInfo->num_back_channels = hDecoder->pce.num_back_channels;
  444.         hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
  445.         chdir = hInfo->num_front_channels;
  446.         if (chdir & 1)
  447.         {
  448.             hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
  449.             chdir--;
  450.         }
  451.         for (i = 0; i < chdir; i += 2)
  452.         {
  453.             hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
  454.             hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
  455.         }
  456.         for (i = 0; i < hInfo->num_side_channels; i += 2)
  457.         {
  458.             hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
  459.             hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
  460.         }
  461.         chdir = hInfo->num_back_channels;
  462.         if (chdir & 1)
  463.         {
  464.             back_center = 1;
  465.             chdir--;
  466.         }
  467.         for (i = 0; i < chdir; i += 2)
  468.         {
  469.             hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
  470.             hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
  471.         }
  472.         if (back_center)
  473.         {
  474.             hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
  475.         }
  476.         for (i = 0; i < hInfo->num_lfe_channels; i++)
  477.         {
  478.             hInfo->channel_position[chpos++] = LFE_CHANNEL;
  479.         }
  480.     } else {
  481.         switch (hDecoder->channelConfiguration)
  482.         {
  483.         case 1:
  484.             hInfo->num_front_channels = 1;
  485.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  486.             break;
  487.         case 2:
  488.             hInfo->num_front_channels = 2;
  489.             hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
  490.             hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
  491.             break;
  492.         case 3:
  493.             hInfo->num_front_channels = 3;
  494.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  495.             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
  496.             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
  497.             break;
  498.         case 4:
  499.             hInfo->num_front_channels = 3;
  500.             hInfo->num_back_channels = 1;
  501.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  502.             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
  503.             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
  504.             hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
  505.             break;
  506.         case 5:
  507.             hInfo->num_front_channels = 3;
  508.             hInfo->num_back_channels = 2;
  509.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  510.             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
  511.             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
  512.             hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
  513.             hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
  514.             break;
  515.         case 6:
  516.             hInfo->num_front_channels = 3;
  517.             hInfo->num_back_channels = 2;
  518.             hInfo->num_lfe_channels = 1;
  519.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  520.             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
  521.             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
  522.             hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
  523.             hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
  524.             hInfo->channel_position[5] = LFE_CHANNEL;
  525.             break;
  526.         case 7:
  527.             hInfo->num_front_channels = 3;
  528.             hInfo->num_side_channels = 2;
  529.             hInfo->num_back_channels = 2;
  530.             hInfo->num_lfe_channels = 1;
  531.             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  532.             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
  533.             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
  534.             hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
  535.             hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
  536.             hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
  537.             hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
  538.             hInfo->channel_position[7] = LFE_CHANNEL;
  539.             break;
  540.         default: /* channelConfiguration == 0 || channelConfiguration > 7 */
  541.             {
  542.                 uint8_t i;
  543.                 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
  544.                 if (ch & 1) /* there's either a center front or a center back channel */
  545.                 {
  546.                     uint8_t ch1 = (ch-1)/2;
  547.                     if (hDecoder->first_syn_ele == ID_SCE)
  548.                     {
  549.                         hInfo->num_front_channels = ch1 + 1;
  550.                         hInfo->num_back_channels = ch1;
  551.                         hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  552.                         for (i = 1; i <= ch1; i+=2)
  553.                         {
  554.                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
  555.                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
  556.                         }
  557.                         for (i = ch1+1; i < ch; i+=2)
  558.                         {
  559.                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
  560.                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
  561.                         }
  562.                     } else {
  563.                         hInfo->num_front_channels = ch1;
  564.                         hInfo->num_back_channels = ch1 + 1;
  565.                         for (i = 0; i < ch1; i+=2)
  566.                         {
  567.                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
  568.                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
  569.                         }
  570.                         for (i = ch1; i < ch-1; i+=2)
  571.                         {
  572.                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
  573.                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
  574.                         }
  575.                         hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
  576.                     }
  577.                 } else {
  578.                     uint8_t ch1 = (ch)/2;
  579.                     hInfo->num_front_channels = ch1;
  580.                     hInfo->num_back_channels = ch1;
  581.                     if (ch1 & 1)
  582.                     {
  583.                         hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
  584.                         for (i = 1; i <= ch1; i+=2)
  585.                         {
  586.                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
  587.                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
  588.                         }
  589.                         for (i = ch1+1; i < ch-1; i+=2)
  590.                         {
  591.                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
  592.                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
  593.                         }
  594.                         hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
  595.                     } else {
  596.                         for (i = 0; i < ch1; i+=2)
  597.                         {
  598.                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
  599.                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
  600.                         }
  601.                         for (i = ch1; i < ch; i+=2)
  602.                         {
  603.                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
  604.                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
  605.                         }
  606.                     }
  607.                 }
  608.                 hInfo->num_lfe_channels = hDecoder->has_lfe;
  609.                 for (i = ch; i < hDecoder->fr_channels; i++)
  610.                 {
  611.                     hInfo->channel_position[i] = LFE_CHANNEL;
  612.                 }
  613.             }
  614.             break;
  615.         }
  616.     }
  617. }
  618. void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
  619.                                  NeAACDecFrameInfo *hInfo,
  620.                                  uint8_t *buffer, uint32_t buffer_size)
  621. {
  622.     return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
  623. }
  624. void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
  625.                                   NeAACDecFrameInfo *hInfo,
  626.                                   uint8_t *buffer, uint32_t buffer_size,
  627.                                   void **sample_buffer, uint32_t sample_buffer_size)
  628. {
  629.     if ((sample_buffer == NULL) || (sample_buffer_size == 0))
  630.     {
  631.         hInfo->error = 27;
  632.         return NULL;
  633.     }
  634.     return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
  635.         sample_buffer, sample_buffer_size);
  636. }
  637. #ifdef DRM
  638. #define ERROR_STATE_INIT 6
  639. static void conceal_output(NeAACDecHandle hDecoder, uint16_t frame_len,
  640.                            uint8_t out_ch, void *sample_buffer)
  641. {
  642.     uint16_t i;
  643.     int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
  644.     int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
  645. #ifndef FIXED_POINT
  646.     float32_t *float_sample_buffer = (float32_t*)sample_buffer;
  647.     double    *double_sample_buffer = (double*)sample_buffer;
  648. #endif
  649.     static const int8_t mute_tab[ERROR_STATE_INIT+1] = { 0, 1, 1, 2, 2, 3, 3 };
  650.     if (hDecoder->error_state > 0)
  651.     {
  652.         switch (hDecoder->config.outputFormat)
  653.         {
  654.         case FAAD_FMT_16BIT:
  655.             for (i = 0; i < out_ch*frame_len; i++)
  656.             {
  657.                 short_sample_buffer[i] >>= mute_tab[hDecoder->error_state];
  658.                 //short_sample_buffer[i] = 0;
  659.             }
  660.             break;
  661.         case FAAD_FMT_24BIT:
  662.         case FAAD_FMT_32BIT:
  663. #ifdef FIXED_POINT
  664.         case FAAD_FMT_FIXED:
  665. #endif
  666.             for (i = 0; i < out_ch*frame_len; i++)
  667.             {
  668.                 int_sample_buffer[i] >>= mute_tab[hDecoder->error_state];
  669.                 //int_sample_buffer[i] = 0;
  670.             }
  671.             break;
  672. #ifndef FIXED_POINT
  673.         case FAAD_FMT_FLOAT:
  674.             for (i = 0; i < out_ch*frame_len; i++)
  675.             {
  676.                 float_sample_buffer[i] /= (float)(1<<mute_tab[hDecoder->error_state]);
  677.                 //float_sample_buffer[i] = 0;
  678.             }
  679.             break;
  680.         case FAAD_FMT_DOUBLE:
  681.             for (i = 0; i < out_ch*frame_len; i++)
  682.             {
  683.                 double_sample_buffer[i] /= (float)(1<<mute_tab[hDecoder->error_state]);
  684.                 //double_sample_buffer[i] = 0;
  685.             }
  686.             break;
  687. #endif
  688.         }
  689.         hDecoder->error_state--;
  690.     }
  691. }
  692. #endif
  693. static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
  694.                               uint8_t *buffer, uint32_t buffer_size,
  695.                               void **sample_buffer2, uint32_t sample_buffer_size)
  696. {
  697.     uint16_t i;
  698.     uint8_t channels = 0;
  699.     uint8_t output_channels = 0;
  700.     bitfile ld;
  701.     uint32_t bitsconsumed;
  702.     uint16_t frame_len;
  703.     void *sample_buffer;
  704. #ifdef PROFILE
  705.     int64_t count = faad_get_ts();
  706. #endif
  707.     /* safety checks */
  708.     if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
  709.     {
  710.         return NULL;
  711.     }
  712. #if 0
  713.     printf("%dn", buffer_size*8);
  714. #endif
  715.     frame_len = hDecoder->frameLength;
  716.     memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
  717.     memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
  718.     /* check for some common metadata tag types in the bitstream
  719.      * No need to return an error
  720.      */
  721.     /* ID3 */
  722.     if (buffer_size >= 128)
  723.     {
  724.         if (memcmp(buffer, "TAG", 3) == 0)
  725.         {
  726.             /* found it */
  727.             hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
  728.             /* no error, but no output either */
  729.             return NULL;
  730.         }
  731.     }
  732.     /* initialize the bitstream */
  733.     faad_initbits(&ld, buffer, buffer_size);
  734. #if 0
  735.     {
  736.         int i;
  737.         for (i = 0; i < ((buffer_size+3)>>2); i++)
  738.         {
  739.             uint8_t *buf;
  740.             uint32_t temp = 0;
  741.             buf = faad_getbitbuffer(&ld, 32);
  742.             //temp = getdword((void*)buf);
  743.             temp = *((uint32_t*)buf);
  744.             printf("0x%.8Xn", temp);
  745.             free(buf);
  746.         }
  747.         faad_endbits(&ld);
  748.         faad_initbits(&ld, buffer, buffer_size);
  749.     }
  750. #endif
  751. #ifdef DRM
  752.     if (hDecoder->object_type == DRM_ER_LC)
  753.     {
  754.         /* We do not support stereo right now */
  755.         if (0) //(hDecoder->channelConfiguration == 2)
  756.         {
  757.             hInfo->error = 28; // Throw CRC error
  758.             goto error;
  759.         }
  760.         faad_getbits(&ld, 8
  761.             DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
  762.     }
  763. #endif
  764.     if (hDecoder->adts_header_present)
  765.     {
  766.         adts_header adts;
  767.         adts.old_format = hDecoder->config.useOldADTSFormat;
  768.         if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
  769.             goto error;
  770.         /* MPEG2 does byte_alignment() here,
  771.          * but ADTS header is always multiple of 8 bits in MPEG2
  772.          * so not needed to actually do it.
  773.          */
  774.     }
  775. #ifdef ANALYSIS
  776.     dbg_count = 0;
  777. #endif
  778.     /* decode the complete bitstream */
  779. #ifdef DRM
  780.     if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC))
  781.     {
  782.         DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
  783.     } else {
  784. #endif
  785.         raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
  786. #ifdef DRM
  787.     }
  788. #endif
  789.     channels = hDecoder->fr_channels;
  790.     if (hInfo->error > 0)
  791.         goto error;
  792.     /* safety check */
  793.     if (channels == 0 || channels > MAX_CHANNELS)
  794.     {
  795.         /* invalid number of channels */
  796.         hInfo->error = 12;
  797.         goto error;
  798.     }
  799.     /* no more bit reading after this */
  800.     bitsconsumed = faad_get_processed_bits(&ld);
  801.     hInfo->bytesconsumed = bit2byte(bitsconsumed);
  802.     if (ld.error)
  803.     {
  804.         hInfo->error = 14;
  805.         goto error;
  806.     }
  807.     faad_endbits(&ld);
  808.     if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
  809.     {
  810.         if (hDecoder->channelConfiguration == 0)
  811.             hDecoder->channelConfiguration = channels;
  812.         if (channels == 8) /* 7.1 */
  813.             hDecoder->channelConfiguration = 7;
  814.         if (channels == 7) /* not a standard channelConfiguration */
  815.             hDecoder->channelConfiguration = 0;
  816.     }
  817.     if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
  818.     {
  819.         hDecoder->downMatrix = 1;
  820.         output_channels = 2;
  821.     } else {
  822.         output_channels = channels;
  823.     }
  824. #if (defined(PS_DEC) || defined(DRM_PS))
  825.     hDecoder->upMatrix = 0;
  826.     /* check if we have a mono file */
  827.     if (output_channels == 1)
  828.     {
  829.         /* upMatrix to 2 channels for implicit signalling of PS */
  830.         hDecoder->upMatrix = 1;
  831.         output_channels = 2;
  832.     }
  833. #endif
  834.     /* Make a channel configuration based on either a PCE or a channelConfiguration */
  835.     create_channel_config(hDecoder, hInfo);
  836.     /* number of samples in this frame */
  837.     hInfo->samples = frame_len*output_channels;
  838.     /* number of channels in this frame */
  839.     hInfo->channels = output_channels;
  840.     /* samplerate */
  841.     hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
  842.     /* object type */
  843.     hInfo->object_type = hDecoder->object_type;
  844.     /* sbr */
  845.     hInfo->sbr = NO_SBR;
  846.     /* header type */
  847.     hInfo->header_type = RAW;
  848.     if (hDecoder->adif_header_present)
  849.         hInfo->header_type = ADIF;
  850.     if (hDecoder->adts_header_present)
  851.         hInfo->header_type = ADTS;
  852. #if (defined(PS_DEC) || defined(DRM_PS))
  853.     hInfo->ps = hDecoder->ps_used_global;
  854. #endif
  855.     /* check if frame has channel elements */
  856.     if (channels == 0)
  857.     {
  858.         hDecoder->frame++;
  859.         return NULL;
  860.     }
  861.     /* allocate the buffer for the final samples */
  862.     if ((hDecoder->sample_buffer == NULL) ||
  863.         (hDecoder->alloced_channels != output_channels))
  864.     {
  865.         static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
  866.             sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
  867.             sizeof(int16_t), sizeof(int16_t), 0, 0, 0
  868.         };
  869.         uint8_t stride = str[hDecoder->config.outputFormat-1];
  870. #ifdef SBR_DEC
  871.         if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
  872.         {
  873.             stride = 2 * stride;
  874.         }
  875. #endif
  876.         /* check if we want to use internal sample_buffer */
  877.         if (sample_buffer_size == 0)
  878.         {
  879.             if (hDecoder->sample_buffer)
  880.                 faad_free(hDecoder->sample_buffer);
  881.             hDecoder->sample_buffer = NULL;
  882.             hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
  883.         } else if (sample_buffer_size < frame_len*output_channels*stride) {
  884.             /* provided sample buffer is not big enough */
  885.             hInfo->error = 27;
  886.             return NULL;
  887.         }
  888.         hDecoder->alloced_channels = output_channels;
  889.     }
  890.     if (sample_buffer_size == 0)
  891.     {
  892.         sample_buffer = hDecoder->sample_buffer;
  893.     } else {
  894.         sample_buffer = *sample_buffer2;
  895.     }
  896. #ifdef SBR_DEC
  897.     if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
  898.     {
  899.         uint8_t ele;
  900.         /* this data is different when SBR is used or when the data is upsampled */
  901.         if (!hDecoder->downSampledSBR)
  902.         {
  903.             frame_len *= 2;
  904.             hInfo->samples *= 2;
  905.             hInfo->samplerate *= 2;
  906.         }
  907.         /* check if every element was provided with SBR data */
  908.         for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
  909.         {
  910.             if (hDecoder->sbr[ele] == NULL)
  911.             {
  912.                 hInfo->error = 25;
  913.                 goto error;
  914.             }
  915.         }
  916.         /* sbr */
  917.         if (hDecoder->sbr_present_flag == 1)
  918.         {
  919.             hInfo->object_type = HE_AAC;
  920.             hInfo->sbr = SBR_UPSAMPLED;
  921.         } else {
  922.             hInfo->sbr = NO_SBR_UPSAMPLED;
  923.         }
  924.         if (hDecoder->downSampledSBR)
  925.         {
  926.             hInfo->sbr = SBR_DOWNSAMPLED;
  927.         }
  928.     }
  929. #endif
  930.     sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
  931.         output_channels, frame_len, hDecoder->config.outputFormat);
  932. #ifdef DRM
  933.     //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
  934. #endif
  935.     hDecoder->postSeekResetFlag = 0;
  936.     hDecoder->frame++;
  937. #ifdef LD_DEC
  938.     if (hDecoder->object_type != LD)
  939.     {
  940. #endif
  941.         if (hDecoder->frame <= 1)
  942.             hInfo->samples = 0;
  943. #ifdef LD_DEC
  944.     } else {
  945.         /* LD encoders will give lower delay */
  946.         if (hDecoder->frame <= 0)
  947.             hInfo->samples = 0;
  948.     }
  949. #endif
  950.     /* cleanup */
  951. #ifdef ANALYSIS
  952.     fflush(stdout);
  953. #endif
  954. #ifdef PROFILE
  955.     count = faad_get_ts() - count;
  956.     hDecoder->cycles += count;
  957. #endif
  958.     return sample_buffer;
  959. error:
  960. #ifdef DRM
  961.     hDecoder->error_state = ERROR_STATE_INIT;
  962. #endif
  963.     /* reset filterbank state */
  964.     for (i = 0; i < MAX_CHANNELS; i++)
  965.     {
  966.         if (hDecoder->fb_intermed[i] != NULL)
  967.         {
  968.             memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
  969.         }
  970.     }
  971. #ifdef SBR_DEC
  972.     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
  973.     {
  974.         if (hDecoder->sbr[i] != NULL)
  975.         {
  976.             sbrReset(hDecoder->sbr[i]);
  977.         }
  978.     }
  979. #endif
  980.     faad_endbits(&ld);
  981.     /* cleanup */
  982. #ifdef ANALYSIS
  983.     fflush(stdout);
  984. #endif
  985.     return NULL;
  986. }