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