10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
10989
|
25 ** $Id: sbr_dec.c,v 1.12 2003/09/25 12:04:31 menno Exp $
|
10725
|
26 **/
|
|
27
|
|
28
|
|
29 #include "common.h"
|
|
30 #include "structs.h"
|
|
31
|
|
32 #ifdef SBR_DEC
|
|
33
|
|
34 #include <stdlib.h>
|
|
35
|
|
36 #include "syntax.h"
|
|
37 #include "bits.h"
|
|
38 #include "sbr_syntax.h"
|
|
39 #include "sbr_qmf.h"
|
|
40 #include "sbr_hfgen.h"
|
|
41 #include "sbr_hfadj.h"
|
|
42
|
|
43
|
10989
|
44 sbr_info *sbrDecodeInit(uint16_t framelength
|
|
45 #ifdef DRM
|
|
46 , uint8_t IsDRM
|
|
47 #endif
|
|
48 )
|
10725
|
49 {
|
|
50 sbr_info *sbr = malloc(sizeof(sbr_info));
|
|
51 memset(sbr, 0, sizeof(sbr_info));
|
|
52
|
|
53 sbr->bs_freq_scale = 2;
|
|
54 sbr->bs_alter_scale = 1;
|
|
55 sbr->bs_noise_bands = 2;
|
|
56 sbr->bs_limiter_bands = 2;
|
|
57 sbr->bs_limiter_gains = 2;
|
|
58 sbr->bs_interpol_freq = 1;
|
|
59 sbr->bs_smoothing_mode = 1;
|
|
60 sbr->bs_start_freq = 5;
|
|
61 sbr->bs_amp_res = 1;
|
|
62 sbr->bs_samplerate_mode = 1;
|
|
63 sbr->prevEnvIsShort[0] = -1;
|
|
64 sbr->prevEnvIsShort[1] = -1;
|
|
65 sbr->header_count = 0;
|
|
66
|
10989
|
67 #ifdef DRM
|
|
68 sbr->Is_DRM_SBR = 0;
|
|
69 if (IsDRM)
|
|
70 {
|
|
71 sbr->Is_DRM_SBR = 1;
|
|
72 sbr->tHFGen = T_HFGEN_DRM;
|
|
73 sbr->tHFAdj = T_HFADJ_DRM;
|
|
74
|
|
75 /* "offset" is different in DRM */
|
|
76 sbr->bs_samplerate_mode = 0;
|
|
77 } else
|
|
78 #endif
|
|
79 {
|
|
80 sbr->bs_samplerate_mode = 1;
|
|
81 sbr->tHFGen = T_HFGEN;
|
|
82 sbr->tHFAdj = T_HFADJ;
|
|
83 }
|
|
84
|
|
85 /* force sbr reset */
|
|
86 sbr->bs_start_freq_prev = -1;
|
|
87
|
|
88 if (framelength == 960)
|
|
89 {
|
|
90 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS_960;
|
|
91 sbr->numTimeSlots = NO_TIME_SLOTS_960;
|
|
92 } else {
|
|
93 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS;
|
|
94 sbr->numTimeSlots = NO_TIME_SLOTS;
|
|
95 }
|
|
96
|
10725
|
97 return sbr;
|
|
98 }
|
|
99
|
|
100 void sbrDecodeEnd(sbr_info *sbr)
|
|
101 {
|
|
102 uint8_t j;
|
|
103
|
|
104 if (sbr)
|
|
105 {
|
|
106 qmfa_end(sbr->qmfa[0]);
|
|
107 qmfs_end(sbr->qmfs[0]);
|
|
108 if (sbr->id_aac == ID_CPE)
|
|
109 {
|
|
110 qmfa_end(sbr->qmfa[1]);
|
|
111 qmfs_end(sbr->qmfs[1]);
|
|
112 }
|
|
113
|
|
114 if (sbr->Xcodec[0]) free(sbr->Xcodec[0]);
|
|
115 if (sbr->Xsbr[0]) free(sbr->Xsbr[0]);
|
|
116 if (sbr->Xcodec[1]) free(sbr->Xcodec[1]);
|
|
117 if (sbr->Xsbr[1]) free(sbr->Xsbr[1]);
|
|
118
|
|
119 for (j = 0; j < 5; j++)
|
|
120 {
|
|
121 if (sbr->G_temp_prev[0][j]) free(sbr->G_temp_prev[0][j]);
|
|
122 if (sbr->Q_temp_prev[0][j]) free(sbr->Q_temp_prev[0][j]);
|
|
123 if (sbr->G_temp_prev[1][j]) free(sbr->G_temp_prev[1][j]);
|
|
124 if (sbr->Q_temp_prev[1][j]) free(sbr->Q_temp_prev[1][j]);
|
|
125 }
|
|
126
|
|
127 free(sbr);
|
|
128 }
|
|
129 }
|
|
130
|
|
131 void sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
|
|
132 {
|
|
133 uint8_t i;
|
|
134
|
|
135 /* save data for next frame */
|
|
136 sbr->kx_prev = sbr->kx;
|
|
137
|
|
138 sbr->L_E_prev[ch] = sbr->L_E[ch];
|
|
139
|
|
140 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
|
|
141 for (i = 0; i < 64; i++)
|
|
142 {
|
|
143 sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
|
|
144 sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
|
|
145 }
|
|
146
|
|
147 for (i = 0; i < 64; i++)
|
|
148 {
|
|
149 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
|
|
150 }
|
|
151 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
|
|
152
|
|
153 if (sbr->l_A[ch] == sbr->L_E[ch])
|
|
154 sbr->prevEnvIsShort[ch] = 0;
|
|
155 else
|
|
156 sbr->prevEnvIsShort[ch] = -1;
|
|
157 }
|
|
158
|
|
159 void sbrDecodeFrame(sbr_info *sbr, real_t *left_channel,
|
|
160 real_t *right_channel, uint8_t id_aac,
|
10989
|
161 uint8_t just_seeked, uint8_t upsample_only)
|
10725
|
162 {
|
|
163 int16_t i, k, l;
|
|
164
|
|
165 uint8_t dont_process = 0;
|
|
166 uint8_t ch, channels, ret;
|
|
167 real_t *ch_buf;
|
|
168
|
|
169 qmf_t X[32*64];
|
|
170 #ifdef SBR_LOW_POWER
|
|
171 real_t deg[64];
|
|
172 #endif
|
|
173
|
10989
|
174 bitfile *ld = NULL;
|
10725
|
175
|
|
176 sbr->id_aac = id_aac;
|
|
177 channels = (id_aac == ID_SCE) ? 1 : 2;
|
|
178
|
10989
|
179 if (sbr->data == NULL || sbr->data_size == 0)
|
|
180 {
|
|
181 ret = 1;
|
|
182 } else {
|
|
183 ld = (bitfile*)malloc(sizeof(bitfile));
|
|
184
|
10725
|
185 /* initialise and read the bitstream */
|
|
186 faad_initbits(ld, sbr->data, sbr->data_size);
|
|
187
|
|
188 ret = sbr_extension_data(ld, sbr, id_aac);
|
|
189
|
|
190 ret = ld->error ? ld->error : ret;
|
|
191 faad_endbits(ld);
|
|
192 if (ld) free(ld);
|
|
193 ld = NULL;
|
10989
|
194 }
|
|
195
|
|
196 if (sbr->data) free(sbr->data);
|
|
197 sbr->data = NULL;
|
|
198
|
10725
|
199 if (ret || (sbr->header_count == 0))
|
|
200 {
|
|
201 /* don't process just upsample */
|
|
202 dont_process = 1;
|
10989
|
203
|
|
204 /* Re-activate reset for next frame */
|
|
205 if (ret && sbr->Reset)
|
|
206 sbr->bs_start_freq_prev = -1;
|
10725
|
207 }
|
|
208
|
|
209 if (just_seeked)
|
10989
|
210 {
|
10725
|
211 sbr->just_seeked = 1;
|
10989
|
212 } else {
|
10725
|
213 sbr->just_seeked = 0;
|
10989
|
214 }
|
10725
|
215
|
|
216 for (ch = 0; ch < channels; ch++)
|
|
217 {
|
|
218 if (sbr->frame == 0)
|
|
219 {
|
|
220 uint8_t j;
|
|
221 sbr->qmfa[ch] = qmfa_init(32);
|
|
222 sbr->qmfs[ch] = qmfs_init(64);
|
|
223
|
|
224 for (j = 0; j < 5; j++)
|
|
225 {
|
|
226 sbr->G_temp_prev[ch][j] = malloc(64*sizeof(real_t));
|
|
227 sbr->Q_temp_prev[ch][j] = malloc(64*sizeof(real_t));
|
|
228 }
|
|
229
|
10989
|
230 sbr->Xsbr[ch] = malloc((sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
|
|
231 sbr->Xcodec[ch] = malloc((sbr->numTimeSlotsRate+sbr->tHFGen)*32 * sizeof(qmf_t));
|
10725
|
232
|
10989
|
233 memset(sbr->Xsbr[ch], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
|
|
234 memset(sbr->Xcodec[ch], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*32 * sizeof(qmf_t));
|
10725
|
235 }
|
|
236
|
|
237 if (ch == 0)
|
|
238 ch_buf = left_channel;
|
|
239 else
|
|
240 ch_buf = right_channel;
|
|
241
|
10989
|
242 #if 0
|
|
243 for (i = 0; i < sbr->tHFAdj; i++)
|
10725
|
244 {
|
|
245 int8_t j;
|
|
246 for (j = sbr->kx_prev; j < sbr->kx; j++)
|
|
247 {
|
|
248 QMF_RE(sbr->Xcodec[ch][i*32 + j]) = 0;
|
|
249 #ifndef SBR_LOW_POWER
|
|
250 QMF_IM(sbr->Xcodec[ch][i*32 + j]) = 0;
|
|
251 #endif
|
|
252 }
|
|
253 }
|
10989
|
254 #endif
|
10725
|
255
|
|
256 /* subband analysis */
|
10989
|
257 if (dont_process)
|
|
258 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], ch_buf, sbr->Xcodec[ch], sbr->tHFGen, 32);
|
|
259 else
|
|
260 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], ch_buf, sbr->Xcodec[ch], sbr->tHFGen, sbr->kx);
|
10725
|
261
|
|
262 if (!dont_process)
|
|
263 {
|
|
264 /* insert high frequencies here */
|
|
265 /* hf generation using patching */
|
|
266 hf_generation(sbr, sbr->Xcodec[ch], sbr->Xsbr[ch]
|
|
267 #ifdef SBR_LOW_POWER
|
|
268 ,deg
|
|
269 #endif
|
|
270 ,ch);
|
|
271
|
|
272 #ifdef SBR_LOW_POWER
|
|
273 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
|
|
274 {
|
|
275 for (k = 0; k < sbr->kx; k++)
|
|
276 {
|
10989
|
277 QMF_RE(sbr->Xsbr[ch][(sbr->tHFAdj + l)*64 + k]) = 0;
|
10725
|
278 }
|
|
279 }
|
|
280 #endif
|
|
281
|
10989
|
282 #if 1
|
10725
|
283 /* hf adjustment */
|
|
284 hf_adjustment(sbr, sbr->Xsbr[ch]
|
|
285 #ifdef SBR_LOW_POWER
|
|
286 ,deg
|
|
287 #endif
|
|
288 ,ch);
|
10989
|
289 #endif
|
10725
|
290 }
|
|
291
|
|
292 if ((sbr->just_seeked != 0) || dont_process)
|
|
293 {
|
10989
|
294 for (l = 0; l < sbr->numTimeSlotsRate; l++)
|
10725
|
295 {
|
|
296 for (k = 0; k < 32; k++)
|
|
297 {
|
10989
|
298 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
10725
|
299 #ifndef SBR_LOW_POWER
|
10989
|
300 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
10725
|
301 #endif
|
|
302 }
|
|
303 for (k = 32; k < 64; k++)
|
|
304 {
|
|
305 QMF_RE(X[l * 64 + k]) = 0;
|
|
306 #ifndef SBR_LOW_POWER
|
|
307 QMF_IM(X[l * 64 + k]) = 0;
|
|
308 #endif
|
|
309 }
|
|
310 }
|
|
311 } else {
|
10989
|
312 for (l = 0; l < sbr->numTimeSlotsRate; l++)
|
10725
|
313 {
|
|
314 uint8_t xover_band;
|
|
315
|
|
316 if (l < sbr->t_E[ch][0])
|
|
317 xover_band = sbr->kx_prev;
|
|
318 else
|
|
319 xover_band = sbr->kx;
|
|
320
|
10989
|
321 #ifdef DRM
|
|
322 if (sbr->Is_DRM_SBR)
|
|
323 xover_band = sbr->kx;
|
|
324 #endif
|
|
325
|
10725
|
326 for (k = 0; k < xover_band; k++)
|
|
327 {
|
10989
|
328 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
10725
|
329 #ifndef SBR_LOW_POWER
|
10989
|
330 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
10725
|
331 #endif
|
|
332 }
|
|
333 for (k = xover_band; k < 64; k++)
|
|
334 {
|
10989
|
335 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xsbr[ch][(l + sbr->tHFAdj)*64 + k]);
|
10725
|
336 #ifndef SBR_LOW_POWER
|
10989
|
337 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xsbr[ch][(l + sbr->tHFAdj)*64 + k]);
|
10725
|
338 #endif
|
|
339 }
|
|
340 #ifdef SBR_LOW_POWER
|
10989
|
341 QMF_RE(X[l * 64 + xover_band - 1]) += QMF_RE(sbr->Xsbr[ch][(l + sbr->tHFAdj)*64 + xover_band - 1]);
|
|
342 #endif
|
|
343 #ifdef DRM
|
|
344 if (sbr->Is_DRM_SBR)
|
|
345 {
|
|
346 for (k = xover_band; k < xover_band + 4; k++)
|
|
347 {
|
|
348 QMF_RE(X[l * 64 + k]) += QMF_RE(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
|
349 QMF_IM(X[l * 64 + k]) += QMF_IM(sbr->Xcodec[ch][(l + sbr->tHFAdj)*32 + k]);
|
|
350 }
|
|
351 }
|
10725
|
352 #endif
|
|
353 }
|
|
354 }
|
|
355
|
|
356 /* subband synthesis */
|
10989
|
357 sbr_qmf_synthesis_64(sbr, sbr->qmfs[ch], (const complex_t*)X, ch_buf);
|
10725
|
358
|
|
359 for (i = 0; i < 32; i++)
|
|
360 {
|
|
361 int8_t j;
|
10989
|
362 for (j = 0; j < sbr->tHFGen; j++)
|
10725
|
363 {
|
10989
|
364 QMF_RE(sbr->Xcodec[ch][j*32 + i]) = QMF_RE(sbr->Xcodec[ch][(j+sbr->numTimeSlotsRate)*32 + i]);
|
10725
|
365 #ifndef SBR_LOW_POWER
|
10989
|
366 QMF_IM(sbr->Xcodec[ch][j*32 + i]) = QMF_IM(sbr->Xcodec[ch][(j+sbr->numTimeSlotsRate)*32 + i]);
|
10725
|
367 #endif
|
|
368 }
|
|
369 }
|
|
370 for (i = 0; i < 64; i++)
|
|
371 {
|
|
372 int8_t j;
|
10989
|
373 for (j = 0; j < sbr->tHFGen; j++)
|
10725
|
374 {
|
10989
|
375 QMF_RE(sbr->Xsbr[ch][j*64 + i]) = QMF_RE(sbr->Xsbr[ch][(j+sbr->numTimeSlotsRate)*64 + i]);
|
10725
|
376 #ifndef SBR_LOW_POWER
|
10989
|
377 QMF_IM(sbr->Xsbr[ch][j*64 + i]) = QMF_IM(sbr->Xsbr[ch][(j+sbr->numTimeSlotsRate)*64 + i]);
|
10725
|
378 #endif
|
|
379 }
|
|
380 }
|
|
381 }
|
|
382
|
|
383 if (sbr->bs_header_flag)
|
|
384 sbr->just_seeked = 0;
|
|
385
|
|
386 if (sbr->header_count != 0)
|
|
387 {
|
|
388 for (ch = 0; ch < channels; ch++)
|
|
389 sbr_save_prev_data(sbr, ch);
|
|
390 }
|
|
391
|
|
392 sbr->frame++;
|
|
393 }
|
|
394
|
|
395 #endif
|