summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/sbr_dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/sbr_dec.c')
-rw-r--r--lib/rbcodec/codecs/libfaad/sbr_dec.c597
1 files changed, 597 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/sbr_dec.c b/lib/rbcodec/codecs/libfaad/sbr_dec.c
new file mode 100644
index 0000000000..21e8e028a1
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/sbr_dec.c
@@ -0,0 +1,597 @@
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.
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$
26**/
27
28
29#include "common.h"
30#include "structs.h"
31
32#ifdef SBR_DEC
33
34#include <string.h>
35#include <stdlib.h>
36
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"
43
44/* type definitons */
45typedef 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;
57
58/* static variables */
59static XLR_t *p_XLR = NULL;
60#if defined(FAAD_STATIC_ALLOC) || defined(HAVE_FAAD_XLR_IN_IRAM)
61static XLR_t s_XLR IBSS_ATTR_FAAD_XLR MEM_ALIGN_ATTR;
62#endif
63#if defined(FAAD_STATIC_ALLOC)
64static sbr_info s_sbr[MAX_SYNTAX_ELEMENTS];
65#endif
66#ifdef SBR_LOW_POWER
67static real_t deg[64] MEM_ALIGN_ATTR;
68#endif
69
70/* static function declarations */
71static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch);
72static void sbr_save_matrix(sbr_info *sbr, uint8_t ch);
73
74
75sbr_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)
78{
79 (void)downSampledSBR;
80#ifndef DRM
81 (void)IsDRM;
82#endif
83
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)
91 {
92 /* could not allocate memory */
93 return NULL;
94 }
95#endif
96 memset(sbr, 0, sizeof(sbr_info));
97
98#ifdef PS_DEC
99 /* initialize PS variables */
100 ps_init(&sbr->ps);
101#endif
102
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(HAVE_FAAD_XLR_IN_IRAM)
106 p_XLR = &s_XLR;
107#else
108 p_XLR = (XLR_t*)faad_malloc(sizeof(XLR_t));
109 if (p_XLR == NULL)
110 {
111 /* could not allocate memory */
112 return NULL;
113 }
114#endif
115 memset(p_XLR, 0, sizeof(XLR_t));
116
117 /* save id of the parent element */
118 sbr->id_aac = id_aac;
119 sbr->sample_rate = sample_rate;
120
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;
135
136#ifdef DRM
137 sbr->Is_DRM_SBR = IsDRM;
138#endif
139 sbr->tHFGen = T_HFGEN;
140 sbr->tHFAdj = T_HFADJ;
141
142 sbr->bsco = 0;
143 sbr->bsco_prev = 0;
144 sbr->M_prev = 0;
145 sbr->frame_len = framelength;
146
147 /* force sbr reset */
148 sbr->bs_start_freq_prev = -1;
149
150 if (framelength == 960)
151 {
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;
157 }
158
159 sbr->GQ_ringbuf_index[0] = 0;
160 sbr->GQ_ringbuf_index[1] = 0;
161
162 memset(sbr->qmfa, 0, 2*sizeof(qmfa_info));
163 memset(sbr->qmfs, 0, 2*sizeof(qmfs_info));
164
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));
167
168 return sbr;
169}
170
171static uint8_t sbr_save_prev_data(sbr_info *sbr, uint8_t ch)
172{
173 uint8_t i;
174
175 /* save data for next frame */
176 sbr->kx_prev = sbr->kx;
177 sbr->M_prev = sbr->M;
178 sbr->bsco_prev = sbr->bsco;
179
180 sbr->L_E_prev[ch] = sbr->L_E[ch];
181
182 /* sbr->L_E[ch] can become 0 on files with bit errors */
183 if (sbr->L_E[ch] <= 0)
184 return 19;
185
186 sbr->f_prev[ch] = sbr->f[ch][sbr->L_E[ch] - 1];
187 for (i = 0; i < MAX_M; i++)
188 {
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];
191 }
192
193 for (i = 0; i < MAX_M; i++)
194 {
195 sbr->bs_add_harmonic_prev[ch][i] = sbr->bs_add_harmonic[ch][i];
196 }
197 sbr->bs_add_harmonic_flag_prev[ch] = sbr->bs_add_harmonic_flag[ch];
198
199 if (sbr->l_A[ch] == sbr->L_E[ch])
200 sbr->prevEnvIsShort[ch] = 0;
201 else
202 sbr->prevEnvIsShort[ch] = -1;
203
204 return 0;
205}
206
207static void sbr_save_matrix(sbr_info *sbr, uint8_t ch)
208{
209 uint8_t i;
210
211 for (i = 0; i < sbr->tHFGen; i++)
212 {
213 memmove(sbr->Xsbr[ch][i], sbr->Xsbr[ch][i+sbr->numTimeSlotsRate], 64 * sizeof(qmf_t));
214 }
215 for (i = sbr->tHFGen; i < MAX_NTSRHFG; i++)
216 {
217 memset(sbr->Xsbr[ch][i], 0, 64 * sizeof(qmf_t));
218 }
219}
220
221static 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)
224{
225 int16_t k, l;
226
227 (void)downSampledSBR;
228#ifdef DRM
229 if (sbr->Is_DRM_SBR)
230 {
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
236 }
237#endif
238
239
240//#define PRE_QMF_PRINT
241#ifdef PRE_QMF_PRINT
242 {
243 int i;
244 for (i = 0; i < 1024; i++)
245 {
246 printf("%d\n", channel_buf[i]);
247 }
248 }
249#endif
250
251
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);
257
258 if (!dont_process)
259 {
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
269
270#ifdef SBR_LOW_POWER
271 for (l = sbr->t_E[ch][0]; l < sbr->t_E[ch][sbr->L_E[ch]]; l++)
272 {
273 for (k = 0; k < sbr->kx; k++)
274 {
275 QMF_RE(sbr->Xsbr[ch][sbr->tHFAdj + l][k]) = 0;
276 }
277 }
278#endif
279
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
288 }
289
290 if ((sbr->just_seeked != 0) || dont_process)
291 {
292 for (l = 0; l < sbr->numTimeSlotsRate; l++)
293 {
294 for (k = 0; k < 32; k++)
295 {
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
300 }
301 for (k = 32; k < 64; k++)
302 {
303 QMF_RE(X[l][k]) = 0;
304#ifndef SBR_LOW_POWER
305 QMF_IM(X[l][k]) = 0;
306#endif
307 }
308 }
309 } else {
310 for (l = 0; l < sbr->numTimeSlotsRate; l++)
311 {
312 uint8_t kx_band, M_band, bsco_band;
313
314 if (l < sbr->t_E[ch][0])
315 {
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;
323 }
324
325#ifndef SBR_LOW_POWER
326 for (k = 0; k < kx_band + bsco_band; k++)
327 {
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]);
330 }
331 for (k = kx_band + bsco_band; k < kx_band + M_band; k++)
332 {
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]);
335 }
336 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
337 {
338 QMF_RE(X[l][k]) = 0;
339 QMF_IM(X[l][k]) = 0;
340 }
341#else
342 for (k = 0; k < kx_band + bsco_band; k++)
343 {
344 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
345 }
346 for (k = kx_band + bsco_band; k < min(kx_band + M_band, 63); k++)
347 {
348 QMF_RE(X[l][k]) = QMF_RE(sbr->Xsbr[ch][l + sbr->tHFAdj][k]);
349 }
350 for (k = max(kx_band + bsco_band, kx_band + M_band); k < 64; k++)
351 {
352 QMF_RE(X[l][k]) = 0;
353 }
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
357 }
358 }
359}
360
361uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan,
362 const uint8_t just_seeked, const uint8_t downSampledSBR)
363{
364 uint8_t dont_process = 0;
365 uint8_t ret = 0;
366
367 if (sbr == NULL)
368 return 20;
369
370 /* case can occur due to bit errors */
371 if (sbr->id_aac != ID_CPE)
372 return 21;
373
374 if (sbr->ret || (sbr->header_count == 0))
375 {
376 /* don't process just upsample */
377 dont_process = 1;
378
379 /* Re-activate reset for next frame */
380 if (sbr->ret && sbr->Reset)
381 sbr->bs_start_freq_prev = -1;
382 }
383
384 if (just_seeked)
385 {
386 sbr->just_seeked = 1;
387 } else {
388 sbr->just_seeked = 0;
389 }
390
391 sbr_process_channel(sbr, left_chan, p_XLR->X_L, 0, dont_process, downSampledSBR);
392 /* subband synthesis */
393 if (downSampledSBR)
394 {
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);
398 }
399
400 sbr_process_channel(sbr, right_chan, p_XLR->X_R, 1, dont_process, downSampledSBR);
401 /* subband synthesis */
402 if (downSampledSBR)
403 {
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);
407 }
408
409 if (sbr->bs_header_flag)
410 sbr->just_seeked = 0;
411
412 if (sbr->header_count != 0 && sbr->ret == 0)
413 {
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;
418 }
419
420 sbr_save_matrix(sbr, 0);
421 sbr_save_matrix(sbr, 1);
422
423 sbr->frame++;
424
425//#define POST_QMF_PRINT
426#ifdef POST_QMF_PRINT
427 {
428 int i;
429 for (i = 0; i < 2048; i++)
430 {
431 printf("%d\n", left_chan[i]);
432 }
433 for (i = 0; i < 2048; i++)
434 {
435 printf("%d\n", right_chan[i]);
436 }
437 }
438#endif
439
440 return 0;
441}
442
443uint8_t sbrDecodeSingleFrame(sbr_info *sbr, real_t *channel,
444 const uint8_t just_seeked, const uint8_t downSampledSBR)
445{
446 uint8_t dont_process = 0;
447 uint8_t ret = 0;
448
449 if (sbr == NULL)
450 return 20;
451
452 /* case can occur due to bit errors */
453 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
454 return 21;
455
456 if (sbr->ret || (sbr->header_count == 0))
457 {
458 /* don't process just upsample */
459 dont_process = 1;
460
461 /* Re-activate reset for next frame */
462 if (sbr->ret && sbr->Reset)
463 sbr->bs_start_freq_prev = -1;
464 }
465
466 if (just_seeked)
467 {
468 sbr->just_seeked = 1;
469 } else {
470 sbr->just_seeked = 0;
471 }
472
473 sbr_process_channel(sbr, channel, p_XLR->X_L, 0, dont_process, downSampledSBR);
474 /* subband synthesis */
475 if (downSampledSBR)
476 {
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);
480 }
481
482 if (sbr->bs_header_flag)
483 sbr->just_seeked = 0;
484
485 if (sbr->header_count != 0 && sbr->ret == 0)
486 {
487 ret = sbr_save_prev_data(sbr, 0);
488 if (ret) return ret;
489 }
490
491 sbr_save_matrix(sbr, 0);
492
493 sbr->frame++;
494
495//#define POST_QMF_PRINT
496#ifdef POST_QMF_PRINT
497 {
498 int i;
499 for (i = 0; i < 2048; i++)
500 {
501 printf("%d\n", channel[i]);
502 }
503 }
504#endif
505
506 return 0;
507}
508
509
510#if (defined(PS_DEC) || defined(DRM_PS))
511uint8_t sbrDecodeSingleFramePS(sbr_info *sbr, real_t *left_channel, real_t *right_channel,
512 const uint8_t just_seeked, const uint8_t downSampledSBR)
513{
514 uint8_t l, k;
515 uint8_t dont_process = 0;
516 uint8_t ret = 0;
517
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;
522
523 /* case can occur due to bit errors */
524 if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE)
525 return 21;
526
527 if (sbr->ret || (sbr->header_count == 0))
528 {
529 /* don't process just upsample */
530 dont_process = 1;
531
532 /* Re-activate reset for next frame */
533 if (sbr->ret && sbr->Reset)
534 sbr->bs_start_freq_prev = -1;
535 }
536
537 if (just_seeked)
538 {
539 sbr->just_seeked = 1;
540 } else {
541 sbr->just_seeked = 0;
542 }
543
544 sbr_process_channel(sbr, left_channel, p_XLR->X_L, 0, dont_process, downSampledSBR);
545
546 /* copy some extra data for PS */
547 for (l = 32; l < 38; l++)
548 {
549 for (k = 0; k < 5; k++)
550 {
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]);
553 }
554 }
555
556 /* perform parametric stereo */
557#ifdef DRM_PS
558 if (sbr->Is_DRM_SBR)
559 {
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
567 }
568#endif
569
570 /* subband synthesis */
571 if (downSampledSBR)
572 {
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);
578 }
579
580 if (sbr->bs_header_flag)
581 sbr->just_seeked = 0;
582
583 if (sbr->header_count != 0 && sbr->ret == 0)
584 {
585 ret = sbr_save_prev_data(sbr, 0);
586 if (ret) return ret;
587 }
588
589 sbr_save_matrix(sbr, 0);
590
591 sbr->frame++;
592
593 return 0;
594}
595#endif
596
597#endif