]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/sbr_huff.c
844b969fdb96c842b866103aa764c77463dedf3b
[16.git] / src / lib / doslib / ext / faad / sbr_huff.c
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4 **  
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 ** 
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 ** 
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software 
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: sbr_huff.c,v 1.21 2007/11/01 12:33:35 menno Exp $
29 **/
30
31 #include "common.h"
32 #include "structs.h"
33
34 #ifdef SBR_DEC
35
36 #include "sbr_synt.h"
37 #include "bits.h"
38 #include "sbr_huff.h"
39 #include "sbr_e_nf.h"
40
41
42 typedef const int8_t (*sbr_huff_tab)[2];
43
44 static const int8_t t_huffman_env_1_5dB[120][2] = {
45     {   1,   2 },    { -64, -65 },    {   3,   4 },    { -63, -66 },
46     {   5,   6 },    { -62, -67 },    {   7,   8 },    { -61, -68 },
47     {   9,  10 },    { -60, -69 },    {  11,  12 },    { -59, -70 },
48     {  13,  14 },    { -58, -71 },    {  15,  16 },    { -57, -72 },
49     {  17,  18 },    { -73, -56 },    {  19,  21 },    { -74,  20 },
50     { -55, -75 },    {  22,  26 },    {  23,  24 },    { -54, -76 },
51     { -77,  25 },    { -53, -78 },    {  27,  34 },    {  28,  29 },
52     { -52, -79 },    {  30,  31 },    { -80, -51 },    {  32,  33 },
53     { -83, -82 },    { -81, -50 },    {  35,  57 },    {  36,  40 },
54     {  37,  38 },    { -88, -84 },    { -48,  39 },    { -90, -85 },
55     {  41,  46 },    {  42,  43 },    { -49, -87 },    {  44,  45 },
56     { -89, -86 },    {-124,-123 },    {  47,  50 },    {  48,  49 },
57     {-122,-121 },    {-120,-119 },    {  51,  54 },    {  52,  53 },
58     {-118,-117 },    {-116,-115 },    {  55,  56 },    {-114,-113 },
59     {-112,-111 },    {  58,  89 },    {  59,  74 },    {  60,  67 },
60     {  61,  64 },    {  62,  63 },    {-110,-109 },    {-108,-107 },
61     {  65,  66 },    {-106,-105 },    {-104,-103 },    {  68,  71 },
62     {  69,  70 },    {-102,-101 },    {-100, -99 },    {  72,  73 },
63     { -98, -97 },    { -96, -95 },    {  75,  82 },    {  76,  79 },
64     {  77,  78 },    { -94, -93 },    { -92, -91 },    {  80,  81 },
65     { -47, -46 },    { -45, -44 },    {  83,  86 },    {  84,  85 },
66     { -43, -42 },    { -41, -40 },    {  87,  88 },    { -39, -38 },
67     { -37, -36 },    {  90, 105 },    {  91,  98 },    {  92,  95 },
68     {  93,  94 },    { -35, -34 },    { -33, -32 },    {  96,  97 },
69     { -31, -30 },    { -29, -28 },    {  99, 102 },    { 100, 101 },
70     { -27, -26 },    { -25, -24 },    { 103, 104 },    { -23, -22 },
71     { -21, -20 },    { 106, 113 },    { 107, 110 },    { 108, 109 },
72     { -19, -18 },    { -17, -16 },    { 111, 112 },    { -15, -14 },
73     { -13, -12 },    { 114, 117 },    { 115, 116 },    { -11, -10 },
74     {  -9,  -8 },    { 118, 119 },    {  -7,  -6 },    {  -5,  -4 }
75 };
76
77 static const int8_t f_huffman_env_1_5dB[120][2] = {
78     {   1,   2 },    { -64, -65 },    {   3,   4 },    { -63, -66 },
79     {   5,   6 },    { -67, -62 },    {   7,   8 },    { -68, -61 },
80     {   9,  10 },    { -69, -60 },    {  11,  13 },    { -70,  12 },
81     { -59, -71 },    {  14,  16 },    { -58,  15 },    { -72, -57 },
82     {  17,  19 },    { -73,  18 },    { -56, -74 },    {  20,  23 },
83     {  21,  22 },    { -55, -75 },    { -54, -53 },    {  24,  27 },
84     {  25,  26 },    { -76, -52 },    { -77, -51 },    {  28,  31 },
85     {  29,  30 },    { -50, -78 },    { -79, -49 },    {  32,  36 },
86     {  33,  34 },    { -48, -47 },    { -80,  35 },    { -81, -82 },
87     {  37,  47 },    {  38,  41 },    {  39,  40 },    { -83, -46 },
88     { -45, -84 },    {  42,  44 },    { -85,  43 },    { -44, -43 },
89     {  45,  46 },    { -88, -87 },    { -86, -90 },    {  48,  66 },
90     {  49,  56 },    {  50,  53 },    {  51,  52 },    { -92, -42 },
91     { -41, -39 },    {  54,  55 },    {-105, -89 },    { -38, -37 },
92     {  57,  60 },    {  58,  59 },    { -94, -91 },    { -40, -36 },
93     {  61,  63 },    { -20,  62 },    {-115,-110 },    {  64,  65 },
94     {-108,-107 },    {-101, -97 },    {  67,  89 },    {  68,  75 },
95     {  69,  72 },    {  70,  71 },    { -95, -93 },    { -34, -27 },
96     {  73,  74 },    { -22, -17 },    { -16,-124 },    {  76,  82 },
97     {  77,  79 },    {-123,  78 },    {-122,-121 },    {  80,  81 },
98     {-120,-119 },    {-118,-117 },    {  83,  86 },    {  84,  85 },
99     {-116,-114 },    {-113,-112 },    {  87,  88 },    {-111,-109 },
100     {-106,-104 },    {  90, 105 },    {  91,  98 },    {  92,  95 },
101     {  93,  94 },    {-103,-102 },    {-100, -99 },    {  96,  97 },
102     { -98, -96 },    { -35, -33 },    {  99, 102 },    { 100, 101 },
103     { -32, -31 },    { -30, -29 },    { 103, 104 },    { -28, -26 },
104     { -25, -24 },    { 106, 113 },    { 107, 110 },    { 108, 109 },
105     { -23, -21 },    { -19, -18 },    { 111, 112 },    { -15, -14 },
106     { -13, -12 },    { 114, 117 },    { 115, 116 },    { -11, -10 },
107     {  -9,  -8 },    { 118, 119 },    {  -7,  -6 },    {  -5,  -4 }
108 };
109
110 static const int8_t t_huffman_env_bal_1_5dB[48][2] = {
111     { -64,   1 },    { -63,   2 },    { -65,   3 },    { -62,   4 },
112     { -66,   5 },    { -61,   6 },    { -67,   7 },    { -60,   8 },
113     { -68,   9 },    {  10,  11 },    { -69, -59 },    {  12,  13 },
114     { -70, -58 },    {  14,  28 },    {  15,  21 },    {  16,  18 },
115     { -57,  17 },    { -71, -56 },    {  19,  20 },    { -88, -87 },
116     { -86, -85 },    {  22,  25 },    {  23,  24 },    { -84, -83 },
117     { -82, -81 },    {  26,  27 },    { -80, -79 },    { -78, -77 },
118     {  29,  36 },    {  30,  33 },    {  31,  32 },    { -76, -75 },
119     { -74, -73 },    {  34,  35 },    { -72, -55 },    { -54, -53 },
120     {  37,  41 },    {  38,  39 },    { -52, -51 },    { -50,  40 },
121     { -49, -48 },    {  42,  45 },    {  43,  44 },    { -47, -46 },
122     { -45, -44 },    {  46,  47 },    { -43, -42 },    { -41, -40 }
123 };
124
125 static const int8_t f_huffman_env_bal_1_5dB[48][2] = {
126     { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
127     { -62,   5 },    { -61,   6 },    { -67,   7 },    { -68,   8 },
128     { -60,   9 },    {  10,  11 },    { -69, -59 },    { -70,  12 },
129     { -58,  13 },    {  14,  17 },    { -71,  15 },    { -57,  16 },
130     { -56, -73 },    {  18,  32 },    {  19,  25 },    {  20,  22 },
131     { -72,  21 },    { -88, -87 },    {  23,  24 },    { -86, -85 },
132     { -84, -83 },    {  26,  29 },    {  27,  28 },    { -82, -81 },
133     { -80, -79 },    {  30,  31 },    { -78, -77 },    { -76, -75 },
134     {  33,  40 },    {  34,  37 },    {  35,  36 },    { -74, -55 },
135     { -54, -53 },    {  38,  39 },    { -52, -51 },    { -50, -49 },
136     {  41,  44 },    {  42,  43 },    { -48, -47 },    { -46, -45 },
137     {  45,  46 },    { -44, -43 },    { -42,  47 },    { -41, -40 }
138 };
139
140 static const int8_t t_huffman_env_3_0dB[62][2] = {
141     { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
142     { -62,   5 },    { -67,   6 },    { -61,   7 },    { -68,   8 },
143     { -60,   9 },    {  10,  11 },    { -69, -59 },    {  12,  14 },
144     { -70,  13 },    { -71, -58 },    {  15,  18 },    {  16,  17 },
145     { -72, -57 },    { -73, -74 },    {  19,  22 },    { -56,  20 },
146     { -55,  21 },    { -54, -77 },    {  23,  31 },    {  24,  25 },
147     { -75, -76 },    {  26,  27 },    { -78, -53 },    {  28,  29 },
148     { -52, -95 },    { -94,  30 },    { -93, -92 },    {  32,  47 },
149     {  33,  40 },    {  34,  37 },    {  35,  36 },    { -91, -90 },
150     { -89, -88 },    {  38,  39 },    { -87, -86 },    { -85, -84 },
151     {  41,  44 },    {  42,  43 },    { -83, -82 },    { -81, -80 },
152     {  45,  46 },    { -79, -51 },    { -50, -49 },    {  48,  55 },
153     {  49,  52 },    {  50,  51 },    { -48, -47 },    { -46, -45 },
154     {  53,  54 },    { -44, -43 },    { -42, -41 },    {  56,  59 },
155     {  57,  58 },    { -40, -39 },    { -38, -37 },    {  60,  61 },
156     { -36, -35 },    { -34, -33 }
157 };
158
159 static const int8_t f_huffman_env_3_0dB[62][2] = {
160     { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
161     { -62,   5 },    { -67,   6 },    {   7,   8 },    { -61, -68 },
162     {   9,  10 },    { -60, -69 },    {  11,  12 },    { -59, -70 },
163     {  13,  14 },    { -58, -71 },    {  15,  16 },    { -57, -72 },
164     {  17,  19 },    { -56,  18 },    { -55, -73 },    {  20,  24 },
165     {  21,  22 },    { -74, -54 },    { -53,  23 },    { -75, -76 },
166     {  25,  30 },    {  26,  27 },    { -52, -51 },    {  28,  29 },
167     { -77, -79 },    { -50, -49 },    {  31,  39 },    {  32,  35 },
168     {  33,  34 },    { -78, -46 },    { -82, -88 },    {  36,  37 },
169     { -83, -48 },    { -47,  38 },    { -86, -85 },    {  40,  47 },
170     {  41,  44 },    {  42,  43 },    { -80, -44 },    { -43, -42 },
171     {  45,  46 },    { -39, -87 },    { -84, -40 },    {  48,  55 },
172     {  49,  52 },    {  50,  51 },    { -95, -94 },    { -93, -92 },
173     {  53,  54 },    { -91, -90 },    { -89, -81 },    {  56,  59 },
174     {  57,  58 },    { -45, -41 },    { -38, -37 },    {  60,  61 },
175     { -36, -35 },    { -34, -33 }
176 };
177
178 static const int8_t t_huffman_env_bal_3_0dB[24][2] = {
179     { -64,   1 },    { -63,   2 },    { -65,   3 },    { -66,   4 },
180     { -62,   5 },    { -61,   6 },    { -67,   7 },    { -68,   8 },
181     { -60,   9 },    {  10,  16 },    {  11,  13 },    { -69,  12 },
182     { -76, -75 },    {  14,  15 },    { -74, -73 },    { -72, -71 },
183     {  17,  20 },    {  18,  19 },    { -70, -59 },    { -58, -57 },
184     {  21,  22 },    { -56, -55 },    { -54,  23 },    { -53, -52 }
185 };
186
187 static const int8_t f_huffman_env_bal_3_0dB[24][2] = {
188     { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
189     { -62,   5 },    { -61,   6 },    { -67,   7 },    { -68,   8 },
190     { -60,   9 },    {  10,  13 },    { -69,  11 },    { -59,  12 },
191     { -58, -76 },    {  14,  17 },    {  15,  16 },    { -75, -74 },
192     { -73, -72 },    {  18,  21 },    {  19,  20 },    { -71, -70 },
193     { -57, -56 },    {  22,  23 },    { -55, -54 },    { -53, -52 }
194 };
195
196 static const int8_t t_huffman_noise_3_0dB[62][2] = {
197     { -64,   1 },    { -63,   2 },    { -65,   3 },    { -66,   4 },
198     { -62,   5 },    { -67,   6 },    {   7,   8 },    { -61, -68 },
199     {   9,  30 },    {  10,  15 },    { -60,  11 },    { -69,  12 },
200     {  13,  14 },    { -59, -53 },    { -95, -94 },    {  16,  23 },
201     {  17,  20 },    {  18,  19 },    { -93, -92 },    { -91, -90 },
202     {  21,  22 },    { -89, -88 },    { -87, -86 },    {  24,  27 },
203     {  25,  26 },    { -85, -84 },    { -83, -82 },    {  28,  29 },
204     { -81, -80 },    { -79, -78 },    {  31,  46 },    {  32,  39 },
205     {  33,  36 },    {  34,  35 },    { -77, -76 },    { -75, -74 },
206     {  37,  38 },    { -73, -72 },    { -71, -70 },    {  40,  43 },
207     {  41,  42 },    { -58, -57 },    { -56, -55 },    {  44,  45 },
208     { -54, -52 },    { -51, -50 },    {  47,  54 },    {  48,  51 },
209     {  49,  50 },    { -49, -48 },    { -47, -46 },    {  52,  53 },
210     { -45, -44 },    { -43, -42 },    {  55,  58 },    {  56,  57 },
211     { -41, -40 },    { -39, -38 },    {  59,  60 },    { -37, -36 },
212     { -35,  61 },    { -34, -33 }
213 };
214
215 static const int8_t t_huffman_noise_bal_3_0dB[24][2] = {
216     { -64,   1 },    { -65,   2 },    { -63,   3 },    {   4,   9 },
217     { -66,   5 },    { -62,   6 },    {   7,   8 },    { -76, -75 },
218     { -74, -73 },    {  10,  17 },    {  11,  14 },    {  12,  13 },
219     { -72, -71 },    { -70, -69 },    {  15,  16 },    { -68, -67 },
220     { -61, -60 },    {  18,  21 },    {  19,  20 },    { -59, -58 },
221     { -57, -56 },    {  22,  23 },    { -55, -54 },    { -53, -52 }
222 };
223
224
225 static INLINE int16_t sbr_huff_dec(bitfile *ld, sbr_huff_tab t_huff)
226 {
227     uint8_t bit;
228     int16_t index = 0;
229
230     while (index >= 0)
231     {
232         bit = (uint8_t)faad_get1bit(ld);
233         index = t_huff[index][bit];
234     }
235
236     return index + 64;
237 }
238
239 /* table 10 */
240 void sbr_envelope(bitfile *ld, sbr_info *sbr, uint8_t ch)
241 {
242     uint8_t env, band;
243     int8_t delta = 0;
244     sbr_huff_tab t_huff, f_huff;
245
246     if ((sbr->L_E[ch] == 1) && (sbr->bs_frame_class[ch] == FIXFIX))
247         sbr->amp_res[ch] = 0;
248     else
249         sbr->amp_res[ch] = sbr->bs_amp_res;
250
251     if ((sbr->bs_coupling) && (ch == 1))
252     {
253         delta = 1;
254         if (sbr->amp_res[ch])
255         {
256             t_huff = t_huffman_env_bal_3_0dB;
257             f_huff = f_huffman_env_bal_3_0dB;
258         } else {
259             t_huff = t_huffman_env_bal_1_5dB;
260             f_huff = f_huffman_env_bal_1_5dB;
261         }
262     } else {
263         delta = 0;
264         if (sbr->amp_res[ch])
265         {
266             t_huff = t_huffman_env_3_0dB;
267             f_huff = f_huffman_env_3_0dB;
268         } else {
269             t_huff = t_huffman_env_1_5dB;
270             f_huff = f_huffman_env_1_5dB;
271         }
272     }
273
274     for (env = 0; env < sbr->L_E[ch]; env++)
275     {
276         if (sbr->bs_df_env[ch][env] == 0)
277         {
278             if ((sbr->bs_coupling == 1) && (ch == 1))
279             {
280                 if (sbr->amp_res[ch])
281                 {
282                     sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 5
283                         DEBUGVAR(1,272,"sbr_envelope(): bs_data_env")) << delta);
284                 } else {
285                     sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
286                         DEBUGVAR(1,273,"sbr_envelope(): bs_data_env")) << delta);
287                 }
288             } else {
289                 if (sbr->amp_res[ch])
290                 {
291                     sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
292                         DEBUGVAR(1,274,"sbr_envelope(): bs_data_env")) << delta);
293                 } else {
294                     sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 7
295                         DEBUGVAR(1,275,"sbr_envelope(): bs_data_env")) << delta);
296                 }
297             }
298
299             for (band = 1; band < sbr->n[sbr->f[ch][env]]; band++)
300             {
301                 sbr->E[ch][band][env] = (sbr_huff_dec(ld, f_huff) << delta);
302             }
303
304         } else {
305             for (band = 0; band < sbr->n[sbr->f[ch][env]]; band++)
306             {
307                 sbr->E[ch][band][env] = (sbr_huff_dec(ld, t_huff) << delta);
308             }
309         }
310     }
311
312     extract_envelope_data(sbr, ch);
313 }
314
315 /* table 11 */
316 void sbr_noise(bitfile *ld, sbr_info *sbr, uint8_t ch)
317 {
318     uint8_t noise, band;
319     int8_t delta = 0;
320     sbr_huff_tab t_huff, f_huff;
321
322     if ((sbr->bs_coupling == 1) && (ch == 1))
323     {
324         delta = 1;
325         t_huff = t_huffman_noise_bal_3_0dB;
326         f_huff = f_huffman_env_bal_3_0dB;
327     } else {
328         delta = 0;
329         t_huff = t_huffman_noise_3_0dB;
330         f_huff = f_huffman_env_3_0dB;
331     }
332
333     for (noise = 0; noise < sbr->L_Q[ch]; noise++)
334     {
335         if(sbr->bs_df_noise[ch][noise] == 0)
336         {
337             if ((sbr->bs_coupling == 1) && (ch == 1))
338             {
339                 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
340                     DEBUGVAR(1,276,"sbr_noise(): bs_data_noise")) << delta);
341             } else {
342                 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
343                     DEBUGVAR(1,277,"sbr_noise(): bs_data_noise")) << delta);
344             }
345             for (band = 1; band < sbr->N_Q; band++)
346             {
347                 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, f_huff) << delta);
348             }
349         } else {
350             for (band = 0; band < sbr->N_Q; band++)
351             {
352                 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, t_huff) << delta);
353             }
354         }
355     }
356
357     extract_noise_floor_data(sbr, ch);
358 }
359
360 #endif