1*e5436536SAndroid Build Coastguard Worker /* -----------------------------------------------------------------------------
2*e5436536SAndroid Build Coastguard Worker Software License for The Fraunhofer FDK AAC Codec Library for Android
3*e5436536SAndroid Build Coastguard Worker
4*e5436536SAndroid Build Coastguard Worker © Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5*e5436536SAndroid Build Coastguard Worker Forschung e.V. All rights reserved.
6*e5436536SAndroid Build Coastguard Worker
7*e5436536SAndroid Build Coastguard Worker 1. INTRODUCTION
8*e5436536SAndroid Build Coastguard Worker The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9*e5436536SAndroid Build Coastguard Worker that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10*e5436536SAndroid Build Coastguard Worker scheme for digital audio. This FDK AAC Codec software is intended to be used on
11*e5436536SAndroid Build Coastguard Worker a wide variety of Android devices.
12*e5436536SAndroid Build Coastguard Worker
13*e5436536SAndroid Build Coastguard Worker AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14*e5436536SAndroid Build Coastguard Worker general perceptual audio codecs. AAC-ELD is considered the best-performing
15*e5436536SAndroid Build Coastguard Worker full-bandwidth communications codec by independent studies and is widely
16*e5436536SAndroid Build Coastguard Worker deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17*e5436536SAndroid Build Coastguard Worker specifications.
18*e5436536SAndroid Build Coastguard Worker
19*e5436536SAndroid Build Coastguard Worker Patent licenses for necessary patent claims for the FDK AAC Codec (including
20*e5436536SAndroid Build Coastguard Worker those of Fraunhofer) may be obtained through Via Licensing
21*e5436536SAndroid Build Coastguard Worker (www.vialicensing.com) or through the respective patent owners individually for
22*e5436536SAndroid Build Coastguard Worker the purpose of encoding or decoding bit streams in products that are compliant
23*e5436536SAndroid Build Coastguard Worker with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24*e5436536SAndroid Build Coastguard Worker Android devices already license these patent claims through Via Licensing or
25*e5436536SAndroid Build Coastguard Worker directly from the patent owners, and therefore FDK AAC Codec software may
26*e5436536SAndroid Build Coastguard Worker already be covered under those patent licenses when it is used for those
27*e5436536SAndroid Build Coastguard Worker licensed purposes only.
28*e5436536SAndroid Build Coastguard Worker
29*e5436536SAndroid Build Coastguard Worker Commercially-licensed AAC software libraries, including floating-point versions
30*e5436536SAndroid Build Coastguard Worker with enhanced sound quality, are also available from Fraunhofer. Users are
31*e5436536SAndroid Build Coastguard Worker encouraged to check the Fraunhofer website for additional applications
32*e5436536SAndroid Build Coastguard Worker information and documentation.
33*e5436536SAndroid Build Coastguard Worker
34*e5436536SAndroid Build Coastguard Worker 2. COPYRIGHT LICENSE
35*e5436536SAndroid Build Coastguard Worker
36*e5436536SAndroid Build Coastguard Worker Redistribution and use in source and binary forms, with or without modification,
37*e5436536SAndroid Build Coastguard Worker are permitted without payment of copyright license fees provided that you
38*e5436536SAndroid Build Coastguard Worker satisfy the following conditions:
39*e5436536SAndroid Build Coastguard Worker
40*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in redistributions of
41*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec or your modifications thereto in source code form.
42*e5436536SAndroid Build Coastguard Worker
43*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in the documentation
44*e5436536SAndroid Build Coastguard Worker and/or other materials provided with redistributions of the FDK AAC Codec or
45*e5436536SAndroid Build Coastguard Worker your modifications thereto in binary form. You must make available free of
46*e5436536SAndroid Build Coastguard Worker charge copies of the complete source code of the FDK AAC Codec and your
47*e5436536SAndroid Build Coastguard Worker modifications thereto to recipients of copies in binary form.
48*e5436536SAndroid Build Coastguard Worker
49*e5436536SAndroid Build Coastguard Worker The name of Fraunhofer may not be used to endorse or promote products derived
50*e5436536SAndroid Build Coastguard Worker from this library without prior written permission.
51*e5436536SAndroid Build Coastguard Worker
52*e5436536SAndroid Build Coastguard Worker You may not charge copyright license fees for anyone to use, copy or distribute
53*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec software or your modifications thereto.
54*e5436536SAndroid Build Coastguard Worker
55*e5436536SAndroid Build Coastguard Worker Your modified versions of the FDK AAC Codec must carry prominent notices stating
56*e5436536SAndroid Build Coastguard Worker that you changed the software and the date of any change. For modified versions
57*e5436536SAndroid Build Coastguard Worker of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58*e5436536SAndroid Build Coastguard Worker must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59*e5436536SAndroid Build Coastguard Worker AAC Codec Library for Android."
60*e5436536SAndroid Build Coastguard Worker
61*e5436536SAndroid Build Coastguard Worker 3. NO PATENT LICENSE
62*e5436536SAndroid Build Coastguard Worker
63*e5436536SAndroid Build Coastguard Worker NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64*e5436536SAndroid Build Coastguard Worker limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65*e5436536SAndroid Build Coastguard Worker Fraunhofer provides no warranty of patent non-infringement with respect to this
66*e5436536SAndroid Build Coastguard Worker software.
67*e5436536SAndroid Build Coastguard Worker
68*e5436536SAndroid Build Coastguard Worker You may use this FDK AAC Codec software or modifications thereto only for
69*e5436536SAndroid Build Coastguard Worker purposes that are authorized by appropriate patent licenses.
70*e5436536SAndroid Build Coastguard Worker
71*e5436536SAndroid Build Coastguard Worker 4. DISCLAIMER
72*e5436536SAndroid Build Coastguard Worker
73*e5436536SAndroid Build Coastguard Worker This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74*e5436536SAndroid Build Coastguard Worker holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75*e5436536SAndroid Build Coastguard Worker including but not limited to the implied warranties of merchantability and
76*e5436536SAndroid Build Coastguard Worker fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77*e5436536SAndroid Build Coastguard Worker CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78*e5436536SAndroid Build Coastguard Worker or consequential damages, including but not limited to procurement of substitute
79*e5436536SAndroid Build Coastguard Worker goods or services; loss of use, data, or profits, or business interruption,
80*e5436536SAndroid Build Coastguard Worker however caused and on any theory of liability, whether in contract, strict
81*e5436536SAndroid Build Coastguard Worker liability, or tort (including negligence), arising in any way out of the use of
82*e5436536SAndroid Build Coastguard Worker this software, even if advised of the possibility of such damage.
83*e5436536SAndroid Build Coastguard Worker
84*e5436536SAndroid Build Coastguard Worker 5. CONTACT INFORMATION
85*e5436536SAndroid Build Coastguard Worker
86*e5436536SAndroid Build Coastguard Worker Fraunhofer Institute for Integrated Circuits IIS
87*e5436536SAndroid Build Coastguard Worker Attention: Audio and Multimedia Departments - FDK AAC LL
88*e5436536SAndroid Build Coastguard Worker Am Wolfsmantel 33
89*e5436536SAndroid Build Coastguard Worker 91058 Erlangen, Germany
90*e5436536SAndroid Build Coastguard Worker
91*e5436536SAndroid Build Coastguard Worker www.iis.fraunhofer.de/amm
92*e5436536SAndroid Build Coastguard Worker [email protected]
93*e5436536SAndroid Build Coastguard Worker ----------------------------------------------------------------------------- */
94*e5436536SAndroid Build Coastguard Worker
95*e5436536SAndroid Build Coastguard Worker /**************************** AAC encoder library ******************************
96*e5436536SAndroid Build Coastguard Worker
97*e5436536SAndroid Build Coastguard Worker Author(s): M.Werner
98*e5436536SAndroid Build Coastguard Worker
99*e5436536SAndroid Build Coastguard Worker Description: Noiseless coder module
100*e5436536SAndroid Build Coastguard Worker
101*e5436536SAndroid Build Coastguard Worker *******************************************************************************/
102*e5436536SAndroid Build Coastguard Worker
103*e5436536SAndroid Build Coastguard Worker #include "dyn_bits.h"
104*e5436536SAndroid Build Coastguard Worker #include "bit_cnt.h"
105*e5436536SAndroid Build Coastguard Worker #include "psy_const.h"
106*e5436536SAndroid Build Coastguard Worker #include "aacenc_pns.h"
107*e5436536SAndroid Build Coastguard Worker #include "aacEnc_ram.h"
108*e5436536SAndroid Build Coastguard Worker #include "aacEnc_rom.h"
109*e5436536SAndroid Build Coastguard Worker
110*e5436536SAndroid Build Coastguard Worker typedef INT (*lookUpTable)[CODE_BOOK_ESC_NDX + 1];
111*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_getSideInfoBits(const SECTION_INFO * const huffsection,const SHORT * const sideInfoTab,const INT useHCR)112*e5436536SAndroid Build Coastguard Worker static INT FDKaacEnc_getSideInfoBits(const SECTION_INFO* const huffsection,
113*e5436536SAndroid Build Coastguard Worker const SHORT* const sideInfoTab,
114*e5436536SAndroid Build Coastguard Worker const INT useHCR) {
115*e5436536SAndroid Build Coastguard Worker INT sideInfoBits;
116*e5436536SAndroid Build Coastguard Worker
117*e5436536SAndroid Build Coastguard Worker if (useHCR &&
118*e5436536SAndroid Build Coastguard Worker ((huffsection->codeBook == 11) || (huffsection->codeBook >= 16))) {
119*e5436536SAndroid Build Coastguard Worker sideInfoBits = 5;
120*e5436536SAndroid Build Coastguard Worker } else {
121*e5436536SAndroid Build Coastguard Worker sideInfoBits = sideInfoTab[huffsection->sfbCnt];
122*e5436536SAndroid Build Coastguard Worker }
123*e5436536SAndroid Build Coastguard Worker
124*e5436536SAndroid Build Coastguard Worker return (sideInfoBits);
125*e5436536SAndroid Build Coastguard Worker }
126*e5436536SAndroid Build Coastguard Worker
127*e5436536SAndroid Build Coastguard Worker /* count bits using all possible tables */
FDKaacEnc_buildBitLookUp(const SHORT * const quantSpectrum,const INT maxSfb,const INT * const sfbOffset,const UINT * const sfbMax,INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],SECTION_INFO * const huffsection)128*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_buildBitLookUp(
129*e5436536SAndroid Build Coastguard Worker const SHORT* const quantSpectrum, const INT maxSfb,
130*e5436536SAndroid Build Coastguard Worker const INT* const sfbOffset, const UINT* const sfbMax,
131*e5436536SAndroid Build Coastguard Worker INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1],
132*e5436536SAndroid Build Coastguard Worker SECTION_INFO* const huffsection) {
133*e5436536SAndroid Build Coastguard Worker INT i, sfbWidth;
134*e5436536SAndroid Build Coastguard Worker
135*e5436536SAndroid Build Coastguard Worker for (i = 0; i < maxSfb; i++) {
136*e5436536SAndroid Build Coastguard Worker huffsection[i].sfbCnt = 1;
137*e5436536SAndroid Build Coastguard Worker huffsection[i].sfbStart = i;
138*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits = INVALID_BITCOUNT;
139*e5436536SAndroid Build Coastguard Worker huffsection[i].codeBook = -1;
140*e5436536SAndroid Build Coastguard Worker sfbWidth = sfbOffset[i + 1] - sfbOffset[i];
141*e5436536SAndroid Build Coastguard Worker FDKaacEnc_bitCount(quantSpectrum + sfbOffset[i], sfbWidth, sfbMax[i],
142*e5436536SAndroid Build Coastguard Worker bitLookUp[i]);
143*e5436536SAndroid Build Coastguard Worker }
144*e5436536SAndroid Build Coastguard Worker }
145*e5436536SAndroid Build Coastguard Worker
146*e5436536SAndroid Build Coastguard Worker /* essential helper functions */
FDKaacEnc_findBestBook(const INT * const bc,INT * const book,const INT useVCB11)147*e5436536SAndroid Build Coastguard Worker static inline INT FDKaacEnc_findBestBook(const INT* const bc, INT* const book,
148*e5436536SAndroid Build Coastguard Worker const INT useVCB11) {
149*e5436536SAndroid Build Coastguard Worker INT minBits = INVALID_BITCOUNT, j;
150*e5436536SAndroid Build Coastguard Worker
151*e5436536SAndroid Build Coastguard Worker int end = CODE_BOOK_ESC_NDX;
152*e5436536SAndroid Build Coastguard Worker
153*e5436536SAndroid Build Coastguard Worker for (j = 0; j <= end; j++) {
154*e5436536SAndroid Build Coastguard Worker if (bc[j] < minBits) {
155*e5436536SAndroid Build Coastguard Worker minBits = bc[j];
156*e5436536SAndroid Build Coastguard Worker *book = j;
157*e5436536SAndroid Build Coastguard Worker }
158*e5436536SAndroid Build Coastguard Worker }
159*e5436536SAndroid Build Coastguard Worker return (minBits);
160*e5436536SAndroid Build Coastguard Worker }
161*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_findMinMergeBits(const INT * const bc1,const INT * const bc2,const INT useVCB11)162*e5436536SAndroid Build Coastguard Worker static inline INT FDKaacEnc_findMinMergeBits(const INT* const bc1,
163*e5436536SAndroid Build Coastguard Worker const INT* const bc2,
164*e5436536SAndroid Build Coastguard Worker const INT useVCB11) {
165*e5436536SAndroid Build Coastguard Worker INT minBits = INVALID_BITCOUNT, j;
166*e5436536SAndroid Build Coastguard Worker
167*e5436536SAndroid Build Coastguard Worker DWORD_ALIGNED(bc1);
168*e5436536SAndroid Build Coastguard Worker DWORD_ALIGNED(bc2);
169*e5436536SAndroid Build Coastguard Worker
170*e5436536SAndroid Build Coastguard Worker for (j = 0; j <= CODE_BOOK_ESC_NDX; j++) {
171*e5436536SAndroid Build Coastguard Worker minBits = fixMin(minBits, bc1[j] + bc2[j]);
172*e5436536SAndroid Build Coastguard Worker }
173*e5436536SAndroid Build Coastguard Worker return (minBits);
174*e5436536SAndroid Build Coastguard Worker }
175*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_mergeBitLookUp(INT * const RESTRICT bc1,const INT * const RESTRICT bc2)176*e5436536SAndroid Build Coastguard Worker static inline void FDKaacEnc_mergeBitLookUp(INT* const RESTRICT bc1,
177*e5436536SAndroid Build Coastguard Worker const INT* const RESTRICT bc2) {
178*e5436536SAndroid Build Coastguard Worker int j;
179*e5436536SAndroid Build Coastguard Worker
180*e5436536SAndroid Build Coastguard Worker for (j = 0; j <= CODE_BOOK_ESC_NDX; j++) {
181*e5436536SAndroid Build Coastguard Worker FDK_ASSERT(INVALID_BITCOUNT == 0x1FFFFFFF);
182*e5436536SAndroid Build Coastguard Worker bc1[j] = fixMin(bc1[j] + bc2[j], INVALID_BITCOUNT);
183*e5436536SAndroid Build Coastguard Worker }
184*e5436536SAndroid Build Coastguard Worker }
185*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_findMaxMerge(const INT * const mergeGainLookUp,const SECTION_INFO * const huffsection,const INT maxSfb,INT * const maxNdx)186*e5436536SAndroid Build Coastguard Worker static inline INT FDKaacEnc_findMaxMerge(const INT* const mergeGainLookUp,
187*e5436536SAndroid Build Coastguard Worker const SECTION_INFO* const huffsection,
188*e5436536SAndroid Build Coastguard Worker const INT maxSfb, INT* const maxNdx) {
189*e5436536SAndroid Build Coastguard Worker INT i, maxMergeGain = 0;
190*e5436536SAndroid Build Coastguard Worker int lastMaxNdx = 0;
191*e5436536SAndroid Build Coastguard Worker
192*e5436536SAndroid Build Coastguard Worker for (i = 0; i + huffsection[i].sfbCnt < maxSfb; i += huffsection[i].sfbCnt) {
193*e5436536SAndroid Build Coastguard Worker if (mergeGainLookUp[i] > maxMergeGain) {
194*e5436536SAndroid Build Coastguard Worker maxMergeGain = mergeGainLookUp[i];
195*e5436536SAndroid Build Coastguard Worker lastMaxNdx = i;
196*e5436536SAndroid Build Coastguard Worker }
197*e5436536SAndroid Build Coastguard Worker }
198*e5436536SAndroid Build Coastguard Worker *maxNdx = lastMaxNdx;
199*e5436536SAndroid Build Coastguard Worker return (maxMergeGain);
200*e5436536SAndroid Build Coastguard Worker }
201*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_CalcMergeGain(const SECTION_INFO * const huffsection,const INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],const SHORT * const sideInfoTab,const INT ndx1,const INT ndx2,const INT useVCB11)202*e5436536SAndroid Build Coastguard Worker static inline INT FDKaacEnc_CalcMergeGain(
203*e5436536SAndroid Build Coastguard Worker const SECTION_INFO* const huffsection,
204*e5436536SAndroid Build Coastguard Worker const INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1],
205*e5436536SAndroid Build Coastguard Worker const SHORT* const sideInfoTab, const INT ndx1, const INT ndx2,
206*e5436536SAndroid Build Coastguard Worker const INT useVCB11) {
207*e5436536SAndroid Build Coastguard Worker INT MergeGain, MergeBits, SplitBits;
208*e5436536SAndroid Build Coastguard Worker
209*e5436536SAndroid Build Coastguard Worker MergeBits =
210*e5436536SAndroid Build Coastguard Worker sideInfoTab[huffsection[ndx1].sfbCnt + huffsection[ndx2].sfbCnt] +
211*e5436536SAndroid Build Coastguard Worker FDKaacEnc_findMinMergeBits(bitLookUp[ndx1], bitLookUp[ndx2], useVCB11);
212*e5436536SAndroid Build Coastguard Worker SplitBits =
213*e5436536SAndroid Build Coastguard Worker huffsection[ndx1].sectionBits +
214*e5436536SAndroid Build Coastguard Worker huffsection[ndx2].sectionBits; /* Bit amount for splitted huffsections */
215*e5436536SAndroid Build Coastguard Worker MergeGain = SplitBits - MergeBits;
216*e5436536SAndroid Build Coastguard Worker
217*e5436536SAndroid Build Coastguard Worker if ((huffsection[ndx1].codeBook == CODE_BOOK_PNS_NO) ||
218*e5436536SAndroid Build Coastguard Worker (huffsection[ndx2].codeBook == CODE_BOOK_PNS_NO) ||
219*e5436536SAndroid Build Coastguard Worker (huffsection[ndx1].codeBook == CODE_BOOK_IS_OUT_OF_PHASE_NO) ||
220*e5436536SAndroid Build Coastguard Worker (huffsection[ndx2].codeBook == CODE_BOOK_IS_OUT_OF_PHASE_NO) ||
221*e5436536SAndroid Build Coastguard Worker (huffsection[ndx1].codeBook == CODE_BOOK_IS_IN_PHASE_NO) ||
222*e5436536SAndroid Build Coastguard Worker (huffsection[ndx2].codeBook == CODE_BOOK_IS_IN_PHASE_NO)) {
223*e5436536SAndroid Build Coastguard Worker MergeGain = -1;
224*e5436536SAndroid Build Coastguard Worker }
225*e5436536SAndroid Build Coastguard Worker
226*e5436536SAndroid Build Coastguard Worker return (MergeGain);
227*e5436536SAndroid Build Coastguard Worker }
228*e5436536SAndroid Build Coastguard Worker
229*e5436536SAndroid Build Coastguard Worker /* sectioning Stage 0:find minimum codbooks */
FDKaacEnc_gmStage0(SECTION_INFO * const RESTRICT huffsection,const INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],const INT maxSfb,const INT * const noiseNrg,const INT * const isBook)230*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_gmStage0(
231*e5436536SAndroid Build Coastguard Worker SECTION_INFO* const RESTRICT huffsection,
232*e5436536SAndroid Build Coastguard Worker const INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], const INT maxSfb,
233*e5436536SAndroid Build Coastguard Worker const INT* const noiseNrg, const INT* const isBook) {
234*e5436536SAndroid Build Coastguard Worker INT i;
235*e5436536SAndroid Build Coastguard Worker
236*e5436536SAndroid Build Coastguard Worker for (i = 0; i < maxSfb; i++) {
237*e5436536SAndroid Build Coastguard Worker /* Side-Info bits will be calculated in Stage 1! */
238*e5436536SAndroid Build Coastguard Worker if (huffsection[i].sectionBits == INVALID_BITCOUNT) {
239*e5436536SAndroid Build Coastguard Worker /* intensity and pns codebooks are already allocated in bitcount.c */
240*e5436536SAndroid Build Coastguard Worker if (noiseNrg[i] != NO_NOISE_PNS) {
241*e5436536SAndroid Build Coastguard Worker huffsection[i].codeBook = CODE_BOOK_PNS_NO;
242*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits = 0;
243*e5436536SAndroid Build Coastguard Worker } else if (isBook[i]) {
244*e5436536SAndroid Build Coastguard Worker huffsection[i].codeBook = isBook[i];
245*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits = 0;
246*e5436536SAndroid Build Coastguard Worker } else {
247*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits =
248*e5436536SAndroid Build Coastguard Worker FDKaacEnc_findBestBook(bitLookUp[i], &(huffsection[i].codeBook),
249*e5436536SAndroid Build Coastguard Worker 0); /* useVCB11 must be 0!!! */
250*e5436536SAndroid Build Coastguard Worker }
251*e5436536SAndroid Build Coastguard Worker }
252*e5436536SAndroid Build Coastguard Worker }
253*e5436536SAndroid Build Coastguard Worker }
254*e5436536SAndroid Build Coastguard Worker
255*e5436536SAndroid Build Coastguard Worker /*
256*e5436536SAndroid Build Coastguard Worker sectioning Stage 1:merge all connected regions with the same code book and
257*e5436536SAndroid Build Coastguard Worker calculate side info
258*e5436536SAndroid Build Coastguard Worker */
FDKaacEnc_gmStage1(SECTION_INFO * const RESTRICT huffsection,INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],const INT maxSfb,const SHORT * const sideInfoTab,const INT useVCB11)259*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_gmStage1(
260*e5436536SAndroid Build Coastguard Worker SECTION_INFO* const RESTRICT huffsection,
261*e5436536SAndroid Build Coastguard Worker INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], const INT maxSfb,
262*e5436536SAndroid Build Coastguard Worker const SHORT* const sideInfoTab, const INT useVCB11) {
263*e5436536SAndroid Build Coastguard Worker INT mergeStart = 0, mergeEnd;
264*e5436536SAndroid Build Coastguard Worker
265*e5436536SAndroid Build Coastguard Worker do {
266*e5436536SAndroid Build Coastguard Worker for (mergeEnd = mergeStart + 1; mergeEnd < maxSfb; mergeEnd++) {
267*e5436536SAndroid Build Coastguard Worker if (huffsection[mergeStart].codeBook != huffsection[mergeEnd].codeBook)
268*e5436536SAndroid Build Coastguard Worker break;
269*e5436536SAndroid Build Coastguard Worker
270*e5436536SAndroid Build Coastguard Worker /* we can merge. update tables, side info bits will be updated outside of
271*e5436536SAndroid Build Coastguard Worker * this loop */
272*e5436536SAndroid Build Coastguard Worker huffsection[mergeStart].sfbCnt++;
273*e5436536SAndroid Build Coastguard Worker huffsection[mergeStart].sectionBits += huffsection[mergeEnd].sectionBits;
274*e5436536SAndroid Build Coastguard Worker
275*e5436536SAndroid Build Coastguard Worker /* update bit look up for all code books */
276*e5436536SAndroid Build Coastguard Worker FDKaacEnc_mergeBitLookUp(bitLookUp[mergeStart], bitLookUp[mergeEnd]);
277*e5436536SAndroid Build Coastguard Worker }
278*e5436536SAndroid Build Coastguard Worker
279*e5436536SAndroid Build Coastguard Worker /* add side info info bits */
280*e5436536SAndroid Build Coastguard Worker huffsection[mergeStart].sectionBits += FDKaacEnc_getSideInfoBits(
281*e5436536SAndroid Build Coastguard Worker &huffsection[mergeStart], sideInfoTab, useVCB11);
282*e5436536SAndroid Build Coastguard Worker huffsection[mergeEnd - 1].sfbStart =
283*e5436536SAndroid Build Coastguard Worker huffsection[mergeStart].sfbStart; /* speed up prev search */
284*e5436536SAndroid Build Coastguard Worker
285*e5436536SAndroid Build Coastguard Worker mergeStart = mergeEnd;
286*e5436536SAndroid Build Coastguard Worker
287*e5436536SAndroid Build Coastguard Worker } while (mergeStart < maxSfb);
288*e5436536SAndroid Build Coastguard Worker }
289*e5436536SAndroid Build Coastguard Worker
290*e5436536SAndroid Build Coastguard Worker /*
291*e5436536SAndroid Build Coastguard Worker sectioning Stage 2:greedy merge algorithm, merge connected sections with
292*e5436536SAndroid Build Coastguard Worker maximum bit gain until no more gain is possible
293*e5436536SAndroid Build Coastguard Worker */
FDKaacEnc_gmStage2(SECTION_INFO * const RESTRICT huffsection,INT * const RESTRICT mergeGainLookUp,INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],const INT maxSfb,const SHORT * const sideInfoTab,const INT useVCB11)294*e5436536SAndroid Build Coastguard Worker static inline void FDKaacEnc_gmStage2(
295*e5436536SAndroid Build Coastguard Worker SECTION_INFO* const RESTRICT huffsection,
296*e5436536SAndroid Build Coastguard Worker INT* const RESTRICT mergeGainLookUp,
297*e5436536SAndroid Build Coastguard Worker INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], const INT maxSfb,
298*e5436536SAndroid Build Coastguard Worker const SHORT* const sideInfoTab, const INT useVCB11) {
299*e5436536SAndroid Build Coastguard Worker INT i;
300*e5436536SAndroid Build Coastguard Worker
301*e5436536SAndroid Build Coastguard Worker for (i = 0; i + huffsection[i].sfbCnt < maxSfb; i += huffsection[i].sfbCnt) {
302*e5436536SAndroid Build Coastguard Worker mergeGainLookUp[i] =
303*e5436536SAndroid Build Coastguard Worker FDKaacEnc_CalcMergeGain(huffsection, bitLookUp, sideInfoTab, i,
304*e5436536SAndroid Build Coastguard Worker i + huffsection[i].sfbCnt, useVCB11);
305*e5436536SAndroid Build Coastguard Worker }
306*e5436536SAndroid Build Coastguard Worker
307*e5436536SAndroid Build Coastguard Worker while (TRUE) {
308*e5436536SAndroid Build Coastguard Worker INT maxMergeGain, maxNdx, maxNdxNext, maxNdxLast;
309*e5436536SAndroid Build Coastguard Worker
310*e5436536SAndroid Build Coastguard Worker maxMergeGain =
311*e5436536SAndroid Build Coastguard Worker FDKaacEnc_findMaxMerge(mergeGainLookUp, huffsection, maxSfb, &maxNdx);
312*e5436536SAndroid Build Coastguard Worker
313*e5436536SAndroid Build Coastguard Worker /* exit while loop if no more gain is possible */
314*e5436536SAndroid Build Coastguard Worker if (maxMergeGain <= 0) break;
315*e5436536SAndroid Build Coastguard Worker
316*e5436536SAndroid Build Coastguard Worker maxNdxNext = maxNdx + huffsection[maxNdx].sfbCnt;
317*e5436536SAndroid Build Coastguard Worker
318*e5436536SAndroid Build Coastguard Worker /* merge sections with maximum bit gain */
319*e5436536SAndroid Build Coastguard Worker huffsection[maxNdx].sfbCnt += huffsection[maxNdxNext].sfbCnt;
320*e5436536SAndroid Build Coastguard Worker huffsection[maxNdx].sectionBits +=
321*e5436536SAndroid Build Coastguard Worker huffsection[maxNdxNext].sectionBits - maxMergeGain;
322*e5436536SAndroid Build Coastguard Worker
323*e5436536SAndroid Build Coastguard Worker /* update bit look up table for merged huffsection */
324*e5436536SAndroid Build Coastguard Worker FDKaacEnc_mergeBitLookUp(bitLookUp[maxNdx], bitLookUp[maxNdxNext]);
325*e5436536SAndroid Build Coastguard Worker
326*e5436536SAndroid Build Coastguard Worker /* update mergeLookUpTable */
327*e5436536SAndroid Build Coastguard Worker if (maxNdx != 0) {
328*e5436536SAndroid Build Coastguard Worker maxNdxLast = huffsection[maxNdx - 1].sfbStart;
329*e5436536SAndroid Build Coastguard Worker mergeGainLookUp[maxNdxLast] = FDKaacEnc_CalcMergeGain(
330*e5436536SAndroid Build Coastguard Worker huffsection, bitLookUp, sideInfoTab, maxNdxLast, maxNdx, useVCB11);
331*e5436536SAndroid Build Coastguard Worker }
332*e5436536SAndroid Build Coastguard Worker maxNdxNext = maxNdx + huffsection[maxNdx].sfbCnt;
333*e5436536SAndroid Build Coastguard Worker
334*e5436536SAndroid Build Coastguard Worker huffsection[maxNdxNext - 1].sfbStart = huffsection[maxNdx].sfbStart;
335*e5436536SAndroid Build Coastguard Worker
336*e5436536SAndroid Build Coastguard Worker if (maxNdxNext < maxSfb)
337*e5436536SAndroid Build Coastguard Worker mergeGainLookUp[maxNdx] = FDKaacEnc_CalcMergeGain(
338*e5436536SAndroid Build Coastguard Worker huffsection, bitLookUp, sideInfoTab, maxNdx, maxNdxNext, useVCB11);
339*e5436536SAndroid Build Coastguard Worker }
340*e5436536SAndroid Build Coastguard Worker }
341*e5436536SAndroid Build Coastguard Worker
342*e5436536SAndroid Build Coastguard Worker /* count bits used by the noiseless coder */
FDKaacEnc_noiselessCounter(SECTION_DATA * const RESTRICT sectionData,INT mergeGainLookUp[MAX_SFB_LONG],INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX+1],const SHORT * const quantSpectrum,const UINT * const maxValueInSfb,const INT * const sfbOffset,const INT blockType,const INT * const noiseNrg,const INT * const isBook,const INT useVCB11)343*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_noiselessCounter(
344*e5436536SAndroid Build Coastguard Worker SECTION_DATA* const RESTRICT sectionData, INT mergeGainLookUp[MAX_SFB_LONG],
345*e5436536SAndroid Build Coastguard Worker INT bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1],
346*e5436536SAndroid Build Coastguard Worker const SHORT* const quantSpectrum, const UINT* const maxValueInSfb,
347*e5436536SAndroid Build Coastguard Worker const INT* const sfbOffset, const INT blockType, const INT* const noiseNrg,
348*e5436536SAndroid Build Coastguard Worker const INT* const isBook, const INT useVCB11) {
349*e5436536SAndroid Build Coastguard Worker INT grpNdx, i;
350*e5436536SAndroid Build Coastguard Worker const SHORT* sideInfoTab = NULL;
351*e5436536SAndroid Build Coastguard Worker SECTION_INFO* huffsection;
352*e5436536SAndroid Build Coastguard Worker
353*e5436536SAndroid Build Coastguard Worker /* use appropriate side info table */
354*e5436536SAndroid Build Coastguard Worker switch (blockType) {
355*e5436536SAndroid Build Coastguard Worker case LONG_WINDOW:
356*e5436536SAndroid Build Coastguard Worker case START_WINDOW:
357*e5436536SAndroid Build Coastguard Worker case STOP_WINDOW:
358*e5436536SAndroid Build Coastguard Worker default:
359*e5436536SAndroid Build Coastguard Worker sideInfoTab = FDKaacEnc_sideInfoTabLong;
360*e5436536SAndroid Build Coastguard Worker break;
361*e5436536SAndroid Build Coastguard Worker case SHORT_WINDOW:
362*e5436536SAndroid Build Coastguard Worker sideInfoTab = FDKaacEnc_sideInfoTabShort;
363*e5436536SAndroid Build Coastguard Worker break;
364*e5436536SAndroid Build Coastguard Worker }
365*e5436536SAndroid Build Coastguard Worker
366*e5436536SAndroid Build Coastguard Worker sectionData->noOfSections = 0;
367*e5436536SAndroid Build Coastguard Worker sectionData->huffmanBits = 0;
368*e5436536SAndroid Build Coastguard Worker sectionData->sideInfoBits = 0;
369*e5436536SAndroid Build Coastguard Worker
370*e5436536SAndroid Build Coastguard Worker if (sectionData->maxSfbPerGroup == 0) return;
371*e5436536SAndroid Build Coastguard Worker
372*e5436536SAndroid Build Coastguard Worker /* loop trough groups */
373*e5436536SAndroid Build Coastguard Worker for (grpNdx = 0; grpNdx < sectionData->sfbCnt;
374*e5436536SAndroid Build Coastguard Worker grpNdx += sectionData->sfbPerGroup) {
375*e5436536SAndroid Build Coastguard Worker huffsection = sectionData->huffsection + sectionData->noOfSections;
376*e5436536SAndroid Build Coastguard Worker
377*e5436536SAndroid Build Coastguard Worker /* count bits in this group */
378*e5436536SAndroid Build Coastguard Worker FDKaacEnc_buildBitLookUp(quantSpectrum, sectionData->maxSfbPerGroup,
379*e5436536SAndroid Build Coastguard Worker sfbOffset + grpNdx, maxValueInSfb + grpNdx,
380*e5436536SAndroid Build Coastguard Worker bitLookUp, huffsection);
381*e5436536SAndroid Build Coastguard Worker
382*e5436536SAndroid Build Coastguard Worker /* 0.Stage :Find minimum Codebooks */
383*e5436536SAndroid Build Coastguard Worker FDKaacEnc_gmStage0(huffsection, bitLookUp, sectionData->maxSfbPerGroup,
384*e5436536SAndroid Build Coastguard Worker noiseNrg + grpNdx, isBook + grpNdx);
385*e5436536SAndroid Build Coastguard Worker
386*e5436536SAndroid Build Coastguard Worker /* 1.Stage :Merge all connected regions with the same code book */
387*e5436536SAndroid Build Coastguard Worker FDKaacEnc_gmStage1(huffsection, bitLookUp, sectionData->maxSfbPerGroup,
388*e5436536SAndroid Build Coastguard Worker sideInfoTab, useVCB11);
389*e5436536SAndroid Build Coastguard Worker
390*e5436536SAndroid Build Coastguard Worker /*
391*e5436536SAndroid Build Coastguard Worker 2.Stage
392*e5436536SAndroid Build Coastguard Worker greedy merge algorithm, merge connected huffsections with maximum bit
393*e5436536SAndroid Build Coastguard Worker gain until no more gain is possible
394*e5436536SAndroid Build Coastguard Worker */
395*e5436536SAndroid Build Coastguard Worker
396*e5436536SAndroid Build Coastguard Worker FDKaacEnc_gmStage2(huffsection, mergeGainLookUp, bitLookUp,
397*e5436536SAndroid Build Coastguard Worker sectionData->maxSfbPerGroup, sideInfoTab, useVCB11);
398*e5436536SAndroid Build Coastguard Worker
399*e5436536SAndroid Build Coastguard Worker /*
400*e5436536SAndroid Build Coastguard Worker compress output, calculate total huff and side bits
401*e5436536SAndroid Build Coastguard Worker since we did not update the actual codebook in stage 2
402*e5436536SAndroid Build Coastguard Worker to save time, we must set it here for later use in bitenc
403*e5436536SAndroid Build Coastguard Worker */
404*e5436536SAndroid Build Coastguard Worker
405*e5436536SAndroid Build Coastguard Worker for (i = 0; i < sectionData->maxSfbPerGroup; i += huffsection[i].sfbCnt) {
406*e5436536SAndroid Build Coastguard Worker if ((huffsection[i].codeBook == CODE_BOOK_PNS_NO) ||
407*e5436536SAndroid Build Coastguard Worker (huffsection[i].codeBook == CODE_BOOK_IS_OUT_OF_PHASE_NO) ||
408*e5436536SAndroid Build Coastguard Worker (huffsection[i].codeBook == CODE_BOOK_IS_IN_PHASE_NO)) {
409*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits = 0;
410*e5436536SAndroid Build Coastguard Worker } else {
411*e5436536SAndroid Build Coastguard Worker /* the sections in the sectionData are now marked with the optimal code
412*e5436536SAndroid Build Coastguard Worker * book */
413*e5436536SAndroid Build Coastguard Worker
414*e5436536SAndroid Build Coastguard Worker FDKaacEnc_findBestBook(bitLookUp[i], &(huffsection[i].codeBook),
415*e5436536SAndroid Build Coastguard Worker useVCB11);
416*e5436536SAndroid Build Coastguard Worker
417*e5436536SAndroid Build Coastguard Worker sectionData->huffmanBits +=
418*e5436536SAndroid Build Coastguard Worker huffsection[i].sectionBits -
419*e5436536SAndroid Build Coastguard Worker FDKaacEnc_getSideInfoBits(&huffsection[i], sideInfoTab, useVCB11);
420*e5436536SAndroid Build Coastguard Worker }
421*e5436536SAndroid Build Coastguard Worker
422*e5436536SAndroid Build Coastguard Worker huffsection[i].sfbStart += grpNdx;
423*e5436536SAndroid Build Coastguard Worker
424*e5436536SAndroid Build Coastguard Worker /* sum up side info bits (section data bits) */
425*e5436536SAndroid Build Coastguard Worker sectionData->sideInfoBits +=
426*e5436536SAndroid Build Coastguard Worker FDKaacEnc_getSideInfoBits(&huffsection[i], sideInfoTab, useVCB11);
427*e5436536SAndroid Build Coastguard Worker sectionData->huffsection[sectionData->noOfSections++] = huffsection[i];
428*e5436536SAndroid Build Coastguard Worker }
429*e5436536SAndroid Build Coastguard Worker }
430*e5436536SAndroid Build Coastguard Worker }
431*e5436536SAndroid Build Coastguard Worker
432*e5436536SAndroid Build Coastguard Worker /*******************************************************************************
433*e5436536SAndroid Build Coastguard Worker
434*e5436536SAndroid Build Coastguard Worker functionname: FDKaacEnc_scfCount
435*e5436536SAndroid Build Coastguard Worker returns : ---
436*e5436536SAndroid Build Coastguard Worker description : count bits used by scalefactors.
437*e5436536SAndroid Build Coastguard Worker
438*e5436536SAndroid Build Coastguard Worker not in all cases if maxValueInSfb[] == 0 we set deltaScf
439*e5436536SAndroid Build Coastguard Worker to zero. only if the difference of the last and future
440*e5436536SAndroid Build Coastguard Worker scalefacGain is not greater then CODE_BOOK_SCF_LAV (60).
441*e5436536SAndroid Build Coastguard Worker
442*e5436536SAndroid Build Coastguard Worker example:
443*e5436536SAndroid Build Coastguard Worker ^
444*e5436536SAndroid Build Coastguard Worker scalefacGain |
445*e5436536SAndroid Build Coastguard Worker |
446*e5436536SAndroid Build Coastguard Worker | last 75
447*e5436536SAndroid Build Coastguard Worker | |
448*e5436536SAndroid Build Coastguard Worker | |
449*e5436536SAndroid Build Coastguard Worker | |
450*e5436536SAndroid Build Coastguard Worker | | current 50
451*e5436536SAndroid Build Coastguard Worker | | |
452*e5436536SAndroid Build Coastguard Worker | | |
453*e5436536SAndroid Build Coastguard Worker | | |
454*e5436536SAndroid Build Coastguard Worker | | |
455*e5436536SAndroid Build Coastguard Worker | | | future 5
456*e5436536SAndroid Build Coastguard Worker | | | |
457*e5436536SAndroid Build Coastguard Worker --- ... ---------------------------- ... --------->
458*e5436536SAndroid Build Coastguard Worker sfb
459*e5436536SAndroid Build Coastguard Worker
460*e5436536SAndroid Build Coastguard Worker
461*e5436536SAndroid Build Coastguard Worker if maxValueInSfb[] of current is zero because of a
462*e5436536SAndroid Build Coastguard Worker notfallstrategie, we do not save bits and transmit a
463*e5436536SAndroid Build Coastguard Worker deltaScf of 25. otherwise the deltaScf between the last
464*e5436536SAndroid Build Coastguard Worker scalfacGain (75) and the future scalefacGain (5) is 70.
465*e5436536SAndroid Build Coastguard Worker
466*e5436536SAndroid Build Coastguard Worker ********************************************************************************/
FDKaacEnc_scfCount(const INT * const scalefacGain,const UINT * const maxValueInSfb,SECTION_DATA * const RESTRICT sectionData,const INT * const isScale)467*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_scfCount(const INT* const scalefacGain,
468*e5436536SAndroid Build Coastguard Worker const UINT* const maxValueInSfb,
469*e5436536SAndroid Build Coastguard Worker SECTION_DATA* const RESTRICT sectionData,
470*e5436536SAndroid Build Coastguard Worker const INT* const isScale) {
471*e5436536SAndroid Build Coastguard Worker INT i, j, k, m, n;
472*e5436536SAndroid Build Coastguard Worker
473*e5436536SAndroid Build Coastguard Worker INT lastValScf = 0;
474*e5436536SAndroid Build Coastguard Worker INT deltaScf = 0;
475*e5436536SAndroid Build Coastguard Worker INT found = 0;
476*e5436536SAndroid Build Coastguard Worker INT scfSkipCounter = 0;
477*e5436536SAndroid Build Coastguard Worker INT lastValIs = 0;
478*e5436536SAndroid Build Coastguard Worker
479*e5436536SAndroid Build Coastguard Worker sectionData->scalefacBits = 0;
480*e5436536SAndroid Build Coastguard Worker
481*e5436536SAndroid Build Coastguard Worker if (scalefacGain == NULL) return;
482*e5436536SAndroid Build Coastguard Worker
483*e5436536SAndroid Build Coastguard Worker sectionData->firstScf = 0;
484*e5436536SAndroid Build Coastguard Worker
485*e5436536SAndroid Build Coastguard Worker for (i = 0; i < sectionData->noOfSections; i++) {
486*e5436536SAndroid Build Coastguard Worker if (sectionData->huffsection[i].codeBook != CODE_BOOK_ZERO_NO) {
487*e5436536SAndroid Build Coastguard Worker sectionData->firstScf = sectionData->huffsection[i].sfbStart;
488*e5436536SAndroid Build Coastguard Worker lastValScf = scalefacGain[sectionData->firstScf];
489*e5436536SAndroid Build Coastguard Worker break;
490*e5436536SAndroid Build Coastguard Worker }
491*e5436536SAndroid Build Coastguard Worker }
492*e5436536SAndroid Build Coastguard Worker
493*e5436536SAndroid Build Coastguard Worker for (i = 0; i < sectionData->noOfSections; i++) {
494*e5436536SAndroid Build Coastguard Worker if ((sectionData->huffsection[i].codeBook ==
495*e5436536SAndroid Build Coastguard Worker CODE_BOOK_IS_OUT_OF_PHASE_NO) ||
496*e5436536SAndroid Build Coastguard Worker (sectionData->huffsection[i].codeBook == CODE_BOOK_IS_IN_PHASE_NO)) {
497*e5436536SAndroid Build Coastguard Worker for (j = sectionData->huffsection[i].sfbStart;
498*e5436536SAndroid Build Coastguard Worker j < sectionData->huffsection[i].sfbStart +
499*e5436536SAndroid Build Coastguard Worker sectionData->huffsection[i].sfbCnt;
500*e5436536SAndroid Build Coastguard Worker j++) {
501*e5436536SAndroid Build Coastguard Worker INT deltaIs = isScale[j] - lastValIs;
502*e5436536SAndroid Build Coastguard Worker lastValIs = isScale[j];
503*e5436536SAndroid Build Coastguard Worker sectionData->scalefacBits +=
504*e5436536SAndroid Build Coastguard Worker FDKaacEnc_bitCountScalefactorDelta(deltaIs);
505*e5436536SAndroid Build Coastguard Worker }
506*e5436536SAndroid Build Coastguard Worker } /* Intensity */
507*e5436536SAndroid Build Coastguard Worker else if ((sectionData->huffsection[i].codeBook != CODE_BOOK_ZERO_NO) &&
508*e5436536SAndroid Build Coastguard Worker (sectionData->huffsection[i].codeBook != CODE_BOOK_PNS_NO)) {
509*e5436536SAndroid Build Coastguard Worker INT tmp = sectionData->huffsection[i].sfbStart +
510*e5436536SAndroid Build Coastguard Worker sectionData->huffsection[i].sfbCnt;
511*e5436536SAndroid Build Coastguard Worker for (j = sectionData->huffsection[i].sfbStart; j < tmp; j++) {
512*e5436536SAndroid Build Coastguard Worker /* check if we can repeat the last value to save bits */
513*e5436536SAndroid Build Coastguard Worker if (maxValueInSfb[j] == 0) {
514*e5436536SAndroid Build Coastguard Worker found = 0;
515*e5436536SAndroid Build Coastguard Worker /* are scalefactors skipped? */
516*e5436536SAndroid Build Coastguard Worker if (scfSkipCounter == 0) {
517*e5436536SAndroid Build Coastguard Worker /* end of section */
518*e5436536SAndroid Build Coastguard Worker if (j == (tmp - 1))
519*e5436536SAndroid Build Coastguard Worker found = 0; /* search in other sections for maxValueInSfb != 0 */
520*e5436536SAndroid Build Coastguard Worker else {
521*e5436536SAndroid Build Coastguard Worker /* search in this section for the next maxValueInSfb[] != 0 */
522*e5436536SAndroid Build Coastguard Worker for (k = (j + 1); k < tmp; k++) {
523*e5436536SAndroid Build Coastguard Worker if (maxValueInSfb[k] != 0) {
524*e5436536SAndroid Build Coastguard Worker found = 1;
525*e5436536SAndroid Build Coastguard Worker if ((fixp_abs(scalefacGain[k] - lastValScf)) <=
526*e5436536SAndroid Build Coastguard Worker CODE_BOOK_SCF_LAV)
527*e5436536SAndroid Build Coastguard Worker deltaScf = 0; /* save bits */
528*e5436536SAndroid Build Coastguard Worker else {
529*e5436536SAndroid Build Coastguard Worker /* do not save bits */
530*e5436536SAndroid Build Coastguard Worker deltaScf = lastValScf - scalefacGain[j];
531*e5436536SAndroid Build Coastguard Worker lastValScf = scalefacGain[j];
532*e5436536SAndroid Build Coastguard Worker scfSkipCounter = 0;
533*e5436536SAndroid Build Coastguard Worker }
534*e5436536SAndroid Build Coastguard Worker break;
535*e5436536SAndroid Build Coastguard Worker }
536*e5436536SAndroid Build Coastguard Worker /* count scalefactor skip */
537*e5436536SAndroid Build Coastguard Worker scfSkipCounter++;
538*e5436536SAndroid Build Coastguard Worker }
539*e5436536SAndroid Build Coastguard Worker }
540*e5436536SAndroid Build Coastguard Worker
541*e5436536SAndroid Build Coastguard Worker /* search for the next maxValueInSfb[] != 0 in all other sections */
542*e5436536SAndroid Build Coastguard Worker for (m = (i + 1); (m < sectionData->noOfSections) && (found == 0);
543*e5436536SAndroid Build Coastguard Worker m++) {
544*e5436536SAndroid Build Coastguard Worker if ((sectionData->huffsection[m].codeBook != CODE_BOOK_ZERO_NO) &&
545*e5436536SAndroid Build Coastguard Worker (sectionData->huffsection[m].codeBook != CODE_BOOK_PNS_NO)) {
546*e5436536SAndroid Build Coastguard Worker INT end = sectionData->huffsection[m].sfbStart +
547*e5436536SAndroid Build Coastguard Worker sectionData->huffsection[m].sfbCnt;
548*e5436536SAndroid Build Coastguard Worker for (n = sectionData->huffsection[m].sfbStart; n < end; n++) {
549*e5436536SAndroid Build Coastguard Worker if (maxValueInSfb[n] != 0) {
550*e5436536SAndroid Build Coastguard Worker found = 1;
551*e5436536SAndroid Build Coastguard Worker if (fixp_abs(scalefacGain[n] - lastValScf) <=
552*e5436536SAndroid Build Coastguard Worker CODE_BOOK_SCF_LAV)
553*e5436536SAndroid Build Coastguard Worker deltaScf = 0; /* save bits */
554*e5436536SAndroid Build Coastguard Worker else {
555*e5436536SAndroid Build Coastguard Worker /* do not save bits */
556*e5436536SAndroid Build Coastguard Worker deltaScf = lastValScf - scalefacGain[j];
557*e5436536SAndroid Build Coastguard Worker lastValScf = scalefacGain[j];
558*e5436536SAndroid Build Coastguard Worker scfSkipCounter = 0;
559*e5436536SAndroid Build Coastguard Worker }
560*e5436536SAndroid Build Coastguard Worker break;
561*e5436536SAndroid Build Coastguard Worker }
562*e5436536SAndroid Build Coastguard Worker /* count scalefactor skip */
563*e5436536SAndroid Build Coastguard Worker scfSkipCounter++;
564*e5436536SAndroid Build Coastguard Worker }
565*e5436536SAndroid Build Coastguard Worker }
566*e5436536SAndroid Build Coastguard Worker }
567*e5436536SAndroid Build Coastguard Worker /* no maxValueInSfb[] != 0 found */
568*e5436536SAndroid Build Coastguard Worker if (found == 0) {
569*e5436536SAndroid Build Coastguard Worker deltaScf = 0;
570*e5436536SAndroid Build Coastguard Worker scfSkipCounter = 0;
571*e5436536SAndroid Build Coastguard Worker }
572*e5436536SAndroid Build Coastguard Worker } else {
573*e5436536SAndroid Build Coastguard Worker /* consider skipped scalefactors */
574*e5436536SAndroid Build Coastguard Worker deltaScf = 0;
575*e5436536SAndroid Build Coastguard Worker scfSkipCounter--;
576*e5436536SAndroid Build Coastguard Worker }
577*e5436536SAndroid Build Coastguard Worker } else {
578*e5436536SAndroid Build Coastguard Worker deltaScf = lastValScf - scalefacGain[j];
579*e5436536SAndroid Build Coastguard Worker lastValScf = scalefacGain[j];
580*e5436536SAndroid Build Coastguard Worker }
581*e5436536SAndroid Build Coastguard Worker sectionData->scalefacBits +=
582*e5436536SAndroid Build Coastguard Worker FDKaacEnc_bitCountScalefactorDelta(deltaScf);
583*e5436536SAndroid Build Coastguard Worker }
584*e5436536SAndroid Build Coastguard Worker }
585*e5436536SAndroid Build Coastguard Worker } /* for (i=0; i<sectionData->noOfSections; i++) */
586*e5436536SAndroid Build Coastguard Worker }
587*e5436536SAndroid Build Coastguard Worker
588*e5436536SAndroid Build Coastguard Worker /* count bits used by pns */
FDKaacEnc_noiseCount(SECTION_DATA * const RESTRICT sectionData,const INT * const noiseNrg)589*e5436536SAndroid Build Coastguard Worker static void FDKaacEnc_noiseCount(SECTION_DATA* const RESTRICT sectionData,
590*e5436536SAndroid Build Coastguard Worker const INT* const noiseNrg) {
591*e5436536SAndroid Build Coastguard Worker INT noisePCMFlag = TRUE;
592*e5436536SAndroid Build Coastguard Worker INT lastValPns = 0, deltaPns;
593*e5436536SAndroid Build Coastguard Worker int i, j;
594*e5436536SAndroid Build Coastguard Worker
595*e5436536SAndroid Build Coastguard Worker sectionData->noiseNrgBits = 0;
596*e5436536SAndroid Build Coastguard Worker
597*e5436536SAndroid Build Coastguard Worker for (i = 0; i < sectionData->noOfSections; i++) {
598*e5436536SAndroid Build Coastguard Worker if (sectionData->huffsection[i].codeBook == CODE_BOOK_PNS_NO) {
599*e5436536SAndroid Build Coastguard Worker int sfbStart = sectionData->huffsection[i].sfbStart;
600*e5436536SAndroid Build Coastguard Worker int sfbEnd = sfbStart + sectionData->huffsection[i].sfbCnt;
601*e5436536SAndroid Build Coastguard Worker for (j = sfbStart; j < sfbEnd; j++) {
602*e5436536SAndroid Build Coastguard Worker if (noisePCMFlag) {
603*e5436536SAndroid Build Coastguard Worker sectionData->noiseNrgBits += PNS_PCM_BITS;
604*e5436536SAndroid Build Coastguard Worker lastValPns = noiseNrg[j];
605*e5436536SAndroid Build Coastguard Worker noisePCMFlag = FALSE;
606*e5436536SAndroid Build Coastguard Worker } else {
607*e5436536SAndroid Build Coastguard Worker deltaPns = noiseNrg[j] - lastValPns;
608*e5436536SAndroid Build Coastguard Worker lastValPns = noiseNrg[j];
609*e5436536SAndroid Build Coastguard Worker sectionData->noiseNrgBits +=
610*e5436536SAndroid Build Coastguard Worker FDKaacEnc_bitCountScalefactorDelta(deltaPns);
611*e5436536SAndroid Build Coastguard Worker }
612*e5436536SAndroid Build Coastguard Worker }
613*e5436536SAndroid Build Coastguard Worker }
614*e5436536SAndroid Build Coastguard Worker }
615*e5436536SAndroid Build Coastguard Worker }
616*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_dynBitCount(BITCNTR_STATE * const hBC,const SHORT * const quantSpectrum,const UINT * const maxValueInSfb,const INT * const scalefac,const INT blockType,const INT sfbCnt,const INT maxSfbPerGroup,const INT sfbPerGroup,const INT * const sfbOffset,SECTION_DATA * const RESTRICT sectionData,const INT * const noiseNrg,const INT * const isBook,const INT * const isScale,const UINT syntaxFlags)617*e5436536SAndroid Build Coastguard Worker INT FDKaacEnc_dynBitCount(BITCNTR_STATE* const hBC,
618*e5436536SAndroid Build Coastguard Worker const SHORT* const quantSpectrum,
619*e5436536SAndroid Build Coastguard Worker const UINT* const maxValueInSfb,
620*e5436536SAndroid Build Coastguard Worker const INT* const scalefac, const INT blockType,
621*e5436536SAndroid Build Coastguard Worker const INT sfbCnt, const INT maxSfbPerGroup,
622*e5436536SAndroid Build Coastguard Worker const INT sfbPerGroup, const INT* const sfbOffset,
623*e5436536SAndroid Build Coastguard Worker SECTION_DATA* const RESTRICT sectionData,
624*e5436536SAndroid Build Coastguard Worker const INT* const noiseNrg, const INT* const isBook,
625*e5436536SAndroid Build Coastguard Worker const INT* const isScale, const UINT syntaxFlags) {
626*e5436536SAndroid Build Coastguard Worker sectionData->blockType = blockType;
627*e5436536SAndroid Build Coastguard Worker sectionData->sfbCnt = sfbCnt;
628*e5436536SAndroid Build Coastguard Worker sectionData->sfbPerGroup = sfbPerGroup;
629*e5436536SAndroid Build Coastguard Worker sectionData->noOfGroups = sfbCnt / sfbPerGroup;
630*e5436536SAndroid Build Coastguard Worker sectionData->maxSfbPerGroup = maxSfbPerGroup;
631*e5436536SAndroid Build Coastguard Worker
632*e5436536SAndroid Build Coastguard Worker FDKaacEnc_noiselessCounter(sectionData, hBC->mergeGainLookUp,
633*e5436536SAndroid Build Coastguard Worker (lookUpTable)hBC->bitLookUp, quantSpectrum,
634*e5436536SAndroid Build Coastguard Worker maxValueInSfb, sfbOffset, blockType, noiseNrg,
635*e5436536SAndroid Build Coastguard Worker isBook, (syntaxFlags & AC_ER_VCB11) ? 1 : 0);
636*e5436536SAndroid Build Coastguard Worker
637*e5436536SAndroid Build Coastguard Worker FDKaacEnc_scfCount(scalefac, maxValueInSfb, sectionData, isScale);
638*e5436536SAndroid Build Coastguard Worker
639*e5436536SAndroid Build Coastguard Worker FDKaacEnc_noiseCount(sectionData, noiseNrg);
640*e5436536SAndroid Build Coastguard Worker
641*e5436536SAndroid Build Coastguard Worker return (sectionData->huffmanBits + sectionData->sideInfoBits +
642*e5436536SAndroid Build Coastguard Worker sectionData->scalefacBits + sectionData->noiseNrgBits);
643*e5436536SAndroid Build Coastguard Worker }
644*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_BCNew(BITCNTR_STATE ** phBC,UCHAR * dynamic_RAM)645*e5436536SAndroid Build Coastguard Worker INT FDKaacEnc_BCNew(BITCNTR_STATE** phBC, UCHAR* dynamic_RAM) {
646*e5436536SAndroid Build Coastguard Worker BITCNTR_STATE* hBC = GetRam_aacEnc_BitCntrState();
647*e5436536SAndroid Build Coastguard Worker
648*e5436536SAndroid Build Coastguard Worker if (hBC) {
649*e5436536SAndroid Build Coastguard Worker *phBC = hBC;
650*e5436536SAndroid Build Coastguard Worker hBC->bitLookUp = GetRam_aacEnc_BitLookUp(0, dynamic_RAM);
651*e5436536SAndroid Build Coastguard Worker hBC->mergeGainLookUp = GetRam_aacEnc_MergeGainLookUp(0, dynamic_RAM);
652*e5436536SAndroid Build Coastguard Worker if (hBC->bitLookUp == 0 || hBC->mergeGainLookUp == 0) {
653*e5436536SAndroid Build Coastguard Worker return 1;
654*e5436536SAndroid Build Coastguard Worker }
655*e5436536SAndroid Build Coastguard Worker }
656*e5436536SAndroid Build Coastguard Worker return (hBC == 0) ? 1 : 0;
657*e5436536SAndroid Build Coastguard Worker }
658*e5436536SAndroid Build Coastguard Worker
FDKaacEnc_BCClose(BITCNTR_STATE ** phBC)659*e5436536SAndroid Build Coastguard Worker void FDKaacEnc_BCClose(BITCNTR_STATE** phBC) {
660*e5436536SAndroid Build Coastguard Worker if (*phBC != NULL) {
661*e5436536SAndroid Build Coastguard Worker FreeRam_aacEnc_BitCntrState(phBC);
662*e5436536SAndroid Build Coastguard Worker }
663*e5436536SAndroid Build Coastguard Worker }
664