Coverage Report

Created: 2025-07-11 06:50

/src/aac/libAACenc/src/bit_cnt.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#define HI_LTAB(a) (a >> 16)
108
0
#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
121
static void FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11(const SHORT *const values,
122
                                                   const INT width,
123
0
                                                   INT *RESTRICT bitCount) {
124
0
  INT i;
125
0
  INT bc1_2, bc3_4, bc5_6, bc7_8, bc9_10, bc11, sc;
126
0
  INT t0, t1, t2, t3;
127
0
  bc1_2 = 0;
128
0
  bc3_4 = 0;
129
0
  bc5_6 = 0;
130
0
  bc7_8 = 0;
131
0
  bc9_10 = 0;
132
0
  bc11 = 0;
133
0
  sc = 0;
134
135
0
  DWORD_ALIGNED(values);
136
137
0
  for (i = 0; i < width; i += 4) {
138
0
    t0 = values[i + 0];
139
0
    t1 = values[i + 1];
140
0
    t2 = values[i + 2];
141
0
    t3 = values[i + 3];
142
143
0
    bc1_2 += (INT)FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1];
144
0
    bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
145
0
             (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
146
147
0
    t0 = fixp_abs(t0);
148
0
    sc += (t0 > 0);
149
0
    t1 = fixp_abs(t1);
150
0
    sc += (t1 > 0);
151
0
    t2 = fixp_abs(t2);
152
0
    sc += (t2 > 0);
153
0
    t3 = fixp_abs(t3);
154
0
    sc += (t3 > 0);
155
156
0
    bc3_4 += (INT)FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
157
0
    bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
158
0
             (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
159
0
    bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
160
0
              (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
161
0
    bc11 +=
162
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
163
0
  }
164
0
  bitCount[1] = HI_LTAB(bc1_2);
165
0
  bitCount[2] = LO_LTAB(bc1_2);
166
0
  bitCount[3] = HI_LTAB(bc3_4) + sc;
167
0
  bitCount[4] = LO_LTAB(bc3_4) + sc;
168
0
  bitCount[5] = HI_LTAB(bc5_6);
169
0
  bitCount[6] = LO_LTAB(bc5_6);
170
0
  bitCount[7] = HI_LTAB(bc7_8) + sc;
171
0
  bitCount[8] = LO_LTAB(bc7_8) + sc;
172
0
  bitCount[9] = HI_LTAB(bc9_10) + sc;
173
0
  bitCount[10] = LO_LTAB(bc9_10) + sc;
174
0
  bitCount[11] = bc11 + sc;
175
0
}
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
187
static void FDKaacEnc_count3_4_5_6_7_8_9_10_11(const SHORT *const values,
188
                                               const INT width,
189
0
                                               INT *RESTRICT bitCount) {
190
0
  INT i;
191
0
  INT bc3_4, bc5_6, bc7_8, bc9_10, bc11, sc;
192
0
  INT t0, t1, t2, t3;
193
194
0
  bc3_4 = 0;
195
0
  bc5_6 = 0;
196
0
  bc7_8 = 0;
197
0
  bc9_10 = 0;
198
0
  bc11 = 0;
199
0
  sc = 0;
200
201
0
  DWORD_ALIGNED(values);
202
203
0
  for (i = 0; i < width; i += 4) {
204
0
    t0 = values[i + 0];
205
0
    t1 = values[i + 1];
206
0
    t2 = values[i + 2];
207
0
    t3 = values[i + 3];
208
209
0
    bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
210
0
             (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
211
212
0
    t0 = fixp_abs(t0);
213
0
    sc += (t0 > 0);
214
0
    t1 = fixp_abs(t1);
215
0
    sc += (t1 > 0);
216
0
    t2 = fixp_abs(t2);
217
0
    sc += (t2 > 0);
218
0
    t3 = fixp_abs(t3);
219
0
    sc += (t3 > 0);
220
221
0
    bc3_4 += (INT)FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3];
222
0
    bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
223
0
             (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
224
0
    bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
225
0
              (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
226
0
    bc11 +=
227
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
228
0
  }
229
230
0
  bitCount[1] = INVALID_BITCOUNT;
231
0
  bitCount[2] = INVALID_BITCOUNT;
232
0
  bitCount[3] = HI_LTAB(bc3_4) + sc;
233
0
  bitCount[4] = LO_LTAB(bc3_4) + sc;
234
0
  bitCount[5] = HI_LTAB(bc5_6);
235
0
  bitCount[6] = LO_LTAB(bc5_6);
236
0
  bitCount[7] = HI_LTAB(bc7_8) + sc;
237
0
  bitCount[8] = LO_LTAB(bc7_8) + sc;
238
0
  bitCount[9] = HI_LTAB(bc9_10) + sc;
239
0
  bitCount[10] = LO_LTAB(bc9_10) + sc;
240
0
  bitCount[11] = bc11 + sc;
241
0
}
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
253
static void FDKaacEnc_count5_6_7_8_9_10_11(const SHORT *const values,
254
                                           const INT width,
255
0
                                           INT *RESTRICT bitCount) {
256
0
  INT i;
257
0
  INT bc5_6, bc7_8, bc9_10, bc11, sc;
258
0
  INT t0, t1, t2, t3;
259
0
  bc5_6 = 0;
260
0
  bc7_8 = 0;
261
0
  bc9_10 = 0;
262
0
  bc11 = 0;
263
0
  sc = 0;
264
265
0
  DWORD_ALIGNED(values);
266
267
0
  for (i = 0; i < width; i += 4) {
268
0
    t0 = values[i + 0];
269
0
    t1 = values[i + 1];
270
0
    t2 = values[i + 2];
271
0
    t3 = values[i + 3];
272
273
0
    bc5_6 += (INT)FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4] +
274
0
             (INT)FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4];
275
276
0
    t0 = fixp_abs(t0);
277
0
    sc += (t0 > 0);
278
0
    t1 = fixp_abs(t1);
279
0
    sc += (t1 > 0);
280
0
    t2 = fixp_abs(t2);
281
0
    sc += (t2 > 0);
282
0
    t3 = fixp_abs(t3);
283
0
    sc += (t3 > 0);
284
285
0
    bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
286
0
             (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
287
0
    bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
288
0
              (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
289
0
    bc11 +=
290
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
291
0
  }
292
0
  bitCount[1] = INVALID_BITCOUNT;
293
0
  bitCount[2] = INVALID_BITCOUNT;
294
0
  bitCount[3] = INVALID_BITCOUNT;
295
0
  bitCount[4] = INVALID_BITCOUNT;
296
0
  bitCount[5] = HI_LTAB(bc5_6);
297
0
  bitCount[6] = LO_LTAB(bc5_6);
298
0
  bitCount[7] = HI_LTAB(bc7_8) + sc;
299
0
  bitCount[8] = LO_LTAB(bc7_8) + sc;
300
0
  bitCount[9] = HI_LTAB(bc9_10) + sc;
301
0
  bitCount[10] = LO_LTAB(bc9_10) + sc;
302
0
  bitCount[11] = bc11 + sc;
303
0
}
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
315
static void FDKaacEnc_count7_8_9_10_11(const SHORT *const values,
316
                                       const INT width,
317
0
                                       INT *RESTRICT bitCount) {
318
0
  INT i;
319
0
  INT bc7_8, bc9_10, bc11, sc;
320
0
  INT t0, t1, t2, t3;
321
322
0
  bc7_8 = 0;
323
0
  bc9_10 = 0;
324
0
  bc11 = 0;
325
0
  sc = 0;
326
327
0
  DWORD_ALIGNED(values);
328
329
0
  for (i = 0; i < width; i += 4) {
330
0
    t0 = values[i + 0];
331
0
    t1 = values[i + 1];
332
0
    t2 = values[i + 2];
333
0
    t3 = values[i + 3];
334
335
0
    t0 = fixp_abs(t0);
336
0
    sc += (t0 > 0);
337
0
    t1 = fixp_abs(t1);
338
0
    sc += (t1 > 0);
339
0
    t2 = fixp_abs(t2);
340
0
    sc += (t2 > 0);
341
0
    t3 = fixp_abs(t3);
342
0
    sc += (t3 > 0);
343
344
0
    bc7_8 += (INT)FDKaacEnc_huff_ltab7_8[t0][t1] +
345
0
             (INT)FDKaacEnc_huff_ltab7_8[t2][t3];
346
0
    bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
347
0
              (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
348
0
    bc11 +=
349
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
350
0
  }
351
352
0
  bitCount[1] = INVALID_BITCOUNT;
353
0
  bitCount[2] = INVALID_BITCOUNT;
354
0
  bitCount[3] = INVALID_BITCOUNT;
355
0
  bitCount[4] = INVALID_BITCOUNT;
356
0
  bitCount[5] = INVALID_BITCOUNT;
357
0
  bitCount[6] = INVALID_BITCOUNT;
358
0
  bitCount[7] = HI_LTAB(bc7_8) + sc;
359
0
  bitCount[8] = LO_LTAB(bc7_8) + sc;
360
0
  bitCount[9] = HI_LTAB(bc9_10) + sc;
361
0
  bitCount[10] = LO_LTAB(bc9_10) + sc;
362
0
  bitCount[11] = bc11 + sc;
363
0
}
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
375
static void FDKaacEnc_count9_10_11(const SHORT *const values, const INT width,
376
0
                                   INT *RESTRICT bitCount) {
377
0
  INT i;
378
0
  INT bc9_10, bc11, sc;
379
0
  INT t0, t1, t2, t3;
380
381
0
  bc9_10 = 0;
382
0
  bc11 = 0;
383
0
  sc = 0;
384
385
0
  DWORD_ALIGNED(values);
386
387
0
  for (i = 0; i < width; i += 4) {
388
0
    t0 = values[i + 0];
389
0
    t1 = values[i + 1];
390
0
    t2 = values[i + 2];
391
0
    t3 = values[i + 3];
392
393
0
    t0 = fixp_abs(t0);
394
0
    sc += (t0 > 0);
395
0
    t1 = fixp_abs(t1);
396
0
    sc += (t1 > 0);
397
0
    t2 = fixp_abs(t2);
398
0
    sc += (t2 > 0);
399
0
    t3 = fixp_abs(t3);
400
0
    sc += (t3 > 0);
401
402
0
    bc9_10 += (INT)FDKaacEnc_huff_ltab9_10[t0][t1] +
403
0
              (INT)FDKaacEnc_huff_ltab9_10[t2][t3];
404
0
    bc11 +=
405
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
406
0
  }
407
408
0
  bitCount[1] = INVALID_BITCOUNT;
409
0
  bitCount[2] = INVALID_BITCOUNT;
410
0
  bitCount[3] = INVALID_BITCOUNT;
411
0
  bitCount[4] = INVALID_BITCOUNT;
412
0
  bitCount[5] = INVALID_BITCOUNT;
413
0
  bitCount[6] = INVALID_BITCOUNT;
414
0
  bitCount[7] = INVALID_BITCOUNT;
415
0
  bitCount[8] = INVALID_BITCOUNT;
416
0
  bitCount[9] = HI_LTAB(bc9_10) + sc;
417
0
  bitCount[10] = LO_LTAB(bc9_10) + sc;
418
0
  bitCount[11] = bc11 + sc;
419
0
}
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
431
static void FDKaacEnc_count11(const SHORT *const values, const INT width,
432
0
                              INT *RESTRICT bitCount) {
433
0
  INT i;
434
0
  INT bc11, sc;
435
0
  INT t0, t1, t2, t3;
436
437
0
  bc11 = 0;
438
0
  sc = 0;
439
440
0
  DWORD_ALIGNED(values);
441
442
0
  for (i = 0; i < width; i += 4) {
443
0
    t0 = values[i + 0];
444
0
    t1 = values[i + 1];
445
0
    t2 = values[i + 2];
446
0
    t3 = values[i + 3];
447
448
0
    t0 = fixp_abs(t0);
449
0
    sc += (t0 > 0);
450
0
    t1 = fixp_abs(t1);
451
0
    sc += (t1 > 0);
452
0
    t2 = fixp_abs(t2);
453
0
    sc += (t2 > 0);
454
0
    t3 = fixp_abs(t3);
455
0
    sc += (t3 > 0);
456
457
0
    bc11 +=
458
0
        (INT)FDKaacEnc_huff_ltab11[t0][t1] + (INT)FDKaacEnc_huff_ltab11[t2][t3];
459
0
  }
460
461
0
  bitCount[1] = INVALID_BITCOUNT;
462
0
  bitCount[2] = INVALID_BITCOUNT;
463
0
  bitCount[3] = INVALID_BITCOUNT;
464
0
  bitCount[4] = INVALID_BITCOUNT;
465
0
  bitCount[5] = INVALID_BITCOUNT;
466
0
  bitCount[6] = INVALID_BITCOUNT;
467
0
  bitCount[7] = INVALID_BITCOUNT;
468
0
  bitCount[8] = INVALID_BITCOUNT;
469
0
  bitCount[9] = INVALID_BITCOUNT;
470
0
  bitCount[10] = INVALID_BITCOUNT;
471
0
  bitCount[11] = bc11 + sc;
472
0
}
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
484
static void FDKaacEnc_countEsc(const SHORT *const values, const INT width,
485
0
                               INT *RESTRICT bitCount) {
486
0
  INT i;
487
0
  INT bc11, ec, sc;
488
0
  INT t0, t1, t00, t01;
489
490
0
  bc11 = 0;
491
0
  sc = 0;
492
0
  ec = 0;
493
0
  for (i = 0; i < width; i += 2) {
494
0
    t0 = fixp_abs(values[i + 0]);
495
0
    t1 = fixp_abs(values[i + 1]);
496
497
0
    sc += (t0 > 0) + (t1 > 0);
498
499
0
    t00 = fixMin(t0, 16);
500
0
    t01 = fixMin(t1, 16);
501
0
    bc11 += (INT)FDKaacEnc_huff_ltab11[t00][t01];
502
503
0
    if (t0 >= 16) {
504
0
      ec += 5;
505
0
      while ((t0 >>= 1) >= 16) ec += 2;
506
0
    }
507
508
0
    if (t1 >= 16) {
509
0
      ec += 5;
510
0
      while ((t1 >>= 1) >= 16) ec += 2;
511
0
    }
512
0
  }
513
514
0
  for (i = 0; i < 11; i++) bitCount[i] = INVALID_BITCOUNT;
515
516
0
  bitCount[11] = bc11 + sc + ec;
517
0
}
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
543
INT FDKaacEnc_bitCount(const SHORT *const values, const INT width,
544
0
                       const INT maxVal, INT *const RESTRICT bitCount) {
545
  /*
546
    check if we can use codebook 0
547
  */
548
549
0
  bitCount[0] = (maxVal == 0) ? 0 : INVALID_BITCOUNT;
550
551
0
  countFuncTable[fixMin(maxVal, (INT)CODE_BOOK_ESC_LAV)](values, width,
552
0
                                                         bitCount);
553
554
0
  return (0);
555
0
}
556
557
/*
558
  count difference between actual and zeroed lines
559
*/
560
0
INT FDKaacEnc_countValues(SHORT *RESTRICT values, INT width, INT codeBook) {
561
0
  INT i, t0, t1, t2, t3;
562
0
  INT bitCnt = 0;
563
564
0
  switch (codeBook) {
565
0
    case CODE_BOOK_ZERO_NO:
566
0
      break;
567
568
0
    case CODE_BOOK_1_NO:
569
0
      for (i = 0; i < width; i += 4) {
570
0
        t0 = values[i + 0];
571
0
        t1 = values[i + 1];
572
0
        t2 = values[i + 2];
573
0
        t3 = values[i + 3];
574
0
        bitCnt +=
575
0
            HI_LTAB(FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1]);
576
0
      }
577
0
      break;
578
579
0
    case CODE_BOOK_2_NO:
580
0
      for (i = 0; i < width; i += 4) {
581
0
        t0 = values[i + 0];
582
0
        t1 = values[i + 1];
583
0
        t2 = values[i + 2];
584
0
        t3 = values[i + 3];
585
0
        bitCnt +=
586
0
            LO_LTAB(FDKaacEnc_huff_ltab1_2[t0 + 1][t1 + 1][t2 + 1][t3 + 1]);
587
0
      }
588
0
      break;
589
590
0
    case CODE_BOOK_3_NO:
591
0
      for (i = 0; i < width; i += 4) {
592
0
        t0 = fixp_abs(values[i + 0]);
593
0
        bitCnt += (t0 > 0);
594
0
        t1 = fixp_abs(values[i + 1]);
595
0
        bitCnt += (t1 > 0);
596
0
        t2 = fixp_abs(values[i + 2]);
597
0
        bitCnt += (t2 > 0);
598
0
        t3 = fixp_abs(values[i + 3]);
599
0
        bitCnt += (t3 > 0);
600
0
        bitCnt += HI_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
601
0
      }
602
0
      break;
603
604
0
    case CODE_BOOK_4_NO:
605
0
      for (i = 0; i < width; i += 4) {
606
0
        t0 = fixp_abs(values[i + 0]);
607
0
        bitCnt += (t0 > 0);
608
0
        t1 = fixp_abs(values[i + 1]);
609
0
        bitCnt += (t1 > 0);
610
0
        t2 = fixp_abs(values[i + 2]);
611
0
        bitCnt += (t2 > 0);
612
0
        t3 = fixp_abs(values[i + 3]);
613
0
        bitCnt += (t3 > 0);
614
0
        bitCnt += LO_LTAB(FDKaacEnc_huff_ltab3_4[t0][t1][t2][t3]);
615
0
      }
616
0
      break;
617
618
0
    case CODE_BOOK_5_NO:
619
0
      for (i = 0; i < width; i += 4) {
620
0
        t0 = values[i + 0];
621
0
        t1 = values[i + 1];
622
0
        t2 = values[i + 2];
623
0
        t3 = values[i + 3];
624
0
        bitCnt += HI_LTAB(FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4]) +
625
0
                  HI_LTAB(FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4]);
626
0
      }
627
0
      break;
628
629
0
    case CODE_BOOK_6_NO:
630
0
      for (i = 0; i < width; i += 4) {
631
0
        t0 = values[i + 0];
632
0
        t1 = values[i + 1];
633
0
        t2 = values[i + 2];
634
0
        t3 = values[i + 3];
635
0
        bitCnt += LO_LTAB(FDKaacEnc_huff_ltab5_6[t0 + 4][t1 + 4]) +
636
0
                  LO_LTAB(FDKaacEnc_huff_ltab5_6[t2 + 4][t3 + 4]);
637
0
      }
638
0
      break;
639
640
0
    case CODE_BOOK_7_NO:
641
0
      for (i = 0; i < width; i += 4) {
642
0
        t0 = fixp_abs(values[i + 0]);
643
0
        bitCnt += (t0 > 0);
644
0
        t1 = fixp_abs(values[i + 1]);
645
0
        bitCnt += (t1 > 0);
646
0
        t2 = fixp_abs(values[i + 2]);
647
0
        bitCnt += (t2 > 0);
648
0
        t3 = fixp_abs(values[i + 3]);
649
0
        bitCnt += (t3 > 0);
650
0
        bitCnt += HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]) +
651
0
                  HI_LTAB(FDKaacEnc_huff_ltab7_8[t2][t3]);
652
0
      }
653
0
      break;
654
655
0
    case CODE_BOOK_8_NO:
656
0
      for (i = 0; i < width; i += 4) {
657
0
        t0 = fixp_abs(values[i + 0]);
658
0
        bitCnt += (t0 > 0);
659
0
        t1 = fixp_abs(values[i + 1]);
660
0
        bitCnt += (t1 > 0);
661
0
        t2 = fixp_abs(values[i + 2]);
662
0
        bitCnt += (t2 > 0);
663
0
        t3 = fixp_abs(values[i + 3]);
664
0
        bitCnt += (t3 > 0);
665
0
        bitCnt += LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]) +
666
0
                  LO_LTAB(FDKaacEnc_huff_ltab7_8[t2][t3]);
667
0
      }
668
0
      break;
669
670
0
    case CODE_BOOK_9_NO:
671
0
      for (i = 0; i < width; i += 4) {
672
0
        t0 = fixp_abs(values[i + 0]);
673
0
        bitCnt += (t0 > 0);
674
0
        t1 = fixp_abs(values[i + 1]);
675
0
        bitCnt += (t1 > 0);
676
0
        t2 = fixp_abs(values[i + 2]);
677
0
        bitCnt += (t2 > 0);
678
0
        t3 = fixp_abs(values[i + 3]);
679
0
        bitCnt += (t3 > 0);
680
0
        bitCnt += HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]) +
681
0
                  HI_LTAB(FDKaacEnc_huff_ltab9_10[t2][t3]);
682
0
      }
683
0
      break;
684
685
0
    case CODE_BOOK_10_NO:
686
0
      for (i = 0; i < width; i += 4) {
687
0
        t0 = fixp_abs(values[i + 0]);
688
0
        bitCnt += (t0 > 0);
689
0
        t1 = fixp_abs(values[i + 1]);
690
0
        bitCnt += (t1 > 0);
691
0
        t2 = fixp_abs(values[i + 2]);
692
0
        bitCnt += (t2 > 0);
693
0
        t3 = fixp_abs(values[i + 3]);
694
0
        bitCnt += (t3 > 0);
695
0
        bitCnt += LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]) +
696
0
                  LO_LTAB(FDKaacEnc_huff_ltab9_10[t2][t3]);
697
0
      }
698
0
      break;
699
700
0
    case CODE_BOOK_ESC_NO:
701
0
      for (i = 0; i < width; i += 2) {
702
0
        t0 = fixp_abs(values[i + 0]);
703
0
        bitCnt += (t0 > 0);
704
0
        t1 = fixp_abs(values[i + 1]);
705
0
        bitCnt += (t1 > 0);
706
0
        bitCnt += (INT)FDKaacEnc_huff_ltab11[fixMin(t0, 16)][fixMin(t1, 16)];
707
0
        if (t0 >= 16) {
708
0
          bitCnt += 5;
709
0
          while ((t0 >>= 1) >= 16) bitCnt += 2;
710
0
        }
711
0
        if (t1 >= 16) {
712
0
          bitCnt += 5;
713
0
          while ((t1 >>= 1) >= 16) bitCnt += 2;
714
0
        }
715
0
      }
716
0
      break;
717
718
0
    default:
719
0
      break;
720
0
  }
721
722
0
  return (bitCnt);
723
0
}
724
725
INT FDKaacEnc_codeValues(SHORT *RESTRICT values, INT width, INT codeBook,
726
0
                         HANDLE_FDK_BITSTREAM hBitstream) {
727
0
  INT i, t0, t1, t2, t3, t00, t01;
728
0
  INT codeWord, codeLength;
729
0
  INT sign, signLength;
730
731
0
  DWORD_ALIGNED(values);
732
733
0
  switch (codeBook) {
734
0
    case CODE_BOOK_ZERO_NO:
735
0
      break;
736
737
0
    case CODE_BOOK_1_NO:
738
0
      for (i = 0; i < width; i += 4) {
739
0
        t0 = values[i + 0] + 1;
740
0
        t1 = values[i + 1] + 1;
741
0
        t2 = values[i + 2] + 1;
742
0
        t3 = values[i + 3] + 1;
743
0
        codeWord = FDKaacEnc_huff_ctab1[t0][t1][t2][t3];
744
0
        codeLength = HI_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
745
0
        FDKwriteBits(hBitstream, codeWord, codeLength);
746
0
      }
747
0
      break;
748
749
0
    case CODE_BOOK_2_NO:
750
0
      for (i = 0; i < width; i += 4) {
751
0
        t0 = values[i + 0] + 1;
752
0
        t1 = values[i + 1] + 1;
753
0
        t2 = values[i + 2] + 1;
754
0
        t3 = values[i + 3] + 1;
755
0
        codeWord = FDKaacEnc_huff_ctab2[t0][t1][t2][t3];
756
0
        codeLength = LO_LTAB(FDKaacEnc_huff_ltab1_2[t0][t1][t2][t3]);
757
0
        FDKwriteBits(hBitstream, codeWord, codeLength);
758
0
      }
759
0
      break;
760
761
0
    case CODE_BOOK_3_NO:
762
0
      for (i = 0; i < (width >> 2); i++) {
763
0
        sign = 0;
764
0
        signLength = 0;
765
0
        int index[4];
766
0
        for (int j = 0; j < 4; j++) {
767
0
          int ti = *values++;
768
0
          int zero = (ti == 0) ? 0 : 1;
769
0
          signLength += zero;
770
0
          sign = (sign << zero) + ((UINT)ti >> 31);
771
0
          index[j] = fixp_abs(ti);
772
0
        }
773
0
        codeWord = FDKaacEnc_huff_ctab3[index[0]][index[1]][index[2]][index[3]];
774
0
        codeLength = HI_LTAB(
775
0
            FDKaacEnc_huff_ltab3_4[index[0]][index[1]][index[2]][index[3]]);
776
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
777
0
                     codeLength + signLength);
778
0
      }
779
0
      break;
780
781
0
    case CODE_BOOK_4_NO:
782
0
      for (i = 0; i < width; i += 4) {
783
0
        sign = 0;
784
0
        signLength = 0;
785
0
        int index[4];
786
0
        for (int j = 0; j < 4; j++) {
787
0
          int ti = *values++;
788
0
          int zero = (ti == 0) ? 0 : 1;
789
0
          signLength += zero;
790
0
          sign = (sign << zero) + ((UINT)ti >> 31);
791
0
          index[j] = fixp_abs(ti);
792
0
        }
793
0
        codeWord = FDKaacEnc_huff_ctab4[index[0]][index[1]][index[2]][index[3]];
794
0
        codeLength = LO_LTAB(
795
0
            FDKaacEnc_huff_ltab3_4[index[0]][index[1]][index[2]][index[3]]);
796
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
797
0
                     codeLength + signLength);
798
0
      }
799
0
      break;
800
801
0
    case CODE_BOOK_5_NO:
802
0
      for (i = 0; i < (width >> 2); i++) {
803
0
        t0 = *values++ + 4;
804
0
        t1 = *values++ + 4;
805
0
        t2 = *values++ + 4;
806
0
        t3 = *values++ + 4;
807
0
        codeWord = FDKaacEnc_huff_ctab5[t0][t1];
808
0
        codeLength =
809
0
            HI_LTAB(FDKaacEnc_huff_ltab5_6[t2][t3]); /* length of 2nd cw */
810
0
        codeWord = (codeWord << codeLength) + FDKaacEnc_huff_ctab5[t2][t3];
811
0
        codeLength += HI_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
812
0
        FDKwriteBits(hBitstream, codeWord, codeLength);
813
0
      }
814
0
      break;
815
816
0
    case CODE_BOOK_6_NO:
817
0
      for (i = 0; i < (width >> 2); i++) {
818
0
        t0 = *values++ + 4;
819
0
        t1 = *values++ + 4;
820
0
        t2 = *values++ + 4;
821
0
        t3 = *values++ + 4;
822
0
        codeWord = FDKaacEnc_huff_ctab6[t0][t1];
823
0
        codeLength =
824
0
            LO_LTAB(FDKaacEnc_huff_ltab5_6[t2][t3]); /* length of 2nd cw */
825
0
        codeWord = (codeWord << codeLength) + FDKaacEnc_huff_ctab6[t2][t3];
826
0
        codeLength += LO_LTAB(FDKaacEnc_huff_ltab5_6[t0][t1]);
827
0
        FDKwriteBits(hBitstream, codeWord, codeLength);
828
0
      }
829
0
      break;
830
831
0
    case CODE_BOOK_7_NO:
832
0
      for (i = 0; i < (width >> 1); i++) {
833
0
        t0 = *values++;
834
0
        sign = ((UINT)t0 >> 31);
835
0
        t0 = fixp_abs(t0);
836
0
        signLength = (t0 == 0) ? 0 : 1;
837
0
        t1 = *values++;
838
0
        INT zero = (t1 == 0) ? 0 : 1;
839
0
        signLength += zero;
840
0
        sign = (sign << zero) + ((UINT)t1 >> 31);
841
0
        t1 = fixp_abs(t1);
842
0
        codeWord = FDKaacEnc_huff_ctab7[t0][t1];
843
0
        codeLength = HI_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
844
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
845
0
                     codeLength + signLength);
846
0
      }
847
0
      break;
848
849
0
    case CODE_BOOK_8_NO:
850
0
      for (i = 0; i < (width >> 1); i++) {
851
0
        t0 = *values++;
852
0
        sign = ((UINT)t0 >> 31);
853
0
        t0 = fixp_abs(t0);
854
0
        signLength = (t0 == 0) ? 0 : 1;
855
0
        t1 = *values++;
856
0
        INT zero = (t1 == 0) ? 0 : 1;
857
0
        signLength += zero;
858
0
        sign = (sign << zero) + ((UINT)t1 >> 31);
859
0
        t1 = fixp_abs(t1);
860
0
        codeWord = FDKaacEnc_huff_ctab8[t0][t1];
861
0
        codeLength = LO_LTAB(FDKaacEnc_huff_ltab7_8[t0][t1]);
862
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
863
0
                     codeLength + signLength);
864
0
      }
865
0
      break;
866
867
0
    case CODE_BOOK_9_NO:
868
0
      for (i = 0; i < (width >> 1); i++) {
869
0
        t0 = *values++;
870
0
        sign = ((UINT)t0 >> 31);
871
0
        t0 = fixp_abs(t0);
872
0
        signLength = (t0 == 0) ? 0 : 1;
873
0
        t1 = *values++;
874
0
        INT zero = (t1 == 0) ? 0 : 1;
875
0
        signLength += zero;
876
0
        sign = (sign << zero) + ((UINT)t1 >> 31);
877
0
        t1 = fixp_abs(t1);
878
0
        codeWord = FDKaacEnc_huff_ctab9[t0][t1];
879
0
        codeLength = HI_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
880
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
881
0
                     codeLength + signLength);
882
0
      }
883
0
      break;
884
885
0
    case CODE_BOOK_10_NO:
886
0
      for (i = 0; i < (width >> 1); i++) {
887
0
        t0 = *values++;
888
0
        sign = ((UINT)t0 >> 31);
889
0
        t0 = fixp_abs(t0);
890
0
        signLength = (t0 == 0) ? 0 : 1;
891
0
        t1 = *values++;
892
0
        INT zero = (t1 == 0) ? 0 : 1;
893
0
        signLength += zero;
894
0
        sign = (sign << zero) + ((UINT)t1 >> 31);
895
0
        t1 = fixp_abs(t1);
896
0
        codeWord = FDKaacEnc_huff_ctab10[t0][t1];
897
0
        codeLength = LO_LTAB(FDKaacEnc_huff_ltab9_10[t0][t1]);
898
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
899
0
                     codeLength + signLength);
900
0
      }
901
0
      break;
902
903
0
    case CODE_BOOK_ESC_NO:
904
0
      for (i = 0; i < (width >> 1); i++) {
905
0
        t0 = *values++;
906
0
        sign = ((UINT)t0 >> 31);
907
0
        t0 = fixp_abs(t0);
908
0
        signLength = (t0 == 0) ? 0 : 1;
909
0
        t1 = *values++;
910
0
        INT zero = (t1 == 0) ? 0 : 1;
911
0
        signLength += zero;
912
0
        sign = (sign << zero) + ((UINT)t1 >> 31);
913
0
        t1 = fixp_abs(t1);
914
915
0
        t00 = fixMin(t0, 16);
916
0
        t01 = fixMin(t1, 16);
917
918
0
        codeWord = FDKaacEnc_huff_ctab11[t00][t01];
919
0
        codeLength = (INT)FDKaacEnc_huff_ltab11[t00][t01];
920
0
        FDKwriteBits(hBitstream, (codeWord << signLength) | sign,
921
0
                     codeLength + signLength);
922
0
        for (int j = 0; j < 2; j++) {
923
0
          if (t0 >= 16) {
924
0
            INT n = 4, p = t0;
925
0
            for (; (p >>= 1) >= 16;) n++;
926
0
            FDKwriteBits(hBitstream,
927
0
                         (((1 << (n - 3)) - 2) << n) | (t0 - (1 << n)),
928
0
                         n + n - 3);
929
0
          }
930
0
          t0 = t1;
931
0
        }
932
0
      }
933
0
      break;
934
935
0
    default:
936
0
      break;
937
0
  }
938
0
  return (0);
939
0
}
940
941
0
INT FDKaacEnc_codeScalefactorDelta(INT delta, HANDLE_FDK_BITSTREAM hBitstream) {
942
0
  INT codeWord, codeLength;
943
944
0
  if (fixp_abs(delta) > CODE_BOOK_SCF_LAV) return (1);
945
946
0
  codeWord = FDKaacEnc_huff_ctabscf[delta + CODE_BOOK_SCF_LAV];
947
0
  codeLength = (INT)FDKaacEnc_huff_ltabscf[delta + CODE_BOOK_SCF_LAV];
948
0
  FDKwriteBits(hBitstream, codeWord, codeLength);
949
0
  return (0);
950
0
}