1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2020 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 /******************* Library for basic calculation routines ********************
96 
97    Author(s):   Omer Osman
98 
99    Description: SAC/SAOC Dec Noiseless Coding
100 
101 *******************************************************************************/
102 
103 #include "nlc_dec.h"
104 #include "FDK_tools_rom.h"
105 
106 /* MAX_PARAMETER_BANDS defines array length in huffdec */
107 
108 #ifndef min
109 #define min(a, b) (((a) < (b)) ? (a) : (b))
110 #endif
111 
sym_restoreIPD(HANDLE_FDK_BITSTREAM strm,int lav,SCHAR data[2])112 ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
113   int sum_val = data[0] + data[1];
114   int diff_val = data[0] - data[1];
115 
116   if (sum_val > lav) {
117     data[0] = -sum_val + (2 * lav + 1);
118     data[1] = -diff_val;
119   } else {
120     data[0] = sum_val;
121     data[1] = diff_val;
122   }
123 
124   if (data[0] - data[1] != 0) {
125     ULONG sym_bit;
126     sym_bit = FDKreadBits(strm, 1);
127     if (sym_bit) {
128       int tmp;
129       tmp = data[0];
130       data[0] = data[1];
131       data[1] = tmp;
132     }
133   }
134 
135   return HUFFDEC_OK;
136 }
137 
ilog2(unsigned int i)138 static int ilog2(unsigned int i) {
139   int l = 0;
140 
141   if (i) i--;
142   while (i > 0) {
143     i >>= 1;
144     l++;
145   }
146 
147   return l;
148 }
149 
pcm_decode(HANDLE_FDK_BITSTREAM strm,SCHAR * out_data_1,SCHAR * out_data_2,int offset,int num_val,int num_levels)150 static ERROR_t pcm_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1,
151                           SCHAR* out_data_2, int offset, int num_val,
152                           int num_levels) {
153   int i = 0, j = 0, idx = 0;
154   int max_grp_len = 0, next_val = 0;
155   ULONG tmp;
156 
157   int pcm_chunk_size[7] = {0};
158 
159   switch (num_levels) {
160     case 3:
161       max_grp_len = 5;
162       break;
163     case 7:
164       max_grp_len = 6;
165       break;
166     case 11:
167       max_grp_len = 2;
168       break;
169     case 13:
170       max_grp_len = 4;
171       break;
172     case 19:
173       max_grp_len = 4;
174       break;
175     case 25:
176       max_grp_len = 3;
177       break;
178     case 51:
179       max_grp_len = 4;
180       break;
181     case 4:
182     case 8:
183     case 15:
184     case 16:
185     case 26:
186     case 31:
187       max_grp_len = 1;
188       break;
189     default:
190       return HUFFDEC_NOTOK;
191   }
192 
193   tmp = 1;
194   for (i = 1; i <= max_grp_len; i++) {
195     tmp *= num_levels;
196     pcm_chunk_size[i] = ilog2(tmp);
197   }
198 
199   for (i = 0; i < num_val; i += max_grp_len) {
200     int grp_len, grp_val, data;
201     grp_len = min(max_grp_len, num_val - i);
202     data = FDKreadBits(strm, pcm_chunk_size[grp_len]);
203 
204     grp_val = data;
205 
206     for (j = 0; j < grp_len; j++) {
207       idx = i + (grp_len - j - 1);
208       next_val = grp_val % num_levels;
209 
210       if (out_data_2 == NULL) {
211         out_data_1[idx] = next_val - offset;
212       } else if (out_data_1 == NULL) {
213         out_data_2[idx] = next_val - offset;
214       } else {
215         if (idx % 2) {
216           out_data_2[idx / 2] = next_val - offset;
217         } else {
218           out_data_1[idx / 2] = next_val - offset;
219         }
220       }
221 
222       grp_val = (grp_val - next_val) / num_levels;
223     }
224   }
225 
226   return HUFFDEC_OK;
227 }
228 
huff_read(HANDLE_FDK_BITSTREAM strm,const SHORT (* nodeTab)[MAX_ENTRIES][2],int * out_data)229 static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm,
230                          const SHORT (*nodeTab)[MAX_ENTRIES][2],
231                          int* out_data) {
232   int node = 0;
233   int len = 0;
234 
235   do {
236     ULONG next_bit;
237     next_bit = FDKreadBits(strm, 1);
238     len++;
239     node = (*nodeTab)[node][next_bit];
240   } while (node > 0);
241 
242   *out_data = node;
243 
244   return HUFFDEC_OK;
245 }
246 
huff_read_2D(HANDLE_FDK_BITSTREAM strm,const SHORT (* nodeTab)[MAX_ENTRIES][2],SCHAR out_data[2],int * escape)247 static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm,
248                             const SHORT (*nodeTab)[MAX_ENTRIES][2],
249                             SCHAR out_data[2], int* escape) {
250   ERROR_t err = HUFFDEC_OK;
251 
252   int huff_2D_8bit = 0;
253   int node = 0;
254 
255   if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
256     goto bail;
257   }
258   *escape = (node == 0);
259 
260   if (*escape) {
261     out_data[0] = 0;
262     out_data[1] = 1;
263   } else {
264     huff_2D_8bit = -(node + 1);
265     out_data[0] = huff_2D_8bit >> 4;
266     out_data[1] = huff_2D_8bit & 0xf;
267   }
268 
269 bail:
270   return err;
271 }
272 
sym_restore(HANDLE_FDK_BITSTREAM strm,int lav,SCHAR data[2])273 static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
274   ULONG sym_bit = 0;
275 
276   int sum_val = data[0] + data[1];
277   int diff_val = data[0] - data[1];
278 
279   if (sum_val > lav) {
280     data[0] = -sum_val + (2 * lav + 1);
281     data[1] = -diff_val;
282   } else {
283     data[0] = sum_val;
284     data[1] = diff_val;
285   }
286 
287   if (data[0] + data[1] != 0) {
288     sym_bit = FDKreadBits(strm, 1);
289     if (sym_bit) {
290       data[0] = -data[0];
291       data[1] = -data[1];
292     }
293   }
294 
295   if (data[0] - data[1] != 0) {
296     sym_bit = FDKreadBits(strm, 1);
297     if (sym_bit) {
298       int tmp;
299       tmp = data[0];
300       data[0] = data[1];
301       data[1] = tmp;
302     }
303   }
304 
305   return HUFFDEC_OK;
306 }
307 
huff_dec_1D(HANDLE_FDK_BITSTREAM strm,const DATA_TYPE data_type,const INT dim1,SCHAR * out_data,const INT num_val,const INT p0_flag)308 static ERROR_t huff_dec_1D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
309                            const INT dim1, SCHAR* out_data, const INT num_val,
310                            const INT p0_flag)
311 
312 {
313   ERROR_t err = HUFFDEC_OK;
314   int i = 0, node = 0, offset = 0;
315   int od = 0, od_sign = 0;
316   ULONG data = 0;
317   int bitsAvail = 0;
318 
319   const SHORT(*partTab)[MAX_ENTRIES][2] = NULL;
320   const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
321 
322   switch (data_type) {
323     case t_CLD:
324       partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
325       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0];
326       break;
327     case t_ICC:
328       partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
329       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0];
330       break;
331     case t_OLD:
332       partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0];
333       nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h1D[dim1]->nodeTab[0][0];
334       break;
335     case t_IPD:
336       partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
337       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0];
338       break;
339     default:
340       FDK_ASSERT(0);
341       err = HUFFDEC_NOTOK;
342       goto bail;
343   }
344 
345   if (p0_flag) {
346     if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) {
347       goto bail;
348     }
349 
350     out_data[0] = -(node + 1);
351     offset = 1;
352   }
353 
354   for (i = offset; i < num_val; i++) {
355     bitsAvail = FDKgetValidBits(strm);
356     if (bitsAvail < 1) {
357       err = HUFFDEC_NOTOK;
358       goto bail;
359     }
360 
361     if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
362       goto bail;
363     }
364     od = -(node + 1);
365 
366     if (data_type != t_IPD) {
367       if (od != 0) {
368         bitsAvail = FDKgetValidBits(strm);
369         if (bitsAvail < 1) {
370           err = HUFFDEC_NOTOK;
371           goto bail;
372         }
373 
374         data = FDKreadBits(strm, 1);
375         od_sign = data;
376 
377         if (od_sign) od = -od;
378       }
379     }
380 
381     out_data[i] = od;
382   }
383 
384 bail:
385   return err;
386 }
387 
huff_dec_2D(HANDLE_FDK_BITSTREAM strm,const DATA_TYPE data_type,const INT dim1,const INT dim2,SCHAR out_data[][2],const INT num_val,const INT stride,SCHAR * p0_data[2])388 static ERROR_t huff_dec_2D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
389                            const INT dim1, const INT dim2, SCHAR out_data[][2],
390                            const INT num_val, const INT stride,
391                            SCHAR* p0_data[2]) {
392   ERROR_t err = HUFFDEC_OK;
393   int i = 0, lav = 0, escape = 0, escCntr = 0;
394   int node = 0;
395   unsigned long data = 0;
396 
397   SCHAR esc_data[2][28] = {{0}};
398   int escIdx[28] = {0};
399   const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
400 
401   /* LAV */
402   if ((err =
403            huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0],
404                      &node)) != HUFFDEC_OK) {
405     goto bail;
406   }
407   data = -(node + 1);
408 
409   switch (data_type) {
410     case t_CLD:
411       lav = 2 * data + 3; /* 3, 5, 7, 9 */
412       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
413       break;
414     case t_ICC:
415       lav = 2 * data + 1; /* 1, 3, 5, 7 */
416       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
417       break;
418     case t_OLD:
419       lav = 3 * data + 3;
420       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0];
421       break;
422     case t_IPD:
423       if (data == 0)
424         data = 3;
425       else
426         data--;
427       lav = 2 * data + 1; /* 1, 3, 5, 7 */
428       nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
429       break;
430     default:
431       FDK_ASSERT(0);
432       err = HUFFDEC_NOTOK;
433       goto bail;
434   }
435 
436   /* Partition 0 */
437   if (p0_data[0] != NULL) {
438     if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
439       goto bail;
440     }
441     *p0_data[0] = -(node + 1);
442   }
443   if (p0_data[1] != NULL) {
444     if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
445       goto bail;
446     }
447     *p0_data[1] = -(node + 1);
448   }
449 
450   switch (data_type) {
451     case t_CLD:
452       switch (lav) {
453         case 3:
454           nodeTab =
455               (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0];
456           break;
457         case 5:
458           nodeTab =
459               (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0];
460           break;
461         case 7:
462           nodeTab =
463               (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0];
464           break;
465         case 9:
466           nodeTab =
467               (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0];
468           break;
469       }
470       break;
471     case t_ICC:
472       switch (lav) {
473         case 1:
474           nodeTab =
475               (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0];
476           break;
477         case 3:
478           nodeTab =
479               (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0];
480           break;
481         case 5:
482           nodeTab =
483               (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0];
484           break;
485         case 7:
486           nodeTab =
487               (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0];
488           break;
489       }
490       break;
491     case t_OLD:
492       switch (lav) {
493         case 3:
494           nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav3[0][0];
495           break;
496         case 6:
497           nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav6[0][0];
498           break;
499         case 9:
500           nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav9[0][0];
501           break;
502         case 12:
503           nodeTab =
504               (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav12[0][0];
505           break;
506       }
507       break;
508     case t_IPD:
509       switch (lav) {
510         case 1:
511           nodeTab =
512               (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav1[0][0];
513           break;
514         case 3:
515           nodeTab =
516               (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0];
517           break;
518         case 5:
519           nodeTab =
520               (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0];
521           break;
522         case 7:
523           nodeTab =
524               (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0];
525           break;
526       }
527       break;
528     default:
529       break;
530   }
531 
532   for (i = 0; i < num_val; i += stride) {
533     if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) !=
534         HUFFDEC_OK) {
535       goto bail;
536     }
537 
538     if (escape) {
539       escIdx[escCntr++] = i;
540     } else {
541       if (data_type == t_IPD) {
542         if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) {
543           goto bail;
544         }
545       } else {
546         if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) {
547           goto bail;
548         }
549       }
550     }
551   } /* i */
552 
553   if (escCntr > 0) {
554     if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr,
555                           (2 * lav + 1))) != HUFFDEC_OK) {
556       goto bail;
557     }
558 
559     for (i = 0; i < escCntr; i++) {
560       out_data[escIdx[i]][0] = esc_data[0][i] - lav;
561       out_data[escIdx[i]][1] = esc_data[1][i] - lav;
562     }
563   }
564 bail:
565   return err;
566 }
567 
huff_decode(HANDLE_FDK_BITSTREAM strm,SCHAR * out_data_1,SCHAR * out_data_2,DATA_TYPE data_type,DIFF_TYPE diff_type_1,DIFF_TYPE diff_type_2,int num_val,PAIRING * pairing_scheme,int ldMode)568 static ERROR_t huff_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1,
569                            SCHAR* out_data_2, DATA_TYPE data_type,
570                            DIFF_TYPE diff_type_1, DIFF_TYPE diff_type_2,
571                            int num_val, PAIRING* pairing_scheme, int ldMode) {
572   ERROR_t err = HUFFDEC_OK;
573   CODING_SCHEME coding_scheme = HUFF_1D;
574   DIFF_TYPE diff_type;
575 
576   int i = 0;
577 
578   SCHAR pair_vec[28][2];
579 
580   SCHAR* p0_data_1[2] = {NULL, NULL};
581   SCHAR* p0_data_2[2] = {NULL, NULL};
582 
583   int p0_flag[2];
584 
585   int num_val_1_int = num_val;
586   int num_val_2_int = num_val;
587 
588   SCHAR* out_data_1_int = out_data_1;
589   SCHAR* out_data_2_int = out_data_2;
590 
591   int df_rest_flag_1 = 0;
592   int df_rest_flag_2 = 0;
593 
594   int hufYY1;
595   int hufYY2;
596   int hufYY;
597 
598   /* Coding scheme */
599   coding_scheme = (CODING_SCHEME)FDKreadBits(strm, 1);
600 
601   if (coding_scheme == HUFF_2D) {
602     if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) {
603       *pairing_scheme = (PAIRING)FDKreadBits(strm, 1);
604     } else {
605       *pairing_scheme = FREQ_PAIR;
606     }
607   }
608 
609   {
610     hufYY1 = diff_type_1;
611     hufYY2 = diff_type_2;
612   }
613 
614   switch (coding_scheme) {
615     case HUFF_1D:
616       p0_flag[0] = (diff_type_1 == DIFF_FREQ);
617       p0_flag[1] = (diff_type_2 == DIFF_FREQ);
618       if (out_data_1 != NULL) {
619         if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1,
620                                num_val_1_int, p0_flag[0])) != HUFFDEC_OK) {
621           goto bail;
622         }
623       }
624       if (out_data_2 != NULL) {
625         if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2,
626                                num_val_2_int, p0_flag[1])) != HUFFDEC_OK) {
627           goto bail;
628         }
629       }
630 
631       break; /* HUFF_1D */
632 
633     case HUFF_2D:
634 
635       switch (*pairing_scheme) {
636         case FREQ_PAIR:
637 
638           if (out_data_1 != NULL) {
639             if (diff_type_1 == DIFF_FREQ) {
640               p0_data_1[0] = &out_data_1[0];
641               p0_data_1[1] = NULL;
642 
643               num_val_1_int -= 1;
644               out_data_1_int += 1;
645             }
646             df_rest_flag_1 = num_val_1_int % 2;
647             if (df_rest_flag_1) num_val_1_int -= 1;
648             if (num_val_1_int < 0) {
649               err = HUFFDEC_NOTOK;
650               goto bail;
651             }
652           }
653           if (out_data_2 != NULL) {
654             if (diff_type_2 == DIFF_FREQ) {
655               p0_data_2[0] = NULL;
656               p0_data_2[1] = &out_data_2[0];
657 
658               num_val_2_int -= 1;
659               out_data_2_int += 1;
660             }
661             df_rest_flag_2 = num_val_2_int % 2;
662             if (df_rest_flag_2) num_val_2_int -= 1;
663             if (num_val_2_int < 0) {
664               err = HUFFDEC_NOTOK;
665               goto bail;
666             }
667           }
668 
669           if (out_data_1 != NULL) {
670             if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec,
671                                    num_val_1_int, 2, p0_data_1)) !=
672                 HUFFDEC_OK) {
673               goto bail;
674             }
675             if (df_rest_flag_1) {
676               if ((err = huff_dec_1D(strm, data_type, hufYY1,
677                                      out_data_1_int + num_val_1_int, 1, 0)) !=
678                   HUFFDEC_OK) {
679                 goto bail;
680               }
681             }
682           }
683           if (out_data_2 != NULL) {
684             if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR,
685                                    pair_vec + 1, num_val_2_int, 2,
686                                    p0_data_2)) != HUFFDEC_OK) {
687               goto bail;
688             }
689             if (df_rest_flag_2) {
690               if ((err = huff_dec_1D(strm, data_type, hufYY2,
691                                      out_data_2_int + num_val_2_int, 1, 0)) !=
692                   HUFFDEC_OK) {
693                 goto bail;
694               }
695             }
696           }
697 
698           if (out_data_1 != NULL) {
699             for (i = 0; i < num_val_1_int - 1; i += 2) {
700               out_data_1_int[i] = pair_vec[i][0];
701               out_data_1_int[i + 1] = pair_vec[i][1];
702             }
703           }
704           if (out_data_2 != NULL) {
705             for (i = 0; i < num_val_2_int - 1; i += 2) {
706               out_data_2_int[i] = pair_vec[i + 1][0];
707               out_data_2_int[i + 1] = pair_vec[i + 1][1];
708             }
709           }
710           break; /* FREQ_PAIR */
711 
712         case TIME_PAIR:
713           if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
714             p0_data_1[0] = &out_data_1[0];
715             p0_data_1[1] = &out_data_2[0];
716 
717             out_data_1_int += 1;
718             out_data_2_int += 1;
719 
720             num_val_1_int -= 1;
721           }
722 
723           if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
724             diff_type = DIFF_TIME;
725           } else {
726             diff_type = DIFF_FREQ;
727           }
728           { hufYY = diff_type; }
729 
730           if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec,
731                                  num_val_1_int, 1, p0_data_1)) != HUFFDEC_OK) {
732             goto bail;
733           }
734 
735           for (i = 0; i < num_val_1_int; i++) {
736             out_data_1_int[i] = pair_vec[i][0];
737             out_data_2_int[i] = pair_vec[i][1];
738           }
739 
740           break; /* TIME_PAIR */
741 
742         default:
743           break;
744       }
745 
746       break; /* HUFF_2D */
747 
748     default:
749       break;
750   }
751 bail:
752   return err;
753 }
754 
diff_freq_decode(const SCHAR * const diff_data,SCHAR * const out_data,const int num_val)755 static void diff_freq_decode(const SCHAR* const diff_data,
756                              SCHAR* const out_data, const int num_val) {
757   int i = 0;
758   out_data[0] = diff_data[0];
759 
760   for (i = 1; i < num_val; i++) {
761     out_data[i] = out_data[i - 1] + diff_data[i];
762   }
763 }
764 
diff_time_decode_backwards(const SCHAR * const prev_data,const SCHAR * const diff_data,SCHAR * const out_data,const int mixed_diff_type,const int num_val)765 static void diff_time_decode_backwards(const SCHAR* const prev_data,
766                                        const SCHAR* const diff_data,
767                                        SCHAR* const out_data,
768                                        const int mixed_diff_type,
769                                        const int num_val) {
770   int i = 0; /* default start value*/
771 
772   if (mixed_diff_type) {
773     out_data[0] = diff_data[0];
774     i = 1; /* new start value */
775   }
776   for (; i < num_val; i++) {
777     out_data[i] = prev_data[i] + diff_data[i];
778   }
779 }
780 
diff_time_decode_forwards(const SCHAR * const prev_data,const SCHAR * const diff_data,SCHAR * const out_data,const int mixed_diff_type,const int num_val)781 static void diff_time_decode_forwards(const SCHAR* const prev_data,
782                                       const SCHAR* const diff_data,
783                                       SCHAR* const out_data,
784                                       const int mixed_diff_type,
785                                       const int num_val) {
786   int i = 0; /* default start value*/
787 
788   if (mixed_diff_type) {
789     out_data[0] = diff_data[0];
790     i = 1; /* new start value */
791   }
792   for (; i < num_val; i++) {
793     out_data[i] = prev_data[i] - diff_data[i];
794   }
795 }
796 
attach_lsb(HANDLE_FDK_BITSTREAM strm,SCHAR * in_data_msb,int offset,int num_lsb,int num_val,SCHAR * out_data)797 static ERROR_t attach_lsb(HANDLE_FDK_BITSTREAM strm, SCHAR* in_data_msb,
798                           int offset, int num_lsb, int num_val,
799                           SCHAR* out_data) {
800   int i = 0, lsb = 0;
801   ULONG data = 0;
802 
803   for (i = 0; i < num_val; i++) {
804     int msb;
805     msb = in_data_msb[i];
806 
807     if (num_lsb > 0) {
808       data = FDKreadBits(strm, num_lsb);
809       lsb = data;
810 
811       out_data[i] = ((msb << num_lsb) | lsb) - offset;
812     } else
813       out_data[i] = msb - offset;
814   }
815 
816   return HUFFDEC_OK; /* dummy */
817 }
818 
EcDataPairDec(DECODER_TYPE DECODER,HANDLE_FDK_BITSTREAM strm,SCHAR * aaOutData1,SCHAR * aaOutData2,SCHAR * aHistory,DATA_TYPE data_type,int startBand,int dataBands,int pair_flag,int coarse_flag,int allowDiffTimeBack_flag)819 ERROR_t EcDataPairDec(DECODER_TYPE DECODER, HANDLE_FDK_BITSTREAM strm,
820                       SCHAR* aaOutData1, SCHAR* aaOutData2, SCHAR* aHistory,
821                       DATA_TYPE data_type, int startBand, int dataBands,
822                       int pair_flag, int coarse_flag,
823                       int allowDiffTimeBack_flag)
824 
825 {
826   ERROR_t err = HUFFDEC_OK;
827 
828   // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
829   int attachLsb_flag = 0;
830   int pcmCoding_flag = 0;
831 
832   int mixed_time_pair = 0, numValPcm = 0;
833   int quant_levels = 0, quant_offset = 0;
834   ULONG data = 0;
835 
836   SCHAR aaDataPair[2][28] = {{0}};
837   SCHAR aaDataDiff[2][28] = {{0}};
838 
839   SCHAR aHistoryMsb[28] = {0};
840 
841   SCHAR* pDataVec[2] = {NULL, NULL};
842 
843   DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
844   PAIRING pairing = FREQ_PAIR;
845   DIRECTION direction = BACKWARDS;
846 
847   switch (data_type) {
848     case t_CLD:
849       if (coarse_flag) {
850         attachLsb_flag = 0;
851         quant_levels = 15;
852         quant_offset = 7;
853       } else {
854         attachLsb_flag = 0;
855         quant_levels = 31;
856         quant_offset = 15;
857       }
858 
859       break;
860 
861     case t_ICC:
862       if (coarse_flag) {
863         attachLsb_flag = 0;
864         quant_levels = 4;
865         quant_offset = 0;
866       } else {
867         attachLsb_flag = 0;
868         quant_levels = 8;
869         quant_offset = 0;
870       }
871 
872       break;
873 
874     case t_OLD:
875       if (coarse_flag) {
876         attachLsb_flag = 0;
877         quant_levels = 8;
878         quant_offset = 0;
879       } else {
880         attachLsb_flag = 0;
881         quant_levels = 16;
882         quant_offset = 0;
883       }
884       break;
885 
886     case t_NRG:
887       if (coarse_flag) {
888         attachLsb_flag = 0;
889         quant_levels = 32;
890         quant_offset = 0;
891       } else {
892         attachLsb_flag = 0;
893         quant_levels = 64;
894         quant_offset = 0;
895       }
896       break;
897 
898     case t_IPD:
899       if (!coarse_flag) {
900         attachLsb_flag = 1;
901         quant_levels = 16;
902         quant_offset = 0;
903       } else {
904         attachLsb_flag = 0;
905         quant_levels = 8;
906         quant_offset = 0;
907       }
908       break;
909 
910     default:
911       return HUFFDEC_NOTOK;
912   }
913 
914   data = FDKreadBits(strm, 1);
915   pcmCoding_flag = data;
916 
917   if (pcmCoding_flag) {
918     if (pair_flag) {
919       pDataVec[0] = aaDataPair[0];
920       pDataVec[1] = aaDataPair[1];
921       numValPcm = 2 * dataBands;
922     } else {
923       pDataVec[0] = aaDataPair[0];
924       pDataVec[1] = NULL;
925       numValPcm = dataBands;
926     }
927 
928     err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm,
929                      quant_levels);
930     if (err != HUFFDEC_OK) return HUFFDEC_NOTOK;
931 
932   } else { /* Differential/Huffman/LSB Coding */
933 
934     if (pair_flag) {
935       pDataVec[0] = aaDataDiff[0];
936       pDataVec[1] = aaDataDiff[1];
937     } else {
938       pDataVec[0] = aaDataDiff[0];
939       pDataVec[1] = NULL;
940     }
941 
942     diff_type[0] = DIFF_FREQ;
943     diff_type[1] = DIFF_FREQ;
944 
945     direction = BACKWARDS;
946     {
947       if (pair_flag || allowDiffTimeBack_flag) {
948         data = FDKreadBits(strm, 1);
949         diff_type[0] = (DIFF_TYPE)data;
950       }
951 
952       if (pair_flag &&
953           ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) {
954         data = FDKreadBits(strm, 1);
955         diff_type[1] = (DIFF_TYPE)data;
956       }
957     }
958     /* Huffman decoding */
959     err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0],
960                       diff_type[1], dataBands, &pairing,
961                       (DECODER == SAOC_DECODER));
962     if (err != HUFFDEC_OK) {
963       return HUFFDEC_NOTOK;
964     }
965 
966     {
967       /* Differential decoding */
968       if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
969         if (DECODER == SAOC_DECODER) {
970           direction = BACKWARDS;
971         } else {
972           if (pair_flag) {
973             if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) {
974               direction = FORWARDS;
975             } else if (diff_type[1] == DIFF_TIME) {
976               direction = BACKWARDS;
977             } else {
978               data = FDKreadBits(strm, 1);
979               direction = (DIRECTION)data;
980             }
981           } else {
982             direction = BACKWARDS;
983           }
984         }
985       }
986 
987       mixed_time_pair =
988           (diff_type[0] != diff_type[1]) && (pairing == TIME_PAIR);
989 
990       if (direction == BACKWARDS) {
991         if (diff_type[0] == DIFF_FREQ) {
992           diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
993         } else {
994           int i;
995           for (i = 0; i < dataBands; i++) {
996             aHistoryMsb[i] = aHistory[i + startBand] + quant_offset;
997             if (attachLsb_flag) {
998               aHistoryMsb[i] >>= 1;
999             }
1000           }
1001           diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0],
1002                                      mixed_time_pair, dataBands);
1003         }
1004         if (diff_type[1] == DIFF_FREQ) {
1005           diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1006         } else {
1007           diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1],
1008                                      aaDataPair[1], mixed_time_pair, dataBands);
1009         }
1010       } else {
1011         /* diff_type[1] MUST BE DIFF_FREQ */
1012         diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1013 
1014         if (diff_type[0] == DIFF_FREQ) {
1015           diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
1016         } else {
1017           diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0],
1018                                     mixed_time_pair, dataBands);
1019         }
1020       }
1021     }
1022 
1023     /* LSB decoding */
1024     err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0,
1025                      dataBands, aaDataPair[0]);
1026     if (err != HUFFDEC_OK) goto bail;
1027 
1028     if (pair_flag) {
1029       err = attach_lsb(strm, aaDataPair[1], quant_offset,
1030                        attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]);
1031       if (err != HUFFDEC_OK) goto bail;
1032     }
1033   } /* End: Differential/Huffman/LSB Coding */
1034 
1035   /* Copy data to output arrays */
1036   FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands);
1037   if (pair_flag) {
1038     FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands);
1039   }
1040 
1041 bail:
1042   return err;
1043 }
1044 
huff_dec_reshape(HANDLE_FDK_BITSTREAM strm,int * out_data,int num_val)1045 ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data,
1046                          int num_val) {
1047   ERROR_t err = HUFFDEC_OK;
1048   int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1049   SCHAR rl_data[2] = {0};
1050 
1051   while (val_rcvd < num_val) {
1052     err = huff_read_2D(strm,
1053                        (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0],
1054                        rl_data, &dummy);
1055     if (err != HUFFDEC_OK) goto bail;
1056     val = rl_data[0];
1057     len = rl_data[1] + 1;
1058     if (val_rcvd + len > num_val) {
1059       err = HUFFDEC_NOTOK;
1060       goto bail;
1061     }
1062     for (i = val_rcvd; i < val_rcvd + len; i++) {
1063       out_data[i] = val;
1064     }
1065     val_rcvd += len;
1066   }
1067 bail:
1068   return err;
1069 }
1070