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