• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*********************** MPEG surround encoder library *************************
96 
97    Author(s):   Karsten Linzmeier
98 
99    Description: Noiseless Coding
100                 Huffman encoder
101 
102 *******************************************************************************/
103 
104 /* Includes ******************************************************************/
105 #include "sacenc_nlc_enc.h"
106 
107 #include "genericStds.h"
108 #include "fixpoint_math.h"
109 
110 #include "sacenc_const.h"
111 #include "sacenc_huff_tab.h"
112 #include "sacenc_paramextract.h"
113 
114 /* Defines *******************************************************************/
115 #define PAIR_SHIFT 4
116 #define PAIR_MASK 0xf
117 
118 #define PBC_MIN_BANDS 5
119 
120 typedef enum {
121   BACKWARDS = 0x0,
122   FORWARDS = 0x1
123 
124 } DIRECTION;
125 
126 typedef enum {
127   DIFF_FREQ = 0x0,
128   DIFF_TIME = 0x1
129 
130 } DIFF_TYPE;
131 
132 typedef enum {
133   HUFF_1D = 0x0,
134   HUFF_2D = 0x1
135 
136 } CODING_SCHEME;
137 
138 typedef enum {
139   FREQ_PAIR = 0x0,
140   TIME_PAIR = 0x1
141 
142 } PAIRING;
143 
144 /* Data Types ****************************************************************/
145 
146 /* Constants *****************************************************************/
147 static const UCHAR lavHuffVal[4] = {0, 2, 6, 7};
148 static const UCHAR lavHuffLen[4] = {1, 2, 3, 3};
149 
150 static const UCHAR lav_step_CLD[] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3};
151 static const UCHAR lav_step_ICC[] = {0, 0, 1, 1, 2, 2, 3, 3};
152 
153 /* Function / Class Declarations *********************************************/
154 
155 /* Function / Class Definition ***********************************************/
split_lsb(const SHORT * const in_data,SHORT offset,const INT num_val,SHORT * const out_data_lsb,SHORT * const out_data_msb)156 static void split_lsb(const SHORT *const in_data, SHORT offset,
157                       const INT num_val, SHORT *const out_data_lsb,
158                       SHORT *const out_data_msb) {
159   int i;
160 
161   for (i = 0; i < num_val; i++) {
162     SHORT val = in_data[i] + offset;
163     if (out_data_lsb != NULL) out_data_lsb[i] = val & 0x0001;
164     if (out_data_msb != NULL) out_data_msb[i] = val >> 1;
165   }
166 }
167 
apply_lsb_coding(HANDLE_FDK_BITSTREAM strm,const SHORT * const in_data_lsb,const UINT num_lsb,const INT num_val)168 static void apply_lsb_coding(HANDLE_FDK_BITSTREAM strm,
169                              const SHORT *const in_data_lsb, const UINT num_lsb,
170                              const INT num_val) {
171   int i;
172 
173   for (i = 0; i < num_val; i++) {
174     FDKwriteBits(strm, in_data_lsb[i], num_lsb);
175   }
176 }
177 
calc_diff_freq(const SHORT * const in_data,SHORT * const out_data,const INT num_val)178 static void calc_diff_freq(const SHORT *const in_data, SHORT *const out_data,
179                            const INT num_val) {
180   int i;
181   out_data[0] = in_data[0];
182 
183   for (i = 1; i < num_val; i++) {
184     out_data[i] = in_data[i] - in_data[i - 1];
185   }
186 }
187 
calc_diff_time(const SHORT * const in_data,const SHORT * const prev_data,SHORT * const out_data,const INT num_val)188 static void calc_diff_time(const SHORT *const in_data,
189                            const SHORT *const prev_data, SHORT *const out_data,
190                            const INT num_val) {
191   int i;
192   out_data[0] = in_data[0];
193   out_data[1] = prev_data[0];
194 
195   for (i = 0; i < num_val; i++) {
196     out_data[i + 2] = in_data[i] - prev_data[i];
197   }
198 }
199 
sym_check(SHORT data[2],const INT lav,SHORT * const pSym_bits)200 static INT sym_check(SHORT data[2], const INT lav, SHORT *const pSym_bits) {
201   UCHAR symBits = 0;
202   int sum_val = data[0] + data[1];
203   int diff_val = data[0] - data[1];
204   int num_sbits = 0;
205 
206   if (sum_val != 0) {
207     int sum_neg = (sum_val < 0) ? 1 : 0;
208     if (sum_neg) {
209       sum_val = -sum_val;
210       diff_val = -diff_val;
211     }
212     symBits = (symBits << 1) | sum_neg;
213     num_sbits++;
214   }
215 
216   if (diff_val != 0) {
217     int diff_neg = (diff_val < 0) ? 1 : 0;
218     if (diff_neg) {
219       diff_val = -diff_val;
220     }
221     symBits = (symBits << 1) | diff_neg;
222     num_sbits++;
223   }
224 
225   if (pSym_bits != NULL) {
226     *pSym_bits = symBits;
227   }
228 
229   if (sum_val % 2) {
230     data[0] = lav - sum_val / 2;
231     data[1] = lav - diff_val / 2;
232   } else {
233     data[0] = sum_val / 2;
234     data[1] = diff_val / 2;
235   }
236 
237   return num_sbits;
238 }
239 
ilog2(UINT i)240 static INT ilog2(UINT i) {
241   int l = 0;
242 
243   if (i) i--;
244   while (i > 0) {
245     i >>= 1;
246     l++;
247   }
248 
249   return l;
250 }
251 
calc_pcm_bits(const SHORT num_val,const SHORT num_levels)252 static SHORT calc_pcm_bits(const SHORT num_val, const SHORT num_levels) {
253   SHORT num_complete_chunks = 0, rest_chunk_size = 0;
254   SHORT max_grp_len = 0, bits_pcm = 0;
255   int chunk_levels, i;
256 
257   switch (num_levels) {
258     case 3:
259       max_grp_len = 5;
260       break;
261     case 6:
262       max_grp_len = 5;
263       break;
264     case 7:
265       max_grp_len = 6;
266       break;
267     case 11:
268       max_grp_len = 2;
269       break;
270     case 13:
271       max_grp_len = 4;
272       break;
273     case 19:
274       max_grp_len = 4;
275       break;
276     case 25:
277       max_grp_len = 3;
278       break;
279     case 51:
280       max_grp_len = 4;
281       break;
282     default:
283       max_grp_len = 1;
284   }
285 
286   num_complete_chunks = num_val / max_grp_len;
287   rest_chunk_size = num_val % max_grp_len;
288 
289   chunk_levels = 1;
290   for (i = 1; i <= max_grp_len; i++) {
291     chunk_levels *= num_levels;
292   }
293 
294   bits_pcm = (SHORT)(ilog2(chunk_levels) * num_complete_chunks);
295   bits_pcm += (SHORT)(ilog2(num_levels) * rest_chunk_size);
296 
297   return bits_pcm;
298 }
299 
apply_pcm_coding(HANDLE_FDK_BITSTREAM strm,const SHORT * const in_data_1,const SHORT * const in_data_2,const SHORT offset,const SHORT num_val,const SHORT num_levels)300 static void apply_pcm_coding(HANDLE_FDK_BITSTREAM strm,
301                              const SHORT *const in_data_1,
302                              const SHORT *const in_data_2, const SHORT offset,
303                              const SHORT num_val, const SHORT num_levels) {
304   SHORT i = 0, j = 0, idx = 0;
305   SHORT max_grp_len = 0, grp_len = 0, next_val = 0;
306   int grp_val = 0, chunk_levels = 0;
307 
308   SHORT pcm_chunk_size[7] = {0};
309 
310   switch (num_levels) {
311     case 3:
312       max_grp_len = 5;
313       break;
314     case 5:
315       max_grp_len = 3;
316       break;
317     case 6:
318       max_grp_len = 5;
319       break;
320     case 7:
321       max_grp_len = 6;
322       break;
323     case 9:
324       max_grp_len = 5;
325       break;
326     case 11:
327       max_grp_len = 2;
328       break;
329     case 13:
330       max_grp_len = 4;
331       break;
332     case 19:
333       max_grp_len = 4;
334       break;
335     case 25:
336       max_grp_len = 3;
337       break;
338     case 51:
339       max_grp_len = 4;
340       break;
341     default:
342       max_grp_len = 1;
343   }
344 
345   chunk_levels = 1;
346   for (i = 1; i <= max_grp_len; i++) {
347     chunk_levels *= num_levels;
348     pcm_chunk_size[i] = ilog2(chunk_levels);
349   }
350 
351   for (i = 0; i < num_val; i += max_grp_len) {
352     grp_len = FDKmin(max_grp_len, num_val - i);
353     grp_val = 0;
354     for (j = 0; j < grp_len; j++) {
355       idx = i + j;
356       if (in_data_2 == NULL) {
357         next_val = in_data_1[idx];
358       } else if (in_data_1 == NULL) {
359         next_val = in_data_2[idx];
360       } else {
361         next_val = ((idx % 2) ? in_data_2[idx / 2] : in_data_1[idx / 2]);
362       }
363       next_val += offset;
364       grp_val = grp_val * num_levels + next_val;
365     }
366 
367     FDKwriteBits(strm, grp_val, pcm_chunk_size[grp_len]);
368   }
369 }
370 
huff_enc_1D(HANDLE_FDK_BITSTREAM strm,const DATA_TYPE data_type,const INT dim1,SHORT * const in_data,const SHORT num_val,const SHORT p0_flag)371 static UINT huff_enc_1D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
372                         const INT dim1, SHORT *const in_data,
373                         const SHORT num_val, const SHORT p0_flag) {
374   int i, offset = 0;
375   UINT huffBits = 0;
376 
377   HUFF_ENTRY part0 = {0};
378   const HUFF_ENTRY *pHuffTab = NULL;
379 
380   switch (data_type) {
381     case t_CLD:
382       pHuffTab = fdk_sacenc_huffCLDTab.h1D[dim1];
383       break;
384     case t_ICC:
385       pHuffTab = fdk_sacenc_huffICCTab.h1D[dim1];
386       break;
387   }
388 
389   if (p0_flag) {
390     switch (data_type) {
391       case t_CLD:
392         part0 = fdk_sacenc_huffPart0Tab.cld[in_data[0]];
393         break;
394       case t_ICC:
395         part0 = fdk_sacenc_huffPart0Tab.icc[in_data[0]];
396         break;
397     }
398     huffBits += FDKwriteBits(strm, HUFF_VALUE(part0), HUFF_LENGTH(part0));
399     offset = 1;
400   }
401 
402   for (i = offset; i < num_val; i++) {
403     int id_sign = 0;
404     int id = in_data[i];
405 
406     if (id != 0) {
407       id_sign = 0;
408       if (id < 0) {
409         id = -id;
410         id_sign = 1;
411       }
412     }
413 
414     huffBits +=
415         FDKwriteBits(strm, HUFF_VALUE(pHuffTab[id]), HUFF_LENGTH(pHuffTab[id]));
416 
417     if (id != 0) {
418       huffBits += FDKwriteBits(strm, id_sign, 1);
419     }
420   } /* for i */
421 
422   return huffBits;
423 }
424 
getHuffEntry(const INT lav,const DATA_TYPE data_type,const INT i,const SHORT tab_idx_2D[2],const SHORT in_data[][2],HUFF_ENTRY * const pEntry,HUFF_ENTRY * const pEscape)425 static void getHuffEntry(const INT lav, const DATA_TYPE data_type, const INT i,
426                          const SHORT tab_idx_2D[2], const SHORT in_data[][2],
427                          HUFF_ENTRY *const pEntry, HUFF_ENTRY *const pEscape) {
428   const HUFF_CLD_TAB_2D *pCLD2dTab =
429       &fdk_sacenc_huffCLDTab.h2D[tab_idx_2D[0]][tab_idx_2D[1]];
430   const HUFF_ICC_TAB_2D *pICC2dTab =
431       &fdk_sacenc_huffICCTab.h2D[tab_idx_2D[0]][tab_idx_2D[1]];
432 
433   switch (lav) {
434     case 1: {
435       const LAV1_2D *pLav1 = NULL;
436       switch (data_type) {
437         case t_CLD:
438           pLav1 = NULL;
439           break;
440         case t_ICC:
441           pLav1 = &pICC2dTab->lav1;
442           break;
443       }
444       if (pLav1 != NULL) {
445         *pEntry = pLav1->entry[in_data[i][0]][in_data[i][1]];
446         *pEscape = pLav1->escape;
447       }
448     } break;
449     case 3: {
450       const LAV3_2D *pLav3 = NULL;
451       switch (data_type) {
452         case t_CLD:
453           pLav3 = &pCLD2dTab->lav3;
454           break;
455         case t_ICC:
456           pLav3 = &pICC2dTab->lav3;
457           break;
458       }
459       if (pLav3 != NULL) {
460         *pEntry = pLav3->entry[in_data[i][0]][in_data[i][1]];
461         *pEscape = pLav3->escape;
462       }
463     } break;
464     case 5: {
465       const LAV5_2D *pLav5 = NULL;
466       switch (data_type) {
467         case t_CLD:
468           pLav5 = &pCLD2dTab->lav5;
469           break;
470         case t_ICC:
471           pLav5 = &pICC2dTab->lav5;
472           break;
473       }
474       if (pLav5 != NULL) {
475         *pEntry = pLav5->entry[in_data[i][0]][in_data[i][1]];
476         *pEscape = pLav5->escape;
477       }
478     } break;
479     case 7: {
480       const LAV7_2D *pLav7 = NULL;
481       switch (data_type) {
482         case t_CLD:
483           pLav7 = &pCLD2dTab->lav7;
484           break;
485         case t_ICC:
486           pLav7 = &pICC2dTab->lav7;
487           break;
488       }
489       if (pLav7 != NULL) {
490         *pEntry = pLav7->entry[in_data[i][0]][in_data[i][1]];
491         *pEscape = pLav7->escape;
492       }
493     } break;
494     case 9: {
495       const LAV9_2D *pLav9 = NULL;
496       switch (data_type) {
497         case t_CLD:
498           pLav9 = &pCLD2dTab->lav9;
499           break;
500         case t_ICC:
501           pLav9 = NULL;
502           break;
503       }
504       if (pLav9 != NULL) {
505         *pEntry = pLav9->entry[in_data[i][0]][in_data[i][1]];
506         *pEscape = pLav9->escape;
507       }
508     } break;
509   }
510 }
511 
huff_enc_2D(HANDLE_FDK_BITSTREAM strm,const DATA_TYPE data_type,SHORT tab_idx_2D[2],SHORT lav_idx,SHORT in_data[][2],SHORT num_val,SHORT stride,SHORT * p0_data[2])512 static UINT huff_enc_2D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
513                         SHORT tab_idx_2D[2], SHORT lav_idx, SHORT in_data[][2],
514                         SHORT num_val, SHORT stride, SHORT *p0_data[2]) {
515   SHORT i = 0, lav = 0, num_sbits = 0, sym_bits = 0, escIdx = 0;
516   SHORT esc_data[2][MAXBANDS] = {{0}};
517 
518   UINT huffBits = 0;
519 
520   const HUFF_ENTRY *pHuffEntry = NULL;
521 
522   switch (data_type) {
523     case t_CLD:
524       lav = 2 * lav_idx + 3; /* LAV */
525       pHuffEntry = fdk_sacenc_huffPart0Tab.cld;
526       break;
527     case t_ICC:
528       lav = 2 * lav_idx + 1; /* LAV */
529       pHuffEntry = fdk_sacenc_huffPart0Tab.icc;
530       break;
531   }
532 
533   /* Partition 0 */
534   if (p0_data[0] != NULL) {
535     HUFF_ENTRY entry = pHuffEntry[*p0_data[0]];
536     huffBits += FDKwriteBits(strm, HUFF_VALUE(entry), HUFF_LENGTH(entry));
537   }
538   if (p0_data[1] != NULL) {
539     HUFF_ENTRY entry = pHuffEntry[*p0_data[1]];
540     huffBits += FDKwriteBits(strm, HUFF_VALUE(entry), HUFF_LENGTH(entry));
541   }
542 
543   for (i = 0; i < num_val; i += stride) {
544     HUFF_ENTRY entry = {0};
545     HUFF_ENTRY escape = {0};
546 
547     esc_data[0][escIdx] = in_data[i][0] + lav;
548     esc_data[1][escIdx] = in_data[i][1] + lav;
549 
550     num_sbits = sym_check(in_data[i], lav, &sym_bits);
551 
552     getHuffEntry(lav, data_type, i, tab_idx_2D, in_data, &entry, &escape);
553 
554     huffBits += FDKwriteBits(strm, HUFF_VALUE(entry), HUFF_LENGTH(entry));
555 
556     if ((HUFF_VALUE(entry) == HUFF_VALUE(escape)) &&
557         (HUFF_LENGTH(entry) == HUFF_LENGTH(escape))) {
558       escIdx++;
559     } else {
560       huffBits += FDKwriteBits(strm, sym_bits, num_sbits);
561     }
562   } /* for i */
563 
564   if (escIdx > 0) {
565     huffBits += calc_pcm_bits(2 * escIdx, (2 * lav + 1));
566     if (strm != NULL) {
567       apply_pcm_coding(strm, esc_data[0], esc_data[1], 0 /*offset*/, 2 * escIdx,
568                        (2 * lav + 1));
569     }
570   }
571 
572   return huffBits;
573 }
574 
get_next_lav_step(const INT lav,const DATA_TYPE data_type)575 static SCHAR get_next_lav_step(const INT lav, const DATA_TYPE data_type) {
576   SCHAR lav_step = 0;
577 
578   switch (data_type) {
579     case t_CLD:
580       lav_step = (lav > 9) ? -1 : lav_step_CLD[lav];
581       break;
582     case t_ICC:
583       lav_step = (lav > 7) ? -1 : lav_step_ICC[lav];
584       break;
585   }
586 
587   return lav_step;
588 }
589 
diff_type_offset(const DIFF_TYPE diff_type)590 static INT diff_type_offset(const DIFF_TYPE diff_type) {
591   int offset = 0;
592   switch (diff_type) {
593     case DIFF_FREQ:
594       offset = 0;
595       break;
596     case DIFF_TIME:
597       offset = 2;
598       break;
599   }
600   return offset;
601 }
602 
calc_huff_bits(SHORT * in_data_1,SHORT * in_data_2,const DATA_TYPE data_type,const DIFF_TYPE diff_type_1,const DIFF_TYPE diff_type_2,const SHORT num_val,SHORT * const lav_idx,SHORT * const cdg_scheme)603 static SHORT calc_huff_bits(SHORT *in_data_1, SHORT *in_data_2,
604                             const DATA_TYPE data_type,
605                             const DIFF_TYPE diff_type_1,
606                             const DIFF_TYPE diff_type_2, const SHORT num_val,
607                             SHORT *const lav_idx, SHORT *const cdg_scheme) {
608   SHORT tab_idx_2D[2][2] = {{0}};
609   SHORT tab_idx_1D[2] = {0};
610   SHORT df_rest_flag[2] = {0};
611   SHORT p0_flag[2] = {0};
612 
613   SHORT pair_vec[MAXBANDS][2] = {{0}};
614 
615   SHORT *p0_data_1[2] = {NULL};
616   SHORT *p0_data_2[2] = {NULL};
617 
618   SHORT i = 0;
619   SHORT lav_fp[2] = {0};
620 
621   SHORT bit_count_1D = 0;
622   SHORT bit_count_2D_freq = 0;
623   SHORT bit_count_min = 0;
624 
625   SHORT num_val_1_short = 0;
626   SHORT num_val_2_short = 0;
627 
628   SHORT *in_data_1_short = NULL;
629   SHORT *in_data_2_short = NULL;
630 
631   /* 1D Huffman coding */
632   bit_count_1D = 1; /* HUFF_1D */
633 
634   num_val_1_short = num_val;
635   num_val_2_short = num_val;
636 
637   if (in_data_1 != NULL) {
638     in_data_1_short = in_data_1 + diff_type_offset(diff_type_1);
639   }
640   if (in_data_2 != NULL) {
641     in_data_2_short = in_data_2 + diff_type_offset(diff_type_2);
642   }
643 
644   p0_flag[0] = (diff_type_1 == DIFF_FREQ);
645   p0_flag[1] = (diff_type_2 == DIFF_FREQ);
646 
647   tab_idx_1D[0] = (diff_type_1 == DIFF_FREQ) ? 0 : 1;
648   tab_idx_1D[1] = (diff_type_2 == DIFF_FREQ) ? 0 : 1;
649 
650   if (in_data_1 != NULL) {
651     bit_count_1D += huff_enc_1D(NULL, data_type, tab_idx_1D[0], in_data_1_short,
652                                 num_val_1_short, p0_flag[0]);
653   }
654   if (in_data_2 != NULL) {
655     bit_count_1D += huff_enc_1D(NULL, data_type, tab_idx_1D[1], in_data_2_short,
656                                 num_val_2_short, p0_flag[1]);
657   }
658 
659   bit_count_min = bit_count_1D;
660   *cdg_scheme = HUFF_1D << PAIR_SHIFT;
661   lav_idx[0] = lav_idx[1] = -1;
662 
663   /* Huffman 2D frequency pairs */
664   bit_count_2D_freq = 1; /* HUFF_2D */
665 
666   num_val_1_short = num_val;
667   num_val_2_short = num_val;
668 
669   if (in_data_1 != NULL) {
670     in_data_1_short = in_data_1 + diff_type_offset(diff_type_1);
671   }
672   if (in_data_2 != NULL) {
673     in_data_2_short = in_data_2 + diff_type_offset(diff_type_2);
674   }
675 
676   lav_fp[0] = lav_fp[1] = 0;
677 
678   p0_data_1[0] = NULL;
679   p0_data_1[1] = NULL;
680   p0_data_2[0] = NULL;
681   p0_data_2[1] = NULL;
682 
683   if (in_data_1 != NULL) {
684     if (diff_type_1 == DIFF_FREQ) {
685       p0_data_1[0] = &in_data_1[0];
686       p0_data_1[1] = NULL;
687 
688       num_val_1_short -= 1;
689       in_data_1_short += 1;
690     }
691 
692     df_rest_flag[0] = num_val_1_short % 2;
693 
694     if (df_rest_flag[0]) num_val_1_short -= 1;
695 
696     for (i = 0; i < num_val_1_short - 1; i += 2) {
697       pair_vec[i][0] = in_data_1_short[i];
698       pair_vec[i][1] = in_data_1_short[i + 1];
699 
700       lav_fp[0] = FDKmax(lav_fp[0], fAbs(pair_vec[i][0]));
701       lav_fp[0] = FDKmax(lav_fp[0], fAbs(pair_vec[i][1]));
702     }
703 
704     tab_idx_2D[0][0] = (diff_type_1 == DIFF_TIME) ? 1 : 0;
705     tab_idx_2D[0][1] = 0;
706 
707     tab_idx_1D[0] = (diff_type_1 == DIFF_FREQ) ? 0 : 1;
708 
709     lav_fp[0] = get_next_lav_step(lav_fp[0], data_type);
710 
711     if (lav_fp[0] != -1) bit_count_2D_freq += lavHuffLen[lav_fp[0]];
712   }
713 
714   if (in_data_2 != NULL) {
715     if (diff_type_2 == DIFF_FREQ) {
716       p0_data_2[0] = NULL;
717       p0_data_2[1] = &in_data_2[0];
718 
719       num_val_2_short -= 1;
720       in_data_2_short += 1;
721     }
722 
723     df_rest_flag[1] = num_val_2_short % 2;
724 
725     if (df_rest_flag[1]) num_val_2_short -= 1;
726 
727     for (i = 0; i < num_val_2_short - 1; i += 2) {
728       pair_vec[i + 1][0] = in_data_2_short[i];
729       pair_vec[i + 1][1] = in_data_2_short[i + 1];
730 
731       lav_fp[1] = FDKmax(lav_fp[1], fAbs(pair_vec[i + 1][0]));
732       lav_fp[1] = FDKmax(lav_fp[1], fAbs(pair_vec[i + 1][1]));
733     }
734 
735     tab_idx_2D[1][0] = (diff_type_2 == DIFF_TIME) ? 1 : 0;
736     tab_idx_2D[1][1] = 0;
737 
738     tab_idx_1D[1] = (diff_type_2 == DIFF_FREQ) ? 0 : 1;
739 
740     lav_fp[1] = get_next_lav_step(lav_fp[1], data_type);
741 
742     if (lav_fp[1] != -1) bit_count_2D_freq += lavHuffLen[lav_fp[1]];
743   }
744 
745   if ((lav_fp[0] != -1) && (lav_fp[1] != -1)) {
746     if (in_data_1 != NULL) {
747       bit_count_2D_freq +=
748           huff_enc_2D(NULL, data_type, tab_idx_2D[0], lav_fp[0], pair_vec,
749                       num_val_1_short, 2, p0_data_1);
750     }
751     if (in_data_2 != NULL) {
752       bit_count_2D_freq +=
753           huff_enc_2D(NULL, data_type, tab_idx_2D[1], lav_fp[1], pair_vec + 1,
754                       num_val_2_short, 2, p0_data_2);
755     }
756     if (in_data_1 != NULL) {
757       if (df_rest_flag[0])
758         bit_count_2D_freq +=
759             huff_enc_1D(NULL, data_type, tab_idx_1D[0],
760                         in_data_1_short + num_val_1_short, 1, 0);
761     }
762     if (in_data_2 != NULL) {
763       if (df_rest_flag[1])
764         bit_count_2D_freq +=
765             huff_enc_1D(NULL, data_type, tab_idx_1D[1],
766                         in_data_2_short + num_val_2_short, 1, 0);
767     }
768 
769     if (bit_count_2D_freq < bit_count_min) {
770       bit_count_min = bit_count_2D_freq;
771       *cdg_scheme = HUFF_2D << PAIR_SHIFT | FREQ_PAIR;
772       lav_idx[0] = lav_fp[0];
773       lav_idx[1] = lav_fp[1];
774     }
775   }
776 
777   return bit_count_min;
778 }
779 
apply_huff_coding(HANDLE_FDK_BITSTREAM strm,SHORT * const in_data_1,SHORT * const in_data_2,const DATA_TYPE data_type,const DIFF_TYPE diff_type_1,const DIFF_TYPE diff_type_2,const SHORT num_val,const SHORT * const lav_idx,const SHORT cdg_scheme)780 static void apply_huff_coding(HANDLE_FDK_BITSTREAM strm, SHORT *const in_data_1,
781                               SHORT *const in_data_2, const DATA_TYPE data_type,
782                               const DIFF_TYPE diff_type_1,
783                               const DIFF_TYPE diff_type_2, const SHORT num_val,
784                               const SHORT *const lav_idx,
785                               const SHORT cdg_scheme) {
786   SHORT tab_idx_2D[2][2] = {{0}};
787   SHORT tab_idx_1D[2] = {0};
788   SHORT df_rest_flag[2] = {0};
789   SHORT p0_flag[2] = {0};
790 
791   SHORT pair_vec[MAXBANDS][2] = {{0}};
792 
793   SHORT *p0_data_1[2] = {NULL};
794   SHORT *p0_data_2[2] = {NULL};
795 
796   SHORT i = 0;
797 
798   SHORT num_val_1_short = num_val;
799   SHORT num_val_2_short = num_val;
800 
801   SHORT *in_data_1_short = NULL;
802   SHORT *in_data_2_short = NULL;
803 
804   /* Offset */
805   if (in_data_1 != NULL) {
806     in_data_1_short = in_data_1 + diff_type_offset(diff_type_1);
807   }
808   if (in_data_2 != NULL) {
809     in_data_2_short = in_data_2 + diff_type_offset(diff_type_2);
810   }
811 
812   /* Signalize coding scheme */
813   FDKwriteBits(strm, cdg_scheme >> PAIR_SHIFT, 1);
814 
815   switch (cdg_scheme >> PAIR_SHIFT) {
816     case HUFF_1D:
817 
818       p0_flag[0] = (diff_type_1 == DIFF_FREQ);
819       p0_flag[1] = (diff_type_2 == DIFF_FREQ);
820 
821       tab_idx_1D[0] = (diff_type_1 == DIFF_FREQ) ? 0 : 1;
822       tab_idx_1D[1] = (diff_type_2 == DIFF_FREQ) ? 0 : 1;
823 
824       if (in_data_1 != NULL) {
825         huff_enc_1D(strm, data_type, tab_idx_1D[0], in_data_1_short,
826                     num_val_1_short, p0_flag[0]);
827       }
828       if (in_data_2 != NULL) {
829         huff_enc_1D(strm, data_type, tab_idx_1D[1], in_data_2_short,
830                     num_val_2_short, p0_flag[1]);
831       }
832       break; /* HUFF_1D */
833 
834     case HUFF_2D:
835 
836       switch (cdg_scheme & PAIR_MASK) {
837         case FREQ_PAIR:
838 
839           if (in_data_1 != NULL) {
840             if (diff_type_1 == DIFF_FREQ) {
841               p0_data_1[0] = &in_data_1[0];
842               p0_data_1[1] = NULL;
843 
844               num_val_1_short -= 1;
845               in_data_1_short += 1;
846             }
847 
848             df_rest_flag[0] = num_val_1_short % 2;
849 
850             if (df_rest_flag[0]) num_val_1_short -= 1;
851 
852             for (i = 0; i < num_val_1_short - 1; i += 2) {
853               pair_vec[i][0] = in_data_1_short[i];
854               pair_vec[i][1] = in_data_1_short[i + 1];
855             }
856 
857             tab_idx_2D[0][0] = (diff_type_1 == DIFF_TIME) ? 1 : 0;
858             tab_idx_2D[0][1] = 0;
859 
860             tab_idx_1D[0] = (diff_type_1 == DIFF_FREQ) ? 0 : 1;
861           } /* if( in_data_1 != NULL ) */
862 
863           if (in_data_2 != NULL) {
864             if (diff_type_2 == DIFF_FREQ) {
865               p0_data_2[0] = NULL;
866               p0_data_2[1] = &in_data_2[0];
867 
868               num_val_2_short -= 1;
869               in_data_2_short += 1;
870             }
871 
872             df_rest_flag[1] = num_val_2_short % 2;
873 
874             if (df_rest_flag[1]) num_val_2_short -= 1;
875 
876             for (i = 0; i < num_val_2_short - 1; i += 2) {
877               pair_vec[i + 1][0] = in_data_2_short[i];
878               pair_vec[i + 1][1] = in_data_2_short[i + 1];
879             }
880 
881             tab_idx_2D[1][0] = (diff_type_2 == DIFF_TIME) ? 1 : 0;
882             tab_idx_2D[1][1] = 0;
883 
884             tab_idx_1D[1] = (diff_type_2 == DIFF_FREQ) ? 0 : 1;
885           } /* if( in_data_2 != NULL ) */
886 
887           if (in_data_1 != NULL) {
888             FDKwriteBits(strm, lavHuffVal[lav_idx[0]], lavHuffLen[lav_idx[0]]);
889             huff_enc_2D(strm, data_type, tab_idx_2D[0], lav_idx[0], pair_vec,
890                         num_val_1_short, 2, p0_data_1);
891             if (df_rest_flag[0]) {
892               huff_enc_1D(strm, data_type, tab_idx_1D[0],
893                           in_data_1_short + num_val_1_short, 1, 0);
894             }
895           }
896           if (in_data_2 != NULL) {
897             FDKwriteBits(strm, lavHuffVal[lav_idx[1]], lavHuffLen[lav_idx[1]]);
898             huff_enc_2D(strm, data_type, tab_idx_2D[1], lav_idx[1],
899                         pair_vec + 1, num_val_2_short, 2, p0_data_2);
900             if (df_rest_flag[1]) {
901               huff_enc_1D(strm, data_type, tab_idx_1D[1],
902                           in_data_2_short + num_val_2_short, 1, 0);
903             }
904           }
905           break; /* FREQ_PAIR */
906 
907         case TIME_PAIR:
908 
909           if ((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ)) {
910             p0_data_1[0] = &in_data_1[0];
911             p0_data_1[1] = &in_data_2[0];
912 
913             in_data_1_short += 1;
914             in_data_2_short += 1;
915 
916             num_val_1_short -= 1;
917           }
918 
919           for (i = 0; i < num_val_1_short; i++) {
920             pair_vec[i][0] = in_data_1_short[i];
921             pair_vec[i][1] = in_data_2_short[i];
922           }
923 
924           tab_idx_2D[0][0] =
925               ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) ? 1
926                                                                          : 0;
927           tab_idx_2D[0][1] = 1;
928 
929           FDKwriteBits(strm, lavHuffVal[lav_idx[0]], lavHuffLen[lav_idx[0]]);
930 
931           huff_enc_2D(strm, data_type, tab_idx_2D[0], lav_idx[0], pair_vec,
932                       num_val_1_short, 1, p0_data_1);
933 
934           break; /* TIME_PAIR */
935       }          /* switch( cdg_scheme & PAIR_MASK ) */
936 
937       break; /* HUFF_2D */
938 
939     default:
940       break;
941   } /* switch( cdg_scheme >> PAIR_SHIFT ) */
942 }
943 
fdk_sacenc_ecDataPairEnc(HANDLE_FDK_BITSTREAM strm,SHORT aaInData[][MAXBANDS],SHORT aHistory[MAXBANDS],const DATA_TYPE data_type,const INT setIdx,const INT startBand,const INT dataBands,const INT coarse_flag,const INT independency_flag)944 INT fdk_sacenc_ecDataPairEnc(HANDLE_FDK_BITSTREAM strm,
945                              SHORT aaInData[][MAXBANDS],
946                              SHORT aHistory[MAXBANDS],
947                              const DATA_TYPE data_type, const INT setIdx,
948                              const INT startBand, const INT dataBands,
949                              const INT coarse_flag,
950                              const INT independency_flag) {
951   SHORT reset = 0, pb = 0;
952   SHORT quant_levels = 0, quant_offset = 0, num_pcm_val = 0;
953 
954   SHORT splitLsb_flag = 0;
955   SHORT pcmCoding_flag = 0;
956 
957   SHORT allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
958 
959   SHORT num_lsb_bits = -1;
960   SHORT num_pcm_bits = -1;
961 
962   SHORT quant_data_lsb[2][MAXBANDS];
963   SHORT quant_data_msb[2][MAXBANDS];
964 
965   SHORT quant_data_hist_lsb[MAXBANDS];
966   SHORT quant_data_hist_msb[MAXBANDS];
967 
968   SHORT data_diff_freq[2][MAXBANDS];
969   SHORT data_diff_time[2][MAXBANDS + 2];
970 
971   SHORT *p_quant_data_msb[2];
972   SHORT *p_quant_data_hist_msb = NULL;
973 
974   SHORT min_bits_all = 0;
975   SHORT min_found = 0;
976 
977   SHORT min_bits_df_df = -1;
978   SHORT min_bits_df_dt = -1;
979   SHORT min_bits_dtbw_df = -1;
980   SHORT min_bits_dt_dt = -1;
981 
982   SHORT lav_df_df[2] = {-1, -1};
983   SHORT lav_df_dt[2] = {-1, -1};
984   SHORT lav_dtbw_df[2] = {-1, -1};
985   SHORT lav_dt_dt[2] = {-1, -1};
986 
987   SHORT coding_scheme_df_df = 0;
988   SHORT coding_scheme_df_dt = 0;
989   SHORT coding_scheme_dtbw_df = 0;
990   SHORT coding_scheme_dt_dt = 0;
991 
992   switch (data_type) {
993     case t_CLD:
994       if (coarse_flag) {
995         splitLsb_flag = 0;
996         quant_levels = 15;
997         quant_offset = 7;
998       } else {
999         splitLsb_flag = 0;
1000         quant_levels = 31;
1001         quant_offset = 15;
1002       }
1003       break;
1004     case t_ICC:
1005       if (coarse_flag) {
1006         splitLsb_flag = 0;
1007         quant_levels = 4;
1008         quant_offset = 0;
1009       } else {
1010         splitLsb_flag = 0;
1011         quant_levels = 8;
1012         quant_offset = 0;
1013       }
1014       break;
1015   } /* switch( data_type ) */
1016 
1017   /* Split off LSB */
1018   if (splitLsb_flag) {
1019     split_lsb(aaInData[setIdx] + startBand, quant_offset, dataBands,
1020               quant_data_lsb[0], quant_data_msb[0]);
1021 
1022     split_lsb(aaInData[setIdx + 1] + startBand, quant_offset, dataBands,
1023               quant_data_lsb[1], quant_data_msb[1]);
1024 
1025     p_quant_data_msb[0] = quant_data_msb[0];
1026     p_quant_data_msb[1] = quant_data_msb[1];
1027 
1028     num_lsb_bits = 2 * dataBands;
1029   } else if (quant_offset != 0) {
1030     for (pb = 0; pb < dataBands; pb++) {
1031       quant_data_msb[0][pb] = aaInData[setIdx][startBand + pb] + quant_offset;
1032       quant_data_msb[1][pb] =
1033           aaInData[setIdx + 1][startBand + pb] + quant_offset;
1034     }
1035 
1036     p_quant_data_msb[0] = quant_data_msb[0];
1037     p_quant_data_msb[1] = quant_data_msb[1];
1038 
1039     num_lsb_bits = 0;
1040   } else {
1041     p_quant_data_msb[0] = aaInData[setIdx] + startBand;
1042     p_quant_data_msb[1] = aaInData[setIdx + 1] + startBand;
1043 
1044     num_lsb_bits = 0;
1045   }
1046 
1047   if (allowDiffTimeBack_flag) {
1048     if (splitLsb_flag) {
1049       split_lsb(aHistory + startBand, quant_offset, dataBands,
1050                 quant_data_hist_lsb, quant_data_hist_msb);
1051 
1052       p_quant_data_hist_msb = quant_data_hist_msb;
1053     } else if (quant_offset != 0) {
1054       for (pb = 0; pb < dataBands; pb++) {
1055         quant_data_hist_msb[pb] = aHistory[startBand + pb] + quant_offset;
1056       }
1057       p_quant_data_hist_msb = quant_data_hist_msb;
1058     } else {
1059       p_quant_data_hist_msb = aHistory + startBand;
1060     }
1061   }
1062 
1063   /* Calculate frequency differences */
1064   calc_diff_freq(p_quant_data_msb[0], data_diff_freq[0], dataBands);
1065 
1066   calc_diff_freq(p_quant_data_msb[1], data_diff_freq[1], dataBands);
1067 
1068   /* Calculate time differences */
1069   if (allowDiffTimeBack_flag) {
1070     calc_diff_time(p_quant_data_msb[0], p_quant_data_hist_msb,
1071                    data_diff_time[0], dataBands);
1072   }
1073 
1074   calc_diff_time(p_quant_data_msb[1], p_quant_data_msb[0], data_diff_time[1],
1075                  dataBands);
1076 
1077   /* Calculate coding scheme with minumum bit consumption */
1078 
1079   /**********************************************************/
1080   num_pcm_bits = calc_pcm_bits(2 * dataBands, quant_levels);
1081   num_pcm_val = 2 * dataBands;
1082 
1083   /**********************************************************/
1084 
1085   min_bits_all = num_pcm_bits;
1086 
1087   /**********************************************************/
1088   /**********************************************************/
1089 
1090   /**********************************************************/
1091   min_bits_df_df =
1092       calc_huff_bits(data_diff_freq[0], data_diff_freq[1], data_type, DIFF_FREQ,
1093                      DIFF_FREQ, dataBands, lav_df_df, &coding_scheme_df_df);
1094 
1095   min_bits_df_df += 2;
1096 
1097   min_bits_df_df += num_lsb_bits;
1098 
1099   if (min_bits_df_df < min_bits_all) {
1100     min_bits_all = min_bits_df_df;
1101   }
1102   /**********************************************************/
1103 
1104   /**********************************************************/
1105   min_bits_df_dt =
1106       calc_huff_bits(data_diff_freq[0], data_diff_time[1], data_type, DIFF_FREQ,
1107                      DIFF_TIME, dataBands, lav_df_dt, &coding_scheme_df_dt);
1108 
1109   min_bits_df_dt += 2;
1110 
1111   min_bits_df_dt += num_lsb_bits;
1112 
1113   if (min_bits_df_dt < min_bits_all) {
1114     min_bits_all = min_bits_df_dt;
1115   }
1116   /**********************************************************/
1117 
1118   /**********************************************************/
1119   /**********************************************************/
1120 
1121   if (allowDiffTimeBack_flag) {
1122     /**********************************************************/
1123     min_bits_dtbw_df = calc_huff_bits(
1124         data_diff_time[0], data_diff_freq[1], data_type, DIFF_TIME, DIFF_FREQ,
1125         dataBands, lav_dtbw_df, &coding_scheme_dtbw_df);
1126 
1127     min_bits_dtbw_df += 2;
1128 
1129     min_bits_dtbw_df += num_lsb_bits;
1130 
1131     if (min_bits_dtbw_df < min_bits_all) {
1132       min_bits_all = min_bits_dtbw_df;
1133     }
1134     /**********************************************************/
1135 
1136     /**********************************************************/
1137     min_bits_dt_dt = calc_huff_bits(data_diff_time[0], data_diff_time[1],
1138                                     data_type, DIFF_TIME, DIFF_TIME, dataBands,
1139                                     lav_dt_dt, &coding_scheme_dt_dt);
1140 
1141     min_bits_dt_dt += 2;
1142 
1143     min_bits_dt_dt += num_lsb_bits;
1144 
1145     if (min_bits_dt_dt < min_bits_all) {
1146       min_bits_all = min_bits_dt_dt;
1147     }
1148     /**********************************************************/
1149 
1150   } /* if( allowDiffTimeBack_flag ) */
1151 
1152   /***************************/
1153   /* Start actual coding now */
1154   /***************************/
1155 
1156   /* PCM or Diff/Huff Coding? */
1157   pcmCoding_flag = (min_bits_all == num_pcm_bits);
1158 
1159   FDKwriteBits(strm, pcmCoding_flag, 1);
1160 
1161   if (pcmCoding_flag) {
1162     /* Grouped PCM Coding */
1163     apply_pcm_coding(strm, aaInData[setIdx] + startBand,
1164                      aaInData[setIdx + 1] + startBand, quant_offset,
1165                      num_pcm_val, quant_levels);
1166   } else {
1167     /* Diff/Huff Coding */
1168 
1169     min_found = 0;
1170 
1171     /*******************************************/
1172     if (min_bits_all == min_bits_df_df) {
1173       FDKwriteBits(strm, DIFF_FREQ, 1);
1174       FDKwriteBits(strm, DIFF_FREQ, 1);
1175 
1176       apply_huff_coding(strm, data_diff_freq[0], data_diff_freq[1], data_type,
1177                         DIFF_FREQ, DIFF_FREQ, dataBands, lav_df_df,
1178                         coding_scheme_df_df);
1179 
1180       min_found = 1;
1181     }
1182     /*******************************************/
1183 
1184     /*******************************************/
1185     if (!min_found && (min_bits_all == min_bits_df_dt)) {
1186       FDKwriteBits(strm, DIFF_FREQ, 1);
1187       FDKwriteBits(strm, DIFF_TIME, 1);
1188 
1189       apply_huff_coding(strm, data_diff_freq[0], data_diff_time[1], data_type,
1190                         DIFF_FREQ, DIFF_TIME, dataBands, lav_df_dt,
1191                         coding_scheme_df_dt);
1192 
1193       min_found = 1;
1194     }
1195     /*******************************************/
1196 
1197     /*******************************************/
1198     /*******************************************/
1199 
1200     if (allowDiffTimeBack_flag) {
1201       /*******************************************/
1202       if (!min_found && (min_bits_all == min_bits_dtbw_df)) {
1203         FDKwriteBits(strm, DIFF_TIME, 1);
1204         FDKwriteBits(strm, DIFF_FREQ, 1);
1205 
1206         apply_huff_coding(strm, data_diff_time[0], data_diff_freq[1], data_type,
1207                           DIFF_TIME, DIFF_FREQ, dataBands, lav_dtbw_df,
1208                           coding_scheme_dtbw_df);
1209 
1210         min_found = 1;
1211       }
1212       /*******************************************/
1213 
1214       /*******************************************/
1215       if (!min_found && (min_bits_all == min_bits_dt_dt)) {
1216         FDKwriteBits(strm, DIFF_TIME, 1);
1217         FDKwriteBits(strm, DIFF_TIME, 1);
1218 
1219         apply_huff_coding(strm, data_diff_time[0], data_diff_time[1], data_type,
1220                           DIFF_TIME, DIFF_TIME, dataBands, lav_dt_dt,
1221                           coding_scheme_dt_dt);
1222       }
1223       /*******************************************/
1224 
1225     } /* if( allowDiffTimeBack_flag ) */
1226 
1227     /* LSB coding */
1228     if (splitLsb_flag) {
1229       apply_lsb_coding(strm, quant_data_lsb[0], 1, dataBands);
1230 
1231       apply_lsb_coding(strm, quant_data_lsb[1], 1, dataBands);
1232     }
1233 
1234   } /* Diff/Huff/LSB coding */
1235 
1236   return reset;
1237 }
1238 
fdk_sacenc_ecDataSingleEnc(HANDLE_FDK_BITSTREAM strm,SHORT aaInData[][MAXBANDS],SHORT aHistory[MAXBANDS],const DATA_TYPE data_type,const INT setIdx,const INT startBand,const INT dataBands,const INT coarse_flag,const INT independency_flag)1239 INT fdk_sacenc_ecDataSingleEnc(HANDLE_FDK_BITSTREAM strm,
1240                                SHORT aaInData[][MAXBANDS],
1241                                SHORT aHistory[MAXBANDS],
1242                                const DATA_TYPE data_type, const INT setIdx,
1243                                const INT startBand, const INT dataBands,
1244                                const INT coarse_flag,
1245                                const INT independency_flag) {
1246   SHORT reset = 0, pb = 0;
1247   SHORT quant_levels = 0, quant_offset = 0, num_pcm_val = 0;
1248 
1249   SHORT splitLsb_flag = 0;
1250   SHORT pcmCoding_flag = 0;
1251 
1252   SHORT allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
1253 
1254   SHORT num_lsb_bits = -1;
1255   SHORT num_pcm_bits = -1;
1256 
1257   SHORT quant_data_lsb[MAXBANDS];
1258   SHORT quant_data_msb[MAXBANDS];
1259 
1260   SHORT quant_data_hist_lsb[MAXBANDS];
1261   SHORT quant_data_hist_msb[MAXBANDS];
1262 
1263   SHORT data_diff_freq[MAXBANDS];
1264   SHORT data_diff_time[MAXBANDS + 2];
1265 
1266   SHORT *p_quant_data_msb;
1267   SHORT *p_quant_data_hist_msb = NULL;
1268 
1269   SHORT min_bits_all = 0;
1270   SHORT min_found = 0;
1271 
1272   SHORT min_bits_df = -1;
1273   SHORT min_bits_dt = -1;
1274 
1275   SHORT lav_df[2] = {-1, -1};
1276   SHORT lav_dt[2] = {-1, -1};
1277 
1278   SHORT coding_scheme_df = 0;
1279   SHORT coding_scheme_dt = 0;
1280 
1281   switch (data_type) {
1282     case t_CLD:
1283       if (coarse_flag) {
1284         splitLsb_flag = 0;
1285         quant_levels = 15;
1286         quant_offset = 7;
1287       } else {
1288         splitLsb_flag = 0;
1289         quant_levels = 31;
1290         quant_offset = 15;
1291       }
1292       break;
1293     case t_ICC:
1294       if (coarse_flag) {
1295         splitLsb_flag = 0;
1296         quant_levels = 4;
1297         quant_offset = 0;
1298       } else {
1299         splitLsb_flag = 0;
1300         quant_levels = 8;
1301         quant_offset = 0;
1302       }
1303       break;
1304   } /* switch( data_type ) */
1305 
1306   /* Split off LSB */
1307   if (splitLsb_flag) {
1308     split_lsb(aaInData[setIdx] + startBand, quant_offset, dataBands,
1309               quant_data_lsb, quant_data_msb);
1310 
1311     p_quant_data_msb = quant_data_msb;
1312     num_lsb_bits = dataBands;
1313   } else if (quant_offset != 0) {
1314     for (pb = 0; pb < dataBands; pb++) {
1315       quant_data_msb[pb] = aaInData[setIdx][startBand + pb] + quant_offset;
1316     }
1317 
1318     p_quant_data_msb = quant_data_msb;
1319     num_lsb_bits = 0;
1320   } else {
1321     p_quant_data_msb = aaInData[setIdx] + startBand;
1322     num_lsb_bits = 0;
1323   }
1324 
1325   if (allowDiffTimeBack_flag) {
1326     if (splitLsb_flag) {
1327       split_lsb(aHistory + startBand, quant_offset, dataBands,
1328                 quant_data_hist_lsb, quant_data_hist_msb);
1329 
1330       p_quant_data_hist_msb = quant_data_hist_msb;
1331     } else if (quant_offset != 0) {
1332       for (pb = 0; pb < dataBands; pb++) {
1333         quant_data_hist_msb[pb] = aHistory[startBand + pb] + quant_offset;
1334       }
1335       p_quant_data_hist_msb = quant_data_hist_msb;
1336     } else {
1337       p_quant_data_hist_msb = aHistory + startBand;
1338     }
1339   }
1340 
1341   /* Calculate frequency differences */
1342   calc_diff_freq(p_quant_data_msb, data_diff_freq, dataBands);
1343 
1344   /* Calculate time differences */
1345   if (allowDiffTimeBack_flag) {
1346     calc_diff_time(p_quant_data_msb, p_quant_data_hist_msb, data_diff_time,
1347                    dataBands);
1348   }
1349 
1350   /* Calculate coding scheme with minumum bit consumption */
1351 
1352   /**********************************************************/
1353   num_pcm_bits = calc_pcm_bits(dataBands, quant_levels);
1354   num_pcm_val = dataBands;
1355 
1356   /**********************************************************/
1357 
1358   min_bits_all = num_pcm_bits;
1359 
1360   /**********************************************************/
1361   /**********************************************************/
1362 
1363   /**********************************************************/
1364   min_bits_df = calc_huff_bits(data_diff_freq, NULL, data_type, DIFF_FREQ,
1365                                DIFF_FREQ, dataBands, lav_df, &coding_scheme_df);
1366 
1367   if (allowDiffTimeBack_flag) min_bits_df += 1;
1368 
1369   min_bits_df += num_lsb_bits;
1370 
1371   if (min_bits_df < min_bits_all) {
1372     min_bits_all = min_bits_df;
1373   }
1374   /**********************************************************/
1375 
1376   /**********************************************************/
1377   if (allowDiffTimeBack_flag) {
1378     min_bits_dt =
1379         calc_huff_bits(data_diff_time, NULL, data_type, DIFF_TIME, DIFF_TIME,
1380                        dataBands, lav_dt, &coding_scheme_dt);
1381 
1382     min_bits_dt += 1;
1383     min_bits_dt += num_lsb_bits;
1384 
1385     if (min_bits_dt < min_bits_all) {
1386       min_bits_all = min_bits_dt;
1387     }
1388   } /* if( allowDiffTimeBack_flag ) */
1389 
1390   /***************************/
1391   /* Start actual coding now */
1392   /***************************/
1393 
1394   /* PCM or Diff/Huff Coding? */
1395   pcmCoding_flag = (min_bits_all == num_pcm_bits);
1396 
1397   FDKwriteBits(strm, pcmCoding_flag, 1);
1398 
1399   if (pcmCoding_flag) {
1400     /* Grouped PCM Coding */
1401     apply_pcm_coding(strm, aaInData[setIdx] + startBand, NULL, quant_offset,
1402                      num_pcm_val, quant_levels);
1403   } else {
1404     /* Diff/Huff Coding */
1405 
1406     min_found = 0;
1407 
1408     /*******************************************/
1409     if (min_bits_all == min_bits_df) {
1410       if (allowDiffTimeBack_flag) {
1411         FDKwriteBits(strm, DIFF_FREQ, 1);
1412       }
1413 
1414       apply_huff_coding(strm, data_diff_freq, NULL, data_type, DIFF_FREQ,
1415                         DIFF_FREQ, dataBands, lav_df, coding_scheme_df);
1416 
1417       min_found = 1;
1418     } /* if( min_bits_all == min_bits_df ) */
1419     /*******************************************/
1420 
1421     /*******************************************/
1422     if (allowDiffTimeBack_flag) {
1423       /*******************************************/
1424       if (!min_found && (min_bits_all == min_bits_dt)) {
1425         FDKwriteBits(strm, DIFF_TIME, 1);
1426 
1427         apply_huff_coding(strm, data_diff_time, NULL, data_type, DIFF_TIME,
1428                           DIFF_TIME, dataBands, lav_dt, coding_scheme_dt);
1429       }
1430       /*******************************************/
1431 
1432     } /* if( allowDiffTimeBack_flag ) */
1433 
1434     /* LSB coding */
1435     if (splitLsb_flag) {
1436       apply_lsb_coding(strm, quant_data_lsb, 1, dataBands);
1437     }
1438 
1439   } /* Diff/Huff/LSB coding */
1440 
1441   return reset;
1442 }
1443