FFmpeg
Loading...
Searching...
No Matches
aacdec_proc_template.c
Go to the documentation of this file.
1/*
2 * AAC decoder
3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5 * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
6 *
7 * AAC LATM decoder
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10 *
11 * AAC decoder fixed-point implementation
12 * Copyright (c) 2013
13 * MIPS Technologies, Inc., California.
14 *
15 * This file is part of FFmpeg.
16 *
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32/**
33 * linear congruential pseudorandom number generator
34 *
35 * @param previous_val pointer to the current state of the generator
36 *
37 * @return Returns a 32-bit pseudorandom integer
38 */
39static av_always_inline int lcg_random(unsigned previous_val)
40{
41 union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
42 return v.s;
43}
44
45/**
46 * Decode spectral data; reference: table 4.50.
47 * Dequantize and scale spectral data; reference: 4.6.3.3.
48 *
49 * @param coef array of dequantized, scaled spectral data
50 * @param sf array of scalefactors or intensity stereo positions
51 * @param pulse_present set if pulses are present
52 * @param pulse pointer to pulse data struct
53 * @param band_type array of the used band type
54 *
55 * @return Returns error status. 0 - OK, !0 - error
56 */
58 GetBitContext *gb,
59 const Pulse *pulse,
61{
62 int i, k, g, idx = 0;
63 INTFLOAT *coef = sce->AAC_RENAME(coeffs);
64 IndividualChannelStream *ics = &sce->ics;
65 const int c = 1024 / ics->num_windows;
66 const uint16_t *offsets = ics->swb_offset;
67 const INTFLOAT *sf = sce->AAC_RENAME(sf);
68 const enum BandType *band_type = sce->band_type;
69 INTFLOAT *coef_base = coef;
70
71 for (g = 0; g < ics->num_windows; g++)
72 memset(coef + g * 128 + offsets[ics->max_sfb], 0,
73 sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
74
75 for (g = 0; g < ics->num_window_groups; g++) {
76 unsigned g_len = ics->group_len[g];
77
78 for (i = 0; i < ics->max_sfb; i++, idx++) {
79 const unsigned cbt_m1 = band_type[idx] - 1;
80 INTFLOAT *cfo = coef + offsets[i];
81 int off_len = offsets[i + 1] - offsets[i];
82 int group;
83
84 if (cbt_m1 >= INTENSITY_BT2 - 1) {
85 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
86 memset(cfo, 0, off_len * sizeof(*cfo));
87 }
88 } else if (cbt_m1 == NOISE_BT - 1) {
89 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
90 INTFLOAT band_energy;
91#if USE_FIXED
92 for (k = 0; k < off_len; k++) {
93 ac->random_state = lcg_random(ac->random_state);
94 cfo[k] = ac->random_state >> 3;
95 }
96
97 band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
98 band_energy = fixed_sqrt(band_energy, 31);
99 noise_scale(cfo, sf[idx], band_energy, off_len);
100#else
101 float scale;
102
103 for (k = 0; k < off_len; k++) {
104 ac->random_state = lcg_random(ac->random_state);
105 cfo[k] = ac->random_state;
106 }
107
108 band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
109 scale = sf[idx] / sqrtf(band_energy);
110 ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
111#endif /* USE_FIXED */
112 }
113 } else {
114#if !USE_FIXED
115 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
116#endif /* !USE_FIXED */
117 const VLCElem *vlc_tab = ff_vlc_spectral[cbt_m1];
118 OPEN_READER(re, gb);
119
120 switch (cbt_m1 >> 1) {
121 case 0:
122 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
123 INTFLOAT *cf = cfo;
124 int len = off_len;
125
126 do {
127 int code;
128 unsigned cb_idx;
129
130 UPDATE_CACHE(re, gb);
131 GET_VLC(code, re, gb, vlc_tab, 8, 2);
132 cb_idx = code;
133#if USE_FIXED
134 cf = DEC_SQUAD(cf, cb_idx);
135#else
136 cf = VMUL4(cf, vq, cb_idx, sf + idx);
137#endif /* USE_FIXED */
138 } while (len -= 4);
139 }
140 break;
141
142 case 1:
143 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
144 INTFLOAT *cf = cfo;
145 int len = off_len;
146
147 do {
148 int code;
149 unsigned nnz;
150 unsigned cb_idx;
151 uint32_t bits;
152
153 UPDATE_CACHE(re, gb);
154 GET_VLC(code, re, gb, vlc_tab, 8, 2);
155 cb_idx = code;
156 nnz = cb_idx >> 8 & 15;
157 bits = nnz ? GET_CACHE(re, gb) : 0;
158 LAST_SKIP_BITS(re, gb, nnz);
159#if USE_FIXED
160 cf = DEC_UQUAD(cf, cb_idx, bits);
161#else
162 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
163#endif /* USE_FIXED */
164 } while (len -= 4);
165 }
166 break;
167
168 case 2:
169 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
170 INTFLOAT *cf = cfo;
171 int len = off_len;
172
173 do {
174 int code;
175 unsigned cb_idx;
176
177 UPDATE_CACHE(re, gb);
178 GET_VLC(code, re, gb, vlc_tab, 8, 2);
179 cb_idx = code;
180#if USE_FIXED
181 cf = DEC_SPAIR(cf, cb_idx);
182#else
183 cf = VMUL2(cf, vq, cb_idx, sf + idx);
184#endif /* USE_FIXED */
185 } while (len -= 2);
186 }
187 break;
188
189 case 3:
190 case 4:
191 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
192 INTFLOAT *cf = cfo;
193 int len = off_len;
194
195 do {
196 int code;
197 unsigned nnz;
198 unsigned cb_idx;
199 unsigned sign;
200
201 UPDATE_CACHE(re, gb);
202 GET_VLC(code, re, gb, vlc_tab, 8, 2);
203 cb_idx = code;
204 nnz = cb_idx >> 8 & 15;
205 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
206 LAST_SKIP_BITS(re, gb, nnz);
207#if USE_FIXED
208 cf = DEC_UPAIR(cf, cb_idx, sign);
209#else
210 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
211#endif /* USE_FIXED */
212 } while (len -= 2);
213 }
214 break;
215
216 default:
217 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
218#if USE_FIXED
219 int *icf = cfo;
220 int v;
221#else
222 float *cf = cfo;
223 uint32_t *icf = (uint32_t *) cf;
224#endif /* USE_FIXED */
225 int len = off_len;
226
227 do {
228 int code;
229 unsigned nzt, nnz;
230 unsigned cb_idx;
231 uint32_t bits;
232 int j;
233
234 UPDATE_CACHE(re, gb);
235 GET_VLC(code, re, gb, vlc_tab, 8, 2);
236 cb_idx = code;
237
238 if (cb_idx == 0x0000) {
239 *icf++ = 0;
240 *icf++ = 0;
241 continue;
242 }
243
244 nnz = cb_idx >> 12;
245 nzt = cb_idx >> 8;
246 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
247 LAST_SKIP_BITS(re, gb, nnz);
248
249 for (j = 0; j < 2; j++) {
250 if (nzt & 1<<j) {
251 uint32_t b;
252 int n;
253 /* The total length of escape_sequence must be < 22 bits according
254 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
255 UPDATE_CACHE(re, gb);
256 b = GET_CACHE(re, gb);
257 b = 31 - av_log2(~b);
258
259 if (b > 8) {
260 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
261 return AVERROR_INVALIDDATA;
262 }
263
264 SKIP_BITS(re, gb, b + 1);
265 b += 4;
266 n = (1 << b) + SHOW_UBITS(re, gb, b);
267 LAST_SKIP_BITS(re, gb, b);
268#if USE_FIXED
269 v = n;
270 if (bits & 1U<<31)
271 v = -v;
272 *icf++ = v;
273#else
274 *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
275#endif /* USE_FIXED */
276 bits <<= 1;
277 } else {
278#if USE_FIXED
279 v = cb_idx & 15;
280 if (bits & 1U<<31)
281 v = -v;
282 *icf++ = v;
283#else
284 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
285 *icf++ = (bits & 1U<<31) | v;
286#endif /* USE_FIXED */
287 bits <<= !!v;
288 }
289 cb_idx >>= 4;
290 }
291 } while (len -= 2);
292#if !USE_FIXED
293 ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
294#endif /* !USE_FIXED */
295 }
296 }
297
298 CLOSE_READER(re, gb);
299 }
300 }
301 coef += g_len << 7;
302 }
303
304 if (pulse) {
305 idx = 0;
306 for (i = 0; i < pulse->num_pulse; i++) {
307 INTFLOAT co = coef_base[ pulse->pos[i] ];
308 while (offsets[idx + 1] <= pulse->pos[i])
309 idx++;
310 if (band_type[idx] != NOISE_BT && sf[idx]) {
311 INTFLOAT ico = -pulse->amp[i];
312#if USE_FIXED
313 if (co) {
314 ico = co + (co > 0 ? -ico : ico);
315 }
316 coef_base[ pulse->pos[i] ] = ico;
317#else
318 if (co) {
319 co /= sf[idx];
320 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
321 }
322 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
323#endif /* USE_FIXED */
324 }
325 }
326 }
327#if USE_FIXED
328 coef = coef_base;
329 idx = 0;
330 for (g = 0; g < ics->num_window_groups; g++) {
331 unsigned g_len = ics->group_len[g];
332
333 for (i = 0; i < ics->max_sfb; i++, idx++) {
334 const unsigned cbt_m1 = band_type[idx] - 1;
335 int *cfo = coef + offsets[i];
336 int off_len = offsets[i + 1] - offsets[i];
337 int group;
338
339 if (cbt_m1 < NOISE_BT - 1) {
340 for (group = 0; group < (int)g_len; group++, cfo+=128) {
341 vector_pow43(cfo, off_len);
342 subband_scale(cfo, cfo, sf[idx], 34, off_len, ac->avctx);
343 }
344 }
345 }
346 coef += g_len << 7;
347 }
348#endif /* USE_FIXED */
349 return 0;
350}
351
352/**
353 * Decode coupling_channel_element; reference: table 4.8.
354 *
355 * @return Returns error status. 0 - OK, !0 - error
356 */
358{
359 int num_gain = 0;
360 int c, g, sfb, ret;
361 int sign;
363 SingleChannelElement *sce = &che->ch[0];
364 ChannelCoupling *coup = &che->coup;
365
366 coup->coupling_point = 2 * get_bits1(gb);
367 coup->num_coupled = get_bits(gb, 3);
368 for (c = 0; c <= coup->num_coupled; c++) {
369 num_gain++;
370 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
371 coup->id_select[c] = get_bits(gb, 4);
372 if (coup->type[c] == TYPE_CPE) {
373 coup->ch_select[c] = get_bits(gb, 2);
374 if (coup->ch_select[c] == 3)
375 num_gain++;
376 } else
377 coup->ch_select[c] = 2;
378 }
379 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
380
381 sign = get_bits(gb, 1);
382#if USE_FIXED
383 scale = get_bits(gb, 2);
384#else
385 scale = cce_scale[get_bits(gb, 2)];
386#endif
387
388 if ((ret = ff_aac_decode_ics(ac, sce, gb, 0, 0)))
389 return ret;
390
391 for (c = 0; c < num_gain; c++) {
392 int idx = 0;
393 int cge = 1;
394 int gain = 0;
395 INTFLOAT gain_cache = FIXR10(1.);
396 if (c) {
397 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
398 gain = cge ? get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - 60: 0;
399 gain_cache = GET_GAIN(scale, gain);
400#if USE_FIXED
401 if ((abs(gain_cache)-1024) >> 3 > 30)
402 return AVERROR(ERANGE);
403#endif
404 }
405 if (coup->coupling_point == AFTER_IMDCT) {
406 coup->gain[c][0] = gain_cache;
407 } else {
408 for (g = 0; g < sce->ics.num_window_groups; g++) {
409 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
410 if (sce->band_type[idx] != ZERO_BT) {
411 if (!cge) {
412 int t = get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - 60;
413 if (t) {
414 int s = 1;
415 t = gain += t;
416 if (sign) {
417 s -= 2 * (t & 0x1);
418 t >>= 1;
419 }
420 gain_cache = GET_GAIN(scale, t) * s;
421#if USE_FIXED
422 if ((abs(gain_cache)-1024) >> 3 > 30)
423 return AVERROR(ERANGE);
424#endif
425 }
426 }
427 coup->gain[c][idx] = gain_cache;
428 }
429 }
430 }
431 }
432 }
433 return 0;
434}
435
437{
438#define SET(member) aac_proc->member = AAC_RENAME(member)
441#undef SET
442#define SET(member) aac_proc->member = AV_JOIN(ff_aac_, AAC_RENAME(member));
443 SET(sbr_ctx_alloc_init);
444 SET(sbr_decode_extension);
445 SET(sbr_apply);
446 SET(sbr_ctx_close);
447#undef SET
448}
BandType
Definition aac.h:70
@ ZERO_BT
Scalefactors and spectral data are all zero.
Definition aac.h:71
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition aac.h:76
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition aac.h:75
@ TYPE_CPE
Definition aac.h:45
@ TYPE_SCE
Definition aac.h:44
#define AAC_RENAME(x)
Definition aac_defines.h:99
unsigned AAC_SIGNE
#define FIXR10(x)
#define GET_GAIN(x, y)
@ AFTER_IMDCT
Definition aacdec.h:72
static void subband_scale(int *dst, int *src, int scale, int offset, int len, void *log_context)
static int * DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
static int * DEC_SPAIR(int *dst, unsigned idx)
static int * DEC_SQUAD(int *dst, unsigned idx)
static void vector_pow43(int *coefs, int len)
static void noise_scale(int *coefs, int scale, int band_energy, int len)
static int * DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
static float * VMUL4S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
static float * VMUL2S(float *dst, const float *v, unsigned idx, unsigned sign, const float *scale)
static float * VMUL2(float *dst, const float *v, unsigned idx, const float *scale)
Dequantization-related.
static float * VMUL4(float *dst, const float *v, unsigned idx, const float *scale)
static const float cce_scale[]
static int AAC_RENAME decode_cce(AACDecContext *ac, GetBitContext *gb, ChannelElement *che)
Decode coupling_channel_element; reference: table 4.8.
#define SET(member)
static av_cold void AAC_RENAME aac_proc_init(AACDecProc *aac_proc)
static int AAC_RENAME decode_spectrum_and_dequant(AACDecContext *ac, GetBitContext *gb, const Pulse *pulse, SingleChannelElement *sce)
Decode spectral data; reference: table 4.50.
static av_always_inline int lcg_random(unsigned previous_val)
linear congruential pseudorandom number generator
const VLCElem * ff_vlc_spectral[11]
Definition aacdec_tab.c:112
VLCElem ff_vlc_scalefactors[352]
Definition aacdec_tab.c:111
const float *const ff_aac_codebook_vector_vals[]
Definition aactab.c:1182
const uint32_t ff_cbrt_tab[LUT_SIZE]
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static __device__ float sqrtf(float a)
#define abs(x)
static __device__ float fabsf(float a)
#define INTFLOAT
static const uint8_t bits[8]
Definition fastaudio.c:100
static av_always_inline int fixed_sqrt(int x, int bits)
Calculate the square root.
Definition fixed_dsp.h:176
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition get_bits.h:573
#define GET_CACHE(name, gb)
Definition get_bits.h:251
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
#define SHOW_UBITS(name, gb, num)
Definition get_bits.h:247
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
#define OPEN_READER(name, gb)
Definition get_bits.h:182
#define SKIP_BITS(name, gb, num)
Definition get_bits.h:229
#define UPDATE_CACHE(name, gb)
Definition get_bits.h:213
#define LAST_SKIP_BITS(name, gb, num)
Definition get_bits.h:235
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static const int offsets[]
Definition hevc_pel.c:34
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
int ff_aac_decode_ics(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, int common_window, int scale_flag)
Decode an individual_channel_stream payload; reference: table 4.44.
Definition aacdec.c:1785
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
static av_always_inline float cbrtf(float x)
Definition libm.h:63
const uint8_t * code
Definition spdifenc.c:433
main AAC decoding context
Definition aacdec.h:500
Decode-specific primitives.
Definition aacdec.h:447
coupling parameters
Definition aacdec.h:203
int id_select[8]
element id
Definition aacdec.h:207
enum CouplingPoint coupling_point
The point during decoding at which coupling is applied.
Definition aacdec.h:204
int num_coupled
number of target elements
Definition aacdec.h:205
int ch_select[8]
[0] shared list of gains; [1] list of gains for right channel; [2] list of gains for left channel; [3...
Definition aacdec.h:208
enum RawDataBlockType type[8]
Type of channel element to be coupled - SCE or CPE.
Definition aacdec.h:206
channel element - generic struct for SCE/CPE/CCE/LFE
Definition aacdec.h:296
Individual Channel Stream.
Definition aacdec.h:169
uint8_t max_sfb
number of scalefactor bands per group
Definition aacdec.h:170
uint8_t group_len[8]
Definition aacdec.h:175
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition aacdec.h:177
Definition aac.h:103
Single Channel Element - used for both SCE and LFE elements.
Definition aacdec.h:217
enum BandType band_type[128]
band types
Definition aacdec.h:221
IndividualChannelStream ics
Definition aacdec.h:218
Definition vlc.h:32
#define av_log(a,...)
const char * g
Definition vf_curves.c:128
int len
static double c[64]