The last submit had one define too much. Nevertheless fix the IRAM configuration...
[kugel-rb.git] / apps / codecs / libfaad / sbr_dec.c
blob4904c702ee7956bc67b213df442af96459b8c820
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 ** $Id$
26 **/
29 #include "common.h"
30 #include "structs.h"
32 #ifdef SBR_DEC
34 #include <string.h>
35 #include <stdlib.h>
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"
44 /* type definitons */
45 typedef struct {
46 #if (defined(PS_DEC) || defined(DRM_PS))
47 /* In case of PS_DEC or DRM_PS we need larger buffer data when calling
48 * ps_decode() or drm_ps_decode(). */
49 qmf_t X_L[MAX_NTSRPS][64];
50 qmf_t X_R[MAX_NTSRPS][64];
51 #else
52 /* No PS functions called. Keep using MAX_NTSR as array size. */
53 qmf_t X_L[MAX_NTSR][64];
54 qmf_t X_R[MAX_NTSR][64];
55 #endif
56 } XLR_t;
58 /* static variables */
59 static XLR_t *p_XLR = NULL;
60 #if defined(FAAD_STATIC_ALLOC) || defined(IBSS_ATTR_FAAD_XLR)
61 static XLR_t s_XLR IBSS_ATTR_FAAD_XLR MEM_ALIGN_ATTR;
62 #endif
63 #if defined(FAAD_STATIC_ALLOC)
64 static sbr_info s_sbr[MAX_SYNTAX_ELEMENTS];
65 #endif
66 #ifdef SBR_LOW_POWER
67 static real_t deg[64] MEM_ALIGN_ATTR;
68 #endif
70 /* static function declarations */
71 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
72 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch);
75 sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac, uint8_t id_ele,
76 uint32_t sample_rate, uint8_t downSampledSBR,
77 uint8_t IsDRM)
79 (void)downSampledSBR;
80 #ifndef DRM
81 (void)IsDRM;
82 #endif
84 /* Allocate sbr_info. */
85 #if defined(FAAD_STATIC_ALLOC)
86 sbr_info *sbr = &s_sbr[id_ele];
87 #else
88 (void)id_ele;
89 sbr_info *sbr = (sbr_info*)faad_malloc(sizeof(sbr_info));
90 if (sbr == NULL)
92 /* could not allocate memory */
93 return NULL;
95 #endif
96 memset(sbr, 0, sizeof(sbr_info));
98 #ifdef PS_DEC
99 /* initialize PS variables */
100 ps_init(&sbr->ps);
101 #endif
103 /* Allocate XLR temporary variable. Use static allocation if either
104 * FAAD_STATIC_ALLOC is set or XLR fits to IRAM. */
105 #if defined(FAAD_STATIC_ALLOC) || defined(IBSS_ATTR_FAAD_XLR)
106 p_XLR = &s_XLR;
107 #else
108 p_XLR = (XLR_t*)faad_malloc(sizeof(XLR_t));
109 if (p_XLR == NULL)
111 /* could not allocate memory */
112 return NULL;
114 #endif
115 memset(p_XLR, 0, sizeof(XLR_t));
117 /* save id of the parent element */
118 sbr->id_aac = id_aac;
119 sbr->sample_rate = sample_rate;
121 sbr->bs_freq_scale = 2;
122 sbr->bs_alter_scale = 1;
123 sbr->bs_noise_bands = 2;
124 sbr->bs_limiter_bands = 2;
125 sbr->bs_limiter_gains = 2;
126 sbr->bs_interpol_freq = 1;
127 sbr->bs_smoothing_mode = 1;
128 sbr->bs_start_freq = 5;
129 sbr->bs_amp_res = 1;
130 sbr->bs_samplerate_mode = 1;
131 sbr->prevEnvIsShort[0] = -1;
132 sbr->prevEnvIsShort[1] = -1;
133 sbr->header_count = 0;
134 sbr->Reset = 1;
136 #ifdef DRM
137 sbr->Is_DRM_SBR = IsDRM;
138 #endif
139 sbr->tHFGen = T_HFGEN;
140 sbr->tHFAdj = T_HFADJ;
142 sbr->bsco = 0;
143 sbr->bsco_prev = 0;
144 sbr->M_prev = 0;
145 sbr->frame_len = framelength;
147 /* force sbr reset */
148 sbr->bs_start_freq_prev = -1;
150 if (framelength == 960)
152 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS_960;
153 sbr->numTimeSlots = NO_TIME_SLOTS_960;
154 } else {
155 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS;
156 sbr->numTimeSlots = NO_TIME_SLOTS;
159 sbr->GQ_ringbuf_index[0] = 0;
160 sbr->GQ_ringbuf_index[1] = 0;
162 memset(sbr->qmfa, 0, 2*sizeof(qmfa_info));
163 memset(sbr->qmfs, 0, 2*sizeof(qmfs_info));
165 memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
166 memset(sbr->Xsbr[1], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
168 return sbr;
171 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
173 uint8_t i;
175 /* save data for next frame */
176 sbr->kx_prev = sbr->kx;
177 sbr->M_prev = sbr->M;
178 sbr->bsco_prev = sbr->bsco;
180 sbr->L_E_prev[ch] = sbr->L_E[ch];
182 /* sbr->L_E[ch] can become 0 on files with bit errors */
183 if (sbr->L_E[ch] <= 0)
184 return 19;
186 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
187 for (i = 0; i < MAX_M; i++)
189 sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
190 sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
193 for (i = 0; i < MAX_M; i++)
195 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
197 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
199 if (sbr->l_A[ch] == sbr->L_E[ch])
200 sbr->prevEnvIsShort[ch] = 0;
201 else
202 sbr->prevEnvIsShort[ch] = -1;
204 return 0;
207 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch)
209 uint8_t i;
211 for (i = 0; i < sbr->tHFGen; i++)
213 memmove(sbr->Xsbr[ch][i], sbr->Xsbr[ch][i+sbr->numTimeSlotsRate], 64 * sizeof(qmf_t));
215 for (i = sbr->tHFGen; i < MAX_NTSRHFG; i++)
217 memset(sbr->Xsbr[ch][i], 0, 64 * sizeof(qmf_t));
221 static void sbr_process_channel(sbr_info *sbr, real_t *channel_buf, qmf_t X[MAX_NTSR][64],
222 uint8_t ch, uint8_t dont_process,
223 const uint8_t downSampledSBR)
225 int16_t k, l;
227 (void)downSampledSBR;
228 #ifdef DRM
229 if (sbr->Is_DRM_SBR)
231 sbr->bsco = max((int32_t)sbr->maxAACLine*32/(int32_t)sbr->frame_len - (int32_t)sbr->kx, 0);
232 } else {
233 #endif
234 sbr->bsco = 0;
235 #ifdef DRM
237 #endif
240 //#define PRE_QMF_PRINT
241 #ifdef PRE_QMF_PRINT
243 int i;
244 for (i = 0; i < 1024; i++)
246 printf("%d\n", channel_buf[i]);
249 #endif
252 /* subband analysis */
253 if (dont_process)
254 sbr_qmf_analysis_32(sbr, &sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, 32);
255 else
256 sbr_qmf_analysis_32(sbr, &sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, sbr->kx);
258 if (!dont_process)
260 #if 1
261 /* insert high frequencies here */
262 /* hf generation using patching */
263 hf_generation(sbr, sbr->Xsbr[ch], sbr->Xsbr[ch]
264 #ifdef SBR_LOW_POWER
265 ,deg
266 #endif
267 ,ch);
268 #endif
270 #ifdef SBR_LOW_POWER
271 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
273 for (k = 0; k < sbr->kx; k++)
275 QMF_RE(sbr->Xsbr[ch][sbr->tHFAdj + l][k]) = 0;
278 #endif
280 #if 1
281 /* hf adjustment */
282 hf_adjustment(sbr, sbr->Xsbr[ch]
283 #ifdef SBR_LOW_POWER
284 ,deg
285 #endif
286 ,ch);
287 #endif
290 if ((sbr->just_seeked != 0) || dont_process)
292 for (l = 0; l < sbr->numTimeSlotsRate; l++)
294 for (k = 0; k < 32; k++)
296 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
297 #ifndef SBR_LOW_POWER
298 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
299 #endif
301 for (k = 32; k < 64; k++)
303 QMF_RE(X[l][k]) = 0;
304 #ifndef SBR_LOW_POWER
305 QMF_IM(X[l][k]) = 0;
306 #endif
309 } else {
310 for (l = 0; l < sbr->numTimeSlotsRate; l++)
312 uint8_t kx_band, M_band, bsco_band;
314 if (l < sbr->t_E[ch][0])
316 kx_band = sbr->kx_prev;
317 M_band = sbr->M_prev;
318 bsco_band = sbr->bsco_prev;
319 } else {
320 kx_band = sbr->kx;
321 M_band = sbr->M;
322 bsco_band = sbr->bsco;
325 #ifndef SBR_LOW_POWER
326 for (k = 0; k < kx_band + bsco_band; k++)
328 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
329 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
331 for (k = kx_band + bsco_band; k < kx_band + M_band; k++)
333 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
334 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
336 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
338 QMF_RE(X[l][k]) = 0;
339 QMF_IM(X[l][k]) = 0;
341 #else
342 for (k = 0; k < kx_band + bsco_band; k++)
344 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
346 for (k = kx_band + bsco_band; k < min(kx_band + M_band, 63); k++)
348 QMF_RE(X[l][k]) = QMF_RE(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;
354 QMF_RE(X[l][kx_band - 1 + bsco_band]) +=
355 QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][kx_band - 1 + bsco_band]);
356 #endif
361 uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan,
362 const uint8_t just_seeked, const uint8_t downSampledSBR)
364 uint8_t dont_process = 0;
365 uint8_t ret = 0;
367 if (sbr == NULL)
368 return 20;
370 /* case can occur due to bit errors */
371 if (sbr->id_aac != ID_CPE)
372 return 21;
374 if (sbr->ret || (sbr->header_count == 0))
376 /* don't process just upsample */
377 dont_process = 1;
379 /* Re-activate reset for next frame */
380 if (sbr->ret && sbr->Reset)
381 sbr->bs_start_freq_prev = -1;
384 if (just_seeked)
386 sbr->just_seeked = 1;
387 } else {
388 sbr->just_seeked = 0;
391 sbr_process_channel(sbr, left_chan, p_XLR->X_L, 0, dont_process, downSampledSBR);
392 /* subband synthesis */
393 if (downSampledSBR)
395 sbr_qmf_synthesis_32(sbr, &sbr->qmfs[0], p_XLR->X_L, left_chan);
396 } else {
397 sbr_qmf_synthesis_64(sbr, &sbr->qmfs[0], p_XLR->X_L, left_chan);
400 sbr_process_channel(sbr, right_chan, p_XLR->X_R, 1, dont_process, downSampledSBR);
401 /* subband synthesis */
402 if (downSampledSBR)
404 sbr_qmf_synthesis_32(sbr, &sbr->qmfs[1], p_XLR->X_R, right_chan);
405 } else {
406 sbr_qmf_synthesis_64(sbr, &sbr->qmfs[1], p_XLR->X_R, right_chan);
409 if (sbr->bs_header_flag)
410 sbr->just_seeked = 0;
412 if (sbr->header_count != 0 && sbr->ret == 0)
414 ret = sbr_save_prev_data(sbr, 0);
415 if (ret) return ret;
416 ret = sbr_save_prev_data(sbr, 1);
417 if (ret) return ret;
420 sbr_save_matrix(sbr, 0);
421 sbr_save_matrix(sbr, 1);
423 sbr->frame++;
425 //#define POST_QMF_PRINT
426 #ifdef POST_QMF_PRINT
428 int i;
429 for (i = 0; i < 2048; i++)
431 printf("%d\n", left_chan[i]);
433 for (i = 0; i < 2048; i++)
435 printf("%d\n", right_chan[i]);
438 #endif
440 return 0;
443 uint8_t sbrDecodeSingleFrame(sbr_info *sbr, real_t *channel,
444 const uint8_t just_seeked, const uint8_t downSampledSBR)
446 uint8_t dont_process = 0;
447 uint8_t ret = 0;
449 if (sbr == NULL)
450 return 20;
452 /* case can occur due to bit errors */
453 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
454 return 21;
456 if (sbr->ret || (sbr->header_count == 0))
458 /* don't process just upsample */
459 dont_process = 1;
461 /* Re-activate reset for next frame */
462 if (sbr->ret && sbr->Reset)
463 sbr->bs_start_freq_prev = -1;
466 if (just_seeked)
468 sbr->just_seeked = 1;
469 } else {
470 sbr->just_seeked = 0;
473 sbr_process_channel(sbr, channel, p_XLR->X_L, 0, dont_process, downSampledSBR);
474 /* subband synthesis */
475 if (downSampledSBR)
477 sbr_qmf_synthesis_32(sbr, &sbr->qmfs[0], p_XLR->X_L, channel);
478 } else {
479 sbr_qmf_synthesis_64(sbr, &sbr->qmfs[0], p_XLR->X_L, channel);
482 if (sbr->bs_header_flag)
483 sbr->just_seeked = 0;
485 if (sbr->header_count != 0 && sbr->ret == 0)
487 ret = sbr_save_prev_data(sbr, 0);
488 if (ret) return ret;
491 sbr_save_matrix(sbr, 0);
493 sbr->frame++;
495 //#define POST_QMF_PRINT
496 #ifdef POST_QMF_PRINT
498 int i;
499 for (i = 0; i < 2048; i++)
501 printf("%d\n", channel[i]);
504 #endif
506 return 0;
510 #if (defined(PS_DEC) || defined(DRM_PS))
511 uint8_t sbrDecodeSingleFramePS(sbr_info *sbr, real_t *left_channel, real_t *right_channel,
512 const uint8_t just_seeked, const uint8_t downSampledSBR)
514 uint8_t l, k;
515 uint8_t dont_process = 0;
516 uint8_t ret = 0;
518 memset(p_XLR->X_L, 0, sizeof(*p_XLR->X_L));
519 memset(p_XLR->X_R, 0, sizeof(*p_XLR->X_R));
520 if (sbr == NULL)
521 return 20;
523 /* case can occur due to bit errors */
524 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
525 return 21;
527 if (sbr->ret || (sbr->header_count == 0))
529 /* don't process just upsample */
530 dont_process = 1;
532 /* Re-activate reset for next frame */
533 if (sbr->ret && sbr->Reset)
534 sbr->bs_start_freq_prev = -1;
537 if (just_seeked)
539 sbr->just_seeked = 1;
540 } else {
541 sbr->just_seeked = 0;
544 sbr_process_channel(sbr, left_channel, p_XLR->X_L, 0, dont_process, downSampledSBR);
546 /* copy some extra data for PS */
547 for (l = 32; l < 38; l++)
549 for (k = 0; k < 5; k++)
551 QMF_RE(p_XLR->X_L[l][k]) = QMF_RE(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
552 QMF_IM(p_XLR->X_L[l][k]) = QMF_IM(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
556 /* perform parametric stereo */
557 #ifdef DRM_PS
558 if (sbr->Is_DRM_SBR)
560 drm_ps_decode(sbr->drm_ps, (sbr->ret > 0), sbr->sample_rate, p_XLR->X_L, p_XLR->X_R);
561 } else {
562 #endif
563 #ifdef PS_DEC
564 ps_decode(&sbr->ps, p_XLR->X_L, p_XLR->X_R);
565 #endif
566 #ifdef DRM_PS
568 #endif
570 /* subband synthesis */
571 if (downSampledSBR)
573 sbr_qmf_synthesis_32(sbr, &sbr->qmfs[0], p_XLR->X_L, left_channel);
574 sbr_qmf_synthesis_32(sbr, &sbr->qmfs[1], p_XLR->X_R, right_channel);
575 } else {
576 sbr_qmf_synthesis_64(sbr, &sbr->qmfs[0], p_XLR->X_L, left_channel);
577 sbr_qmf_synthesis_64(sbr, &sbr->qmfs[1], p_XLR->X_R, right_channel);
580 if (sbr->bs_header_flag)
581 sbr->just_seeked = 0;
583 if (sbr->header_count != 0 && sbr->ret == 0)
585 ret = sbr_save_prev_data(sbr, 0);
586 if (ret) return ret;
589 sbr_save_matrix(sbr, 0);
591 sbr->frame++;
593 return 0;
595 #endif
597 #endif