10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
12527
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
10725
|
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 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
12527
|
25 ** $Id: sbr_dec.c,v 1.2 2003/10/03 22:22:27 alex Exp $
|
10725
|
26 **/
|
|
27
|
|
28
|
|
29 #include "common.h"
|
|
30 #include "structs.h"
|
|
31
|
|
32 #ifdef SBR_DEC
|
|
33
|
12527
|
34 #include <string.h>
|
10725
|
35 #include <stdlib.h>
|
|
36
|
|
37 #include "syntax.h"
|
|
38 #include "bits.h"
|
|
39 #include "sbr_syntax.h"
|
|
40 #include "sbr_qmf.h"
|
|
41 #include "sbr_hfgen.h"
|
|
42 #include "sbr_hfadj.h"
|
|
43
|
12527
|
44 /* static function declarations */
|
|
45 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
|
10725
|
46
|
12527
|
47 sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
|
|
48 uint32_t sample_rate
|
10989
|
49 #ifdef DRM
|
|
50 , uint8_t IsDRM
|
|
51 #endif
|
|
52 )
|
10725
|
53 {
|
12527
|
54 sbr_info *sbr = faad_malloc(sizeof(sbr_info));
|
10725
|
55 memset(sbr, 0, sizeof(sbr_info));
|
|
56
|
12527
|
57 /* save id of the parent element */
|
|
58 sbr->id_aac = id_aac;
|
|
59 sbr->sample_rate = sample_rate;
|
|
60
|
10725
|
61 sbr->bs_freq_scale = 2;
|
|
62 sbr->bs_alter_scale = 1;
|
|
63 sbr->bs_noise_bands = 2;
|
|
64 sbr->bs_limiter_bands = 2;
|
|
65 sbr->bs_limiter_gains = 2;
|
|
66 sbr->bs_interpol_freq = 1;
|
|
67 sbr->bs_smoothing_mode = 1;
|
|
68 sbr->bs_start_freq = 5;
|
|
69 sbr->bs_amp_res = 1;
|
|
70 sbr->bs_samplerate_mode = 1;
|
|
71 sbr->prevEnvIsShort[0] = -1;
|
|
72 sbr->prevEnvIsShort[1] = -1;
|
|
73 sbr->header_count = 0;
|
|
74
|
10989
|
75 #ifdef DRM
|
12527
|
76 sbr->Is_DRM_SBR = IsDRM;
|
10989
|
77 #endif
|
12527
|
78 sbr->bs_samplerate_mode = 1;
|
|
79 sbr->tHFGen = T_HFGEN;
|
|
80 sbr->tHFAdj = T_HFADJ;
|
10989
|
81
|
|
82 /* force sbr reset */
|
|
83 sbr->bs_start_freq_prev = -1;
|
|
84
|
|
85 if (framelength == 960)
|
|
86 {
|
|
87 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS_960;
|
|
88 sbr->numTimeSlots = NO_TIME_SLOTS_960;
|
|
89 } else {
|
|
90 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS;
|
|
91 sbr->numTimeSlots = NO_TIME_SLOTS;
|
|
92 }
|
|
93
|
10725
|
94 return sbr;
|
|
95 }
|
|
96
|
|
97 void sbrDecodeEnd(sbr_info *sbr)
|
|
98 {
|
|
99 uint8_t j;
|
|
100
|
|
101 if (sbr)
|
|
102 {
|
|
103 qmfa_end(sbr->qmfa[0]);
|
|
104 qmfs_end(sbr->qmfs[0]);
|
12527
|
105 if (sbr->qmfs[1] != NULL)
|
10725
|
106 {
|
|
107 qmfa_end(sbr->qmfa[1]);
|
|
108 qmfs_end(sbr->qmfs[1]);
|
|
109 }
|
|
110
|
|
111 for (j = 0; j < 5; j++)
|
|
112 {
|
12527
|
113 if (sbr->G_temp_prev[0][j]) faad_free(sbr->G_temp_prev[0][j]);
|
|
114 if (sbr->Q_temp_prev[0][j]) faad_free(sbr->Q_temp_prev[0][j]);
|
|
115 if (sbr->G_temp_prev[1][j]) faad_free(sbr->G_temp_prev[1][j]);
|
|
116 if (sbr->Q_temp_prev[1][j]) faad_free(sbr->Q_temp_prev[1][j]);
|
10725
|
117 }
|
|
118
|
12527
|
119 faad_free(sbr);
|
10725
|
120 }
|
|
121 }
|
|
122
|
12527
|
123 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
|
10725
|
124 {
|
|
125 uint8_t i;
|
|
126
|
|
127 /* save data for next frame */
|
|
128 sbr->kx_prev = sbr->kx;
|
|
129
|
|
130 sbr->L_E_prev[ch] = sbr->L_E[ch];
|
|
131
|
12527
|
132 /* sbr->L_E[ch] can become 0 on files with bit errors */
|
|
133 if (sbr->L_E[ch] <= 0)
|
|
134 return 19;
|
|
135
|
10725
|
136 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
|
|
137 for (i = 0; i < 64; i++)
|
|
138 {
|
|
139 sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
|
|
140 sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
|
|
141 }
|
|
142
|
|
143 for (i = 0; i < 64; i++)
|
|
144 {
|
|
145 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
|
|
146 }
|
|
147 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
|
|
148
|
|
149 if (sbr->l_A[ch] == sbr->L_E[ch])
|
|
150 sbr->prevEnvIsShort[ch] = 0;
|
|
151 else
|
|
152 sbr->prevEnvIsShort[ch] = -1;
|
12527
|
153
|
|
154 return 0;
|
10725
|
155 }
|
|
156
|
12527
|
157 static void sbr_process_channel(sbr_info *sbr, real_t *channel_buf, qmf_t X[MAX_NTSR][64],
|
|
158 uint8_t ch, uint8_t dont_process)
|
10725
|
159 {
|
|
160 int16_t i, k, l;
|
|
161
|
12527
|
162 #ifdef SBR_LOW_POWER
|
|
163 ALIGN real_t deg[64];
|
|
164 #endif
|
|
165
|
|
166 if (sbr->frame == 0)
|
|
167 {
|
|
168 uint8_t j;
|
|
169 sbr->qmfa[ch] = qmfa_init(32);
|
|
170 sbr->qmfs[ch] = qmfs_init(64);
|
|
171
|
|
172 for (j = 0; j < 5; j++)
|
|
173 {
|
|
174 sbr->G_temp_prev[ch][j] = faad_malloc(64*sizeof(real_t));
|
|
175 sbr->Q_temp_prev[ch][j] = faad_malloc(64*sizeof(real_t));
|
|
176 }
|
|
177
|
|
178 memset(sbr->Xsbr[ch], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
|
|
179 memset(sbr->Xcodec[ch], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*32 * sizeof(qmf_t));
|
|
180 }
|
10725
|
181
|
12527
|
182 /* subband analysis */
|
|
183 if (dont_process)
|
|
184 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xcodec[ch], sbr->tHFGen, 32);
|
|
185 else
|
|
186 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xcodec[ch], sbr->tHFGen, sbr->kx);
|
|
187
|
|
188 if (!dont_process)
|
|
189 {
|
|
190 #if 1
|
|
191 /* insert high frequencies here */
|
|
192 /* hf generation using patching */
|
|
193 hf_generation(sbr, sbr->Xcodec[ch], sbr->Xsbr[ch]
|
10725
|
194 #ifdef SBR_LOW_POWER
|
12527
|
195 ,deg
|
|
196 #endif
|
|
197 ,ch);
|
|
198 #endif
|
|
199
|
|
200 #ifdef SBR_LOW_POWER
|
|
201 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
|
|
202 {
|
|
203 for (k = 0; k < sbr->kx; k++)
|
|
204 {
|
|
205 QMF_RE(sbr->Xsbr[ch][sbr->tHFAdj + l][k]) = 0;
|
|
206 }
|
|
207 }
|
10725
|
208 #endif
|
|
209
|
12527
|
210 #if 1
|
|
211 /* hf adjustment */
|
|
212 hf_adjustment(sbr, sbr->Xsbr[ch]
|
|
213 #ifdef SBR_LOW_POWER
|
|
214 ,deg
|
|
215 #endif
|
|
216 ,ch);
|
|
217 #endif
|
10989
|
218 }
|
|
219
|
12527
|
220 if ((sbr->just_seeked != 0) || dont_process)
|
|
221 {
|
|
222 for (l = 0; l < sbr->numTimeSlotsRate; l++)
|
|
223 {
|
|
224 for (k = 0; k < 32; k++)
|
|
225 {
|
|
226 QMF_RE(X[l][k]) = QMF_RE(sbr->Xcodec[ch][l + sbr->tHFAdj][k]);
|
|
227 #ifndef SBR_LOW_POWER
|
|
228 QMF_IM(X[l][k]) = QMF_IM(sbr->Xcodec[ch][l + sbr->tHFAdj][k]);
|
|
229 #endif
|
|
230 }
|
|
231 for (k = 32; k < 64; k++)
|
|
232 {
|
|
233 QMF_RE(X[l][k]) = 0;
|
|
234 #ifndef SBR_LOW_POWER
|
|
235 QMF_IM(X[l][k]) = 0;
|
|
236 #endif
|
|
237 }
|
|
238 }
|
|
239 } else {
|
|
240 for (l = 0; l < sbr->numTimeSlotsRate; l++)
|
|
241 {
|
|
242 uint8_t xover_band;
|
|
243
|
|
244 if (l < sbr->t_E[ch][0])
|
|
245 xover_band = sbr->kx_prev;
|
|
246 else
|
|
247 xover_band = sbr->kx;
|
10989
|
248
|
12527
|
249 for (k = 0; k < xover_band; k++)
|
|
250 {
|
|
251 QMF_RE(X[l][k]) = QMF_RE(sbr->Xcodec[ch][l + sbr->tHFAdj][k]);
|
|
252 #ifndef SBR_LOW_POWER
|
|
253 QMF_IM(X[l][k]) = QMF_IM(sbr->Xcodec[ch][l + sbr->tHFAdj][k]);
|
|
254 #endif
|
|
255 }
|
|
256 for (k = xover_band; k < 64; k++)
|
|
257 {
|
|
258 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
|
|
259 #ifndef SBR_LOW_POWER
|
|
260 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
|
|
261 #endif
|
|
262 }
|
|
263 #ifdef SBR_LOW_POWER
|
|
264 QMF_RE(X[l][xover_band - 1]) += QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][xover_band - 1]);
|
|
265 #endif
|
|
266 }
|
|
267 }
|
|
268
|
|
269 for (i = 0; i < sbr->tHFGen; i++)
|
|
270 {
|
|
271 memmove(sbr->Xcodec[ch][i], sbr->Xcodec[ch][i+sbr->numTimeSlotsRate], 32 * sizeof(qmf_t));
|
|
272 memmove(sbr->Xsbr[ch][i], sbr->Xsbr[ch][i+sbr->numTimeSlotsRate], 64 * sizeof(qmf_t));
|
|
273 }
|
|
274 }
|
|
275
|
|
276 uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan,
|
|
277 const uint8_t just_seeked, const uint8_t upsample_only)
|
|
278 {
|
|
279 uint8_t dont_process = 0;
|
|
280 uint8_t ret = 0;
|
|
281 ALIGN qmf_t X[MAX_NTSR][64];
|
|
282
|
|
283 if (sbr == NULL)
|
|
284 return 20;
|
|
285
|
|
286 /* case can occur due to bit errors */
|
|
287 if (sbr->id_aac != ID_CPE)
|
|
288 return 21;
|
|
289
|
|
290 if (sbr->ret || (sbr->header_count == 0))
|
10725
|
291 {
|
|
292 /* don't process just upsample */
|
|
293 dont_process = 1;
|
10989
|
294
|
|
295 /* Re-activate reset for next frame */
|
12527
|
296 if (sbr->ret && sbr->Reset)
|
10989
|
297 sbr->bs_start_freq_prev = -1;
|
10725
|
298 }
|
|
299
|
|
300 if (just_seeked)
|
10989
|
301 {
|
10725
|
302 sbr->just_seeked = 1;
|
10989
|
303 } else {
|
10725
|
304 sbr->just_seeked = 0;
|
10989
|
305 }
|
10725
|
306
|
12527
|
307 sbr_process_channel(sbr, left_chan, X, 0, dont_process);
|
|
308 /* subband synthesis */
|
|
309 #ifndef USE_SSE
|
|
310 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, left_chan);
|
|
311 #else
|
|
312 sbr->qmfs[ch]->qmf_func(sbr, sbr->qmfs[0], X, left_chan);
|
10989
|
313 #endif
|
10725
|
314
|
12527
|
315 sbr_process_channel(sbr, right_chan, X, 1, dont_process);
|
|
316 /* subband synthesis */
|
|
317 #ifndef USE_SSE
|
|
318 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X, right_chan);
|
|
319 #else
|
|
320 sbr->qmfs[ch]->qmf_func(sbr, sbr->qmfs[1], X, right_chan);
|
10725
|
321 #endif
|
|
322
|
12527
|
323 if (sbr->bs_header_flag)
|
|
324 sbr->just_seeked = 0;
|
|
325
|
|
326 if (sbr->header_count != 0 && sbr->ret == 0)
|
|
327 {
|
|
328 ret = sbr_save_prev_data(sbr, 0);
|
|
329 if (ret) return ret;
|
|
330 ret = sbr_save_prev_data(sbr, 1);
|
|
331 if (ret) return ret;
|
|
332 }
|
|
333
|
|
334 sbr->frame++;
|
|
335
|
|
336 return 0;
|
|
337 }
|
|
338
|
|
339 uint8_t sbrDecodeSingleFrame(sbr_info *sbr, real_t *channel,
|
|
340 const uint8_t just_seeked, const uint8_t upsample_only)
|
|
341 {
|
|
342 uint8_t dont_process = 0;
|
|
343 uint8_t ret = 0;
|
|
344 ALIGN qmf_t X[MAX_NTSR][64];
|
|
345
|
|
346 if (sbr == NULL)
|
|
347 return 20;
|
10725
|
348
|
12527
|
349 /* case can occur due to bit errors */
|
|
350 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
|
|
351 return 21;
|
|
352
|
|
353 if (sbr->ret || (sbr->header_count == 0))
|
|
354 {
|
|
355 /* don't process just upsample */
|
|
356 dont_process = 1;
|
|
357
|
|
358 /* Re-activate reset for next frame */
|
|
359 if (sbr->ret && sbr->Reset)
|
|
360 sbr->bs_start_freq_prev = -1;
|
|
361 }
|
10725
|
362
|
12527
|
363 if (just_seeked)
|
|
364 {
|
|
365 sbr->just_seeked = 1;
|
|
366 } else {
|
|
367 sbr->just_seeked = 0;
|
|
368 }
|
10725
|
369
|
12527
|
370 sbr_process_channel(sbr, channel, X, 0, dont_process);
|
|
371 /* subband synthesis */
|
|
372 #ifndef USE_SSE
|
|
373 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, channel);
|
|
374 #else
|
|
375 sbr->qmfs[ch]->qmf_func(sbr, sbr->qmfs[0], X, channel);
|
10725
|
376 #endif
|
|
377
|
|
378 if (sbr->bs_header_flag)
|
|
379 sbr->just_seeked = 0;
|
|
380
|
12527
|
381 if (sbr->header_count != 0 && sbr->ret == 0)
|
10725
|
382 {
|
12527
|
383 ret = sbr_save_prev_data(sbr, 0);
|
|
384 if (ret) return ret;
|
10725
|
385 }
|
|
386
|
|
387 sbr->frame++;
|
12527
|
388
|
|
389 return 0;
|
|
390 }
|
|
391
|
|
392 static void ps_dummy_function(qmf_t X_mono[MAX_NTSR][64],
|
|
393 qmf_t X_left[MAX_NTSR][64], qmf_t X_right[MAX_NTSR][64])
|
|
394 {
|
|
395 uint8_t i, j;
|
|
396
|
|
397 for (i = 0; i < MAX_NTSR; i++)
|
|
398 {
|
|
399 for (j = 0; j < 64; j++)
|
|
400 {
|
|
401 QMF_RE(X_left[i][j]) = QMF_RE(X_mono[i][j]);
|
|
402 QMF_RE(X_right[i][j]) = QMF_RE(X_mono[i][j]);
|
|
403 #ifndef SBR_LOW_POWER
|
|
404 QMF_IM(X_left[i][j]) = QMF_IM(X_mono[i][j]);
|
|
405 QMF_IM(X_right[i][j]) = QMF_IM(X_mono[i][j]);
|
|
406 #endif
|
|
407 }
|
|
408 }
|
10725
|
409 }
|
|
410
|
12527
|
411 #if (defined(PS_DEC) || defined(DRM_PS))
|
|
412 uint8_t sbrDecodeSingleFramePS(sbr_info *sbr, real_t *left_channel, real_t *right_channel,
|
|
413 const uint8_t just_seeked, const uint8_t upsample_only)
|
|
414 {
|
|
415 uint8_t dont_process = 0;
|
|
416 uint8_t ret = 0;
|
|
417 ALIGN qmf_t X_mono[MAX_NTSR][64];
|
|
418 ALIGN qmf_t X_left[MAX_NTSR][64];
|
|
419 ALIGN qmf_t X_right[MAX_NTSR][64];
|
|
420
|
|
421 if (sbr == NULL)
|
|
422 return 20;
|
|
423
|
|
424 /* case can occur due to bit errors */
|
|
425 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
|
|
426 return 21;
|
|
427
|
|
428 if (sbr->ret || (sbr->header_count == 0))
|
|
429 {
|
|
430 /* don't process just upsample */
|
|
431 dont_process = 1;
|
|
432
|
|
433 /* Re-activate reset for next frame */
|
|
434 if (sbr->ret && sbr->Reset)
|
|
435 sbr->bs_start_freq_prev = -1;
|
|
436 }
|
|
437
|
|
438 if (just_seeked)
|
|
439 {
|
|
440 sbr->just_seeked = 1;
|
|
441 } else {
|
|
442 sbr->just_seeked = 0;
|
|
443 }
|
|
444
|
|
445 if (sbr->frame == 0)
|
|
446 {
|
|
447 sbr->qmfs[1] = qmfs_init(64);
|
|
448 }
|
|
449
|
|
450 sbr_process_channel(sbr, left_channel, X_mono, 0, dont_process);
|
|
451
|
|
452 /* perform parametric stereo */
|
|
453 ps_dummy_function(X_mono, X_left, X_right);
|
|
454
|
|
455 /* subband synthesis */
|
|
456 #ifndef USE_SSE
|
|
457 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X_left, left_channel);
|
|
458 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X_right, right_channel);
|
|
459 #else
|
|
460 sbr->qmfs[ch]->qmf_func(sbr, sbr->qmfs[0], X_left, left_channel);
|
|
461 sbr->qmfs[ch]->qmf_func(sbr, sbr->qmfs[1], X_right, right_channel);
|
10725
|
462 #endif
|
12527
|
463
|
|
464 if (sbr->bs_header_flag)
|
|
465 sbr->just_seeked = 0;
|
|
466
|
|
467 if (sbr->header_count != 0 && sbr->ret == 0)
|
|
468 {
|
|
469 ret = sbr_save_prev_data(sbr, 0);
|
|
470 if (ret) return ret;
|
|
471 }
|
|
472
|
|
473 sbr->frame++;
|
|
474
|
|
475 return 0;
|
|
476 }
|
|
477 #endif
|
|
478
|
|
479 #endif
|