1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
20 Pathname: pvmp4audiodecodeframe
22 ------------------------------------------------------------------------------
25 Description: Modified from original shareware code
27 Description: Pulled in loop structure from console.c, so that this function
28 now decodes all frames in the file.
30 Original program used several global variables. These have been
31 eliminated, except for situations in which the global variables
32 could be converted into const types. Otherwise, they are passed
33 by reference through the functions.
35 Description: Begin mods for file I/O removal
37 Description: Merged trans4m_freq_2_time, trans4m_time_2_freq, etc.
39 Description: Removing commented out sections of code. This includes the
40 removal of unneeded functions init_lt_pred, reset_mc_info,
42 Description: Copied from aac_decode_frame.c and renamed file,
45 Description: Prepare for code review
47 Description: Update per review comments:
48 1) Add comment about leaveGetLoop
49 2) Remove inverseTNSCoef array
50 3) fix wnd_shape_this_bk to wnd_shape_prev_bk in F to T
52 5) Change call to long_term_synthesis
54 Description: Remove division for calculation of bitrate.
56 Description: Remove update of LTP buffers if not LTP audio object type.
58 Description: Add hasmask to call to right_ch_sfb_tools_ms
61 Modified to call ltp related routines on the left channel
62 before intensity is called on the right channel. The previous version
63 was causing a problem when IS was used on the right channel and LTP
64 on the left channel for the same scalefactor band.
66 This fix required creating a new function, apply_ms_synt, deleting another
67 function (right_ch_sfb_tools_noms.c), and modifying the calling order of
70 Description: Made changes per review comments.
72 Description: Changed name of right_ch_sfb_tools_ms to pns_intensity_right
74 Description: Added cast, since pVars->inputStream.usedBits is UInt, and
75 pExt->remainderBits is Int.
78 (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK);
80 Description: Modified to pass a pointer to scratch memory into
83 Description: Removed include of "s_TNSInfo.h"
85 Description: Removed call to "tns_setup_filter" which has been eliminated
86 by merging its functionality into "get_tns"
88 Description: Passing in a pointer to a q-format array, rather than
89 the address of a single q-format, for the inverse filter case for
93 (1) Added #include of "e_ElementId.h"
94 Previously, this function was relying on another include file
95 to include "e_ElementId.h"
97 (2) Updated the copyright header.
100 Per review comments, declared two temporary variables
102 pChLeftShare = pChVars[LEFT]->pShareWfxpCoef;
103 pChRightShare = pChVars[RIGHT]->pShareWfxpCoef;
106 long_term_synthesis should have been invoked with max_sfb
107 as the 2nd parameter, rather than pFrameInfo->sfb_per_win[0].
112 pFrameInfo->sfb_per_win[0] ...
117 pChVars[ch]->pShareWfxpCoef->max_sfb ...
119 This problem caused long_term_synthesis to read memory which
120 was not initialized in get_ics_info.c
123 (1) Utilize scratch memory for the scratch Prog_Config.
125 Description: (1) Modified to decode ID_END syntactic element after header
128 (1) Reconfigured LTP buffer as a circular buffer. This saves
129 2048 Int16->Int16 copies per frame.
131 Description: Updated so ltp buffers are not used as a wasteful
132 intermediate buffer for LC streams. Data is transferred directly
133 from the filterbank to the output stream.
135 Description: Decode ADIF header if frame count is zero.
136 The AudioSpecificConfig is decoded by a separate API.
138 Description: Added comments explaining how the ltp_buffer_state
142 Description: Modified code to take advantage of new trans4m_freq_2_time_fxp,
143 which writes the output directly into a 16-bit output buffer. This
144 improvement allows faster operation by reducing the amount of memory
145 transfers. Speed can be further improved on most platforms via use of a
146 DMA transfer in the function write_output.c
148 Description: perChan[] is an array of structures in tDec_Int_File. Made
149 corresponding changes.
151 Description: Included changes in interface for q_normalize() and
152 trans4m_freq_2_time_fxp.
154 Description: Included changes in interface for long_term_prediction.
156 Description: Added support for DSE (Data Streaming Channel). Added
157 function get_dse() and included file get_dse.h
159 Description: Added support for the ill-case when a raw data block contains
160 only a terminator <ID_END>. This is illegal but is added
163 Description: Added support for empty audio frames, such the one containing
164 only DSE or FILL elements. A trap was added to stop processing
165 when no audio information was sent.
167 Description: Added support for adts format files. Added saturation to
168 floating point version of aac+ decoding
172 ------------------------------------------------------------------------------
173 INPUT AND OUTPUT DEFINITIONS
176 pExt = pointer to the external interface structure. See the file
177 PVMP4AudioDecoder_API.h for a description of each field.
178 Data type of pointer to a tPVMP4AudioDecoderExternal
181 pMem = void pointer to hide the internal implementation of the library
182 It is cast back to a tDec_Int_File structure. This structure
183 contains information that needs to persist between calls to
184 this function, or is too big to be placed on the stack, even
185 though the data is only needed during execution of this function
186 Data type void pointer, internally pointer to a tDec_Int_File
189 Local Stores/Buffers/Pointers Needed: None
190 (The memory set aside in pMem performs this task)
192 Global Stores/Buffers/Pointers Needed: None
195 status = 0 if no error occurred
196 MP4AUDEC_NONRECOVERABLE if a non-recoverable error occurred
197 MP4AUDEC_RECOVERABLE if a recoverable error occurred.
198 Presently a recoverable error does not exist, but this
202 Pointers and Buffers Modified:
203 pMem contents are modified.
204 pExt: (more detail in the file PVMP4AudioDecoder_API.h)
205 inputBufferUsedLength - number of array elements used up by the stream.
206 remainderBits - remaining bits in the next UInt32 buffer
207 samplingRate - sampling rate in samples per sec
208 bitRate - bit rate in bits per second, varies frame to frame.
209 encodedChannels - channels found on the file (informative)
210 frameLength - length of the frame
212 Local Stores Modified: None.
214 Global Stores Modified: None.
216 ------------------------------------------------------------------------------
219 Decodes one frame of an MPEG-2/MPEG-4 encoded audio bitstream.
221 This function calls the various components of the decoder in the proper order.
224 Left Channel Right Channel
229 #1 ____________________ #2 ____________________
231 | Huffman Decoding | | Huffman Decoding |
232 |__________________| |__________________|
237 #3 ____________________ |
240 |__________________| |
245 #4 ______________________________________________________________________
248 |____________________________________________________________________|
252 #5 ____________________ |
255 |__________________| I
260 #6 ____________________ R
263 |__________________| E
268 #7 ____________________ H
271 |__________________| N
276 #8 ____________________ |
278 | Long Term Synth | |
279 |__________________| |
282 | #9 ____________________
284 |--DATA ON LEFT CHANNEL MAY BE USED----->| PNS/Intensity Rt |
285 | |__________________|
289 | #10 ____________________
292 I |__________________|
297 R #11 ____________________
300 I |__________________|
305 C #12 ____________________
308 N |__________________|
313 | #13 ____________________
315 | | Long Term Synth |
316 | |__________________|
321 #14 ____________________ #18 ____________________
324 |__________________| |__________________|
329 #15 ____________________ #19 ____________________
331 | qFormatNorm | | qFormatNorm |
332 |__________________| |__________________|
337 #16 ____________________ #20 ____________________
339 | Freq / Time | | Freq / Time |
340 |__________________| |__________________|
345 #17 ____________________ #21 ____________________
347 | Limit Buffer | | Limit Buffer |
348 |__________________| |__________________|
353 #22 ______________________________________________________________________
356 |____________________________________________________________________|
359 ------------------------------------------------------------------------------
362 PacketVideo Document # CCC-AUD-AAC-ERS-0003
364 ------------------------------------------------------------------------------
367 (1) MPEG-2 NBC Audio Decoder
368 "This software module was originally developed by AT&T, Dolby
369 Laboratories, Fraunhofer Gesellschaft IIS in the course of development
370 of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
371 3. This software module is an implementation of a part of one or more
372 MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
373 Audio standard. ISO/IEC gives users of the MPEG-2 NBC/MPEG-4 Audio
374 standards free license to this software module or modifications thereof
375 for use in hardware or software products claiming conformance to the
376 MPEG-2 NBC/MPEG-4 Audio standards. Those intending to use this software
377 module in hardware or software products are advised that this use may
378 infringe existing patents. The original developer of this software
379 module and his/her company, the subsequent editors and their companies,
380 and ISO/IEC have no liability for use of this software module or
381 modifications thereof in an implementation. Copyright is not released
382 for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
383 developer retains full right to use the code for his/her own purpose,
384 assign or donate the code to a third party and to inhibit third party
385 from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
386 This copyright notice must be included in all copies or derivative
390 ------------------------------------------------------------------------------
392 When the code is written for a specific target processor the
393 the resources used should be documented below.
395 STACK USAGE: [stack count for this module] + [variable to represent
396 stack usage for each subroutine called]
398 where: [stack usage variable] = stack usage for [subroutine
399 name] (see [filename].ext)
401 DATA MEMORY USED: x words
403 PROGRAM MEMORY USED: x words
405 CLOCK CYCLES: [cycle count equation for this module] + [variable
406 used to represent cycle count for each subroutine
409 where: [cycle count variable] = cycle count for [subroutine
410 name] (see [filename].ext)
412 ------------------------------------------------------------------------------
416 /*----------------------------------------------------------------------------
418 ----------------------------------------------------------------------------*/
419 #include "pv_audio_type_defs.h"
421 #include "s_tdec_int_chan.h"
422 #include "s_tdec_int_file.h"
423 #include "aac_mem_funcs.h"
424 #include "sfb.h" /* Where samp_rate_info[] is declared */
425 #include "e_tmp4audioobjecttype.h"
426 #include "e_elementid.h"
429 #include "get_adif_header.h"
430 #include "get_adts_header.h"
431 #include "get_audio_specific_config.h"
432 #include "ibstream.h" /* where getbits is declared */
434 #include "huffman.h" /* where huffdecode is declared */
435 #include "get_prog_config.h"
437 #include "pns_left.h"
439 #include "apply_ms_synt.h"
440 #include "pns_intensity_right.h"
441 #include "q_normalize.h"
442 #include "long_term_prediction.h"
443 #include "long_term_synthesis.h"
444 #include "ltp_common_internal.h"
445 #include "apply_tns.h"
447 #include "window_block_fxp.h"
449 #include "write_output.h"
451 #include "pvmp4audiodecoder_api.h" /* Where this function is declared */
454 #include "sbr_applied.h"
455 #include "sbr_open.h"
456 #include "get_sbr_bitstream.h"
457 #include "e_sbr_element_id.h"
461 /*----------------------------------------------------------------------------
463 ; Define module specific macros here
464 ----------------------------------------------------------------------------*/
466 /*----------------------------------------------------------------------------
468 ; Include all pre-processor statements here. Include conditional
469 ; compile variables also.
470 ----------------------------------------------------------------------------*/
476 /*----------------------------------------------------------------------------
477 ; LOCAL FUNCTION DEFINITIONS
478 ; Function Prototype declaration
479 ----------------------------------------------------------------------------*/
481 /*----------------------------------------------------------------------------
482 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
483 ; Variable declaration - defined here and used outside this module
484 ----------------------------------------------------------------------------*/
486 /*----------------------------------------------------------------------------
487 ; EXTERNAL FUNCTION REFERENCES
488 ; Declare functions defined elsewhere and referenced in this module
489 ----------------------------------------------------------------------------*/
491 void InitSbrSynFilterbank(bool bDownSampleSBR);
495 /*----------------------------------------------------------------------------
496 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
497 ; Declare variables used in this module but defined elsewhere
498 ----------------------------------------------------------------------------*/
500 /*----------------------------------------------------------------------------
502 ----------------------------------------------------------------------------*/
505 OSCL_EXPORT_REF Int PVMP4AudioDecodeFrame(
506 tPVMP4AudioDecoderExternal *pExt,
509 Int frameLength; /* Helper variable */
512 UInt initialUsedBits; /* Unsigned for C55x */
514 Int qPredictedSamples;
516 MC_Info *pMC_Info; /* Helper pointer */
517 FrameInfo *pFrameInfo; /* Helper pointer */
518 tDec_Int_File *pVars; /* Helper pointer */
519 tDec_Int_Chan *pChVars[Chans]; /* Helper pointer */
521 per_chan_share_w_fxpCoef *pChLeftShare; /* Helper pointer */
522 per_chan_share_w_fxpCoef *pChRightShare; /* Helper pointer */
524 Int status = MP4AUDEC_SUCCESS;
531 SBRDECODER_DATA *sbrDecoderData;
533 SBRBITSTREAM *sbrBitStream;
537 * Initialize "helper" pointers to existing memory.
539 pVars = (tDec_Int_File *)pMem;
541 pMC_Info = &pVars->mc_info;
543 pChVars[LEFT] = &pVars->perChan[LEFT];
544 pChVars[RIGHT] = &pVars->perChan[RIGHT];
546 pChLeftShare = pChVars[LEFT]->pShareWfxpCoef;
547 pChRightShare = pChVars[RIGHT]->pShareWfxpCoef;
552 sbrDecoderData = (SBRDECODER_DATA *) & pVars->sbrDecoderData;
553 sbrDec = (SBR_DEC *) & pVars->sbrDec;
554 sbrBitStream = (SBRBITSTREAM *) & pVars->sbrBitStr;
556 #ifdef PARAMETRICSTEREO
557 sbrDecoderData->hParametricStereoDec = (HANDLE_PS_DEC) & pVars->sbrDecoderData.ParametricStereoDec;
562 * Translate input buffer variables.
564 pVars->inputStream.pBuffer = pExt->pInputBuffer;
566 pVars->inputStream.inputBufferCurrentLength = (UInt)pExt->inputBufferCurrentLength;
568 pVars->inputStream.availableBits =
569 (UInt)(pExt->inputBufferCurrentLength << INBUF_ARRAY_INDEX_SHIFT);
572 (UInt)((pExt->inputBufferUsedLength << INBUF_ARRAY_INDEX_SHIFT) +
573 pExt->remainderBits);
575 pVars->inputStream.usedBits = initialUsedBits;
577 if (initialUsedBits > pVars->inputStream.availableBits)
579 status = MP4AUDEC_INVALID_FRAME;
581 else if (pVars->bno == 0)
584 * Attempt to read in ADIF format first because it is easily identified.
585 * If its not an ADIF bitstream, get_adif_header rewinds the "pointer"
586 * (actually usedBits).
591 &(pVars->scratch.scratch_prog_config));
593 byte_align(&pVars->inputStream);
595 if (status == SUCCESS)
597 pVars->prog_config.file_is_adts = FALSE;
599 else /* we've tried simple audio config, adif, then it should be adts */
601 pVars->prog_config.file_is_adts = TRUE;
604 else if ((pVars->bno == 1) && (pVars->prog_config.file_is_adts == FALSE))
608 * There might be an ID_END element following immediately after the
609 * AudioSpecificConfig header. This syntactic element should be read
610 * and byte_aligned before proceeds to decode "real" AAC raw data.
612 id_syn_ele = (Int)getbits(LEN_SE_ID, &pVars->inputStream) ;
614 if (id_syn_ele == ID_END)
617 byte_align(&pVars->inputStream);
619 pExt->inputBufferUsedLength =
620 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;
622 pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK;
631 * Rewind bitstream pointer so that the syntactic element can be
632 * read when decoding raw bitstream
634 pVars->inputStream.usedBits -= LEN_SE_ID;
639 if (pVars->prog_config.file_is_adts == TRUE)
642 * If file is adts format, let the decoder handle only on data raw
643 * block at the time, once the last (or only) data block has been
644 * processed, then synch on the next header
646 if (pVars->prog_config.headerless_frames)
648 pVars->prog_config.headerless_frames--; /* raw data block counter */
652 status = get_adts_header(pVars,
655 3); /* CorrectlyReadFramesCount */
657 if (status != SUCCESS)
659 status = MP4AUDEC_LOST_FRAME_SYNC; /* we lost track of header */
665 byte_align(&pVars->inputStream);
669 sbrBitStream->NrElements = 0;
670 sbrBitStream->NrElementsCore = 0;
675 * The variable leaveGetLoop is used to signal that the following
676 * loop can be left, which retrieves audio syntatic elements until
677 * an ID_END is found, or an error occurs.
679 leaveGetLoop = FALSE;
682 while ((leaveGetLoop == FALSE) && (status == SUCCESS))
684 /* get audio syntactic element */
685 id_syn_ele = (Int)get9_n_lessbits(LEN_SE_ID, &pVars->inputStream);
688 * As fractional frames are a possible input, check that parsing does not
689 * go beyond the available bits before parsing the syntax.
691 if (pVars->inputStream.usedBits > pVars->inputStream.availableBits)
693 status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */
694 id_syn_ele = ID_END; /* quit while-loop */
699 case ID_END: /* terminator field */
703 case ID_SCE: /* single channel */
704 case ID_CPE: /* channel pair */
709 &(pVars->inputStream),
714 if (id_syn_ele == ID_SCE)
716 sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_SCE;
718 else if (id_syn_ele == ID_CPE)
720 sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_CPE;
722 sbrBitStream->NrElementsCore++;
729 case ID_PCE: /* program config element */
731 * PCE are not accepted in the middle of a
732 * raw_data_block. If found, a possible error may happen
733 * If a PCE is encountered during the first 2 frames,
734 * it will be read and accepted
735 * if its tag matches the first, with no error checking
736 * (inside of get_prog_config)
741 status = get_prog_config(pVars,
742 &(pVars->scratch.scratch_prog_config));
746 status = MP4AUDEC_INVALID_FRAME;
750 case ID_FIL: /* fill element */
752 get_sbr_bitstream(sbrBitStream, &pVars->inputStream);
755 getfill(&pVars->inputStream);
760 case ID_DSE: /* Data Streaming element */
761 get_dse(pVars->share.data_stream_bytes,
762 &pVars->inputStream);
765 default: /* Unsupported element, including ID_LFE */
766 status = -1; /* ERROR CODE needs to be updated */
773 byte_align(&pVars->inputStream);
776 * After parsing the first frame ( bno=0 (adif), bno=1 (raw))
777 * verify if implicit signalling is forcing to upsample AAC with
778 * no AAC+/eAAC+ content. If so, disable upsampling
784 if ((pVars->mc_info.ExtendedAudioObjectType == MP4AUDIO_AAC_LC) &&
785 (!sbrBitStream->NrElements))
787 PVMP4AudioDecoderDisableAacPlus(pExt, pMem);
793 * There might be an empty raw data block with only a
794 * ID_END element or non audio ID_DSE, ID_FIL
795 * This is an "illegal" condition but this trap
796 * avoids any further processing
799 if (empty_frame == TRUE)
801 pExt->inputBufferUsedLength =
802 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;
804 pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK;
814 if (sbrBitStream->NrElements)
816 /* for every core SCE or CPE there must be an SBR element, otherwise sths. wrong */
817 if (sbrBitStream->NrElements != sbrBitStream->NrElementsCore)
819 status = MP4AUDEC_INVALID_FRAME;
822 if (pExt->aacPlusEnabled == false)
824 sbrBitStream->NrElements = 0; /* disable aac processing */
830 * This is AAC, but if aac+/eaac+ was declared in the stream, and there is not sbr content
833 if (pMC_Info->sbrPresentFlag || pMC_Info->psPresentFlag)
835 status = MP4AUDEC_INVALID_FRAME;
844 * Signal processing section.
846 frameLength = pVars->frameLength;
848 if (status == SUCCESS)
851 * PNS and INTENSITY STEREO and MS
854 pFrameInfo = pVars->winmap[pChVars[LEFT]->wnd];
859 pChLeftShare->cb_map,
860 pChLeftShare->factors,
861 pChLeftShare->lt_status.sfb_prediction_used,
862 pChLeftShare->lt_status.ltp_data_present,
863 pChVars[LEFT]->fxpCoef,
864 pChLeftShare->qFormat,
865 &(pVars->pns_cur_noise_state));
868 * apply_ms_synt can only be ran for common windows.
869 * (where both the left and right channel share the
870 * same grouping, window length, etc.
872 * pVars->hasmask will be > 0 only if
873 * common windows are enabled for this frame.
876 if (pVars->hasmask > 0)
882 pChLeftShare->cb_map,
883 pChVars[LEFT]->fxpCoef,
884 pChVars[RIGHT]->fxpCoef,
885 pChLeftShare->qFormat,
886 pChRightShare->qFormat);
889 for (ch = 0; (ch < pMC_Info->nch); ch++)
891 pFrameInfo = pVars->winmap[pChVars[ch]->wnd];
894 * Note: This MP4 library assumes that if there are two channels,
895 * then the second channel is right AND it was a coupled channel,
896 * therefore there is no need to check the "is_cpe" flag.
904 pChRightShare->group,
906 pChRightShare->cb_map,
907 pChLeftShare->factors,
908 pChRightShare->factors,
909 pChRightShare->lt_status.sfb_prediction_used,
910 pChRightShare->lt_status.ltp_data_present,
911 pChVars[LEFT]->fxpCoef,
912 pChVars[RIGHT]->fxpCoef,
913 pChLeftShare->qFormat,
914 pChRightShare->qFormat,
915 &(pVars->pns_cur_noise_state));
918 if (pChVars[ch]->pShareWfxpCoef->lt_status.ltp_data_present != FALSE)
921 * LTP - Long Term Prediction
924 qPredictedSamples = long_term_prediction(
926 pChVars[ch]->pShareWfxpCoef->lt_status.
928 pChVars[ch]->pShareWfxpCoef->lt_status.
930 pChVars[ch]->ltp_buffer,
931 pVars->ltp_buffer_state,
932 pChVars[ch]->time_quant,
933 pVars->share.predictedSamples, /* Scratch */
936 trans4m_time_2_freq_fxp(
937 pVars->share.predictedSamples,
939 pChVars[ch]->wnd_shape_prev_bk,
940 pChVars[ch]->wnd_shape_this_bk,
942 pVars->scratch.fft); /* scratch memory for FFT */
946 * To solve a potential problem where a pointer tied to
947 * the qFormat was being incremented, a pointer to
948 * pChVars[ch]->qFormat is passed in here rather than
949 * the address of qPredictedSamples.
951 * Neither values are actually needed in the case of
952 * inverse filtering, but the pointer was being
953 * passed (and incremented) regardless.
955 * So, the solution is to pass a space of memory
956 * that a pointer can happily point to.
959 /* This is the inverse filter */
961 pVars->share.predictedSamples, /* scratch re-used for each ch */
962 pChVars[ch]->pShareWfxpCoef->qFormat, /* Not used by the inv_filter */
964 &(pChVars[ch]->pShareWfxpCoef->tns),
965 TRUE, /* TRUE is FIR */
966 pVars->scratch.tns_inv_filter);
969 * For the next function long_term_synthesis,
970 * the third param win_sfb_top[], and
971 * the tenth param coef_per_win,
972 * are used differently that in the rest of the project. This
973 * is because originally the ISO code was going to have
974 * these parameters change as the "short window" changed.
975 * These are all now the same value for each of the eight
976 * windows. This is why there is a [0] at the
977 * end of each of theses parameters.
978 * Note in particular that win_sfb_top was originally an
979 * array of pointers to arrays, but inside long_term_synthesis
980 * it is now a simple array.
981 * When the rest of the project functions are changed, the
982 * structure FrameInfo changes, and the [0]'s are removed,
983 * this comment could go away.
987 pChVars[ch]->pShareWfxpCoef->max_sfb,
988 pFrameInfo->win_sfb_top[0], /* Look above */
989 pChVars[ch]->pShareWfxpCoef->lt_status.win_prediction_used,
990 pChVars[ch]->pShareWfxpCoef->lt_status.sfb_prediction_used,
991 pChVars[ch]->fxpCoef, /* input and output */
992 pChVars[ch]->pShareWfxpCoef->qFormat, /* input and output */
993 pVars->share.predictedSamples,
994 qPredictedSamples, /* q format for previous aray */
995 pFrameInfo->coef_per_win[0], /* Look above */
997 NUM_RECONSTRUCTED_SFB);
999 } /* end if (pChVars[ch]->lt_status.ltp_data_present != FALSE) */
1003 for (ch = 0; (ch < pMC_Info->nch); ch++)
1006 pFrameInfo = pVars->winmap[pChVars[ch]->wnd];
1009 * TNS - Temporal Noise Shaping
1012 /* This is the forward filter
1014 * A special note: Scratch memory is not used by
1015 * the forward filter, but is passed in to maintain
1016 * common interface for inverse and forward filter
1019 pChVars[ch]->fxpCoef,
1020 pChVars[ch]->pShareWfxpCoef->qFormat,
1022 &(pChVars[ch]->pShareWfxpCoef->tns),
1023 FALSE, /* FALSE is IIR */
1024 pVars->scratch.tns_inv_filter);
1027 * Normalize the q format across all scale factor bands
1032 pChVars[ch]->pShareWfxpCoef->qFormat,
1034 pChVars[ch]->abs_max_per_window,
1035 pChVars[ch]->fxpCoef);
1038 * filterbank - converts frequency coeficients to time domain.
1042 if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1)
1044 trans4m_freq_2_time_fxp_2(
1045 pChVars[ch]->fxpCoef,
1046 pChVars[ch]->time_quant,
1047 pChVars[ch]->wnd, /* window sequence */
1048 pChVars[ch]->wnd_shape_prev_bk,
1049 pChVars[ch]->wnd_shape_this_bk,
1051 pChVars[ch]->abs_max_per_window,
1053 &pExt->pOutputBuffer[ch]);
1055 * Update LTP buffers if needed
1058 if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP)
1060 Int16 * pt = &pExt->pOutputBuffer[ch];
1061 Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]);
1063 for (Int32 i = HALF_LONG_WINDOW; i != 0; i--)
1076 trans4m_freq_2_time_fxp_1(
1077 pChVars[ch]->fxpCoef,
1078 pChVars[ch]->time_quant,
1079 &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state + 288]),
1080 pChVars[ch]->wnd, /* window sequence */
1081 pChVars[ch]->wnd_shape_prev_bk,
1082 pChVars[ch]->wnd_shape_this_bk,
1084 pChVars[ch]->abs_max_per_window,
1085 pVars->scratch.fft);
1090 trans4m_freq_2_time_fxp_2(
1091 pChVars[ch]->fxpCoef,
1092 pChVars[ch]->time_quant,
1093 pChVars[ch]->wnd, /* window sequence */
1094 pChVars[ch]->wnd_shape_prev_bk,
1095 pChVars[ch]->wnd_shape_this_bk,
1097 pChVars[ch]->abs_max_per_window,
1099 &pExt->pOutputBuffer[ch]);
1101 * Update LTP buffers only if needed
1104 if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP)
1106 Int16 * pt = &pExt->pOutputBuffer[ch];
1107 Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]);
1109 for (Int32 i = HALF_LONG_WINDOW; i != 0; i--)
1125 /* Update the window shape */
1126 pChVars[ch]->wnd_shape_prev_bk = pChVars[ch]->wnd_shape_this_bk;
1132 * Copy to the final output buffer, taking into account the desired
1133 * channels from the calling environment, the actual channels, and
1134 * whether the data should be interleaved or not.
1136 * If the stream had only one channel, write_output will not use
1137 * the right channel data.
1142 /* CONSIDER USE OF DMA OPTIMIZATIONS WITHIN THE write_output FUNCTION.
1144 * It is presumed that the ltp_buffer will reside in internal (fast)
1145 * memory, while the pExt->pOutputBuffer will reside in external
1153 if (sbrBitStream->NrElements || pMC_Info->upsamplingFactor == 2)
1156 if (pVars->bno <= 1) /* allows console to operate with ADIF and audio config */
1158 if (sbrDec->outSampleRate == 0) /* do it only once (disregarding of signaling type) */
1160 sbr_open(samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate,
1163 pVars->mc_info.bDownSampledSbr);
1167 pMC_Info->upsamplingFactor =
1168 sbrDecoderData->SbrChannel[0].frameData.sbr_header.sampleRateMode;
1171 /* reuse right aac spectrum channel */
1173 Int16 *pt_left = &(pChVars[LEFT ]->ltp_buffer[pVars->ltp_buffer_state]);
1174 Int16 *pt_right = &(pChVars[RIGHT]->ltp_buffer[pVars->ltp_buffer_state]);
1176 if (sbr_applied(sbrDecoderData,
1180 pExt->pOutputBuffer,
1183 pMC_Info->nch) != SBRDEC_OK)
1185 status = MP4AUDEC_INVALID_FRAME;
1190 } /* if( pExt->aacPlusEnabled == FALSE) */
1194 * Copied mono data in both channels or just leave it as mono,
1195 * according with desiredChannels (default is 2)
1198 if (pExt->desiredChannels == 2)
1201 #if defined(AAC_PLUS)
1202 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)
1203 if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1)
1205 if (pMC_Info->nch != 2)
1208 if (pMC_Info->nch != 2)
1214 Int16 * pt = &pExt->pOutputBuffer[0];
1215 Int16 * pt2 = &pExt->pOutputBuffer[1];
1217 if (pMC_Info->upsamplingFactor == 2)
1219 for (i = 0; i < 1024; i++)
1225 pt = &pExt->pOutputBuffer_plus[0];
1226 pt2 = &pExt->pOutputBuffer_plus[1];
1228 for (i = 0; i < 1024; i++)
1237 for (i = 0; i < 1024; i++)
1247 #if defined(AAC_PLUS)
1248 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)
1250 else if (pMC_Info->psPresentFlag == 1)
1252 Int32 frameSize = 0;
1253 if (pExt->aacPlusEnabled == false)
1256 * Decoding eaac+ when only aac is enabled, copy L into R
1260 else if (sbrDecoderData->SbrChannel[0].syncState != SBR_ACTIVE)
1263 * Decoding eaac+ when no PS data was found, copy upsampled L into R
1268 Int16 * pt = &pExt->pOutputBuffer[0];
1269 Int16 * pt2 = &pExt->pOutputBuffer[1];
1271 for (i = 0; i < frameSize; i++)
1285 #if defined(AAC_PLUS)
1286 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)
1287 if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1)
1289 if (pMC_Info->nch != 2)
1292 if (pMC_Info->nch != 2)
1296 Int16 * pt = &pExt->pOutputBuffer[0];
1297 Int16 * pt2 = &pExt->pOutputBuffer[0];
1300 if (pMC_Info->upsamplingFactor == 2)
1302 for (i = 0; i < 1024; i++)
1308 pt = &pExt->pOutputBuffer_plus[0];
1309 pt2 = &pExt->pOutputBuffer_plus[0];
1311 for (i = 0; i < 1024; i++)
1319 for (i = 0; i < 1024; i++)
1333 /* pVars->ltp_buffer_state cycles between 0 and 1024. The value
1334 * indicates the location of the data corresponding to t == -2.
1336 * | t == -2 | t == -1 | pVars->ltp_buffer_state == 0
1338 * | t == -1 | t == -2 | pVars->ltp_buffer_state == 1024
1343 if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1)
1345 pVars->ltp_buffer_state ^= frameLength;
1349 pVars->ltp_buffer_state ^= (frameLength + 288);
1352 pVars->ltp_buffer_state ^= frameLength;
1356 if (pVars->bno <= 1)
1359 * to set these values only during the second call
1362 pExt->samplingRate =
1363 samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate;
1365 pVars->mc_info.implicit_channeling = 0; /* disable flag, as this is allowed
1366 * only the first time
1372 if (pMC_Info->upsamplingFactor == 2)
1374 pExt->samplingRate *= pMC_Info->upsamplingFactor;
1375 pExt->aacPlusUpsamplingFactor = pMC_Info->upsamplingFactor;
1380 pExt->extendedAudioObjectType = pMC_Info->ExtendedAudioObjectType;
1381 pExt->audioObjectType = pMC_Info->audioObjectType;
1383 pExt->encodedChannels = pMC_Info->nch;
1384 pExt->frameLength = pVars->frameLength;
1391 * Using unit analysis, the bitrate is a function of the sampling rate, bits,
1394 * bits samples frame
1395 * ---- = --------- * bits * -------
1398 * To save a divide, a shift is used. Presently only the value of
1399 * 1024 is used by this library, so make it the most accurate for that
1400 * value. This may need to be updated later.
1403 pExt->bitRate = (pExt->samplingRate *
1404 (pVars->inputStream.usedBits - initialUsedBits)) >> 10; /* LONG_WINDOW 1024 */
1406 pExt->bitRate >>= (pMC_Info->upsamplingFactor - 1);
1409 } /* end if (status == SUCCESS) */
1412 if (status != MP4AUDEC_SUCCESS)
1415 * A non-SUCCESS decoding could be due to an error on the bitstream or
1416 * an incomplete frame. As access to the bitstream beyond frame boundaries
1417 * are not allowed, in those cases the bitstream reading routine return a 0
1418 * Zero values guarantees that the data structures are filled in with values
1419 * that eventually will signal an error (like invalid parameters) or that allow
1420 * completion of the parsing routine. Either way, the partial frame condition
1421 * is verified at this time.
1423 if (pVars->prog_config.file_is_adts == TRUE)
1425 status = MP4AUDEC_LOST_FRAME_SYNC;
1426 pVars->prog_config.headerless_frames = 0; /* synchronization forced */
1431 * Check if the decoding error was due to buffer overrun, if it was,
1434 if (pVars->inputStream.usedBits > pVars->inputStream.availableBits)
1436 /* all bits were used but were not enough to complete decoding */
1437 pVars->inputStream.usedBits = pVars->inputStream.availableBits;
1439 status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */
1445 * Translate from units of bits back into units of words.
1448 pExt->inputBufferUsedLength =
1449 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;
1451 pExt->remainderBits = (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK);
1457 } /* PVMP4AudioDecoderDecodeFrame */