decoder.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:21k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /************************* MPEG-2 NBC Audio Decoder **************************
  2.  *                                                                           *
  3. "This software module was originally developed by
  4. AT&T, Dolby Laboratories, Fraunhofer Gesellschaft IIS
  5. and edited by
  6. Yoshiaki Oikawa (Sony Corporation),
  7. Mitsuyuki Hatanaka (Sony Corporation)
  8. in the course of development of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7,
  9. 14496-1,2 and 3. This software module is an implementation of a part of one or more
  10. MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
  11. Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
  12. standards free license to this software module or modifications thereof for use in
  13. hardware or software products claiming conformance to the MPEG-2 NBC/MPEG-4
  14. Audio  standards. Those intending to use this software module in hardware or
  15. software products are advised that this use may infringe existing patents.
  16. The original developer of this software module and his/her company, the subsequent
  17. editors and their companies, and ISO/IEC have no liability for use of this software
  18. module or modifications thereof in an implementation. Copyright is not released for
  19. non MPEG-2 NBC/MPEG-4 Audio conforming products.The original developer
  20. retains full right to use the code for his/her  own purpose, assign or donate the
  21. code to a third party and to inhibit third party from using the code for non
  22. MPEG-2 NBC/MPEG-4 Audio conforming products. This copyright notice must
  23. be included in all copies or derivative works."
  24. Copyright(c)1996.
  25.  *                                                                           *
  26.  ****************************************************************************/
  27. #ifdef WIN32
  28. #define WIN32_MEAN_AND_LEAN
  29. #include <windows.h>
  30. #endif
  31. #include "all.h"
  32. #include "block.h"
  33. #include "nok_lt_prediction.h"
  34. #include "transfo.h"
  35. #include "bits.h"
  36. #include "util.h"
  37. /* D E F I N E S */
  38. #define ftol(A,B) {tmp = *(int*) & A - 0x4B7F8000; B = (short)( (tmp==(short)tmp) ? tmp : (tmp>>31)^0x7FFF);}
  39. #define BYTE_NUMBIT 8       /* bits in byte (char) */
  40. #define bit2byte(a) ((a)/BYTE_NUMBIT) /* (((a)+BYTE_NUMBIT-1)/BYTE_NUMBIT) */
  41. static unsigned long ObjectTypesTable[32] = {0, 1, 1, 1, 1, };
  42. int parse_audio_decoder_specific_info(faacDecHandle hDecoder,unsigned long *samplerate,unsigned long *channels)
  43. {
  44.     unsigned long ObjectTypeIndex, SamplingFrequencyIndex, ChannelsConfiguration;
  45.     faad_byte_align(&hDecoder->ld);
  46.     ObjectTypeIndex = faad_getbits(&hDecoder->ld, 5);
  47.     SamplingFrequencyIndex = faad_getbits(&hDecoder->ld, 4);
  48.     ChannelsConfiguration = faad_getbits(&hDecoder->ld, 4);
  49.     if(ObjectTypesTable[ObjectTypeIndex] != 1){
  50.         return -1;
  51.     }
  52.     *samplerate = SampleRates[SamplingFrequencyIndex];
  53.     if(*samplerate == 0){
  54.         return -2;
  55.     }
  56.     hDecoder->numChannels = *channels = ChannelsConfiguration;
  57.     hDecoder->mc_info.object_type = ObjectTypeIndex - 1;
  58.     hDecoder->mc_info.sampling_rate_idx = SamplingFrequencyIndex;
  59.     if((ChannelsConfiguration != 1) && (ChannelsConfiguration != 2)){
  60.         return -3;
  61.     }
  62.     return 0;
  63. }
  64. int FAADAPI faacDecInit2(faacDecHandle hDecoder,
  65.                          unsigned char* pBuffer,unsigned long SizeOfDecoderSpecificInfo,
  66.                          unsigned long *samplerate, unsigned long *channels)
  67. {
  68.     int rc;
  69.     hDecoder->adif_header_present = 0;
  70.     hDecoder->adts_header_present = 0;
  71.     if((hDecoder == NULL)
  72.         || (pBuffer == NULL)
  73.         || (SizeOfDecoderSpecificInfo < 2)
  74.         || (samplerate == NULL)
  75.         || (channels == NULL)){
  76.         return -1;
  77.     }
  78.     /* get adif header */
  79.     faad_initbits(&hDecoder->ld, pBuffer, 0);
  80.     rc = parse_audio_decoder_specific_info(hDecoder,samplerate,channels);
  81.     if(rc != 0){
  82.         return rc;
  83.     }
  84.     huffbookinit(hDecoder);
  85.     nok_init_lt_pred(hDecoder->nok_lt_status, Chans);
  86.     init_pred(hDecoder, hDecoder->sp_status, Chans);
  87.     MakeFFTOrder(hDecoder);
  88.     InitBlock();  /* calculate windows */
  89.     hDecoder->winmap[0] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  90.     hDecoder->winmap[1] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  91.     hDecoder->winmap[2] = hDecoder->win_seq_info[EIGHT_SHORT_WINDOW];
  92.     hDecoder->winmap[3] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  93.     return 0;
  94. }
  95. faacDecHandle FAADAPI faacDecOpen(void)
  96. {
  97.     int i;
  98.     faacDecHandle hDecoder = NULL;
  99.     hDecoder = (faacDecHandle)AllocMemory(sizeof(faacDecStruct));
  100. #ifndef WIN32
  101.     SetMemory(hDecoder, 0, sizeof(faacDecStruct));
  102. #endif
  103.     hDecoder->frameNum = 0;
  104.     hDecoder->isMpeg4 = 1;
  105.     /* set defaults */
  106.     hDecoder->current_program = -1;
  107.     hDecoder->default_config = 1;
  108.     hDecoder->dolbyShortOffset_f2t = 1;
  109.     hDecoder->dolbyShortOffset_t2f = 1;
  110.     hDecoder->first_cpe = 0;
  111.     hDecoder->warn_flag = 1;
  112.     hDecoder->config.defObjectType = AACMAIN;
  113.     hDecoder->config.defSampleRate = 44100;
  114.     for(i=0; i < Chans; i++)
  115.     {
  116.         hDecoder->coef[i] = (Float*)AllocMemory(LN2*sizeof(Float));
  117.         hDecoder->data[i] = (Float*)AllocMemory(LN2*sizeof(Float));
  118.         hDecoder->state[i] = (Float*)AllocMemory(LN*sizeof(Float));
  119.         hDecoder->factors[i] = (int*)AllocMemory(MAXBANDS*sizeof(int));
  120.         hDecoder->cb_map[i] = (byte*)AllocMemory(MAXBANDS*sizeof(byte));
  121.         hDecoder->group[i] = (byte*)AllocMemory(NSHORT*sizeof(byte));
  122.         hDecoder->lpflag[i] = (int*)AllocMemory(MAXBANDS*sizeof(int));
  123.         hDecoder->prstflag[i] = (int*)AllocMemory((LEN_PRED_RSTGRP+1)*sizeof(int));
  124.         hDecoder->tns[i] = (TNS_frame_info*)AllocMemory(sizeof(TNS_frame_info));
  125.         hDecoder->nok_lt_status[i]  = (NOK_LT_PRED_STATUS*)AllocMemory(sizeof(NOK_LT_PRED_STATUS));
  126.         hDecoder->sp_status[i]  = (PRED_STATUS*)AllocMemory(LN*sizeof(PRED_STATUS));
  127.         hDecoder->last_rstgrp_num[i] = 0;
  128.         hDecoder->wnd_shape[i].prev_bk = 0;
  129. #ifndef WIN32 /* WIN32 API sets memory to 0 in allocation */
  130.         SetMemory(hDecoder->coef[i],0,LN2*sizeof(Float));
  131.         SetMemory(hDecoder->data[i],0,LN2*sizeof(Float));
  132.         SetMemory(hDecoder->state[i],0,LN*sizeof(Float));
  133.         SetMemory(hDecoder->factors[i],0,MAXBANDS*sizeof(int));
  134.         SetMemory(hDecoder->cb_map[i],0,MAXBANDS*sizeof(byte));
  135.         SetMemory(hDecoder->group[i],0,NSHORT*sizeof(byte));
  136.         SetMemory(hDecoder->lpflag[i],0,MAXBANDS*sizeof(int));
  137.         SetMemory(hDecoder->prstflag[i],0,(LEN_PRED_RSTGRP+1)*sizeof(int));
  138.         SetMemory(hDecoder->tns[i],0,sizeof(TNS_frame_info));
  139.         SetMemory(hDecoder->nok_lt_status[i],0,sizeof(NOK_LT_PRED_STATUS));
  140.         SetMemory(hDecoder->sp_status[i],0,LN*sizeof(PRED_STATUS));
  141. #endif
  142.     }
  143.     hDecoder->mnt_table = AllocMemory(128*sizeof(float));
  144.     hDecoder->exp_table = AllocMemory(256*sizeof(float));
  145.     hDecoder->iq_exp_tbl = AllocMemory(MAX_IQ_TBL*sizeof(Float));
  146.     hDecoder->exptable = AllocMemory(TEXP*sizeof(Float));
  147.     hDecoder->unscambled64 = AllocMemory(64*sizeof(int));
  148.     hDecoder->unscambled512 = AllocMemory(512*sizeof(int));
  149.     SetMemory(hDecoder->lp_store, 0, MAXBANDS*sizeof(int));
  150.     SetMemory(hDecoder->noise_state_save, 0, MAXBANDS*sizeof(long));
  151.     for(i=0; i<Winds; i++)
  152.     {
  153.         hDecoder->mask[i] = (byte*)AllocMemory(MAXBANDS*sizeof(byte));
  154.         SetMemory(hDecoder->mask[i],0,MAXBANDS*sizeof(byte));
  155.     }
  156.     for (i = 0; i < NUM_WIN_SEQ; i++)
  157.         hDecoder->win_seq_info[i] = NULL;
  158.     return hDecoder;
  159. }
  160. faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder)
  161. {
  162.     faacDecConfigurationPtr config = &(hDecoder->config);
  163.     return config;
  164. }
  165. int FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
  166.                                     faacDecConfigurationPtr config)
  167. {
  168.     hDecoder->config.defObjectType = config->defObjectType;
  169.     hDecoder->config.defSampleRate = config->defSampleRate;
  170.     /* OK */
  171.     return 1;
  172. }
  173. int FAADAPI faacDecInit(faacDecHandle hDecoder,
  174. unsigned char *buffer,
  175.                         unsigned long *samplerate,
  176. unsigned long *channels)
  177. {
  178.     int i, bits = 0;
  179.     char chk_header[4];
  180.     faad_initbits(&hDecoder->ld, buffer, 4);
  181. #if 0
  182.     faad_bookmark(&hDecoder->ld, 1);
  183. #endif
  184.     for (i = 0; i < 4; i++) {
  185.       chk_header[i] = buffer[i];
  186.     }
  187.     /* Check if an ADIF header is present */
  188.     if (stringcmp(chk_header, "ADIF", 4) == 0)
  189.         hDecoder->adif_header_present = 1;
  190.     else
  191.         hDecoder->adif_header_present = 0;
  192.     /* Check if an ADTS header is present */
  193.     if (hDecoder->adif_header_present == 0)
  194.     {
  195.         if (((int) ( chk_header[0] == (char) 0xFF)) &&
  196.             ((int) ( (chk_header[1] & (char) 0xF0) == (char) 0xF0)))
  197.         {
  198.             hDecoder->adts_header_present = 1;
  199.         } else {
  200.             hDecoder->adts_header_present = 0;
  201.         }
  202.     }
  203.     /* get adif header */
  204.     if (hDecoder->adif_header_present) {
  205.         hDecoder->pceChannels = 2;
  206. #if 0
  207.         faad_bookmark(&hDecoder->ld, 0);
  208.         faad_bookmark(&hDecoder->ld, 1);
  209. #else
  210. faad_initbits(&hDecoder->ld, buffer, 4);
  211. #endif
  212.         get_adif_header(hDecoder);
  213.         /* only MPEG2 ADIF header uses byte_alignment */
  214.         /* but the PCE already byte aligns the data */
  215.         /*
  216.         if (!hDecoder->isMpeg4)
  217.             byte_align(&hDecoder->ld);
  218.         */
  219.         bits = faad_get_processed_bits(&hDecoder->ld);
  220.     } else if (hDecoder->adts_header_present) {
  221. #if 0
  222.         faad_bookmark(&hDecoder->ld, 0);
  223.         faad_bookmark(&hDecoder->ld, 1);
  224. #else
  225. faad_initbits(&hDecoder->ld, buffer, 4);
  226. #endif
  227.         get_adts_header(hDecoder);
  228.         /* only MPEG2 ADTS header uses byte_alignment */
  229.         /* but it already is a multiple of 8 bits */
  230.         /*
  231.         if (!hDecoder->isMpeg4)
  232.             byte_align(&hDecoder->ld);
  233.         */
  234.         bits = 0;
  235.     } else {
  236.         hDecoder->mc_info.object_type = hDecoder->config.defObjectType;
  237.         hDecoder->mc_info.sampling_rate_idx = get_sr_index(hDecoder->config.defSampleRate);
  238.     }
  239.     *samplerate = hDecoder->config.defSampleRate;
  240.     hDecoder->numChannels = *channels = 2;
  241.     hDecoder->chans_inited = 0;
  242.     if (hDecoder->adif_header_present) {
  243.       hDecoder->chans_inited = 1;
  244.         *samplerate = SampleRates[hDecoder->prog_config.sampling_rate_idx];
  245.         hDecoder->numChannels = *channels = hDecoder->pceChannels;
  246.     } else if (hDecoder->adts_header_present) {
  247.       hDecoder->chans_inited = 1;
  248.         *samplerate = SampleRates[hDecoder->adts_header.fixed.sampling_rate_idx];
  249.         hDecoder->numChannels = *channels = hDecoder->adts_header.fixed.channel_configuration; /* This works up to 6 channels */
  250.     }
  251.     huffbookinit(hDecoder);
  252.     nok_init_lt_pred(hDecoder->nok_lt_status, Chans);
  253.     init_pred(hDecoder, hDecoder->sp_status, Chans);
  254.     MakeFFTOrder(hDecoder);
  255.     InitBlock();  /* calculate windows */
  256.     hDecoder->winmap[0] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  257.     hDecoder->winmap[1] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  258.     hDecoder->winmap[2] = hDecoder->win_seq_info[EIGHT_SHORT_WINDOW];
  259.     hDecoder->winmap[3] = hDecoder->win_seq_info[ONLY_LONG_WINDOW];
  260. #if 0
  261.     faad_bookmark(&hDecoder->ld, 0);
  262. #endif
  263.     return bit2byte(bits);
  264. }
  265. void FAADAPI faacDecClose(faacDecHandle hDecoder)
  266. {
  267.     int i;
  268.     EndBlock();
  269.     nok_end_lt_pred(hDecoder->nok_lt_status, Chans);
  270.     for(i=0; i < Chans; i++)
  271.     {
  272.         if (hDecoder->coef[i]) FreeMemory(hDecoder->coef[i]);
  273.         if (hDecoder->data[i]) FreeMemory(hDecoder->data[i]);
  274.         if (hDecoder->state[i]) FreeMemory(hDecoder->state[i]);
  275.         if (hDecoder->factors[i]) FreeMemory(hDecoder->factors[i]);
  276.         if (hDecoder->cb_map[i]) FreeMemory(hDecoder->cb_map[i]);
  277.         if (hDecoder->group[i]) FreeMemory(hDecoder->group[i]);
  278.         if (hDecoder->lpflag[i]) FreeMemory(hDecoder->lpflag[i]);
  279.         if (hDecoder->prstflag[i]) FreeMemory(hDecoder->prstflag[i]);
  280.         if (hDecoder->tns[i]) FreeMemory(hDecoder->tns[i]);
  281.         if (hDecoder->nok_lt_status[i]) FreeMemory(hDecoder->nok_lt_status[i]);
  282.         if (hDecoder->sp_status[i]) FreeMemory(hDecoder->sp_status[i]);
  283.     }
  284.     if (hDecoder->mnt_table) FreeMemory(hDecoder->mnt_table);
  285.     if (hDecoder->exp_table) FreeMemory(hDecoder->exp_table);
  286.     if (hDecoder->iq_exp_tbl) FreeMemory(hDecoder->iq_exp_tbl);
  287.     if (hDecoder->exptable) FreeMemory(hDecoder->exptable);
  288.     if (hDecoder->unscambled64) FreeMemory(hDecoder->unscambled64);
  289.     if (hDecoder->unscambled512) FreeMemory(hDecoder->unscambled512);
  290.     for(i=0; i<Winds; i++)
  291.     {
  292.         if (hDecoder->mask[i]) FreeMemory(hDecoder->mask[i]);
  293.     }
  294.     if (hDecoder) FreeMemory(hDecoder);
  295. }
  296. int FAADAPI faacDecGetProgConfig(faacDecHandle hDecoder,
  297.                                  faacProgConfig *progConfig)
  298. {
  299.     return hDecoder->numChannels;
  300. }
  301. int FAADAPI faacDecDecode(faacDecHandle hDecoder,
  302.                           unsigned char *buffer,
  303.                           unsigned long *bytesconsumed,
  304.                           short *sample_buffer,
  305.                           unsigned long *samples)
  306. {
  307.     unsigned char d_bytes[MAX_DBYTES];
  308.     int i, j, ch, wn, ele_id;
  309.     int left, right, tmp;
  310.     int d_tag, d_cnt;
  311.     int channels = 0;
  312.     Info *info;
  313.     MC_Info *mip = &(hDecoder->mc_info);
  314.     Ch_Info *cip;
  315.     int retCode = FAAD_OK;
  316.     faad_initbits(&hDecoder->ld, buffer, *bytesconsumed);
  317.     if (hDecoder->adts_header_present)
  318.     {
  319.         if (get_adts_header(hDecoder))
  320.         {
  321.             goto error;
  322.         }
  323.         /* MPEG2 does byte_alignment() here
  324.         * but ADTS header is always multiple of 8 bits in MPEG2
  325.         * so not needed
  326.         */
  327.     }
  328.     reset_mc_info(hDecoder, mip);
  329.     while ((ele_id=faad_getbits(&hDecoder->ld, LEN_SE_ID)) != ID_END)
  330.     {
  331.         /* get audio syntactic element */
  332.         switch (ele_id) {
  333.         case ID_SCE:        /* single channel */
  334.         case ID_CPE:        /* channel pair */
  335.         case ID_LFE:        /* low freq effects channel */
  336.             if (huffdecode(hDecoder, ele_id, mip, hDecoder->wnd, hDecoder->wnd_shape,
  337.                 hDecoder->cb_map, hDecoder->factors, hDecoder->group, hDecoder->hasmask,
  338.                 hDecoder->mask, hDecoder->max_sfb,
  339.                 hDecoder->lpflag, hDecoder->prstflag, hDecoder->nok_lt_status,
  340.                 hDecoder->tns, hDecoder->coef) < 0)
  341.                 goto error;
  342.                 /* CommonExit(1,"2022: Error in huffman decoder"); */
  343.             if (ele_id == ID_CPE)
  344.                 channels += 2;
  345.             else
  346.                 channels++;
  347.             break;
  348.         case ID_DSE:        /* data element */
  349.             if (getdata(hDecoder, &d_tag, &d_cnt, d_bytes) < 0)
  350.                 goto error;
  351.             break;
  352.         case ID_PCE:        /* program config element */
  353.             get_prog_config(hDecoder, &hDecoder->prog_config);
  354.             break;
  355.         case ID_FIL:        /* fill element */
  356.             getfill(hDecoder, d_bytes);
  357.             break;
  358.         default:
  359.             /* CommonWarning("Element not supported"); */
  360.             goto error;
  361.         }
  362.     }
  363.     if ((channels != hDecoder->numChannels) &&
  364. (hDecoder->chans_inited == 0)) {
  365.         hDecoder->numChannels = channels;
  366.         retCode = FAAD_OK_CHUPDATE;
  367.         *bytesconsumed = 0;
  368. #if 0
  369. // wmay = fall through...
  370.         /* no errors, but channel update */
  371.         return retCode;
  372. #endif
  373.     }
  374.     if (!check_mc_info(hDecoder, mip, (hDecoder->frameNum==0 && hDecoder->default_config))) {
  375.         goto error;
  376.     }
  377.     /* m/s stereo */
  378.     for (ch=0; ch<Chans; ch++) {
  379.         cip = &mip->ch_info[ch];
  380.         if ((cip->present) && (cip->cpe) && (cip->ch_is_left)) {
  381.             wn = cip->widx;
  382.             if(hDecoder->hasmask[wn]) {
  383.                 left = ch;
  384.                 right = cip->paired_ch;
  385.                 info = hDecoder->winmap[hDecoder->wnd[wn]];
  386.                 if (hDecoder->hasmask[wn] == 1)
  387.                     map_mask(info, hDecoder->group[wn], hDecoder->mask[wn],
  388.                     hDecoder->cb_map[right]);
  389.                 synt(info, hDecoder->group[wn], hDecoder->mask[wn],
  390.                     hDecoder->coef[right], hDecoder->coef[left]);
  391.             }
  392.         }
  393.     }
  394.     /* intensity stereo and prediction */
  395.     for (ch=0; ch<Chans; ch++) {
  396.         if (!(mip->ch_info[ch].present))
  397.             continue;
  398.         wn = mip->ch_info[ch].widx;
  399.         info = hDecoder->winmap[hDecoder->wnd[wn]];
  400.         pns(hDecoder, mip, info, wn, ch,
  401.             hDecoder->group[wn], hDecoder->cb_map[ch], hDecoder->factors[ch],
  402.             hDecoder->lpflag[wn], hDecoder->coef);
  403.         intensity(mip, info, wn, ch,
  404.             hDecoder->group[wn], hDecoder->cb_map[ch], hDecoder->factors[ch],
  405.             hDecoder->lpflag[wn], hDecoder->coef);
  406.         if (mip->object_type == AACLTP) {
  407.             nok_lt_predict(hDecoder, info, hDecoder->wnd[wn], &hDecoder->wnd_shape[wn],
  408.                 hDecoder->nok_lt_status[ch]->sbk_prediction_used,
  409.                 hDecoder->nok_lt_status[ch]->sfb_prediction_used,
  410.                 hDecoder->nok_lt_status[ch], hDecoder->nok_lt_status[ch]->weight,
  411.                 hDecoder->nok_lt_status[ch]->delay, hDecoder->coef[ch],
  412.                 BLOCK_LEN_LONG, 0, BLOCK_LEN_SHORT, hDecoder->tns[ch]);
  413.         } else if (mip->object_type == AACMAIN) {
  414.             predict(hDecoder, info, mip->object_type, hDecoder->lpflag[wn],
  415.                 hDecoder->sp_status[ch], hDecoder->coef[ch]);
  416.         }
  417.     }
  418.     for (ch = 0; ch < Chans; ch++) {
  419.         if (!(mip->ch_info[ch].present))
  420.             continue;
  421.         wn = mip->ch_info[ch].widx;
  422.         info = hDecoder->winmap[hDecoder->wnd[wn]];
  423.         /* predictor reset */
  424.         if (mip->object_type == AACMAIN) {
  425.             left = ch;
  426.             right = left;
  427.             if ((mip->ch_info[ch].cpe) && (mip->ch_info[ch].common_window))
  428.                 /* prstflag's shared by channel pair */
  429.                 right = mip->ch_info[ch].paired_ch;
  430.             predict_reset(hDecoder, info, hDecoder->prstflag[wn], hDecoder->sp_status,
  431.                 left, right, hDecoder->last_rstgrp_num);
  432.             /* PNS predictor reset */
  433.             predict_pns_reset(info, hDecoder->sp_status[ch], hDecoder->cb_map[ch]);
  434.         }
  435.         /* tns */
  436.         for (i=j=0; i < hDecoder->tns[ch]->n_subblocks; i++) {
  437.             tns_decode_subblock(hDecoder, &hDecoder->coef[ch][j],
  438.                 hDecoder->max_sfb[wn],
  439.                 info->sbk_sfb_top[i],
  440.                 info->islong,
  441.                 &(hDecoder->tns[ch]->info[i]) );
  442.             j += info->bins_per_sbk[i];
  443.         }
  444.         /* inverse transform */
  445.         freq2time_adapt(hDecoder, hDecoder->wnd[wn], &hDecoder->wnd_shape[wn],
  446.             hDecoder->coef[ch], hDecoder->state[ch], hDecoder->data[ch]);
  447.         if (mip->object_type == AACLTP) {
  448.             nok_lt_update(hDecoder->nok_lt_status[ch], hDecoder->data[ch],
  449.                 hDecoder->state[ch], BLOCK_LEN_LONG);
  450.         }
  451.     }
  452.     /* Copy output to a standard PCM buffer */
  453.     for(i=0; i < 1024; i++) /* prkoat */
  454.     {
  455.         for (ch=0; ch < mip->nch; ch++)
  456.         {
  457. /* much faster FTOL */
  458. #ifndef ENDIAN_SAFE
  459.             float ftemp;
  460.             /* ftemp = truncate(data[ch][i]) + 0xff8000; */
  461.             ftemp = hDecoder->data[ch][i] + 0xff8000;
  462.             ftol(ftemp, sample_buffer[(i*mip->nch)+ch]);
  463. #else
  464.             sample_buffer[(i*mip->nch)+ch] = (short)truncate(data[ch][i]);
  465. #endif
  466.         }
  467.     }
  468.     hDecoder->frameNum++;
  469.     faad_byte_align(&hDecoder->ld);
  470.     *bytesconsumed = bit2byte(faad_get_processed_bits(&hDecoder->ld));
  471.     if (hDecoder->frameNum > 2)
  472.         *samples = 1024*mip->nch;
  473.     else
  474.         *samples = 0;
  475.     /* no errors */
  476.     return retCode;
  477. error:
  478. #if 0
  479.     //
  480.     // wmay - remove this error recovery, and let calling app handle this -
  481.     // with mpeg4ip, we can get better recovery, especially with RTP.
  482.     //
  483.     /* search for next ADTS header first, so decoding can be continued */
  484.     /* ADIF and RAW AAC files will not be able to continue playing */
  485.     if (hDecoder->adts_header_present) {
  486.         int k, sync = 0;
  487.         faad_byte_align(&hDecoder->ld);
  488.         for(k = 0; sync != 4096 - 1; k++)
  489.         {
  490.             sync = faad_getbits(&hDecoder->ld, 12); /* 12 bit SYNCWORD */
  491.             faad_getbits(&hDecoder->ld, 4);
  492.             /* Bail out if no syncword found */
  493.             if(k >= (6144 / 16))
  494.             {
  495.                 SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);
  496.                 /* unrecoverable error */
  497.                 return FAAD_FATAL_ERROR;
  498.             }
  499.         }
  500.         *bytesconsumed = bit2byte(hDecoder->ld.framebits - 16);
  501.         SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);
  502.         /* error, but recoverable */
  503.         return FAAD_ERROR;
  504.     } else {
  505.         SetMemory(sample_buffer, 0, sizeof(short)*mip->nch*1024);
  506.         /* unrecoverable error */
  507.         return FAAD_FATAL_ERROR;
  508.     }
  509. #else
  510.     *bytesconsumed = bit2byte(faad_get_processed_bits(&hDecoder->ld));
  511.     return FAAD_ERROR;
  512. #endif
  513. }