Use extra_cflags variable instead of CFLAGS to add system-specific CFLAGS.
[mplayer/glamo.git] / libfaad2 / sbr_dec.c
blob2ad36ff8fa8e2e5fabc3675258f322358c952621
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** Initially modified for use with MPlayer on 2005/12/05
26 ** $Id: sbr_dec.c,v 1.39 2004/09/04 14:56:28 menno Exp $
27 ** detailed changelog at http://svn.mplayerhq.hu/mplayer/trunk/
28 ** local_changes.diff contains the exact changes to this file.
29 **/
32 #include "common.h"
33 #include "structs.h"
35 #ifdef SBR_DEC
37 #include <string.h>
38 #include <stdlib.h>
40 #include "syntax.h"
41 #include "bits.h"
42 #include "sbr_syntax.h"
43 #include "sbr_qmf.h"
44 #include "sbr_hfgen.h"
45 #include "sbr_hfadj.h"
48 /* static function declarations */
49 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
50 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch);
53 sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
54 uint32_t sample_rate, uint8_t downSampledSBR
55 #ifdef DRM
56 , uint8_t IsDRM
57 #endif
60 sbr_info *sbr = faad_malloc(sizeof(sbr_info));
61 memset(sbr, 0, sizeof(sbr_info));
63 /* save id of the parent element */
64 sbr->id_aac = id_aac;
65 sbr->sample_rate = sample_rate;
67 sbr->bs_freq_scale = 2;
68 sbr->bs_alter_scale = 1;
69 sbr->bs_noise_bands = 2;
70 sbr->bs_limiter_bands = 2;
71 sbr->bs_limiter_gains = 2;
72 sbr->bs_interpol_freq = 1;
73 sbr->bs_smoothing_mode = 1;
74 sbr->bs_start_freq = 5;
75 sbr->bs_amp_res = 1;
76 sbr->bs_samplerate_mode = 1;
77 sbr->prevEnvIsShort[0] = -1;
78 sbr->prevEnvIsShort[1] = -1;
79 sbr->header_count = 0;
80 sbr->Reset = 1;
82 #ifdef DRM
83 sbr->Is_DRM_SBR = IsDRM;
84 #endif
85 sbr->tHFGen = T_HFGEN;
86 sbr->tHFAdj = T_HFADJ;
88 sbr->bsco = 0;
89 sbr->bsco_prev = 0;
90 sbr->M_prev = 0;
91 sbr->frame_len = framelength;
93 /* force sbr reset */
94 sbr->bs_start_freq_prev = -1;
96 if (framelength == 960)
98 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS_960;
99 sbr->numTimeSlots = NO_TIME_SLOTS_960;
100 } else {
101 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS;
102 sbr->numTimeSlots = NO_TIME_SLOTS;
105 sbr->GQ_ringbuf_index[0] = 0;
106 sbr->GQ_ringbuf_index[1] = 0;
108 if (id_aac == ID_CPE)
110 /* stereo */
111 uint8_t j;
112 sbr->qmfa[0] = qmfa_init(32);
113 sbr->qmfa[1] = qmfa_init(32);
114 sbr->qmfs[0] = qmfs_init((downSampledSBR)?32:64);
115 sbr->qmfs[1] = qmfs_init((downSampledSBR)?32:64);
117 for (j = 0; j < 5; j++)
119 sbr->G_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
120 sbr->G_temp_prev[1][j] = faad_malloc(64*sizeof(real_t));
121 sbr->Q_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
122 sbr->Q_temp_prev[1][j] = faad_malloc(64*sizeof(real_t));
125 memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
126 memset(sbr->Xsbr[1], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
127 } else {
128 /* mono */
129 uint8_t j;
130 sbr->qmfa[0] = qmfa_init(32);
131 sbr->qmfs[0] = qmfs_init((downSampledSBR)?32:64);
132 sbr->qmfs[1] = NULL;
134 for (j = 0; j < 5; j++)
136 sbr->G_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
137 sbr->Q_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
140 memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
143 return sbr;
146 void sbrDecodeEnd(sbr_info *sbr)
148 uint8_t j;
150 if (sbr)
152 qmfa_end(sbr->qmfa[0]);
153 qmfs_end(sbr->qmfs[0]);
154 if (sbr->qmfs[1] != NULL)
156 qmfa_end(sbr->qmfa[1]);
157 qmfs_end(sbr->qmfs[1]);
160 for (j = 0; j < 5; j++)
162 if (sbr->G_temp_prev[0][j]) faad_free(sbr->G_temp_prev[0][j]);
163 if (sbr->Q_temp_prev[0][j]) faad_free(sbr->Q_temp_prev[0][j]);
164 if (sbr->G_temp_prev[1][j]) faad_free(sbr->G_temp_prev[1][j]);
165 if (sbr->Q_temp_prev[1][j]) faad_free(sbr->Q_temp_prev[1][j]);
168 #ifdef PS_DEC
169 if (sbr->ps != NULL)
170 ps_free(sbr->ps);
171 #endif
173 #ifdef DRM_PS
174 if (sbr->drm_ps != NULL)
175 drm_ps_free(sbr->drm_ps);
176 #endif
178 faad_free(sbr);
182 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
184 uint8_t i;
186 /* save data for next frame */
187 sbr->kx_prev = sbr->kx;
188 sbr->M_prev = sbr->M;
189 sbr->bsco_prev = sbr->bsco;
191 sbr->L_E_prev[ch] = sbr->L_E[ch];
193 /* sbr->L_E[ch] can become 0 on files with bit errors */
194 if (sbr->L_E[ch] <= 0)
195 return 19;
197 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
198 for (i = 0; i < MAX_M; i++)
200 sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
201 sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
204 for (i = 0; i < MAX_M; i++)
206 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
208 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
210 if (sbr->l_A[ch] == sbr->L_E[ch])
211 sbr->prevEnvIsShort[ch] = 0;
212 else
213 sbr->prevEnvIsShort[ch] = -1;
215 return 0;
218 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch)
220 uint8_t i;
222 for (i = 0; i < sbr->tHFGen; i++)
224 memmove(sbr->Xsbr[ch][i], sbr->Xsbr[ch][i+sbr->numTimeSlotsRate], 64 * sizeof(qmf_t));
226 for (i = sbr->tHFGen; i < MAX_NTSRHFG; i++)
228 memset(sbr->Xsbr[ch][i], 0, 64 * sizeof(qmf_t));
232 static void sbr_process_channel(sbr_info *sbr, real_t *channel_buf, qmf_t X[MAX_NTSR][64],
233 uint8_t ch, uint8_t dont_process,
234 const uint8_t downSampledSBR)
236 int16_t k, l;
238 #ifdef SBR_LOW_POWER
239 ALIGN real_t deg[64];
240 #endif
242 #ifdef DRM
243 if (sbr->Is_DRM_SBR)
245 sbr->bsco = max((int32_t)sbr->maxAACLine*32/(int32_t)sbr->frame_len - (int32_t)sbr->kx, 0);
246 } else {
247 #endif
248 sbr->bsco = 0;
249 #ifdef DRM
251 #endif
254 //#define PRE_QMF_PRINT
255 #ifdef PRE_QMF_PRINT
257 int i;
258 for (i = 0; i < 1024; i++)
260 printf("%d\n", channel_buf[i]);
263 #endif
266 /* subband analysis */
267 if (dont_process)
268 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, 32);
269 else
270 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, sbr->kx);
272 if (!dont_process)
274 #if 1
275 /* insert high frequencies here */
276 /* hf generation using patching */
277 hf_generation(sbr, sbr->Xsbr[ch], sbr->Xsbr[ch]
278 #ifdef SBR_LOW_POWER
279 ,deg
280 #endif
281 ,ch);
282 #endif
284 #ifdef SBR_LOW_POWER
285 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
287 for (k = 0; k < sbr->kx; k++)
289 QMF_RE(sbr->Xsbr[ch][sbr->tHFAdj + l][k]) = 0;
292 #endif
294 #if 1
295 /* hf adjustment */
296 hf_adjustment(sbr, sbr->Xsbr[ch]
297 #ifdef SBR_LOW_POWER
298 ,deg
299 #endif
300 ,ch);
301 #endif
304 if ((sbr->just_seeked != 0) || dont_process)
306 for (l = 0; l < sbr->numTimeSlotsRate; l++)
308 for (k = 0; k < 32; k++)
310 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
311 #ifndef SBR_LOW_POWER
312 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
313 #endif
315 for (k = 32; k < 64; k++)
317 QMF_RE(X[l][k]) = 0;
318 #ifndef SBR_LOW_POWER
319 QMF_IM(X[l][k]) = 0;
320 #endif
323 } else {
324 for (l = 0; l < sbr->numTimeSlotsRate; l++)
326 uint8_t kx_band, M_band, bsco_band;
328 if (l < sbr->t_E[ch][0])
330 kx_band = sbr->kx_prev;
331 M_band = sbr->M_prev;
332 bsco_band = sbr->bsco_prev;
333 } else {
334 kx_band = sbr->kx;
335 M_band = sbr->M;
336 bsco_band = sbr->bsco;
339 #ifndef SBR_LOW_POWER
340 for (k = 0; k < kx_band + bsco_band; k++)
342 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
343 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
345 for (k = kx_band + bsco_band; k < kx_band + M_band; k++)
347 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
348 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
350 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
352 QMF_RE(X[l][k]) = 0;
353 QMF_IM(X[l][k]) = 0;
355 #else
356 for (k = 0; k < kx_band + bsco_band; k++)
358 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
360 for (k = kx_band + bsco_band; k < min(kx_band + M_band, 63); k++)
362 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
364 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
366 QMF_RE(X[l][k]) = 0;
368 QMF_RE(X[l][kx_band - 1 + bsco_band]) +=
369 QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][kx_band - 1 + bsco_band]);
370 #endif
375 uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan,
376 const uint8_t just_seeked, const uint8_t downSampledSBR)
378 uint8_t dont_process = 0;
379 uint8_t ret = 0;
380 ALIGN qmf_t X[MAX_NTSR][64];
382 if (sbr == NULL)
383 return 20;
385 /* case can occur due to bit errors */
386 if (sbr->id_aac != ID_CPE)
387 return 21;
389 if (sbr->ret || (sbr->header_count == 0))
391 /* don't process just upsample */
392 dont_process = 1;
394 /* Re-activate reset for next frame */
395 if (sbr->ret && sbr->Reset)
396 sbr->bs_start_freq_prev = -1;
399 if (just_seeked)
401 sbr->just_seeked = 1;
402 } else {
403 sbr->just_seeked = 0;
406 sbr_process_channel(sbr, left_chan, X, 0, dont_process, downSampledSBR);
407 /* subband synthesis */
408 if (downSampledSBR)
410 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X, left_chan);
411 } else {
412 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, left_chan);
415 sbr_process_channel(sbr, right_chan, X, 1, dont_process, downSampledSBR);
416 /* subband synthesis */
417 if (downSampledSBR)
419 sbr_qmf_synthesis_32(sbr, sbr->qmfs[1], X, right_chan);
420 } else {
421 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X, right_chan);
424 if (sbr->bs_header_flag)
425 sbr->just_seeked = 0;
427 if (sbr->header_count != 0 && sbr->ret == 0)
429 ret = sbr_save_prev_data(sbr, 0);
430 if (ret) return ret;
431 ret = sbr_save_prev_data(sbr, 1);
432 if (ret) return ret;
435 sbr_save_matrix(sbr, 0);
436 sbr_save_matrix(sbr, 1);
438 sbr->frame++;
440 //#define POST_QMF_PRINT
441 #ifdef POST_QMF_PRINT
443 int i;
444 for (i = 0; i < 2048; i++)
446 printf("%d\n", left_chan[i]);
448 for (i = 0; i < 2048; i++)
450 printf("%d\n", right_chan[i]);
453 #endif
455 return 0;
458 uint8_t sbrDecodeSingleFrame(sbr_info *sbr, real_t *channel,
459 const uint8_t just_seeked, const uint8_t downSampledSBR)
461 uint8_t dont_process = 0;
462 uint8_t ret = 0;
463 ALIGN qmf_t X[MAX_NTSR][64];
465 if (sbr == NULL)
466 return 20;
468 /* case can occur due to bit errors */
469 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
470 return 21;
472 if (sbr->ret || (sbr->header_count == 0))
474 /* don't process just upsample */
475 dont_process = 1;
477 /* Re-activate reset for next frame */
478 if (sbr->ret && sbr->Reset)
479 sbr->bs_start_freq_prev = -1;
482 if (just_seeked)
484 sbr->just_seeked = 1;
485 } else {
486 sbr->just_seeked = 0;
489 sbr_process_channel(sbr, channel, X, 0, dont_process, downSampledSBR);
490 /* subband synthesis */
491 if (downSampledSBR)
493 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X, channel);
494 } else {
495 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, channel);
498 if (sbr->bs_header_flag)
499 sbr->just_seeked = 0;
501 if (sbr->header_count != 0 && sbr->ret == 0)
503 ret = sbr_save_prev_data(sbr, 0);
504 if (ret) return ret;
507 sbr_save_matrix(sbr, 0);
509 sbr->frame++;
511 //#define POST_QMF_PRINT
512 #ifdef POST_QMF_PRINT
514 int i;
515 for (i = 0; i < 2048; i++)
517 printf("%d\n", channel[i]);
520 #endif
522 return 0;
525 #if (defined(PS_DEC) || defined(DRM_PS))
526 uint8_t sbrDecodeSingleFramePS(sbr_info *sbr, real_t *left_channel, real_t *right_channel,
527 const uint8_t just_seeked, const uint8_t downSampledSBR)
529 uint8_t l, k;
530 uint8_t dont_process = 0;
531 uint8_t ret = 0;
532 ALIGN qmf_t X_left[38][64] = {{{0}}};
533 ALIGN qmf_t X_right[38][64] = {{{0}}}; /* must set this to 0 */
535 if (sbr == NULL)
536 return 20;
538 /* case can occur due to bit errors */
539 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
540 return 21;
542 if (sbr->ret || (sbr->header_count == 0))
544 /* don't process just upsample */
545 dont_process = 1;
547 /* Re-activate reset for next frame */
548 if (sbr->ret && sbr->Reset)
549 sbr->bs_start_freq_prev = -1;
552 if (just_seeked)
554 sbr->just_seeked = 1;
555 } else {
556 sbr->just_seeked = 0;
559 if (sbr->qmfs[1] == NULL)
561 sbr->qmfs[1] = qmfs_init((downSampledSBR)?32:64);
564 sbr_process_channel(sbr, left_channel, X_left, 0, dont_process, downSampledSBR);
566 /* copy some extra data for PS */
567 for (l = 32; l < 38; l++)
569 for (k = 0; k < 5; k++)
571 QMF_RE(X_left[l][k]) = QMF_RE(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
572 QMF_IM(X_left[l][k]) = QMF_IM(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
576 /* perform parametric stereo */
577 #ifdef DRM_PS
578 if (sbr->Is_DRM_SBR)
580 drm_ps_decode(sbr->drm_ps, (sbr->ret > 0), sbr->sample_rate, X_left, X_right);
581 } else {
582 #endif
583 #ifdef PS_DEC
584 ps_decode(sbr->ps, X_left, X_right);
585 #endif
586 #ifdef DRM_PS
588 #endif
590 /* subband synthesis */
591 if (downSampledSBR)
593 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X_left, left_channel);
594 sbr_qmf_synthesis_32(sbr, sbr->qmfs[1], X_right, right_channel);
595 } else {
596 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X_left, left_channel);
597 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X_right, right_channel);
600 if (sbr->bs_header_flag)
601 sbr->just_seeked = 0;
603 if (sbr->header_count != 0 && sbr->ret == 0)
605 ret = sbr_save_prev_data(sbr, 0);
606 if (ret) return ret;
609 sbr_save_matrix(sbr, 0);
611 sbr->frame++;
613 return 0;
615 #endif
617 #endif