comparison libfaad2/sbr_dec.c @ 10725:e989150f8216

libfaad2 v2.0rc1 imported
author arpi
date Sat, 30 Aug 2003 22:30:28 +0000
parents
children 3185f64f6350
comparison
equal deleted inserted replaced
10724:adf5697b9d83 10725:e989150f8216
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 **
25 ** $Id: sbr_dec.c,v 1.5 2003/07/29 08:20:13 menno Exp $
26 **/
27
28 /*
29 SBR Decoder overview:
30
31 To achieve a synchronized output signal, the following steps have to be
32 acknowledged in the decoder:
33 - The bitstream parser divides the bitstream into two parts; the AAC
34 core coder part and the SBR part.
35 - The SBR bitstream part is fed to the bitstream de-multiplexer followed
36 by de-quantization The raw data is Huffman decoded.
37 - The AAC bitstream part is fed to the AAC core decoder, where the
38 bitstream data of the current frame is decoded, yielding a time domain
39 audio signal block of 1024 samples. The block length could easily be
40 adapted to other sizes e.g. 960.
41 - The core coder audio block is fed to the analysis QMF bank using a
42 delay of 1312 samples.
43 - The analysis QMF bank performs the filtering of the delayed core coder
44 audio signal. The output from the filtering is stored in the matrix
45 Xlow. The output from the analysis QMF bank is delayed tHFGen subband
46 samples, before being fed to the synthesis QMF bank. To achieve
47 synchronization tHFGen = 32, i.e. the value must equal the number of
48 subband samples corresponding to one frame.
49 - The HF generator calculates XHigh given the matrix XLow. The process
50 is guided by the SBR data contained in the current frame.
51 - The envelope adjuster calculates the matrix Y given the matrix XHigh
52 and the SBR envelope data, extracted from the SBR bitstream. To
53 achieve synchronization, tHFAdj has to be set to tHFAdj = 0, i.e. the
54 envelope adjuster operates on data delayed tHFGen subband samples.
55 - The synthesis QMF bank operates on the delayed output from the analysis
56 QMF bank and the output from the envelope adjuster.
57 */
58
59 #include "common.h"
60 #include "structs.h"
61
62 #ifdef SBR_DEC
63
64 #include <stdlib.h>
65
66 #include "syntax.h"
67 #include "bits.h"
68 #include "sbr_syntax.h"
69 #include "sbr_qmf.h"
70 #include "sbr_hfgen.h"
71 #include "sbr_hfadj.h"
72
73
74 sbr_info *sbrDecodeInit()
75 {
76 sbr_info *sbr = malloc(sizeof(sbr_info));
77 memset(sbr, 0, sizeof(sbr_info));
78
79 sbr->bs_freq_scale = 2;
80 sbr->bs_alter_scale = 1;
81 sbr->bs_noise_bands = 2;
82 sbr->bs_limiter_bands = 2;
83 sbr->bs_limiter_gains = 2;
84 sbr->bs_interpol_freq = 1;
85 sbr->bs_smoothing_mode = 1;
86 sbr->bs_start_freq = 5;
87 sbr->bs_amp_res = 1;
88 sbr->bs_samplerate_mode = 1;
89 sbr->prevEnvIsShort[0] = -1;
90 sbr->prevEnvIsShort[1] = -1;
91 sbr->header_count = 0;
92
93 return sbr;
94 }
95
96 void sbrDecodeEnd(sbr_info *sbr)
97 {
98 uint8_t j;
99
100 if (sbr)
101 {
102 qmfa_end(sbr->qmfa[0]);
103 qmfs_end(sbr->qmfs[0]);
104 if (sbr->id_aac == ID_CPE)
105 {
106 qmfa_end(sbr->qmfa[1]);
107 qmfs_end(sbr->qmfs[1]);
108 }
109
110 if (sbr->Xcodec[0]) free(sbr->Xcodec[0]);
111 if (sbr->Xsbr[0]) free(sbr->Xsbr[0]);
112 if (sbr->Xcodec[1]) free(sbr->Xcodec[1]);
113 if (sbr->Xsbr[1]) free(sbr->Xsbr[1]);
114
115 for (j = 0; j < 5; j++)
116 {
117 if (sbr->G_temp_prev[0][j]) free(sbr->G_temp_prev[0][j]);
118 if (sbr->Q_temp_prev[0][j]) free(sbr->Q_temp_prev[0][j]);
119 if (sbr->G_temp_prev[1][j]) free(sbr->G_temp_prev[1][j]);
120 if (sbr->Q_temp_prev[1][j]) free(sbr->Q_temp_prev[1][j]);
121 }
122
123 free(sbr);
124 }
125 }
126
127 void sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
128 {
129 uint8_t i;
130
131 /* save data for next frame */
132 sbr->kx_prev = sbr->kx;
133
134 sbr->L_E_prev[ch] = sbr->L_E[ch];
135
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;
153 }
154
155
156 void sbrDecodeFrame(sbr_info *sbr, real_t *left_channel,
157 real_t *right_channel, uint8_t id_aac,
158 uint8_t just_seeked)
159 {
160 int16_t i, k, l;
161
162 uint8_t dont_process = 0;
163 uint8_t ch, channels, ret;
164 real_t *ch_buf;
165
166 qmf_t X[32*64];
167 #ifdef SBR_LOW_POWER
168 real_t deg[64];
169 #endif
170
171 bitfile *ld = (bitfile*)malloc(sizeof(bitfile));
172
173
174 sbr->id_aac = id_aac;
175 channels = (id_aac == ID_SCE) ? 1 : 2;
176
177 /* initialise and read the bitstream */
178 faad_initbits(ld, sbr->data, sbr->data_size);
179
180 ret = sbr_extension_data(ld, sbr, id_aac);
181
182 if (sbr->data) free(sbr->data);
183 sbr->data = NULL;
184
185 ret = ld->error ? ld->error : ret;
186 faad_endbits(ld);
187 if (ld) free(ld);
188 ld = NULL;
189 if (ret || (sbr->header_count == 0))
190 {
191 /* don't process just upsample */
192 dont_process = 1;
193 }
194
195 if (just_seeked)
196 sbr->just_seeked = 1;
197 else
198 sbr->just_seeked = 0;
199
200 for (ch = 0; ch < channels; ch++)
201 {
202 if (sbr->frame == 0)
203 {
204 uint8_t j;
205 sbr->qmfa[ch] = qmfa_init(32);
206 sbr->qmfs[ch] = qmfs_init(64);
207
208 for (j = 0; j < 5; j++)
209 {
210 sbr->G_temp_prev[ch][j] = malloc(64*sizeof(real_t));
211 sbr->Q_temp_prev[ch][j] = malloc(64*sizeof(real_t));
212 }
213
214 sbr->Xsbr[ch] = malloc((32+tHFGen)*64 * sizeof(qmf_t));
215 sbr->Xcodec[ch] = malloc((32+tHFGen)*32 * sizeof(qmf_t));
216
217 memset(sbr->Xsbr[ch], 0, (32+tHFGen)*64 * sizeof(qmf_t));
218 memset(sbr->Xcodec[ch], 0, (32+tHFGen)*32 * sizeof(qmf_t));
219 }
220
221 if (ch == 0)
222 ch_buf = left_channel;
223 else
224 ch_buf = right_channel;
225
226 for (i = 0; i < tHFAdj; i++)
227 {
228 int8_t j;
229 for (j = sbr->kx_prev; j < sbr->kx; j++)
230 {
231 QMF_RE(sbr->Xcodec[ch][i*32 + j]) = 0;
232 #ifndef SBR_LOW_POWER
233 QMF_IM(sbr->Xcodec[ch][i*32 + j]) = 0;
234 #endif
235 }
236 }
237
238 /* subband analysis */
239 sbr_qmf_analysis_32(sbr->qmfa[ch], ch_buf, sbr->Xcodec[ch], tHFGen);
240
241 if (!dont_process)
242 {
243 /* insert high frequencies here */
244 /* hf generation using patching */
245 hf_generation(sbr, sbr->Xcodec[ch], sbr->Xsbr[ch]
246 #ifdef SBR_LOW_POWER
247 ,deg
248 #endif
249 ,ch);
250
251 #ifdef SBR_LOW_POWER
252 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
253 {
254 for (k = 0; k < sbr->kx; k++)
255 {
256 QMF_RE(sbr->Xsbr[ch][(tHFAdj + l)*64 + k]) = 0;
257 }
258 }
259 #endif
260
261 /* hf adjustment */
262 hf_adjustment(sbr, sbr->Xsbr[ch]
263 #ifdef SBR_LOW_POWER
264 ,deg
265 #endif
266 ,ch);
267 }
268
269 if ((sbr->just_seeked != 0) || dont_process)
270 {
271 for (l = 0; l < 32; l++)
272 {
273 for (k = 0; k < 32; k++)
274 {
275 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xcodec[ch][(l + tHFAdj)*32 + k]);
276 #ifndef SBR_LOW_POWER
277 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xcodec[ch][(l + tHFAdj)*32 + k]);
278 #endif
279 }
280 for (k = 32; k < 64; k++)
281 {
282 QMF_RE(X[l * 64 + k]) = 0;
283 #ifndef SBR_LOW_POWER
284 QMF_IM(X[l * 64 + k]) = 0;
285 #endif
286 }
287 }
288 } else {
289 for (l = 0; l < 32; l++)
290 {
291 uint8_t xover_band;
292
293 if (l < sbr->t_E[ch][0])
294 xover_band = sbr->kx_prev;
295 else
296 xover_band = sbr->kx;
297
298 for (k = 0; k < xover_band; k++)
299 {
300 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xcodec[ch][(l + tHFAdj)*32 + k]);
301 #ifndef SBR_LOW_POWER
302 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xcodec[ch][(l + tHFAdj)*32 + k]);
303 #endif
304 }
305 for (k = xover_band; k < 64; k++)
306 {
307 QMF_RE(X[l * 64 + k]) = QMF_RE(sbr->Xsbr[ch][(l + tHFAdj)*64 + k]);
308 #ifndef SBR_LOW_POWER
309 QMF_IM(X[l * 64 + k]) = QMF_IM(sbr->Xsbr[ch][(l + tHFAdj)*64 + k]);
310 #endif
311 }
312 #ifdef SBR_LOW_POWER
313 QMF_RE(X[l * 64 + xover_band - 1]) += QMF_RE(sbr->Xsbr[ch][(l + tHFAdj)*64 + xover_band - 1]);
314 #endif
315 }
316 }
317
318 /* subband synthesis */
319 sbr_qmf_synthesis_64(sbr->qmfs[ch], (const complex_t*)X, ch_buf);
320
321 for (i = 0; i < 32; i++)
322 {
323 int8_t j;
324 for (j = 0; j < tHFGen; j++)
325 {
326 QMF_RE(sbr->Xcodec[ch][j*32 + i]) = QMF_RE(sbr->Xcodec[ch][(j+32)*32 + i]);
327 #ifndef SBR_LOW_POWER
328 QMF_IM(sbr->Xcodec[ch][j*32 + i]) = QMF_IM(sbr->Xcodec[ch][(j+32)*32 + i]);
329 #endif
330 }
331 }
332 for (i = 0; i < 64; i++)
333 {
334 int8_t j;
335 for (j = 0; j < tHFGen; j++)
336 {
337 QMF_RE(sbr->Xsbr[ch][j*64 + i]) = QMF_RE(sbr->Xsbr[ch][(j+32)*64 + i]);
338 #ifndef SBR_LOW_POWER
339 QMF_IM(sbr->Xsbr[ch][j*64 + i]) = QMF_IM(sbr->Xsbr[ch][(j+32)*64 + i]);
340 #endif
341 }
342 }
343 }
344
345 if (sbr->bs_header_flag)
346 sbr->just_seeked = 0;
347
348 if (sbr->header_count != 0)
349 {
350 for (ch = 0; ch < channels; ch++)
351 sbr_save_prev_data(sbr, ch);
352 }
353
354 sbr->frame++;
355 }
356
357 #endif