xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_bits_count.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2023 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 #include <limits.h>
21 #include <stdlib.h>
22 #include "ixheaac_type_def.h"
23 #include "ixheaac_constants.h"
24 #include "impd_drc_common_enc.h"
25 #include "impd_drc_uni_drc.h"
26 #include "impd_drc_tables.h"
27 #include "impd_drc_api.h"
28 #include "ixheaace_api.h"
29 #include "ixheaace_aac_constants.h"
30 
31 #include "ixheaac_basic_ops32.h"
32 #include "ixheaac_basic_ops16.h"
33 #include "ixheaac_basic_ops40.h"
34 #include "ixheaac_basic_ops.h"
35 
36 #include "ixheaace_bitbuffer.h"
37 #include "ixheaace_psy_const.h"
38 #include "ixheaace_tns.h"
39 #include "ixheaace_tns_params.h"
40 #include "ixheaace_rom.h"
41 #include "ixheaace_bits_count.h"
42 
43 #include "ixheaace_common_rom.h"
44 #include "ixheaace_common_utils.h"
45 
ia_enhaacplus_enc_count1_2_3_4_5_6_7_8_9_10_11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)46 VOID ia_enhaacplus_enc_count1_2_3_4_5_6_7_8_9_10_11(const WORD16 *values, const WORD32 width,
47                                                     WORD32 *bitcnt,
48                                                     ixheaace_huffman_tables *pstr_huffman_tbl,
49                                                     WORD32 invalid_bitcnt) {
50   WORD32 i;
51   WORD32 bits1_2, bits3_4, bits5_6, bits7_8, bits9_10, bit11, sc;
52   WORD32 temp_0, temp_1, temp_2, temp_3;
53   (VOID) invalid_bitcnt;
54   bits1_2 = 0;
55   bits3_4 = 0;
56   bits5_6 = 0;
57   bits7_8 = 0;
58   bits9_10 = 0;
59   bit11 = 0;
60   sc = 0;
61 
62   for (i = 0; i < width; i += 4) {
63     temp_0 = values[i + 0];
64     temp_1 = values[i + 1];
65     temp_2 = values[i + 2];
66     temp_3 = values[i + 3];
67 
68     bits1_2 +=
69         EXPAND(pstr_huffman_tbl->huff_ltab1_2[temp_0 + 1][temp_1 + 1][temp_2 + 1][temp_3 + 1]);
70 
71     bits5_6 += EXPAND(pstr_huffman_tbl->huff_ltab5_6[temp_0 + 4][temp_1 + 4]);
72 
73     bits5_6 += EXPAND(pstr_huffman_tbl->huff_ltab5_6[temp_2 + 4][temp_3 + 4]);
74 
75     sc += (temp_0 != 0) + (temp_1 != 0) + (temp_2 != 0) + (temp_3 != 0);
76 
77     temp_0 = abs32(temp_0);
78     temp_1 = abs32(temp_1);
79     temp_2 = abs32(temp_2);
80     temp_3 = abs32(temp_3);
81 
82     bits3_4 += EXPAND(pstr_huffman_tbl->huff_ltab3_4[temp_0][temp_1][temp_2][temp_3]);
83 
84     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
85 
86     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_2][temp_3]);
87 
88     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
89 
90     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_2][temp_3]);
91 
92     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
93 
94     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_2][temp_3]);
95   }
96 
97   bitcnt[1] = HI_EXPLTAB(bits1_2);
98 
99   bitcnt[2] = LO_EXPLTAB(bits1_2);
100 
101   bitcnt[3] = HI_EXPLTAB(bits3_4) + sc;
102 
103   bitcnt[4] = LO_EXPLTAB(bits3_4) + sc;
104 
105   bitcnt[5] = HI_EXPLTAB(bits5_6);
106 
107   bitcnt[6] = LO_EXPLTAB(bits5_6);
108 
109   bitcnt[7] = HI_EXPLTAB(bits7_8) + sc;
110 
111   bitcnt[8] = LO_EXPLTAB(bits7_8) + sc;
112 
113   bitcnt[9] = HI_EXPLTAB(bits9_10) + sc;
114 
115   bitcnt[10] = LO_EXPLTAB(bits9_10) + sc;
116 
117   bitcnt[11] = bit11 + sc;
118 }
119 
ia_enhaacplus_enc_count3_4_5_6_7_8_9_10_11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)120 static VOID ia_enhaacplus_enc_count3_4_5_6_7_8_9_10_11(const WORD16 *values, const WORD32 width,
121                                                        WORD32 *bitcnt,
122                                                        ixheaace_huffman_tables *pstr_huffman_tbl,
123                                                        WORD32 invalid_bitcnt) {
124   WORD32 i;
125   WORD32 bits3_4, bits5_6, bits7_8, bits9_10, bit11, sc;
126   WORD32 temp_0, temp_1, temp_2, temp_3;
127 
128   bits3_4 = 0;
129   bits5_6 = 0;
130   bits7_8 = 0;
131   bits9_10 = 0;
132   bit11 = 0;
133   sc = 0;
134 
135   for (i = 0; i < width; i += 4) {
136     temp_0 = values[i + 0];
137     temp_1 = values[i + 1];
138     temp_2 = values[i + 2];
139     temp_3 = values[i + 3];
140 
141     bits5_6 += EXPAND(pstr_huffman_tbl->huff_ltab5_6[temp_0 + 4][temp_1 + 4]);
142 
143     bits5_6 += EXPAND(pstr_huffman_tbl->huff_ltab5_6[temp_2 + 4][temp_3 + 4]);
144 
145     temp_0 = abs32(temp_0);
146     temp_1 = abs32(temp_1);
147     temp_2 = abs32(temp_2);
148     temp_3 = abs32(temp_3);
149 
150     bits3_4 += EXPAND(pstr_huffman_tbl->huff_ltab3_4[temp_0][temp_1][temp_2][temp_3]);
151 
152     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
153 
154     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_2][temp_3]);
155 
156     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
157 
158     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_2][temp_3]);
159 
160     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
161 
162     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_2][temp_3]);
163 
164     sc += (temp_0 > 0) + (temp_1 > 0) + (temp_2 > 0) + (temp_3 > 0);
165   }
166 
167   bitcnt[1] = invalid_bitcnt;
168   bitcnt[2] = invalid_bitcnt;
169 
170   bitcnt[3] = HI_EXPLTAB(bits3_4) + sc;
171 
172   bitcnt[4] = LO_EXPLTAB(bits3_4) + sc;
173 
174   bitcnt[5] = HI_EXPLTAB(bits5_6);
175 
176   bitcnt[6] = LO_EXPLTAB(bits5_6);
177 
178   bitcnt[7] = HI_EXPLTAB(bits7_8) + sc;
179 
180   bitcnt[8] = LO_EXPLTAB(bits7_8) + sc;
181 
182   bitcnt[9] = HI_EXPLTAB(bits9_10) + sc;
183 
184   bitcnt[10] = LO_EXPLTAB(bits9_10) + sc;
185 
186   bitcnt[11] = bit11 + sc;
187 }
188 
ia_enhaacplus_enc_count5_6_7_8_9_10_11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)189 static VOID ia_enhaacplus_enc_count5_6_7_8_9_10_11(const WORD16 *values, const WORD32 width,
190                                                    WORD32 *bitcnt,
191                                                    ixheaace_huffman_tables *pstr_huffman_tbl,
192                                                    WORD32 invalid_bitcnt) {
193   WORD32 i;
194   WORD32 bits5_6, bits7_8, bits9_10, bit11, sc;
195   WORD32 temp_0, temp_1;
196 
197   bits5_6 = 0;
198   bits7_8 = 0;
199   bits9_10 = 0;
200   bit11 = 0;
201   sc = 0;
202 
203   for (i = 0; i < width; i += 2) {
204     temp_0 = values[i + 0];
205     temp_1 = values[i + 1];
206 
207     bits5_6 += EXPAND(pstr_huffman_tbl->huff_ltab5_6[temp_0 + 4][temp_1 + 4]);
208 
209     temp_0 = abs32(temp_0);
210     temp_1 = abs32(temp_1);
211 
212     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
213 
214     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
215 
216     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
217 
218     sc += (temp_0 > 0) + (temp_1 > 0);
219   }
220 
221   bitcnt[1] = invalid_bitcnt;
222   bitcnt[2] = invalid_bitcnt;
223   bitcnt[3] = invalid_bitcnt;
224   bitcnt[4] = invalid_bitcnt;
225 
226   bitcnt[5] = HI_EXPLTAB(bits5_6);
227 
228   bitcnt[6] = LO_EXPLTAB(bits5_6);
229 
230   bitcnt[7] = HI_EXPLTAB(bits7_8) + sc;
231 
232   bitcnt[8] = LO_EXPLTAB(bits7_8) + sc;
233 
234   bitcnt[9] = HI_EXPLTAB(bits9_10) + sc;
235 
236   bitcnt[10] = LO_EXPLTAB(bits9_10) + sc;
237 
238   bitcnt[11] = bit11 + sc;
239 }
240 
ia_enhaacplus_enc_count7_8_9_10_11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)241 static VOID ia_enhaacplus_enc_count7_8_9_10_11(const WORD16 *values, const WORD32 width,
242                                                WORD32 *bitcnt,
243                                                ixheaace_huffman_tables *pstr_huffman_tbl,
244                                                WORD32 invalid_bitcnt) {
245   WORD32 i;
246   WORD32 bits7_8, bits9_10, bit11, sc;
247   WORD32 temp_0, temp_1;
248 
249   bits7_8 = 0;
250   bits9_10 = 0;
251   bit11 = 0;
252   sc = 0;
253 
254   for (i = 0; i < width; i += 2) {
255     temp_0 = abs32(values[i + 0]);
256     temp_1 = abs32(values[i + 1]);
257 
258     bits7_8 += EXPAND(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
259 
260     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
261 
262     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
263 
264     sc += (temp_0 > 0) + (temp_1 > 0);
265   }
266 
267   bitcnt[1] = invalid_bitcnt;
268   bitcnt[2] = invalid_bitcnt;
269   bitcnt[3] = invalid_bitcnt;
270   bitcnt[4] = invalid_bitcnt;
271   bitcnt[5] = invalid_bitcnt;
272   bitcnt[6] = invalid_bitcnt;
273 
274   bitcnt[7] = HI_EXPLTAB(bits7_8) + sc;
275 
276   bitcnt[8] = LO_EXPLTAB(bits7_8) + sc;
277 
278   bitcnt[9] = HI_EXPLTAB(bits9_10) + sc;
279 
280   bitcnt[10] = LO_EXPLTAB(bits9_10) + sc;
281 
282   bitcnt[11] = bit11 + sc;
283 }
284 
ia_enhaacplus_enc_count9_10_11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)285 static VOID ia_enhaacplus_enc_count9_10_11(const WORD16 *values, const WORD32 width,
286                                            WORD32 *bitcnt,
287                                            ixheaace_huffman_tables *pstr_huffman_tbl,
288                                            WORD32 invalid_bitcnt) {
289   WORD32 i;
290   WORD32 bits9_10, bit11, sc;
291   WORD32 temp_0, temp_1;
292 
293   bits9_10 = 0;
294   bit11 = 0;
295   sc = 0;
296 
297   for (i = 0; i < width; i += 2) {
298     temp_0 = abs32(values[i + 0]);
299     temp_1 = abs32(values[i + 1]);
300 
301     bits9_10 += EXPAND(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
302 
303     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
304 
305     sc += (temp_0 > 0) + (temp_1 > 0);
306   }
307 
308   bitcnt[1] = invalid_bitcnt;
309   bitcnt[2] = invalid_bitcnt;
310   bitcnt[3] = invalid_bitcnt;
311   bitcnt[4] = invalid_bitcnt;
312   bitcnt[5] = invalid_bitcnt;
313   bitcnt[6] = invalid_bitcnt;
314   bitcnt[7] = invalid_bitcnt;
315   bitcnt[8] = invalid_bitcnt;
316 
317   bitcnt[9] = HI_EXPLTAB(bits9_10) + sc;
318 
319   bitcnt[10] = LO_EXPLTAB(bits9_10) + sc;
320 
321   bitcnt[11] = bit11 + sc;
322 }
323 
ia_enhaacplus_enc_count11(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)324 static VOID ia_enhaacplus_enc_count11(const WORD16 *values, const WORD32 width, WORD32 *bitcnt,
325                                       ixheaace_huffman_tables *pstr_huffman_tbl,
326                                       WORD32 invalid_bitcnt) {
327   WORD32 i;
328   WORD32 bit11, sc;
329   WORD32 temp_0, temp_1;
330 
331   bit11 = 0;
332   sc = 0;
333 
334   for (i = 0; i < width; i += 2) {
335     temp_0 = abs32(values[i + 0]);
336     temp_1 = abs32(values[i + 1]);
337 
338     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[temp_0][temp_1]);
339 
340     sc += (temp_0 > 0) + (temp_1 > 0);
341   }
342 
343   bitcnt[1] = invalid_bitcnt;
344   bitcnt[2] = invalid_bitcnt;
345   bitcnt[3] = invalid_bitcnt;
346   bitcnt[4] = invalid_bitcnt;
347   bitcnt[5] = invalid_bitcnt;
348   bitcnt[6] = invalid_bitcnt;
349   bitcnt[7] = invalid_bitcnt;
350   bitcnt[8] = invalid_bitcnt;
351   bitcnt[9] = invalid_bitcnt;
352   bitcnt[10] = invalid_bitcnt;
353 
354   bitcnt[11] = bit11 + sc;
355 }
356 
ia_enhaacplus_enc_count_esc(const WORD16 * values,const WORD32 width,WORD32 * bitcnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 invalid_bitcnt)357 static VOID ia_enhaacplus_enc_count_esc(const WORD16 *values, const WORD32 width, WORD32 *bitcnt,
358                                         ixheaace_huffman_tables *pstr_huffman_tbl,
359                                         WORD32 invalid_bitcnt) {
360   WORD32 i;
361   WORD32 bit11, ec, sc;
362   WORD32 temp_0, temp_1, t00, t01;
363 
364   bit11 = 0;
365   sc = 0;
366   ec = 0;
367 
368   for (i = 0; i < width; i += 2) {
369     temp_0 = abs32(values[i + 0]);
370     temp_1 = abs32(values[i + 1]);
371 
372     sc += (temp_0 > 0) + (temp_1 > 0);
373     t00 = MIN(temp_0, 16);
374     t01 = MIN(temp_1, 16);
375 
376     bit11 += EXPAND(pstr_huffman_tbl->huff_ltab11[t00][t01]);
377 
378     if (temp_0 >= 16) {
379       ec += 5;
380 
381       while ((temp_0 >>= 1) >= 16) {
382         ec += 2;
383       }
384     }
385 
386     if (temp_1 >= 16) {
387       ec += 5;
388 
389       while ((temp_1 >>= 1) >= 16) {
390         ec += 2;
391       }
392     }
393   }
394 
395   bitcnt[1] = invalid_bitcnt;
396   bitcnt[2] = invalid_bitcnt;
397   bitcnt[3] = invalid_bitcnt;
398   bitcnt[4] = invalid_bitcnt;
399   bitcnt[5] = invalid_bitcnt;
400   bitcnt[6] = invalid_bitcnt;
401   bitcnt[7] = invalid_bitcnt;
402   bitcnt[8] = invalid_bitcnt;
403   bitcnt[9] = invalid_bitcnt;
404   bitcnt[10] = invalid_bitcnt;
405 
406   bitcnt[11] = bit11 + sc + ec;
407 }
408 
409 typedef VOID (*COUNT_FUNCTION)(const WORD16 *values, const WORD32 width, WORD32 *bitcnt,
410                                ixheaace_huffman_tables *pstr_huffman_tbl, WORD32 invalid_bitcnt);
411 
412 static COUNT_FUNCTION ia_enhaacplus_enc_count_func_tab[CODE_BCK_ESC_LAV + 1] = {
413 
414     ia_enhaacplus_enc_count1_2_3_4_5_6_7_8_9_10_11, /* 0  */
415     ia_enhaacplus_enc_count1_2_3_4_5_6_7_8_9_10_11, /* 1  */
416     ia_enhaacplus_enc_count3_4_5_6_7_8_9_10_11,     /* 2  */
417     ia_enhaacplus_enc_count5_6_7_8_9_10_11,         /* 3  */
418     ia_enhaacplus_enc_count5_6_7_8_9_10_11,         /* 4  */
419     ia_enhaacplus_enc_count7_8_9_10_11,             /* 5  */
420     ia_enhaacplus_enc_count7_8_9_10_11,             /* 6  */
421     ia_enhaacplus_enc_count7_8_9_10_11,             /* 7  */
422     ia_enhaacplus_enc_count9_10_11,                 /* 8  */
423     ia_enhaacplus_enc_count9_10_11,                 /* 9  */
424     ia_enhaacplus_enc_count9_10_11,                 /* 10 */
425     ia_enhaacplus_enc_count9_10_11,                 /* 11 */
426     ia_enhaacplus_enc_count9_10_11,                 /* 12 */
427     ia_enhaacplus_enc_count11,                      /* 13 */
428     ia_enhaacplus_enc_count11,                      /* 14 */
429     ia_enhaacplus_enc_count11,                      /* 15 */
430     ia_enhaacplus_enc_count_esc                     /* 16 */
431 };
432 
ia_enhaacplus_enc_bitcount(const WORD16 * ptr_values,const WORD32 width,WORD32 max_val,WORD32 * bit_cnt,ixheaace_huffman_tables * pstr_huffman_tbl,WORD32 aot)433 VOID ia_enhaacplus_enc_bitcount(const WORD16 *ptr_values, const WORD32 width, WORD32 max_val,
434                                 WORD32 *bit_cnt, ixheaace_huffman_tables *pstr_huffman_tbl,
435                                 WORD32 aot) {
436   WORD32 invalid_bitcnt;
437 
438   if (max_val == 0) {
439     *bit_cnt = 0;
440   } else {
441     switch (aot) {
442       case AOT_AAC_LC:
443       case AOT_SBR:
444       case AOT_PS:
445         *bit_cnt = INVALID_BITCOUNT_LC;
446         break;
447 
448       case AOT_AAC_LD:
449       case AOT_AAC_ELD:
450         *bit_cnt = INVALID_BITCOUNT_LD;
451         break;
452     }
453   }
454   invalid_bitcnt = *bit_cnt;
455   max_val = MIN(max_val, CODE_BCK_ESC_LAV);
456 
457   ia_enhaacplus_enc_count_func_tab[max_val](ptr_values, width, bit_cnt, pstr_huffman_tbl,
458                                             invalid_bitcnt);
459 }
460 
ia_enhaacplus_enc_code_values(WORD16 * ptr_values,WORD32 width,WORD32 code_book,ixheaace_bit_buf_handle pstr_bitstream,ixheaace_huffman_tables * pstr_huffman_tbl)461 VOID ia_enhaacplus_enc_code_values(WORD16 *ptr_values, WORD32 width, WORD32 code_book,
462                                    ixheaace_bit_buf_handle pstr_bitstream,
463                                    ixheaace_huffman_tables *pstr_huffman_tbl) {
464   WORD32 i, temp_0, temp_1, temp_2, temp_3, t00, t01;
465   WORD32 code_word, code_length;
466   WORD32 sign, sign_length;
467   WORD16 *ptr_temp_values = ptr_values;
468 
469   switch (code_book) {
470     case CODE_BCK_ZERO_NO:
471       break;
472 
473     case CODE_BCK_1_NO:
474       width = width >> 2;
475 
476       for (i = width - 1; i >= 0; i--) {
477         temp_0 = *ptr_temp_values++;
478         temp_1 = *ptr_temp_values++;
479         temp_2 = *ptr_temp_values++;
480         temp_3 = *ptr_temp_values++;
481 
482         code_word = pstr_huffman_tbl->huff_ctab1[temp_0 + 1][temp_1 + 1][temp_2 + 1][temp_3 + 1];
483 
484         code_length = HI_LTAB(
485             pstr_huffman_tbl->huff_ltab1_2[temp_0 + 1][temp_1 + 1][temp_2 + 1][temp_3 + 1]);
486 
487         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
488       }
489       break;
490 
491     case CODE_BCK_2_NO:
492 
493       width = width >> 2;
494 
495       for (i = width - 1; i >= 0; i--) {
496         temp_0 = *ptr_temp_values++;
497         temp_1 = *ptr_temp_values++;
498         temp_2 = *ptr_temp_values++;
499         temp_3 = *ptr_temp_values++;
500 
501         code_word = pstr_huffman_tbl->huff_ctab2[temp_0 + 1][temp_1 + 1][temp_2 + 1][temp_3 + 1];
502 
503         code_length = LO_LTAB(
504             pstr_huffman_tbl->huff_ltab1_2[temp_0 + 1][temp_1 + 1][temp_2 + 1][temp_3 + 1]);
505 
506         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
507       }
508       break;
509 
510     case CODE_BCK_3_NO:
511 
512       for (i = 0; i < width; i += 4) {
513         sign = 0;
514         sign_length = 0;
515         temp_0 = ptr_values[i + 0];
516 
517         if (temp_0 != 0) {
518           sign_length++;
519 
520           sign <<= 1;
521 
522           if (temp_0 < 0) {
523             sign |= 1;
524 
525             temp_0 = abs32(temp_0);
526           }
527         }
528 
529         temp_1 = ptr_values[i + 1];
530 
531         if (temp_1 != 0) {
532           sign_length++;
533 
534           sign <<= 1;
535 
536           if (temp_1 < 0) {
537             sign |= 1;
538 
539             temp_1 = abs32(temp_1);
540           }
541         }
542 
543         temp_2 = ptr_values[i + 2];
544 
545         if (temp_2 != 0) {
546           sign_length++;
547 
548           sign <<= 1;
549 
550           if (temp_2 < 0) {
551             sign |= 1;
552 
553             temp_2 = abs32(temp_2);
554           }
555         }
556 
557         temp_3 = ptr_values[i + 3];
558 
559         if (temp_3 != 0) {
560           sign_length++;
561 
562           sign <<= 1;
563 
564           if (temp_3 < 0) {
565             sign |= 1;
566 
567             temp_3 = abs32(temp_3);
568           }
569         }
570 
571         code_word = pstr_huffman_tbl->huff_ctab3[temp_0][temp_1][temp_2][temp_3];
572 
573         code_length = HI_LTAB(pstr_huffman_tbl->huff_ltab3_4[temp_0][temp_1][temp_2][temp_3]);
574 
575         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
576 
577         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
578       }
579       break;
580 
581     case CODE_BCK_4_NO:
582 
583       for (i = 0; i < width; i += 4) {
584         sign = 0;
585         sign_length = 0;
586         temp_0 = ptr_values[i + 0];
587 
588         if (temp_0 != 0) {
589           sign_length++;
590 
591           sign <<= 1;
592 
593           if (temp_0 < 0) {
594             sign |= 1;
595 
596             temp_0 = abs32(temp_0);
597           }
598         }
599 
600         temp_1 = ptr_values[i + 1];
601 
602         if (temp_1 != 0) {
603           sign_length++;
604 
605           sign <<= 1;
606 
607           if (temp_1 < 0) {
608             sign |= 1;
609 
610             temp_1 = abs32(temp_1);
611           }
612         }
613 
614         temp_2 = ptr_values[i + 2];
615 
616         if (temp_2 != 0) {
617           sign_length++;
618 
619           sign <<= 1;
620 
621           if (temp_2 < 0) {
622             sign |= 1;
623 
624             temp_2 = abs32(temp_2);
625           }
626         }
627 
628         temp_3 = ptr_values[i + 3];
629 
630         if (temp_3 != 0) {
631           sign_length++;
632 
633           sign <<= 1;
634 
635           if (temp_3 < 0) {
636             sign |= 1;
637 
638             temp_3 = abs32(temp_3);
639           }
640         }
641 
642         code_word = pstr_huffman_tbl->huff_ctab4[temp_0][temp_1][temp_2][temp_3];
643 
644         code_length = LO_LTAB(pstr_huffman_tbl->huff_ltab3_4[temp_0][temp_1][temp_2][temp_3]);
645 
646         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
647 
648         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
649       }
650       break;
651 
652     case CODE_BCK_5_NO:
653 
654       width = width >> 1;
655 
656       for (i = width - 1; i >= 0; i--) {
657         temp_0 = *ptr_temp_values++;
658         temp_1 = *ptr_temp_values++;
659 
660         code_word = pstr_huffman_tbl->huff_ctab5[temp_0 + 4][temp_1 + 4];
661 
662         code_length = HI_LTAB(pstr_huffman_tbl->huff_ltab5_6[temp_0 + 4][temp_1 + 4]);
663 
664         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
665       }
666       break;
667 
668     case CODE_BCK_6_NO:
669 
670       width = width >> 1;
671 
672       for (i = width - 1; i >= 0; i--) {
673         temp_0 = *ptr_temp_values++;
674         temp_1 = *ptr_temp_values++;
675 
676         code_word = pstr_huffman_tbl->huff_ctab6[temp_0 + 4][temp_1 + 4];
677 
678         code_length = LO_LTAB(pstr_huffman_tbl->huff_ltab5_6[temp_0 + 4][temp_1 + 4]);
679 
680         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
681       }
682       break;
683 
684     case CODE_BCK_7_NO:
685 
686       for (i = 0; i < width; i += 2) {
687         sign = 0;
688         sign_length = 0;
689         temp_0 = ptr_values[i + 0];
690 
691         if (temp_0 != 0) {
692           sign_length++;
693 
694           sign <<= 1;
695 
696           if (temp_0 < 0) {
697             sign |= 1;
698 
699             temp_0 = abs32(temp_0);
700           }
701         }
702 
703         temp_1 = ptr_values[i + 1];
704 
705         if (temp_1 != 0) {
706           sign_length++;
707 
708           sign <<= 1;
709 
710           if (temp_1 < 0) {
711             sign |= 1;
712 
713             temp_1 = abs32(temp_1);
714           }
715         }
716 
717         code_word = pstr_huffman_tbl->huff_ctab7[temp_0][temp_1];
718 
719         code_length = HI_LTAB(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
720 
721         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
722 
723         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
724       }
725       break;
726 
727     case CODE_BCK_8_NO:
728 
729       for (i = 0; i < width; i += 2) {
730         sign = 0;
731         sign_length = 0;
732         temp_0 = ptr_values[i + 0];
733 
734         if (temp_0 != 0) {
735           sign_length++;
736 
737           sign <<= 1;
738 
739           if (temp_0 < 0) {
740             sign |= 1;
741 
742             temp_0 = abs32(temp_0);
743           }
744         }
745 
746         temp_1 = ptr_values[i + 1];
747 
748         if (temp_1 != 0) {
749           sign_length++;
750 
751           sign <<= 1;
752 
753           if (temp_1 < 0) {
754             sign |= 1;
755 
756             temp_1 = abs32(temp_1);
757           }
758         }
759 
760         code_word = pstr_huffman_tbl->huff_ctab8[temp_0][temp_1];
761 
762         code_length = LO_LTAB(pstr_huffman_tbl->huff_ltab7_8[temp_0][temp_1]);
763 
764         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
765 
766         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
767       }
768       break;
769 
770     case CODE_BCK_9_NO:
771 
772       for (i = 0; i < width; i += 2) {
773         sign = 0;
774         sign_length = 0;
775         temp_0 = ptr_values[i + 0];
776 
777         if (temp_0 != 0) {
778           sign_length++;
779 
780           sign <<= 1;
781 
782           if (temp_0 < 0) {
783             sign |= 1;
784 
785             temp_0 = abs32(temp_0);
786           }
787         }
788 
789         temp_1 = ptr_values[i + 1];
790 
791         if (temp_1 != 0) {
792           sign_length++;
793 
794           sign <<= 1;
795 
796           if (temp_1 < 0) {
797             sign |= 1;
798 
799             temp_1 = abs32(temp_1);
800           }
801         }
802 
803         code_word = pstr_huffman_tbl->huff_ctab9[temp_0][temp_1];
804 
805         code_length = HI_LTAB(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
806 
807         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
808 
809         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
810       }
811       break;
812 
813     case CODE_BCK_10_NO:
814 
815       for (i = 0; i < width; i += 2) {
816         sign = 0;
817         sign_length = 0;
818         temp_0 = ptr_values[i + 0];
819 
820         if (temp_0 != 0) {
821           sign_length++;
822 
823           sign <<= 1;
824 
825           if (temp_0 < 0) {
826             sign |= 1;
827 
828             temp_0 = abs32(temp_0);
829           }
830         }
831 
832         temp_1 = ptr_values[i + 1];
833 
834         if (temp_1 != 0) {
835           sign_length++;
836 
837           sign <<= 1;
838 
839           if (temp_1 < 0) {
840             sign |= 1;
841 
842             temp_1 = abs32(temp_1);
843           }
844         }
845 
846         code_word = pstr_huffman_tbl->huff_ctab10[temp_0][temp_1];
847 
848         code_length = LO_LTAB(pstr_huffman_tbl->huff_ltab9_10[temp_0][temp_1]);
849 
850         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
851 
852         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
853       }
854       break;
855 
856     case CODE_BCK_ESC_NO:
857 
858       for (i = 0; i < width; i += 2) {
859         sign = 0;
860         sign_length = 0;
861         temp_0 = ptr_values[i + 0];
862 
863         if (temp_0 != 0) {
864           sign_length++;
865 
866           sign <<= 1;
867 
868           if (temp_0 < 0) {
869             sign |= 1;
870 
871             temp_0 = abs32(temp_0);
872           }
873         }
874 
875         temp_1 = ptr_values[i + 1];
876 
877         if (temp_1 != 0) {
878           sign_length++;
879 
880           sign <<= 1;
881 
882           if (temp_1 < 0) {
883             sign |= 1;
884 
885             temp_1 = abs32(temp_1);
886           }
887         }
888 
889         t00 = MIN(temp_0, 16);
890         t01 = MIN(temp_1, 16);
891 
892         code_word = pstr_huffman_tbl->huff_ctab11[t00][t01];
893         code_length = pstr_huffman_tbl->huff_ltab11[t00][t01];
894 
895         ixheaace_write_bits(pstr_bitstream, code_word, (UWORD8)code_length);
896 
897         ixheaace_write_bits(pstr_bitstream, sign, (UWORD8)sign_length);
898 
899         if (temp_0 >= 16) {
900           WORD32 n, p;
901 
902           n = 0;
903           p = temp_0;
904 
905           while ((p >>= 1) >= 16) {
906             ixheaace_write_bits(pstr_bitstream, 1, 1);
907 
908             n++;
909           }
910 
911           ixheaace_write_bits(pstr_bitstream, 0, 1);
912 
913           ixheaace_write_bits(pstr_bitstream, temp_0 - (1 << (n + 4)), (UWORD8)(n + 4));
914         }
915         if (temp_1 >= 16) {
916           WORD32 n, p;
917 
918           n = 0;
919           p = temp_1;
920 
921           while ((p >>= 1) >= 16) {
922             ixheaace_write_bits(pstr_bitstream, 1, 1);
923 
924             n++;
925           }
926 
927           ixheaace_write_bits(pstr_bitstream, 0, 1);
928 
929           ixheaace_write_bits(pstr_bitstream, temp_1 - (1 << (n + 4)), (UWORD8)(n + 4));
930         }
931       }
932       break;
933 
934     default:
935       break;
936   }
937 }
938 
ia_enhaacplus_enc_code_scale_factor_delta(WORD32 delta,ixheaace_bit_buf_handle h_bitstream,ixheaace_huffman_tables * pstr_huffman_tbl)939 VOID ia_enhaacplus_enc_code_scale_factor_delta(WORD32 delta,
940                                                ixheaace_bit_buf_handle h_bitstream,
941                                                ixheaace_huffman_tables *pstr_huffman_tbl) {
942   WORD32 code_word, code_length;
943 
944   code_word = pstr_huffman_tbl->huff_ctabscf[delta + CODE_BCK_SCF_LAV];
945   code_length = pstr_huffman_tbl->huff_ltabscf[delta + CODE_BCK_SCF_LAV];
946 
947   ixheaace_write_bits(h_bitstream, code_word, (UWORD8)code_length);
948 }
949