1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** AAC encoder library ******************************
96 
97    Author(s):   M.Werner
98 
99    Description: Huffman Bitcounter & coder
100 
101 *******************************************************************************/
102 
103 #include "bit_cnt.h"
104 
105 #include "aacEnc_ram.h"
106 
107 #define HI_LTAB(a) (a >> 16)
108 #define LO_LTAB(a) (a & 0xffff)
109 
110 /*****************************************************************************
111 
112 
113     functionname: FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11
114     description:  counts tables 1-11
115     returns:
116     input:        quantized spectrum
117     output:       bitCount for tables 1-11
118 
119 *****************************************************************************/
120 
FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)121 static void FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11(const SHORT *const values,
122                                                    const INT width,
123                                                    INT *RESTRICT bitCount) {
124   INT i;
125   INT bc1_2, bc3_4, bc5_6, bc7_8, bc9_10, bc11, sc;
126   INT t0, t1, t2, t3;
127   bc1_2 = 0;
128   bc3_4 = 0;
129   bc5_6 = 0;
130   bc7_8 = 0;
131   bc9_10 = 0;
132   bc11 = 0;
133   sc = 0;
134 
135   DWORD_ALIGNED(values);
136 
137   for (i = 0; i < width; i += 4) {
138     t0 = values[i + 0];
139     t1 = values[i + 1];
140     t2 = values[i + 2];
141     t3 = values[i + 3];
142 
143     bc1_2 += (INT)FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1];
144     bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
145              (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
146 
147     t0 = fixp_abs(t0);
148     sc += (t0 > 0);
149     t1 = fixp_abs(t1);
150     sc += (t1 > 0);
151     t2 = fixp_abs(t2);
152     sc += (t2 > 0);
153     t3 = fixp_abs(t3);
154     sc += (t3 > 0);
155 
156     bc3_4 += (INT)FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
157     bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
158              (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
159     bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
160               (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
161     bc11 +=
162         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
163   }
164   bitCount[1] = HI_LTAB(bc1_2);
165   bitCount[2] = LO_LTAB(bc1_2);
166   bitCount[3] = HI_LTAB(bc3_4) + sc;
167   bitCount[4] = LO_LTAB(bc3_4) + sc;
168   bitCount[5] = HI_LTAB(bc5_6);
169   bitCount[6] = LO_LTAB(bc5_6);
170   bitCount[7] = HI_LTAB(bc7_8) + sc;
171   bitCount[8] = LO_LTAB(bc7_8) + sc;
172   bitCount[9] = HI_LTAB(bc9_10) + sc;
173   bitCount[10] = LO_LTAB(bc9_10) + sc;
174   bitCount[11] = bc11 + sc;
175 }
176 
177 /*****************************************************************************
178 
179     functionname: FDKaacEnc_count3_4_5_6_7_8_9_10_11
180     description:  counts tables 3-11
181     returns:
182     input:        quantized spectrum
183     output:       bitCount for tables 3-11
184 
185 *****************************************************************************/
186 
FDKaacEnc_count3_4_5_6_7_8_9_10_11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)187 static void FDKaacEnc_count3_4_5_6_7_8_9_10_11(const SHORT *const values,
188                                                const INT width,
189                                                INT *RESTRICT bitCount) {
190   INT i;
191   INT bc3_4, bc5_6, bc7_8, bc9_10, bc11, sc;
192   INT t0, t1, t2, t3;
193 
194   bc3_4 = 0;
195   bc5_6 = 0;
196   bc7_8 = 0;
197   bc9_10 = 0;
198   bc11 = 0;
199   sc = 0;
200 
201   DWORD_ALIGNED(values);
202 
203   for (i = 0; i < width; i += 4) {
204     t0 = values[i + 0];
205     t1 = values[i + 1];
206     t2 = values[i + 2];
207     t3 = values[i + 3];
208 
209     bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
210              (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
211 
212     t0 = fixp_abs(t0);
213     sc += (t0 > 0);
214     t1 = fixp_abs(t1);
215     sc += (t1 > 0);
216     t2 = fixp_abs(t2);
217     sc += (t2 > 0);
218     t3 = fixp_abs(t3);
219     sc += (t3 > 0);
220 
221     bc3_4 += (INT)FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
222     bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
223              (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
224     bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
225               (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
226     bc11 +=
227         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
228   }
229 
230   bitCount[1] = INVALID_BITCOUNT;
231   bitCount[2] = INVALID_BITCOUNT;
232   bitCount[3] = HI_LTAB(bc3_4) + sc;
233   bitCount[4] = LO_LTAB(bc3_4) + sc;
234   bitCount[5] = HI_LTAB(bc5_6);
235   bitCount[6] = LO_LTAB(bc5_6);
236   bitCount[7] = HI_LTAB(bc7_8) + sc;
237   bitCount[8] = LO_LTAB(bc7_8) + sc;
238   bitCount[9] = HI_LTAB(bc9_10) + sc;
239   bitCount[10] = LO_LTAB(bc9_10) + sc;
240   bitCount[11] = bc11 + sc;
241 }
242 
243 /*****************************************************************************
244 
245     functionname: FDKaacEnc_count5_6_7_8_9_10_11
246     description:  counts tables 5-11
247     returns:
248     input:        quantized spectrum
249     output:       bitCount for tables 5-11
250 
251 *****************************************************************************/
252 
FDKaacEnc_count5_6_7_8_9_10_11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)253 static void FDKaacEnc_count5_6_7_8_9_10_11(const SHORT *const values,
254                                            const INT width,
255                                            INT *RESTRICT bitCount) {
256   INT i;
257   INT bc5_6, bc7_8, bc9_10, bc11, sc;
258   INT t0, t1, t2, t3;
259   bc5_6 = 0;
260   bc7_8 = 0;
261   bc9_10 = 0;
262   bc11 = 0;
263   sc = 0;
264 
265   DWORD_ALIGNED(values);
266 
267   for (i = 0; i < width; i += 4) {
268     t0 = values[i + 0];
269     t1 = values[i + 1];
270     t2 = values[i + 2];
271     t3 = values[i + 3];
272 
273     bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
274              (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
275 
276     t0 = fixp_abs(t0);
277     sc += (t0 > 0);
278     t1 = fixp_abs(t1);
279     sc += (t1 > 0);
280     t2 = fixp_abs(t2);
281     sc += (t2 > 0);
282     t3 = fixp_abs(t3);
283     sc += (t3 > 0);
284 
285     bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
286              (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
287     bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
288               (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
289     bc11 +=
290         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
291   }
292   bitCount[1] = INVALID_BITCOUNT;
293   bitCount[2] = INVALID_BITCOUNT;
294   bitCount[3] = INVALID_BITCOUNT;
295   bitCount[4] = INVALID_BITCOUNT;
296   bitCount[5] = HI_LTAB(bc5_6);
297   bitCount[6] = LO_LTAB(bc5_6);
298   bitCount[7] = HI_LTAB(bc7_8) + sc;
299   bitCount[8] = LO_LTAB(bc7_8) + sc;
300   bitCount[9] = HI_LTAB(bc9_10) + sc;
301   bitCount[10] = LO_LTAB(bc9_10) + sc;
302   bitCount[11] = bc11 + sc;
303 }
304 
305 /*****************************************************************************
306 
307     functionname: FDKaacEnc_count7_8_9_10_11
308     description:  counts tables 7-11
309     returns:
310     input:        quantized spectrum
311     output:       bitCount for tables 7-11
312 
313 *****************************************************************************/
314 
FDKaacEnc_count7_8_9_10_11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)315 static void FDKaacEnc_count7_8_9_10_11(const SHORT *const values,
316                                        const INT width,
317                                        INT *RESTRICT bitCount) {
318   INT i;
319   INT bc7_8, bc9_10, bc11, sc;
320   INT t0, t1, t2, t3;
321 
322   bc7_8 = 0;
323   bc9_10 = 0;
324   bc11 = 0;
325   sc = 0;
326 
327   DWORD_ALIGNED(values);
328 
329   for (i = 0; i < width; i += 4) {
330     t0 = values[i + 0];
331     t1 = values[i + 1];
332     t2 = values[i + 2];
333     t3 = values[i + 3];
334 
335     t0 = fixp_abs(t0);
336     sc += (t0 > 0);
337     t1 = fixp_abs(t1);
338     sc += (t1 > 0);
339     t2 = fixp_abs(t2);
340     sc += (t2 > 0);
341     t3 = fixp_abs(t3);
342     sc += (t3 > 0);
343 
344     bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
345              (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
346     bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
347               (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
348     bc11 +=
349         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
350   }
351 
352   bitCount[1] = INVALID_BITCOUNT;
353   bitCount[2] = INVALID_BITCOUNT;
354   bitCount[3] = INVALID_BITCOUNT;
355   bitCount[4] = INVALID_BITCOUNT;
356   bitCount[5] = INVALID_BITCOUNT;
357   bitCount[6] = INVALID_BITCOUNT;
358   bitCount[7] = HI_LTAB(bc7_8) + sc;
359   bitCount[8] = LO_LTAB(bc7_8) + sc;
360   bitCount[9] = HI_LTAB(bc9_10) + sc;
361   bitCount[10] = LO_LTAB(bc9_10) + sc;
362   bitCount[11] = bc11 + sc;
363 }
364 
365 /*****************************************************************************
366 
367     functionname: FDKaacEnc_count9_10_11
368     description:  counts tables 9-11
369     returns:
370     input:        quantized spectrum
371     output:       bitCount for tables 9-11
372 
373 *****************************************************************************/
374 
FDKaacEnc_count9_10_11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)375 static void FDKaacEnc_count9_10_11(const SHORT *const values, const INT width,
376                                    INT *RESTRICT bitCount) {
377   INT i;
378   INT bc9_10, bc11, sc;
379   INT t0, t1, t2, t3;
380 
381   bc9_10 = 0;
382   bc11 = 0;
383   sc = 0;
384 
385   DWORD_ALIGNED(values);
386 
387   for (i = 0; i < width; i += 4) {
388     t0 = values[i + 0];
389     t1 = values[i + 1];
390     t2 = values[i + 2];
391     t3 = values[i + 3];
392 
393     t0 = fixp_abs(t0);
394     sc += (t0 > 0);
395     t1 = fixp_abs(t1);
396     sc += (t1 > 0);
397     t2 = fixp_abs(t2);
398     sc += (t2 > 0);
399     t3 = fixp_abs(t3);
400     sc += (t3 > 0);
401 
402     bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
403               (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
404     bc11 +=
405         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
406   }
407 
408   bitCount[1] = INVALID_BITCOUNT;
409   bitCount[2] = INVALID_BITCOUNT;
410   bitCount[3] = INVALID_BITCOUNT;
411   bitCount[4] = INVALID_BITCOUNT;
412   bitCount[5] = INVALID_BITCOUNT;
413   bitCount[6] = INVALID_BITCOUNT;
414   bitCount[7] = INVALID_BITCOUNT;
415   bitCount[8] = INVALID_BITCOUNT;
416   bitCount[9] = HI_LTAB(bc9_10) + sc;
417   bitCount[10] = LO_LTAB(bc9_10) + sc;
418   bitCount[11] = bc11 + sc;
419 }
420 
421 /*****************************************************************************
422 
423     functionname: FDKaacEnc_count11
424     description:  counts table 11
425     returns:
426     input:        quantized spectrum
427     output:       bitCount for table 11
428 
429 *****************************************************************************/
430 
FDKaacEnc_count11(const SHORT * const values,const INT width,INT * RESTRICT bitCount)431 static void FDKaacEnc_count11(const SHORT *const values, const INT width,
432                               INT *RESTRICT bitCount) {
433   INT i;
434   INT bc11, sc;
435   INT t0, t1, t2, t3;
436 
437   bc11 = 0;
438   sc = 0;
439 
440   DWORD_ALIGNED(values);
441 
442   for (i = 0; i < width; i += 4) {
443     t0 = values[i + 0];
444     t1 = values[i + 1];
445     t2 = values[i + 2];
446     t3 = values[i + 3];
447 
448     t0 = fixp_abs(t0);
449     sc += (t0 > 0);
450     t1 = fixp_abs(t1);
451     sc += (t1 > 0);
452     t2 = fixp_abs(t2);
453     sc += (t2 > 0);
454     t3 = fixp_abs(t3);
455     sc += (t3 > 0);
456 
457     bc11 +=
458         (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
459   }
460 
461   bitCount[1] = INVALID_BITCOUNT;
462   bitCount[2] = INVALID_BITCOUNT;
463   bitCount[3] = INVALID_BITCOUNT;
464   bitCount[4] = INVALID_BITCOUNT;
465   bitCount[5] = INVALID_BITCOUNT;
466   bitCount[6] = INVALID_BITCOUNT;
467   bitCount[7] = INVALID_BITCOUNT;
468   bitCount[8] = INVALID_BITCOUNT;
469   bitCount[9] = INVALID_BITCOUNT;
470   bitCount[10] = INVALID_BITCOUNT;
471   bitCount[11] = bc11 + sc;
472 }
473 
474 /*****************************************************************************
475 
476     functionname: FDKaacEnc_countEsc
477     description:  counts table 11 (with Esc)
478     returns:
479     input:        quantized spectrum
480     output:       bitCount for tables 11 (with Esc)
481 
482 *****************************************************************************/
483 
FDKaacEnc_countEsc(const SHORT * const values,const INT width,INT * RESTRICT bitCount)484 static void FDKaacEnc_countEsc(const SHORT *const values, const INT width,
485                                INT *RESTRICT bitCount) {
486   INT i;
487   INT bc11, ec, sc;
488   INT t0, t1, t00, t01;
489 
490   bc11 = 0;
491   sc = 0;
492   ec = 0;
493   for (i = 0; i < width; i += 2) {
494     t0 = fixp_abs(values[i + 0]);
495     t1 = fixp_abs(values[i + 1]);
496 
497     sc += (t0 > 0) + (t1 > 0);
498 
499     t00 = fixMin(t0, 16);
500     t01 = fixMin(t1, 16);
501     bc11 += (INT)FDKaacEnc_huff_ltab11[t00][t01];
502 
503     if (t0 >= 16) {
504       ec += 5;
505       while ((t0 >>= 1) >= 16) ec += 2;
506     }
507 
508     if (t1 >= 16) {
509       ec += 5;
510       while ((t1 >>= 1) >= 16) ec += 2;
511     }
512   }
513 
514   for (i = 0; i < 11; i++) bitCount[i] = INVALID_BITCOUNT;
515 
516   bitCount[11] = bc11 + sc + ec;
517 }
518 
519 typedef void (*COUNT_FUNCTION)(const SHORT *const values, const INT width,
520                                INT *RESTRICT bitCount);
521 
522 static const COUNT_FUNCTION countFuncTable[CODE_BOOK_ESC_LAV + 1] = {
523 
524     FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 0  */
525     FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 1  */
526     FDKaacEnc_count3_4_5_6_7_8_9_10_11,     /* 2  */
527     FDKaacEnc_count5_6_7_8_9_10_11,         /* 3  */
528     FDKaacEnc_count5_6_7_8_9_10_11,         /* 4  */
529     FDKaacEnc_count7_8_9_10_11,             /* 5  */
530     FDKaacEnc_count7_8_9_10_11,             /* 6  */
531     FDKaacEnc_count7_8_9_10_11,             /* 7  */
532     FDKaacEnc_count9_10_11,                 /* 8  */
533     FDKaacEnc_count9_10_11,                 /* 9  */
534     FDKaacEnc_count9_10_11,                 /* 10 */
535     FDKaacEnc_count9_10_11,                 /* 11 */
536     FDKaacEnc_count9_10_11,                 /* 12 */
537     FDKaacEnc_count11,                      /* 13 */
538     FDKaacEnc_count11,                      /* 14 */
539     FDKaacEnc_count11,                      /* 15 */
540     FDKaacEnc_countEsc                      /* 16 */
541 };
542 
FDKaacEnc_bitCount(const SHORT * const values,const INT width,const INT maxVal,INT * const RESTRICT bitCount)543 INT FDKaacEnc_bitCount(const SHORT *const values, const INT width,
544                        const INT maxVal, INT *const RESTRICT bitCount) {
545   /*
546     check if we can use codebook 0
547   */
548 
549   bitCount[0] = (maxVal == 0) ? 0 : INVALID_BITCOUNT;
550 
551   countFuncTable[fixMin(maxVal, (INT)CODE_BOOK_ESC_LAV)](values, width,
552                                                          bitCount);
553 
554   return (0);
555 }
556 
557 /*
558   count difference between actual and zeroed lines
559 */
FDKaacEnc_countValues(SHORT * RESTRICT values,INT width,INT codeBook)560 INT FDKaacEnc_countValues(SHORT *RESTRICT values, INT width, INT codeBook) {
561   INT i, t0, t1, t2, t3;
562   INT bitCnt = 0;
563 
564   switch (codeBook) {
565     case CODE_BOOK_ZERO_NO:
566       break;
567 
568     case CODE_BOOK_1_NO:
569       for (i = 0; i < width; i += 4) {
570         t0 = values[i + 0];
571         t1 = values[i + 1];
572         t2 = values[i + 2];
573         t3 = values[i + 3];
574         bitCnt +=
575             HI_LTAB(FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1]);
576       }
577       break;
578 
579     case CODE_BOOK_2_NO:
580       for (i = 0; i < width; i += 4) {
581         t0 = values[i + 0];
582         t1 = values[i + 1];
583         t2 = values[i + 2];
584         t3 = values[i + 3];
585         bitCnt +=
586             LO_LTAB(FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1]);
587       }
588       break;
589 
590     case CODE_BOOK_3_NO:
591       for (i = 0; i < width; i += 4) {
592         t0 = fixp_abs(values[i + 0]);
593         bitCnt += (t0 > 0);
594         t1 = fixp_abs(values[i + 1]);
595         bitCnt += (t1 > 0);
596         t2 = fixp_abs(values[i + 2]);
597         bitCnt += (t2 > 0);
598         t3 = fixp_abs(values[i + 3]);
599         bitCnt += (t3 > 0);
600         bitCnt += HI_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
601       }
602       break;
603 
604     case CODE_BOOK_4_NO:
605       for (i = 0; i < width; i += 4) {
606         t0 = fixp_abs(values[i + 0]);
607         bitCnt += (t0 > 0);
608         t1 = fixp_abs(values[i + 1]);
609         bitCnt += (t1 > 0);
610         t2 = fixp_abs(values[i + 2]);
611         bitCnt += (t2 > 0);
612         t3 = fixp_abs(values[i + 3]);
613         bitCnt += (t3 > 0);
614         bitCnt += LO_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
615       }
616       break;
617 
618     case CODE_BOOK_5_NO:
619       for (i = 0; i < width; i += 4) {
620         t0 = values[i + 0];
621         t1 = values[i + 1];
622         t2 = values[i + 2];
623         t3 = values[i + 3];
624         bitCnt += HI_LTAB(FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4]) +
625                   HI_LTAB(FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4]);
626       }
627       break;
628 
629     case CODE_BOOK_6_NO:
630       for (i = 0; i < width; i += 4) {
631         t0 = values[i + 0];
632         t1 = values[i + 1];
633         t2 = values[i + 2];
634         t3 = values[i + 3];
635         bitCnt += LO_LTAB(FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4]) +
636                   LO_LTAB(FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4]);
637       }
638       break;
639 
640     case CODE_BOOK_7_NO:
641       for (i = 0; i < width; i += 4) {
642         t0 = fixp_abs(values[i + 0]);
643         bitCnt += (t0 > 0);
644         t1 = fixp_abs(values[i + 1]);
645         bitCnt += (t1 > 0);
646         t2 = fixp_abs(values[i + 2]);
647         bitCnt += (t2 > 0);
648         t3 = fixp_abs(values[i + 3]);
649         bitCnt += (t3 > 0);
650         bitCnt += HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]) +
651                   HI_LTAB(FDKaacEnc_huff_ltab7_8[t2][t3]);
652       }
653       break;
654 
655     case CODE_BOOK_8_NO:
656       for (i = 0; i < width; i += 4) {
657         t0 = fixp_abs(values[i + 0]);
658         bitCnt += (t0 > 0);
659         t1 = fixp_abs(values[i + 1]);
660         bitCnt += (t1 > 0);
661         t2 = fixp_abs(values[i + 2]);
662         bitCnt += (t2 > 0);
663         t3 = fixp_abs(values[i + 3]);
664         bitCnt += (t3 > 0);
665         bitCnt += LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]) +
666                   LO_LTAB(FDKaacEnc_huff_ltab7_8[t2][t3]);
667       }
668       break;
669 
670     case CODE_BOOK_9_NO:
671       for (i = 0; i < width; i += 4) {
672         t0 = fixp_abs(values[i + 0]);
673         bitCnt += (t0 > 0);
674         t1 = fixp_abs(values[i + 1]);
675         bitCnt += (t1 > 0);
676         t2 = fixp_abs(values[i + 2]);
677         bitCnt += (t2 > 0);
678         t3 = fixp_abs(values[i + 3]);
679         bitCnt += (t3 > 0);
680         bitCnt += HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]) +
681                   HI_LTAB(FDKaacEnc_huff_ltab9_10[t2][t3]);
682       }
683       break;
684 
685     case CODE_BOOK_10_NO:
686       for (i = 0; i < width; i += 4) {
687         t0 = fixp_abs(values[i + 0]);
688         bitCnt += (t0 > 0);
689         t1 = fixp_abs(values[i + 1]);
690         bitCnt += (t1 > 0);
691         t2 = fixp_abs(values[i + 2]);
692         bitCnt += (t2 > 0);
693         t3 = fixp_abs(values[i + 3]);
694         bitCnt += (t3 > 0);
695         bitCnt += LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]) +
696                   LO_LTAB(FDKaacEnc_huff_ltab9_10[t2][t3]);
697       }
698       break;
699 
700     case CODE_BOOK_ESC_NO:
701       for (i = 0; i < width; i += 2) {
702         t0 = fixp_abs(values[i + 0]);
703         bitCnt += (t0 > 0);
704         t1 = fixp_abs(values[i + 1]);
705         bitCnt += (t1 > 0);
706         bitCnt += (INT)FDKaacEnc_huff_ltab11[fixMin(t0, 16)][fixMin(t1, 16)];
707         if (t0 >= 16) {
708           bitCnt += 5;
709           while ((t0 >>= 1) >= 16) bitCnt += 2;
710         }
711         if (t1 >= 16) {
712           bitCnt += 5;
713           while ((t1 >>= 1) >= 16) bitCnt += 2;
714         }
715       }
716       break;
717 
718     default:
719       break;
720   }
721 
722   return (bitCnt);
723 }
724 
FDKaacEnc_codeValues(SHORT * RESTRICT values,INT width,INT codeBook,HANDLE_FDK_BITSTREAM hBitstream)725 INT FDKaacEnc_codeValues(SHORT *RESTRICT values, INT width, INT codeBook,
726                          HANDLE_FDK_BITSTREAM hBitstream) {
727   INT i, t0, t1, t2, t3, t00, t01;
728   INT codeWord, codeLength;
729   INT sign, signLength;
730 
731   DWORD_ALIGNED(values);
732 
733   switch (codeBook) {
734     case CODE_BOOK_ZERO_NO:
735       break;
736 
737     case CODE_BOOK_1_NO:
738       for (i = 0; i < width; i += 4) {
739         t0 = values[i + 0] + 1;
740         t1 = values[i + 1] + 1;
741         t2 = values[i + 2] + 1;
742         t3 = values[i + 3] + 1;
743         codeWord = FDKaacEnc_huff_ctab1[t0][t1][t2][t3];
744         codeLength = HI_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
745         FDKwriteBits(hBitstream, codeWord, codeLength);
746       }
747       break;
748 
749     case CODE_BOOK_2_NO:
750       for (i = 0; i < width; i += 4) {
751         t0 = values[i + 0] + 1;
752         t1 = values[i + 1] + 1;
753         t2 = values[i + 2] + 1;
754         t3 = values[i + 3] + 1;
755         codeWord = FDKaacEnc_huff_ctab2[t0][t1][t2][t3];
756         codeLength = LO_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
757         FDKwriteBits(hBitstream, codeWord, codeLength);
758       }
759       break;
760 
761     case CODE_BOOK_3_NO:
762       for (i = 0; i < (width >> 2); i++) {
763         sign = 0;
764         signLength = 0;
765         int index[4];
766         for (int j = 0; j < 4; j++) {
767           int ti = *values++;
768           int zero = (ti == 0) ? 0 : 1;
769           signLength += zero;
770           sign = (sign << zero) + ((UINT)ti >> 31);
771           index[j] = fixp_abs(ti);
772         }
773         codeWord = FDKaacEnc_huff_ctab3[index[0]][index[1]][index[2]][index[3]];
774         codeLength = HI_LTAB(
775             FDKaacEnc_huff_ltab3_4[index[0]][index[1]][index[2]][index[3]]);
776         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
777                      codeLength + signLength);
778       }
779       break;
780 
781     case CODE_BOOK_4_NO:
782       for (i = 0; i < width; i += 4) {
783         sign = 0;
784         signLength = 0;
785         int index[4];
786         for (int j = 0; j < 4; j++) {
787           int ti = *values++;
788           int zero = (ti == 0) ? 0 : 1;
789           signLength += zero;
790           sign = (sign << zero) + ((UINT)ti >> 31);
791           index[j] = fixp_abs(ti);
792         }
793         codeWord = FDKaacEnc_huff_ctab4[index[0]][index[1]][index[2]][index[3]];
794         codeLength = LO_LTAB(
795             FDKaacEnc_huff_ltab3_4[index[0]][index[1]][index[2]][index[3]]);
796         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
797                      codeLength + signLength);
798       }
799       break;
800 
801     case CODE_BOOK_5_NO:
802       for (i = 0; i < (width >> 2); i++) {
803         t0 = *values++ + 4;
804         t1 = *values++ + 4;
805         t2 = *values++ + 4;
806         t3 = *values++ + 4;
807         codeWord = FDKaacEnc_huff_ctab5[t0][t1];
808         codeLength =
809             HI_LTAB(FDKaacEnc_huff_ltab5_6[t2][t3]); /* length of 2nd cw */
810         codeWord = (codeWord << codeLength) + FDKaacEnc_huff_ctab5[t2][t3];
811         codeLength += HI_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
812         FDKwriteBits(hBitstream, codeWord, codeLength);
813       }
814       break;
815 
816     case CODE_BOOK_6_NO:
817       for (i = 0; i < (width >> 2); i++) {
818         t0 = *values++ + 4;
819         t1 = *values++ + 4;
820         t2 = *values++ + 4;
821         t3 = *values++ + 4;
822         codeWord = FDKaacEnc_huff_ctab6[t0][t1];
823         codeLength =
824             LO_LTAB(FDKaacEnc_huff_ltab5_6[t2][t3]); /* length of 2nd cw */
825         codeWord = (codeWord << codeLength) + FDKaacEnc_huff_ctab6[t2][t3];
826         codeLength += LO_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
827         FDKwriteBits(hBitstream, codeWord, codeLength);
828       }
829       break;
830 
831     case CODE_BOOK_7_NO:
832       for (i = 0; i < (width >> 1); i++) {
833         t0 = *values++;
834         sign = ((UINT)t0 >> 31);
835         t0 = fixp_abs(t0);
836         signLength = (t0 == 0) ? 0 : 1;
837         t1 = *values++;
838         INT zero = (t1 == 0) ? 0 : 1;
839         signLength += zero;
840         sign = (sign << zero) + ((UINT)t1 >> 31);
841         t1 = fixp_abs(t1);
842         codeWord = FDKaacEnc_huff_ctab7[t0][t1];
843         codeLength = HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
844         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
845                      codeLength + signLength);
846       }
847       break;
848 
849     case CODE_BOOK_8_NO:
850       for (i = 0; i < (width >> 1); i++) {
851         t0 = *values++;
852         sign = ((UINT)t0 >> 31);
853         t0 = fixp_abs(t0);
854         signLength = (t0 == 0) ? 0 : 1;
855         t1 = *values++;
856         INT zero = (t1 == 0) ? 0 : 1;
857         signLength += zero;
858         sign = (sign << zero) + ((UINT)t1 >> 31);
859         t1 = fixp_abs(t1);
860         codeWord = FDKaacEnc_huff_ctab8[t0][t1];
861         codeLength = LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
862         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
863                      codeLength + signLength);
864       }
865       break;
866 
867     case CODE_BOOK_9_NO:
868       for (i = 0; i < (width >> 1); i++) {
869         t0 = *values++;
870         sign = ((UINT)t0 >> 31);
871         t0 = fixp_abs(t0);
872         signLength = (t0 == 0) ? 0 : 1;
873         t1 = *values++;
874         INT zero = (t1 == 0) ? 0 : 1;
875         signLength += zero;
876         sign = (sign << zero) + ((UINT)t1 >> 31);
877         t1 = fixp_abs(t1);
878         codeWord = FDKaacEnc_huff_ctab9[t0][t1];
879         codeLength = HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
880         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
881                      codeLength + signLength);
882       }
883       break;
884 
885     case CODE_BOOK_10_NO:
886       for (i = 0; i < (width >> 1); i++) {
887         t0 = *values++;
888         sign = ((UINT)t0 >> 31);
889         t0 = fixp_abs(t0);
890         signLength = (t0 == 0) ? 0 : 1;
891         t1 = *values++;
892         INT zero = (t1 == 0) ? 0 : 1;
893         signLength += zero;
894         sign = (sign << zero) + ((UINT)t1 >> 31);
895         t1 = fixp_abs(t1);
896         codeWord = FDKaacEnc_huff_ctab10[t0][t1];
897         codeLength = LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
898         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
899                      codeLength + signLength);
900       }
901       break;
902 
903     case CODE_BOOK_ESC_NO:
904       for (i = 0; i < (width >> 1); i++) {
905         t0 = *values++;
906         sign = ((UINT)t0 >> 31);
907         t0 = fixp_abs(t0);
908         signLength = (t0 == 0) ? 0 : 1;
909         t1 = *values++;
910         INT zero = (t1 == 0) ? 0 : 1;
911         signLength += zero;
912         sign = (sign << zero) + ((UINT)t1 >> 31);
913         t1 = fixp_abs(t1);
914 
915         t00 = fixMin(t0, 16);
916         t01 = fixMin(t1, 16);
917 
918         codeWord = FDKaacEnc_huff_ctab11[t00][t01];
919         codeLength = (INT)FDKaacEnc_huff_ltab11[t00][t01];
920         FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
921                      codeLength + signLength);
922         for (int j = 0; j < 2; j++) {
923           if (t0 >= 16) {
924             INT n = 4, p = t0;
925             for (; (p >>= 1) >= 16;) n++;
926             FDKwriteBits(hBitstream,
927                          (((1 << (n - 3)) - 2) << n) | (t0 - (1 << n)),
928                          n + n - 3);
929           }
930           t0 = t1;
931         }
932       }
933       break;
934 
935     default:
936       break;
937   }
938   return (0);
939 }
940 
FDKaacEnc_codeScalefactorDelta(INT delta,HANDLE_FDK_BITSTREAM hBitstream)941 INT FDKaacEnc_codeScalefactorDelta(INT delta, HANDLE_FDK_BITSTREAM hBitstream) {
942   INT codeWord, codeLength;
943 
944   if (fixp_abs(delta) > CODE_BOOK_SCF_LAV) return (1);
945 
946   codeWord = FDKaacEnc_huff_ctabscf[delta + CODE_BOOK_SCF_LAV];
947   codeLength = (INT)FDKaacEnc_huff_ltabscf[delta + CODE_BOOK_SCF_LAV];
948   FDKwriteBits(hBitstream, codeWord, codeLength);
949   return (0);
950 }
951