Ensure that tools are compiled before features - fixes an issue with bmp2rb and multi...
[Rockbox.git] / apps / codecs / libfaad / sbr_dec.c
blobc18ea454d5b9bfa8b6b421f338da94629161067e
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"
45 /* static function declarations */
46 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
47 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch);
50 sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
51 uint32_t sample_rate, uint8_t downSampledSBR
52 #ifdef DRM
53 , uint8_t IsDRM
54 #endif
57 sbr_info *sbr = faad_malloc(sizeof(sbr_info));
58 memset(sbr, 0, sizeof(sbr_info));
60 /* save id of the parent element */
61 sbr->id_aac = id_aac;
62 sbr->sample_rate = sample_rate;
64 sbr->bs_freq_scale = 2;
65 sbr->bs_alter_scale = 1;
66 sbr->bs_noise_bands = 2;
67 sbr->bs_limiter_bands = 2;
68 sbr->bs_limiter_gains = 2;
69 sbr->bs_interpol_freq = 1;
70 sbr->bs_smoothing_mode = 1;
71 sbr->bs_start_freq = 5;
72 sbr->bs_amp_res = 1;
73 sbr->bs_samplerate_mode = 1;
74 sbr->prevEnvIsShort[0] = -1;
75 sbr->prevEnvIsShort[1] = -1;
76 sbr->header_count = 0;
77 sbr->Reset = 1;
79 #ifdef DRM
80 sbr->Is_DRM_SBR = IsDRM;
81 #endif
82 sbr->tHFGen = T_HFGEN;
83 sbr->tHFAdj = T_HFADJ;
85 sbr->bsco = 0;
86 sbr->bsco_prev = 0;
87 sbr->M_prev = 0;
88 sbr->frame_len = framelength;
90 /* force sbr reset */
91 sbr->bs_start_freq_prev = -1;
93 if (framelength == 960)
95 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS_960;
96 sbr->numTimeSlots = NO_TIME_SLOTS_960;
97 } else {
98 sbr->numTimeSlotsRate = RATE * NO_TIME_SLOTS;
99 sbr->numTimeSlots = NO_TIME_SLOTS;
102 sbr->GQ_ringbuf_index[0] = 0;
103 sbr->GQ_ringbuf_index[1] = 0;
105 if (id_aac == ID_CPE)
107 /* stereo */
108 uint8_t j;
109 sbr->qmfa[0] = qmfa_init(32);
110 sbr->qmfa[1] = qmfa_init(32);
111 sbr->qmfs[0] = qmfs_init((downSampledSBR)?32:64);
112 sbr->qmfs[1] = qmfs_init((downSampledSBR)?32:64);
114 for (j = 0; j < 5; j++)
116 sbr->G_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
117 sbr->G_temp_prev[1][j] = faad_malloc(64*sizeof(real_t));
118 sbr->Q_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
119 sbr->Q_temp_prev[1][j] = faad_malloc(64*sizeof(real_t));
122 memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
123 memset(sbr->Xsbr[1], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
124 } else {
125 /* mono */
126 uint8_t j;
127 sbr->qmfa[0] = qmfa_init(32);
128 sbr->qmfs[0] = qmfs_init((downSampledSBR)?32:64);
129 sbr->qmfs[1] = NULL;
131 for (j = 0; j < 5; j++)
133 sbr->G_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
134 sbr->Q_temp_prev[0][j] = faad_malloc(64*sizeof(real_t));
137 memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate+sbr->tHFGen)*64 * sizeof(qmf_t));
140 return sbr;
143 void sbrDecodeEnd(sbr_info *sbr)
145 uint8_t j;
147 if (sbr)
149 qmfa_end(sbr->qmfa[0]);
150 qmfs_end(sbr->qmfs[0]);
151 if (sbr->qmfs[1] != NULL)
153 qmfa_end(sbr->qmfa[1]);
154 qmfs_end(sbr->qmfs[1]);
157 for (j = 0; j < 5; j++)
159 if (sbr->G_temp_prev[0][j]) faad_free(sbr->G_temp_prev[0][j]);
160 if (sbr->Q_temp_prev[0][j]) faad_free(sbr->Q_temp_prev[0][j]);
161 if (sbr->G_temp_prev[1][j]) faad_free(sbr->G_temp_prev[1][j]);
162 if (sbr->Q_temp_prev[1][j]) faad_free(sbr->Q_temp_prev[1][j]);
165 #ifdef PS_DEC
166 if (sbr->ps != NULL)
167 ps_free(sbr->ps);
168 #endif
170 #ifdef DRM_PS
171 if (sbr->drm_ps != NULL)
172 drm_ps_free(sbr->drm_ps);
173 #endif
175 faad_free(sbr);
179 static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
181 uint8_t i;
183 /* save data for next frame */
184 sbr->kx_prev = sbr->kx;
185 sbr->M_prev = sbr->M;
186 sbr->bsco_prev = sbr->bsco;
188 sbr->L_E_prev[ch] = sbr->L_E[ch];
190 /* sbr->L_E[ch] can become 0 on files with bit errors */
191 if (sbr->L_E[ch] <= 0)
192 return 19;
194 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
195 for (i = 0; i < MAX_M; i++)
197 sbr->E_prev[ch][i] = sbr->E[ch][i][sbr->L_E[ch] - 1];
198 sbr->Q_prev[ch][i] = sbr->Q[ch][i][sbr->L_Q[ch] - 1];
201 for (i = 0; i < MAX_M; i++)
203 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
205 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
207 if (sbr->l_A[ch] == sbr->L_E[ch])
208 sbr->prevEnvIsShort[ch] = 0;
209 else
210 sbr->prevEnvIsShort[ch] = -1;
212 return 0;
215 static void sbr_save_matrix(sbr_info *sbr, uint8_t ch)
217 uint8_t i;
219 for (i = 0; i < sbr->tHFGen; i++)
221 memmove(sbr->Xsbr[ch][i], sbr->Xsbr[ch][i+sbr->numTimeSlotsRate], 64 * sizeof(qmf_t));
223 for (i = sbr->tHFGen; i < MAX_NTSRHFG; i++)
225 memset(sbr->Xsbr[ch][i], 0, 64 * sizeof(qmf_t));
229 #ifdef SBR_LOW_POWER
230 ALIGN real_t deg[64];
231 #endif
233 static void sbr_process_channel(sbr_info *sbr, real_t *channel_buf, qmf_t X[MAX_NTSR][64],
234 uint8_t ch, uint8_t dont_process,
235 const uint8_t downSampledSBR)
237 int16_t k, l;
239 (void)downSampledSBR;
240 #ifdef DRM
241 if (sbr->Is_DRM_SBR)
243 sbr->bsco = max((int32_t)sbr->maxAACLine*32/(int32_t)sbr->frame_len - (int32_t)sbr->kx, 0);
244 } else {
245 #endif
246 sbr->bsco = 0;
247 #ifdef DRM
249 #endif
252 //#define PRE_QMF_PRINT
253 #ifdef PRE_QMF_PRINT
255 int i;
256 for (i = 0; i < 1024; i++)
258 printf("%d\n", channel_buf[i]);
261 #endif
264 /* subband analysis */
265 if (dont_process)
266 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, 32);
267 else
268 sbr_qmf_analysis_32(sbr, sbr->qmfa[ch], channel_buf, sbr->Xsbr[ch], sbr->tHFGen, sbr->kx);
270 if (!dont_process)
272 #if 1
273 /* insert high frequencies here */
274 /* hf generation using patching */
275 hf_generation(sbr, sbr->Xsbr[ch], sbr->Xsbr[ch]
276 #ifdef SBR_LOW_POWER
277 ,deg
278 #endif
279 ,ch);
280 #endif
282 #ifdef SBR_LOW_POWER
283 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
285 for (k = 0; k < sbr->kx; k++)
287 QMF_RE(sbr->Xsbr[ch][sbr->tHFAdj + l][k]) = 0;
290 #endif
292 #if 1
293 /* hf adjustment */
294 hf_adjustment(sbr, sbr->Xsbr[ch]
295 #ifdef SBR_LOW_POWER
296 ,deg
297 #endif
298 ,ch);
299 #endif
302 if ((sbr->just_seeked != 0) || dont_process)
304 for (l = 0; l < sbr->numTimeSlotsRate; l++)
306 for (k = 0; k < 32; k++)
308 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
309 #ifndef SBR_LOW_POWER
310 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
311 #endif
313 for (k = 32; k < 64; k++)
315 QMF_RE(X[l][k]) = 0;
316 #ifndef SBR_LOW_POWER
317 QMF_IM(X[l][k]) = 0;
318 #endif
321 } else {
322 for (l = 0; l < sbr->numTimeSlotsRate; l++)
324 uint8_t kx_band, M_band, bsco_band;
326 if (l < sbr->t_E[ch][0])
328 kx_band = sbr->kx_prev;
329 M_band = sbr->M_prev;
330 bsco_band = sbr->bsco_prev;
331 } else {
332 kx_band = sbr->kx;
333 M_band = sbr->M;
334 bsco_band = sbr->bsco;
337 #ifndef SBR_LOW_POWER
338 for (k = 0; k < kx_band + bsco_band; k++)
340 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
341 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
343 for (k = kx_band + bsco_band; k < kx_band + M_band; k++)
345 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
346 QMF_IM(X[l][k]) = QMF_IM(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
348 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
350 QMF_RE(X[l][k]) = 0;
351 QMF_IM(X[l][k]) = 0;
353 #else
354 for (k = 0; k < kx_band + bsco_band; k++)
356 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
358 for (k = kx_band + bsco_band; k < min(kx_band + M_band, 63); k++)
360 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
362 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
364 QMF_RE(X[l][k]) = 0;
366 QMF_RE(X[l][kx_band - 1 + bsco_band]) +=
367 QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][kx_band - 1 + bsco_band]);
368 #endif
373 ALIGN qmf_t X[MAX_NTSR][64];
374 uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan,
375 const uint8_t just_seeked, const uint8_t downSampledSBR)
377 uint8_t dont_process = 0;
378 uint8_t ret = 0;
380 if (sbr == NULL)
381 return 20;
383 /* case can occur due to bit errors */
384 if (sbr->id_aac != ID_CPE)
385 return 21;
387 if (sbr->ret || (sbr->header_count == 0))
389 /* don't process just upsample */
390 dont_process = 1;
392 /* Re-activate reset for next frame */
393 if (sbr->ret && sbr->Reset)
394 sbr->bs_start_freq_prev = -1;
397 if (just_seeked)
399 sbr->just_seeked = 1;
400 } else {
401 sbr->just_seeked = 0;
404 sbr_process_channel(sbr, left_chan, X, 0, dont_process, downSampledSBR);
405 /* subband synthesis */
406 if (downSampledSBR)
408 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X, left_chan);
409 } else {
410 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, left_chan);
413 sbr_process_channel(sbr, right_chan, X, 1, dont_process, downSampledSBR);
414 /* subband synthesis */
415 if (downSampledSBR)
417 sbr_qmf_synthesis_32(sbr, sbr->qmfs[1], X, right_chan);
418 } else {
419 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X, right_chan);
422 if (sbr->bs_header_flag)
423 sbr->just_seeked = 0;
425 if (sbr->header_count != 0 && sbr->ret == 0)
427 ret = sbr_save_prev_data(sbr, 0);
428 if (ret) return ret;
429 ret = sbr_save_prev_data(sbr, 1);
430 if (ret) return ret;
433 sbr_save_matrix(sbr, 0);
434 sbr_save_matrix(sbr, 1);
436 sbr->frame++;
438 //#define POST_QMF_PRINT
439 #ifdef POST_QMF_PRINT
441 int i;
442 for (i = 0; i < 2048; i++)
444 printf("%d\n", left_chan[i]);
446 for (i = 0; i < 2048; i++)
448 printf("%d\n", right_chan[i]);
451 #endif
453 return 0;
456 ALIGN qmf_t X[MAX_NTSR][64];
457 uint8_t sbrDecodeSingleFrame(sbr_info *sbr, real_t *channel,
458 const uint8_t just_seeked, const uint8_t downSampledSBR)
460 uint8_t dont_process = 0;
461 uint8_t ret = 0;
463 if (sbr == NULL)
464 return 20;
466 /* case can occur due to bit errors */
467 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
468 return 21;
470 if (sbr->ret || (sbr->header_count == 0))
472 /* don't process just upsample */
473 dont_process = 1;
475 /* Re-activate reset for next frame */
476 if (sbr->ret && sbr->Reset)
477 sbr->bs_start_freq_prev = -1;
480 if (just_seeked)
482 sbr->just_seeked = 1;
483 } else {
484 sbr->just_seeked = 0;
487 sbr_process_channel(sbr, channel, X, 0, dont_process, downSampledSBR);
488 /* subband synthesis */
489 if (downSampledSBR)
491 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X, channel);
492 } else {
493 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X, channel);
496 if (sbr->bs_header_flag)
497 sbr->just_seeked = 0;
499 if (sbr->header_count != 0 && sbr->ret == 0)
501 ret = sbr_save_prev_data(sbr, 0);
502 if (ret) return ret;
505 sbr_save_matrix(sbr, 0);
507 sbr->frame++;
509 //#define POST_QMF_PRINT
510 #ifdef POST_QMF_PRINT
512 int i;
513 for (i = 0; i < 2048; i++)
515 printf("%d\n", channel[i]);
518 #endif
520 return 0;
524 ALIGN qmf_t X_left[38][64];// = {{0}};
525 ALIGN qmf_t X_right[38][64];// = {{0}}; /* must set this to 0 */
527 #if (defined(PS_DEC) || defined(DRM_PS))
528 uint8_t sbrDecodeSingleFramePS(sbr_info *sbr, real_t *left_channel, real_t *right_channel,
529 const uint8_t just_seeked, const uint8_t downSampledSBR)
531 uint8_t l, k;
532 uint8_t dont_process = 0;
533 uint8_t ret = 0;
535 memset(X_left,0,sizeof(X_left));
536 memset(X_right,0,sizeof(X_right));
537 if (sbr == NULL)
538 return 20;
540 /* case can occur due to bit errors */
541 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
542 return 21;
544 if (sbr->ret || (sbr->header_count == 0))
546 /* don't process just upsample */
547 dont_process = 1;
549 /* Re-activate reset for next frame */
550 if (sbr->ret && sbr->Reset)
551 sbr->bs_start_freq_prev = -1;
554 if (just_seeked)
556 sbr->just_seeked = 1;
557 } else {
558 sbr->just_seeked = 0;
561 if (sbr->qmfs[1] == NULL)
563 sbr->qmfs[1] = qmfs_init((downSampledSBR)?32:64);
566 sbr_process_channel(sbr, left_channel, X_left, 0, dont_process, downSampledSBR);
568 /* copy some extra data for PS */
569 for (l = 32; l < 38; l++)
571 for (k = 0; k < 5; k++)
573 QMF_RE(X_left[l][k]) = QMF_RE(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
574 QMF_IM(X_left[l][k]) = QMF_IM(sbr->Xsbr[0][sbr->tHFAdj+l][k]);
578 /* perform parametric stereo */
579 #ifdef DRM_PS
580 if (sbr->Is_DRM_SBR)
582 drm_ps_decode(sbr->drm_ps, (sbr->ret > 0), sbr->sample_rate, X_left, X_right);
583 } else {
584 #endif
585 #ifdef PS_DEC
586 ps_decode(sbr->ps, X_left, X_right);
587 #endif
588 #ifdef DRM_PS
590 #endif
592 /* subband synthesis */
593 if (downSampledSBR)
595 sbr_qmf_synthesis_32(sbr, sbr->qmfs[0], X_left, left_channel);
596 sbr_qmf_synthesis_32(sbr, sbr->qmfs[1], X_right, right_channel);
597 } else {
598 sbr_qmf_synthesis_64(sbr, sbr->qmfs[0], X_left, left_channel);
599 sbr_qmf_synthesis_64(sbr, sbr->qmfs[1], X_right, right_channel);
602 if (sbr->bs_header_flag)
603 sbr->just_seeked = 0;
605 if (sbr->header_count != 0 && sbr->ret == 0)
607 ret = sbr_save_prev_data(sbr, 0);
608 if (ret) return ret;
611 sbr_save_matrix(sbr, 0);
613 sbr->frame++;
615 return 0;
617 #endif
619 #endif