/src/fdk-aac/libAACdec/src/usacdec_lpd.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /**************************** AAC decoder library ****************************** |
96 | | |
97 | | Author(s): Manuel Jander |
98 | | |
99 | | Description: USAC Linear Prediction Domain coding |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "usacdec_lpd.h" |
104 | | |
105 | | #include "usacdec_rom.h" |
106 | | #include "usacdec_fac.h" |
107 | | #include "usacdec_lpc.h" |
108 | | #include "FDK_tools_rom.h" |
109 | | #include "fft.h" |
110 | | #include "mdct.h" |
111 | | #include "usacdec_acelp.h" |
112 | | #include "overlapadd.h" |
113 | | |
114 | | #include "conceal.h" |
115 | | |
116 | | #include "block.h" |
117 | | |
118 | 221M | #define SF_PITCH_TRACK 6 |
119 | | #define SF_GAIN 3 |
120 | | #define MIN_VAL FL2FXCONST_DBL(0.0f) |
121 | | #define MAX_VAL (FIXP_DBL) MAXVAL_DBL |
122 | | |
123 | | #include "ac_arith_coder.h" |
124 | | |
125 | | void filtLP(const FIXP_DBL *syn, PCM_DEC *syn_out, FIXP_DBL *noise, |
126 | | const FIXP_SGL *filt, const INT aacOutDataHeadroom, INT stop, |
127 | 7.73M | int len) { |
128 | 7.73M | INT i, j; |
129 | 7.73M | FIXP_DBL tmp; |
130 | | |
131 | 7.73M | FDK_ASSERT((aacOutDataHeadroom - 1) >= -(MDCT_OUTPUT_SCALE)); |
132 | | |
133 | 502M | for (i = 0; i < stop; i++) { |
134 | 494M | tmp = fMultDiv2(noise[i], filt[0]); // Filt in Q-1.16 |
135 | 6.43G | for (j = 1; j <= len; j++) { |
136 | 5.93G | tmp += fMult((noise[i - j] >> 1) + (noise[i + j] >> 1), filt[j]); |
137 | 5.93G | } |
138 | 494M | syn_out[i] = (PCM_DEC)( |
139 | 494M | IMDCT_SCALE((syn[i] >> 1) - (tmp >> 1), aacOutDataHeadroom - 1)); |
140 | 494M | } |
141 | 7.73M | } |
142 | | |
143 | | void bass_pf_1sf_delay( |
144 | | FIXP_DBL *syn, /* (i) : 12.8kHz synthesis to postfilter */ |
145 | | const INT *T_sf, /* (i) : Pitch period for all subframes (T_sf[16]) */ |
146 | | FIXP_DBL *pit_gain, |
147 | | const int frame_length, /* (i) : frame length (should be 768|1024) */ |
148 | | const INT l_frame, |
149 | | const INT l_next, /* (i) : look ahead for symmetric filtering */ |
150 | | PCM_DEC *synth_out, /* (o) : filtered synthesis (with delay of 1 subfr) */ |
151 | | const INT aacOutDataHeadroom, /* (i) : headroom of the output time signal to |
152 | | prevent clipping */ |
153 | | FIXP_DBL mem_bpf[]) /* i/o : memory state [L_FILT+L_SUBFR] */ |
154 | 606k | { |
155 | 606k | INT i, sf, i_subfr, T, T2, lg; |
156 | | |
157 | 606k | FIXP_DBL tmp, ener, corr, gain; |
158 | 606k | FIXP_DBL *noise, *noise_in; |
159 | 606k | FIXP_DBL |
160 | 606k | noise_buf[L_FILT + (2 * L_SUBFR)]; // L_FILT = 12, L_SUBFR = 64 => 140 |
161 | 606k | const FIXP_DBL *x, *y; |
162 | | |
163 | 606k | { |
164 | 606k | noise = noise_buf + L_FILT; // L_FILT = 12 delay of upsampling filter |
165 | 606k | noise_in = noise_buf + L_FILT + L_SUBFR; |
166 | | /* Input scaling of the BPF memory */ |
167 | 606k | scaleValues(mem_bpf, (L_FILT + L_SUBFR), 1); |
168 | 606k | } |
169 | | |
170 | 606k | int gain_exp = 17; |
171 | | |
172 | 606k | sf = 0; |
173 | 8.33M | for (i_subfr = 0; i_subfr < l_frame; i_subfr += L_SUBFR, sf++) { |
174 | 7.73M | T = T_sf[sf]; |
175 | 7.73M | gain = pit_gain[sf]; |
176 | | |
177 | | /* Gain is in Q17.14 */ |
178 | | /* If gain > 1 set to 1 */ |
179 | 7.73M | if (gain > (FIXP_DBL)(1 << 14)) gain = (FIXP_DBL)(1 << 14); |
180 | | |
181 | | /* If gain < 0 set to 0 */ |
182 | 7.73M | if (gain < (FIXP_DBL)0) gain = (FIXP_DBL)0; |
183 | | |
184 | 7.73M | if (gain > (FIXP_DBL)0) { |
185 | | /* pitch tracker: test pitch/2 to avoid continuous pitch doubling */ |
186 | | /* Note: pitch is limited to PIT_MIN (34 = 376Hz) at the encoder */ |
187 | 1.73M | T2 = T >> 1; |
188 | 1.73M | x = &syn[i_subfr - L_EXTRA]; |
189 | 1.73M | y = &syn[i_subfr - T2 - L_EXTRA]; |
190 | | |
191 | 1.73M | ener = (FIXP_DBL)0; |
192 | 1.73M | corr = (FIXP_DBL)0; |
193 | 1.73M | tmp = (FIXP_DBL)0; |
194 | | |
195 | 1.73M | int headroom_x = getScalefactor(x, L_SUBFR + L_EXTRA); |
196 | 1.73M | int headroom_y = getScalefactor(y, L_SUBFR + L_EXTRA); |
197 | | |
198 | 1.73M | int width_shift = 7; |
199 | | |
200 | 279M | for (i = 0; i < (L_SUBFR + L_EXTRA); i++) { |
201 | 277M | ener += fPow2Div2((x[i] << headroom_x)) >> width_shift; |
202 | 277M | corr += fMultDiv2((x[i] << headroom_x), (y[i] << headroom_y)) >> |
203 | 277M | width_shift; |
204 | 277M | tmp += fPow2Div2((y[i] << headroom_y)) >> width_shift; |
205 | 277M | } |
206 | | |
207 | 1.73M | int exp_ener = ((17 - headroom_x) << 1) + width_shift + 1; |
208 | 1.73M | int exp_corr = (17 - headroom_x) + (17 - headroom_y) + width_shift + 1; |
209 | 1.73M | int exp_tmp = ((17 - headroom_y) << 1) + width_shift + 1; |
210 | | |
211 | | /* Add 0.01 to "ener". Adjust exponents */ |
212 | 1.73M | FIXP_DBL point_zero_one = (FIXP_DBL)0x51eb851f; /* In Q-6.37 */ |
213 | 1.73M | int diff; |
214 | 1.73M | ener = fAddNorm(ener, exp_ener, point_zero_one, -6, &exp_ener); |
215 | 1.73M | corr = fAddNorm(corr, exp_corr, point_zero_one, -6, &exp_corr); |
216 | 1.73M | tmp = fAddNorm(tmp, exp_tmp, point_zero_one, -6, &exp_tmp); |
217 | | |
218 | | /* use T2 if normalized correlation > 0.95 */ |
219 | 1.73M | INT s1, s2; |
220 | 1.73M | s1 = CntLeadingZeros(ener) - 1; |
221 | 1.73M | s2 = CntLeadingZeros(tmp) - 1; |
222 | | |
223 | 1.73M | FIXP_DBL ener_by_tmp = fMultDiv2(ener << s1, tmp << s2); |
224 | 1.73M | int ener_by_tmp_exp = (exp_ener - s1) + (exp_tmp - s2) + 1; |
225 | | |
226 | 1.73M | if (ener_by_tmp_exp & 1) { |
227 | 1.35M | ener_by_tmp <<= 1; |
228 | 1.35M | ener_by_tmp_exp -= 1; |
229 | 1.35M | } |
230 | | |
231 | 1.73M | int temp_exp = 0; |
232 | | |
233 | 1.73M | FIXP_DBL temp1 = invSqrtNorm2(ener_by_tmp, &temp_exp); |
234 | | |
235 | 1.73M | int temp1_exp = temp_exp - (ener_by_tmp_exp >> 1); |
236 | | |
237 | 1.73M | FIXP_DBL tmp_result = fMult(corr, temp1); |
238 | | |
239 | 1.73M | int tmp_result_exp = exp_corr + temp1_exp; |
240 | | |
241 | 1.73M | diff = tmp_result_exp - 0; |
242 | 1.73M | FIXP_DBL point95 = FL2FXCONST_DBL(0.95f); |
243 | 1.73M | if (diff >= 0) { |
244 | 1.45M | diff = fMin(diff, 31); |
245 | 1.45M | point95 = FL2FXCONST_DBL(0.95f) >> diff; |
246 | 1.45M | } else { |
247 | 283k | diff = fMax(diff, -31); |
248 | 283k | tmp_result >>= (-diff); |
249 | 283k | } |
250 | | |
251 | 1.73M | if (tmp_result > point95) T = T2; |
252 | | |
253 | | /* prevent that noise calculation below reaches into not defined signal |
254 | | parts at the end of the synth_buf or in other words restrict the below |
255 | | used index (i+i_subfr+T) < l_frame + l_next |
256 | | */ |
257 | 1.73M | lg = l_frame + l_next - T - i_subfr; |
258 | | |
259 | 1.73M | if (lg > L_SUBFR) |
260 | 1.72M | lg = L_SUBFR; |
261 | 12.4k | else if (lg < 0) |
262 | 7.04k | lg = 0; |
263 | | |
264 | | /* limit gain to avoid problem on burst */ |
265 | 1.73M | if (lg > 0) { |
266 | 1.72M | FIXP_DBL tmp1; |
267 | | |
268 | | /* max(lg) = 64 => scale with 6 bits minus 1 (fPow2Div2) */ |
269 | | |
270 | 1.72M | s1 = getScalefactor(&syn[i_subfr], lg); |
271 | 1.72M | s2 = getScalefactor(&syn[i_subfr + T], lg); |
272 | 1.72M | INT s = fixMin(s1, s2); |
273 | | |
274 | 1.72M | tmp = (FIXP_DBL)0; |
275 | 1.72M | ener = (FIXP_DBL)0; |
276 | 112M | for (i = 0; i < lg; i++) { |
277 | 110M | tmp += fPow2Div2(syn[i + i_subfr] << s1) >> (SF_PITCH_TRACK); |
278 | 110M | ener += fPow2Div2(syn[i + i_subfr + T] << s2) >> (SF_PITCH_TRACK); |
279 | 110M | } |
280 | 1.72M | tmp = tmp >> fMin(DFRACT_BITS - 1, (2 * (s1 - s))); |
281 | 1.72M | ener = ener >> fMin(DFRACT_BITS - 1, (2 * (s2 - s))); |
282 | | |
283 | | /* error robustness: for the specific case syn[...] == -1.0f for all 64 |
284 | | samples ener or tmp might overflow and become negative. For all sane |
285 | | cases we have enough headroom. |
286 | | */ |
287 | 1.72M | if (ener <= (FIXP_DBL)0) { |
288 | 5.47k | ener = (FIXP_DBL)1; |
289 | 5.47k | } |
290 | 1.72M | if (tmp <= (FIXP_DBL)0) { |
291 | 5.03k | tmp = (FIXP_DBL)1; |
292 | 5.03k | } |
293 | 1.72M | FDK_ASSERT(ener > (FIXP_DBL)0); |
294 | | |
295 | | /* tmp = sqrt(tmp/ener) */ |
296 | 1.72M | int result_e = 0; |
297 | 1.72M | tmp1 = fDivNorm(tmp, ener, &result_e); |
298 | 1.72M | if (result_e & 1) { |
299 | 1.19M | tmp1 >>= 1; |
300 | 1.19M | result_e += 1; |
301 | 1.19M | } |
302 | 1.72M | tmp = sqrtFixp(tmp1); |
303 | 1.72M | result_e >>= 1; |
304 | | |
305 | 1.72M | gain_exp = 17; |
306 | | |
307 | 1.72M | diff = result_e - gain_exp; |
308 | | |
309 | 1.72M | FIXP_DBL gain1 = gain; |
310 | | |
311 | 1.72M | if (diff >= 0) { |
312 | 0 | diff = fMin(diff, 31); |
313 | 0 | gain1 >>= diff; |
314 | 1.72M | } else { |
315 | 1.72M | result_e += (-diff); |
316 | 1.72M | diff = fMax(diff, -31); |
317 | 1.72M | tmp >>= (-diff); |
318 | 1.72M | } |
319 | | |
320 | 1.72M | if (tmp < gain1) { |
321 | 290k | gain = tmp; |
322 | 290k | gain_exp = result_e; |
323 | 290k | } |
324 | 1.72M | } |
325 | | |
326 | | /* calculate noise based on voiced pitch */ |
327 | | /* fMultDiv2() replaces weighting of gain with 0.5 */ |
328 | 1.73M | diff = gain_exp - 17; |
329 | 1.73M | if (diff >= 0) { |
330 | 1.73M | gain <<= diff; |
331 | 1.73M | } else { |
332 | 0 | gain >>= (-diff); |
333 | 0 | } |
334 | | |
335 | 1.73M | s1 = CntLeadingZeros(gain) - 1; |
336 | 1.73M | s1 -= 16; /* Leading bits for SGL */ |
337 | | |
338 | 1.73M | FIXP_SGL gainSGL = FX_DBL2FX_SGL(gain << 16); |
339 | | |
340 | 1.73M | gainSGL = gainSGL << s1; |
341 | | |
342 | 1.73M | { |
343 | 112M | for (i = 0; i < lg; i++) { |
344 | | /* scaled with SF_SYNTH + gain_sf + 1; composition of scalefactor 2: |
345 | | * one additional shift of syn values + fMult => fMultDiv2 */ |
346 | 110M | noise_in[i] = |
347 | 110M | scaleValue(fMultDiv2(gainSGL, (syn[i + i_subfr] >> 1) - |
348 | 110M | (syn[i + i_subfr - T] >> 2) - |
349 | 110M | (syn[i + i_subfr + T] >> 2)), |
350 | 110M | 2 - s1); |
351 | 110M | } |
352 | | |
353 | 2.32M | for (i = lg; i < L_SUBFR; i++) { |
354 | | /* scaled with SF_SYNTH + gain_sf + 1; composition of scalefactor 2: |
355 | | * one additional shift of syn values + fMult => fMultDiv2 */ |
356 | 585k | noise_in[i] = |
357 | 585k | scaleValue(fMultDiv2(gainSGL, (syn[i + i_subfr] >> 1) - |
358 | 585k | (syn[i + i_subfr - T] >> 1)), |
359 | 585k | 2 - s1); |
360 | 585k | } |
361 | 1.73M | } |
362 | 5.99M | } else { |
363 | 5.99M | FDKmemset(noise_in, (FIXP_DBL)0, L_SUBFR * sizeof(FIXP_DBL)); |
364 | 5.99M | } |
365 | | |
366 | 7.73M | { |
367 | 7.73M | FDKmemcpy(noise_buf, mem_bpf, (L_FILT + L_SUBFR) * sizeof(FIXP_DBL)); |
368 | | |
369 | 7.73M | FDKmemcpy(mem_bpf, noise_buf + L_SUBFR, |
370 | 7.73M | (L_FILT + L_SUBFR) * sizeof(FIXP_DBL)); |
371 | 7.73M | } |
372 | | |
373 | | /* substract from voiced speech low-pass filtered noise */ |
374 | | /* filter coefficients are scaled with factor SF_FILT_LP (1) */ |
375 | | |
376 | 7.73M | { |
377 | 7.73M | filtLP(&syn[i_subfr - L_SUBFR], &synth_out[i_subfr], noise, |
378 | 7.73M | fdk_dec_filt_lp, aacOutDataHeadroom, L_SUBFR, L_FILT); |
379 | 7.73M | } |
380 | 7.73M | } |
381 | | |
382 | 606k | { |
383 | | |
384 | 606k | } |
385 | | |
386 | | // To be determined (info from Ben) |
387 | 606k | { |
388 | | /* Output scaling of the BPF memory */ |
389 | 606k | scaleValues(mem_bpf, (L_FILT + L_SUBFR), -1); |
390 | | /* Copy the rest of the signal (after the fac) */ |
391 | 606k | scaleValuesSaturate( |
392 | 606k | (PCM_DEC *)&synth_out[l_frame], (FIXP_DBL *)&syn[l_frame - L_SUBFR], |
393 | 606k | (frame_length - l_frame), MDCT_OUT_HEADROOM - aacOutDataHeadroom); |
394 | 606k | } |
395 | | |
396 | 606k | return; |
397 | 606k | } |
398 | | |
399 | | /* |
400 | | * Frequency Domain Noise Shaping |
401 | | */ |
402 | | |
403 | | /** |
404 | | * \brief Adaptive Low Frequencies Deemphasis of spectral coefficients. |
405 | | * |
406 | | * Ensure quantization of low frequencies in case where the |
407 | | * signal dynamic is higher than the LPC noise shaping. |
408 | | * This is the inverse operation of adap_low_freq_emph(). |
409 | | * Output gain of all blocks. |
410 | | * |
411 | | * \param x pointer to the spectral coefficients, requires 1 bit headroom. |
412 | | * \param lg length of x. |
413 | | * \param bUseNewAlfe if set, apply ALFD for fullband lpd. |
414 | | * \param gainLpc1 pointer to gain based on old input LPC coefficients. |
415 | | * \param gainLpc2 pointer to gain based on new input LPC coefficients. |
416 | | * \param alfd_gains pointer to output gains. |
417 | | * \param s current scale shift factor of x. |
418 | | */ |
419 | 7.13M | #define ALFDPOW2_SCALE 3 |
420 | | /*static*/ |
421 | | void CLpd_AdaptLowFreqDeemph(FIXP_DBL x[], int lg, FIXP_DBL alfd_gains[], |
422 | 119k | INT s) { |
423 | 119k | { |
424 | 119k | int i, j, k, i_max; |
425 | 119k | FIXP_DBL max, fac; |
426 | | /* Note: This stack array saves temporary accumulation results to be used in |
427 | | * a second run */ |
428 | | /* The size should be limited to (1024/4)/8=32 */ |
429 | 119k | FIXP_DBL tmp_pow2[32]; |
430 | | |
431 | 119k | s = s * 2 + ALFDPOW2_SCALE; |
432 | 119k | s = fMin(31, s); |
433 | | |
434 | 119k | k = 8; |
435 | 119k | i_max = lg / 4; /* ALFD range = 1600Hz (lg = 6400Hz) */ |
436 | | |
437 | | /* find spectral peak */ |
438 | 119k | max = FL2FX_DBL(0.01f) >> s; |
439 | 996k | for (i = 0; i < i_max; i += k) { |
440 | 877k | FIXP_DBL tmp; |
441 | | |
442 | 877k | tmp = FIXP_DBL(0); |
443 | 877k | FIXP_DBL *pX = &x[i]; |
444 | | |
445 | 877k | j = 8; |
446 | 3.50M | do { |
447 | 3.50M | FIXP_DBL x0 = *pX++; |
448 | 3.50M | FIXP_DBL x1 = *pX++; |
449 | 3.50M | x0 = fPow2Div2(x0); |
450 | 3.50M | x1 = fPow2Div2(x1); |
451 | 3.50M | tmp = tmp + (x0 >> (ALFDPOW2_SCALE - 1)); |
452 | 3.50M | tmp = tmp + (x1 >> (ALFDPOW2_SCALE - 1)); |
453 | 3.50M | } while ((j = j - 2) != 0); |
454 | 877k | tmp = fMax(tmp, (FL2FX_DBL(0.01f) >> s)); |
455 | 877k | tmp_pow2[i >> 3] = tmp; |
456 | 877k | if (tmp > max) { |
457 | 224k | max = tmp; |
458 | 224k | } |
459 | 877k | } |
460 | | |
461 | | /* deemphasis of all blocks below the peak */ |
462 | 119k | fac = FL2FX_DBL(0.1f) >> 1; |
463 | 996k | for (i = 0; i < i_max; i += k) { |
464 | 877k | FIXP_DBL tmp; |
465 | 877k | INT shifti; |
466 | | |
467 | 877k | tmp = tmp_pow2[i >> 3]; |
468 | | |
469 | | /* tmp = (float)sqrt(tmp/max); */ |
470 | | |
471 | | /* value of tmp is between 8/2*max^2 and max^2 / 2. */ |
472 | | /* required shift factor of division can grow up to 27 |
473 | | (grows exponentially for values toward zero) |
474 | | thus using normalized division to assure valid result. */ |
475 | 877k | { |
476 | 877k | INT sd; |
477 | | |
478 | 877k | if (tmp != (FIXP_DBL)0) { |
479 | 867k | tmp = fDivNorm(max, tmp, &sd); |
480 | 867k | if (sd & 1) { |
481 | 524k | sd++; |
482 | 524k | tmp >>= 1; |
483 | 524k | } |
484 | 867k | } else { |
485 | 9.20k | tmp = (FIXP_DBL)MAXVAL_DBL; |
486 | 9.20k | sd = 0; |
487 | 9.20k | } |
488 | 877k | tmp = invSqrtNorm2(tmp, &shifti); |
489 | 877k | tmp = scaleValue(tmp, shifti - 1 - (sd / 2)); |
490 | 877k | } |
491 | 877k | if (tmp > fac) { |
492 | 230k | fac = tmp; |
493 | 230k | } |
494 | 877k | FIXP_DBL *pX = &x[i]; |
495 | | |
496 | 877k | j = 8; |
497 | 3.50M | do { |
498 | 3.50M | FIXP_DBL x0 = pX[0]; |
499 | 3.50M | FIXP_DBL x1 = pX[1]; |
500 | 3.50M | x0 = fMultDiv2(x0, fac); |
501 | 3.50M | x1 = fMultDiv2(x1, fac); |
502 | 3.50M | x0 = x0 << 2; |
503 | 3.50M | x1 = x1 << 2; |
504 | 3.50M | *pX++ = x0; |
505 | 3.50M | *pX++ = x1; |
506 | | |
507 | 3.50M | } while ((j = j - 2) != 0); |
508 | | /* Store gains for FAC */ |
509 | 877k | *alfd_gains++ = fac; |
510 | 877k | } |
511 | 119k | } |
512 | 119k | } |
513 | | |
514 | | /** |
515 | | * \brief Interpolated Noise Shaping for mdct coefficients. |
516 | | * This algorithm shapes temporally the spectral noise between |
517 | | * the two spectral noise represention (FDNS_NPTS of resolution). |
518 | | * The noise is shaped monotonically between the two points |
519 | | * using a curved shape to favor the lower gain in mid-frame. |
520 | | * ODFT and amplitud calculation are applied to the 2 LPC coefficients first. |
521 | | * |
522 | | * \param r pointer to spectrum data. |
523 | | * \param rms RMS of output spectrum. |
524 | | * \param lg length of r. |
525 | | * \param A1 pointer to old input LPC coefficients of length M_LP_FILTER_ORDER |
526 | | * scaled by SF_A_COEFFS. |
527 | | * \param A2 pointer to new input LPC coefficients of length M_LP_FILTER_ORDER |
528 | | * scaled by SF_A_COEFFS. |
529 | | * \param bLpc2Mdct flags control lpc2mdct conversion and noise shaping. |
530 | | * \param gainLpc1 pointer to gain based on old input LPC coefficients. |
531 | | * \param gainLpc2 pointer to gain based on new input LPC coefficients. |
532 | | * \param gLpc_e pointer to exponent of gainLpc1 and gainLpc2. |
533 | | */ |
534 | | /* static */ |
535 | 34.0M | #define NSHAPE_SCALE (4) |
536 | | |
537 | | #define LPC2MDCT_CALC (1) |
538 | | #define LPC2MDCT_GAIN_LOAD (2) |
539 | | #define LPC2MDCT_GAIN_SAVE (4) |
540 | | #define LPC2MDCT_APPLY_NSHAPE (8) |
541 | | |
542 | | void lpc2mdctAndNoiseShaping(FIXP_DBL *r, SHORT *pScale, const INT lg, |
543 | | const INT fdns_npts, const FIXP_LPC *A1, |
544 | | const INT A1_exp, const FIXP_LPC *A2, |
545 | 119k | const INT A2_exp) { |
546 | 119k | FIXP_DBL *tmp2 = NULL; |
547 | 119k | FIXP_DBL rr_minus_one; |
548 | 119k | int i, k, s, step; |
549 | | |
550 | 119k | C_AALLOC_SCRATCH_START(tmp1, FIXP_DBL, FDNS_NPTS * 8) |
551 | | |
552 | 119k | { |
553 | 119k | tmp2 = tmp1 + fdns_npts * 4; |
554 | | |
555 | | /* lpc2mdct() */ |
556 | | |
557 | | /* ODFT. E_LPC_a_weight() for A1 and A2 vectors is included into the loop |
558 | | * below. */ |
559 | 119k | FIXP_DBL f = FL2FXCONST_DBL(0.92f); |
560 | | |
561 | 119k | const FIXP_STP *SinTab; |
562 | 119k | int k_step; |
563 | | /* needed values: sin(phi), cos(phi); phi = i*PI/(2*fdns_npts), i = 0 ... |
564 | | * M_LP_FILTER_ORDER */ |
565 | 119k | switch (fdns_npts) { |
566 | 6.75k | case 64: |
567 | 6.75k | SinTab = SineTable512; |
568 | 6.75k | k_step = (512 / 64); |
569 | 6.75k | FDK_ASSERT(512 >= 64); |
570 | 6.75k | break; |
571 | 112k | case 48: |
572 | 112k | SinTab = SineTable384; |
573 | 112k | k_step = 384 / 48; |
574 | 112k | FDK_ASSERT(384 >= 48); |
575 | 112k | break; |
576 | 112k | default: |
577 | 0 | FDK_ASSERT(0); |
578 | 0 | return; |
579 | 119k | } |
580 | | |
581 | 2.02M | for (i = 0, k = k_step; i < M_LP_FILTER_ORDER; i++, k += k_step) { |
582 | 1.90M | FIXP_STP cs = SinTab[k]; |
583 | 1.90M | FIXP_DBL wA1, wA2; |
584 | | |
585 | 1.90M | wA1 = fMult(A1[i], f); |
586 | 1.90M | wA2 = fMult(A2[i], f); |
587 | | |
588 | | /* r[i] = A[i]*cos() */ |
589 | 1.90M | tmp1[2 + i * 2] = fMult(wA1, cs.v.re); |
590 | 1.90M | tmp2[2 + i * 2] = fMult(wA2, cs.v.re); |
591 | | /* i[i] = A[i]*sin() */ |
592 | 1.90M | tmp1[3 + i * 2] = -fMult(wA1, cs.v.im); |
593 | 1.90M | tmp2[3 + i * 2] = -fMult(wA2, cs.v.im); |
594 | | |
595 | 1.90M | f = fMult(f, FL2FXCONST_DBL(0.92f)); |
596 | 1.90M | } |
597 | | |
598 | | /* Guarantee at least 2 bits of headroom for the FFT */ |
599 | | /* "3" stands for 1.0 with 2 bits of headroom; (A1_exp + 2) guarantess 2 |
600 | | * bits of headroom if A1_exp > 1 */ |
601 | 119k | int A1_exp_fix = fMax(3, A1_exp + 2); |
602 | 119k | int A2_exp_fix = fMax(3, A2_exp + 2); |
603 | | |
604 | | /* Set 1.0 in the proper format */ |
605 | 119k | tmp1[0] = (FIXP_DBL)(INT)((ULONG)0x80000000 >> A1_exp_fix); |
606 | 119k | tmp2[0] = (FIXP_DBL)(INT)((ULONG)0x80000000 >> A2_exp_fix); |
607 | | |
608 | 119k | tmp1[1] = tmp2[1] = (FIXP_DBL)0; |
609 | | |
610 | | /* Clear the resto of the array */ |
611 | 119k | FDKmemclear( |
612 | 119k | tmp1 + 2 * (M_LP_FILTER_ORDER + 1), |
613 | 119k | 2 * (fdns_npts * 2 - (M_LP_FILTER_ORDER + 1)) * sizeof(FIXP_DBL)); |
614 | 119k | FDKmemclear( |
615 | 119k | tmp2 + 2 * (M_LP_FILTER_ORDER + 1), |
616 | 119k | 2 * (fdns_npts * 2 - (M_LP_FILTER_ORDER + 1)) * sizeof(FIXP_DBL)); |
617 | | |
618 | | /* Guarantee 2 bits of headroom for FFT */ |
619 | 119k | scaleValues(&tmp1[2], (2 * M_LP_FILTER_ORDER), (A1_exp - A1_exp_fix)); |
620 | 119k | scaleValues(&tmp2[2], (2 * M_LP_FILTER_ORDER), (A2_exp - A2_exp_fix)); |
621 | | |
622 | 119k | INT s2; |
623 | 119k | s = A1_exp_fix; |
624 | 119k | s2 = A2_exp_fix; |
625 | | |
626 | 119k | fft(2 * fdns_npts, tmp1, &s); |
627 | 119k | fft(2 * fdns_npts, tmp2, &s2); |
628 | | |
629 | | /* Adjust the exponents of both fft outputs if necessary*/ |
630 | 119k | if (s > s2) { |
631 | 3.60k | scaleValues(tmp2, 2 * fdns_npts, s2 - s); |
632 | 3.60k | s2 = s; |
633 | 115k | } else if (s < s2) { |
634 | 12.5k | scaleValues(tmp1, 2 * fdns_npts, s - s2); |
635 | 12.5k | s = s2; |
636 | 12.5k | } |
637 | | |
638 | 119k | FDK_ASSERT(s == s2); |
639 | 119k | } |
640 | | |
641 | | /* Get amplitude and apply gains */ |
642 | 119k | step = lg / fdns_npts; |
643 | 119k | rr_minus_one = (FIXP_DBL)0; |
644 | | |
645 | 5.95M | for (k = 0; k < fdns_npts; k++) { |
646 | 5.83M | FIXP_DBL g1, g2, inv_g1_g2, a, b; |
647 | 5.83M | INT inv_g1_g2_e; |
648 | 5.83M | int g_e, shift; |
649 | | |
650 | 5.83M | { |
651 | 5.83M | FIXP_DBL real, imag; |
652 | 5.83M | int si1, si2, sInput; |
653 | | |
654 | 5.83M | real = tmp1[k * 2]; |
655 | 5.83M | imag = tmp1[k * 2 + 1]; |
656 | 5.83M | sInput = fMax(fMin(fNorm(real), fNorm(imag)) - 1, 0); |
657 | 5.83M | real <<= sInput; |
658 | 5.83M | imag <<= sInput; |
659 | | /* g1_e = si1 - 2*s/2 */ |
660 | 5.83M | g1 = invSqrtNorm2(fPow2(real) + fPow2(imag), &si1); |
661 | 5.83M | si1 += sInput; |
662 | | |
663 | 5.83M | real = tmp2[k * 2]; |
664 | 5.83M | imag = tmp2[k * 2 + 1]; |
665 | 5.83M | sInput = fMax(fMin(fNorm(real), fNorm(imag)) - 1, 0); |
666 | 5.83M | real <<= sInput; |
667 | 5.83M | imag <<= sInput; |
668 | | /* g2_e = si2 - 2*s/2 */ |
669 | 5.83M | g2 = invSqrtNorm2(fPow2(real) + fPow2(imag), &si2); |
670 | 5.83M | si2 += sInput; |
671 | | |
672 | | /* Pick a common scale factor for g1 and g2 */ |
673 | 5.83M | if (si1 > si2) { |
674 | 1.50M | g2 >>= si1 - si2; |
675 | 1.50M | g_e = si1 - s; |
676 | 4.32M | } else { |
677 | 4.32M | g1 >>= si2 - si1; |
678 | 4.32M | g_e = si2 - s; |
679 | 4.32M | } |
680 | 5.83M | } |
681 | | |
682 | | /* end of lpc2mdct() */ |
683 | | |
684 | 5.83M | FDK_ASSERT(g1 >= (FIXP_DBL)0); |
685 | 5.83M | FDK_ASSERT(g2 >= (FIXP_DBL)0); |
686 | | |
687 | | /* mdct_IntNoiseShaping() */ |
688 | 5.83M | { |
689 | | /* inv_g1_g2 * 2^inv_g1_g2_e = 1/(g1+g2) */ |
690 | 5.83M | inv_g1_g2 = (g1 >> 1) + (g2 >> 1); |
691 | 5.83M | if (inv_g1_g2 != (FIXP_DBL)0) { |
692 | 5.83M | inv_g1_g2 = fDivNorm(FL2FXCONST_DBL(0.5f), inv_g1_g2, &inv_g1_g2_e); |
693 | 5.83M | inv_g1_g2_e = inv_g1_g2_e - g_e; |
694 | 5.83M | } else { |
695 | 0 | inv_g1_g2 = (FIXP_DBL)MAXVAL_DBL; |
696 | 0 | inv_g1_g2_e = 0; |
697 | 0 | } |
698 | | |
699 | 5.83M | if (g_e < 0) { |
700 | | /* a_e = g_e + inv_g1_g2_e + 1 */ |
701 | 948k | a = scaleValue(fMult(fMult(g1, g2), inv_g1_g2), g_e); |
702 | | /* b_e = g_e + inv_g1_g2_e */ |
703 | 948k | b = fMult(g2 - g1, inv_g1_g2); |
704 | 948k | shift = g_e + inv_g1_g2_e + 1 - NSHAPE_SCALE; |
705 | 4.88M | } else { |
706 | | /* a_e = (g_e+g_e) + inv_g1_g2_e + 1 */ |
707 | 4.88M | a = fMult(fMult(g1, g2), inv_g1_g2); |
708 | | /* b_e = (g_e+g_e) + inv_g1_g2_e */ |
709 | 4.88M | b = scaleValue(fMult(g2 - g1, inv_g1_g2), -g_e); |
710 | 4.88M | shift = (g_e + g_e) + inv_g1_g2_e + 1 - NSHAPE_SCALE; |
711 | 4.88M | } |
712 | | |
713 | 33.9M | for (i = k * step; i < (k + 1) * step; i++) { |
714 | 28.0M | FIXP_DBL tmp; |
715 | | |
716 | | /* rr[i] = 2*a*r[i] + b*rr[i-1] */ |
717 | 28.0M | tmp = fMult(a, r[i]); |
718 | 28.0M | tmp += scaleValue(fMultDiv2(b, rr_minus_one), NSHAPE_SCALE); |
719 | 28.0M | tmp = scaleValueSaturate(tmp, shift); |
720 | 28.0M | rr_minus_one = tmp; |
721 | 28.0M | r[i] = tmp; |
722 | 28.0M | } |
723 | 5.83M | } |
724 | 5.83M | } |
725 | | |
726 | | /* end of mdct_IntNoiseShaping() */ |
727 | 119k | { *pScale += NSHAPE_SCALE; } |
728 | | |
729 | 119k | C_AALLOC_SCRATCH_END(tmp1, FIXP_DBL, FDNS_NPTS * 8) |
730 | 119k | } |
731 | | |
732 | | /** |
733 | | * \brief Calculates the energy. |
734 | | * \param r pointer to spectrum. |
735 | | * \param rs scale factor of spectrum r. |
736 | | * \param lg frame length in audio samples. |
737 | | * \param rms_e pointer to exponent of energy value. |
738 | | * \return mantissa of energy value. |
739 | | */ |
740 | | static FIXP_DBL calcEnergy(const FIXP_DBL *r, const SHORT rs, const INT lg, |
741 | 119k | INT *rms_e) { |
742 | 119k | int headroom = getScalefactor(r, lg); |
743 | | |
744 | 119k | FIXP_DBL rms_m = 0; |
745 | | |
746 | | /* Calculate number of growth bits due to addition */ |
747 | 119k | INT shift = (INT)(fNormz((FIXP_DBL)lg)); |
748 | 119k | shift = 31 - shift; |
749 | | |
750 | | /* Generate 1e-2 in Q-6.37 */ |
751 | 119k | const FIXP_DBL value0_01 = 0x51eb851e; |
752 | 119k | const INT value0_01_exp = -6; |
753 | | |
754 | | /* Find the exponent of the resulting energy value */ |
755 | 119k | *rms_e = ((rs - headroom) << 1) + shift + 1; |
756 | | |
757 | 119k | INT delta = *rms_e - value0_01_exp; |
758 | 119k | if (delta > 0) { |
759 | | /* Limit shift_to 31*/ |
760 | 119k | delta = fMin(31, delta); |
761 | 119k | rms_m = value0_01 >> delta; |
762 | 119k | } else { |
763 | 0 | rms_m = value0_01; |
764 | 0 | *rms_e = value0_01_exp; |
765 | 0 | shift = shift - delta; |
766 | | /* Limit shift_to 31*/ |
767 | 0 | shift = fMin(31, shift); |
768 | 0 | } |
769 | | |
770 | 28.1M | for (int i = 0; i < lg; i++) { |
771 | 28.0M | rms_m += fPow2Div2(r[i] << headroom) >> shift; |
772 | 28.0M | } |
773 | | |
774 | 119k | return rms_m; |
775 | 119k | } |
776 | | |
777 | | /** |
778 | | * \brief TCX gain calculation. |
779 | | * \param pAacDecoderChannelInfo channel context data. |
780 | | * \param r output spectrum. |
781 | | * \param rms_e pointer to mantissa of energy value. |
782 | | * \param rms_e pointer to exponent of energy value. |
783 | | * \param frame the frame index of the LPD super frame. |
784 | | * \param lg the frame length in audio samples. |
785 | | * \param gain_m pointer to mantissa of TCX gain. |
786 | | * \param gain_e pointer to exponent of TCX gain. |
787 | | * \param elFlags element specific parser guidance flags. |
788 | | * \param lg_fb the fullband frame length in audio samples. |
789 | | * \param IGF_bgn the IGF start index. |
790 | | */ |
791 | | static void calcTCXGain(CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
792 | | FIXP_DBL *r, FIXP_DBL rms_m, INT rms_e, const INT frame, |
793 | 119k | const INT lg) { |
794 | 119k | if ((rms_m != (FIXP_DBL)0)) { |
795 | 119k | FIXP_DBL tcx_gain_m; |
796 | 119k | INT tcx_gain_e; |
797 | | |
798 | 119k | CLpd_DecodeGain(&tcx_gain_m, &tcx_gain_e, |
799 | 119k | pAacDecoderChannelInfo->pDynData->specificTo.usac |
800 | 119k | .tcx_global_gain[frame]); |
801 | | |
802 | | /* rms * 2^rms_e = lg/sqrt(sum(spec^2)) */ |
803 | 119k | if (rms_e & 1) { |
804 | 21.5k | rms_m >>= 1; |
805 | 21.5k | rms_e++; |
806 | 21.5k | } |
807 | | |
808 | 119k | { |
809 | 119k | FIXP_DBL fx_lg; |
810 | 119k | INT fx_lg_e, s; |
811 | 119k | INT inv_e; |
812 | | |
813 | | /* lg = fx_lg * 2^fx_lg_e */ |
814 | 119k | s = fNorm((FIXP_DBL)lg); |
815 | 119k | fx_lg = (FIXP_DBL)lg << s; |
816 | 119k | fx_lg_e = DFRACT_BITS - 1 - s; |
817 | | /* 1/sqrt(rms) */ |
818 | 119k | rms_m = invSqrtNorm2(rms_m, &inv_e); |
819 | 119k | rms_m = fMult(rms_m, fx_lg); |
820 | 119k | rms_e = inv_e - (rms_e >> 1) + fx_lg_e; |
821 | 119k | } |
822 | | |
823 | 119k | { |
824 | 119k | int s = fNorm(tcx_gain_m); |
825 | 119k | tcx_gain_m = tcx_gain_m << s; |
826 | 119k | tcx_gain_e -= s; |
827 | 119k | } |
828 | | |
829 | 119k | tcx_gain_m = fMultDiv2(tcx_gain_m, rms_m); |
830 | 119k | tcx_gain_e = tcx_gain_e + rms_e; |
831 | | |
832 | | /* global_gain * 2^(global_gain_e+rms_e) = (10^(global_gain/28)) * rms * |
833 | | * 2^rms_e */ |
834 | 119k | { |
835 | 119k | { tcx_gain_e += 1; } |
836 | 119k | } |
837 | | |
838 | 119k | pAacDecoderChannelInfo->data.usac.tcx_gain[frame] = tcx_gain_m; |
839 | 119k | pAacDecoderChannelInfo->data.usac.tcx_gain_e[frame] = tcx_gain_e; |
840 | | |
841 | 119k | pAacDecoderChannelInfo->specScale[frame] += tcx_gain_e; |
842 | 119k | } |
843 | 119k | } |
844 | | |
845 | | /** |
846 | | * \brief FDNS decoding. |
847 | | * \param pAacDecoderChannelInfo channel context data. |
848 | | * \param pAacDecoderStaticChannelInfo channel context static data. |
849 | | * \param r output spectrum. |
850 | | * \param lg the frame length in audio samples. |
851 | | * \param frame the frame index of the LPD super frame. |
852 | | * \param pScale pointer to current scale shift factor of r[]. |
853 | | * \param A1 old input LPC coefficients of length M_LP_FILTER_ORDER. |
854 | | * \param A2 new input LPC coefficients of length M_LP_FILTER_ORDER. |
855 | | * \param pAlfdGains pointer for ALFD gains output scaled by 1. |
856 | | * \param fdns_npts number of lines (FDNS_NPTS). |
857 | | * \param inf_mask pointer to noise mask. |
858 | | * \param IGF_win_mode IGF window mode (LONG, SHORT, TCX10, TCX20). |
859 | | * \param frameType (IGF_FRAME_DIVISION_AAC_OR_TCX_LONG or |
860 | | * IGF_FRAME_DIVISION_TCX_SHORT_1). |
861 | | * \param elFlags element specific parser guidance flags. |
862 | | * \param lg_fb the fullband frame length in audio samples. |
863 | | * \param IGF_bgn the IGF start index. |
864 | | * \param rms_m mantisse of energy. |
865 | | * \param rms_e exponent of energy. |
866 | | */ |
867 | | /* static */ |
868 | | void CLpd_FdnsDecode(CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
869 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
870 | | FIXP_DBL r[], const INT lg, const INT frame, SHORT *pScale, |
871 | | const FIXP_LPC A1[M_LP_FILTER_ORDER], const INT A1_exp, |
872 | | const FIXP_LPC A2[M_LP_FILTER_ORDER], const INT A2_exp, |
873 | 119k | FIXP_DBL pAlfdGains[LFAC / 4], const INT fdns_npts) { |
874 | | /* Weight LPC coefficients using Rm values */ |
875 | 119k | CLpd_AdaptLowFreqDeemph(r, lg, pAlfdGains, *pScale); |
876 | | |
877 | 119k | FIXP_DBL rms_m = (FIXP_DBL)0; |
878 | 119k | INT rms_e = 0; |
879 | 119k | { |
880 | | /* Calculate Energy */ |
881 | 119k | rms_m = calcEnergy(r, *pScale, lg, &rms_e); |
882 | 119k | } |
883 | | |
884 | 119k | calcTCXGain(pAacDecoderChannelInfo, r, rms_m, rms_e, frame, lg); |
885 | | |
886 | | /* Apply ODFT and Noise Shaping. LP coefficient (A1, A2) weighting is done |
887 | | * inside on the fly. */ |
888 | | |
889 | 119k | lpc2mdctAndNoiseShaping(r, pScale, lg, fdns_npts, A1, A1_exp, A2, A2_exp); |
890 | 119k | } |
891 | | |
892 | | /** |
893 | | * find pitch for TCX20 (time domain) concealment. |
894 | | */ |
895 | 119k | static int find_mpitch(FIXP_DBL xri[], int lg) { |
896 | 119k | FIXP_DBL max, pitch; |
897 | 119k | INT pitch_e; |
898 | 119k | int i, n; |
899 | | |
900 | 119k | max = (FIXP_DBL)0; |
901 | 119k | n = 2; |
902 | | |
903 | | /* find maximum below 400Hz */ |
904 | 877k | for (i = 2; i < (lg >> 4); i += 2) { |
905 | 757k | FIXP_DBL tmp = fPow2Div2(xri[i]) + fPow2Div2(xri[i + 1]); |
906 | 757k | if (tmp > max) { |
907 | 150k | max = tmp; |
908 | 150k | n = i; |
909 | 150k | } |
910 | 757k | } |
911 | | |
912 | | // pitch = ((float)lg<<1)/(float)n; |
913 | 119k | pitch = fDivNorm((FIXP_DBL)lg << 1, (FIXP_DBL)n, &pitch_e); |
914 | 119k | pitch >>= fixMax(0, DFRACT_BITS - 1 - pitch_e - 16); |
915 | | |
916 | | /* find pitch multiple under 20ms */ |
917 | 119k | if (pitch >= (FIXP_DBL)((256 << 16) - 1)) { /*231.0f*/ |
918 | 9.33k | n = 256; |
919 | 109k | } else { |
920 | 109k | FIXP_DBL mpitch = pitch; |
921 | 364k | while (mpitch < (FIXP_DBL)(255 << 16)) { |
922 | 254k | mpitch += pitch; |
923 | 254k | } |
924 | 109k | n = (int)(mpitch - pitch) >> 16; |
925 | 109k | } |
926 | | |
927 | 119k | return (n); |
928 | 119k | } |
929 | | |
930 | | /** |
931 | | * number of spectral coefficients / time domain samples using frame mode as |
932 | | * index. |
933 | | */ |
934 | | static const int lg_table_ccfl[2][4] = { |
935 | | {256, 256, 512, 1024}, /* coreCoderFrameLength = 1024 */ |
936 | | {192, 192, 384, 768} /* coreCoderFrameLength = 768 */ |
937 | | }; |
938 | | |
939 | | /** |
940 | | * \brief Decode and render one MDCT-TCX frame. |
941 | | * \param pAacDecoderChannelInfo channel context data. |
942 | | * \param lg the frame length in audio samples. |
943 | | * \param frame the frame index of the LPD super frame. |
944 | | */ |
945 | | static void CLpd_TcxDecode( |
946 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
947 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, UINT flags, |
948 | 119k | int mod, int last_mod, int frame, int frameOk) { |
949 | 119k | FIXP_DBL *pAlfd_gains = pAacDecoderStaticChannelInfo->last_alfd_gains; |
950 | 119k | ULONG *pSeed = &pAacDecoderStaticChannelInfo->nfRandomSeed; |
951 | 119k | int lg = (pAacDecoderChannelInfo->granuleLength == 128) |
952 | 119k | ? lg_table_ccfl[0][mod + 0] |
953 | 119k | : lg_table_ccfl[1][mod + 0]; |
954 | 119k | int next_frame = frame + (1 << (mod - 1)); |
955 | 119k | int isFullBandLpd = 0; |
956 | | |
957 | | /* Obtain r[] vector by combining the quant[] and noise[] vectors */ |
958 | 119k | { |
959 | 119k | FIXP_DBL noise_level; |
960 | 119k | FIXP_DBL *coeffs = |
961 | 119k | SPEC_TCX(pAacDecoderChannelInfo->pSpectralCoefficient, frame, |
962 | 119k | pAacDecoderChannelInfo->granuleLength, isFullBandLpd); |
963 | 119k | int scale = pAacDecoderChannelInfo->specScale[frame]; |
964 | 119k | int i, nfBgn, nfEnd; |
965 | 119k | UCHAR tcx_noise_factor = pAacDecoderChannelInfo->pDynData->specificTo.usac |
966 | 119k | .tcx_noise_factor[frame]; |
967 | | |
968 | | /* find pitch for bfi case */ |
969 | 119k | pAacDecoderStaticChannelInfo->last_tcx_pitch = find_mpitch(coeffs, lg); |
970 | | |
971 | 119k | if (frameOk) { |
972 | | /* store for concealment */ |
973 | 119k | pAacDecoderStaticChannelInfo->last_tcx_noise_factor = tcx_noise_factor; |
974 | 119k | } else { |
975 | | /* restore last frames value */ |
976 | 0 | tcx_noise_factor = pAacDecoderStaticChannelInfo->last_tcx_noise_factor; |
977 | 0 | } |
978 | | |
979 | 119k | noise_level = |
980 | 119k | (FIXP_DBL)((LONG)FL2FXCONST_DBL(0.0625f) * (8 - tcx_noise_factor)); |
981 | 119k | noise_level = scaleValue(noise_level, -scale); |
982 | | |
983 | 119k | const FIXP_DBL neg_noise_level = -noise_level; |
984 | | |
985 | 119k | { |
986 | 119k | nfBgn = lg / 6; |
987 | 119k | nfEnd = lg; |
988 | 119k | } |
989 | | |
990 | 3.03M | for (i = nfBgn; i < nfEnd - 7; i += 8) { |
991 | 2.91M | LONG tmp; |
992 | | |
993 | | /* Fill all 8 consecutive zero coeffs with noise */ |
994 | 2.91M | tmp = coeffs[i + 0] | coeffs[i + 1] | coeffs[i + 2] | coeffs[i + 3] | |
995 | 2.91M | coeffs[i + 4] | coeffs[i + 5] | coeffs[i + 6] | coeffs[i + 7]; |
996 | | |
997 | 2.91M | if (tmp == 0) { |
998 | 12.8M | for (int k = i; k < i + 8; k++) { |
999 | 11.4M | UsacRandomSign(pSeed) ? (coeffs[k] = neg_noise_level) |
1000 | 11.4M | : (coeffs[k] = noise_level); |
1001 | 11.4M | } |
1002 | 1.42M | } |
1003 | 2.91M | } |
1004 | 119k | if ((nfEnd - i) > |
1005 | 119k | 0) { /* noise filling for last "band" with less than 8 bins */ |
1006 | 6.75k | LONG tmp = (LONG)coeffs[i]; |
1007 | 6.75k | int k; |
1008 | | |
1009 | 6.75k | FDK_ASSERT((nfEnd - i) < 8); |
1010 | 36.4k | for (k = 1; k < (nfEnd - i); k++) { |
1011 | 29.7k | tmp |= (LONG)coeffs[i + k]; |
1012 | 29.7k | } |
1013 | 6.75k | if (tmp == 0) { |
1014 | 35.5k | for (k = i; k < nfEnd; k++) { |
1015 | 29.8k | UsacRandomSign(pSeed) ? (coeffs[k] = neg_noise_level) |
1016 | 29.8k | : (coeffs[k] = noise_level); |
1017 | 29.8k | } |
1018 | 5.64k | } |
1019 | 6.75k | } |
1020 | 119k | } |
1021 | | |
1022 | 119k | { |
1023 | | /* Convert LPC to LP domain */ |
1024 | 119k | if (last_mod == 0) { |
1025 | | /* Note: The case where last_mod == 255 is handled by other means |
1026 | | * in CLpdChannelStream_Read() */ |
1027 | 40.2k | E_LPC_f_lsp_a_conversion( |
1028 | 40.2k | pAacDecoderChannelInfo->data.usac.lsp_coeff[frame], |
1029 | 40.2k | pAacDecoderChannelInfo->data.usac.lp_coeff[frame], |
1030 | 40.2k | &pAacDecoderChannelInfo->data.usac.lp_coeff_exp[frame]); |
1031 | 40.2k | } |
1032 | | |
1033 | 119k | E_LPC_f_lsp_a_conversion( |
1034 | 119k | pAacDecoderChannelInfo->data.usac.lsp_coeff[next_frame], |
1035 | 119k | pAacDecoderChannelInfo->data.usac.lp_coeff[next_frame], |
1036 | 119k | &pAacDecoderChannelInfo->data.usac.lp_coeff_exp[next_frame]); |
1037 | | |
1038 | | /* FDNS decoding */ |
1039 | 119k | CLpd_FdnsDecode( |
1040 | 119k | pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo, |
1041 | 119k | SPEC_TCX(pAacDecoderChannelInfo->pSpectralCoefficient, frame, |
1042 | 119k | pAacDecoderChannelInfo->granuleLength, isFullBandLpd), |
1043 | 119k | lg, frame, pAacDecoderChannelInfo->specScale + frame, |
1044 | 119k | pAacDecoderChannelInfo->data.usac.lp_coeff[frame], |
1045 | 119k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[frame], |
1046 | 119k | pAacDecoderChannelInfo->data.usac.lp_coeff[next_frame], |
1047 | 119k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[next_frame], pAlfd_gains, |
1048 | 119k | pAacDecoderChannelInfo->granuleLength / 2 /* == FDNS_NPTS(ccfl) */ |
1049 | 119k | ); |
1050 | 119k | } |
1051 | 119k | } |
1052 | | |
1053 | | /** |
1054 | | * \brief Read the tcx_coding bitstream part |
1055 | | * \param hBs bitstream handle to read from. |
1056 | | * \param pAacDecoderChannelInfo channel context info to store data into. |
1057 | | * \param lg the frame length in audio samples. |
1058 | | * \param first_tcx_flag flag indicating that this is the first TCX frame. |
1059 | | * \param frame the frame index of the LPD super frame. |
1060 | | */ |
1061 | | static AAC_DECODER_ERROR CLpd_TCX_Read( |
1062 | | HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
1063 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, int lg, |
1064 | 397k | int first_tcx_flag, int frame, UINT flags) { |
1065 | 397k | AAC_DECODER_ERROR errorAAC = AAC_DEC_OK; |
1066 | 397k | ARITH_CODING_ERROR error = ARITH_CODER_OK; |
1067 | 397k | FIXP_DBL *pSpec; |
1068 | 397k | int arith_reset_flag = 0; |
1069 | 397k | int isFullBandLpd = 0; |
1070 | | |
1071 | 397k | pSpec = SPEC_TCX(pAacDecoderChannelInfo->pSpectralCoefficient, frame, |
1072 | 397k | pAacDecoderChannelInfo->granuleLength, isFullBandLpd); |
1073 | | |
1074 | | /* TCX noise level */ |
1075 | 397k | { |
1076 | 397k | pAacDecoderChannelInfo->pDynData->specificTo.usac.tcx_noise_factor[frame] = |
1077 | 397k | FDKreadBits(hBs, 3); |
1078 | 397k | } |
1079 | | /* TCX global gain */ |
1080 | 397k | pAacDecoderChannelInfo->pDynData->specificTo.usac.tcx_global_gain[frame] = |
1081 | 397k | FDKreadBits(hBs, 7); |
1082 | | |
1083 | | /* Arithmetic coded residual/spectrum */ |
1084 | 397k | if (first_tcx_flag) { |
1085 | 284k | if (flags & AC_INDEP) { |
1086 | 242k | arith_reset_flag = 1; |
1087 | 242k | } else { |
1088 | 42.1k | arith_reset_flag = FDKreadBits(hBs, 1); |
1089 | 42.1k | } |
1090 | 284k | } |
1091 | | |
1092 | | /* CArco_DecodeArithData() output scale of "pSpec" is DFRACT_BITS-1 */ |
1093 | 397k | error = CArco_DecodeArithData(pAacDecoderStaticChannelInfo->hArCo, hBs, pSpec, |
1094 | 397k | lg, lg, arith_reset_flag); |
1095 | | |
1096 | | /* Rescale residual/spectrum */ |
1097 | 397k | { |
1098 | 397k | int scale = getScalefactor(pSpec, lg) - 2; /* Leave 2 bits headroom */ |
1099 | | |
1100 | | /* Exponent of CArco_DecodeArithData() output is DFRACT_BITS; integer |
1101 | | * values. */ |
1102 | 397k | scaleValues(pSpec, lg, scale); |
1103 | 397k | scale = DFRACT_BITS - 1 - scale; |
1104 | | |
1105 | 397k | pAacDecoderChannelInfo->specScale[frame] = scale; |
1106 | 397k | } |
1107 | | |
1108 | 397k | if (error == ARITH_CODER_ERROR) errorAAC = AAC_DEC_UNKNOWN; |
1109 | | |
1110 | 397k | return errorAAC; |
1111 | 397k | } |
1112 | | |
1113 | | /** |
1114 | | * \brief translate lpd_mode into the mod[] array which describes the mode of |
1115 | | * each each LPD frame |
1116 | | * \param mod[] the array that will be filled with the mode indexes of the |
1117 | | * inidividual frames. |
1118 | | * \param lpd_mode the lpd_mode field read from the lpd_channel_stream |
1119 | | */ |
1120 | | static AAC_DECODER_ERROR CLpd_ReadAndMapLpdModeToModArray( |
1121 | 944k | UCHAR mod[4], HANDLE_FDK_BITSTREAM hBs, UINT elFlags) { |
1122 | 944k | int lpd_mode; |
1123 | | |
1124 | 944k | { |
1125 | 944k | lpd_mode = FDKreadBits(hBs, 5); |
1126 | | |
1127 | 944k | if (lpd_mode > 25 || lpd_mode < 0) { |
1128 | 620k | return AAC_DEC_PARSE_ERROR; |
1129 | 620k | } |
1130 | | |
1131 | 324k | switch (lpd_mode) { |
1132 | 5.92k | case 25: |
1133 | | /* 1 80MS frame */ |
1134 | 5.92k | mod[0] = mod[1] = mod[2] = mod[3] = 3; |
1135 | 5.92k | break; |
1136 | 1.00k | case 24: |
1137 | | /* 2 40MS frames */ |
1138 | 1.00k | mod[0] = mod[1] = mod[2] = mod[3] = 2; |
1139 | 1.00k | break; |
1140 | 317k | default: |
1141 | 317k | switch (lpd_mode >> 2) { |
1142 | 52.3k | case 4: |
1143 | | /* lpd_mode 19 - 16 => 1 40MS and 2 20MS frames */ |
1144 | 52.3k | mod[0] = mod[1] = 2; |
1145 | 52.3k | mod[2] = (lpd_mode & 1) ? 1 : 0; |
1146 | 52.3k | mod[3] = (lpd_mode & 2) ? 1 : 0; |
1147 | 52.3k | break; |
1148 | 68.5k | case 5: |
1149 | | /* lpd_mode 23 - 20 => 2 20MS and 1 40MS frames */ |
1150 | 68.5k | mod[2] = mod[3] = 2; |
1151 | 68.5k | mod[0] = (lpd_mode & 1) ? 1 : 0; |
1152 | 68.5k | mod[1] = (lpd_mode & 2) ? 1 : 0; |
1153 | 68.5k | break; |
1154 | 196k | default: |
1155 | | /* lpd_mode < 16 => 4 20MS frames */ |
1156 | 196k | mod[0] = (lpd_mode & 1) ? 1 : 0; |
1157 | 196k | mod[1] = (lpd_mode & 2) ? 1 : 0; |
1158 | 196k | mod[2] = (lpd_mode & 4) ? 1 : 0; |
1159 | 196k | mod[3] = (lpd_mode & 8) ? 1 : 0; |
1160 | 196k | break; |
1161 | 317k | } |
1162 | 317k | break; |
1163 | 324k | } |
1164 | 324k | } |
1165 | 324k | return AAC_DEC_OK; |
1166 | 324k | } |
1167 | | |
1168 | | static void CLpd_Reset( |
1169 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
1170 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
1171 | 188k | int keep_past_signal) { |
1172 | 188k | int i; |
1173 | | |
1174 | | /* Reset TCX / ACELP common memory */ |
1175 | 188k | if (!keep_past_signal) { |
1176 | 180k | FDKmemclear(pAacDecoderStaticChannelInfo->old_synth, |
1177 | 180k | sizeof(pAacDecoderStaticChannelInfo->old_synth)); |
1178 | 180k | } |
1179 | | |
1180 | | /* Initialize the LSFs */ |
1181 | 3.20M | for (i = 0; i < M_LP_FILTER_ORDER; i++) { |
1182 | 3.01M | pAacDecoderStaticChannelInfo->lpc4_lsf[i] = fdk_dec_lsf_init[i]; |
1183 | 3.01M | } |
1184 | | |
1185 | | /* Reset memory needed by bass post-filter */ |
1186 | 188k | FDKmemclear(pAacDecoderStaticChannelInfo->mem_bpf, |
1187 | 188k | sizeof(pAacDecoderStaticChannelInfo->mem_bpf)); |
1188 | | |
1189 | 188k | pAacDecoderStaticChannelInfo->old_bpf_control_info = 0; |
1190 | 1.50M | for (i = 0; i < SYN_SFD; i++) { |
1191 | 1.32M | pAacDecoderStaticChannelInfo->old_T_pf[i] = 64; |
1192 | 1.32M | pAacDecoderStaticChannelInfo->old_gain_pf[i] = (FIXP_DBL)0; |
1193 | 1.32M | } |
1194 | | |
1195 | | /* Reset ACELP memory */ |
1196 | 188k | CLpd_AcelpReset(&pAacDecoderStaticChannelInfo->acelp); |
1197 | | |
1198 | 188k | pAacDecoderStaticChannelInfo->last_lpc_lost = 0; /* prev_lpc_lost */ |
1199 | 188k | pAacDecoderStaticChannelInfo->last_tcx_pitch = L_DIV; /* pitch_tcx */ |
1200 | 188k | pAacDecoderStaticChannelInfo->numLostLpdFrames = 0; /* nbLostCmpt */ |
1201 | 188k | } |
1202 | | |
1203 | | /* |
1204 | | * Externally visible functions |
1205 | | */ |
1206 | | |
1207 | | AAC_DECODER_ERROR CLpdChannelStream_Read( |
1208 | | HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
1209 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
1210 | 989k | const SamplingRateInfo *pSamplingRateInfo, UINT flags) { |
1211 | 989k | AAC_DECODER_ERROR error = AAC_DEC_OK; |
1212 | 989k | int first_tcx_flag; |
1213 | 989k | int k, nbDiv, fFacDataPresent, first_lpd_flag, acelp_core_mode, |
1214 | 989k | facGetMemState = 0; |
1215 | 989k | UCHAR *mod = pAacDecoderChannelInfo->data.usac.mod; |
1216 | 989k | int lpd_mode_last, prev_frame_was_lpd; |
1217 | 989k | USAC_COREMODE core_mode_last; |
1218 | 989k | const int lg_table_offset = 0; |
1219 | 989k | const int *lg_table = (pAacDecoderChannelInfo->granuleLength == 128) |
1220 | 989k | ? &lg_table_ccfl[0][lg_table_offset] |
1221 | 989k | : &lg_table_ccfl[1][lg_table_offset]; |
1222 | 989k | int last_lpc_lost = pAacDecoderStaticChannelInfo->last_lpc_lost; |
1223 | | |
1224 | 989k | int last_frame_ok = CConcealment_GetLastFrameOk( |
1225 | 989k | &pAacDecoderStaticChannelInfo->concealmentInfo, 1); |
1226 | | |
1227 | 989k | INT i_offset; |
1228 | 989k | UINT samplingRate; |
1229 | | |
1230 | 989k | samplingRate = pSamplingRateInfo->samplingRate; |
1231 | | |
1232 | 989k | i_offset = |
1233 | 989k | (INT)(samplingRate * PIT_MIN_12k8 + (FSCALE_DENOM / 2)) / FSCALE_DENOM - |
1234 | 989k | (INT)PIT_MIN_12k8; |
1235 | | |
1236 | 989k | if ((samplingRate < FAC_FSCALE_MIN) || (samplingRate > FAC_FSCALE_MAX)) { |
1237 | 44.8k | error = AAC_DEC_PARSE_ERROR; |
1238 | 44.8k | goto bail; |
1239 | 44.8k | } |
1240 | | |
1241 | 944k | acelp_core_mode = FDKreadBits(hBs, 3); |
1242 | | |
1243 | | /* lpd_mode */ |
1244 | 944k | error = CLpd_ReadAndMapLpdModeToModArray(mod, hBs, 0); |
1245 | 944k | if (error != AAC_DEC_OK) { |
1246 | 620k | goto bail; |
1247 | 620k | } |
1248 | | |
1249 | | /* bpf_control_info */ |
1250 | 324k | pAacDecoderChannelInfo->data.usac.bpf_control_info = FDKreadBit(hBs); |
1251 | | |
1252 | | /* last_core_mode */ |
1253 | 324k | prev_frame_was_lpd = FDKreadBit(hBs); |
1254 | | /* fac_data_present */ |
1255 | 324k | fFacDataPresent = FDKreadBit(hBs); |
1256 | | |
1257 | | /* Set valid values from |
1258 | | * pAacDecoderStaticChannelInfo->{last_core_mode,last_lpd_mode} */ |
1259 | 324k | pAacDecoderChannelInfo->data.usac.core_mode_last = |
1260 | 324k | pAacDecoderStaticChannelInfo->last_core_mode; |
1261 | 324k | lpd_mode_last = pAacDecoderChannelInfo->data.usac.lpd_mode_last = |
1262 | 324k | pAacDecoderStaticChannelInfo->last_lpd_mode; |
1263 | | |
1264 | 324k | if (prev_frame_was_lpd == 0) { |
1265 | | /* Last frame was FD */ |
1266 | 216k | pAacDecoderChannelInfo->data.usac.core_mode_last = FD_LONG; |
1267 | 216k | pAacDecoderChannelInfo->data.usac.lpd_mode_last = 255; |
1268 | 216k | } else { |
1269 | | /* Last frame was LPD */ |
1270 | 107k | pAacDecoderChannelInfo->data.usac.core_mode_last = LPD; |
1271 | 107k | if (((mod[0] == 0) && fFacDataPresent) || |
1272 | 107k | ((mod[0] != 0) && !fFacDataPresent)) { |
1273 | | /* Currend mod is ACELP, fac data present -> TCX, current mod TCX, no fac |
1274 | | * data -> TCX */ |
1275 | 30.0k | if (lpd_mode_last == 0) { |
1276 | | /* Bit stream interruption detected. Assume last TCX mode as TCX20. */ |
1277 | 5.02k | pAacDecoderChannelInfo->data.usac.lpd_mode_last = 1; |
1278 | 5.02k | } |
1279 | | /* Else assume that remembered TCX mode is correct. */ |
1280 | 77.3k | } else { |
1281 | 77.3k | pAacDecoderChannelInfo->data.usac.lpd_mode_last = 0; |
1282 | 77.3k | } |
1283 | 107k | } |
1284 | | |
1285 | 324k | first_lpd_flag = (pAacDecoderChannelInfo->data.usac.core_mode_last != |
1286 | 324k | LPD); /* Depends on bitstream configuration */ |
1287 | 324k | first_tcx_flag = 1; |
1288 | | |
1289 | 324k | if (pAacDecoderStaticChannelInfo->last_core_mode != |
1290 | 324k | LPD) { /* ATTENTION: Reset depends on what we rendered before! */ |
1291 | 180k | CLpd_Reset(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo, 0); |
1292 | | |
1293 | 180k | if (!last_frame_ok) { |
1294 | | /* If last rendered frame was not LPD and first lpd flag is not set, this |
1295 | | * must be an error - set last_lpc_lost flag */ |
1296 | 90.8k | last_lpc_lost |= (first_lpd_flag) ? 0 : 1; |
1297 | 90.8k | } |
1298 | 180k | } |
1299 | | |
1300 | 324k | core_mode_last = pAacDecoderChannelInfo->data.usac.core_mode_last; |
1301 | 324k | lpd_mode_last = pAacDecoderChannelInfo->data.usac.lpd_mode_last; |
1302 | | |
1303 | 324k | nbDiv = NB_DIV; |
1304 | | |
1305 | | /* k is the frame index. If a frame is of size 40MS or 80MS, |
1306 | | this frame index is incremented 2 or 4 instead of 1 respectively. */ |
1307 | | |
1308 | 324k | k = 0; |
1309 | 995k | while (k < nbDiv) { |
1310 | | /* Reset FAC data pointers in order to avoid applying old random FAC data. |
1311 | | */ |
1312 | 866k | pAacDecoderChannelInfo->data.usac.fac_data[k] = NULL; |
1313 | | |
1314 | 866k | if ((k == 0 && core_mode_last == LPD && fFacDataPresent) || |
1315 | 866k | (lpd_mode_last == 0 && mod[k] > 0) || |
1316 | 866k | ((lpd_mode_last != 255) && lpd_mode_last > 0 && mod[k] == 0)) { |
1317 | 330k | int err; |
1318 | | |
1319 | | /* Assign FAC memory */ |
1320 | 330k | pAacDecoderChannelInfo->data.usac.fac_data[k] = |
1321 | 330k | CLpd_FAC_GetMemory(pAacDecoderChannelInfo, mod, &facGetMemState); |
1322 | | |
1323 | | /* FAC for (ACELP -> TCX) or (TCX -> ACELP) */ |
1324 | 330k | err = CLpd_FAC_Read( |
1325 | 330k | hBs, pAacDecoderChannelInfo->data.usac.fac_data[k], |
1326 | 330k | pAacDecoderChannelInfo->data.usac.fac_data_e, |
1327 | 330k | pAacDecoderChannelInfo->granuleLength, /* == fac_length */ |
1328 | 330k | 0, k); |
1329 | 330k | if (err != 0) { |
1330 | 4.38k | error = AAC_DEC_PARSE_ERROR; |
1331 | 4.38k | goto bail; |
1332 | 4.38k | } |
1333 | 330k | } |
1334 | | |
1335 | 861k | if (mod[k] == 0) /* acelp-mode */ |
1336 | 464k | { |
1337 | 464k | int err; |
1338 | 464k | err = CLpd_AcelpRead( |
1339 | 464k | hBs, &pAacDecoderChannelInfo->data.usac.acelp[k], acelp_core_mode, |
1340 | 464k | pAacDecoderChannelInfo->granuleLength * 8 /* coreCoderFrameLength */, |
1341 | 464k | i_offset); |
1342 | 464k | if (err != 0) { |
1343 | 0 | error = AAC_DEC_PARSE_ERROR; |
1344 | 0 | goto bail; |
1345 | 0 | } |
1346 | | |
1347 | 464k | lpd_mode_last = 0; |
1348 | 464k | k++; |
1349 | 464k | } else /* mode != 0 => TCX */ |
1350 | 397k | { |
1351 | 397k | error = CLpd_TCX_Read(hBs, pAacDecoderChannelInfo, |
1352 | 397k | pAacDecoderStaticChannelInfo, lg_table[mod[k]], |
1353 | 397k | first_tcx_flag, k, flags); |
1354 | | |
1355 | 397k | lpd_mode_last = mod[k]; |
1356 | 397k | first_tcx_flag = 0; |
1357 | 397k | k += 1 << (mod[k] - 1); |
1358 | 397k | } |
1359 | 861k | if (error != AAC_DEC_OK) { |
1360 | 190k | error = AAC_DEC_PARSE_ERROR; |
1361 | 190k | goto bail; |
1362 | 190k | } |
1363 | 861k | } |
1364 | | |
1365 | 129k | { |
1366 | 129k | int err; |
1367 | | |
1368 | | /* Read LPC coefficients */ |
1369 | 129k | err = CLpc_Read( |
1370 | 129k | hBs, pAacDecoderChannelInfo->data.usac.lsp_coeff, |
1371 | 129k | pAacDecoderStaticChannelInfo->lpc4_lsf, |
1372 | 129k | pAacDecoderChannelInfo->data.usac.lsf_adaptive_mean_cand, |
1373 | 129k | pAacDecoderChannelInfo->data.usac.aStability, mod, first_lpd_flag, |
1374 | | /* if last lpc4 is available from concealment do not extrapolate lpc0 |
1375 | | from lpc2 */ |
1376 | 129k | (mod[0] & 0x3) ? 0 |
1377 | 129k | : (last_lpc_lost && |
1378 | 41.5k | pAacDecoderStaticChannelInfo->last_core_mode != LPD), |
1379 | 129k | last_frame_ok); |
1380 | 129k | if (err != 0) { |
1381 | 8.31k | error = AAC_DEC_PARSE_ERROR; |
1382 | 8.31k | goto bail; |
1383 | 8.31k | } |
1384 | 129k | } |
1385 | | |
1386 | | /* adjust old lsp[] following to a bad frame (to avoid overshoot) (ref: |
1387 | | * dec_LPD.c) */ |
1388 | 120k | if (last_lpc_lost && !last_frame_ok) { |
1389 | 62.7k | int k_next; |
1390 | 62.7k | k = 0; |
1391 | 293k | while (k < nbDiv) { |
1392 | 230k | int i; |
1393 | 230k | k_next = k + (((mod[k] & 0x3) == 0) ? 1 : (1 << (mod[k] - 1))); |
1394 | 230k | FIXP_LPC *lsp_old = pAacDecoderChannelInfo->data.usac.lsp_coeff[k]; |
1395 | 230k | FIXP_LPC *lsp_new = pAacDecoderChannelInfo->data.usac.lsp_coeff[k_next]; |
1396 | | |
1397 | 3.92M | for (i = 0; i < M_LP_FILTER_ORDER; i++) { |
1398 | 3.69M | if (lsp_new[i] < lsp_old[i]) { |
1399 | 1.47M | lsp_old[i] = lsp_new[i]; |
1400 | 1.47M | } |
1401 | 3.69M | } |
1402 | 230k | k = k_next; |
1403 | 230k | } |
1404 | 62.7k | } |
1405 | | |
1406 | 120k | if (!CConcealment_GetLastFrameOk( |
1407 | 120k | &pAacDecoderStaticChannelInfo->concealmentInfo, 1)) { |
1408 | 80.8k | E_LPC_f_lsp_a_conversion( |
1409 | 80.8k | pAacDecoderChannelInfo->data.usac.lsp_coeff[0], |
1410 | 80.8k | pAacDecoderChannelInfo->data.usac.lp_coeff[0], |
1411 | 80.8k | &pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0]); |
1412 | 80.8k | } else if (pAacDecoderStaticChannelInfo->last_lpd_mode != 0) { |
1413 | 39.4k | if (pAacDecoderStaticChannelInfo->last_lpd_mode == 255) { |
1414 | | /* We need it for TCX decoding or ACELP excitation update */ |
1415 | 37.2k | E_LPC_f_lsp_a_conversion( |
1416 | 37.2k | pAacDecoderChannelInfo->data.usac.lsp_coeff[0], |
1417 | 37.2k | pAacDecoderChannelInfo->data.usac.lp_coeff[0], |
1418 | 37.2k | &pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0]); |
1419 | 37.2k | } else { /* last_lpd_mode was TCX */ |
1420 | | /* Copy old LPC4 LP domain coefficients to LPC0 LP domain buffer (to avoid |
1421 | | * converting LSP coefficients again). */ |
1422 | 2.22k | FDKmemcpy(pAacDecoderChannelInfo->data.usac.lp_coeff[0], |
1423 | 2.22k | pAacDecoderStaticChannelInfo->lp_coeff_old[0], |
1424 | 2.22k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1425 | 2.22k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0] = |
1426 | 2.22k | pAacDecoderStaticChannelInfo->lp_coeff_old_exp[0]; |
1427 | 2.22k | } |
1428 | 39.4k | } /* case last_lpd_mode was ACELP is handled by CLpd_TcxDecode() */ |
1429 | | |
1430 | 120k | if (fFacDataPresent && (core_mode_last != LPD)) { |
1431 | 12.9k | int prev_frame_was_short; |
1432 | | |
1433 | 12.9k | prev_frame_was_short = FDKreadBit(hBs); |
1434 | | |
1435 | 12.9k | if (prev_frame_was_short) { |
1436 | 3.65k | core_mode_last = pAacDecoderChannelInfo->data.usac.core_mode_last = |
1437 | 3.65k | FD_SHORT; |
1438 | 3.65k | pAacDecoderChannelInfo->data.usac.lpd_mode_last = 255; |
1439 | | |
1440 | 3.65k | if ((pAacDecoderStaticChannelInfo->last_core_mode != FD_SHORT) && |
1441 | 3.65k | CConcealment_GetLastFrameOk( |
1442 | 3.59k | &pAacDecoderStaticChannelInfo->concealmentInfo, 1)) { |
1443 | | /* USAC Conformance document: |
1444 | | short_fac_flag shall be encoded with a value of 1 if the |
1445 | | window_sequence of the previous frame was 2 (EIGHT_SHORT_SEQUENCE). |
1446 | | Otherwise short_fac_flag shall be encoded with a |
1447 | | value of 0. */ |
1448 | 584 | error = AAC_DEC_PARSE_ERROR; |
1449 | 584 | goto bail; |
1450 | 584 | } |
1451 | 3.65k | } |
1452 | | |
1453 | | /* Assign memory */ |
1454 | 12.4k | pAacDecoderChannelInfo->data.usac.fac_data[0] = |
1455 | 12.4k | CLpd_FAC_GetMemory(pAacDecoderChannelInfo, mod, &facGetMemState); |
1456 | | |
1457 | 12.4k | { |
1458 | 12.4k | int err; |
1459 | | |
1460 | | /* FAC for FD -> ACELP */ |
1461 | 12.4k | err = CLpd_FAC_Read( |
1462 | 12.4k | hBs, pAacDecoderChannelInfo->data.usac.fac_data[0], |
1463 | 12.4k | pAacDecoderChannelInfo->data.usac.fac_data_e, |
1464 | 12.4k | CLpd_FAC_getLength(core_mode_last != FD_SHORT, |
1465 | 12.4k | pAacDecoderChannelInfo->granuleLength), |
1466 | 12.4k | 1, 0); |
1467 | 12.4k | if (err != 0) { |
1468 | 82 | error = AAC_DEC_PARSE_ERROR; |
1469 | 82 | goto bail; |
1470 | 82 | } |
1471 | 12.4k | } |
1472 | 12.4k | } |
1473 | | |
1474 | 989k | bail: |
1475 | 989k | if (error == AAC_DEC_OK) { |
1476 | | /* check consitency of last core/lpd mode values */ |
1477 | 120k | if ((pAacDecoderChannelInfo->data.usac.core_mode_last != |
1478 | 120k | pAacDecoderStaticChannelInfo->last_core_mode) && |
1479 | 120k | (pAacDecoderStaticChannelInfo->last_lpc_lost == 0)) { |
1480 | | /* Something got wrong! */ |
1481 | | /* error = AAC_DEC_PARSE_ERROR; */ /* Throwing errors does not help */ |
1482 | 95.5k | } else if ((pAacDecoderChannelInfo->data.usac.core_mode_last == LPD) && |
1483 | 95.5k | (pAacDecoderChannelInfo->data.usac.lpd_mode_last != |
1484 | 10.3k | pAacDecoderStaticChannelInfo->last_lpd_mode) && |
1485 | 95.5k | (pAacDecoderStaticChannelInfo->last_lpc_lost == 0)) { |
1486 | | /* Something got wrong! */ |
1487 | | /* error = AAC_DEC_PARSE_ERROR; */ /* Throwing errors does not help */ |
1488 | 1.08k | } |
1489 | 120k | } |
1490 | | |
1491 | 989k | return error; |
1492 | 120k | } |
1493 | | |
1494 | | void CLpdChannelStream_Decode( |
1495 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
1496 | 58.1k | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, UINT flags) { |
1497 | 58.1k | UCHAR *mod = pAacDecoderChannelInfo->data.usac.mod; |
1498 | 58.1k | int k; |
1499 | 58.1k | UCHAR last_lpd_mode; |
1500 | 58.1k | int nbDiv = NB_DIV; |
1501 | | |
1502 | | /* k is the frame index. If a frame is of size 40MS or 80MS, |
1503 | | this frame index is incremented 2 or 4 instead of 1 respectively. */ |
1504 | 58.1k | k = 0; |
1505 | 58.1k | last_lpd_mode = |
1506 | 58.1k | pAacDecoderChannelInfo->data.usac |
1507 | 58.1k | .lpd_mode_last; /* could be different to what has been rendered */ |
1508 | 267k | while (k < nbDiv) { |
1509 | 209k | if (mod[k] == 0) { |
1510 | | /* ACELP */ |
1511 | | |
1512 | | /* If FAC (fac_data[k] != NULL), and previous frame was TCX, apply (TCX) |
1513 | | * gains to FAC data */ |
1514 | 90.5k | if (last_lpd_mode > 0 && last_lpd_mode != 255 && |
1515 | 90.5k | pAacDecoderChannelInfo->data.usac.fac_data[k]) { |
1516 | 46.4k | CFac_ApplyGains(pAacDecoderChannelInfo->data.usac.fac_data[k], |
1517 | 46.4k | pAacDecoderChannelInfo->granuleLength, |
1518 | 46.4k | pAacDecoderStaticChannelInfo->last_tcx_gain, |
1519 | 46.4k | pAacDecoderStaticChannelInfo->last_alfd_gains, |
1520 | 46.4k | (last_lpd_mode < 4) ? last_lpd_mode : 3); |
1521 | | |
1522 | 46.4k | pAacDecoderChannelInfo->data.usac.fac_data_e[k] += |
1523 | 46.4k | pAacDecoderStaticChannelInfo->last_tcx_gain_e; |
1524 | 46.4k | } |
1525 | 119k | } else { |
1526 | | /* TCX */ |
1527 | 119k | CLpd_TcxDecode(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo, |
1528 | 119k | flags, mod[k], last_lpd_mode, k, 1 /* frameOk == 1 */ |
1529 | 119k | ); |
1530 | | |
1531 | | /* Store TCX gain scale for next possible FAC transition. */ |
1532 | 119k | pAacDecoderStaticChannelInfo->last_tcx_gain = |
1533 | 119k | pAacDecoderChannelInfo->data.usac.tcx_gain[k]; |
1534 | 119k | pAacDecoderStaticChannelInfo->last_tcx_gain_e = |
1535 | 119k | pAacDecoderChannelInfo->data.usac.tcx_gain_e[k]; |
1536 | | |
1537 | | /* If FAC (fac_data[k] != NULL), apply gains */ |
1538 | 119k | if (last_lpd_mode == 0 && pAacDecoderChannelInfo->data.usac.fac_data[k]) { |
1539 | 40.2k | CFac_ApplyGains( |
1540 | 40.2k | pAacDecoderChannelInfo->data.usac.fac_data[k], |
1541 | 40.2k | pAacDecoderChannelInfo->granuleLength /* == fac_length */, |
1542 | 40.2k | pAacDecoderChannelInfo->data.usac.tcx_gain[k], |
1543 | 40.2k | pAacDecoderStaticChannelInfo->last_alfd_gains, mod[k]); |
1544 | | |
1545 | 40.2k | pAacDecoderChannelInfo->data.usac.fac_data_e[k] += |
1546 | 40.2k | pAacDecoderChannelInfo->data.usac.tcx_gain_e[k]; |
1547 | 40.2k | } |
1548 | 119k | } |
1549 | | |
1550 | | /* remember previous mode */ |
1551 | 209k | last_lpd_mode = mod[k]; |
1552 | | |
1553 | | /* Increase k to next frame */ |
1554 | 209k | k += (mod[k] == 0) ? 1 : (1 << (mod[k] - 1)); |
1555 | 209k | } |
1556 | 58.1k | } |
1557 | | |
1558 | | AAC_DECODER_ERROR CLpd_RenderTimeSignal( |
1559 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
1560 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, PCM_DEC *pTimeData, |
1561 | | INT lFrame, SamplingRateInfo *pSamplingRateInfo, UINT frameOk, |
1562 | 581k | const INT aacOutDataHeadroom, UINT flags, UINT strmFlags) { |
1563 | 581k | UCHAR *mod = pAacDecoderChannelInfo->data.usac.mod; |
1564 | 581k | AAC_DECODER_ERROR error = AAC_DEC_OK; |
1565 | 581k | int k, i_offset; |
1566 | 581k | int last_k; |
1567 | 581k | int nrSamples = 0; |
1568 | 581k | int facFB = 1; |
1569 | 581k | int nbDiv = NB_DIV; |
1570 | 581k | int lDiv = lFrame / nbDiv; /* length of division (acelp or tcx20 frame)*/ |
1571 | 581k | int lFac = lDiv / 2; |
1572 | 581k | int nbSubfr = |
1573 | 581k | lFrame / (nbDiv * L_SUBFR); /* number of subframes per division */ |
1574 | 581k | int nbSubfrSuperfr = nbDiv * nbSubfr; |
1575 | 581k | int synSfd = (nbSubfrSuperfr / 2) - BPF_SFD; |
1576 | 581k | int SynDelay = synSfd * L_SUBFR; |
1577 | 581k | int aacDelay = lFrame / 2; |
1578 | | |
1579 | | /* |
1580 | | In respect to the reference software, the synth pointer here is lagging by |
1581 | | aacDelay ( == SYN_DELAY + BPF_DELAY ) samples. The corresponding old |
1582 | | synthesis samples are handled by the IMDCT overlap. |
1583 | | */ |
1584 | | |
1585 | 581k | FIXP_DBL *synth_buf = |
1586 | 581k | pAacDecoderChannelInfo->pComStaticData->pWorkBufferCore1->synth_buf; |
1587 | 581k | FIXP_DBL *synth = synth_buf + PIT_MAX_MAX - BPF_DELAY; |
1588 | 581k | UCHAR last_lpd_mode, last_last_lpd_mode, last_lpc_lost, last_frame_lost; |
1589 | | |
1590 | 581k | INT pitch[NB_SUBFR_SUPERFR + SYN_SFD]; |
1591 | 581k | FIXP_DBL pit_gain[NB_SUBFR_SUPERFR + SYN_SFD]; |
1592 | | |
1593 | 581k | const int *lg_table; |
1594 | 581k | int lg_table_offset = 0; |
1595 | | |
1596 | 581k | UINT samplingRate = pSamplingRateInfo->samplingRate; |
1597 | | |
1598 | 581k | FDKmemclear(pitch, (NB_SUBFR_SUPERFR + SYN_SFD) * sizeof(INT)); |
1599 | | |
1600 | 581k | if (flags & AACDEC_FLUSH) { |
1601 | 8.49k | CLpd_Reset(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo, |
1602 | 8.49k | flags & AACDEC_FLUSH); |
1603 | 8.49k | frameOk = 0; |
1604 | 8.49k | } |
1605 | | |
1606 | 581k | switch (lFrame) { |
1607 | 131k | case 1024: |
1608 | 131k | lg_table = &lg_table_ccfl[0][lg_table_offset]; |
1609 | 131k | break; |
1610 | 450k | case 768: |
1611 | 450k | lg_table = &lg_table_ccfl[1][lg_table_offset]; |
1612 | 450k | break; |
1613 | 0 | default: |
1614 | 0 | FDK_ASSERT(0); |
1615 | 0 | return AAC_DEC_UNKNOWN; |
1616 | 581k | } |
1617 | | |
1618 | 581k | last_frame_lost = !CConcealment_GetLastFrameOk( |
1619 | 581k | &pAacDecoderStaticChannelInfo->concealmentInfo, 0); |
1620 | | |
1621 | | /* Maintain LPD mode from previous frame */ |
1622 | 581k | if ((pAacDecoderStaticChannelInfo->last_core_mode == FD_LONG) || |
1623 | 581k | (pAacDecoderStaticChannelInfo->last_core_mode == FD_SHORT)) { |
1624 | 26.0k | pAacDecoderStaticChannelInfo->last_lpd_mode = 255; |
1625 | 26.0k | } |
1626 | | |
1627 | 581k | if (!frameOk) { |
1628 | 525k | FIXP_DBL old_tcx_gain; |
1629 | 525k | FIXP_SGL old_stab; |
1630 | 525k | SCHAR old_tcx_gain_e; |
1631 | 525k | int nLostSf; |
1632 | | |
1633 | 525k | last_lpd_mode = pAacDecoderStaticChannelInfo->last_lpd_mode; |
1634 | 525k | old_tcx_gain = pAacDecoderStaticChannelInfo->last_tcx_gain; |
1635 | 525k | old_tcx_gain_e = pAacDecoderStaticChannelInfo->last_tcx_gain_e; |
1636 | 525k | old_stab = pAacDecoderStaticChannelInfo->oldStability; |
1637 | 525k | nLostSf = pAacDecoderStaticChannelInfo->numLostLpdFrames; |
1638 | | |
1639 | | /* patch the last LPD mode */ |
1640 | 525k | pAacDecoderChannelInfo->data.usac.lpd_mode_last = last_lpd_mode; |
1641 | | |
1642 | | /* Do mode extrapolation and repeat the previous mode: |
1643 | | if previous mode = ACELP -> ACELP |
1644 | | if previous mode = TCX-20/40 -> TCX-20 |
1645 | | if previous mode = TCX-80 -> TCX-80 |
1646 | | notes: |
1647 | | - ACELP is not allowed after TCX (no pitch information to reuse) |
1648 | | - TCX-40 is not allowed in the mode repetition to keep the logic simple |
1649 | | */ |
1650 | 525k | switch (last_lpd_mode) { |
1651 | 299k | case 0: |
1652 | 299k | mod[0] = mod[1] = mod[2] = mod[3] = 0; /* -> ACELP concealment */ |
1653 | 299k | break; |
1654 | 21.3k | case 3: |
1655 | 21.3k | mod[0] = mod[1] = mod[2] = mod[3] = 3; /* -> TCX FD concealment */ |
1656 | 21.3k | break; |
1657 | 57.6k | case 2: |
1658 | 57.6k | mod[0] = mod[1] = mod[2] = mod[3] = 2; /* -> TCX FD concealment */ |
1659 | 57.6k | break; |
1660 | 14.5k | case 1: |
1661 | 147k | default: |
1662 | 147k | mod[0] = mod[1] = mod[2] = mod[3] = 4; /* -> TCX TD concealment */ |
1663 | 147k | break; |
1664 | 525k | } |
1665 | | |
1666 | | /* LPC extrapolation */ |
1667 | 525k | CLpc_Conceal(pAacDecoderChannelInfo->data.usac.lsp_coeff, |
1668 | 525k | pAacDecoderStaticChannelInfo->lpc4_lsf, |
1669 | 525k | pAacDecoderStaticChannelInfo->lsf_adaptive_mean, |
1670 | | /*(pAacDecoderStaticChannelInfo->numLostLpdFrames == 0) ||*/ |
1671 | 525k | (last_lpd_mode == 255)); |
1672 | | |
1673 | 525k | if ((last_lpd_mode > 0) && (last_lpd_mode < 255)) { |
1674 | | /* Copy old LPC4 LP domain coefficients to LPC0 LP domain buffer (to avoid |
1675 | | * converting LSP coefficients again). */ |
1676 | 226k | FDKmemcpy(pAacDecoderChannelInfo->data.usac.lp_coeff[0], |
1677 | 226k | pAacDecoderStaticChannelInfo->lp_coeff_old[0], |
1678 | 226k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1679 | 226k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0] = |
1680 | 226k | pAacDecoderStaticChannelInfo->lp_coeff_old_exp[0]; |
1681 | 226k | } /* case last_lpd_mode was ACELP is handled by CLpd_TcxDecode() */ |
1682 | | /* case last_lpd_mode was Time domain TCX concealment is handled after this |
1683 | | * "if (!frameOk)"-block */ |
1684 | | |
1685 | | /* k is the frame index. If a frame is of size 40MS or 80MS, |
1686 | | this frame index is incremented 2 or 4 instead of 1 respectively. */ |
1687 | 525k | k = 0; |
1688 | 2.44M | while (k < nbDiv) { |
1689 | 1.92M | pAacDecoderChannelInfo->data.usac.tcx_gain[k] = old_tcx_gain; |
1690 | 1.92M | pAacDecoderChannelInfo->data.usac.tcx_gain_e[k] = old_tcx_gain_e; |
1691 | | |
1692 | | /* restore stability value from last frame */ |
1693 | 1.92M | pAacDecoderChannelInfo->data.usac.aStability[k] = old_stab; |
1694 | | |
1695 | | /* Increase k to next frame */ |
1696 | 1.92M | k += ((mod[k] & 0x3) == 0) ? 1 : (1 << ((mod[k] & 0x3) - 1)); |
1697 | | |
1698 | 1.92M | nLostSf++; |
1699 | 1.92M | } |
1700 | 525k | } else { |
1701 | 55.8k | if ((pAacDecoderStaticChannelInfo->last_lpd_mode == 4) && (mod[0] > 0)) { |
1702 | | /* Copy old LPC4 LP domain coefficients to LPC0 LP domain buffer (to avoid |
1703 | | * converting LSP coefficients again). */ |
1704 | 7.71k | FDKmemcpy(pAacDecoderChannelInfo->data.usac.lp_coeff[0], |
1705 | 7.71k | pAacDecoderStaticChannelInfo->lp_coeff_old[0], |
1706 | 7.71k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1707 | 7.71k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0] = |
1708 | 7.71k | pAacDecoderStaticChannelInfo->lp_coeff_old_exp[0]; |
1709 | 7.71k | } |
1710 | 55.8k | } |
1711 | | |
1712 | 581k | Acelp_PreProcessing(synth_buf, pAacDecoderStaticChannelInfo->old_synth, pitch, |
1713 | 581k | pAacDecoderStaticChannelInfo->old_T_pf, pit_gain, |
1714 | 581k | pAacDecoderStaticChannelInfo->old_gain_pf, samplingRate, |
1715 | 581k | &i_offset, lFrame, synSfd, nbSubfrSuperfr); |
1716 | | |
1717 | | /* k is the frame index. If a frame is of size 40MS or 80MS, |
1718 | | this frame index is incremented 2 or 4 instead of 1 respectively. */ |
1719 | 581k | k = 0; |
1720 | 581k | last_k = -1; /* mark invalid */ |
1721 | 581k | last_lpd_mode = pAacDecoderStaticChannelInfo->last_lpd_mode; |
1722 | 581k | last_last_lpd_mode = pAacDecoderStaticChannelInfo->last_last_lpd_mode; |
1723 | 581k | last_lpc_lost = pAacDecoderStaticChannelInfo->last_lpc_lost | last_frame_lost; |
1724 | | |
1725 | | /* This buffer must be avalable for the case of FD->ACELP transition. The |
1726 | | beginning of the buffer is used after the BPF to overwrite the output signal. |
1727 | | Only the FAC area must be affected by the BPF */ |
1728 | | |
1729 | 2.70M | while (k < nbDiv) { |
1730 | 2.12M | if (frameOk == 0) { |
1731 | 1.92M | pAacDecoderStaticChannelInfo->numLostLpdFrames++; |
1732 | 1.92M | } else { |
1733 | 202k | last_frame_lost |= |
1734 | 202k | (pAacDecoderStaticChannelInfo->numLostLpdFrames > 0) ? 1 : 0; |
1735 | 202k | pAacDecoderStaticChannelInfo->numLostLpdFrames = 0; |
1736 | 202k | } |
1737 | 2.12M | if (mod[k] == 0 || mod[k] == 4) { |
1738 | | /* ACELP or TCX time domain concealment */ |
1739 | 1.87M | FIXP_DBL *acelp_out; |
1740 | | |
1741 | | /* FAC management */ |
1742 | 1.87M | if ((last_lpd_mode != 0) && (last_lpd_mode != 4)) /* TCX TD concealment */ |
1743 | 63.1k | { |
1744 | 63.1k | FIXP_DBL *pFacData = NULL; |
1745 | | |
1746 | 63.1k | if (frameOk && !last_frame_lost) { |
1747 | 8.79k | pFacData = pAacDecoderChannelInfo->data.usac.fac_data[k]; |
1748 | 8.79k | } |
1749 | | |
1750 | 63.1k | nrSamples += CLpd_FAC_Mdct2Acelp( |
1751 | 63.1k | &pAacDecoderStaticChannelInfo->IMdct, synth + nrSamples, pFacData, |
1752 | 63.1k | pAacDecoderChannelInfo->data.usac.fac_data_e[k], |
1753 | 63.1k | pAacDecoderChannelInfo->data.usac.lp_coeff[k], |
1754 | 63.1k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[k], |
1755 | 63.1k | lFrame - nrSamples, |
1756 | 63.1k | CLpd_FAC_getLength( |
1757 | 63.1k | (pAacDecoderStaticChannelInfo->last_core_mode != FD_SHORT) || |
1758 | 63.1k | (k > 0), |
1759 | 63.1k | lFac), |
1760 | 63.1k | (pAacDecoderStaticChannelInfo->last_core_mode != LPD) && (k == 0), |
1761 | 63.1k | 0); |
1762 | | |
1763 | 63.1k | FDKmemcpy( |
1764 | 63.1k | synth + nrSamples, pAacDecoderStaticChannelInfo->IMdct.overlap.time, |
1765 | 63.1k | pAacDecoderStaticChannelInfo->IMdct.ov_offset * sizeof(FIXP_DBL)); |
1766 | 63.1k | { |
1767 | 63.1k | FIXP_LPC *lp_prev = |
1768 | 63.1k | pAacDecoderChannelInfo->data.usac |
1769 | 63.1k | .lp_coeff[0]; /* init value does not real matter */ |
1770 | 63.1k | INT lp_prev_exp = pAacDecoderChannelInfo->data.usac.lp_coeff_exp[0]; |
1771 | | |
1772 | 63.1k | if (last_lpd_mode != 255) { /* last mode was tcx */ |
1773 | 59.3k | last_k = k - (1 << (last_lpd_mode - 1)); |
1774 | 59.3k | if (last_k < 0) { |
1775 | 15.3k | lp_prev = pAacDecoderStaticChannelInfo->lp_coeff_old[1]; |
1776 | 15.3k | lp_prev_exp = pAacDecoderStaticChannelInfo->lp_coeff_old_exp[1]; |
1777 | 44.0k | } else { |
1778 | 44.0k | lp_prev = pAacDecoderChannelInfo->data.usac.lp_coeff[last_k]; |
1779 | 44.0k | lp_prev_exp = |
1780 | 44.0k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[last_k]; |
1781 | 44.0k | } |
1782 | 59.3k | } |
1783 | | |
1784 | 63.1k | CLpd_AcelpPrepareInternalMem( |
1785 | 63.1k | synth + aacDelay + k * lDiv, last_lpd_mode, |
1786 | 63.1k | (last_last_lpd_mode == 4) ? 0 : last_last_lpd_mode, |
1787 | 63.1k | pAacDecoderChannelInfo->data.usac.lp_coeff[k], |
1788 | 63.1k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[k], lp_prev, |
1789 | 63.1k | lp_prev_exp, &pAacDecoderStaticChannelInfo->acelp, lFrame, |
1790 | 63.1k | (last_frame_lost && k < 2), mod[k]); |
1791 | 63.1k | } |
1792 | 1.81M | } else { |
1793 | 1.81M | if (k == 0 && pAacDecoderStaticChannelInfo->IMdct.ov_offset != |
1794 | 439k | lFrame / facFB / 2) { |
1795 | 0 | pAacDecoderStaticChannelInfo->IMdct.ov_offset = lFrame / facFB / 2; |
1796 | 0 | } |
1797 | 1.81M | nrSamples += imdct_drain(&pAacDecoderStaticChannelInfo->IMdct, |
1798 | 1.81M | synth + nrSamples, lFrame / facFB - nrSamples); |
1799 | 1.81M | } |
1800 | | |
1801 | 1.87M | if (nrSamples >= lFrame / facFB) { |
1802 | | /* Write ACELP time domain samples into IMDCT overlap buffer at |
1803 | | * pAacDecoderStaticChannelInfo->IMdct.overlap.time + |
1804 | | * pAacDecoderStaticChannelInfo->IMdct.ov_offset |
1805 | | */ |
1806 | 947k | acelp_out = pAacDecoderStaticChannelInfo->IMdct.overlap.time + |
1807 | 947k | pAacDecoderStaticChannelInfo->IMdct.ov_offset; |
1808 | | |
1809 | | /* Account ACELP time domain output samples to overlap buffer */ |
1810 | 947k | pAacDecoderStaticChannelInfo->IMdct.ov_offset += lDiv; |
1811 | 947k | } else { |
1812 | | /* Write ACELP time domain samples into output buffer at pTimeData + |
1813 | | * nrSamples */ |
1814 | 926k | acelp_out = synth + nrSamples; |
1815 | | |
1816 | | /* Account ACELP time domain output samples to output buffer */ |
1817 | 926k | nrSamples += lDiv; |
1818 | 926k | } |
1819 | | |
1820 | 1.87M | if (mod[k] == 4) { |
1821 | 589k | pAacDecoderStaticChannelInfo->acelp.wsyn_rms = scaleValue( |
1822 | 589k | pAacDecoderChannelInfo->data.usac.tcx_gain[k], |
1823 | 589k | fixMin(0, |
1824 | 589k | pAacDecoderChannelInfo->data.usac.tcx_gain_e[k] - SF_EXC)); |
1825 | 589k | CLpd_TcxTDConceal(&pAacDecoderStaticChannelInfo->acelp, |
1826 | 589k | &pAacDecoderStaticChannelInfo->last_tcx_pitch, |
1827 | 589k | pAacDecoderChannelInfo->data.usac.lsp_coeff[k], |
1828 | 589k | pAacDecoderChannelInfo->data.usac.lsp_coeff[k + 1], |
1829 | 589k | pAacDecoderChannelInfo->data.usac.aStability[k], |
1830 | 589k | pAacDecoderStaticChannelInfo->numLostLpdFrames, |
1831 | 589k | acelp_out, lFrame, |
1832 | 589k | pAacDecoderStaticChannelInfo->last_tcx_noise_factor); |
1833 | | |
1834 | 1.28M | } else { |
1835 | 1.28M | FDK_ASSERT(pAacDecoderChannelInfo->data.usac.aStability[k] >= |
1836 | 1.28M | (FIXP_SGL)0); |
1837 | 1.28M | CLpd_AcelpDecode(&pAacDecoderStaticChannelInfo->acelp, i_offset, |
1838 | 1.28M | pAacDecoderChannelInfo->data.usac.lsp_coeff[k], |
1839 | 1.28M | pAacDecoderChannelInfo->data.usac.lsp_coeff[k + 1], |
1840 | 1.28M | pAacDecoderChannelInfo->data.usac.aStability[k], |
1841 | 1.28M | &pAacDecoderChannelInfo->data.usac.acelp[k], |
1842 | 1.28M | pAacDecoderStaticChannelInfo->numLostLpdFrames, |
1843 | 1.28M | last_lpc_lost, k, acelp_out, |
1844 | 1.28M | &pitch[(k * nbSubfr) + synSfd], |
1845 | 1.28M | &pit_gain[(k * nbSubfr) + synSfd], lFrame); |
1846 | 1.28M | } |
1847 | | |
1848 | 1.87M | if (mod[k] != 4) { |
1849 | 1.28M | if (last_lpd_mode != 0 && |
1850 | 1.28M | pAacDecoderChannelInfo->data.usac |
1851 | 50.8k | .bpf_control_info) { /* FD/TCX -> ACELP transition */ |
1852 | | /* bass post-filter past FAC area (past two (one for FD short) |
1853 | | * subframes) */ |
1854 | 28.0k | int currentSf = synSfd + k * nbSubfr; |
1855 | | |
1856 | 28.0k | if ((k > 0) || (pAacDecoderStaticChannelInfo->last_core_mode != |
1857 | 27.8k | FD_SHORT)) { /* TCX or FD long -> ACELP */ |
1858 | 27.8k | pitch[currentSf - 2] = pitch[currentSf - 1] = pitch[currentSf]; |
1859 | 27.8k | pit_gain[currentSf - 2] = pit_gain[currentSf - 1] = |
1860 | 27.8k | pit_gain[currentSf]; |
1861 | 27.8k | } else { /* FD short -> ACELP */ |
1862 | 232 | pitch[currentSf - 1] = pitch[currentSf]; |
1863 | 232 | pit_gain[currentSf - 1] = pit_gain[currentSf]; |
1864 | 232 | } |
1865 | 28.0k | } |
1866 | 1.28M | } |
1867 | 1.87M | } else { /* TCX */ |
1868 | 251k | int lg = lg_table[mod[k]]; |
1869 | 251k | int isFullBandLpd = 0; |
1870 | | |
1871 | | /* FAC management */ |
1872 | 251k | if ((last_lpd_mode == 0) || (last_lpd_mode == 4)) /* TCX TD concealment */ |
1873 | 44.4k | { |
1874 | 44.4k | C_AALLOC_SCRATCH_START(fac_buf, FIXP_DBL, 1024 / 8); |
1875 | | |
1876 | | /* pAacDecoderChannelInfo->data.usac.fac_data[k] == NULL means no FAC |
1877 | | * data available. */ |
1878 | 44.4k | if (last_frame_lost == 1 || |
1879 | 44.4k | pAacDecoderChannelInfo->data.usac.fac_data[k] == NULL) { |
1880 | 36.8k | FDKmemclear(fac_buf, 1024 / 8 * sizeof(FIXP_DBL)); |
1881 | 36.8k | pAacDecoderChannelInfo->data.usac.fac_data[k] = fac_buf; |
1882 | 36.8k | pAacDecoderChannelInfo->data.usac.fac_data_e[k] = 0; |
1883 | 36.8k | } |
1884 | | |
1885 | 44.4k | nrSamples += CLpd_FAC_Acelp2Mdct( |
1886 | 44.4k | &pAacDecoderStaticChannelInfo->IMdct, synth + nrSamples, |
1887 | 44.4k | SPEC_TCX(pAacDecoderChannelInfo->pSpectralCoefficient, k, |
1888 | 44.4k | pAacDecoderChannelInfo->granuleLength, isFullBandLpd), |
1889 | 44.4k | pAacDecoderChannelInfo->specScale + k, 1, |
1890 | 44.4k | pAacDecoderChannelInfo->data.usac.fac_data[k], |
1891 | 44.4k | pAacDecoderChannelInfo->data.usac.fac_data_e[k], |
1892 | 44.4k | pAacDecoderChannelInfo->granuleLength /* == fac_length */, |
1893 | 44.4k | lFrame - nrSamples, lg, |
1894 | 44.4k | FDKgetWindowSlope(lDiv, |
1895 | 44.4k | GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1896 | 44.4k | lDiv, pAacDecoderChannelInfo->data.usac.lp_coeff[k], |
1897 | 44.4k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[k], |
1898 | 44.4k | &pAacDecoderStaticChannelInfo->acelp, |
1899 | 44.4k | pAacDecoderChannelInfo->data.usac.tcx_gain[k], |
1900 | 44.4k | (last_frame_lost || !frameOk), 0 /* is not FD FAC */ |
1901 | 44.4k | , |
1902 | 44.4k | last_lpd_mode, k, |
1903 | 44.4k | pAacDecoderChannelInfo |
1904 | 44.4k | ->currAliasingSymmetry /* Note: The current aliasing |
1905 | | symmetry for a TCX (i.e. LPD) |
1906 | | frame must always be 0 */ |
1907 | 44.4k | ); |
1908 | | |
1909 | 44.4k | pitch[(k * nbSubfr) + synSfd + 1] = pitch[(k * nbSubfr) + synSfd] = |
1910 | 44.4k | pitch[(k * nbSubfr) + synSfd - 1]; |
1911 | 44.4k | pit_gain[(k * nbSubfr) + synSfd + 1] = |
1912 | 44.4k | pit_gain[(k * nbSubfr) + synSfd] = |
1913 | 44.4k | pit_gain[(k * nbSubfr) + synSfd - 1]; |
1914 | | |
1915 | 44.4k | C_AALLOC_SCRATCH_END(fac_buf, FIXP_DBL, 1024 / 8); |
1916 | 207k | } else { |
1917 | 207k | int tl = lg; |
1918 | 207k | int fl = lDiv; |
1919 | 207k | int fr = lDiv; |
1920 | | |
1921 | 207k | nrSamples += imlt_block( |
1922 | 207k | &pAacDecoderStaticChannelInfo->IMdct, synth + nrSamples, |
1923 | 207k | SPEC_TCX(pAacDecoderChannelInfo->pSpectralCoefficient, k, |
1924 | 207k | pAacDecoderChannelInfo->granuleLength, isFullBandLpd), |
1925 | 207k | pAacDecoderChannelInfo->specScale + k, 1, lFrame - nrSamples, tl, |
1926 | 207k | FDKgetWindowSlope(fl, |
1927 | 207k | GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1928 | 207k | fl, |
1929 | 207k | FDKgetWindowSlope(fr, |
1930 | 207k | GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1931 | 207k | fr, pAacDecoderChannelInfo->data.usac.tcx_gain[k], |
1932 | 207k | pAacDecoderChannelInfo->currAliasingSymmetry |
1933 | 207k | ? MLT_FLAG_CURR_ALIAS_SYMMETRY |
1934 | 207k | : 0); |
1935 | 207k | } |
1936 | 251k | } |
1937 | | /* remember previous mode */ |
1938 | 2.12M | last_last_lpd_mode = last_lpd_mode; |
1939 | 2.12M | last_lpd_mode = mod[k]; |
1940 | 2.12M | last_lpc_lost = (frameOk == 0) ? 1 : 0; |
1941 | | |
1942 | | /* Increase k to next frame */ |
1943 | 2.12M | last_k = k; |
1944 | 2.12M | k += ((mod[k] & 0x3) == 0) ? 1 : (1 << (mod[k] - 1)); |
1945 | 2.12M | } |
1946 | | |
1947 | 581k | if (frameOk) { |
1948 | | /* assume data was ok => store for concealment */ |
1949 | 55.8k | FDK_ASSERT(pAacDecoderChannelInfo->data.usac.aStability[last_k] >= |
1950 | 55.8k | (FIXP_SGL)0); |
1951 | 55.8k | pAacDecoderStaticChannelInfo->oldStability = |
1952 | 55.8k | pAacDecoderChannelInfo->data.usac.aStability[last_k]; |
1953 | 55.8k | FDKmemcpy(pAacDecoderStaticChannelInfo->lsf_adaptive_mean, |
1954 | 55.8k | pAacDecoderChannelInfo->data.usac.lsf_adaptive_mean_cand, |
1955 | 55.8k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1956 | 55.8k | } |
1957 | | |
1958 | | /* store past lp coeffs for next superframe (they are only valid and needed if |
1959 | | * last_lpd_mode was tcx) */ |
1960 | 581k | if (last_lpd_mode > 0) { |
1961 | 253k | FDKmemcpy(pAacDecoderStaticChannelInfo->lp_coeff_old[0], |
1962 | 253k | pAacDecoderChannelInfo->data.usac.lp_coeff[nbDiv], |
1963 | 253k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1964 | 253k | pAacDecoderStaticChannelInfo->lp_coeff_old_exp[0] = |
1965 | 253k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[nbDiv]; |
1966 | 253k | FDKmemcpy(pAacDecoderStaticChannelInfo->lp_coeff_old[1], |
1967 | 253k | pAacDecoderChannelInfo->data.usac.lp_coeff[last_k], |
1968 | 253k | M_LP_FILTER_ORDER * sizeof(FIXP_LPC)); |
1969 | 253k | pAacDecoderStaticChannelInfo->lp_coeff_old_exp[1] = |
1970 | 253k | pAacDecoderChannelInfo->data.usac.lp_coeff_exp[last_k]; |
1971 | 253k | } |
1972 | | |
1973 | 581k | FDK_ASSERT(nrSamples == lFrame); |
1974 | | |
1975 | | /* check whether usage of bass postfilter was de-activated in the bitstream; |
1976 | | if yes, set pitch gain to 0 */ |
1977 | 581k | if (!(pAacDecoderChannelInfo->data.usac.bpf_control_info)) { |
1978 | 293k | if (mod[0] != 0 && (pAacDecoderStaticChannelInfo->old_bpf_control_info)) { |
1979 | 250k | for (int i = 2; i < nbSubfrSuperfr; i++) |
1980 | 227k | pit_gain[synSfd + i] = (FIXP_DBL)0; |
1981 | 271k | } else { |
1982 | 3.89M | for (int i = 0; i < nbSubfrSuperfr; i++) |
1983 | 3.62M | pit_gain[synSfd + i] = (FIXP_DBL)0; |
1984 | 271k | } |
1985 | 293k | } |
1986 | | |
1987 | | /* for bass postfilter */ |
1988 | 3.75M | for (int n = 0; n < synSfd; n++) { |
1989 | 3.17M | pAacDecoderStaticChannelInfo->old_T_pf[n] = pitch[nbSubfrSuperfr + n]; |
1990 | 3.17M | pAacDecoderStaticChannelInfo->old_gain_pf[n] = pit_gain[nbSubfrSuperfr + n]; |
1991 | 3.17M | } |
1992 | | |
1993 | 581k | pAacDecoderStaticChannelInfo->old_bpf_control_info = |
1994 | 581k | pAacDecoderChannelInfo->data.usac.bpf_control_info; |
1995 | | |
1996 | 581k | { |
1997 | 581k | INT lookahead = -BPF_DELAY; |
1998 | 581k | int copySamp = (mod[nbDiv - 1] == 0) ? (aacDelay) : (aacDelay - lFac); |
1999 | | |
2000 | | /* Copy enough time domain samples from MDCT to synthesis buffer as needed |
2001 | | * by the bass postfilter */ |
2002 | | |
2003 | 581k | lookahead += imdct_copy_ov_and_nr(&pAacDecoderStaticChannelInfo->IMdct, |
2004 | 581k | synth + nrSamples, copySamp); |
2005 | | |
2006 | 581k | FDK_ASSERT(lookahead == copySamp - BPF_DELAY); |
2007 | | |
2008 | 581k | FIXP_DBL *p2_synth = synth + BPF_DELAY; |
2009 | | |
2010 | | /* recalculate pitch gain to allow postfilering on FAC area */ |
2011 | 8.08M | for (int i = 0; i < nbSubfrSuperfr; i++) { |
2012 | 7.50M | int T = pitch[i]; |
2013 | 7.50M | FIXP_DBL gain = pit_gain[i]; |
2014 | | |
2015 | 7.50M | if (gain > (FIXP_DBL)0) { |
2016 | 1.82M | gain = get_gain(&p2_synth[i * L_SUBFR], &p2_synth[(i * L_SUBFR) - T], |
2017 | 1.82M | L_SUBFR); |
2018 | 1.82M | pit_gain[i] = gain; |
2019 | 1.82M | } |
2020 | 7.50M | } |
2021 | | |
2022 | 581k | { |
2023 | 581k | bass_pf_1sf_delay(p2_synth, pitch, pit_gain, lFrame, lFrame / facFB, |
2024 | 581k | mod[nbDiv - 1] ? (SynDelay - (lDiv / 2)) : SynDelay, |
2025 | 581k | pTimeData, aacOutDataHeadroom, |
2026 | 581k | pAacDecoderStaticChannelInfo->mem_bpf); |
2027 | 581k | } |
2028 | 581k | } |
2029 | | |
2030 | 0 | Acelp_PostProcessing(synth_buf, pAacDecoderStaticChannelInfo->old_synth, |
2031 | 581k | pitch, pAacDecoderStaticChannelInfo->old_T_pf, lFrame, |
2032 | 581k | synSfd, nbSubfrSuperfr); |
2033 | | |
2034 | | /* Store last mode for next super frame */ |
2035 | 581k | { pAacDecoderStaticChannelInfo->last_core_mode = LPD; } |
2036 | 581k | pAacDecoderStaticChannelInfo->last_lpd_mode = last_lpd_mode; |
2037 | 581k | pAacDecoderStaticChannelInfo->last_last_lpd_mode = last_last_lpd_mode; |
2038 | 581k | pAacDecoderStaticChannelInfo->last_lpc_lost = last_lpc_lost; |
2039 | | |
2040 | 581k | return error; |
2041 | 581k | } |