annotate aacsbr.c @ 12485:b42e02e9bf2b libavcodec

Move AMR-NB frame unpacking code to a common file so it can be reused in the AMR-WB decoder. Patch by Marcelo Galvo Pvoa.
author vitor
date Fri, 10 Sep 2010 19:51:08 +0000
parents b1c9654b8d68
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1 /*
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
2 * AAC Spectral Band Replication decoding functions
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
3 * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
4 * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
5 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
6 * This file is part of FFmpeg.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
7 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
12 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
16 * Lesser General Public License for more details.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
17 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
21 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
22
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
23 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11590
diff changeset
24 * @file
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
25 * AAC Spectral Band Replication decoding functions
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
26 * @author Robert Swain ( rob opendot cl )
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
27 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
28
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
29 #include "aac.h"
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
30 #include "sbr.h"
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
31 #include "aacsbr.h"
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
32 #include "aacsbrdata.h"
11427
32504af93342 Add a missing fft.h include to the aacsbr decoder.
alexc
parents: 11401
diff changeset
33 #include "fft.h"
11922
8c657be75c11 Move Parametric Stereo related ps* files to aacps*.
alexc
parents: 11901
diff changeset
34 #include "aacps.h"
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
35
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
36 #include <stdint.h>
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
37 #include <float.h>
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
38
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
39 #define ENVELOPE_ADJUSTMENT_OFFSET 2
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
40 #define NOISE_FLOOR_OFFSET 6.0f
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
41
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
42 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
43 * SBR VLC tables
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
44 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
45 enum {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
46 T_HUFFMAN_ENV_1_5DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
47 F_HUFFMAN_ENV_1_5DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
48 T_HUFFMAN_ENV_BAL_1_5DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
49 F_HUFFMAN_ENV_BAL_1_5DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
50 T_HUFFMAN_ENV_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
51 F_HUFFMAN_ENV_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
52 T_HUFFMAN_ENV_BAL_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
53 F_HUFFMAN_ENV_BAL_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
54 T_HUFFMAN_NOISE_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
55 T_HUFFMAN_NOISE_BAL_3_0DB,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
56 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
57
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
58 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
59 * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
60 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
61 enum {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
62 FIXFIX,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
63 FIXVAR,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
64 VARFIX,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
65 VARVAR,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
66 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
67
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
68 enum {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
69 EXTENSION_ID_PS = 2,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
70 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
71
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
72 static VLC vlc_sbr[10];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
73 static const int8_t vlc_sbr_lav[10] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
74 { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
75 static const DECLARE_ALIGNED(16, float, zero64)[64];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
76
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
77 #define SBR_INIT_VLC_STATIC(num, size) \
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
78 INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
79 sbr_tmp[num].sbr_bits , 1, 1, \
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
80 sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
81 size)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
82
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
83 #define SBR_VLC_ROW(name) \
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
84 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
85
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
86 av_cold void ff_aac_sbr_init(void)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
87 {
11848
6b52f0c3263d 10l: Fix types and now unused variables after last commit.
alexc
parents: 11847
diff changeset
88 int n;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
89 static const struct {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
90 const void *sbr_codes, *sbr_bits;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
91 const unsigned int table_size, elem_size;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
92 } sbr_tmp[] = {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
93 SBR_VLC_ROW(t_huffman_env_1_5dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
94 SBR_VLC_ROW(f_huffman_env_1_5dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
95 SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
96 SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
97 SBR_VLC_ROW(t_huffman_env_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
98 SBR_VLC_ROW(f_huffman_env_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
99 SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
100 SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
101 SBR_VLC_ROW(t_huffman_noise_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
102 SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
103 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
104
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
105 // SBR VLC table initialization
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
106 SBR_INIT_VLC_STATIC(0, 1098);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
107 SBR_INIT_VLC_STATIC(1, 1092);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
108 SBR_INIT_VLC_STATIC(2, 768);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
109 SBR_INIT_VLC_STATIC(3, 1026);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
110 SBR_INIT_VLC_STATIC(4, 1058);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
111 SBR_INIT_VLC_STATIC(5, 1052);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
112 SBR_INIT_VLC_STATIC(6, 544);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
113 SBR_INIT_VLC_STATIC(7, 544);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
114 SBR_INIT_VLC_STATIC(8, 592);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
115 SBR_INIT_VLC_STATIC(9, 512);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
116
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
117 for (n = 1; n < 320; n++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
118 sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
119 sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
120 sbr_qmf_window_us[512] = -sbr_qmf_window_us[512];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
121
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
122 for (n = 0; n < 320; n++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
123 sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
124
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
125 ff_ps_init();
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
126 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
127
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
128 av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
129 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
130 sbr->kx[0] = sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
11445
bf53d3860a0d aacsbr: Initialize e_a[1] to -1.
alexc
parents: 11440
diff changeset
131 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
132 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
133 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
134 ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64);
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
135 ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0);
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
136 ff_ps_ctx_init(&sbr->ps);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
137 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
138
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
139 av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
140 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
141 ff_mdct_end(&sbr->mdct);
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
142 ff_mdct_end(&sbr->mdct_ana);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
143 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
144
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
145 static int qsort_comparison_function_int16(const void *a, const void *b)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
146 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
147 return *(const int16_t *)a - *(const int16_t *)b;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
148 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
149
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
150 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
151 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
152 int i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
153 for (i = 0; i <= last_el; i++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
154 if (table[i] == needle)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
155 return 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
156 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
157 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
158
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
159 /// Limiter Frequency Band Table (14496-3 sp04 p198)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
160 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
161 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
162 int k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
163 if (sbr->bs_limiter_bands > 0) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
164 static const float bands_warped[3] = { 1.32715174233856803909f, //2^(0.49/1.2)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
165 1.18509277094158210129f, //2^(0.49/2)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
166 1.11987160404675912501f }; //2^(0.49/3)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
167 const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
11670
53584d5f86b8 Increase size of patch_borders[].
alexc
parents: 11668
diff changeset
168 int16_t patch_borders[7];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
169 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
170
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
171 patch_borders[0] = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
172 for (k = 1; k <= sbr->num_patches; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
173 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
174
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
175 memcpy(sbr->f_tablelim, sbr->f_tablelow,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
176 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
177 if (sbr->num_patches > 1)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
178 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
179 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
180
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
181 qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
182 sizeof(sbr->f_tablelim[0]),
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
183 qsort_comparison_function_int16);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
184
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
185 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
186 while (out < sbr->f_tablelim + sbr->n_lim) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
187 if (*in >= *out * lim_bands_per_octave_warped) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
188 *++out = *in++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
189 } else if (*in == *out ||
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
190 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
191 in++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
192 sbr->n_lim--;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
193 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
194 *out = *in++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
195 sbr->n_lim--;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
196 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
197 *++out = *in++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
198 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
199 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
200 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
201 sbr->f_tablelim[0] = sbr->f_tablelow[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
202 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
203 sbr->n_lim = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
204 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
205 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
206
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
207 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
208 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
209 unsigned int cnt = get_bits_count(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
210 uint8_t bs_header_extra_1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
211 uint8_t bs_header_extra_2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
212 int old_bs_limiter_bands = sbr->bs_limiter_bands;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
213 SpectrumParameters old_spectrum_params;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
214
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
215 sbr->start = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
216
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
217 // Save last spectrum parameters variables to compare to new ones
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
218 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
219
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
220 sbr->bs_amp_res_header = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
221 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
222 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
223 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
224 skip_bits(gb, 2); // bs_reserved
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
225
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
226 bs_header_extra_1 = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
227 bs_header_extra_2 = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
228
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
229 if (bs_header_extra_1) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
230 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
231 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
232 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
233 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
234 sbr->spectrum_params.bs_freq_scale = 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
235 sbr->spectrum_params.bs_alter_scale = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
236 sbr->spectrum_params.bs_noise_bands = 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
237 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
238
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
239 // Check if spectrum parameters changed
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
240 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
241 sbr->reset = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
242
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
243 if (bs_header_extra_2) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
244 sbr->bs_limiter_bands = get_bits(gb, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
245 sbr->bs_limiter_gains = get_bits(gb, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
246 sbr->bs_interpol_freq = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
247 sbr->bs_smoothing_mode = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
248 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
249 sbr->bs_limiter_bands = 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
250 sbr->bs_limiter_gains = 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
251 sbr->bs_interpol_freq = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
252 sbr->bs_smoothing_mode = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
253 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
254
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
255 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
256 sbr_make_f_tablelim(sbr);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
257
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
258 return get_bits_count(gb) - cnt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
259 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
260
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
261 static int array_min_int16(const int16_t *array, int nel)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
262 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
263 int i, min = array[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
264 for (i = 1; i < nel; i++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
265 min = FFMIN(array[i], min);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
266 return min;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
267 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
268
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
269 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
270 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
271 int k, previous, present;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
272 float base, prod;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
273
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
274 base = powf((float)stop / start, 1.0f / num_bands);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
275 prod = start;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
276 previous = start;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
277
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
278 for (k = 0; k < num_bands-1; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
279 prod *= base;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
280 present = lrintf(prod);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
281 bands[k] = present - previous;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
282 previous = present;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
283 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
284 bands[num_bands-1] = stop - previous;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
285 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
286
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
287 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
288 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
289 // Requirements (14496-3 sp04 p205)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
290 if (n_master <= 0) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
291 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
292 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
293 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
294 if (bs_xover_band >= n_master) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
295 av_log(avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
296 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
297 bs_xover_band);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
298 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
299 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
300 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
301 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
302
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
303 /// Master Frequency Band Table (14496-3 sp04 p194)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
304 static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
305 SpectrumParameters *spectrum)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
306 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
307 unsigned int temp, max_qmf_subbands;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
308 unsigned int start_min, stop_min;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
309 int k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
310 const int8_t *sbr_offset_ptr;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
311 int16_t stop_dk[13];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
312
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
313 if (sbr->sample_rate < 32000) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
314 temp = 3000;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
315 } else if (sbr->sample_rate < 64000) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
316 temp = 4000;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
317 } else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
318 temp = 5000;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
319
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
320 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
321 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
322
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
323 switch (sbr->sample_rate) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
324 case 16000:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
325 sbr_offset_ptr = sbr_offset[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
326 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
327 case 22050:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
328 sbr_offset_ptr = sbr_offset[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
329 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
330 case 24000:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
331 sbr_offset_ptr = sbr_offset[2];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
332 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
333 case 32000:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
334 sbr_offset_ptr = sbr_offset[3];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
335 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
336 case 44100: case 48000: case 64000:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
337 sbr_offset_ptr = sbr_offset[4];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
338 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
339 case 88200: case 96000: case 128000: case 176400: case 192000:
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
340 sbr_offset_ptr = sbr_offset[5];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
341 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
342 default:
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
343 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
344 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
345 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
346 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
347
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
348 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
349
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
350 if (spectrum->bs_stop_freq < 14) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
351 sbr->k[2] = stop_min;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
352 make_bands(stop_dk, stop_min, 64, 13);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
353 qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
354 for (k = 0; k < spectrum->bs_stop_freq; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
355 sbr->k[2] += stop_dk[k];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
356 } else if (spectrum->bs_stop_freq == 14) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
357 sbr->k[2] = 2*sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
358 } else if (spectrum->bs_stop_freq == 15) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
359 sbr->k[2] = 3*sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
360 } else {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
361 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
362 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
363 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
364 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
365 sbr->k[2] = FFMIN(64, sbr->k[2]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
366
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
367 // Requirements (14496-3 sp04 p205)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
368 if (sbr->sample_rate <= 32000) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
369 max_qmf_subbands = 48;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
370 } else if (sbr->sample_rate == 44100) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
371 max_qmf_subbands = 35;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
372 } else if (sbr->sample_rate >= 48000)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
373 max_qmf_subbands = 32;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
374
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
375 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
376 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
377 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
378 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
379 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
380
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
381 if (!spectrum->bs_freq_scale) {
11901
829dadee50c8 aacsbr: Make dk signed. There is no point in it being unsigned.
alexc
parents: 11892
diff changeset
382 int dk, k2diff;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
383
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
384 dk = spectrum->bs_alter_scale + 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
385 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
386 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
387 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
388
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
389 for (k = 1; k <= sbr->n_master; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
390 sbr->f_master[k] = dk;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
391
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
392 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
393 if (k2diff < 0) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
394 sbr->f_master[1]--;
11892
a0c269879276 10l: aacsbr: Fix f_master[2] calculation when k2diff == -1.
alexc
parents: 11886
diff changeset
395 sbr->f_master[2]-= (k2diff < -1);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
396 } else if (k2diff) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
397 sbr->f_master[sbr->n_master]++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
398 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
399
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
400 sbr->f_master[0] = sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
401 for (k = 1; k <= sbr->n_master; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
402 sbr->f_master[k] += sbr->f_master[k - 1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
403
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
404 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
405 int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
406 int two_regions, num_bands_0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
407 int vdk0_max, vdk1_min;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
408 int16_t vk0[49];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
409
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
410 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
411 two_regions = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
412 sbr->k[1] = 2 * sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
413 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
414 two_regions = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
415 sbr->k[1] = sbr->k[2];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
416 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
417
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
418 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
419
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
420 if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
421 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
422 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
423 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
424
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
425 vk0[0] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
426
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
427 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
428
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
429 qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
430 vdk0_max = vk0[num_bands_0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
431
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
432 vk0[0] = sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
433 for (k = 1; k <= num_bands_0; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
434 if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
435 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
436 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
437 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
438 vk0[k] += vk0[k-1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
439 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
440
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
441 if (two_regions) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
442 int16_t vk1[49];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
443 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
444 : 1.0f; // bs_alter_scale = {0,1}
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
445 int num_bands_1 = lrintf(half_bands * invwarp *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
446 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
447
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
448 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
449
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
450 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
451
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
452 if (vdk1_min < vdk0_max) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
453 int change;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
454 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
455 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
456 vk1[1] += change;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
457 vk1[num_bands_1] -= change;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
458 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
459
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
460 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
461
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
462 vk1[0] = sbr->k[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
463 for (k = 1; k <= num_bands_1; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
464 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
465 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
466 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
467 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
468 vk1[k] += vk1[k-1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
469 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
470
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
471 sbr->n_master = num_bands_0 + num_bands_1;
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
472 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
473 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
474 memcpy(&sbr->f_master[0], vk0,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
475 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
476 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
477 num_bands_1 * sizeof(sbr->f_master[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
478
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
479 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
480 sbr->n_master = num_bands_0;
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
481 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
482 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
483 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
484 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
485 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
486
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
487 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
488 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
489
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
490 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
491 static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
492 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
493 int i, k, sb = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
494 int msb = sbr->k[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
495 int usb = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
496 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
497
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
498 sbr->num_patches = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
499
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
500 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
501 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
502 } else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
503 k = sbr->n_master;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
504
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
505 do {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
506 int odd = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
507 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
508 sb = sbr->f_master[i];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
509 odd = (sb + sbr->k[0]) & 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
510 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
511
11671
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
512 // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
513 // After this check the final number of patches can still be six which is
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
514 // illegal however the Coding Technologies decoder check stream has a final
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
515 // count of 6 patches
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
516 if (sbr->num_patches > 5) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
517 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
11671
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
518 return -1;
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
519 }
7f301b81b503 Move the SBR patch count check to prevent overwrites.
alexc
parents: 11670
diff changeset
520
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
521 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
522 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
523
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
524 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
525 usb = sb;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
526 msb = sb;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
527 sbr->num_patches++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
528 } else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
529 msb = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
530
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
531 if (sbr->f_master[k] - sb < 3)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
532 k = sbr->n_master;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
533 } while (sb != sbr->kx[1] + sbr->m[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
534
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
535 if (sbr->patch_num_subbands[sbr->num_patches-1] < 3 && sbr->num_patches > 1)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
536 sbr->num_patches--;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
537
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
538 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
539 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
540
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
541 /// Derived Frequency Band Tables (14496-3 sp04 p197)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
542 static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
543 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
544 int k, temp;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
545
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
546 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
547 sbr->n[0] = (sbr->n[1] + 1) >> 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
548
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
549 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
550 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
551 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
552 sbr->kx[1] = sbr->f_tablehigh[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
553
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
554 // Requirements (14496-3 sp04 p205)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
555 if (sbr->kx[1] + sbr->m[1] > 64) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
556 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
557 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
558 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
559 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
560 if (sbr->kx[1] > 32) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
561 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
562 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
563 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
564
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
565 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
566 temp = sbr->n[1] & 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
567 for (k = 1; k <= sbr->n[0]; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
568 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
569
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
570 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
571 log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
572 if (sbr->n_q > 5) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
573 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
574 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
575 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
576
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
577 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
578 temp = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
579 for (k = 1; k <= sbr->n_q; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
580 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
581 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
582 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
583
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
584 if (sbr_hf_calc_npatches(ac, sbr) < 0)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
585 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
586
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
587 sbr_make_f_tablelim(sbr);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
588
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
589 sbr->data[0].f_indexnoise = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
590 sbr->data[1].f_indexnoise = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
591
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
592 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
593 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
594
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
595 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
596 int elements)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
597 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
598 int i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
599 for (i = 0; i < elements; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
600 vec[i] = get_bits1(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
601 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
602 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
603
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
604 /** ceil(log2(index+1)) */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
605 static const int8_t ceil_log2[] = {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
606 0, 1, 2, 2, 3, 3,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
607 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
608
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
609 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
610 GetBitContext *gb, SBRData *ch_data)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
611 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
612 int i;
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
613 unsigned bs_pointer = 0;
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
614 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
615 int abs_bord_trail = 16;
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
616 int num_rel_lead, num_rel_trail;
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
617 unsigned bs_num_env_old = ch_data->bs_num_env;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
618
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
619 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
620 ch_data->bs_amp_res = sbr->bs_amp_res_header;
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
621 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
622
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
623 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
624 case FIXFIX:
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
625 ch_data->bs_num_env = 1 << get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
626 num_rel_lead = ch_data->bs_num_env - 1;
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
627 if (ch_data->bs_num_env == 1)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
628 ch_data->bs_amp_res = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
629
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
630 if (ch_data->bs_num_env > 4) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
631 av_log(ac->avctx, AV_LOG_ERROR,
11434
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
632 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
633 ch_data->bs_num_env);
11434
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
634 return -1;
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
635 }
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
636
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
637 ch_data->t_env[0] = 0;
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
638 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
639
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
640 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
641 ch_data->bs_num_env;
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
642 for (i = 0; i < num_rel_lead; i++)
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
643 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
644
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
645 ch_data->bs_freq_res[1] = get_bits1(gb);
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
646 for (i = 1; i < ch_data->bs_num_env; i++)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
647 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
648 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
649 case FIXVAR:
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
650 abs_bord_trail += get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
651 num_rel_trail = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
652 ch_data->bs_num_env = num_rel_trail + 1;
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
653 ch_data->t_env[0] = 0;
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
654 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
655
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
656 for (i = 0; i < num_rel_trail; i++)
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
657 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
658 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
659
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
660 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
661
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
662 for (i = 0; i < ch_data->bs_num_env; i++)
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
663 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
664 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
665 case VARFIX:
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
666 ch_data->t_env[0] = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
667 num_rel_lead = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
668 ch_data->bs_num_env = num_rel_lead + 1;
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
669 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
670
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
671 for (i = 0; i < num_rel_lead; i++)
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
672 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
673
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
674 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
675
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
676 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
677 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
678 case VARVAR:
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
679 ch_data->t_env[0] = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
680 abs_bord_trail += get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
681 num_rel_lead = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
682 num_rel_trail = get_bits(gb, 2);
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
683 ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
684
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
685 if (ch_data->bs_num_env > 5) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
686 av_log(ac->avctx, AV_LOG_ERROR,
11434
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
687 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
688 ch_data->bs_num_env);
11434
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
689 return -1;
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
690 }
c969ceeffb36 aacsbr: Fail early on illegal envelope counts.
alexc
parents: 11433
diff changeset
691
11450
95123a24a580 aacsbr: Check that bs_num_env is valid before writing arrays with it as an offset.
alexc
parents: 11449
diff changeset
692 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
95123a24a580 aacsbr: Check that bs_num_env is valid before writing arrays with it as an offset.
alexc
parents: 11449
diff changeset
693
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
694 for (i = 0; i < num_rel_lead; i++)
11447
826c8ee54ae9 aacsbr: read bs_rel_bord directly into t_env.
alexc
parents: 11446
diff changeset
695 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
696 for (i = 0; i < num_rel_trail; i++)
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
697 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
698 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
699
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
700 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
701
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
702 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
703 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
704 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
705
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
706 if (bs_pointer > ch_data->bs_num_env + 1) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
707 av_log(ac->avctx, AV_LOG_ERROR,
11433
d8c2170062ce aacsbr: Check for illegal values of bs_pointer in sbr_read_grid().
alexc
parents: 11429
diff changeset
708 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
709 bs_pointer);
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
710 return -1;
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
711 }
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
712
11672
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
713 for (i = 1; i <= ch_data->bs_num_env; i++) {
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
714 if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
715 av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
11672
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
716 return -1;
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
717 }
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
718 }
f838fa3eefee Enforce time border monotonicity.
alexc
parents: 11671
diff changeset
719
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
720 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
721
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
722 ch_data->t_q[0] = ch_data->t_env[0];
11448
9fd8da0f1a50 aacsbr: Factor out the common end border case from t_q setup.
alexc
parents: 11447
diff changeset
723 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
11439
121d700285e7 aacsbr: Remove a slightly incorrect comment.
alexc
parents: 11437
diff changeset
724 if (ch_data->bs_num_noise > 1) {
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
725 unsigned int idx;
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
726 if (ch_data->bs_frame_class == FIXFIX) {
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
727 idx = ch_data->bs_num_env >> 1;
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
728 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
729 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
730 } else { // VARFIX
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
731 if (!bs_pointer)
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
732 idx = 1;
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
733 else if (bs_pointer == 1)
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
734 idx = ch_data->bs_num_env - 1;
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
735 else // bs_pointer > 1
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
736 idx = bs_pointer - 1;
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
737 }
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
738 ch_data->t_q[1] = ch_data->t_env[idx];
11448
9fd8da0f1a50 aacsbr: Factor out the common end border case from t_q setup.
alexc
parents: 11447
diff changeset
739 }
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
740
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
741 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
11436
d90f6c676063 aacsbr: Move the e_a calculation from sbr_mapping() to read_sbr_grid().
alexc
parents: 11435
diff changeset
742 ch_data->e_a[1] = -1;
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
743 if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
744 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
11437
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
745 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
8b2d9e680000 aacsbr: Cleanup the newly merged read_sbr_grid, eliminating several context
alexc
parents: 11436
diff changeset
746 ch_data->e_a[1] = bs_pointer - 1;
11436
d90f6c676063 aacsbr: Move the e_a calculation from sbr_mapping() to read_sbr_grid().
alexc
parents: 11435
diff changeset
747
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
748 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
749 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
750
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
751 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
752 //These variables are saved from the previous frame rather than copied
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
753 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
754 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
755 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
756
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
757 //These variables are read from the bitstream and therefore copied
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
758 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
11435
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
759 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
4dfd0bfbb8dc aacsbr: Merge sbr_time_freq_grid into read_sbr_grid (and into copy_sbr_grid).
alexc
parents: 11434
diff changeset
760 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
11449
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
761 dst->bs_num_env = src->bs_num_env;
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
762 dst->bs_amp_res = src->bs_amp_res;
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
763 dst->bs_num_noise = src->bs_num_noise;
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
764 dst->bs_frame_class = src->bs_frame_class;
e62f45fd47d4 aacsbr: Cleanup read_sbr_grid and copy_sbr_grid after the recent overhaul of those functions.
alexc
parents: 11448
diff changeset
765 dst->e_a[1] = src->e_a[1];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
766 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
767
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
768 /// Read how the envelope and noise floor data is delta coded
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
769 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
770 SBRData *ch_data)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
771 {
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
772 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
773 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
774 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
775
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
776 /// Read inverse filtering data
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
777 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
778 SBRData *ch_data)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
779 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
780 int i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
781
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
782 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
783 for (i = 0; i < sbr->n_q; i++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
784 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
785 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
786
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
787 static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
788 SBRData *ch_data, int ch)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
789 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
790 int bits;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
791 int i, j, k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
792 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
793 int t_lav, f_lav;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
794 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
795 const int odd = sbr->n[1] & 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
796
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
797 if (sbr->bs_coupling && ch) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
798 if (ch_data->bs_amp_res) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
799 bits = 5;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
800 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
801 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
802 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
803 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
804 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
805 bits = 6;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
806 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
807 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
808 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
809 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
810 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
811 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
812 if (ch_data->bs_amp_res) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
813 bits = 6;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
814 t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
815 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
816 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
817 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
818 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
819 bits = 7;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
820 t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
821 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
822 f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
823 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
824 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
825 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
826
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
827 for (i = 0; i < ch_data->bs_num_env; i++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
828 if (ch_data->bs_df_env[i]) {
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
829 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
830 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
831 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
832 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
833 } else if (ch_data->bs_freq_res[i + 1]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
834 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
835 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
836 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
837 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
838 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
839 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
840 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
841 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
842 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
843 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
844 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
845 ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
846 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
847 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
848 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
849 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
850
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
851 //assign 0th elements of env_facs from last elements
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
852 memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
853 sizeof(ch_data->env_facs[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
854 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
855
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
856 static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
857 SBRData *ch_data, int ch)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
858 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
859 int i, j;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
860 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
861 int t_lav, f_lav;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
862 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
863
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
864 if (sbr->bs_coupling && ch) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
865 t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
866 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
867 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
868 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
869 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
870 t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
871 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
872 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
873 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
874 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
875
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
876 for (i = 0; i < ch_data->bs_num_noise; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
877 if (ch_data->bs_df_noise[i]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
878 for (j = 0; j < sbr->n_q; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
879 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
880 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
881 ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
882 for (j = 1; j < sbr->n_q; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
883 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
884 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
885 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
886
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
887 //assign 0th elements of noise_facs from last elements
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
888 memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
889 sizeof(ch_data->noise_facs[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
890 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
891
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
892 static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
893 GetBitContext *gb,
11590
feb045e189bf Reindent read_sbr_extension.
alexc
parents: 11589
diff changeset
894 int bs_extension_id, int *num_bits_left)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
895 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
896 switch (bs_extension_id) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
897 case EXTENSION_ID_PS:
11589
fa81ba4670ea Print an error and skip PS when PS is found but explicitly found but
alexc
parents: 11482
diff changeset
898 if (!ac->m4ac.ps) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
899 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
11590
feb045e189bf Reindent read_sbr_extension.
alexc
parents: 11589
diff changeset
900 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
feb045e189bf Reindent read_sbr_extension.
alexc
parents: 11589
diff changeset
901 *num_bits_left = 0;
11589
fa81ba4670ea Print an error and skip PS when PS is found but explicitly found but
alexc
parents: 11482
diff changeset
902 } else {
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
903 #if 1
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
904 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
905 #else
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
906 av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
11590
feb045e189bf Reindent read_sbr_extension.
alexc
parents: 11589
diff changeset
907 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
feb045e189bf Reindent read_sbr_extension.
alexc
parents: 11589
diff changeset
908 *num_bits_left = 0;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
909 #endif
11589
fa81ba4670ea Print an error and skip PS when PS is found but explicitly found but
alexc
parents: 11482
diff changeset
910 }
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
911 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
912 default:
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
913 av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
914 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
915 *num_bits_left = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
916 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
917 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
918 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
919
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
920 static int read_sbr_single_channel_element(AACContext *ac,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
921 SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
922 GetBitContext *gb)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
923 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
924 if (get_bits1(gb)) // bs_data_extra
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
925 skip_bits(gb, 4); // bs_reserved
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
926
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
927 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
928 return -1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
929 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
930 read_sbr_invf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
931 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
932 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
933
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
934 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
935 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
11429
4eee52db3c4c 10l: Include missing return values in functions made non-void by the previous commit.
alexc
parents: 11428
diff changeset
936
4eee52db3c4c 10l: Include missing return values in functions made non-void by the previous commit.
alexc
parents: 11428
diff changeset
937 return 0;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
938 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
939
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
940 static int read_sbr_channel_pair_element(AACContext *ac,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
941 SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
942 GetBitContext *gb)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
943 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
944 if (get_bits1(gb)) // bs_data_extra
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
945 skip_bits(gb, 8); // bs_reserved
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
946
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
947 if ((sbr->bs_coupling = get_bits1(gb))) {
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
948 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
949 return -1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
950 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
951 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
952 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
953 read_sbr_invf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
954 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
955 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
956 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
957 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
958 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
959 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
960 } else {
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
961 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
962 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
963 return -1;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
964 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
965 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
966 read_sbr_invf(sbr, gb, &sbr->data[0]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
967 read_sbr_invf(sbr, gb, &sbr->data[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
968 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
969 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
970 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
971 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
972 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
973
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
974 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
975 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
976 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
977 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
11429
4eee52db3c4c 10l: Include missing return values in functions made non-void by the previous commit.
alexc
parents: 11428
diff changeset
978
4eee52db3c4c 10l: Include missing return values in functions made non-void by the previous commit.
alexc
parents: 11428
diff changeset
979 return 0;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
980 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
981
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
982 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
983 GetBitContext *gb, int id_aac)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
984 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
985 unsigned int cnt = get_bits_count(gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
986
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
987 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
988 if (read_sbr_single_channel_element(ac, sbr, gb)) {
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
989 sbr->start = 0;
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
990 return get_bits_count(gb) - cnt;
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
991 }
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
992 } else if (id_aac == TYPE_CPE) {
11428
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
993 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
994 sbr->start = 0;
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
995 return get_bits_count(gb) - cnt;
6b0d65c8c13d aacsbr: Propagate errors from read_sbr_grid to prevent crashes in malformatted streams.
alexc
parents: 11427
diff changeset
996 }
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
997 } else {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
998 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
999 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1000 sbr->start = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1001 return get_bits_count(gb) - cnt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1002 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1003 if (get_bits1(gb)) { // bs_extended_data
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1004 int num_bits_left = get_bits(gb, 4); // bs_extension_size
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1005 if (num_bits_left == 15)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1006 num_bits_left += get_bits(gb, 8); // bs_esc_count
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1007
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1008 num_bits_left <<= 3;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1009 while (num_bits_left > 7) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1010 num_bits_left -= 2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1011 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1012 }
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1013 if (num_bits_left < 0) {
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1014 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1015 }
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1016 if (num_bits_left > 0)
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1017 skip_bits(gb, num_bits_left);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1018 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1019
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1020 return get_bits_count(gb) - cnt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1021 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1022
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1023 static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1024 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1025 int err;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1026 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1027 if (err >= 0)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1028 err = sbr_make_f_derived(ac, sbr);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1029 if (err < 0) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
1030 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1031 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1032 sbr->start = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1033 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1034 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1035
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1036 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1037 * Decode Spectral Band Replication extension data; reference: table 4.55.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1038 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1039 * @param crc flag indicating the presence of CRC checksum
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1040 * @param cnt length of TYPE_FIL syntactic element in bytes
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1041 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1042 * @return Returns number of bytes consumed from the TYPE_FIL element.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1043 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1044 int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1045 GetBitContext *gb_host, int crc, int cnt, int id_aac)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1046 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1047 unsigned int num_sbr_bits = 0, num_align_bits;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1048 unsigned bytes_read;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1049 GetBitContext gbc = *gb_host, *gb = &gbc;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1050 skip_bits_long(gb_host, cnt*8 - 4);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1051
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1052 sbr->reset = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1053
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1054 if (!sbr->sample_rate)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1055 sbr->sample_rate = 2 * ac->m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1056 if (!ac->m4ac.ext_sample_rate)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1057 ac->m4ac.ext_sample_rate = 2 * ac->m4ac.sample_rate;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1058
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1059 if (crc) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1060 skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1061 num_sbr_bits += 10;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1062 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1063
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1064 //Save some state from the previous frame.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1065 sbr->kx[0] = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1066 sbr->m[0] = sbr->m[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1067
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1068 num_sbr_bits++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1069 if (get_bits1(gb)) // bs_header_flag
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1070 num_sbr_bits += read_sbr_header(sbr, gb);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1071
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1072 if (sbr->reset)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1073 sbr_reset(ac, sbr);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1074
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1075 if (sbr->start)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1076 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1077
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1078 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1079 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1080
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1081 if (bytes_read > cnt) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
1082 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1083 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1084 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1085 return cnt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1086 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1087
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1088 /// Dequantization and stereo decoding (14496-3 sp04 p203)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1089 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1090 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1091 int k, e;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1092 int ch;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1093
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1094 if (id_aac == TYPE_CPE && sbr->bs_coupling) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1095 float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1096 float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1097 for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1098 for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1099 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1100 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1101 float fac = temp1 / (1.0f + temp2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1102 sbr->data[0].env_facs[e][k] = fac;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1103 sbr->data[1].env_facs[e][k] = fac * temp2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1104 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1105 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1106 for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1107 for (k = 0; k < sbr->n_q; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1108 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1109 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1110 float fac = temp1 / (1.0f + temp2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1111 sbr->data[0].noise_facs[e][k] = fac;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1112 sbr->data[1].noise_facs[e][k] = fac * temp2;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1113 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1114 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1115 } else { // SCE or one non-coupled CPE
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1116 for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1117 float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1118 for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1119 for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1120 sbr->data[ch].env_facs[e][k] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1121 exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1122 for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1123 for (k = 0; k < sbr->n_q; k++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1124 sbr->data[ch].noise_facs[e][k] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1125 exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1126 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1127 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1128 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1129
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1130 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1131 * Analysis QMF Bank (14496-3 sp04 p206)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1132 *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1133 * @param x pointer to the beginning of the first sample window
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1134 * @param W array of complex-valued samples split into subbands
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1135 */
11848
6b52f0c3263d 10l: Fix types and now unused variables after last commit.
alexc
parents: 11847
diff changeset
1136 static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1137 float z[320], float W[2][32][32][2],
11481
96dde15b2e0d aac: Don't bias output during the IMDCT if SBR will be used.
alexc
parents: 11450
diff changeset
1138 float scale)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1139 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1140 int i, k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1141 memcpy(W[0], W[1], sizeof(W[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1142 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
11481
96dde15b2e0d aac: Don't bias output during the IMDCT if SBR will be used.
alexc
parents: 11450
diff changeset
1143 if (scale != 1.0f)
11482
7211254a444c aacsbr: DSPUtilize analysis input scaling.
alexc
parents: 11481
diff changeset
1144 dsp->vector_fmul_scalar(x+288, in, scale, 1024);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1145 else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1146 memcpy(x+288, in, 1024*sizeof(*x));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1147 for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1148 // are not supported
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1149 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1150 for (k = 0; k < 64; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1151 float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1152 z[k] = f;
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1153 }
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1154 //Shuffle to IMDCT
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1155 z[64] = z[0];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1156 for (k = 1; k < 32; k++) {
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1157 z[64+2*k-1] = z[ k];
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1158 z[64+2*k ] = -z[64-k];
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1159 }
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1160 z[64+63] = z[32];
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1161
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1162 ff_imdct_half(mdct, z, z+64);
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1163 for (k = 0; k < 32; k++) {
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1164 W[1][i][k][0] = -z[63-k];
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1165 W[1][i][k][1] = z[k];
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1166 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1167 x += 32;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1168 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1169 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1170
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1171 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1172 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1173 * (14496-3 sp04 p206)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1174 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1175 static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1176 float *out, float X[2][38][64],
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1177 float mdct_buf[2][64],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1178 float *v0, int *v_off, const unsigned int div,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1179 float bias, float scale)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1180 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1181 int i, n;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1182 const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1183 int scale_and_bias = scale != 1.0f || bias != 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1184 float *v;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1185 for (i = 0; i < 32; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1186 if (*v_off == 0) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1187 int saved_samples = (1280 - 128) >> div;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1188 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1189 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1190 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1191 *v_off -= 128 >> div;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1192 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1193 v = v0 + *v_off;
11849
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1194 if (div) {
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1195 for (n = 0; n < 32; n++) {
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1196 X[0][i][ n] = -X[0][i][n];
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1197 X[0][i][32+n] = X[1][i][31-n];
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1198 }
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1199 ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1200 for (n = 0; n < 32; n++) {
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1201 v[ n] = mdct_buf[0][63 - 2*n];
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1202 v[63 - n] = -mdct_buf[0][62 - 2*n];
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1203 }
f2c42ef01008 Base downsampled SBR synthesis filterbank on a single IMDCT.
alexc
parents: 11848
diff changeset
1204 } else {
11850
954d05e65641 Cosmetics: Fold constants and re-indent after last commit.
alexc
parents: 11849
diff changeset
1205 for (n = 1; n < 64; n+=2) {
954d05e65641 Cosmetics: Fold constants and re-indent after last commit.
alexc
parents: 11849
diff changeset
1206 X[1][i][n] = -X[1][i][n];
954d05e65641 Cosmetics: Fold constants and re-indent after last commit.
alexc
parents: 11849
diff changeset
1207 }
954d05e65641 Cosmetics: Fold constants and re-indent after last commit.
alexc
parents: 11849
diff changeset
1208 ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
954d05e65641 Cosmetics: Fold constants and re-indent after last commit.
alexc
parents: 11849
diff changeset
1209 ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1210 for (n = 0; n < 64; n++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1211 v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1212 v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1213 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1214 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1215 dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1216 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1217 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1218 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1219 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1220 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1221 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1222 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1223 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1224 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1225 if (scale_and_bias)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1226 for (n = 0; n < 64 >> div; n++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1227 out[n] = out[n] * scale + bias;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1228 out += 64 >> div;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1229 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1230 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1231
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1232 static void autocorrelate(const float x[40][2], float phi[3][2][2], int lag)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1233 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1234 int i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1235 float real_sum = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1236 float imag_sum = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1237 if (lag) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1238 for (i = 1; i < 38; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1239 real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1240 imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1241 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1242 phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1243 phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1244 if (lag == 1) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1245 phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1246 phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1247 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1248 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1249 for (i = 1; i < 38; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1250 real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1251 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1252 phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1253 phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1254 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1255 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1256
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1257 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1258 * (14496-3 sp04 p214)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1259 * Warning: This routine does not seem numerically stable.
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1260 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1261 static void sbr_hf_inverse_filter(float (*alpha0)[2], float (*alpha1)[2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1262 const float X_low[32][40][2], int k0)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1263 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1264 int k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1265 for (k = 0; k < k0; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1266 float phi[3][2][2], dk;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1267
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1268 autocorrelate(X_low[k], phi, 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1269 autocorrelate(X_low[k], phi, 1);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1270 autocorrelate(X_low[k], phi, 2);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1271
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1272 dk = phi[2][1][0] * phi[1][0][0] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1273 (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1274
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1275 if (!dk) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1276 alpha1[k][0] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1277 alpha1[k][1] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1278 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1279 float temp_real, temp_im;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1280 temp_real = phi[0][0][0] * phi[1][1][0] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1281 phi[0][0][1] * phi[1][1][1] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1282 phi[0][1][0] * phi[1][0][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1283 temp_im = phi[0][0][0] * phi[1][1][1] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1284 phi[0][0][1] * phi[1][1][0] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1285 phi[0][1][1] * phi[1][0][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1286
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1287 alpha1[k][0] = temp_real / dk;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1288 alpha1[k][1] = temp_im / dk;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1289 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1290
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1291 if (!phi[1][0][0]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1292 alpha0[k][0] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1293 alpha0[k][1] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1294 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1295 float temp_real, temp_im;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1296 temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1297 alpha1[k][1] * phi[1][1][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1298 temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1299 alpha1[k][0] * phi[1][1][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1300
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1301 alpha0[k][0] = -temp_real / phi[1][0][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1302 alpha0[k][1] = -temp_im / phi[1][0][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1303 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1304
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1305 if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1306 alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1307 alpha1[k][0] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1308 alpha1[k][1] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1309 alpha0[k][0] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1310 alpha0[k][1] = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1311 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1312 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1313 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1314
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1315 /// Chirp Factors (14496-3 sp04 p214)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1316 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1317 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1318 int i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1319 float new_bw;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1320 static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1321
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1322 for (i = 0; i < sbr->n_q; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1323 if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1324 new_bw = 0.6f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1325 } else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1326 new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1327
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1328 if (new_bw < ch_data->bw_array[i]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1329 new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1330 } else
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1331 new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1332 ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1333 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1334 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1335
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1336 /// Generate the subband filtered lowband
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1337 static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1338 float X_low[32][40][2], const float W[2][32][32][2])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1339 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1340 int i, k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1341 const int t_HFGen = 8;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1342 const int i_f = 32;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1343 memset(X_low, 0, 32*sizeof(*X_low));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1344 for (k = 0; k < sbr->kx[1]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1345 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1346 X_low[k][i][0] = W[1][i - t_HFGen][k][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1347 X_low[k][i][1] = W[1][i - t_HFGen][k][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1348 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1349 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1350 for (k = 0; k < sbr->kx[0]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1351 for (i = 0; i < t_HFGen; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1352 X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1353 X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1354 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1355 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1356 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1357 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1358
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1359 /// High Frequency Generator (14496-3 sp04 p215)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1360 static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1361 float X_high[64][40][2], const float X_low[32][40][2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1362 const float (*alpha0)[2], const float (*alpha1)[2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1363 const float bw_array[5], const uint8_t *t_env,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1364 int bs_num_env)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1365 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1366 int i, j, x;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1367 int g = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1368 int k = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1369 for (j = 0; j < sbr->num_patches; j++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1370 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1371 float alpha[4];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1372 const int p = sbr->patch_start_subband[j] + x;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1373 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1374 g++;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1375 g--;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1376
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1377 if (g < 0) {
11833
9103a9b3573a aacdec: Rename avccontext to avctx.
alexc
parents: 11677
diff changeset
1378 av_log(ac->avctx, AV_LOG_ERROR,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1379 "ERROR : no subband found for frequency %d\n", k);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1380 return -1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1381 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1382
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1383 alpha[0] = alpha1[p][0] * bw_array[g] * bw_array[g];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1384 alpha[1] = alpha1[p][1] * bw_array[g] * bw_array[g];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1385 alpha[2] = alpha0[p][0] * bw_array[g];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1386 alpha[3] = alpha0[p][1] * bw_array[g];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1387
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1388 for (i = 2 * t_env[0]; i < 2 * t_env[bs_num_env]; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1389 const int idx = i + ENVELOPE_ADJUSTMENT_OFFSET;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1390 X_high[k][idx][0] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1391 X_low[p][idx - 2][0] * alpha[0] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1392 X_low[p][idx - 2][1] * alpha[1] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1393 X_low[p][idx - 1][0] * alpha[2] -
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1394 X_low[p][idx - 1][1] * alpha[3] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1395 X_low[p][idx][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1396 X_high[k][idx][1] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1397 X_low[p][idx - 2][1] * alpha[0] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1398 X_low[p][idx - 2][0] * alpha[1] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1399 X_low[p][idx - 1][1] * alpha[2] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1400 X_low[p][idx - 1][0] * alpha[3] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1401 X_low[p][idx][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1402 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1403 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1404 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1405 if (k < sbr->m[1] + sbr->kx[1])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1406 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1407
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1408 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1409 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1410
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1411 /// Generate the subband filtered lowband
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1412 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1413 const float X_low[32][40][2], const float Y[2][38][64][2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1414 int ch)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1415 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1416 int k, i;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1417 const int i_f = 32;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1418 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1419 memset(X, 0, 2*sizeof(*X));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1420 for (k = 0; k < sbr->kx[0]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1421 for (i = 0; i < i_Temp; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1422 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1423 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1424 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1425 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1426 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1427 for (i = 0; i < i_Temp; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1428 X[0][i][k] = Y[0][i + i_f][k][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1429 X[1][i][k] = Y[0][i + i_f][k][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1430 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1431 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1432
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1433 for (k = 0; k < sbr->kx[1]; k++) {
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1434 for (i = i_Temp; i < 38; i++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1435 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1436 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1437 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1438 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1439 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1440 for (i = i_Temp; i < i_f; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1441 X[0][i][k] = Y[1][i][k][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1442 X[1][i][k] = Y[1][i][k][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1443 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1444 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1445 return 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1446 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1447
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1448 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1449 * (14496-3 sp04 p217)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1450 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1451 static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1452 SBRData *ch_data, int e_a[2])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1453 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1454 int e, i, m;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1455
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1456 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1457 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1458 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1459 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1460 int k;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1461
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1462 for (i = 0; i < ilim; i++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1463 for (m = table[i]; m < table[i + 1]; m++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1464 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1465
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1466 // ch_data->bs_num_noise > 1 => 2 noise floors
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1467 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1468 for (i = 0; i < sbr->n_q; i++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1469 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1470 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1471
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1472 for (i = 0; i < sbr->n[1]; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1473 if (ch_data->bs_add_harmonic_flag) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1474 const unsigned int m_midpoint =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1475 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1476
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1477 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1478 (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1479 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1480 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1481
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1482 for (i = 0; i < ilim; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1483 int additional_sinusoid_present = 0;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1484 for (m = table[i]; m < table[i + 1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1485 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1486 additional_sinusoid_present = 1;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1487 break;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1488 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1489 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1490 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1491 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1492 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1493 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1494
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1495 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1496 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1497
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1498 /// Estimation of current envelope (14496-3 sp04 p218)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1499 static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1500 SpectralBandReplication *sbr, SBRData *ch_data)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1501 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1502 int e, i, m;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1503
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1504 if (sbr->bs_interpol_freq) {
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1505 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1506 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1507 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1508 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1509
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1510 for (m = 0; m < sbr->m[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1511 float sum = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1512
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1513 for (i = ilb; i < iub; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1514 sum += X_high[m + sbr->kx[1]][i][0] * X_high[m + sbr->kx[1]][i][0] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1515 X_high[m + sbr->kx[1]][i][1] * X_high[m + sbr->kx[1]][i][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1516 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1517 e_curr[e][m] = sum * recip_env_size;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1518 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1519 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1520 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1521 int k, p;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1522
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1523 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1524 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1525 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1526 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1527 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1528
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1529 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1530 float sum = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1531 const int den = env_size * (table[p + 1] - table[p]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1532
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1533 for (k = table[p]; k < table[p + 1]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1534 for (i = ilb; i < iub; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1535 sum += X_high[k][i][0] * X_high[k][i][0] +
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1536 X_high[k][i][1] * X_high[k][i][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1537 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1538 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1539 sum /= den;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1540 for (k = table[p]; k < table[p + 1]; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1541 e_curr[e][k - sbr->kx[1]] = sum;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1542 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1543 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1544 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1545 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1546 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1547
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1548 /**
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1549 * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1550 * and Calculation of gain (14496-3 sp04 p219)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1551 */
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1552 static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1553 SBRData *ch_data, const int e_a[2])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1554 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1555 int e, k, m;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1556 // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1557 static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1558
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1559 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1560 int delta = !((e == e_a[1]) || (e == e_a[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1561 for (k = 0; k < sbr->n_lim; k++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1562 float gain_boost, gain_max;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1563 float sum[2] = { 0.0f, 0.0f };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1564 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1565 const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1566 sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1567 sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1568 if (!sbr->s_mapped[e][m]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1569 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1570 ((1.0f + sbr->e_curr[e][m]) *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1571 (1.0f + sbr->q_mapped[e][m] * delta)));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1572 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1573 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1574 ((1.0f + sbr->e_curr[e][m]) *
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1575 (1.0f + sbr->q_mapped[e][m])));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1576 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1577 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1578 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1579 sum[0] += sbr->e_origmapped[e][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1580 sum[1] += sbr->e_curr[e][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1581 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1582 gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
12156
b1c9654b8d68 aacsbr: Eliminate double precision arithmetic.
alexc
parents: 11922
diff changeset
1583 gain_max = FFMIN(100000.f, gain_max);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1584 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1585 float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1586 sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1587 sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1588 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1589 sum[0] = sum[1] = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1590 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1591 sum[0] += sbr->e_origmapped[e][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1592 sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1593 + sbr->s_m[e][m] * sbr->s_m[e][m]
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1594 + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1595 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1596 gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
12156
b1c9654b8d68 aacsbr: Eliminate double precision arithmetic.
alexc
parents: 11922
diff changeset
1597 gain_boost = FFMIN(1.584893192f, gain_boost);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1598 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1599 sbr->gain[e][m] *= gain_boost;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1600 sbr->q_m[e][m] *= gain_boost;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1601 sbr->s_m[e][m] *= gain_boost;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1602 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1603 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1604 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1605 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1606
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1607 /// Assembling HF Signals (14496-3 sp04 p220)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1608 static void sbr_hf_assemble(float Y[2][38][64][2], const float X_high[64][40][2],
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1609 SpectralBandReplication *sbr, SBRData *ch_data,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1610 const int e_a[2])
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1611 {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1612 int e, i, j, m;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1613 const int h_SL = 4 * !sbr->bs_smoothing_mode;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1614 const int kx = sbr->kx[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1615 const int m_max = sbr->m[1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1616 static const float h_smooth[5] = {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1617 0.33333333333333,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1618 0.30150283239582,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1619 0.21816949906249,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1620 0.11516383427084,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1621 0.03183050093751,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1622 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1623 static const int8_t phi[2][4] = {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1624 { 1, 0, -1, 0}, // real
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1625 { 0, 1, 0, -1}, // imaginary
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1626 };
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1627 float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1628 int indexnoise = ch_data->f_indexnoise;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1629 int indexsine = ch_data->f_indexsine;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1630 memcpy(Y[0], Y[1], sizeof(Y[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1631
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1632 if (sbr->reset) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1633 for (i = 0; i < h_SL; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1634 memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1635 memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1636 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1637 } else if (h_SL) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1638 memcpy(g_temp[2*ch_data->t_env[0]], g_temp[2*ch_data->t_env_num_env_old], 4*sizeof(g_temp[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1639 memcpy(q_temp[2*ch_data->t_env[0]], q_temp[2*ch_data->t_env_num_env_old], 4*sizeof(q_temp[0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1640 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1641
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1642 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1643 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1644 memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1645 memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1646 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1647 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1648
11446
806dc446061d aacsbr: Make the previous value of bs_num_env local to read_sbr_data().
alexc
parents: 11445
diff changeset
1649 for (e = 0; e < ch_data->bs_num_env; e++) {
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1650 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1651 int phi_sign = (1 - 2*(kx & 1));
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1652
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1653 if (h_SL && e != e_a[0] && e != e_a[1]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1654 for (m = 0; m < m_max; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1655 const int idx1 = i + h_SL;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1656 float g_filt = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1657 for (j = 0; j <= h_SL; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1658 g_filt += g_temp[idx1 - j][m] * h_smooth[j];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1659 Y[1][i][m + kx][0] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1660 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1661 Y[1][i][m + kx][1] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1662 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1663 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1664 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1665 for (m = 0; m < m_max; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1666 const float g_filt = g_temp[i + h_SL][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1667 Y[1][i][m + kx][0] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1668 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1669 Y[1][i][m + kx][1] =
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1670 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1671 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1672 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1673
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1674 if (e != e_a[0] && e != e_a[1]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1675 for (m = 0; m < m_max; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1676 indexnoise = (indexnoise + 1) & 0x1ff;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1677 if (sbr->s_m[e][m]) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1678 Y[1][i][m + kx][0] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1679 sbr->s_m[e][m] * phi[0][indexsine];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1680 Y[1][i][m + kx][1] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1681 sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1682 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1683 float q_filt;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1684 if (h_SL) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1685 const int idx1 = i + h_SL;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1686 q_filt = 0.0f;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1687 for (j = 0; j <= h_SL; j++)
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1688 q_filt += q_temp[idx1 - j][m] * h_smooth[j];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1689 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1690 q_filt = q_temp[i][m];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1691 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1692 Y[1][i][m + kx][0] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1693 q_filt * sbr_noise_table[indexnoise][0];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1694 Y[1][i][m + kx][1] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1695 q_filt * sbr_noise_table[indexnoise][1];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1696 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1697 phi_sign = -phi_sign;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1698 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1699 } else {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1700 indexnoise = (indexnoise + m_max) & 0x1ff;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1701 for (m = 0; m < m_max; m++) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1702 Y[1][i][m + kx][0] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1703 sbr->s_m[e][m] * phi[0][indexsine];
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1704 Y[1][i][m + kx][1] +=
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1705 sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1706 phi_sign = -phi_sign;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1707 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1708 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1709 indexsine = (indexsine + 1) & 3;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1710 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1711 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1712 ch_data->f_indexnoise = indexnoise;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1713 ch_data->f_indexsine = indexsine;
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1714 }
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1715
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1716 void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1717 float* L, float* R)
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1718 {
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1719 int downsampled = ac->m4ac.ext_sample_rate < sbr->sample_rate;
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1720 int ch;
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1721 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1722
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1723 if (sbr->start) {
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1724 sbr_dequant(sbr, id_aac);
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1725 }
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1726 for (ch = 0; ch < nch; ch++) {
11668
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1727 /* decode channel */
11847
67206bbbab10 Rewrite the SBR decoder QMF analysis filter on top of the IMDCT instead of the RDFT.
alexc
parents: 11833
diff changeset
1728 sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
11668
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1729 (float*)sbr->qmf_filter_scratch,
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1730 sbr->data[ch].W, 1/(-1024 * ac->sf_scale));
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1731 sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1732 if (sbr->start) {
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1733 sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1734 sbr_chirp(sbr, &sbr->data[ch]);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1735 sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1736 sbr->data[ch].bw_array, sbr->data[ch].t_env,
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1737 sbr->data[ch].bs_num_env);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1738
11668
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1739 // hf_adj
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1740 sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1741 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1742 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1743 sbr_hf_assemble(sbr->data[ch].Y, sbr->X_high, sbr, &sbr->data[ch],
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1744 sbr->data[ch].e_a);
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1745 }
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1746
11668
db33423d99ac Reindent
alexc
parents: 11667
diff changeset
1747 /* synthesis */
11677
ed3d232c9d31 10l: The SBR refactor requires the use of 2 independent output X buffers.
alexc
parents: 11672
diff changeset
1748 sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch);
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1749 }
11886
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1750
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1751 if (ac->m4ac.ps == 1) {
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1752 if (sbr->ps.start) {
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1753 ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1754 } else {
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1755 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1756 }
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1757 nch = 2;
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1758 }
2d81202be6e2 Add HE-AAC v2 support to the AAC decoder.
alexc
parents: 11850
diff changeset
1759
11677
ed3d232c9d31 10l: The SBR refactor requires the use of 2 independent output X buffers.
alexc
parents: 11672
diff changeset
1760 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1761 sbr->data[0].synthesis_filterbank_samples,
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1762 &sbr->data[0].synthesis_filterbank_samples_offset,
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1763 downsampled,
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1764 ac->add_bias, -1024 * ac->sf_scale);
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1765 if (nch == 2)
11677
ed3d232c9d31 10l: The SBR refactor requires the use of 2 independent output X buffers.
alexc
parents: 11672
diff changeset
1766 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,
11667
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1767 sbr->data[1].synthesis_filterbank_samples,
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1768 &sbr->data[1].synthesis_filterbank_samples_offset,
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1769 downsampled,
6818ea5a25a2 Rewrite ff_sbr_apply in a manner more friendly to PS.
alexc
parents: 11644
diff changeset
1770 ac->add_bias, -1024 * ac->sf_scale);
11401
e340262ba532 Add an HE-AAC v1 decoder.
alexc
parents:
diff changeset
1771 }