Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libAACenc / src / bit_cnt.cpp
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: Huffman Bitcounter & coder
88
89 ******************************************************************************/
90
91 #include "bit_cnt.h"
92
93 #include "aacEnc_ram.h"
94
95 #define HI_LTAB(a) (a>>16)
96 #define LO_LTAB(a) (a & 0xffff)
97
98 /*****************************************************************************
99
100
101 functionname: FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11
102 description: counts tables 1-11
103 returns:
104 input: quantized spectrum
105 output: bitCount for tables 1-11
106
107 *****************************************************************************/
108
109 static void FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11(const SHORT *RESTRICT values,
110 const INT width,
111 INT *bitCount)
112 {
113
114 INT i;
115 INT bc1_2,bc3_4,bc5_6,bc7_8,bc9_10,bc11,sc;
116 INT t0,t1,t2,t3;
117 bc1_2=0;
118 bc3_4=0;
119 bc5_6=0;
120 bc7_8=0;
121 bc9_10=0;
122 bc11=0;
123 sc=0;
124
125 for(i=0;i<width;i+=4){
126
127 t0= values[i+0];
128 t1= values[i+1];
129 t2= values[i+2];
130 t3= values[i+3];
131
132 /* 1,2 */
133
134 bc1_2+=FDKaacEnc_huff_ltab1_2[t0+1][t1+1][t2+1][t3+1];
135
136 /* 5,6 */
137 bc5_6+=FDKaacEnc_huff_ltab5_6[t0+4][t1+4];
138 bc5_6+=FDKaacEnc_huff_ltab5_6[t2+4][t3+4];
139
140 t0=fixp_abs(t0);
141 t1=fixp_abs(t1);
142 t2=fixp_abs(t2);
143 t3=fixp_abs(t3);
144
145
146 bc3_4+= FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
147
148 bc7_8+=FDKaacEnc_huff_ltab7_8[t0][t1];
149 bc7_8+=FDKaacEnc_huff_ltab7_8[t2][t3];
150
151 bc9_10+=FDKaacEnc_huff_ltab9_10[t0][t1];
152 bc9_10+=FDKaacEnc_huff_ltab9_10[t2][t3];
153
154 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
155 bc11+= (INT) FDKaacEnc_huff_ltab11[t2][t3];
156
157 sc+=(t0>0)+(t1>0)+(t2>0)+(t3>0);
158 }
159
160 bitCount[1]=HI_LTAB(bc1_2);
161 bitCount[2]=LO_LTAB(bc1_2);
162 bitCount[3]=HI_LTAB(bc3_4)+sc;
163 bitCount[4]=LO_LTAB(bc3_4)+sc;
164 bitCount[5]=HI_LTAB(bc5_6);
165 bitCount[6]=LO_LTAB(bc5_6);
166 bitCount[7]=HI_LTAB(bc7_8)+sc;
167 bitCount[8]=LO_LTAB(bc7_8)+sc;
168 bitCount[9]=HI_LTAB(bc9_10)+sc;
169 bitCount[10]=LO_LTAB(bc9_10)+sc;
170 bitCount[11]=bc11+sc;
171
172 }
173
174
175 /*****************************************************************************
176
177 functionname: FDKaacEnc_count3_4_5_6_7_8_9_10_11
178 description: counts tables 3-11
179 returns:
180 input: quantized spectrum
181 output: bitCount for tables 3-11
182
183 *****************************************************************************/
184
185 static void FDKaacEnc_count3_4_5_6_7_8_9_10_11(const SHORT *RESTRICT values,
186 const INT width,
187 INT *bitCount)
188 {
189
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 for(i=0;i<width;i+=4){
202
203 t0= values[i+0];
204 t1= values[i+1];
205 t2= values[i+2];
206 t3= values[i+3];
207
208 bc5_6+=FDKaacEnc_huff_ltab5_6[t0+4][t1+4];
209 bc5_6+=FDKaacEnc_huff_ltab5_6[t2+4][t3+4];
210
211 t0=fixp_abs(t0);
212 t1=fixp_abs(t1);
213 t2=fixp_abs(t2);
214 t3=fixp_abs(t3);
215
216 bc3_4+= FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
217
218 bc7_8+=FDKaacEnc_huff_ltab7_8[t0][t1];
219 bc7_8+=FDKaacEnc_huff_ltab7_8[t2][t3];
220
221 bc9_10+=FDKaacEnc_huff_ltab9_10[t0][t1];
222 bc9_10+=FDKaacEnc_huff_ltab9_10[t2][t3];
223
224 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
225 bc11+= (INT) FDKaacEnc_huff_ltab11[t2][t3];
226
227 sc+=(t0>0)+(t1>0)+(t2>0)+(t3>0);
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 /*****************************************************************************
246
247 functionname: FDKaacEnc_count5_6_7_8_9_10_11
248 description: counts tables 5-11
249 returns:
250 input: quantized spectrum
251 output: bitCount for tables 5-11
252
253 *****************************************************************************/
254
255
256 static void FDKaacEnc_count5_6_7_8_9_10_11(const SHORT *RESTRICT values,
257 const INT width,
258 INT *bitCount)
259 {
260
261 INT i;
262 INT bc5_6,bc7_8,bc9_10,bc11,sc;
263 INT t0,t1;
264 bc5_6=0;
265 bc7_8=0;
266 bc9_10=0;
267 bc11=0;
268 sc=0;
269
270 for(i=0;i<width;i+=2){
271
272 t0 = values[i+0];
273 t1 = values[i+1];
274
275 bc5_6+=FDKaacEnc_huff_ltab5_6[t0+4][t1+4];
276
277 t0=fixp_abs(t0);
278 t1=fixp_abs(t1);
279
280 bc7_8+=FDKaacEnc_huff_ltab7_8[t0][t1];
281 bc9_10+=FDKaacEnc_huff_ltab9_10[t0][t1];
282 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
283
284 sc+=(t0>0)+(t1>0);
285 }
286 bitCount[1]=INVALID_BITCOUNT;
287 bitCount[2]=INVALID_BITCOUNT;
288 bitCount[3]=INVALID_BITCOUNT;
289 bitCount[4]=INVALID_BITCOUNT;
290 bitCount[5]=HI_LTAB(bc5_6);
291 bitCount[6]=LO_LTAB(bc5_6);
292 bitCount[7]=HI_LTAB(bc7_8)+sc;
293 bitCount[8]=LO_LTAB(bc7_8)+sc;
294 bitCount[9]=HI_LTAB(bc9_10)+sc;
295 bitCount[10]=LO_LTAB(bc9_10)+sc;
296 bitCount[11]=bc11+sc;
297
298 }
299
300
301 /*****************************************************************************
302
303 functionname: FDKaacEnc_count7_8_9_10_11
304 description: counts tables 7-11
305 returns:
306 input: quantized spectrum
307 output: bitCount for tables 7-11
308
309 *****************************************************************************/
310
311 static void FDKaacEnc_count7_8_9_10_11(const SHORT *RESTRICT values,
312 const INT width,
313 INT *bitCount)
314 {
315
316 INT i;
317 INT bc7_8,bc9_10,bc11,sc;
318 INT t0,t1;
319
320 bc7_8=0;
321 bc9_10=0;
322 bc11=0;
323 sc=0;
324
325 for(i=0;i<width;i+=2){
326 t0=fixp_abs(values[i+0]);
327 t1=fixp_abs(values[i+1]);
328
329 bc7_8+=FDKaacEnc_huff_ltab7_8[t0][t1];
330 bc9_10+=FDKaacEnc_huff_ltab9_10[t0][t1];
331 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
332 sc+=(t0>0)+(t1>0);
333 }
334
335 bitCount[1]=INVALID_BITCOUNT;
336 bitCount[2]=INVALID_BITCOUNT;
337 bitCount[3]=INVALID_BITCOUNT;
338 bitCount[4]=INVALID_BITCOUNT;
339 bitCount[5]=INVALID_BITCOUNT;
340 bitCount[6]=INVALID_BITCOUNT;
341 bitCount[7]=HI_LTAB(bc7_8)+sc;
342 bitCount[8]=LO_LTAB(bc7_8)+sc;
343 bitCount[9]=HI_LTAB(bc9_10)+sc;
344 bitCount[10]=LO_LTAB(bc9_10)+sc;
345 bitCount[11]=bc11+sc;
346
347 }
348
349 /*****************************************************************************
350
351 functionname: FDKaacEnc_count9_10_11
352 description: counts tables 9-11
353 returns:
354 input: quantized spectrum
355 output: bitCount for tables 9-11
356
357 *****************************************************************************/
358
359
360
361 static void FDKaacEnc_count9_10_11(const SHORT *RESTRICT values,
362 const INT width,
363 INT *bitCount)
364 {
365
366 INT i;
367 INT bc9_10,bc11,sc;
368 INT t0,t1;
369
370 bc9_10=0;
371 bc11=0;
372 sc=0;
373
374 for(i=0;i<width;i+=2){
375 t0=fixp_abs(values[i+0]);
376 t1=fixp_abs(values[i+1]);
377
378 bc9_10+=FDKaacEnc_huff_ltab9_10[t0][t1];
379 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
380
381 sc+=(t0>0)+(t1>0);
382 }
383
384 bitCount[1]=INVALID_BITCOUNT;
385 bitCount[2]=INVALID_BITCOUNT;
386 bitCount[3]=INVALID_BITCOUNT;
387 bitCount[4]=INVALID_BITCOUNT;
388 bitCount[5]=INVALID_BITCOUNT;
389 bitCount[6]=INVALID_BITCOUNT;
390 bitCount[7]=INVALID_BITCOUNT;
391 bitCount[8]=INVALID_BITCOUNT;
392 bitCount[9]=HI_LTAB(bc9_10)+sc;
393 bitCount[10]=LO_LTAB(bc9_10)+sc;
394 bitCount[11]=bc11+sc;
395
396 }
397
398 /*****************************************************************************
399
400 functionname: FDKaacEnc_count11
401 description: counts table 11
402 returns:
403 input: quantized spectrum
404 output: bitCount for table 11
405
406 *****************************************************************************/
407
408 static void FDKaacEnc_count11(const SHORT *RESTRICT values,
409 const INT width,
410 INT *bitCount)
411 {
412
413 INT i;
414 INT bc11,sc;
415 INT t0,t1;
416
417 bc11=0;
418 sc=0;
419 for(i=0;i<width;i+=2){
420 t0=fixp_abs(values[i+0]);
421 t1=fixp_abs(values[i+1]);
422 bc11+= (INT) FDKaacEnc_huff_ltab11[t0][t1];
423 sc+=(t0>0)+(t1>0);
424 }
425
426 bitCount[1]=INVALID_BITCOUNT;
427 bitCount[2]=INVALID_BITCOUNT;
428 bitCount[3]=INVALID_BITCOUNT;
429 bitCount[4]=INVALID_BITCOUNT;
430 bitCount[5]=INVALID_BITCOUNT;
431 bitCount[6]=INVALID_BITCOUNT;
432 bitCount[7]=INVALID_BITCOUNT;
433 bitCount[8]=INVALID_BITCOUNT;
434 bitCount[9]=INVALID_BITCOUNT;
435 bitCount[10]=INVALID_BITCOUNT;
436 bitCount[11]=bc11+sc;
437 }
438
439 /*****************************************************************************
440
441 functionname: FDKaacEnc_countEsc
442 description: counts table 11 (with Esc)
443 returns:
444 input: quantized spectrum
445 output: bitCount for tables 11 (with Esc)
446
447 *****************************************************************************/
448
449 static void FDKaacEnc_countEsc(const SHORT *RESTRICT values,
450 const INT width,
451 INT *RESTRICT bitCount)
452 {
453
454 INT i;
455 INT bc11,ec,sc;
456 INT t0,t1,t00,t01;
457
458 bc11=0;
459 sc=0;
460 ec=0;
461 for(i=0;i<width;i+=2){
462 t0=fixp_abs(values[i+0]);
463 t1=fixp_abs(values[i+1]);
464
465 sc+=(t0>0)+(t1>0);
466
467 t00 = fixMin(t0,16);
468 t01 = fixMin(t1,16);
469 bc11+= (INT) FDKaacEnc_huff_ltab11[t00][t01];
470
471 if(t0>=16){
472 ec+=5;
473 while((t0>>=1) >= 16)
474 ec+=2;
475 }
476
477 if(t1>=16){
478 ec+=5;
479 while((t1>>=1) >= 16)
480 ec+=2;
481 }
482 }
483
484 for (i=0; i<11; i++)
485 bitCount[i]=INVALID_BITCOUNT;
486
487 bitCount[11]=bc11+sc+ec;
488 }
489
490
491 typedef void (*COUNT_FUNCTION)(const SHORT *RESTRICT values,
492 const INT width,
493 INT *RESTRICT bitCount);
494
495 static const COUNT_FUNCTION countFuncTable[CODE_BOOK_ESC_LAV+1] =
496 {
497
498 FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 0 */
499 FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 1 */
500 FDKaacEnc_count3_4_5_6_7_8_9_10_11, /* 2 */
501 FDKaacEnc_count5_6_7_8_9_10_11, /* 3 */
502 FDKaacEnc_count5_6_7_8_9_10_11, /* 4 */
503 FDKaacEnc_count7_8_9_10_11, /* 5 */
504 FDKaacEnc_count7_8_9_10_11, /* 6 */
505 FDKaacEnc_count7_8_9_10_11, /* 7 */
506 FDKaacEnc_count9_10_11, /* 8 */
507 FDKaacEnc_count9_10_11, /* 9 */
508 FDKaacEnc_count9_10_11, /* 10 */
509 FDKaacEnc_count9_10_11, /* 11 */
510 FDKaacEnc_count9_10_11, /* 12 */
511 FDKaacEnc_count11, /* 13 */
512 FDKaacEnc_count11, /* 14 */
513 FDKaacEnc_count11, /* 15 */
514 FDKaacEnc_countEsc /* 16 */
515 };
516
517
518
519 INT FDKaacEnc_bitCount(const SHORT *values,
520 const INT width,
521 INT maxVal,
522 INT *bitCount)
523 {
524
525 /*
526 check if we can use codebook 0
527 */
528
529 if(maxVal == 0)
530 bitCount[0] = 0;
531 else
532 bitCount[0] = INVALID_BITCOUNT;
533
534 maxVal = fixMin(maxVal,(INT)CODE_BOOK_ESC_LAV);
535 countFuncTable[maxVal](values,width,bitCount);
536 return(0);
537 }
538
539
540
541
542 /*
543 count difference between actual and zeroed lines
544 */
545 INT FDKaacEnc_countValues(SHORT *RESTRICT values, INT width, INT codeBook)
546 {
547
548 INT i,t0,t1,t2,t3,t00,t01;
549 INT codeLength;
550 INT signLength;
551 INT bitCnt=0;
552
553 switch(codeBook){
554 case CODE_BOOK_ZERO_NO:
555 break;
556
557 case CODE_BOOK_1_NO:
558 for(i=0; i<width; i+=4) {
559 t0 = values[i+0];
560 t1 = values[i+1];
561 t2 = values[i+2];
562 t3 = values[i+3];
563 codeLength = HI_LTAB(FDKaacEnc_huff_ltab1_2[t0+1][t1+1][t2+1][t3+1]);
564 bitCnt+= codeLength;
565 }
566 break;
567
568 case CODE_BOOK_2_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 codeLength = LO_LTAB(FDKaacEnc_huff_ltab1_2[t0+1][t1+1][t2+1][t3+1]);
575 bitCnt+= codeLength;
576 }
577 break;
578
579 case CODE_BOOK_3_NO:
580 for(i=0; i<width; i+=4) {
581 signLength=0;
582 t0 = values[i+0];
583 if(t0 != 0){
584 signLength++;
585 t0=fixp_abs(t0);
586 }
587 t1 = values[i+1];
588 if(t1 != 0){
589 signLength++;
590 t1=fixp_abs(t1);
591 }
592 t2 = values[i+2];
593 if(t2 != 0){
594 signLength++;
595 t2=fixp_abs(t2);
596 }
597 t3 = values[i+3];
598 if(t3 != 0){
599 signLength++;
600 t3=fixp_abs(t3);
601 }
602
603 codeLength = HI_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
604 bitCnt+=codeLength+signLength;
605 }
606 break;
607
608 case CODE_BOOK_4_NO:
609 for(i=0; i<width; i+=4) {
610 signLength=0;
611 t0 = values[i+0];
612 if(t0 != 0){
613 signLength++;
614 t0=fixp_abs(t0);
615 }
616 t1 = values[i+1];
617 if(t1 != 0){
618 signLength++;
619 t1=fixp_abs(t1);
620 }
621 t2 = values[i+2];
622 if(t2 != 0){
623 signLength++;
624 t2=fixp_abs(t2);
625 }
626 t3 = values[i+3];
627 if(t3 != 0){
628 signLength++;
629 t3=fixp_abs(t3);
630 }
631 codeLength = LO_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
632 bitCnt+=codeLength+signLength;
633 }
634 break;
635
636 case CODE_BOOK_5_NO:
637 for(i=0; i<width; i+=2) {
638 t0 = values[i+0];
639 t1 = values[i+1];
640 codeLength = HI_LTAB(FDKaacEnc_huff_ltab5_6[t0+4][t1+4]);
641 bitCnt+=codeLength;
642 }
643 break;
644
645 case CODE_BOOK_6_NO:
646 for(i=0; i<width; i+=2) {
647 t0 = values[i+0];
648 t1 = values[i+1];
649 codeLength = LO_LTAB(FDKaacEnc_huff_ltab5_6[t0+4][t1+4]);
650 bitCnt+=codeLength;
651 }
652 break;
653
654 case CODE_BOOK_7_NO:
655 for(i=0; i<width; i+=2){
656 signLength=0;
657 t0 = values[i+0];
658 if(t0 != 0){
659 signLength++;
660 t0=fixp_abs(t0);
661 }
662
663 t1 = values[i+1];
664 if(t1 != 0){
665 signLength++;
666 t1=fixp_abs(t1);
667 }
668 codeLength = HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
669 bitCnt+=codeLength +signLength;
670 }
671 break;
672
673 case CODE_BOOK_8_NO:
674 for(i=0; i<width; i+=2) {
675 signLength=0;
676 t0 = values[i+0];
677 if(t0 != 0){
678 signLength++;
679 t0=fixp_abs(t0);
680 }
681
682 t1 = values[i+1];
683 if(t1 != 0){
684 signLength++;
685 t1=fixp_abs(t1);
686 }
687 codeLength = LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
688 bitCnt+=codeLength +signLength;
689 }
690 break;
691
692 case CODE_BOOK_9_NO:
693 for(i=0; i<width; i+=2) {
694 signLength=0;
695 t0 = values[i+0];
696 if(t0 != 0){
697 signLength++;
698 t0=fixp_abs(t0);
699 }
700 t1 = values[i+1];
701 if(t1 != 0){
702 signLength++;
703 t1=fixp_abs(t1);
704 }
705 codeLength = HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
706 bitCnt+=codeLength +signLength;
707 }
708 break;
709
710 case CODE_BOOK_10_NO:
711 for(i=0; i<width; i+=2) {
712 signLength=0;
713 t0 = values[i+0];
714 if(t0 != 0){
715 signLength++;
716 t0=fixp_abs(t0);
717 }
718
719 t1 = values[i+1];
720 if(t1 != 0){
721 signLength++;
722 t1=fixp_abs(t1);
723 }
724 codeLength = LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
725 bitCnt+=codeLength +signLength;
726 }
727 break;
728
729 case CODE_BOOK_ESC_NO:
730 for(i=0; i<width; i+=2) {
731 signLength=0;
732 t0 = values[i+0];
733 if(t0 != 0){
734 signLength++;
735 t0=fixp_abs(t0);
736 }
737 t1 = values[i+1];
738 if(t1 != 0){
739 signLength++;
740 t1=fixp_abs(t1);
741 }
742 t00 = fixMin(t0,16);
743 t01 = fixMin(t1,16);
744
745 codeLength = (INT) FDKaacEnc_huff_ltab11[t00][t01];
746 bitCnt+=codeLength +signLength;
747 if(t0 >=16){
748 INT n,p;
749 n=0;
750 p=t0;
751 while((p>>=1) >=16){
752 bitCnt++;
753 n++;
754 }
755 bitCnt+=(n+5);
756 }
757 if(t1 >=16){
758 INT n,p;
759 n=0;
760 p=t1;
761 while((p>>=1) >=16){
762 bitCnt++;
763 n++;
764 }
765 bitCnt+=(n+5);
766 }
767 }
768 break;
769
770 default:
771 break;
772 }
773
774 return(bitCnt);
775 }
776
777
778
779 INT FDKaacEnc_codeValues(SHORT *RESTRICT values, INT width, INT codeBook, HANDLE_FDK_BITSTREAM hBitstream)
780 {
781
782 INT i,t0,t1,t2,t3,t00,t01;
783 INT codeWord,codeLength;
784 INT sign,signLength;
785
786 switch(codeBook){
787 case CODE_BOOK_ZERO_NO:
788 break;
789
790 case CODE_BOOK_1_NO:
791 for(i=0; i<width; i+=4) {
792 t0 = values[i+0]+1;
793 t1 = values[i+1]+1;
794 t2 = values[i+2]+1;
795 t3 = values[i+3]+1;
796 codeWord = FDKaacEnc_huff_ctab1[t0][t1][t2][t3];
797 codeLength = HI_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
798 FDKwriteBits(hBitstream,codeWord,codeLength);
799 }
800 break;
801
802 case CODE_BOOK_2_NO:
803 for(i=0; i<width; i+=4) {
804 t0 = values[i+0]+1;
805 t1 = values[i+1]+1;
806 t2 = values[i+2]+1;
807 t3 = values[i+3]+1;
808 codeWord = FDKaacEnc_huff_ctab2[t0][t1][t2][t3];
809 codeLength = LO_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
810 FDKwriteBits(hBitstream,codeWord,codeLength);
811 }
812 break;
813
814 case CODE_BOOK_3_NO:
815 for(i=0; i<width; i+=4) {
816 sign=0;
817 signLength=0;
818 t0 = values[i+0];
819 if(t0 != 0){
820 signLength++;
821 sign<<=1;
822 if(t0 < 0){
823 sign|=1;
824 t0=fixp_abs(t0);
825 }
826 }
827 t1 = values[i+1];
828 if(t1 != 0){
829 signLength++;
830 sign<<=1;
831 if(t1 < 0){
832 sign|=1;
833 t1=fixp_abs(t1);
834 }
835 }
836 t2 = values[i+2];
837 if(t2 != 0){
838 signLength++;
839 sign<<=1;
840 if(t2 < 0){
841 sign|=1;
842 t2=fixp_abs(t2);
843 }
844 }
845 t3 = values[i+3];
846 if(t3 != 0){
847 signLength++;
848 sign<<=1;
849 if(t3 < 0){
850 sign|=1;
851 t3=fixp_abs(t3);
852 }
853 }
854
855 codeWord = FDKaacEnc_huff_ctab3[t0][t1][t2][t3];
856 codeLength = HI_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
857 FDKwriteBits(hBitstream,codeWord,codeLength);
858 FDKwriteBits(hBitstream,sign,signLength);
859 }
860 break;
861
862 case CODE_BOOK_4_NO:
863 for(i=0; i<width; i+=4) {
864 sign=0;
865 signLength=0;
866 t0 = values[i+0];
867 if(t0 != 0){
868 signLength++;
869 sign<<=1;
870 if(t0 < 0){
871 sign|=1;
872 t0=fixp_abs(t0);
873 }
874 }
875 t1 = values[i+1];
876 if(t1 != 0){
877 signLength++;
878 sign<<=1;
879 if(t1 < 0){
880 sign|=1;
881 t1=fixp_abs(t1);
882 }
883 }
884 t2 = values[i+2];
885 if(t2 != 0){
886 signLength++;
887 sign<<=1;
888 if(t2 < 0){
889 sign|=1;
890 t2=fixp_abs(t2);
891 }
892 }
893 t3 = values[i+3];
894 if(t3 != 0){
895 signLength++;
896 sign<<=1;
897 if(t3 < 0){
898 sign|=1;
899 t3=fixp_abs(t3);
900 }
901 }
902 codeWord = FDKaacEnc_huff_ctab4[t0][t1][t2][t3];
903 codeLength = LO_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
904 FDKwriteBits(hBitstream,codeWord,codeLength);
905 FDKwriteBits(hBitstream,sign,signLength);
906 }
907 break;
908
909 case CODE_BOOK_5_NO:
910 for(i=0; i<width; i+=2) {
911 t0 = values[i+0]+4;
912 t1 = values[i+1]+4;
913 codeWord = FDKaacEnc_huff_ctab5[t0][t1];
914 codeLength = HI_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
915 FDKwriteBits(hBitstream,codeWord,codeLength);
916 }
917 break;
918
919 case CODE_BOOK_6_NO:
920 for(i=0; i<width; i+=2) {
921 t0 = values[i+0]+4;
922 t1 = values[i+1]+4;
923 codeWord = FDKaacEnc_huff_ctab6[t0][t1];
924 codeLength = LO_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
925 FDKwriteBits(hBitstream,codeWord,codeLength);
926 }
927 break;
928
929 case CODE_BOOK_7_NO:
930 for(i=0; i<width; i+=2){
931 sign=0;
932 signLength=0;
933 t0 = values[i+0];
934 if(t0 != 0){
935 signLength++;
936 sign<<=1;
937 if(t0 < 0){
938 sign|=1;
939 t0=fixp_abs(t0);
940 }
941 }
942
943 t1 = values[i+1];
944 if(t1 != 0){
945 signLength++;
946 sign<<=1;
947 if(t1 < 0){
948 sign|=1;
949 t1=fixp_abs(t1);
950 }
951 }
952 codeWord = FDKaacEnc_huff_ctab7[t0][t1];
953 codeLength = HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
954 FDKwriteBits(hBitstream,codeWord,codeLength);
955 FDKwriteBits(hBitstream,sign,signLength);
956 }
957 break;
958
959 case CODE_BOOK_8_NO:
960 for(i=0; i<width; i+=2) {
961 sign=0;
962 signLength=0;
963 t0 = values[i+0];
964 if(t0 != 0){
965 signLength++;
966 sign<<=1;
967 if(t0 < 0){
968 sign|=1;
969 t0=fixp_abs(t0);
970 }
971 }
972
973 t1 = values[i+1];
974 if(t1 != 0){
975 signLength++;
976 sign<<=1;
977 if(t1 < 0){
978 sign|=1;
979 t1=fixp_abs(t1);
980 }
981 }
982 codeWord = FDKaacEnc_huff_ctab8[t0][t1];
983 codeLength = LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
984 FDKwriteBits(hBitstream,codeWord,codeLength);
985 FDKwriteBits(hBitstream,sign,signLength);
986 }
987 break;
988
989 case CODE_BOOK_9_NO:
990 for(i=0; i<width; i+=2) {
991 sign=0;
992 signLength=0;
993 t0 = values[i+0];
994 if(t0 != 0){
995 signLength++;
996 sign<<=1;
997 if(t0 < 0){
998 sign|=1;
999 t0=fixp_abs(t0);
1000 }
1001 }
1002 t1 = values[i+1];
1003 if(t1 != 0){
1004 signLength++;
1005 sign<<=1;
1006 if(t1 < 0){
1007 sign|=1;
1008 t1=fixp_abs(t1);
1009 }
1010 }
1011 codeWord = FDKaacEnc_huff_ctab9[t0][t1];
1012 codeLength = HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
1013 FDKwriteBits(hBitstream,codeWord,codeLength);
1014 FDKwriteBits(hBitstream,sign,signLength);
1015 }
1016 break;
1017
1018 case CODE_BOOK_10_NO:
1019 for(i=0; i<width; i+=2) {
1020 sign=0;
1021 signLength=0;
1022 t0 = values[i+0];
1023 if(t0 != 0){
1024 signLength++;
1025 sign<<=1;
1026 if(t0 < 0){
1027 sign|=1;
1028 t0=fixp_abs(t0);
1029 }
1030 }
1031
1032 t1 = values[i+1];
1033 if(t1 != 0){
1034 signLength++;
1035 sign<<=1;
1036 if(t1 < 0){
1037 sign|=1;
1038 t1=fixp_abs(t1);
1039 }
1040 }
1041 codeWord = FDKaacEnc_huff_ctab10[t0][t1];
1042 codeLength = LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
1043 FDKwriteBits(hBitstream,codeWord,codeLength);
1044 FDKwriteBits(hBitstream,sign,signLength);
1045 }
1046 break;
1047
1048 case CODE_BOOK_ESC_NO:
1049 for(i=0; i<width; i+=2) {
1050 sign=0;
1051 signLength=0;
1052 t0 = values[i+0];
1053 if(t0 != 0){
1054 signLength++;
1055 sign<<=1;
1056 if(t0 < 0){
1057 sign|=1;
1058 t0=fixp_abs(t0);
1059 }
1060 }
1061 t1 = values[i+1];
1062 if(t1 != 0){
1063 signLength++;
1064 sign<<=1;
1065 if(t1 < 0){
1066 sign|=1;
1067 t1=fixp_abs(t1);
1068 }
1069 }
1070 t00 = fixMin(t0,16);
1071 t01 = fixMin(t1,16);
1072
1073 codeWord = FDKaacEnc_huff_ctab11[t00][t01];
1074 codeLength = (INT) FDKaacEnc_huff_ltab11[t00][t01];
1075 FDKwriteBits(hBitstream,codeWord,codeLength);
1076 FDKwriteBits(hBitstream,sign,signLength);
1077 if(t0 >=16){
1078 INT n,p;
1079 n=0;
1080 p=t0;
1081 while((p>>=1) >=16){
1082 FDKwriteBits(hBitstream,1,1);
1083 n++;
1084 }
1085 FDKwriteBits(hBitstream,0,1);
1086 FDKwriteBits(hBitstream,t0-(1<<(n+4)),n+4);
1087 }
1088 if(t1 >=16){
1089 INT n,p;
1090 n=0;
1091 p=t1;
1092 while((p>>=1) >=16){
1093 FDKwriteBits(hBitstream,1,1);
1094 n++;
1095 }
1096 FDKwriteBits(hBitstream,0,1);
1097 FDKwriteBits(hBitstream,t1-(1<<(n+4)),n+4);
1098 }
1099 }
1100 break;
1101
1102 default:
1103 break;
1104 }
1105 return(0);
1106 }
1107
1108 INT FDKaacEnc_codeScalefactorDelta(INT delta, HANDLE_FDK_BITSTREAM hBitstream)
1109 {
1110 INT codeWord,codeLength;
1111
1112 if(fixp_abs(delta) >CODE_BOOK_SCF_LAV)
1113 return(1);
1114
1115 codeWord = FDKaacEnc_huff_ctabscf[delta+CODE_BOOK_SCF_LAV];
1116 codeLength = (INT)FDKaacEnc_huff_ltabscf[delta+CODE_BOOK_SCF_LAV];
1117 FDKwriteBits(hBitstream,codeWord,codeLength);
1118 return(0);
1119 }
1120
1121
1122