Browse code

Backport AAC-HE v2 from trunk

This patch has seen testing for a couple of weeks in ubuntu maverick and debian/experimental w/o negative feedback so far.



Originally committed as revision 24576 to svn://svn.ffmpeg.org/ffmpeg/branches/0.6

Reinhard Tartler authored on 2010/07/29 05:14:28
Showing 18 changed files
... ...
@@ -43,7 +43,7 @@ OBJS-$(CONFIG_VAAPI)                   += vaapi.o
43 43
 OBJS-$(CONFIG_VDPAU)                   += vdpau.o
44 44
 
45 45
 # decoders/encoders/hardware accelerators
46
-OBJS-$(CONFIG_AAC_DECODER)             += aac.o aactab.o aacsbr.o
46
+OBJS-$(CONFIG_AAC_DECODER)             += aacdec.o aactab.o aacsbr.o aacps.o
47 47
 OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o    \
48 48
                                           aacpsy.o aactab.o      \
49 49
                                           psymodel.o iirfilter.o \
50 50
deleted file mode 100644
... ...
@@ -1,2108 +0,0 @@
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
- *
6
- * This file is part of FFmpeg.
7
- *
8
- * FFmpeg is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU Lesser General Public
10
- * License as published by the Free Software Foundation; either
11
- * version 2.1 of the License, or (at your option) any later version.
12
- *
13
- * FFmpeg is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
- * Lesser General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU Lesser General Public
19
- * License along with FFmpeg; if not, write to the Free Software
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
- */
22
-
23
-/**
24
- * @file
25
- * AAC decoder
26
- * @author Oded Shimon  ( ods15 ods15 dyndns org )
27
- * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
28
- */
29
-
30
-/*
31
- * supported tools
32
- *
33
- * Support?             Name
34
- * N (code in SoC repo) gain control
35
- * Y                    block switching
36
- * Y                    window shapes - standard
37
- * N                    window shapes - Low Delay
38
- * Y                    filterbank - standard
39
- * N (code in SoC repo) filterbank - Scalable Sample Rate
40
- * Y                    Temporal Noise Shaping
41
- * N (code in SoC repo) Long Term Prediction
42
- * Y                    intensity stereo
43
- * Y                    channel coupling
44
- * Y                    frequency domain prediction
45
- * Y                    Perceptual Noise Substitution
46
- * Y                    Mid/Side stereo
47
- * N                    Scalable Inverse AAC Quantization
48
- * N                    Frequency Selective Switch
49
- * N                    upsampling filter
50
- * Y                    quantization & coding - AAC
51
- * N                    quantization & coding - TwinVQ
52
- * N                    quantization & coding - BSAC
53
- * N                    AAC Error Resilience tools
54
- * N                    Error Resilience payload syntax
55
- * N                    Error Protection tool
56
- * N                    CELP
57
- * N                    Silence Compression
58
- * N                    HVXC
59
- * N                    HVXC 4kbits/s VR
60
- * N                    Structured Audio tools
61
- * N                    Structured Audio Sample Bank Format
62
- * N                    MIDI
63
- * N                    Harmonic and Individual Lines plus Noise
64
- * N                    Text-To-Speech Interface
65
- * Y                    Spectral Band Replication
66
- * Y (not in this code) Layer-1
67
- * Y (not in this code) Layer-2
68
- * Y (not in this code) Layer-3
69
- * N                    SinuSoidal Coding (Transient, Sinusoid, Noise)
70
- * N (planned)          Parametric Stereo
71
- * N                    Direct Stream Transfer
72
- *
73
- * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
74
- *       - HE AAC v2 comprises LC AAC with Spectral Band Replication and
75
-           Parametric Stereo.
76
- */
77
-
78
-
79
-#include "avcodec.h"
80
-#include "internal.h"
81
-#include "get_bits.h"
82
-#include "dsputil.h"
83
-#include "fft.h"
84
-#include "lpc.h"
85
-
86
-#include "aac.h"
87
-#include "aactab.h"
88
-#include "aacdectab.h"
89
-#include "cbrt_tablegen.h"
90
-#include "sbr.h"
91
-#include "aacsbr.h"
92
-#include "mpeg4audio.h"
93
-#include "aac_parser.h"
94
-
95
-#include <assert.h>
96
-#include <errno.h>
97
-#include <math.h>
98
-#include <string.h>
99
-
100
-#if ARCH_ARM
101
-#   include "arm/aac.h"
102
-#endif
103
-
104
-union float754 {
105
-    float f;
106
-    uint32_t i;
107
-};
108
-
109
-static VLC vlc_scalefactors;
110
-static VLC vlc_spectral[11];
111
-
112
-static const char overread_err[] = "Input buffer exhausted before END element found\n";
113
-
114
-static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
115
-{
116
-    if (ac->tag_che_map[type][elem_id]) {
117
-        return ac->tag_che_map[type][elem_id];
118
-    }
119
-    if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
120
-        return NULL;
121
-    }
122
-    switch (ac->m4ac.chan_config) {
123
-    case 7:
124
-        if (ac->tags_mapped == 3 && type == TYPE_CPE) {
125
-            ac->tags_mapped++;
126
-            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
127
-        }
128
-    case 6:
129
-        /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
130
-           instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have
131
-           encountered such a stream, transfer the LFE[0] element to SCE[1] */
132
-        if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
133
-            ac->tags_mapped++;
134
-            return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
135
-        }
136
-    case 5:
137
-        if (ac->tags_mapped == 2 && type == TYPE_CPE) {
138
-            ac->tags_mapped++;
139
-            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
140
-        }
141
-    case 4:
142
-        if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
143
-            ac->tags_mapped++;
144
-            return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
145
-        }
146
-    case 3:
147
-    case 2:
148
-        if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
149
-            ac->tags_mapped++;
150
-            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
151
-        } else if (ac->m4ac.chan_config == 2) {
152
-            return NULL;
153
-        }
154
-    case 1:
155
-        if (!ac->tags_mapped && type == TYPE_SCE) {
156
-            ac->tags_mapped++;
157
-            return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
158
-        }
159
-    default:
160
-        return NULL;
161
-    }
162
-}
163
-
164
-/**
165
- * Check for the channel element in the current channel position configuration.
166
- * If it exists, make sure the appropriate element is allocated and map the
167
- * channel order to match the internal FFmpeg channel layout.
168
- *
169
- * @param   che_pos current channel position configuration
170
- * @param   type channel element type
171
- * @param   id channel element id
172
- * @param   channels count of the number of channels in the configuration
173
- *
174
- * @return  Returns error status. 0 - OK, !0 - error
175
- */
176
-static av_cold int che_configure(AACContext *ac,
177
-                         enum ChannelPosition che_pos[4][MAX_ELEM_ID],
178
-                         int type, int id,
179
-                         int *channels)
180
-{
181
-    if (che_pos[type][id]) {
182
-        if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
183
-            return AVERROR(ENOMEM);
184
-        ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
185
-        if (type != TYPE_CCE) {
186
-            ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
187
-            if (type == TYPE_CPE) {
188
-                ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
189
-            }
190
-        }
191
-    } else {
192
-        if (ac->che[type][id])
193
-            ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
194
-        av_freep(&ac->che[type][id]);
195
-    }
196
-    return 0;
197
-}
198
-
199
-/**
200
- * Configure output channel order based on the current program configuration element.
201
- *
202
- * @param   che_pos current channel position configuration
203
- * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
204
- *
205
- * @return  Returns error status. 0 - OK, !0 - error
206
- */
207
-static av_cold int output_configure(AACContext *ac,
208
-                            enum ChannelPosition che_pos[4][MAX_ELEM_ID],
209
-                            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
210
-                            int channel_config, enum OCStatus oc_type)
211
-{
212
-    AVCodecContext *avctx = ac->avccontext;
213
-    int i, type, channels = 0, ret;
214
-
215
-    memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
216
-
217
-    if (channel_config) {
218
-        for (i = 0; i < tags_per_config[channel_config]; i++) {
219
-            if ((ret = che_configure(ac, che_pos,
220
-                                     aac_channel_layout_map[channel_config - 1][i][0],
221
-                                     aac_channel_layout_map[channel_config - 1][i][1],
222
-                                     &channels)))
223
-                return ret;
224
-        }
225
-
226
-        memset(ac->tag_che_map, 0,       4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
227
-        ac->tags_mapped = 0;
228
-
229
-        avctx->channel_layout = aac_channel_layout[channel_config - 1];
230
-    } else {
231
-        /* Allocate or free elements depending on if they are in the
232
-         * current program configuration.
233
-         *
234
-         * Set up default 1:1 output mapping.
235
-         *
236
-         * For a 5.1 stream the output order will be:
237
-         *    [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
238
-         */
239
-
240
-        for (i = 0; i < MAX_ELEM_ID; i++) {
241
-            for (type = 0; type < 4; type++) {
242
-                if ((ret = che_configure(ac, che_pos, type, i, &channels)))
243
-                    return ret;
244
-            }
245
-        }
246
-
247
-        memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
248
-        ac->tags_mapped = 4 * MAX_ELEM_ID;
249
-
250
-        avctx->channel_layout = 0;
251
-    }
252
-
253
-    avctx->channels = channels;
254
-
255
-    ac->output_configured = oc_type;
256
-
257
-    return 0;
258
-}
259
-
260
-/**
261
- * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
262
- *
263
- * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
264
- * @param sce_map mono (Single Channel Element) map
265
- * @param type speaker type/position for these channels
266
- */
267
-static void decode_channel_map(enum ChannelPosition *cpe_map,
268
-                               enum ChannelPosition *sce_map,
269
-                               enum ChannelPosition type,
270
-                               GetBitContext *gb, int n)
271
-{
272
-    while (n--) {
273
-        enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
274
-        map[get_bits(gb, 4)] = type;
275
-    }
276
-}
277
-
278
-/**
279
- * Decode program configuration element; reference: table 4.2.
280
- *
281
- * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
282
- *
283
- * @return  Returns error status. 0 - OK, !0 - error
284
- */
285
-static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
286
-                      GetBitContext *gb)
287
-{
288
-    int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
289
-    int comment_len;
290
-
291
-    skip_bits(gb, 2);  // object_type
292
-
293
-    sampling_index = get_bits(gb, 4);
294
-    if (ac->m4ac.sampling_index != sampling_index)
295
-        av_log(ac->avccontext, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
296
-
297
-    num_front       = get_bits(gb, 4);
298
-    num_side        = get_bits(gb, 4);
299
-    num_back        = get_bits(gb, 4);
300
-    num_lfe         = get_bits(gb, 2);
301
-    num_assoc_data  = get_bits(gb, 3);
302
-    num_cc          = get_bits(gb, 4);
303
-
304
-    if (get_bits1(gb))
305
-        skip_bits(gb, 4); // mono_mixdown_tag
306
-    if (get_bits1(gb))
307
-        skip_bits(gb, 4); // stereo_mixdown_tag
308
-
309
-    if (get_bits1(gb))
310
-        skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
311
-
312
-    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
313
-    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE,  gb, num_side );
314
-    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK,  gb, num_back );
315
-    decode_channel_map(NULL,                  new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE,   gb, num_lfe  );
316
-
317
-    skip_bits_long(gb, 4 * num_assoc_data);
318
-
319
-    decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC,    gb, num_cc   );
320
-
321
-    align_get_bits(gb);
322
-
323
-    /* comment field, first byte is length */
324
-    comment_len = get_bits(gb, 8) * 8;
325
-    if (get_bits_left(gb) < comment_len) {
326
-        av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
327
-        return -1;
328
-    }
329
-    skip_bits_long(gb, comment_len);
330
-    return 0;
331
-}
332
-
333
-/**
334
- * Set up channel positions based on a default channel configuration
335
- * as specified in table 1.17.
336
- *
337
- * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
338
- *
339
- * @return  Returns error status. 0 - OK, !0 - error
340
- */
341
-static av_cold int set_default_channel_config(AACContext *ac,
342
-                                      enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
343
-                                      int channel_config)
344
-{
345
-    if (channel_config < 1 || channel_config > 7) {
346
-        av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
347
-               channel_config);
348
-        return -1;
349
-    }
350
-
351
-    /* default channel configurations:
352
-     *
353
-     * 1ch : front center (mono)
354
-     * 2ch : L + R (stereo)
355
-     * 3ch : front center + L + R
356
-     * 4ch : front center + L + R + back center
357
-     * 5ch : front center + L + R + back stereo
358
-     * 6ch : front center + L + R + back stereo + LFE
359
-     * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
360
-     */
361
-
362
-    if (channel_config != 2)
363
-        new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
364
-    if (channel_config > 1)
365
-        new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
366
-    if (channel_config == 4)
367
-        new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;  // back center
368
-    if (channel_config > 4)
369
-        new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
370
-        = AAC_CHANNEL_BACK;  // back stereo
371
-    if (channel_config > 5)
372
-        new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;   // LFE
373
-    if (channel_config == 7)
374
-        new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
375
-
376
-    return 0;
377
-}
378
-
379
-/**
380
- * Decode GA "General Audio" specific configuration; reference: table 4.1.
381
- *
382
- * @return  Returns error status. 0 - OK, !0 - error
383
- */
384
-static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
385
-                                     int channel_config)
386
-{
387
-    enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
388
-    int extension_flag, ret;
389
-
390
-    if (get_bits1(gb)) { // frameLengthFlag
391
-        av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
392
-        return -1;
393
-    }
394
-
395
-    if (get_bits1(gb))       // dependsOnCoreCoder
396
-        skip_bits(gb, 14);   // coreCoderDelay
397
-    extension_flag = get_bits1(gb);
398
-
399
-    if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
400
-        ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
401
-        skip_bits(gb, 3);     // layerNr
402
-
403
-    memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
404
-    if (channel_config == 0) {
405
-        skip_bits(gb, 4);  // element_instance_tag
406
-        if ((ret = decode_pce(ac, new_che_pos, gb)))
407
-            return ret;
408
-    } else {
409
-        if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
410
-            return ret;
411
-    }
412
-    if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
413
-        return ret;
414
-
415
-    if (extension_flag) {
416
-        switch (ac->m4ac.object_type) {
417
-        case AOT_ER_BSAC:
418
-            skip_bits(gb, 5);    // numOfSubFrame
419
-            skip_bits(gb, 11);   // layer_length
420
-            break;
421
-        case AOT_ER_AAC_LC:
422
-        case AOT_ER_AAC_LTP:
423
-        case AOT_ER_AAC_SCALABLE:
424
-        case AOT_ER_AAC_LD:
425
-            skip_bits(gb, 3);  /* aacSectionDataResilienceFlag
426
-                                    * aacScalefactorDataResilienceFlag
427
-                                    * aacSpectralDataResilienceFlag
428
-                                    */
429
-            break;
430
-        }
431
-        skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
432
-    }
433
-    return 0;
434
-}
435
-
436
-/**
437
- * Decode audio specific configuration; reference: table 1.13.
438
- *
439
- * @param   data        pointer to AVCodecContext extradata
440
- * @param   data_size   size of AVCCodecContext extradata
441
- *
442
- * @return  Returns error status. 0 - OK, !0 - error
443
- */
444
-static int decode_audio_specific_config(AACContext *ac, void *data,
445
-                                        int data_size)
446
-{
447
-    GetBitContext gb;
448
-    int i;
449
-
450
-    init_get_bits(&gb, data, data_size * 8);
451
-
452
-    if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
453
-        return -1;
454
-    if (ac->m4ac.sampling_index > 12) {
455
-        av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
456
-        return -1;
457
-    }
458
-
459
-    skip_bits_long(&gb, i);
460
-
461
-    switch (ac->m4ac.object_type) {
462
-    case AOT_AAC_MAIN:
463
-    case AOT_AAC_LC:
464
-        if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
465
-            return -1;
466
-        break;
467
-    default:
468
-        av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
469
-               ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
470
-        return -1;
471
-    }
472
-    return 0;
473
-}
474
-
475
-/**
476
- * linear congruential pseudorandom number generator
477
- *
478
- * @param   previous_val    pointer to the current state of the generator
479
- *
480
- * @return  Returns a 32-bit pseudorandom integer
481
- */
482
-static av_always_inline int lcg_random(int previous_val)
483
-{
484
-    return previous_val * 1664525 + 1013904223;
485
-}
486
-
487
-static av_always_inline void reset_predict_state(PredictorState *ps)
488
-{
489
-    ps->r0   = 0.0f;
490
-    ps->r1   = 0.0f;
491
-    ps->cor0 = 0.0f;
492
-    ps->cor1 = 0.0f;
493
-    ps->var0 = 1.0f;
494
-    ps->var1 = 1.0f;
495
-}
496
-
497
-static void reset_all_predictors(PredictorState *ps)
498
-{
499
-    int i;
500
-    for (i = 0; i < MAX_PREDICTORS; i++)
501
-        reset_predict_state(&ps[i]);
502
-}
503
-
504
-static void reset_predictor_group(PredictorState *ps, int group_num)
505
-{
506
-    int i;
507
-    for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
508
-        reset_predict_state(&ps[i]);
509
-}
510
-
511
-static av_cold int aac_decode_init(AVCodecContext *avccontext)
512
-{
513
-    AACContext *ac = avccontext->priv_data;
514
-    int i;
515
-
516
-    ac->avccontext = avccontext;
517
-    ac->m4ac.sample_rate = avccontext->sample_rate;
518
-
519
-    if (avccontext->extradata_size > 0) {
520
-        if (decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
521
-            return -1;
522
-    }
523
-
524
-    avccontext->sample_fmt = SAMPLE_FMT_S16;
525
-
526
-    AAC_INIT_VLC_STATIC( 0, 304);
527
-    AAC_INIT_VLC_STATIC( 1, 270);
528
-    AAC_INIT_VLC_STATIC( 2, 550);
529
-    AAC_INIT_VLC_STATIC( 3, 300);
530
-    AAC_INIT_VLC_STATIC( 4, 328);
531
-    AAC_INIT_VLC_STATIC( 5, 294);
532
-    AAC_INIT_VLC_STATIC( 6, 306);
533
-    AAC_INIT_VLC_STATIC( 7, 268);
534
-    AAC_INIT_VLC_STATIC( 8, 510);
535
-    AAC_INIT_VLC_STATIC( 9, 366);
536
-    AAC_INIT_VLC_STATIC(10, 462);
537
-
538
-    ff_aac_sbr_init();
539
-
540
-    dsputil_init(&ac->dsp, avccontext);
541
-
542
-    ac->random_state = 0x1f2e3d4c;
543
-
544
-    // -1024 - Compensate wrong IMDCT method.
545
-    // 32768 - Required to scale values to the correct range for the bias method
546
-    //         for float to int16 conversion.
547
-
548
-    if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
549
-        ac->add_bias  = 385.0f;
550
-        ac->sf_scale  = 1. / (-1024. * 32768.);
551
-        ac->sf_offset = 0;
552
-    } else {
553
-        ac->add_bias  = 0.0f;
554
-        ac->sf_scale  = 1. / -1024.;
555
-        ac->sf_offset = 60;
556
-    }
557
-
558
-#if !CONFIG_HARDCODED_TABLES
559
-    for (i = 0; i < 428; i++)
560
-        ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.);
561
-#endif /* CONFIG_HARDCODED_TABLES */
562
-
563
-    INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
564
-                    ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
565
-                    ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
566
-                    352);
567
-
568
-    ff_mdct_init(&ac->mdct, 11, 1, 1.0);
569
-    ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
570
-    // window initialization
571
-    ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
572
-    ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
573
-    ff_init_ff_sine_windows(10);
574
-    ff_init_ff_sine_windows( 7);
575
-
576
-    cbrt_tableinit();
577
-
578
-    return 0;
579
-}
580
-
581
-/**
582
- * Skip data_stream_element; reference: table 4.10.
583
- */
584
-static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
585
-{
586
-    int byte_align = get_bits1(gb);
587
-    int count = get_bits(gb, 8);
588
-    if (count == 255)
589
-        count += get_bits(gb, 8);
590
-    if (byte_align)
591
-        align_get_bits(gb);
592
-
593
-    if (get_bits_left(gb) < 8 * count) {
594
-        av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
595
-        return -1;
596
-    }
597
-    skip_bits_long(gb, 8 * count);
598
-    return 0;
599
-}
600
-
601
-static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
602
-                             GetBitContext *gb)
603
-{
604
-    int sfb;
605
-    if (get_bits1(gb)) {
606
-        ics->predictor_reset_group = get_bits(gb, 5);
607
-        if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
608
-            av_log(ac->avccontext, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
609
-            return -1;
610
-        }
611
-    }
612
-    for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
613
-        ics->prediction_used[sfb] = get_bits1(gb);
614
-    }
615
-    return 0;
616
-}
617
-
618
-/**
619
- * Decode Individual Channel Stream info; reference: table 4.6.
620
- *
621
- * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
622
- */
623
-static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
624
-                           GetBitContext *gb, int common_window)
625
-{
626
-    if (get_bits1(gb)) {
627
-        av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
628
-        memset(ics, 0, sizeof(IndividualChannelStream));
629
-        return -1;
630
-    }
631
-    ics->window_sequence[1] = ics->window_sequence[0];
632
-    ics->window_sequence[0] = get_bits(gb, 2);
633
-    ics->use_kb_window[1]   = ics->use_kb_window[0];
634
-    ics->use_kb_window[0]   = get_bits1(gb);
635
-    ics->num_window_groups  = 1;
636
-    ics->group_len[0]       = 1;
637
-    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
638
-        int i;
639
-        ics->max_sfb = get_bits(gb, 4);
640
-        for (i = 0; i < 7; i++) {
641
-            if (get_bits1(gb)) {
642
-                ics->group_len[ics->num_window_groups - 1]++;
643
-            } else {
644
-                ics->num_window_groups++;
645
-                ics->group_len[ics->num_window_groups - 1] = 1;
646
-            }
647
-        }
648
-        ics->num_windows       = 8;
649
-        ics->swb_offset        =    ff_swb_offset_128[ac->m4ac.sampling_index];
650
-        ics->num_swb           =   ff_aac_num_swb_128[ac->m4ac.sampling_index];
651
-        ics->tns_max_bands     = ff_tns_max_bands_128[ac->m4ac.sampling_index];
652
-        ics->predictor_present = 0;
653
-    } else {
654
-        ics->max_sfb               = get_bits(gb, 6);
655
-        ics->num_windows           = 1;
656
-        ics->swb_offset            =    ff_swb_offset_1024[ac->m4ac.sampling_index];
657
-        ics->num_swb               =   ff_aac_num_swb_1024[ac->m4ac.sampling_index];
658
-        ics->tns_max_bands         = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
659
-        ics->predictor_present     = get_bits1(gb);
660
-        ics->predictor_reset_group = 0;
661
-        if (ics->predictor_present) {
662
-            if (ac->m4ac.object_type == AOT_AAC_MAIN) {
663
-                if (decode_prediction(ac, ics, gb)) {
664
-                    memset(ics, 0, sizeof(IndividualChannelStream));
665
-                    return -1;
666
-                }
667
-            } else if (ac->m4ac.object_type == AOT_AAC_LC) {
668
-                av_log(ac->avccontext, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
669
-                memset(ics, 0, sizeof(IndividualChannelStream));
670
-                return -1;
671
-            } else {
672
-                av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
673
-                memset(ics, 0, sizeof(IndividualChannelStream));
674
-                return -1;
675
-            }
676
-        }
677
-    }
678
-
679
-    if (ics->max_sfb > ics->num_swb) {
680
-        av_log(ac->avccontext, AV_LOG_ERROR,
681
-               "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
682
-               ics->max_sfb, ics->num_swb);
683
-        memset(ics, 0, sizeof(IndividualChannelStream));
684
-        return -1;
685
-    }
686
-
687
-    return 0;
688
-}
689
-
690
-/**
691
- * Decode band types (section_data payload); reference: table 4.46.
692
- *
693
- * @param   band_type           array of the used band type
694
- * @param   band_type_run_end   array of the last scalefactor band of a band type run
695
- *
696
- * @return  Returns error status. 0 - OK, !0 - error
697
- */
698
-static int decode_band_types(AACContext *ac, enum BandType band_type[120],
699
-                             int band_type_run_end[120], GetBitContext *gb,
700
-                             IndividualChannelStream *ics)
701
-{
702
-    int g, idx = 0;
703
-    const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
704
-    for (g = 0; g < ics->num_window_groups; g++) {
705
-        int k = 0;
706
-        while (k < ics->max_sfb) {
707
-            uint8_t sect_end = k;
708
-            int sect_len_incr;
709
-            int sect_band_type = get_bits(gb, 4);
710
-            if (sect_band_type == 12) {
711
-                av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
712
-                return -1;
713
-            }
714
-            while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
715
-                sect_end += sect_len_incr;
716
-            sect_end += sect_len_incr;
717
-            if (get_bits_left(gb) < 0) {
718
-                av_log(ac->avccontext, AV_LOG_ERROR, overread_err);
719
-                return -1;
720
-            }
721
-            if (sect_end > ics->max_sfb) {
722
-                av_log(ac->avccontext, AV_LOG_ERROR,
723
-                       "Number of bands (%d) exceeds limit (%d).\n",
724
-                       sect_end, ics->max_sfb);
725
-                return -1;
726
-            }
727
-            for (; k < sect_end; k++) {
728
-                band_type        [idx]   = sect_band_type;
729
-                band_type_run_end[idx++] = sect_end;
730
-            }
731
-        }
732
-    }
733
-    return 0;
734
-}
735
-
736
-/**
737
- * Decode scalefactors; reference: table 4.47.
738
- *
739
- * @param   global_gain         first scalefactor value as scalefactors are differentially coded
740
- * @param   band_type           array of the used band type
741
- * @param   band_type_run_end   array of the last scalefactor band of a band type run
742
- * @param   sf                  array of scalefactors or intensity stereo positions
743
- *
744
- * @return  Returns error status. 0 - OK, !0 - error
745
- */
746
-static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
747
-                               unsigned int global_gain,
748
-                               IndividualChannelStream *ics,
749
-                               enum BandType band_type[120],
750
-                               int band_type_run_end[120])
751
-{
752
-    const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
753
-    int g, i, idx = 0;
754
-    int offset[3] = { global_gain, global_gain - 90, 100 };
755
-    int noise_flag = 1;
756
-    static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
757
-    for (g = 0; g < ics->num_window_groups; g++) {
758
-        for (i = 0; i < ics->max_sfb;) {
759
-            int run_end = band_type_run_end[idx];
760
-            if (band_type[idx] == ZERO_BT) {
761
-                for (; i < run_end; i++, idx++)
762
-                    sf[idx] = 0.;
763
-            } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
764
-                for (; i < run_end; i++, idx++) {
765
-                    offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
766
-                    if (offset[2] > 255U) {
767
-                        av_log(ac->avccontext, AV_LOG_ERROR,
768
-                               "%s (%d) out of range.\n", sf_str[2], offset[2]);
769
-                        return -1;
770
-                    }
771
-                    sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
772
-                }
773
-            } else if (band_type[idx] == NOISE_BT) {
774
-                for (; i < run_end; i++, idx++) {
775
-                    if (noise_flag-- > 0)
776
-                        offset[1] += get_bits(gb, 9) - 256;
777
-                    else
778
-                        offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
779
-                    if (offset[1] > 255U) {
780
-                        av_log(ac->avccontext, AV_LOG_ERROR,
781
-                               "%s (%d) out of range.\n", sf_str[1], offset[1]);
782
-                        return -1;
783
-                    }
784
-                    sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
785
-                }
786
-            } else {
787
-                for (; i < run_end; i++, idx++) {
788
-                    offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
789
-                    if (offset[0] > 255U) {
790
-                        av_log(ac->avccontext, AV_LOG_ERROR,
791
-                               "%s (%d) out of range.\n", sf_str[0], offset[0]);
792
-                        return -1;
793
-                    }
794
-                    sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
795
-                }
796
-            }
797
-        }
798
-    }
799
-    return 0;
800
-}
801
-
802
-/**
803
- * Decode pulse data; reference: table 4.7.
804
- */
805
-static int decode_pulses(Pulse *pulse, GetBitContext *gb,
806
-                         const uint16_t *swb_offset, int num_swb)
807
-{
808
-    int i, pulse_swb;
809
-    pulse->num_pulse = get_bits(gb, 2) + 1;
810
-    pulse_swb        = get_bits(gb, 6);
811
-    if (pulse_swb >= num_swb)
812
-        return -1;
813
-    pulse->pos[0]    = swb_offset[pulse_swb];
814
-    pulse->pos[0]   += get_bits(gb, 5);
815
-    if (pulse->pos[0] > 1023)
816
-        return -1;
817
-    pulse->amp[0]    = get_bits(gb, 4);
818
-    for (i = 1; i < pulse->num_pulse; i++) {
819
-        pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
820
-        if (pulse->pos[i] > 1023)
821
-            return -1;
822
-        pulse->amp[i] = get_bits(gb, 4);
823
-    }
824
-    return 0;
825
-}
826
-
827
-/**
828
- * Decode Temporal Noise Shaping data; reference: table 4.48.
829
- *
830
- * @return  Returns error status. 0 - OK, !0 - error
831
- */
832
-static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
833
-                      GetBitContext *gb, const IndividualChannelStream *ics)
834
-{
835
-    int w, filt, i, coef_len, coef_res, coef_compress;
836
-    const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
837
-    const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
838
-    for (w = 0; w < ics->num_windows; w++) {
839
-        if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
840
-            coef_res = get_bits1(gb);
841
-
842
-            for (filt = 0; filt < tns->n_filt[w]; filt++) {
843
-                int tmp2_idx;
844
-                tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
845
-
846
-                if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
847
-                    av_log(ac->avccontext, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
848
-                           tns->order[w][filt], tns_max_order);
849
-                    tns->order[w][filt] = 0;
850
-                    return -1;
851
-                }
852
-                if (tns->order[w][filt]) {
853
-                    tns->direction[w][filt] = get_bits1(gb);
854
-                    coef_compress = get_bits1(gb);
855
-                    coef_len = coef_res + 3 - coef_compress;
856
-                    tmp2_idx = 2 * coef_compress + coef_res;
857
-
858
-                    for (i = 0; i < tns->order[w][filt]; i++)
859
-                        tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
860
-                }
861
-            }
862
-        }
863
-    }
864
-    return 0;
865
-}
866
-
867
-/**
868
- * Decode Mid/Side data; reference: table 4.54.
869
- *
870
- * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
871
- *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
872
- *                      [3] reserved for scalable AAC
873
- */
874
-static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
875
-                                   int ms_present)
876
-{
877
-    int idx;
878
-    if (ms_present == 1) {
879
-        for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
880
-            cpe->ms_mask[idx] = get_bits1(gb);
881
-    } else if (ms_present == 2) {
882
-        memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
883
-    }
884
-}
885
-
886
-#ifndef VMUL2
887
-static inline float *VMUL2(float *dst, const float *v, unsigned idx,
888
-                           const float *scale)
889
-{
890
-    float s = *scale;
891
-    *dst++ = v[idx    & 15] * s;
892
-    *dst++ = v[idx>>4 & 15] * s;
893
-    return dst;
894
-}
895
-#endif
896
-
897
-#ifndef VMUL4
898
-static inline float *VMUL4(float *dst, const float *v, unsigned idx,
899
-                           const float *scale)
900
-{
901
-    float s = *scale;
902
-    *dst++ = v[idx    & 3] * s;
903
-    *dst++ = v[idx>>2 & 3] * s;
904
-    *dst++ = v[idx>>4 & 3] * s;
905
-    *dst++ = v[idx>>6 & 3] * s;
906
-    return dst;
907
-}
908
-#endif
909
-
910
-#ifndef VMUL2S
911
-static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
912
-                            unsigned sign, const float *scale)
913
-{
914
-    union float754 s0, s1;
915
-
916
-    s0.f = s1.f = *scale;
917
-    s0.i ^= sign >> 1 << 31;
918
-    s1.i ^= sign      << 31;
919
-
920
-    *dst++ = v[idx    & 15] * s0.f;
921
-    *dst++ = v[idx>>4 & 15] * s1.f;
922
-
923
-    return dst;
924
-}
925
-#endif
926
-
927
-#ifndef VMUL4S
928
-static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
929
-                            unsigned sign, const float *scale)
930
-{
931
-    unsigned nz = idx >> 12;
932
-    union float754 s = { .f = *scale };
933
-    union float754 t;
934
-
935
-    t.i = s.i ^ (sign & 1<<31);
936
-    *dst++ = v[idx    & 3] * t.f;
937
-
938
-    sign <<= nz & 1; nz >>= 1;
939
-    t.i = s.i ^ (sign & 1<<31);
940
-    *dst++ = v[idx>>2 & 3] * t.f;
941
-
942
-    sign <<= nz & 1; nz >>= 1;
943
-    t.i = s.i ^ (sign & 1<<31);
944
-    *dst++ = v[idx>>4 & 3] * t.f;
945
-
946
-    sign <<= nz & 1; nz >>= 1;
947
-    t.i = s.i ^ (sign & 1<<31);
948
-    *dst++ = v[idx>>6 & 3] * t.f;
949
-
950
-    return dst;
951
-}
952
-#endif
953
-
954
-/**
955
- * Decode spectral data; reference: table 4.50.
956
- * Dequantize and scale spectral data; reference: 4.6.3.3.
957
- *
958
- * @param   coef            array of dequantized, scaled spectral data
959
- * @param   sf              array of scalefactors or intensity stereo positions
960
- * @param   pulse_present   set if pulses are present
961
- * @param   pulse           pointer to pulse data struct
962
- * @param   band_type       array of the used band type
963
- *
964
- * @return  Returns error status. 0 - OK, !0 - error
965
- */
966
-static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
967
-                                       GetBitContext *gb, const float sf[120],
968
-                                       int pulse_present, const Pulse *pulse,
969
-                                       const IndividualChannelStream *ics,
970
-                                       enum BandType band_type[120])
971
-{
972
-    int i, k, g, idx = 0;
973
-    const int c = 1024 / ics->num_windows;
974
-    const uint16_t *offsets = ics->swb_offset;
975
-    float *coef_base = coef;
976
-    int err_idx;
977
-
978
-    for (g = 0; g < ics->num_windows; g++)
979
-        memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
980
-
981
-    for (g = 0; g < ics->num_window_groups; g++) {
982
-        unsigned g_len = ics->group_len[g];
983
-
984
-        for (i = 0; i < ics->max_sfb; i++, idx++) {
985
-            const unsigned cbt_m1 = band_type[idx] - 1;
986
-            float *cfo = coef + offsets[i];
987
-            int off_len = offsets[i + 1] - offsets[i];
988
-            int group;
989
-
990
-            if (cbt_m1 >= INTENSITY_BT2 - 1) {
991
-                for (group = 0; group < g_len; group++, cfo+=128) {
992
-                    memset(cfo, 0, off_len * sizeof(float));
993
-                }
994
-            } else if (cbt_m1 == NOISE_BT - 1) {
995
-                for (group = 0; group < g_len; group++, cfo+=128) {
996
-                    float scale;
997
-                    float band_energy;
998
-
999
-                    for (k = 0; k < off_len; k++) {
1000
-                        ac->random_state  = lcg_random(ac->random_state);
1001
-                        cfo[k] = ac->random_state;
1002
-                    }
1003
-
1004
-                    band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
1005
-                    scale = sf[idx] / sqrtf(band_energy);
1006
-                    ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1007
-                }
1008
-            } else {
1009
-                const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1010
-                const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1011
-                VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1012
-                const int cb_size = ff_aac_spectral_sizes[cbt_m1];
1013
-                OPEN_READER(re, gb);
1014
-
1015
-                switch (cbt_m1 >> 1) {
1016
-                case 0:
1017
-                    for (group = 0; group < g_len; group++, cfo+=128) {
1018
-                        float *cf = cfo;
1019
-                        int len = off_len;
1020
-
1021
-                        do {
1022
-                            int code;
1023
-                            unsigned cb_idx;
1024
-
1025
-                            UPDATE_CACHE(re, gb);
1026
-                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1027
-
1028
-                            if (code >= cb_size) {
1029
-                                err_idx = code;
1030
-                                goto err_cb_overflow;
1031
-                            }
1032
-
1033
-                            cb_idx = cb_vector_idx[code];
1034
-                            cf = VMUL4(cf, vq, cb_idx, sf + idx);
1035
-                        } while (len -= 4);
1036
-                    }
1037
-                    break;
1038
-
1039
-                case 1:
1040
-                    for (group = 0; group < g_len; group++, cfo+=128) {
1041
-                        float *cf = cfo;
1042
-                        int len = off_len;
1043
-
1044
-                        do {
1045
-                            int code;
1046
-                            unsigned nnz;
1047
-                            unsigned cb_idx;
1048
-                            uint32_t bits;
1049
-
1050
-                            UPDATE_CACHE(re, gb);
1051
-                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1052
-
1053
-                            if (code >= cb_size) {
1054
-                                err_idx = code;
1055
-                                goto err_cb_overflow;
1056
-                            }
1057
-
1058
-#if MIN_CACHE_BITS < 20
1059
-                            UPDATE_CACHE(re, gb);
1060
-#endif
1061
-                            cb_idx = cb_vector_idx[code];
1062
-                            nnz = cb_idx >> 8 & 15;
1063
-                            bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1064
-                            LAST_SKIP_BITS(re, gb, nnz);
1065
-                            cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1066
-                        } while (len -= 4);
1067
-                    }
1068
-                    break;
1069
-
1070
-                case 2:
1071
-                    for (group = 0; group < g_len; group++, cfo+=128) {
1072
-                        float *cf = cfo;
1073
-                        int len = off_len;
1074
-
1075
-                        do {
1076
-                            int code;
1077
-                            unsigned cb_idx;
1078
-
1079
-                            UPDATE_CACHE(re, gb);
1080
-                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1081
-
1082
-                            if (code >= cb_size) {
1083
-                                err_idx = code;
1084
-                                goto err_cb_overflow;
1085
-                            }
1086
-
1087
-                            cb_idx = cb_vector_idx[code];
1088
-                            cf = VMUL2(cf, vq, cb_idx, sf + idx);
1089
-                        } while (len -= 2);
1090
-                    }
1091
-                    break;
1092
-
1093
-                case 3:
1094
-                case 4:
1095
-                    for (group = 0; group < g_len; group++, cfo+=128) {
1096
-                        float *cf = cfo;
1097
-                        int len = off_len;
1098
-
1099
-                        do {
1100
-                            int code;
1101
-                            unsigned nnz;
1102
-                            unsigned cb_idx;
1103
-                            unsigned sign;
1104
-
1105
-                            UPDATE_CACHE(re, gb);
1106
-                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1107
-
1108
-                            if (code >= cb_size) {
1109
-                                err_idx = code;
1110
-                                goto err_cb_overflow;
1111
-                            }
1112
-
1113
-                            cb_idx = cb_vector_idx[code];
1114
-                            nnz = cb_idx >> 8 & 15;
1115
-                            sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
1116
-                            LAST_SKIP_BITS(re, gb, nnz);
1117
-                            cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1118
-                        } while (len -= 2);
1119
-                    }
1120
-                    break;
1121
-
1122
-                default:
1123
-                    for (group = 0; group < g_len; group++, cfo+=128) {
1124
-                        float *cf = cfo;
1125
-                        uint32_t *icf = (uint32_t *) cf;
1126
-                        int len = off_len;
1127
-
1128
-                        do {
1129
-                            int code;
1130
-                            unsigned nzt, nnz;
1131
-                            unsigned cb_idx;
1132
-                            uint32_t bits;
1133
-                            int j;
1134
-
1135
-                            UPDATE_CACHE(re, gb);
1136
-                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1137
-
1138
-                            if (!code) {
1139
-                                *icf++ = 0;
1140
-                                *icf++ = 0;
1141
-                                continue;
1142
-                            }
1143
-
1144
-                            if (code >= cb_size) {
1145
-                                err_idx = code;
1146
-                                goto err_cb_overflow;
1147
-                            }
1148
-
1149
-                            cb_idx = cb_vector_idx[code];
1150
-                            nnz = cb_idx >> 12;
1151
-                            nzt = cb_idx >> 8;
1152
-                            bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1153
-                            LAST_SKIP_BITS(re, gb, nnz);
1154
-
1155
-                            for (j = 0; j < 2; j++) {
1156
-                                if (nzt & 1<<j) {
1157
-                                    uint32_t b;
1158
-                                    int n;
1159
-                                    /* The total length of escape_sequence must be < 22 bits according
1160
-                                       to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1161
-                                    UPDATE_CACHE(re, gb);
1162
-                                    b = GET_CACHE(re, gb);
1163
-                                    b = 31 - av_log2(~b);
1164
-
1165
-                                    if (b > 8) {
1166
-                                        av_log(ac->avccontext, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1167
-                                        return -1;
1168
-                                    }
1169
-
1170
-#if MIN_CACHE_BITS < 21
1171
-                                    LAST_SKIP_BITS(re, gb, b + 1);
1172
-                                    UPDATE_CACHE(re, gb);
1173
-#else
1174
-                                    SKIP_BITS(re, gb, b + 1);
1175
-#endif
1176
-                                    b += 4;
1177
-                                    n = (1 << b) + SHOW_UBITS(re, gb, b);
1178
-                                    LAST_SKIP_BITS(re, gb, b);
1179
-                                    *icf++ = cbrt_tab[n] | (bits & 1<<31);
1180
-                                    bits <<= 1;
1181
-                                } else {
1182
-                                    unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1183
-                                    *icf++ = (bits & 1<<31) | v;
1184
-                                    bits <<= !!v;
1185
-                                }
1186
-                                cb_idx >>= 4;
1187
-                            }
1188
-                        } while (len -= 2);
1189
-
1190
-                        ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1191
-                    }
1192
-                }
1193
-
1194
-                CLOSE_READER(re, gb);
1195
-            }
1196
-        }
1197
-        coef += g_len << 7;
1198
-    }
1199
-
1200
-    if (pulse_present) {
1201
-        idx = 0;
1202
-        for (i = 0; i < pulse->num_pulse; i++) {
1203
-            float co = coef_base[ pulse->pos[i] ];
1204
-            while (offsets[idx + 1] <= pulse->pos[i])
1205
-                idx++;
1206
-            if (band_type[idx] != NOISE_BT && sf[idx]) {
1207
-                float ico = -pulse->amp[i];
1208
-                if (co) {
1209
-                    co /= sf[idx];
1210
-                    ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1211
-                }
1212
-                coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1213
-            }
1214
-        }
1215
-    }
1216
-    return 0;
1217
-
1218
-err_cb_overflow:
1219
-    av_log(ac->avccontext, AV_LOG_ERROR,
1220
-           "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
1221
-           band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]);
1222
-    return -1;
1223
-}
1224
-
1225
-static av_always_inline float flt16_round(float pf)
1226
-{
1227
-    union float754 tmp;
1228
-    tmp.f = pf;
1229
-    tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1230
-    return tmp.f;
1231
-}
1232
-
1233
-static av_always_inline float flt16_even(float pf)
1234
-{
1235
-    union float754 tmp;
1236
-    tmp.f = pf;
1237
-    tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1238
-    return tmp.f;
1239
-}
1240
-
1241
-static av_always_inline float flt16_trunc(float pf)
1242
-{
1243
-    union float754 pun;
1244
-    pun.f = pf;
1245
-    pun.i &= 0xFFFF0000U;
1246
-    return pun.f;
1247
-}
1248
-
1249
-static av_always_inline void predict(AACContext *ac, PredictorState *ps, float *coef,
1250
-                    int output_enable)
1251
-{
1252
-    const float a     = 0.953125; // 61.0 / 64
1253
-    const float alpha = 0.90625;  // 29.0 / 32
1254
-    float e0, e1;
1255
-    float pv;
1256
-    float k1, k2;
1257
-
1258
-    k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
1259
-    k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
1260
-
1261
-    pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
1262
-    if (output_enable)
1263
-        *coef += pv * ac->sf_scale;
1264
-
1265
-    e0 = *coef / ac->sf_scale;
1266
-    e1 = e0 - k1 * ps->r0;
1267
-
1268
-    ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
1269
-    ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
1270
-    ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
1271
-    ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
1272
-
1273
-    ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
1274
-    ps->r0 = flt16_trunc(a * e0);
1275
-}
1276
-
1277
-/**
1278
- * Apply AAC-Main style frequency domain prediction.
1279
- */
1280
-static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1281
-{
1282
-    int sfb, k;
1283
-
1284
-    if (!sce->ics.predictor_initialized) {
1285
-        reset_all_predictors(sce->predictor_state);
1286
-        sce->ics.predictor_initialized = 1;
1287
-    }
1288
-
1289
-    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1290
-        for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
1291
-            for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
1292
-                predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
1293
-                        sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
1294
-            }
1295
-        }
1296
-        if (sce->ics.predictor_reset_group)
1297
-            reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
1298
-    } else
1299
-        reset_all_predictors(sce->predictor_state);
1300
-}
1301
-
1302
-/**
1303
- * Decode an individual_channel_stream payload; reference: table 4.44.
1304
- *
1305
- * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
1306
- * @param   scale_flag      scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1307
- *
1308
- * @return  Returns error status. 0 - OK, !0 - error
1309
- */
1310
-static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1311
-                      GetBitContext *gb, int common_window, int scale_flag)
1312
-{
1313
-    Pulse pulse;
1314
-    TemporalNoiseShaping    *tns = &sce->tns;
1315
-    IndividualChannelStream *ics = &sce->ics;
1316
-    float *out = sce->coeffs;
1317
-    int global_gain, pulse_present = 0;
1318
-
1319
-    /* This assignment is to silence a GCC warning about the variable being used
1320
-     * uninitialized when in fact it always is.
1321
-     */
1322
-    pulse.num_pulse = 0;
1323
-
1324
-    global_gain = get_bits(gb, 8);
1325
-
1326
-    if (!common_window && !scale_flag) {
1327
-        if (decode_ics_info(ac, ics, gb, 0) < 0)
1328
-            return -1;
1329
-    }
1330
-
1331
-    if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
1332
-        return -1;
1333
-    if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
1334
-        return -1;
1335
-
1336
-    pulse_present = 0;
1337
-    if (!scale_flag) {
1338
-        if ((pulse_present = get_bits1(gb))) {
1339
-            if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1340
-                av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
1341
-                return -1;
1342
-            }
1343
-            if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1344
-                av_log(ac->avccontext, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
1345
-                return -1;
1346
-            }
1347
-        }
1348
-        if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1349
-            return -1;
1350
-        if (get_bits1(gb)) {
1351
-            av_log_missing_feature(ac->avccontext, "SSR", 1);
1352
-            return -1;
1353
-        }
1354
-    }
1355
-
1356
-    if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
1357
-        return -1;
1358
-
1359
-    if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
1360
-        apply_prediction(ac, sce);
1361
-
1362
-    return 0;
1363
-}
1364
-
1365
-/**
1366
- * Mid/Side stereo decoding; reference: 4.6.8.1.3.
1367
- */
1368
-static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
1369
-{
1370
-    const IndividualChannelStream *ics = &cpe->ch[0].ics;
1371
-    float *ch0 = cpe->ch[0].coeffs;
1372
-    float *ch1 = cpe->ch[1].coeffs;
1373
-    int g, i, group, idx = 0;
1374
-    const uint16_t *offsets = ics->swb_offset;
1375
-    for (g = 0; g < ics->num_window_groups; g++) {
1376
-        for (i = 0; i < ics->max_sfb; i++, idx++) {
1377
-            if (cpe->ms_mask[idx] &&
1378
-                    cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
1379
-                for (group = 0; group < ics->group_len[g]; group++) {
1380
-                    ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
1381
-                                              ch1 + group * 128 + offsets[i],
1382
-                                              offsets[i+1] - offsets[i]);
1383
-                }
1384
-            }
1385
-        }
1386
-        ch0 += ics->group_len[g] * 128;
1387
-        ch1 += ics->group_len[g] * 128;
1388
-    }
1389
-}
1390
-
1391
-/**
1392
- * intensity stereo decoding; reference: 4.6.8.2.3
1393
- *
1394
- * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
1395
- *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
1396
- *                      [3] reserved for scalable AAC
1397
- */
1398
-static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
1399
-{
1400
-    const IndividualChannelStream *ics = &cpe->ch[1].ics;
1401
-    SingleChannelElement         *sce1 = &cpe->ch[1];
1402
-    float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1403
-    const uint16_t *offsets = ics->swb_offset;
1404
-    int g, group, i, k, idx = 0;
1405
-    int c;
1406
-    float scale;
1407
-    for (g = 0; g < ics->num_window_groups; g++) {
1408
-        for (i = 0; i < ics->max_sfb;) {
1409
-            if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
1410
-                const int bt_run_end = sce1->band_type_run_end[idx];
1411
-                for (; i < bt_run_end; i++, idx++) {
1412
-                    c = -1 + 2 * (sce1->band_type[idx] - 14);
1413
-                    if (ms_present)
1414
-                        c *= 1 - 2 * cpe->ms_mask[idx];
1415
-                    scale = c * sce1->sf[idx];
1416
-                    for (group = 0; group < ics->group_len[g]; group++)
1417
-                        for (k = offsets[i]; k < offsets[i + 1]; k++)
1418
-                            coef1[group * 128 + k] = scale * coef0[group * 128 + k];
1419
-                }
1420
-            } else {
1421
-                int bt_run_end = sce1->band_type_run_end[idx];
1422
-                idx += bt_run_end - i;
1423
-                i    = bt_run_end;
1424
-            }
1425
-        }
1426
-        coef0 += ics->group_len[g] * 128;
1427
-        coef1 += ics->group_len[g] * 128;
1428
-    }
1429
-}
1430
-
1431
-/**
1432
- * Decode a channel_pair_element; reference: table 4.4.
1433
- *
1434
- * @param   elem_id Identifies the instance of a syntax element.
1435
- *
1436
- * @return  Returns error status. 0 - OK, !0 - error
1437
- */
1438
-static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
1439
-{
1440
-    int i, ret, common_window, ms_present = 0;
1441
-
1442
-    common_window = get_bits1(gb);
1443
-    if (common_window) {
1444
-        if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
1445
-            return -1;
1446
-        i = cpe->ch[1].ics.use_kb_window[0];
1447
-        cpe->ch[1].ics = cpe->ch[0].ics;
1448
-        cpe->ch[1].ics.use_kb_window[1] = i;
1449
-        ms_present = get_bits(gb, 2);
1450
-        if (ms_present == 3) {
1451
-            av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1452
-            return -1;
1453
-        } else if (ms_present)
1454
-            decode_mid_side_stereo(cpe, gb, ms_present);
1455
-    }
1456
-    if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1457
-        return ret;
1458
-    if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1459
-        return ret;
1460
-
1461
-    if (common_window) {
1462
-        if (ms_present)
1463
-            apply_mid_side_stereo(ac, cpe);
1464
-        if (ac->m4ac.object_type == AOT_AAC_MAIN) {
1465
-            apply_prediction(ac, &cpe->ch[0]);
1466
-            apply_prediction(ac, &cpe->ch[1]);
1467
-        }
1468
-    }
1469
-
1470
-    apply_intensity_stereo(cpe, ms_present);
1471
-    return 0;
1472
-}
1473
-
1474
-/**
1475
- * Decode coupling_channel_element; reference: table 4.8.
1476
- *
1477
- * @param   elem_id Identifies the instance of a syntax element.
1478
- *
1479
- * @return  Returns error status. 0 - OK, !0 - error
1480
- */
1481
-static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
1482
-{
1483
-    int num_gain = 0;
1484
-    int c, g, sfb, ret;
1485
-    int sign;
1486
-    float scale;
1487
-    SingleChannelElement *sce = &che->ch[0];
1488
-    ChannelCoupling     *coup = &che->coup;
1489
-
1490
-    coup->coupling_point = 2 * get_bits1(gb);
1491
-    coup->num_coupled = get_bits(gb, 3);
1492
-    for (c = 0; c <= coup->num_coupled; c++) {
1493
-        num_gain++;
1494
-        coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1495
-        coup->id_select[c] = get_bits(gb, 4);
1496
-        if (coup->type[c] == TYPE_CPE) {
1497
-            coup->ch_select[c] = get_bits(gb, 2);
1498
-            if (coup->ch_select[c] == 3)
1499
-                num_gain++;
1500
-        } else
1501
-            coup->ch_select[c] = 2;
1502
-    }
1503
-    coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1504
-
1505
-    sign  = get_bits(gb, 1);
1506
-    scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
1507
-
1508
-    if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1509
-        return ret;
1510
-
1511
-    for (c = 0; c < num_gain; c++) {
1512
-        int idx  = 0;
1513
-        int cge  = 1;
1514
-        int gain = 0;
1515
-        float gain_cache = 1.;
1516
-        if (c) {
1517
-            cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1518
-            gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1519
-            gain_cache = pow(scale, -gain);
1520
-        }
1521
-        if (coup->coupling_point == AFTER_IMDCT) {
1522
-            coup->gain[c][0] = gain_cache;
1523
-        } else {
1524
-            for (g = 0; g < sce->ics.num_window_groups; g++) {
1525
-                for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1526
-                    if (sce->band_type[idx] != ZERO_BT) {
1527
-                        if (!cge) {
1528
-                            int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1529
-                            if (t) {
1530
-                                int s = 1;
1531
-                                t = gain += t;
1532
-                                if (sign) {
1533
-                                    s  -= 2 * (t & 0x1);
1534
-                                    t >>= 1;
1535
-                                }
1536
-                                gain_cache = pow(scale, -t) * s;
1537
-                            }
1538
-                        }
1539
-                        coup->gain[c][idx] = gain_cache;
1540
-                    }
1541
-                }
1542
-            }
1543
-        }
1544
-    }
1545
-    return 0;
1546
-}
1547
-
1548
-/**
1549
- * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1550
- *
1551
- * @return  Returns number of bytes consumed.
1552
- */
1553
-static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1554
-                                         GetBitContext *gb)
1555
-{
1556
-    int i;
1557
-    int num_excl_chan = 0;
1558
-
1559
-    do {
1560
-        for (i = 0; i < 7; i++)
1561
-            che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1562
-    } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1563
-
1564
-    return num_excl_chan / 7;
1565
-}
1566
-
1567
-/**
1568
- * Decode dynamic range information; reference: table 4.52.
1569
- *
1570
- * @param   cnt length of TYPE_FIL syntactic element in bytes
1571
- *
1572
- * @return  Returns number of bytes consumed.
1573
- */
1574
-static int decode_dynamic_range(DynamicRangeControl *che_drc,
1575
-                                GetBitContext *gb, int cnt)
1576
-{
1577
-    int n             = 1;
1578
-    int drc_num_bands = 1;
1579
-    int i;
1580
-
1581
-    /* pce_tag_present? */
1582
-    if (get_bits1(gb)) {
1583
-        che_drc->pce_instance_tag  = get_bits(gb, 4);
1584
-        skip_bits(gb, 4); // tag_reserved_bits
1585
-        n++;
1586
-    }
1587
-
1588
-    /* excluded_chns_present? */
1589
-    if (get_bits1(gb)) {
1590
-        n += decode_drc_channel_exclusions(che_drc, gb);
1591
-    }
1592
-
1593
-    /* drc_bands_present? */
1594
-    if (get_bits1(gb)) {
1595
-        che_drc->band_incr            = get_bits(gb, 4);
1596
-        che_drc->interpolation_scheme = get_bits(gb, 4);
1597
-        n++;
1598
-        drc_num_bands += che_drc->band_incr;
1599
-        for (i = 0; i < drc_num_bands; i++) {
1600
-            che_drc->band_top[i] = get_bits(gb, 8);
1601
-            n++;
1602
-        }
1603
-    }
1604
-
1605
-    /* prog_ref_level_present? */
1606
-    if (get_bits1(gb)) {
1607
-        che_drc->prog_ref_level = get_bits(gb, 7);
1608
-        skip_bits1(gb); // prog_ref_level_reserved_bits
1609
-        n++;
1610
-    }
1611
-
1612
-    for (i = 0; i < drc_num_bands; i++) {
1613
-        che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1614
-        che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1615
-        n++;
1616
-    }
1617
-
1618
-    return n;
1619
-}
1620
-
1621
-/**
1622
- * Decode extension data (incomplete); reference: table 4.51.
1623
- *
1624
- * @param   cnt length of TYPE_FIL syntactic element in bytes
1625
- *
1626
- * @return Returns number of bytes consumed
1627
- */
1628
-static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
1629
-                                    ChannelElement *che, enum RawDataBlockType elem_type)
1630
-{
1631
-    int crc_flag = 0;
1632
-    int res = cnt;
1633
-    switch (get_bits(gb, 4)) { // extension type
1634
-    case EXT_SBR_DATA_CRC:
1635
-        crc_flag++;
1636
-    case EXT_SBR_DATA:
1637
-        if (!che) {
1638
-            av_log(ac->avccontext, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1639
-            return res;
1640
-        } else if (!ac->m4ac.sbr) {
1641
-            av_log(ac->avccontext, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1642
-            skip_bits_long(gb, 8 * cnt - 4);
1643
-            return res;
1644
-        } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
1645
-            av_log(ac->avccontext, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1646
-            skip_bits_long(gb, 8 * cnt - 4);
1647
-            return res;
1648
-        } else {
1649
-            ac->m4ac.sbr = 1;
1650
-        }
1651
-        res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
1652
-        break;
1653
-    case EXT_DYNAMIC_RANGE:
1654
-        res = decode_dynamic_range(&ac->che_drc, gb, cnt);
1655
-        break;
1656
-    case EXT_FILL:
1657
-    case EXT_FILL_DATA:
1658
-    case EXT_DATA_ELEMENT:
1659
-    default:
1660
-        skip_bits_long(gb, 8 * cnt - 4);
1661
-        break;
1662
-    };
1663
-    return res;
1664
-}
1665
-
1666
-/**
1667
- * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
1668
- *
1669
- * @param   decode  1 if tool is used normally, 0 if tool is used in LTP.
1670
- * @param   coef    spectral coefficients
1671
- */
1672
-static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
1673
-                      IndividualChannelStream *ics, int decode)
1674
-{
1675
-    const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
1676
-    int w, filt, m, i;
1677
-    int bottom, top, order, start, end, size, inc;
1678
-    float lpc[TNS_MAX_ORDER];
1679
-
1680
-    for (w = 0; w < ics->num_windows; w++) {
1681
-        bottom = ics->num_swb;
1682
-        for (filt = 0; filt < tns->n_filt[w]; filt++) {
1683
-            top    = bottom;
1684
-            bottom = FFMAX(0, top - tns->length[w][filt]);
1685
-            order  = tns->order[w][filt];
1686
-            if (order == 0)
1687
-                continue;
1688
-
1689
-            // tns_decode_coef
1690
-            compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
1691
-
1692
-            start = ics->swb_offset[FFMIN(bottom, mmm)];
1693
-            end   = ics->swb_offset[FFMIN(   top, mmm)];
1694
-            if ((size = end - start) <= 0)
1695
-                continue;
1696
-            if (tns->direction[w][filt]) {
1697
-                inc = -1;
1698
-                start = end - 1;
1699
-            } else {
1700
-                inc = 1;
1701
-            }
1702
-            start += w * 128;
1703
-
1704
-            // ar filter
1705
-            for (m = 0; m < size; m++, start += inc)
1706
-                for (i = 1; i <= FFMIN(m, order); i++)
1707
-                    coef[start] -= coef[start - i * inc] * lpc[i - 1];
1708
-        }
1709
-    }
1710
-}
1711
-
1712
-/**
1713
- * Conduct IMDCT and windowing.
1714
- */
1715
-static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
1716
-{
1717
-    IndividualChannelStream *ics = &sce->ics;
1718
-    float *in    = sce->coeffs;
1719
-    float *out   = sce->ret;
1720
-    float *saved = sce->saved;
1721
-    const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1722
-    const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1723
-    const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
1724
-    float *buf  = ac->buf_mdct;
1725
-    float *temp = ac->temp;
1726
-    int i;
1727
-
1728
-    // imdct
1729
-    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1730
-        if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
1731
-            av_log(ac->avccontext, AV_LOG_WARNING,
1732
-                   "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
1733
-                   "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
1734
-        for (i = 0; i < 1024; i += 128)
1735
-            ff_imdct_half(&ac->mdct_small, buf + i, in + i);
1736
-    } else
1737
-        ff_imdct_half(&ac->mdct, buf, in);
1738
-
1739
-    /* window overlapping
1740
-     * NOTE: To simplify the overlapping code, all 'meaningless' short to long
1741
-     * and long to short transitions are considered to be short to short
1742
-     * transitions. This leaves just two cases (long to long and short to short)
1743
-     * with a little special sauce for EIGHT_SHORT_SEQUENCE.
1744
-     */
1745
-    if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
1746
-            (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
1747
-        ac->dsp.vector_fmul_window(    out,               saved,            buf,         lwindow_prev, bias, 512);
1748
-    } else {
1749
-        for (i = 0; i < 448; i++)
1750
-            out[i] = saved[i] + bias;
1751
-
1752
-        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1753
-            ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, bias, 64);
1754
-            ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      bias, 64);
1755
-            ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      bias, 64);
1756
-            ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      bias, 64);
1757
-            ac->dsp.vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      bias, 64);
1758
-            memcpy(                    out + 448 + 4*128, temp, 64 * sizeof(float));
1759
-        } else {
1760
-            ac->dsp.vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, bias, 64);
1761
-            for (i = 576; i < 1024; i++)
1762
-                out[i] = buf[i-512] + bias;
1763
-        }
1764
-    }
1765
-
1766
-    // buffer update
1767
-    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1768
-        for (i = 0; i < 64; i++)
1769
-            saved[i] = temp[64 + i] - bias;
1770
-        ac->dsp.vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
1771
-        ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
1772
-        ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
1773
-        memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
1774
-    } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
1775
-        memcpy(                    saved,       buf + 512,        448 * sizeof(float));
1776
-        memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
1777
-    } else { // LONG_STOP or ONLY_LONG
1778
-        memcpy(                    saved,       buf + 512,        512 * sizeof(float));
1779
-    }
1780
-}
1781
-
1782
-/**
1783
- * Apply dependent channel coupling (applied before IMDCT).
1784
- *
1785
- * @param   index   index into coupling gain array
1786
- */
1787
-static void apply_dependent_coupling(AACContext *ac,
1788
-                                     SingleChannelElement *target,
1789
-                                     ChannelElement *cce, int index)
1790
-{
1791
-    IndividualChannelStream *ics = &cce->ch[0].ics;
1792
-    const uint16_t *offsets = ics->swb_offset;
1793
-    float *dest = target->coeffs;
1794
-    const float *src = cce->ch[0].coeffs;
1795
-    int g, i, group, k, idx = 0;
1796
-    if (ac->m4ac.object_type == AOT_AAC_LTP) {
1797
-        av_log(ac->avccontext, AV_LOG_ERROR,
1798
-               "Dependent coupling is not supported together with LTP\n");
1799
-        return;
1800
-    }
1801
-    for (g = 0; g < ics->num_window_groups; g++) {
1802
-        for (i = 0; i < ics->max_sfb; i++, idx++) {
1803
-            if (cce->ch[0].band_type[idx] != ZERO_BT) {
1804
-                const float gain = cce->coup.gain[index][idx];
1805
-                for (group = 0; group < ics->group_len[g]; group++) {
1806
-                    for (k = offsets[i]; k < offsets[i + 1]; k++) {
1807
-                        // XXX dsputil-ize
1808
-                        dest[group * 128 + k] += gain * src[group * 128 + k];
1809
-                    }
1810
-                }
1811
-            }
1812
-        }
1813
-        dest += ics->group_len[g] * 128;
1814
-        src  += ics->group_len[g] * 128;
1815
-    }
1816
-}
1817
-
1818
-/**
1819
- * Apply independent channel coupling (applied after IMDCT).
1820
- *
1821
- * @param   index   index into coupling gain array
1822
- */
1823
-static void apply_independent_coupling(AACContext *ac,
1824
-                                       SingleChannelElement *target,
1825
-                                       ChannelElement *cce, int index)
1826
-{
1827
-    int i;
1828
-    const float gain = cce->coup.gain[index][0];
1829
-    const float bias = ac->add_bias;
1830
-    const float *src = cce->ch[0].ret;
1831
-    float *dest = target->ret;
1832
-    const int len = 1024 << (ac->m4ac.sbr == 1);
1833
-
1834
-    for (i = 0; i < len; i++)
1835
-        dest[i] += gain * (src[i] - bias);
1836
-}
1837
-
1838
-/**
1839
- * channel coupling transformation interface
1840
- *
1841
- * @param   index   index into coupling gain array
1842
- * @param   apply_coupling_method   pointer to (in)dependent coupling function
1843
- */
1844
-static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
1845
-                                   enum RawDataBlockType type, int elem_id,
1846
-                                   enum CouplingPoint coupling_point,
1847
-                                   void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
1848
-{
1849
-    int i, c;
1850
-
1851
-    for (i = 0; i < MAX_ELEM_ID; i++) {
1852
-        ChannelElement *cce = ac->che[TYPE_CCE][i];
1853
-        int index = 0;
1854
-
1855
-        if (cce && cce->coup.coupling_point == coupling_point) {
1856
-            ChannelCoupling *coup = &cce->coup;
1857
-
1858
-            for (c = 0; c <= coup->num_coupled; c++) {
1859
-                if (coup->type[c] == type && coup->id_select[c] == elem_id) {
1860
-                    if (coup->ch_select[c] != 1) {
1861
-                        apply_coupling_method(ac, &cc->ch[0], cce, index);
1862
-                        if (coup->ch_select[c] != 0)
1863
-                            index++;
1864
-                    }
1865
-                    if (coup->ch_select[c] != 2)
1866
-                        apply_coupling_method(ac, &cc->ch[1], cce, index++);
1867
-                } else
1868
-                    index += 1 + (coup->ch_select[c] == 3);
1869
-            }
1870
-        }
1871
-    }
1872
-}
1873
-
1874
-/**
1875
- * Convert spectral data to float samples, applying all supported tools as appropriate.
1876
- */
1877
-static void spectral_to_sample(AACContext *ac)
1878
-{
1879
-    int i, type;
1880
-    float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
1881
-    for (type = 3; type >= 0; type--) {
1882
-        for (i = 0; i < MAX_ELEM_ID; i++) {
1883
-            ChannelElement *che = ac->che[type][i];
1884
-            if (che) {
1885
-                if (type <= TYPE_CPE)
1886
-                    apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
1887
-                if (che->ch[0].tns.present)
1888
-                    apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
1889
-                if (che->ch[1].tns.present)
1890
-                    apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
1891
-                if (type <= TYPE_CPE)
1892
-                    apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
1893
-                if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
1894
-                    imdct_and_windowing(ac, &che->ch[0], imdct_bias);
1895
-                    if (type == TYPE_CPE) {
1896
-                        imdct_and_windowing(ac, &che->ch[1], imdct_bias);
1897
-                    }
1898
-                    if (ac->m4ac.sbr > 0) {
1899
-                        ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
1900
-                    }
1901
-                }
1902
-                if (type <= TYPE_CCE)
1903
-                    apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
1904
-            }
1905
-        }
1906
-    }
1907
-}
1908
-
1909
-static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
1910
-{
1911
-    int size;
1912
-    AACADTSHeaderInfo hdr_info;
1913
-
1914
-    size = ff_aac_parse_header(gb, &hdr_info);
1915
-    if (size > 0) {
1916
-        if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
1917
-            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
1918
-            memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
1919
-            ac->m4ac.chan_config = hdr_info.chan_config;
1920
-            if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
1921
-                return -7;
1922
-            if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
1923
-                return -7;
1924
-        } else if (ac->output_configured != OC_LOCKED) {
1925
-            ac->output_configured = OC_NONE;
1926
-        }
1927
-        if (ac->output_configured != OC_LOCKED)
1928
-            ac->m4ac.sbr = -1;
1929
-        ac->m4ac.sample_rate     = hdr_info.sample_rate;
1930
-        ac->m4ac.sampling_index  = hdr_info.sampling_index;
1931
-        ac->m4ac.object_type     = hdr_info.object_type;
1932
-        if (!ac->avccontext->sample_rate)
1933
-            ac->avccontext->sample_rate = hdr_info.sample_rate;
1934
-        if (hdr_info.num_aac_frames == 1) {
1935
-            if (!hdr_info.crc_absent)
1936
-                skip_bits(gb, 16);
1937
-        } else {
1938
-            av_log_missing_feature(ac->avccontext, "More than one AAC RDB per ADTS frame is", 0);
1939
-            return -1;
1940
-        }
1941
-    }
1942
-    return size;
1943
-}
1944
-
1945
-static int aac_decode_frame(AVCodecContext *avccontext, void *data,
1946
-                            int *data_size, AVPacket *avpkt)
1947
-{
1948
-    const uint8_t *buf = avpkt->data;
1949
-    int buf_size = avpkt->size;
1950
-    AACContext *ac = avccontext->priv_data;
1951
-    ChannelElement *che = NULL, *che_prev = NULL;
1952
-    GetBitContext gb;
1953
-    enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
1954
-    int err, elem_id, data_size_tmp;
1955
-    int buf_consumed;
1956
-    int samples = 1024, multiplier;
1957
-    int buf_offset;
1958
-
1959
-    init_get_bits(&gb, buf, buf_size * 8);
1960
-
1961
-    if (show_bits(&gb, 12) == 0xfff) {
1962
-        if (parse_adts_frame_header(ac, &gb) < 0) {
1963
-            av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
1964
-            return -1;
1965
-        }
1966
-        if (ac->m4ac.sampling_index > 12) {
1967
-            av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
1968
-            return -1;
1969
-        }
1970
-    }
1971
-
1972
-    // parse
1973
-    while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
1974
-        elem_id = get_bits(&gb, 4);
1975
-
1976
-        if (elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {
1977
-            av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
1978
-            return -1;
1979
-        }
1980
-
1981
-        switch (elem_type) {
1982
-
1983
-        case TYPE_SCE:
1984
-            err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
1985
-            break;
1986
-
1987
-        case TYPE_CPE:
1988
-            err = decode_cpe(ac, &gb, che);
1989
-            break;
1990
-
1991
-        case TYPE_CCE:
1992
-            err = decode_cce(ac, &gb, che);
1993
-            break;
1994
-
1995
-        case TYPE_LFE:
1996
-            err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
1997
-            break;
1998
-
1999
-        case TYPE_DSE:
2000
-            err = skip_data_stream_element(ac, &gb);
2001
-            break;
2002
-
2003
-        case TYPE_PCE: {
2004
-            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
2005
-            memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
2006
-            if ((err = decode_pce(ac, new_che_pos, &gb)))
2007
-                break;
2008
-            if (ac->output_configured > OC_TRIAL_PCE)
2009
-                av_log(avccontext, AV_LOG_ERROR,
2010
-                       "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2011
-            else
2012
-                err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
2013
-            break;
2014
-        }
2015
-
2016
-        case TYPE_FIL:
2017
-            if (elem_id == 15)
2018
-                elem_id += get_bits(&gb, 8) - 1;
2019
-            if (get_bits_left(&gb) < 8 * elem_id) {
2020
-                    av_log(avccontext, AV_LOG_ERROR, overread_err);
2021
-                    return -1;
2022
-            }
2023
-            while (elem_id > 0)
2024
-                elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
2025
-            err = 0; /* FIXME */
2026
-            break;
2027
-
2028
-        default:
2029
-            err = -1; /* should not happen, but keeps compiler happy */
2030
-            break;
2031
-        }
2032
-
2033
-        che_prev       = che;
2034
-        elem_type_prev = elem_type;
2035
-
2036
-        if (err)
2037
-            return err;
2038
-
2039
-        if (get_bits_left(&gb) < 3) {
2040
-            av_log(avccontext, AV_LOG_ERROR, overread_err);
2041
-            return -1;
2042
-        }
2043
-    }
2044
-
2045
-    spectral_to_sample(ac);
2046
-
2047
-    multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
2048
-    samples <<= multiplier;
2049
-    if (ac->output_configured < OC_LOCKED) {
2050
-        avccontext->sample_rate = ac->m4ac.sample_rate << multiplier;
2051
-        avccontext->frame_size = samples;
2052
-    }
2053
-
2054
-    data_size_tmp = samples * avccontext->channels * sizeof(int16_t);
2055
-    if (*data_size < data_size_tmp) {
2056
-        av_log(avccontext, AV_LOG_ERROR,
2057
-               "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
2058
-               *data_size, data_size_tmp);
2059
-        return -1;
2060
-    }
2061
-    *data_size = data_size_tmp;
2062
-
2063
-    ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avccontext->channels);
2064
-
2065
-    if (ac->output_configured)
2066
-        ac->output_configured = OC_LOCKED;
2067
-
2068
-    buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2069
-    for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2070
-        if (buf[buf_offset])
2071
-            break;
2072
-
2073
-    return buf_size > buf_offset ? buf_consumed : buf_size;
2074
-}
2075
-
2076
-static av_cold int aac_decode_close(AVCodecContext *avccontext)
2077
-{
2078
-    AACContext *ac = avccontext->priv_data;
2079
-    int i, type;
2080
-
2081
-    for (i = 0; i < MAX_ELEM_ID; i++) {
2082
-        for (type = 0; type < 4; type++) {
2083
-            if (ac->che[type][i])
2084
-                ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2085
-            av_freep(&ac->che[type][i]);
2086
-        }
2087
-    }
2088
-
2089
-    ff_mdct_end(&ac->mdct);
2090
-    ff_mdct_end(&ac->mdct_small);
2091
-    return 0;
2092
-}
2093
-
2094
-AVCodec aac_decoder = {
2095
-    "aac",
2096
-    AVMEDIA_TYPE_AUDIO,
2097
-    CODEC_ID_AAC,
2098
-    sizeof(AACContext),
2099
-    aac_decode_init,
2100
-    NULL,
2101
-    aac_decode_close,
2102
-    aac_decode_frame,
2103
-    .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
2104
-    .sample_fmts = (const enum SampleFormat[]) {
2105
-        SAMPLE_FMT_S16,SAMPLE_FMT_NONE
2106
-    },
2107
-    .channel_layouts = aac_channel_layout,
2108
-};
... ...
@@ -38,12 +38,6 @@
38 38
 
39 39
 #include <stdint.h>
40 40
 
41
-#define AAC_INIT_VLC_STATIC(num, size) \
42
-    INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
43
-         ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
44
-        ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
45
-        size);
46
-
47 41
 #define MAX_CHANNELS 64
48 42
 #define MAX_ELEM_ID 16
49 43
 
... ...
@@ -241,7 +235,7 @@ typedef struct {
241 241
  * main AAC context
242 242
  */
243 243
 typedef struct {
244
-    AVCodecContext * avccontext;
244
+    AVCodecContext *avctx;
245 245
 
246 246
     MPEG4AudioConfig m4ac;
247 247
 
... ...
@@ -255,8 +249,9 @@ typedef struct {
255 255
     enum ChannelPosition che_pos[4][MAX_ELEM_ID]; /**< channel element channel mapping with the
256 256
                                                    *   first index as the first 4 raw data block types
257 257
                                                    */
258
-    ChannelElement * che[4][MAX_ELEM_ID];
259
-    ChannelElement * tag_che_map[4][MAX_ELEM_ID];
258
+    ChannelElement          *che[4][MAX_ELEM_ID];
259
+    ChannelElement  *tag_che_map[4][MAX_ELEM_ID];
260
+    uint8_t tags_seen_this_frame[4][MAX_ELEM_ID];
260 261
     int tags_mapped;
261 262
     /** @} */
262 263
 
263 264
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+/*
1
+ * Generate a header file for hardcoded AAC tables
2
+ *
3
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include <stdlib.h>
23
+#define CONFIG_HARDCODED_TABLES 0
24
+#include "aac_tablegen.h"
25
+#include "tableprint.h"
26
+
27
+int main(void)
28
+{
29
+    ff_aac_tableinit();
30
+
31
+    write_fileheader();
32
+
33
+    printf("const float ff_aac_pow2sf_tab[428] = {\n");
34
+    write_float_array(ff_aac_pow2sf_tab, 428);
35
+    printf("};\n");
36
+
37
+    return 0;
38
+}
0 39
new file mode 100644
... ...
@@ -0,0 +1,42 @@
0
+/*
1
+ * Header file for hardcoded AAC tables
2
+ *
3
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AAC_TABLEGEN_H
23
+#define AAC_TABLEGEN_H
24
+
25
+#include "aac_tablegen_decl.h"
26
+
27
+#if CONFIG_HARDCODED_TABLES
28
+#include "libavcodec/aac_tables.h"
29
+#else
30
+#include "../libavutil/mathematics.h"
31
+float ff_aac_pow2sf_tab[428];
32
+
33
+void ff_aac_tableinit(void)
34
+{
35
+    int i;
36
+    for (i = 0; i < 428; i++)
37
+        ff_aac_pow2sf_tab[i] = pow(2, (i - 200) / 4.);
38
+}
39
+#endif /* CONFIG_HARDCODED_TABLES */
40
+
41
+#endif /* AAC_TABLEGEN_H */
0 42
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+/*
1
+ * Header file for hardcoded AAC tables
2
+ *
3
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AAC_TABLEGEN_INIT_H
23
+#define AAC_TABLEGEN_INIT_H
24
+
25
+#if CONFIG_HARDCODED_TABLES
26
+#define ff_aac_tableinit()
27
+extern const float ff_aac_pow2sf_tab[428];
28
+#else
29
+void ff_aac_tableinit(void);
30
+extern       float ff_aac_pow2sf_tab[428];
31
+#endif /* CONFIG_HARDCODED_TABLES */
32
+
33
+#endif /* AAC_TABLEGEN_INIT_H */
0 34
new file mode 100644
... ...
@@ -0,0 +1,2142 @@
0
+/*
1
+ * AAC decoder
2
+ * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
3
+ * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file
24
+ * AAC decoder
25
+ * @author Oded Shimon  ( ods15 ods15 dyndns org )
26
+ * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
27
+ */
28
+
29
+/*
30
+ * supported tools
31
+ *
32
+ * Support?             Name
33
+ * N (code in SoC repo) gain control
34
+ * Y                    block switching
35
+ * Y                    window shapes - standard
36
+ * N                    window shapes - Low Delay
37
+ * Y                    filterbank - standard
38
+ * N (code in SoC repo) filterbank - Scalable Sample Rate
39
+ * Y                    Temporal Noise Shaping
40
+ * N (code in SoC repo) Long Term Prediction
41
+ * Y                    intensity stereo
42
+ * Y                    channel coupling
43
+ * Y                    frequency domain prediction
44
+ * Y                    Perceptual Noise Substitution
45
+ * Y                    Mid/Side stereo
46
+ * N                    Scalable Inverse AAC Quantization
47
+ * N                    Frequency Selective Switch
48
+ * N                    upsampling filter
49
+ * Y                    quantization & coding - AAC
50
+ * N                    quantization & coding - TwinVQ
51
+ * N                    quantization & coding - BSAC
52
+ * N                    AAC Error Resilience tools
53
+ * N                    Error Resilience payload syntax
54
+ * N                    Error Protection tool
55
+ * N                    CELP
56
+ * N                    Silence Compression
57
+ * N                    HVXC
58
+ * N                    HVXC 4kbits/s VR
59
+ * N                    Structured Audio tools
60
+ * N                    Structured Audio Sample Bank Format
61
+ * N                    MIDI
62
+ * N                    Harmonic and Individual Lines plus Noise
63
+ * N                    Text-To-Speech Interface
64
+ * Y                    Spectral Band Replication
65
+ * Y (not in this code) Layer-1
66
+ * Y (not in this code) Layer-2
67
+ * Y (not in this code) Layer-3
68
+ * N                    SinuSoidal Coding (Transient, Sinusoid, Noise)
69
+ * Y                    Parametric Stereo
70
+ * N                    Direct Stream Transfer
71
+ *
72
+ * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
73
+ *       - HE AAC v2 comprises LC AAC with Spectral Band Replication and
74
+           Parametric Stereo.
75
+ */
76
+
77
+
78
+#include "avcodec.h"
79
+#include "internal.h"
80
+#include "get_bits.h"
81
+#include "dsputil.h"
82
+#include "fft.h"
83
+#include "lpc.h"
84
+
85
+#include "aac.h"
86
+#include "aactab.h"
87
+#include "aacdectab.h"
88
+#include "cbrt_tablegen.h"
89
+#include "sbr.h"
90
+#include "aacsbr.h"
91
+#include "mpeg4audio.h"
92
+#include "aac_parser.h"
93
+
94
+#include <assert.h>
95
+#include <errno.h>
96
+#include <math.h>
97
+#include <string.h>
98
+
99
+#if ARCH_ARM
100
+#   include "arm/aac.h"
101
+#endif
102
+
103
+union float754 {
104
+    float f;
105
+    uint32_t i;
106
+};
107
+
108
+static VLC vlc_scalefactors;
109
+static VLC vlc_spectral[11];
110
+
111
+static const char overread_err[] = "Input buffer exhausted before END element found\n";
112
+
113
+static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
114
+{
115
+    /* Some buggy encoders appear to set all elem_ids to zero and rely on
116
+    channels always occurring in the same order. This is expressly forbidden
117
+    by the spec but we will try to work around it.
118
+    */
119
+    int err_printed = 0;
120
+    while (ac->tags_seen_this_frame[type][elem_id] && elem_id < MAX_ELEM_ID) {
121
+        if (ac->output_configured < OC_LOCKED && !err_printed) {
122
+            av_log(ac->avctx, AV_LOG_WARNING, "Duplicate channel tag found, attempting to remap.\n");
123
+            err_printed = 1;
124
+        }
125
+        elem_id++;
126
+    }
127
+    if (elem_id == MAX_ELEM_ID)
128
+        return NULL;
129
+    ac->tags_seen_this_frame[type][elem_id] = 1;
130
+
131
+    if (ac->tag_che_map[type][elem_id]) {
132
+        return ac->tag_che_map[type][elem_id];
133
+    }
134
+    if (ac->tags_mapped >= tags_per_config[ac->m4ac.chan_config]) {
135
+        return NULL;
136
+    }
137
+    switch (ac->m4ac.chan_config) {
138
+    case 7:
139
+        if (ac->tags_mapped == 3 && type == TYPE_CPE) {
140
+            ac->tags_mapped++;
141
+            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
142
+        }
143
+    case 6:
144
+        /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
145
+           instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
146
+           encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
147
+        if (ac->tags_mapped == tags_per_config[ac->m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
148
+            ac->tags_mapped++;
149
+            return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
150
+        }
151
+    case 5:
152
+        if (ac->tags_mapped == 2 && type == TYPE_CPE) {
153
+            ac->tags_mapped++;
154
+            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
155
+        }
156
+    case 4:
157
+        if (ac->tags_mapped == 2 && ac->m4ac.chan_config == 4 && type == TYPE_SCE) {
158
+            ac->tags_mapped++;
159
+            return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
160
+        }
161
+    case 3:
162
+    case 2:
163
+        if (ac->tags_mapped == (ac->m4ac.chan_config != 2) && type == TYPE_CPE) {
164
+            ac->tags_mapped++;
165
+            return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
166
+        } else if (ac->m4ac.chan_config == 2) {
167
+            return NULL;
168
+        }
169
+    case 1:
170
+        if (!ac->tags_mapped && type == TYPE_SCE) {
171
+            ac->tags_mapped++;
172
+            return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
173
+        }
174
+    default:
175
+        return NULL;
176
+    }
177
+}
178
+
179
+/**
180
+ * Check for the channel element in the current channel position configuration.
181
+ * If it exists, make sure the appropriate element is allocated and map the
182
+ * channel order to match the internal FFmpeg channel layout.
183
+ *
184
+ * @param   che_pos current channel position configuration
185
+ * @param   type channel element type
186
+ * @param   id channel element id
187
+ * @param   channels count of the number of channels in the configuration
188
+ *
189
+ * @return  Returns error status. 0 - OK, !0 - error
190
+ */
191
+static av_cold int che_configure(AACContext *ac,
192
+                         enum ChannelPosition che_pos[4][MAX_ELEM_ID],
193
+                         int type, int id,
194
+                         int *channels)
195
+{
196
+    if (che_pos[type][id]) {
197
+        if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
198
+            return AVERROR(ENOMEM);
199
+        ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
200
+        if (type != TYPE_CCE) {
201
+            ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
202
+            if (type == TYPE_CPE ||
203
+                (type == TYPE_SCE && ac->m4ac.ps == 1)) {
204
+                ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
205
+            }
206
+        }
207
+    } else {
208
+        if (ac->che[type][id])
209
+            ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
210
+        av_freep(&ac->che[type][id]);
211
+    }
212
+    return 0;
213
+}
214
+
215
+/**
216
+ * Configure output channel order based on the current program configuration element.
217
+ *
218
+ * @param   che_pos current channel position configuration
219
+ * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
220
+ *
221
+ * @return  Returns error status. 0 - OK, !0 - error
222
+ */
223
+static av_cold int output_configure(AACContext *ac,
224
+                            enum ChannelPosition che_pos[4][MAX_ELEM_ID],
225
+                            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
226
+                            int channel_config, enum OCStatus oc_type)
227
+{
228
+    AVCodecContext *avctx = ac->avctx;
229
+    int i, type, channels = 0, ret;
230
+
231
+    if (new_che_pos != che_pos)
232
+    memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
233
+
234
+    if (channel_config) {
235
+        for (i = 0; i < tags_per_config[channel_config]; i++) {
236
+            if ((ret = che_configure(ac, che_pos,
237
+                                     aac_channel_layout_map[channel_config - 1][i][0],
238
+                                     aac_channel_layout_map[channel_config - 1][i][1],
239
+                                     &channels)))
240
+                return ret;
241
+        }
242
+
243
+        memset(ac->tag_che_map, 0,       4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
244
+        ac->tags_mapped = 0;
245
+
246
+        avctx->channel_layout = aac_channel_layout[channel_config - 1];
247
+    } else {
248
+        /* Allocate or free elements depending on if they are in the
249
+         * current program configuration.
250
+         *
251
+         * Set up default 1:1 output mapping.
252
+         *
253
+         * For a 5.1 stream the output order will be:
254
+         *    [ Center ] [ Front Left ] [ Front Right ] [ LFE ] [ Surround Left ] [ Surround Right ]
255
+         */
256
+
257
+        for (i = 0; i < MAX_ELEM_ID; i++) {
258
+            for (type = 0; type < 4; type++) {
259
+                if ((ret = che_configure(ac, che_pos, type, i, &channels)))
260
+                    return ret;
261
+            }
262
+        }
263
+
264
+        memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
265
+        ac->tags_mapped = 4 * MAX_ELEM_ID;
266
+
267
+        avctx->channel_layout = 0;
268
+    }
269
+
270
+    avctx->channels = channels;
271
+
272
+    ac->output_configured = oc_type;
273
+
274
+    return 0;
275
+}
276
+
277
+/**
278
+ * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
279
+ *
280
+ * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
281
+ * @param sce_map mono (Single Channel Element) map
282
+ * @param type speaker type/position for these channels
283
+ */
284
+static void decode_channel_map(enum ChannelPosition *cpe_map,
285
+                               enum ChannelPosition *sce_map,
286
+                               enum ChannelPosition type,
287
+                               GetBitContext *gb, int n)
288
+{
289
+    while (n--) {
290
+        enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
291
+        map[get_bits(gb, 4)] = type;
292
+    }
293
+}
294
+
295
+/**
296
+ * Decode program configuration element; reference: table 4.2.
297
+ *
298
+ * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
299
+ *
300
+ * @return  Returns error status. 0 - OK, !0 - error
301
+ */
302
+static int decode_pce(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
303
+                      GetBitContext *gb)
304
+{
305
+    int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
306
+    int comment_len;
307
+
308
+    skip_bits(gb, 2);  // object_type
309
+
310
+    sampling_index = get_bits(gb, 4);
311
+    if (ac->m4ac.sampling_index != sampling_index)
312
+        av_log(ac->avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
313
+
314
+    num_front       = get_bits(gb, 4);
315
+    num_side        = get_bits(gb, 4);
316
+    num_back        = get_bits(gb, 4);
317
+    num_lfe         = get_bits(gb, 2);
318
+    num_assoc_data  = get_bits(gb, 3);
319
+    num_cc          = get_bits(gb, 4);
320
+
321
+    if (get_bits1(gb))
322
+        skip_bits(gb, 4); // mono_mixdown_tag
323
+    if (get_bits1(gb))
324
+        skip_bits(gb, 4); // stereo_mixdown_tag
325
+
326
+    if (get_bits1(gb))
327
+        skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
328
+
329
+    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
330
+    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE,  gb, num_side );
331
+    decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK,  gb, num_back );
332
+    decode_channel_map(NULL,                  new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE,   gb, num_lfe  );
333
+
334
+    skip_bits_long(gb, 4 * num_assoc_data);
335
+
336
+    decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC,    gb, num_cc   );
337
+
338
+    align_get_bits(gb);
339
+
340
+    /* comment field, first byte is length */
341
+    comment_len = get_bits(gb, 8) * 8;
342
+    if (get_bits_left(gb) < comment_len) {
343
+        av_log(ac->avctx, AV_LOG_ERROR, overread_err);
344
+        return -1;
345
+    }
346
+    skip_bits_long(gb, comment_len);
347
+    return 0;
348
+}
349
+
350
+/**
351
+ * Set up channel positions based on a default channel configuration
352
+ * as specified in table 1.17.
353
+ *
354
+ * @param   new_che_pos New channel position configuration - we only do something if it differs from the current one.
355
+ *
356
+ * @return  Returns error status. 0 - OK, !0 - error
357
+ */
358
+static av_cold int set_default_channel_config(AACContext *ac,
359
+                                      enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
360
+                                      int channel_config)
361
+{
362
+    if (channel_config < 1 || channel_config > 7) {
363
+        av_log(ac->avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
364
+               channel_config);
365
+        return -1;
366
+    }
367
+
368
+    /* default channel configurations:
369
+     *
370
+     * 1ch : front center (mono)
371
+     * 2ch : L + R (stereo)
372
+     * 3ch : front center + L + R
373
+     * 4ch : front center + L + R + back center
374
+     * 5ch : front center + L + R + back stereo
375
+     * 6ch : front center + L + R + back stereo + LFE
376
+     * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
377
+     */
378
+
379
+    if (channel_config != 2)
380
+        new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
381
+    if (channel_config > 1)
382
+        new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
383
+    if (channel_config == 4)
384
+        new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK;  // back center
385
+    if (channel_config > 4)
386
+        new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
387
+        = AAC_CHANNEL_BACK;  // back stereo
388
+    if (channel_config > 5)
389
+        new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE;   // LFE
390
+    if (channel_config == 7)
391
+        new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
392
+
393
+    return 0;
394
+}
395
+
396
+/**
397
+ * Decode GA "General Audio" specific configuration; reference: table 4.1.
398
+ *
399
+ * @return  Returns error status. 0 - OK, !0 - error
400
+ */
401
+static int decode_ga_specific_config(AACContext *ac, GetBitContext *gb,
402
+                                     int channel_config)
403
+{
404
+    enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
405
+    int extension_flag, ret;
406
+
407
+    if (get_bits1(gb)) { // frameLengthFlag
408
+        av_log_missing_feature(ac->avctx, "960/120 MDCT window is", 1);
409
+        return -1;
410
+    }
411
+
412
+    if (get_bits1(gb))       // dependsOnCoreCoder
413
+        skip_bits(gb, 14);   // coreCoderDelay
414
+    extension_flag = get_bits1(gb);
415
+
416
+    if (ac->m4ac.object_type == AOT_AAC_SCALABLE ||
417
+        ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
418
+        skip_bits(gb, 3);     // layerNr
419
+
420
+    memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
421
+    if (channel_config == 0) {
422
+        skip_bits(gb, 4);  // element_instance_tag
423
+        if ((ret = decode_pce(ac, new_che_pos, gb)))
424
+            return ret;
425
+    } else {
426
+        if ((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
427
+            return ret;
428
+    }
429
+    if ((ret = output_configure(ac, ac->che_pos, new_che_pos, channel_config, OC_GLOBAL_HDR)))
430
+        return ret;
431
+
432
+    if (extension_flag) {
433
+        switch (ac->m4ac.object_type) {
434
+        case AOT_ER_BSAC:
435
+            skip_bits(gb, 5);    // numOfSubFrame
436
+            skip_bits(gb, 11);   // layer_length
437
+            break;
438
+        case AOT_ER_AAC_LC:
439
+        case AOT_ER_AAC_LTP:
440
+        case AOT_ER_AAC_SCALABLE:
441
+        case AOT_ER_AAC_LD:
442
+            skip_bits(gb, 3);  /* aacSectionDataResilienceFlag
443
+                                    * aacScalefactorDataResilienceFlag
444
+                                    * aacSpectralDataResilienceFlag
445
+                                    */
446
+            break;
447
+        }
448
+        skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
449
+    }
450
+    return 0;
451
+}
452
+
453
+/**
454
+ * Decode audio specific configuration; reference: table 1.13.
455
+ *
456
+ * @param   data        pointer to AVCodecContext extradata
457
+ * @param   data_size   size of AVCCodecContext extradata
458
+ *
459
+ * @return  Returns error status. 0 - OK, !0 - error
460
+ */
461
+static int decode_audio_specific_config(AACContext *ac, void *data,
462
+                                        int data_size)
463
+{
464
+    GetBitContext gb;
465
+    int i;
466
+
467
+    init_get_bits(&gb, data, data_size * 8);
468
+
469
+    if ((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
470
+        return -1;
471
+    if (ac->m4ac.sampling_index > 12) {
472
+        av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
473
+        return -1;
474
+    }
475
+    if (ac->m4ac.sbr == 1 && ac->m4ac.ps == -1)
476
+        ac->m4ac.ps = 1;
477
+
478
+    skip_bits_long(&gb, i);
479
+
480
+    switch (ac->m4ac.object_type) {
481
+    case AOT_AAC_MAIN:
482
+    case AOT_AAC_LC:
483
+        if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
484
+            return -1;
485
+        break;
486
+    default:
487
+        av_log(ac->avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
488
+               ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
489
+        return -1;
490
+    }
491
+    return 0;
492
+}
493
+
494
+/**
495
+ * linear congruential pseudorandom number generator
496
+ *
497
+ * @param   previous_val    pointer to the current state of the generator
498
+ *
499
+ * @return  Returns a 32-bit pseudorandom integer
500
+ */
501
+static av_always_inline int lcg_random(int previous_val)
502
+{
503
+    return previous_val * 1664525 + 1013904223;
504
+}
505
+
506
+static av_always_inline void reset_predict_state(PredictorState *ps)
507
+{
508
+    ps->r0   = 0.0f;
509
+    ps->r1   = 0.0f;
510
+    ps->cor0 = 0.0f;
511
+    ps->cor1 = 0.0f;
512
+    ps->var0 = 1.0f;
513
+    ps->var1 = 1.0f;
514
+}
515
+
516
+static void reset_all_predictors(PredictorState *ps)
517
+{
518
+    int i;
519
+    for (i = 0; i < MAX_PREDICTORS; i++)
520
+        reset_predict_state(&ps[i]);
521
+}
522
+
523
+static void reset_predictor_group(PredictorState *ps, int group_num)
524
+{
525
+    int i;
526
+    for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
527
+        reset_predict_state(&ps[i]);
528
+}
529
+
530
+#define AAC_INIT_VLC_STATIC(num, size) \
531
+    INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
532
+         ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
533
+        ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
534
+        size);
535
+
536
+static av_cold int aac_decode_init(AVCodecContext *avctx)
537
+{
538
+    AACContext *ac = avctx->priv_data;
539
+
540
+    ac->avctx = avctx;
541
+    ac->m4ac.sample_rate = avctx->sample_rate;
542
+
543
+    if (avctx->extradata_size > 0) {
544
+        if (decode_audio_specific_config(ac, avctx->extradata, avctx->extradata_size))
545
+            return -1;
546
+    }
547
+
548
+    avctx->sample_fmt = SAMPLE_FMT_S16;
549
+
550
+    AAC_INIT_VLC_STATIC( 0, 304);
551
+    AAC_INIT_VLC_STATIC( 1, 270);
552
+    AAC_INIT_VLC_STATIC( 2, 550);
553
+    AAC_INIT_VLC_STATIC( 3, 300);
554
+    AAC_INIT_VLC_STATIC( 4, 328);
555
+    AAC_INIT_VLC_STATIC( 5, 294);
556
+    AAC_INIT_VLC_STATIC( 6, 306);
557
+    AAC_INIT_VLC_STATIC( 7, 268);
558
+    AAC_INIT_VLC_STATIC( 8, 510);
559
+    AAC_INIT_VLC_STATIC( 9, 366);
560
+    AAC_INIT_VLC_STATIC(10, 462);
561
+
562
+    ff_aac_sbr_init();
563
+
564
+    dsputil_init(&ac->dsp, avctx);
565
+
566
+    ac->random_state = 0x1f2e3d4c;
567
+
568
+    // -1024 - Compensate wrong IMDCT method.
569
+    // 32768 - Required to scale values to the correct range for the bias method
570
+    //         for float to int16 conversion.
571
+
572
+    if (ac->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
573
+        ac->add_bias  = 385.0f;
574
+        ac->sf_scale  = 1. / (-1024. * 32768.);
575
+        ac->sf_offset = 0;
576
+    } else {
577
+        ac->add_bias  = 0.0f;
578
+        ac->sf_scale  = 1. / -1024.;
579
+        ac->sf_offset = 60;
580
+    }
581
+
582
+    ff_aac_tableinit();
583
+
584
+    INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
585
+                    ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
586
+                    ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
587
+                    352);
588
+
589
+    ff_mdct_init(&ac->mdct, 11, 1, 1.0);
590
+    ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
591
+    // window initialization
592
+    ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
593
+    ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
594
+    ff_init_ff_sine_windows(10);
595
+    ff_init_ff_sine_windows( 7);
596
+
597
+    cbrt_tableinit();
598
+
599
+    return 0;
600
+}
601
+
602
+/**
603
+ * Skip data_stream_element; reference: table 4.10.
604
+ */
605
+static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
606
+{
607
+    int byte_align = get_bits1(gb);
608
+    int count = get_bits(gb, 8);
609
+    if (count == 255)
610
+        count += get_bits(gb, 8);
611
+    if (byte_align)
612
+        align_get_bits(gb);
613
+
614
+    if (get_bits_left(gb) < 8 * count) {
615
+        av_log(ac->avctx, AV_LOG_ERROR, overread_err);
616
+        return -1;
617
+    }
618
+    skip_bits_long(gb, 8 * count);
619
+    return 0;
620
+}
621
+
622
+static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
623
+                             GetBitContext *gb)
624
+{
625
+    int sfb;
626
+    if (get_bits1(gb)) {
627
+        ics->predictor_reset_group = get_bits(gb, 5);
628
+        if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
629
+            av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
630
+            return -1;
631
+        }
632
+    }
633
+    for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->m4ac.sampling_index]); sfb++) {
634
+        ics->prediction_used[sfb] = get_bits1(gb);
635
+    }
636
+    return 0;
637
+}
638
+
639
+/**
640
+ * Decode Individual Channel Stream info; reference: table 4.6.
641
+ *
642
+ * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
643
+ */
644
+static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
645
+                           GetBitContext *gb, int common_window)
646
+{
647
+    if (get_bits1(gb)) {
648
+        av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
649
+        memset(ics, 0, sizeof(IndividualChannelStream));
650
+        return -1;
651
+    }
652
+    ics->window_sequence[1] = ics->window_sequence[0];
653
+    ics->window_sequence[0] = get_bits(gb, 2);
654
+    ics->use_kb_window[1]   = ics->use_kb_window[0];
655
+    ics->use_kb_window[0]   = get_bits1(gb);
656
+    ics->num_window_groups  = 1;
657
+    ics->group_len[0]       = 1;
658
+    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
659
+        int i;
660
+        ics->max_sfb = get_bits(gb, 4);
661
+        for (i = 0; i < 7; i++) {
662
+            if (get_bits1(gb)) {
663
+                ics->group_len[ics->num_window_groups - 1]++;
664
+            } else {
665
+                ics->num_window_groups++;
666
+                ics->group_len[ics->num_window_groups - 1] = 1;
667
+            }
668
+        }
669
+        ics->num_windows       = 8;
670
+        ics->swb_offset        =    ff_swb_offset_128[ac->m4ac.sampling_index];
671
+        ics->num_swb           =   ff_aac_num_swb_128[ac->m4ac.sampling_index];
672
+        ics->tns_max_bands     = ff_tns_max_bands_128[ac->m4ac.sampling_index];
673
+        ics->predictor_present = 0;
674
+    } else {
675
+        ics->max_sfb               = get_bits(gb, 6);
676
+        ics->num_windows           = 1;
677
+        ics->swb_offset            =    ff_swb_offset_1024[ac->m4ac.sampling_index];
678
+        ics->num_swb               =   ff_aac_num_swb_1024[ac->m4ac.sampling_index];
679
+        ics->tns_max_bands         = ff_tns_max_bands_1024[ac->m4ac.sampling_index];
680
+        ics->predictor_present     = get_bits1(gb);
681
+        ics->predictor_reset_group = 0;
682
+        if (ics->predictor_present) {
683
+            if (ac->m4ac.object_type == AOT_AAC_MAIN) {
684
+                if (decode_prediction(ac, ics, gb)) {
685
+                    memset(ics, 0, sizeof(IndividualChannelStream));
686
+                    return -1;
687
+                }
688
+            } else if (ac->m4ac.object_type == AOT_AAC_LC) {
689
+                av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
690
+                memset(ics, 0, sizeof(IndividualChannelStream));
691
+                return -1;
692
+            } else {
693
+                av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
694
+                memset(ics, 0, sizeof(IndividualChannelStream));
695
+                return -1;
696
+            }
697
+        }
698
+    }
699
+
700
+    if (ics->max_sfb > ics->num_swb) {
701
+        av_log(ac->avctx, AV_LOG_ERROR,
702
+               "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
703
+               ics->max_sfb, ics->num_swb);
704
+        memset(ics, 0, sizeof(IndividualChannelStream));
705
+        return -1;
706
+    }
707
+
708
+    return 0;
709
+}
710
+
711
+/**
712
+ * Decode band types (section_data payload); reference: table 4.46.
713
+ *
714
+ * @param   band_type           array of the used band type
715
+ * @param   band_type_run_end   array of the last scalefactor band of a band type run
716
+ *
717
+ * @return  Returns error status. 0 - OK, !0 - error
718
+ */
719
+static int decode_band_types(AACContext *ac, enum BandType band_type[120],
720
+                             int band_type_run_end[120], GetBitContext *gb,
721
+                             IndividualChannelStream *ics)
722
+{
723
+    int g, idx = 0;
724
+    const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
725
+    for (g = 0; g < ics->num_window_groups; g++) {
726
+        int k = 0;
727
+        while (k < ics->max_sfb) {
728
+            uint8_t sect_end = k;
729
+            int sect_len_incr;
730
+            int sect_band_type = get_bits(gb, 4);
731
+            if (sect_band_type == 12) {
732
+                av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
733
+                return -1;
734
+            }
735
+            while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits) - 1)
736
+                sect_end += sect_len_incr;
737
+            sect_end += sect_len_incr;
738
+            if (get_bits_left(gb) < 0) {
739
+                av_log(ac->avctx, AV_LOG_ERROR, overread_err);
740
+                return -1;
741
+            }
742
+            if (sect_end > ics->max_sfb) {
743
+                av_log(ac->avctx, AV_LOG_ERROR,
744
+                       "Number of bands (%d) exceeds limit (%d).\n",
745
+                       sect_end, ics->max_sfb);
746
+                return -1;
747
+            }
748
+            for (; k < sect_end; k++) {
749
+                band_type        [idx]   = sect_band_type;
750
+                band_type_run_end[idx++] = sect_end;
751
+            }
752
+        }
753
+    }
754
+    return 0;
755
+}
756
+
757
+/**
758
+ * Decode scalefactors; reference: table 4.47.
759
+ *
760
+ * @param   global_gain         first scalefactor value as scalefactors are differentially coded
761
+ * @param   band_type           array of the used band type
762
+ * @param   band_type_run_end   array of the last scalefactor band of a band type run
763
+ * @param   sf                  array of scalefactors or intensity stereo positions
764
+ *
765
+ * @return  Returns error status. 0 - OK, !0 - error
766
+ */
767
+static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
768
+                               unsigned int global_gain,
769
+                               IndividualChannelStream *ics,
770
+                               enum BandType band_type[120],
771
+                               int band_type_run_end[120])
772
+{
773
+    const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
774
+    int g, i, idx = 0;
775
+    int offset[3] = { global_gain, global_gain - 90, 100 };
776
+    int noise_flag = 1;
777
+    static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
778
+    for (g = 0; g < ics->num_window_groups; g++) {
779
+        for (i = 0; i < ics->max_sfb;) {
780
+            int run_end = band_type_run_end[idx];
781
+            if (band_type[idx] == ZERO_BT) {
782
+                for (; i < run_end; i++, idx++)
783
+                    sf[idx] = 0.;
784
+            } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
785
+                for (; i < run_end; i++, idx++) {
786
+                    offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
787
+                    if (offset[2] > 255U) {
788
+                        av_log(ac->avctx, AV_LOG_ERROR,
789
+                               "%s (%d) out of range.\n", sf_str[2], offset[2]);
790
+                        return -1;
791
+                    }
792
+                    sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
793
+                }
794
+            } else if (band_type[idx] == NOISE_BT) {
795
+                for (; i < run_end; i++, idx++) {
796
+                    if (noise_flag-- > 0)
797
+                        offset[1] += get_bits(gb, 9) - 256;
798
+                    else
799
+                        offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
800
+                    if (offset[1] > 255U) {
801
+                        av_log(ac->avctx, AV_LOG_ERROR,
802
+                               "%s (%d) out of range.\n", sf_str[1], offset[1]);
803
+                        return -1;
804
+                    }
805
+                    sf[idx] = -ff_aac_pow2sf_tab[offset[1] + sf_offset + 100];
806
+                }
807
+            } else {
808
+                for (; i < run_end; i++, idx++) {
809
+                    offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
810
+                    if (offset[0] > 255U) {
811
+                        av_log(ac->avctx, AV_LOG_ERROR,
812
+                               "%s (%d) out of range.\n", sf_str[0], offset[0]);
813
+                        return -1;
814
+                    }
815
+                    sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
816
+                }
817
+            }
818
+        }
819
+    }
820
+    return 0;
821
+}
822
+
823
+/**
824
+ * Decode pulse data; reference: table 4.7.
825
+ */
826
+static int decode_pulses(Pulse *pulse, GetBitContext *gb,
827
+                         const uint16_t *swb_offset, int num_swb)
828
+{
829
+    int i, pulse_swb;
830
+    pulse->num_pulse = get_bits(gb, 2) + 1;
831
+    pulse_swb        = get_bits(gb, 6);
832
+    if (pulse_swb >= num_swb)
833
+        return -1;
834
+    pulse->pos[0]    = swb_offset[pulse_swb];
835
+    pulse->pos[0]   += get_bits(gb, 5);
836
+    if (pulse->pos[0] > 1023)
837
+        return -1;
838
+    pulse->amp[0]    = get_bits(gb, 4);
839
+    for (i = 1; i < pulse->num_pulse; i++) {
840
+        pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
841
+        if (pulse->pos[i] > 1023)
842
+            return -1;
843
+        pulse->amp[i] = get_bits(gb, 4);
844
+    }
845
+    return 0;
846
+}
847
+
848
+/**
849
+ * Decode Temporal Noise Shaping data; reference: table 4.48.
850
+ *
851
+ * @return  Returns error status. 0 - OK, !0 - error
852
+ */
853
+static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
854
+                      GetBitContext *gb, const IndividualChannelStream *ics)
855
+{
856
+    int w, filt, i, coef_len, coef_res, coef_compress;
857
+    const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
858
+    const int tns_max_order = is8 ? 7 : ac->m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
859
+    for (w = 0; w < ics->num_windows; w++) {
860
+        if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
861
+            coef_res = get_bits1(gb);
862
+
863
+            for (filt = 0; filt < tns->n_filt[w]; filt++) {
864
+                int tmp2_idx;
865
+                tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
866
+
867
+                if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
868
+                    av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
869
+                           tns->order[w][filt], tns_max_order);
870
+                    tns->order[w][filt] = 0;
871
+                    return -1;
872
+                }
873
+                if (tns->order[w][filt]) {
874
+                    tns->direction[w][filt] = get_bits1(gb);
875
+                    coef_compress = get_bits1(gb);
876
+                    coef_len = coef_res + 3 - coef_compress;
877
+                    tmp2_idx = 2 * coef_compress + coef_res;
878
+
879
+                    for (i = 0; i < tns->order[w][filt]; i++)
880
+                        tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
881
+                }
882
+            }
883
+        }
884
+    }
885
+    return 0;
886
+}
887
+
888
+/**
889
+ * Decode Mid/Side data; reference: table 4.54.
890
+ *
891
+ * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
892
+ *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
893
+ *                      [3] reserved for scalable AAC
894
+ */
895
+static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
896
+                                   int ms_present)
897
+{
898
+    int idx;
899
+    if (ms_present == 1) {
900
+        for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
901
+            cpe->ms_mask[idx] = get_bits1(gb);
902
+    } else if (ms_present == 2) {
903
+        memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
904
+    }
905
+}
906
+
907
+#ifndef VMUL2
908
+static inline float *VMUL2(float *dst, const float *v, unsigned idx,
909
+                           const float *scale)
910
+{
911
+    float s = *scale;
912
+    *dst++ = v[idx    & 15] * s;
913
+    *dst++ = v[idx>>4 & 15] * s;
914
+    return dst;
915
+}
916
+#endif
917
+
918
+#ifndef VMUL4
919
+static inline float *VMUL4(float *dst, const float *v, unsigned idx,
920
+                           const float *scale)
921
+{
922
+    float s = *scale;
923
+    *dst++ = v[idx    & 3] * s;
924
+    *dst++ = v[idx>>2 & 3] * s;
925
+    *dst++ = v[idx>>4 & 3] * s;
926
+    *dst++ = v[idx>>6 & 3] * s;
927
+    return dst;
928
+}
929
+#endif
930
+
931
+#ifndef VMUL2S
932
+static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
933
+                            unsigned sign, const float *scale)
934
+{
935
+    union float754 s0, s1;
936
+
937
+    s0.f = s1.f = *scale;
938
+    s0.i ^= sign >> 1 << 31;
939
+    s1.i ^= sign      << 31;
940
+
941
+    *dst++ = v[idx    & 15] * s0.f;
942
+    *dst++ = v[idx>>4 & 15] * s1.f;
943
+
944
+    return dst;
945
+}
946
+#endif
947
+
948
+#ifndef VMUL4S
949
+static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
950
+                            unsigned sign, const float *scale)
951
+{
952
+    unsigned nz = idx >> 12;
953
+    union float754 s = { .f = *scale };
954
+    union float754 t;
955
+
956
+    t.i = s.i ^ (sign & 1<<31);
957
+    *dst++ = v[idx    & 3] * t.f;
958
+
959
+    sign <<= nz & 1; nz >>= 1;
960
+    t.i = s.i ^ (sign & 1<<31);
961
+    *dst++ = v[idx>>2 & 3] * t.f;
962
+
963
+    sign <<= nz & 1; nz >>= 1;
964
+    t.i = s.i ^ (sign & 1<<31);
965
+    *dst++ = v[idx>>4 & 3] * t.f;
966
+
967
+    sign <<= nz & 1; nz >>= 1;
968
+    t.i = s.i ^ (sign & 1<<31);
969
+    *dst++ = v[idx>>6 & 3] * t.f;
970
+
971
+    return dst;
972
+}
973
+#endif
974
+
975
+/**
976
+ * Decode spectral data; reference: table 4.50.
977
+ * Dequantize and scale spectral data; reference: 4.6.3.3.
978
+ *
979
+ * @param   coef            array of dequantized, scaled spectral data
980
+ * @param   sf              array of scalefactors or intensity stereo positions
981
+ * @param   pulse_present   set if pulses are present
982
+ * @param   pulse           pointer to pulse data struct
983
+ * @param   band_type       array of the used band type
984
+ *
985
+ * @return  Returns error status. 0 - OK, !0 - error
986
+ */
987
+static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
988
+                                       GetBitContext *gb, const float sf[120],
989
+                                       int pulse_present, const Pulse *pulse,
990
+                                       const IndividualChannelStream *ics,
991
+                                       enum BandType band_type[120])
992
+{
993
+    int i, k, g, idx = 0;
994
+    const int c = 1024 / ics->num_windows;
995
+    const uint16_t *offsets = ics->swb_offset;
996
+    float *coef_base = coef;
997
+    int err_idx;
998
+
999
+    for (g = 0; g < ics->num_windows; g++)
1000
+        memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
1001
+
1002
+    for (g = 0; g < ics->num_window_groups; g++) {
1003
+        unsigned g_len = ics->group_len[g];
1004
+
1005
+        for (i = 0; i < ics->max_sfb; i++, idx++) {
1006
+            const unsigned cbt_m1 = band_type[idx] - 1;
1007
+            float *cfo = coef + offsets[i];
1008
+            int off_len = offsets[i + 1] - offsets[i];
1009
+            int group;
1010
+
1011
+            if (cbt_m1 >= INTENSITY_BT2 - 1) {
1012
+                for (group = 0; group < g_len; group++, cfo+=128) {
1013
+                    memset(cfo, 0, off_len * sizeof(float));
1014
+                }
1015
+            } else if (cbt_m1 == NOISE_BT - 1) {
1016
+                for (group = 0; group < g_len; group++, cfo+=128) {
1017
+                    float scale;
1018
+                    float band_energy;
1019
+
1020
+                    for (k = 0; k < off_len; k++) {
1021
+                        ac->random_state  = lcg_random(ac->random_state);
1022
+                        cfo[k] = ac->random_state;
1023
+                    }
1024
+
1025
+                    band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
1026
+                    scale = sf[idx] / sqrtf(band_energy);
1027
+                    ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1028
+                }
1029
+            } else {
1030
+                const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1031
+                const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1032
+                VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1033
+                const int cb_size = ff_aac_spectral_sizes[cbt_m1];
1034
+                OPEN_READER(re, gb);
1035
+
1036
+                switch (cbt_m1 >> 1) {
1037
+                case 0:
1038
+                    for (group = 0; group < g_len; group++, cfo+=128) {
1039
+                        float *cf = cfo;
1040
+                        int len = off_len;
1041
+
1042
+                        do {
1043
+                            int code;
1044
+                            unsigned cb_idx;
1045
+
1046
+                            UPDATE_CACHE(re, gb);
1047
+                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1048
+
1049
+                            if (code >= cb_size) {
1050
+                                err_idx = code;
1051
+                                goto err_cb_overflow;
1052
+                            }
1053
+
1054
+                            cb_idx = cb_vector_idx[code];
1055
+                            cf = VMUL4(cf, vq, cb_idx, sf + idx);
1056
+                        } while (len -= 4);
1057
+                    }
1058
+                    break;
1059
+
1060
+                case 1:
1061
+                    for (group = 0; group < g_len; group++, cfo+=128) {
1062
+                        float *cf = cfo;
1063
+                        int len = off_len;
1064
+
1065
+                        do {
1066
+                            int code;
1067
+                            unsigned nnz;
1068
+                            unsigned cb_idx;
1069
+                            uint32_t bits;
1070
+
1071
+                            UPDATE_CACHE(re, gb);
1072
+                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1073
+
1074
+                            if (code >= cb_size) {
1075
+                                err_idx = code;
1076
+                                goto err_cb_overflow;
1077
+                            }
1078
+
1079
+#if MIN_CACHE_BITS < 20
1080
+                            UPDATE_CACHE(re, gb);
1081
+#endif
1082
+                            cb_idx = cb_vector_idx[code];
1083
+                            nnz = cb_idx >> 8 & 15;
1084
+                            bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1085
+                            LAST_SKIP_BITS(re, gb, nnz);
1086
+                            cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1087
+                        } while (len -= 4);
1088
+                    }
1089
+                    break;
1090
+
1091
+                case 2:
1092
+                    for (group = 0; group < g_len; group++, cfo+=128) {
1093
+                        float *cf = cfo;
1094
+                        int len = off_len;
1095
+
1096
+                        do {
1097
+                            int code;
1098
+                            unsigned cb_idx;
1099
+
1100
+                            UPDATE_CACHE(re, gb);
1101
+                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1102
+
1103
+                            if (code >= cb_size) {
1104
+                                err_idx = code;
1105
+                                goto err_cb_overflow;
1106
+                            }
1107
+
1108
+                            cb_idx = cb_vector_idx[code];
1109
+                            cf = VMUL2(cf, vq, cb_idx, sf + idx);
1110
+                        } while (len -= 2);
1111
+                    }
1112
+                    break;
1113
+
1114
+                case 3:
1115
+                case 4:
1116
+                    for (group = 0; group < g_len; group++, cfo+=128) {
1117
+                        float *cf = cfo;
1118
+                        int len = off_len;
1119
+
1120
+                        do {
1121
+                            int code;
1122
+                            unsigned nnz;
1123
+                            unsigned cb_idx;
1124
+                            unsigned sign;
1125
+
1126
+                            UPDATE_CACHE(re, gb);
1127
+                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1128
+
1129
+                            if (code >= cb_size) {
1130
+                                err_idx = code;
1131
+                                goto err_cb_overflow;
1132
+                            }
1133
+
1134
+                            cb_idx = cb_vector_idx[code];
1135
+                            nnz = cb_idx >> 8 & 15;
1136
+                            sign = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
1137
+                            LAST_SKIP_BITS(re, gb, nnz);
1138
+                            cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1139
+                        } while (len -= 2);
1140
+                    }
1141
+                    break;
1142
+
1143
+                default:
1144
+                    for (group = 0; group < g_len; group++, cfo+=128) {
1145
+                        float *cf = cfo;
1146
+                        uint32_t *icf = (uint32_t *) cf;
1147
+                        int len = off_len;
1148
+
1149
+                        do {
1150
+                            int code;
1151
+                            unsigned nzt, nnz;
1152
+                            unsigned cb_idx;
1153
+                            uint32_t bits;
1154
+                            int j;
1155
+
1156
+                            UPDATE_CACHE(re, gb);
1157
+                            GET_VLC(code, re, gb, vlc_tab, 8, 2);
1158
+
1159
+                            if (!code) {
1160
+                                *icf++ = 0;
1161
+                                *icf++ = 0;
1162
+                                continue;
1163
+                            }
1164
+
1165
+                            if (code >= cb_size) {
1166
+                                err_idx = code;
1167
+                                goto err_cb_overflow;
1168
+                            }
1169
+
1170
+                            cb_idx = cb_vector_idx[code];
1171
+                            nnz = cb_idx >> 12;
1172
+                            nzt = cb_idx >> 8;
1173
+                            bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1174
+                            LAST_SKIP_BITS(re, gb, nnz);
1175
+
1176
+                            for (j = 0; j < 2; j++) {
1177
+                                if (nzt & 1<<j) {
1178
+                                    uint32_t b;
1179
+                                    int n;
1180
+                                    /* The total length of escape_sequence must be < 22 bits according
1181
+                                       to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1182
+                                    UPDATE_CACHE(re, gb);
1183
+                                    b = GET_CACHE(re, gb);
1184
+                                    b = 31 - av_log2(~b);
1185
+
1186
+                                    if (b > 8) {
1187
+                                        av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1188
+                                        return -1;
1189
+                                    }
1190
+
1191
+#if MIN_CACHE_BITS < 21
1192
+                                    LAST_SKIP_BITS(re, gb, b + 1);
1193
+                                    UPDATE_CACHE(re, gb);
1194
+#else
1195
+                                    SKIP_BITS(re, gb, b + 1);
1196
+#endif
1197
+                                    b += 4;
1198
+                                    n = (1 << b) + SHOW_UBITS(re, gb, b);
1199
+                                    LAST_SKIP_BITS(re, gb, b);
1200
+                                    *icf++ = cbrt_tab[n] | (bits & 1<<31);
1201
+                                    bits <<= 1;
1202
+                                } else {
1203
+                                    unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1204
+                                    *icf++ = (bits & 1<<31) | v;
1205
+                                    bits <<= !!v;
1206
+                                }
1207
+                                cb_idx >>= 4;
1208
+                            }
1209
+                        } while (len -= 2);
1210
+
1211
+                        ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1212
+                    }
1213
+                }
1214
+
1215
+                CLOSE_READER(re, gb);
1216
+            }
1217
+        }
1218
+        coef += g_len << 7;
1219
+    }
1220
+
1221
+    if (pulse_present) {
1222
+        idx = 0;
1223
+        for (i = 0; i < pulse->num_pulse; i++) {
1224
+            float co = coef_base[ pulse->pos[i] ];
1225
+            while (offsets[idx + 1] <= pulse->pos[i])
1226
+                idx++;
1227
+            if (band_type[idx] != NOISE_BT && sf[idx]) {
1228
+                float ico = -pulse->amp[i];
1229
+                if (co) {
1230
+                    co /= sf[idx];
1231
+                    ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1232
+                }
1233
+                coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1234
+            }
1235
+        }
1236
+    }
1237
+    return 0;
1238
+
1239
+err_cb_overflow:
1240
+    av_log(ac->avctx, AV_LOG_ERROR,
1241
+           "Read beyond end of ff_aac_codebook_vectors[%d][]. index %d >= %d\n",
1242
+           band_type[idx], err_idx, ff_aac_spectral_sizes[band_type[idx]]);
1243
+    return -1;
1244
+}
1245
+
1246
+static av_always_inline float flt16_round(float pf)
1247
+{
1248
+    union float754 tmp;
1249
+    tmp.f = pf;
1250
+    tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1251
+    return tmp.f;
1252
+}
1253
+
1254
+static av_always_inline float flt16_even(float pf)
1255
+{
1256
+    union float754 tmp;
1257
+    tmp.f = pf;
1258
+    tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1259
+    return tmp.f;
1260
+}
1261
+
1262
+static av_always_inline float flt16_trunc(float pf)
1263
+{
1264
+    union float754 pun;
1265
+    pun.f = pf;
1266
+    pun.i &= 0xFFFF0000U;
1267
+    return pun.f;
1268
+}
1269
+
1270
+static av_always_inline void predict(AACContext *ac, PredictorState *ps, float *coef,
1271
+                    int output_enable)
1272
+{
1273
+    const float a     = 0.953125; // 61.0 / 64
1274
+    const float alpha = 0.90625;  // 29.0 / 32
1275
+    float e0, e1;
1276
+    float pv;
1277
+    float k1, k2;
1278
+
1279
+    k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
1280
+    k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
1281
+
1282
+    pv = flt16_round(k1 * ps->r0 + k2 * ps->r1);
1283
+    if (output_enable)
1284
+        *coef += pv * ac->sf_scale;
1285
+
1286
+    e0 = *coef / ac->sf_scale;
1287
+    e1 = e0 - k1 * ps->r0;
1288
+
1289
+    ps->cor1 = flt16_trunc(alpha * ps->cor1 + ps->r1 * e1);
1290
+    ps->var1 = flt16_trunc(alpha * ps->var1 + 0.5 * (ps->r1 * ps->r1 + e1 * e1));
1291
+    ps->cor0 = flt16_trunc(alpha * ps->cor0 + ps->r0 * e0);
1292
+    ps->var0 = flt16_trunc(alpha * ps->var0 + 0.5 * (ps->r0 * ps->r0 + e0 * e0));
1293
+
1294
+    ps->r1 = flt16_trunc(a * (ps->r0 - k1 * e0));
1295
+    ps->r0 = flt16_trunc(a * e0);
1296
+}
1297
+
1298
+/**
1299
+ * Apply AAC-Main style frequency domain prediction.
1300
+ */
1301
+static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1302
+{
1303
+    int sfb, k;
1304
+
1305
+    if (!sce->ics.predictor_initialized) {
1306
+        reset_all_predictors(sce->predictor_state);
1307
+        sce->ics.predictor_initialized = 1;
1308
+    }
1309
+
1310
+    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1311
+        for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->m4ac.sampling_index]; sfb++) {
1312
+            for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
1313
+                predict(ac, &sce->predictor_state[k], &sce->coeffs[k],
1314
+                        sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
1315
+            }
1316
+        }
1317
+        if (sce->ics.predictor_reset_group)
1318
+            reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
1319
+    } else
1320
+        reset_all_predictors(sce->predictor_state);
1321
+}
1322
+
1323
+/**
1324
+ * Decode an individual_channel_stream payload; reference: table 4.44.
1325
+ *
1326
+ * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
1327
+ * @param   scale_flag      scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1328
+ *
1329
+ * @return  Returns error status. 0 - OK, !0 - error
1330
+ */
1331
+static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1332
+                      GetBitContext *gb, int common_window, int scale_flag)
1333
+{
1334
+    Pulse pulse;
1335
+    TemporalNoiseShaping    *tns = &sce->tns;
1336
+    IndividualChannelStream *ics = &sce->ics;
1337
+    float *out = sce->coeffs;
1338
+    int global_gain, pulse_present = 0;
1339
+
1340
+    /* This assignment is to silence a GCC warning about the variable being used
1341
+     * uninitialized when in fact it always is.
1342
+     */
1343
+    pulse.num_pulse = 0;
1344
+
1345
+    global_gain = get_bits(gb, 8);
1346
+
1347
+    if (!common_window && !scale_flag) {
1348
+        if (decode_ics_info(ac, ics, gb, 0) < 0)
1349
+            return -1;
1350
+    }
1351
+
1352
+    if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
1353
+        return -1;
1354
+    if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
1355
+        return -1;
1356
+
1357
+    pulse_present = 0;
1358
+    if (!scale_flag) {
1359
+        if ((pulse_present = get_bits1(gb))) {
1360
+            if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1361
+                av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
1362
+                return -1;
1363
+            }
1364
+            if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1365
+                av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
1366
+                return -1;
1367
+            }
1368
+        }
1369
+        if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1370
+            return -1;
1371
+        if (get_bits1(gb)) {
1372
+            av_log_missing_feature(ac->avctx, "SSR", 1);
1373
+            return -1;
1374
+        }
1375
+    }
1376
+
1377
+    if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
1378
+        return -1;
1379
+
1380
+    if (ac->m4ac.object_type == AOT_AAC_MAIN && !common_window)
1381
+        apply_prediction(ac, sce);
1382
+
1383
+    return 0;
1384
+}
1385
+
1386
+/**
1387
+ * Mid/Side stereo decoding; reference: 4.6.8.1.3.
1388
+ */
1389
+static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
1390
+{
1391
+    const IndividualChannelStream *ics = &cpe->ch[0].ics;
1392
+    float *ch0 = cpe->ch[0].coeffs;
1393
+    float *ch1 = cpe->ch[1].coeffs;
1394
+    int g, i, group, idx = 0;
1395
+    const uint16_t *offsets = ics->swb_offset;
1396
+    for (g = 0; g < ics->num_window_groups; g++) {
1397
+        for (i = 0; i < ics->max_sfb; i++, idx++) {
1398
+            if (cpe->ms_mask[idx] &&
1399
+                    cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
1400
+                for (group = 0; group < ics->group_len[g]; group++) {
1401
+                    ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
1402
+                                              ch1 + group * 128 + offsets[i],
1403
+                                              offsets[i+1] - offsets[i]);
1404
+                }
1405
+            }
1406
+        }
1407
+        ch0 += ics->group_len[g] * 128;
1408
+        ch1 += ics->group_len[g] * 128;
1409
+    }
1410
+}
1411
+
1412
+/**
1413
+ * intensity stereo decoding; reference: 4.6.8.2.3
1414
+ *
1415
+ * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
1416
+ *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
1417
+ *                      [3] reserved for scalable AAC
1418
+ */
1419
+static void apply_intensity_stereo(ChannelElement *cpe, int ms_present)
1420
+{
1421
+    const IndividualChannelStream *ics = &cpe->ch[1].ics;
1422
+    SingleChannelElement         *sce1 = &cpe->ch[1];
1423
+    float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1424
+    const uint16_t *offsets = ics->swb_offset;
1425
+    int g, group, i, k, idx = 0;
1426
+    int c;
1427
+    float scale;
1428
+    for (g = 0; g < ics->num_window_groups; g++) {
1429
+        for (i = 0; i < ics->max_sfb;) {
1430
+            if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
1431
+                const int bt_run_end = sce1->band_type_run_end[idx];
1432
+                for (; i < bt_run_end; i++, idx++) {
1433
+                    c = -1 + 2 * (sce1->band_type[idx] - 14);
1434
+                    if (ms_present)
1435
+                        c *= 1 - 2 * cpe->ms_mask[idx];
1436
+                    scale = c * sce1->sf[idx];
1437
+                    for (group = 0; group < ics->group_len[g]; group++)
1438
+                        for (k = offsets[i]; k < offsets[i + 1]; k++)
1439
+                            coef1[group * 128 + k] = scale * coef0[group * 128 + k];
1440
+                }
1441
+            } else {
1442
+                int bt_run_end = sce1->band_type_run_end[idx];
1443
+                idx += bt_run_end - i;
1444
+                i    = bt_run_end;
1445
+            }
1446
+        }
1447
+        coef0 += ics->group_len[g] * 128;
1448
+        coef1 += ics->group_len[g] * 128;
1449
+    }
1450
+}
1451
+
1452
+/**
1453
+ * Decode a channel_pair_element; reference: table 4.4.
1454
+ *
1455
+ * @param   elem_id Identifies the instance of a syntax element.
1456
+ *
1457
+ * @return  Returns error status. 0 - OK, !0 - error
1458
+ */
1459
+static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
1460
+{
1461
+    int i, ret, common_window, ms_present = 0;
1462
+
1463
+    common_window = get_bits1(gb);
1464
+    if (common_window) {
1465
+        if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
1466
+            return -1;
1467
+        i = cpe->ch[1].ics.use_kb_window[0];
1468
+        cpe->ch[1].ics = cpe->ch[0].ics;
1469
+        cpe->ch[1].ics.use_kb_window[1] = i;
1470
+        ms_present = get_bits(gb, 2);
1471
+        if (ms_present == 3) {
1472
+            av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1473
+            return -1;
1474
+        } else if (ms_present)
1475
+            decode_mid_side_stereo(cpe, gb, ms_present);
1476
+    }
1477
+    if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1478
+        return ret;
1479
+    if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1480
+        return ret;
1481
+
1482
+    if (common_window) {
1483
+        if (ms_present)
1484
+            apply_mid_side_stereo(ac, cpe);
1485
+        if (ac->m4ac.object_type == AOT_AAC_MAIN) {
1486
+            apply_prediction(ac, &cpe->ch[0]);
1487
+            apply_prediction(ac, &cpe->ch[1]);
1488
+        }
1489
+    }
1490
+
1491
+    apply_intensity_stereo(cpe, ms_present);
1492
+    return 0;
1493
+}
1494
+
1495
+/**
1496
+ * Decode coupling_channel_element; reference: table 4.8.
1497
+ *
1498
+ * @param   elem_id Identifies the instance of a syntax element.
1499
+ *
1500
+ * @return  Returns error status. 0 - OK, !0 - error
1501
+ */
1502
+static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
1503
+{
1504
+    int num_gain = 0;
1505
+    int c, g, sfb, ret;
1506
+    int sign;
1507
+    float scale;
1508
+    SingleChannelElement *sce = &che->ch[0];
1509
+    ChannelCoupling     *coup = &che->coup;
1510
+
1511
+    coup->coupling_point = 2 * get_bits1(gb);
1512
+    coup->num_coupled = get_bits(gb, 3);
1513
+    for (c = 0; c <= coup->num_coupled; c++) {
1514
+        num_gain++;
1515
+        coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1516
+        coup->id_select[c] = get_bits(gb, 4);
1517
+        if (coup->type[c] == TYPE_CPE) {
1518
+            coup->ch_select[c] = get_bits(gb, 2);
1519
+            if (coup->ch_select[c] == 3)
1520
+                num_gain++;
1521
+        } else
1522
+            coup->ch_select[c] = 2;
1523
+    }
1524
+    coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1525
+
1526
+    sign  = get_bits(gb, 1);
1527
+    scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));
1528
+
1529
+    if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1530
+        return ret;
1531
+
1532
+    for (c = 0; c < num_gain; c++) {
1533
+        int idx  = 0;
1534
+        int cge  = 1;
1535
+        int gain = 0;
1536
+        float gain_cache = 1.;
1537
+        if (c) {
1538
+            cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1539
+            gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1540
+            gain_cache = pow(scale, -gain);
1541
+        }
1542
+        if (coup->coupling_point == AFTER_IMDCT) {
1543
+            coup->gain[c][0] = gain_cache;
1544
+        } else {
1545
+            for (g = 0; g < sce->ics.num_window_groups; g++) {
1546
+                for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1547
+                    if (sce->band_type[idx] != ZERO_BT) {
1548
+                        if (!cge) {
1549
+                            int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1550
+                            if (t) {
1551
+                                int s = 1;
1552
+                                t = gain += t;
1553
+                                if (sign) {
1554
+                                    s  -= 2 * (t & 0x1);
1555
+                                    t >>= 1;
1556
+                                }
1557
+                                gain_cache = pow(scale, -t) * s;
1558
+                            }
1559
+                        }
1560
+                        coup->gain[c][idx] = gain_cache;
1561
+                    }
1562
+                }
1563
+            }
1564
+        }
1565
+    }
1566
+    return 0;
1567
+}
1568
+
1569
+/**
1570
+ * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1571
+ *
1572
+ * @return  Returns number of bytes consumed.
1573
+ */
1574
+static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1575
+                                         GetBitContext *gb)
1576
+{
1577
+    int i;
1578
+    int num_excl_chan = 0;
1579
+
1580
+    do {
1581
+        for (i = 0; i < 7; i++)
1582
+            che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1583
+    } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1584
+
1585
+    return num_excl_chan / 7;
1586
+}
1587
+
1588
+/**
1589
+ * Decode dynamic range information; reference: table 4.52.
1590
+ *
1591
+ * @param   cnt length of TYPE_FIL syntactic element in bytes
1592
+ *
1593
+ * @return  Returns number of bytes consumed.
1594
+ */
1595
+static int decode_dynamic_range(DynamicRangeControl *che_drc,
1596
+                                GetBitContext *gb, int cnt)
1597
+{
1598
+    int n             = 1;
1599
+    int drc_num_bands = 1;
1600
+    int i;
1601
+
1602
+    /* pce_tag_present? */
1603
+    if (get_bits1(gb)) {
1604
+        che_drc->pce_instance_tag  = get_bits(gb, 4);
1605
+        skip_bits(gb, 4); // tag_reserved_bits
1606
+        n++;
1607
+    }
1608
+
1609
+    /* excluded_chns_present? */
1610
+    if (get_bits1(gb)) {
1611
+        n += decode_drc_channel_exclusions(che_drc, gb);
1612
+    }
1613
+
1614
+    /* drc_bands_present? */
1615
+    if (get_bits1(gb)) {
1616
+        che_drc->band_incr            = get_bits(gb, 4);
1617
+        che_drc->interpolation_scheme = get_bits(gb, 4);
1618
+        n++;
1619
+        drc_num_bands += che_drc->band_incr;
1620
+        for (i = 0; i < drc_num_bands; i++) {
1621
+            che_drc->band_top[i] = get_bits(gb, 8);
1622
+            n++;
1623
+        }
1624
+    }
1625
+
1626
+    /* prog_ref_level_present? */
1627
+    if (get_bits1(gb)) {
1628
+        che_drc->prog_ref_level = get_bits(gb, 7);
1629
+        skip_bits1(gb); // prog_ref_level_reserved_bits
1630
+        n++;
1631
+    }
1632
+
1633
+    for (i = 0; i < drc_num_bands; i++) {
1634
+        che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1635
+        che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1636
+        n++;
1637
+    }
1638
+
1639
+    return n;
1640
+}
1641
+
1642
+/**
1643
+ * Decode extension data (incomplete); reference: table 4.51.
1644
+ *
1645
+ * @param   cnt length of TYPE_FIL syntactic element in bytes
1646
+ *
1647
+ * @return Returns number of bytes consumed
1648
+ */
1649
+static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
1650
+                                    ChannelElement *che, enum RawDataBlockType elem_type)
1651
+{
1652
+    int crc_flag = 0;
1653
+    int res = cnt;
1654
+    switch (get_bits(gb, 4)) { // extension type
1655
+    case EXT_SBR_DATA_CRC:
1656
+        crc_flag++;
1657
+    case EXT_SBR_DATA:
1658
+        if (!che) {
1659
+            av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1660
+            return res;
1661
+        } else if (!ac->m4ac.sbr) {
1662
+            av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1663
+            skip_bits_long(gb, 8 * cnt - 4);
1664
+            return res;
1665
+        } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
1666
+            av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1667
+            skip_bits_long(gb, 8 * cnt - 4);
1668
+            return res;
1669
+        } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
1670
+            ac->m4ac.sbr = 1;
1671
+            ac->m4ac.ps = 1;
1672
+            output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
1673
+        } else {
1674
+            ac->m4ac.sbr = 1;
1675
+        }
1676
+        res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
1677
+        break;
1678
+    case EXT_DYNAMIC_RANGE:
1679
+        res = decode_dynamic_range(&ac->che_drc, gb, cnt);
1680
+        break;
1681
+    case EXT_FILL:
1682
+    case EXT_FILL_DATA:
1683
+    case EXT_DATA_ELEMENT:
1684
+    default:
1685
+        skip_bits_long(gb, 8 * cnt - 4);
1686
+        break;
1687
+    };
1688
+    return res;
1689
+}
1690
+
1691
+/**
1692
+ * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
1693
+ *
1694
+ * @param   decode  1 if tool is used normally, 0 if tool is used in LTP.
1695
+ * @param   coef    spectral coefficients
1696
+ */
1697
+static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
1698
+                      IndividualChannelStream *ics, int decode)
1699
+{
1700
+    const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
1701
+    int w, filt, m, i;
1702
+    int bottom, top, order, start, end, size, inc;
1703
+    float lpc[TNS_MAX_ORDER];
1704
+
1705
+    for (w = 0; w < ics->num_windows; w++) {
1706
+        bottom = ics->num_swb;
1707
+        for (filt = 0; filt < tns->n_filt[w]; filt++) {
1708
+            top    = bottom;
1709
+            bottom = FFMAX(0, top - tns->length[w][filt]);
1710
+            order  = tns->order[w][filt];
1711
+            if (order == 0)
1712
+                continue;
1713
+
1714
+            // tns_decode_coef
1715
+            compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
1716
+
1717
+            start = ics->swb_offset[FFMIN(bottom, mmm)];
1718
+            end   = ics->swb_offset[FFMIN(   top, mmm)];
1719
+            if ((size = end - start) <= 0)
1720
+                continue;
1721
+            if (tns->direction[w][filt]) {
1722
+                inc = -1;
1723
+                start = end - 1;
1724
+            } else {
1725
+                inc = 1;
1726
+            }
1727
+            start += w * 128;
1728
+
1729
+            // ar filter
1730
+            for (m = 0; m < size; m++, start += inc)
1731
+                for (i = 1; i <= FFMIN(m, order); i++)
1732
+                    coef[start] -= coef[start - i * inc] * lpc[i - 1];
1733
+        }
1734
+    }
1735
+}
1736
+
1737
+/**
1738
+ * Conduct IMDCT and windowing.
1739
+ */
1740
+static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce, float bias)
1741
+{
1742
+    IndividualChannelStream *ics = &sce->ics;
1743
+    float *in    = sce->coeffs;
1744
+    float *out   = sce->ret;
1745
+    float *saved = sce->saved;
1746
+    const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1747
+    const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1748
+    const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
1749
+    float *buf  = ac->buf_mdct;
1750
+    float *temp = ac->temp;
1751
+    int i;
1752
+
1753
+    // imdct
1754
+    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1755
+        if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
1756
+            av_log(ac->avctx, AV_LOG_WARNING,
1757
+                   "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
1758
+                   "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
1759
+        for (i = 0; i < 1024; i += 128)
1760
+            ff_imdct_half(&ac->mdct_small, buf + i, in + i);
1761
+    } else
1762
+        ff_imdct_half(&ac->mdct, buf, in);
1763
+
1764
+    /* window overlapping
1765
+     * NOTE: To simplify the overlapping code, all 'meaningless' short to long
1766
+     * and long to short transitions are considered to be short to short
1767
+     * transitions. This leaves just two cases (long to long and short to short)
1768
+     * with a little special sauce for EIGHT_SHORT_SEQUENCE.
1769
+     */
1770
+    if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
1771
+            (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
1772
+        ac->dsp.vector_fmul_window(    out,               saved,            buf,         lwindow_prev, bias, 512);
1773
+    } else {
1774
+        for (i = 0; i < 448; i++)
1775
+            out[i] = saved[i] + bias;
1776
+
1777
+        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1778
+            ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, bias, 64);
1779
+            ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      bias, 64);
1780
+            ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      bias, 64);
1781
+            ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      bias, 64);
1782
+            ac->dsp.vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      bias, 64);
1783
+            memcpy(                    out + 448 + 4*128, temp, 64 * sizeof(float));
1784
+        } else {
1785
+            ac->dsp.vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, bias, 64);
1786
+            for (i = 576; i < 1024; i++)
1787
+                out[i] = buf[i-512] + bias;
1788
+        }
1789
+    }
1790
+
1791
+    // buffer update
1792
+    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1793
+        for (i = 0; i < 64; i++)
1794
+            saved[i] = temp[64 + i] - bias;
1795
+        ac->dsp.vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 0, 64);
1796
+        ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 0, 64);
1797
+        ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 0, 64);
1798
+        memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
1799
+    } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
1800
+        memcpy(                    saved,       buf + 512,        448 * sizeof(float));
1801
+        memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
1802
+    } else { // LONG_STOP or ONLY_LONG
1803
+        memcpy(                    saved,       buf + 512,        512 * sizeof(float));
1804
+    }
1805
+}
1806
+
1807
+/**
1808
+ * Apply dependent channel coupling (applied before IMDCT).
1809
+ *
1810
+ * @param   index   index into coupling gain array
1811
+ */
1812
+static void apply_dependent_coupling(AACContext *ac,
1813
+                                     SingleChannelElement *target,
1814
+                                     ChannelElement *cce, int index)
1815
+{
1816
+    IndividualChannelStream *ics = &cce->ch[0].ics;
1817
+    const uint16_t *offsets = ics->swb_offset;
1818
+    float *dest = target->coeffs;
1819
+    const float *src = cce->ch[0].coeffs;
1820
+    int g, i, group, k, idx = 0;
1821
+    if (ac->m4ac.object_type == AOT_AAC_LTP) {
1822
+        av_log(ac->avctx, AV_LOG_ERROR,
1823
+               "Dependent coupling is not supported together with LTP\n");
1824
+        return;
1825
+    }
1826
+    for (g = 0; g < ics->num_window_groups; g++) {
1827
+        for (i = 0; i < ics->max_sfb; i++, idx++) {
1828
+            if (cce->ch[0].band_type[idx] != ZERO_BT) {
1829
+                const float gain = cce->coup.gain[index][idx];
1830
+                for (group = 0; group < ics->group_len[g]; group++) {
1831
+                    for (k = offsets[i]; k < offsets[i + 1]; k++) {
1832
+                        // XXX dsputil-ize
1833
+                        dest[group * 128 + k] += gain * src[group * 128 + k];
1834
+                    }
1835
+                }
1836
+            }
1837
+        }
1838
+        dest += ics->group_len[g] * 128;
1839
+        src  += ics->group_len[g] * 128;
1840
+    }
1841
+}
1842
+
1843
+/**
1844
+ * Apply independent channel coupling (applied after IMDCT).
1845
+ *
1846
+ * @param   index   index into coupling gain array
1847
+ */
1848
+static void apply_independent_coupling(AACContext *ac,
1849
+                                       SingleChannelElement *target,
1850
+                                       ChannelElement *cce, int index)
1851
+{
1852
+    int i;
1853
+    const float gain = cce->coup.gain[index][0];
1854
+    const float bias = ac->add_bias;
1855
+    const float *src = cce->ch[0].ret;
1856
+    float *dest = target->ret;
1857
+    const int len = 1024 << (ac->m4ac.sbr == 1);
1858
+
1859
+    for (i = 0; i < len; i++)
1860
+        dest[i] += gain * (src[i] - bias);
1861
+}
1862
+
1863
+/**
1864
+ * channel coupling transformation interface
1865
+ *
1866
+ * @param   index   index into coupling gain array
1867
+ * @param   apply_coupling_method   pointer to (in)dependent coupling function
1868
+ */
1869
+static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
1870
+                                   enum RawDataBlockType type, int elem_id,
1871
+                                   enum CouplingPoint coupling_point,
1872
+                                   void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
1873
+{
1874
+    int i, c;
1875
+
1876
+    for (i = 0; i < MAX_ELEM_ID; i++) {
1877
+        ChannelElement *cce = ac->che[TYPE_CCE][i];
1878
+        int index = 0;
1879
+
1880
+        if (cce && cce->coup.coupling_point == coupling_point) {
1881
+            ChannelCoupling *coup = &cce->coup;
1882
+
1883
+            for (c = 0; c <= coup->num_coupled; c++) {
1884
+                if (coup->type[c] == type && coup->id_select[c] == elem_id) {
1885
+                    if (coup->ch_select[c] != 1) {
1886
+                        apply_coupling_method(ac, &cc->ch[0], cce, index);
1887
+                        if (coup->ch_select[c] != 0)
1888
+                            index++;
1889
+                    }
1890
+                    if (coup->ch_select[c] != 2)
1891
+                        apply_coupling_method(ac, &cc->ch[1], cce, index++);
1892
+                } else
1893
+                    index += 1 + (coup->ch_select[c] == 3);
1894
+            }
1895
+        }
1896
+    }
1897
+}
1898
+
1899
+/**
1900
+ * Convert spectral data to float samples, applying all supported tools as appropriate.
1901
+ */
1902
+static void spectral_to_sample(AACContext *ac)
1903
+{
1904
+    int i, type;
1905
+    float imdct_bias = (ac->m4ac.sbr <= 0) ? ac->add_bias : 0.0f;
1906
+    for (type = 3; type >= 0; type--) {
1907
+        for (i = 0; i < MAX_ELEM_ID; i++) {
1908
+            ChannelElement *che = ac->che[type][i];
1909
+            if (che) {
1910
+                if (type <= TYPE_CPE)
1911
+                    apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
1912
+                if (che->ch[0].tns.present)
1913
+                    apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
1914
+                if (che->ch[1].tns.present)
1915
+                    apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
1916
+                if (type <= TYPE_CPE)
1917
+                    apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
1918
+                if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
1919
+                    imdct_and_windowing(ac, &che->ch[0], imdct_bias);
1920
+                    if (type == TYPE_CPE) {
1921
+                        imdct_and_windowing(ac, &che->ch[1], imdct_bias);
1922
+                    }
1923
+                    if (ac->m4ac.sbr > 0) {
1924
+                        ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
1925
+                    }
1926
+                }
1927
+                if (type <= TYPE_CCE)
1928
+                    apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
1929
+            }
1930
+        }
1931
+    }
1932
+}
1933
+
1934
+static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
1935
+{
1936
+    int size;
1937
+    AACADTSHeaderInfo hdr_info;
1938
+
1939
+    size = ff_aac_parse_header(gb, &hdr_info);
1940
+    if (size > 0) {
1941
+        if (ac->output_configured != OC_LOCKED && hdr_info.chan_config) {
1942
+            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
1943
+            memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
1944
+            ac->m4ac.chan_config = hdr_info.chan_config;
1945
+            if (set_default_channel_config(ac, new_che_pos, hdr_info.chan_config))
1946
+                return -7;
1947
+            if (output_configure(ac, ac->che_pos, new_che_pos, hdr_info.chan_config, OC_TRIAL_FRAME))
1948
+                return -7;
1949
+        } else if (ac->output_configured != OC_LOCKED) {
1950
+            ac->output_configured = OC_NONE;
1951
+        }
1952
+        if (ac->output_configured != OC_LOCKED) {
1953
+            ac->m4ac.sbr = -1;
1954
+            ac->m4ac.ps  = -1;
1955
+        }
1956
+        ac->m4ac.sample_rate     = hdr_info.sample_rate;
1957
+        ac->m4ac.sampling_index  = hdr_info.sampling_index;
1958
+        ac->m4ac.object_type     = hdr_info.object_type;
1959
+        if (!ac->avctx->sample_rate)
1960
+            ac->avctx->sample_rate = hdr_info.sample_rate;
1961
+        if (hdr_info.num_aac_frames == 1) {
1962
+            if (!hdr_info.crc_absent)
1963
+                skip_bits(gb, 16);
1964
+        } else {
1965
+            av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
1966
+            return -1;
1967
+        }
1968
+    }
1969
+    return size;
1970
+}
1971
+
1972
+static int aac_decode_frame(AVCodecContext *avctx, void *data,
1973
+                            int *data_size, AVPacket *avpkt)
1974
+{
1975
+    const uint8_t *buf = avpkt->data;
1976
+    int buf_size = avpkt->size;
1977
+    AACContext *ac = avctx->priv_data;
1978
+    ChannelElement *che = NULL, *che_prev = NULL;
1979
+    GetBitContext gb;
1980
+    enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
1981
+    int err, elem_id, data_size_tmp;
1982
+    int buf_consumed;
1983
+    int samples = 0, multiplier;
1984
+    int buf_offset;
1985
+
1986
+    init_get_bits(&gb, buf, buf_size * 8);
1987
+
1988
+    if (show_bits(&gb, 12) == 0xfff) {
1989
+        if (parse_adts_frame_header(ac, &gb) < 0) {
1990
+            av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
1991
+            return -1;
1992
+        }
1993
+        if (ac->m4ac.sampling_index > 12) {
1994
+            av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
1995
+            return -1;
1996
+        }
1997
+    }
1998
+
1999
+    memset(ac->tags_seen_this_frame, 0, sizeof(ac->tags_seen_this_frame));
2000
+    // parse
2001
+    while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
2002
+        elem_id = get_bits(&gb, 4);
2003
+
2004
+        if (elem_type < TYPE_DSE) {
2005
+            if (!(che=get_che(ac, elem_type, elem_id))) {
2006
+                av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2007
+                       elem_type, elem_id);
2008
+                return -1;
2009
+            }
2010
+            samples = 1024;
2011
+        }
2012
+
2013
+        switch (elem_type) {
2014
+
2015
+        case TYPE_SCE:
2016
+            err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
2017
+            break;
2018
+
2019
+        case TYPE_CPE:
2020
+            err = decode_cpe(ac, &gb, che);
2021
+            break;
2022
+
2023
+        case TYPE_CCE:
2024
+            err = decode_cce(ac, &gb, che);
2025
+            break;
2026
+
2027
+        case TYPE_LFE:
2028
+            err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
2029
+            break;
2030
+
2031
+        case TYPE_DSE:
2032
+            err = skip_data_stream_element(ac, &gb);
2033
+            break;
2034
+
2035
+        case TYPE_PCE: {
2036
+            enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
2037
+            memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
2038
+            if ((err = decode_pce(ac, new_che_pos, &gb)))
2039
+                break;
2040
+            if (ac->output_configured > OC_TRIAL_PCE)
2041
+                av_log(avctx, AV_LOG_ERROR,
2042
+                       "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2043
+            else
2044
+                err = output_configure(ac, ac->che_pos, new_che_pos, 0, OC_TRIAL_PCE);
2045
+            break;
2046
+        }
2047
+
2048
+        case TYPE_FIL:
2049
+            if (elem_id == 15)
2050
+                elem_id += get_bits(&gb, 8) - 1;
2051
+            if (get_bits_left(&gb) < 8 * elem_id) {
2052
+                    av_log(avctx, AV_LOG_ERROR, overread_err);
2053
+                    return -1;
2054
+            }
2055
+            while (elem_id > 0)
2056
+                elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
2057
+            err = 0; /* FIXME */
2058
+            break;
2059
+
2060
+        default:
2061
+            err = -1; /* should not happen, but keeps compiler happy */
2062
+            break;
2063
+        }
2064
+
2065
+        che_prev       = che;
2066
+        elem_type_prev = elem_type;
2067
+
2068
+        if (err)
2069
+            return err;
2070
+
2071
+        if (get_bits_left(&gb) < 3) {
2072
+            av_log(avctx, AV_LOG_ERROR, overread_err);
2073
+            return -1;
2074
+        }
2075
+    }
2076
+
2077
+    spectral_to_sample(ac);
2078
+
2079
+    multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
2080
+    samples <<= multiplier;
2081
+    if (ac->output_configured < OC_LOCKED) {
2082
+        avctx->sample_rate = ac->m4ac.sample_rate << multiplier;
2083
+        avctx->frame_size = samples;
2084
+    }
2085
+
2086
+    data_size_tmp = samples * avctx->channels * sizeof(int16_t);
2087
+    if (*data_size < data_size_tmp) {
2088
+        av_log(avctx, AV_LOG_ERROR,
2089
+               "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
2090
+               *data_size, data_size_tmp);
2091
+        return -1;
2092
+    }
2093
+    *data_size = data_size_tmp;
2094
+
2095
+    if (samples)
2096
+        ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avctx->channels);
2097
+
2098
+    if (ac->output_configured)
2099
+        ac->output_configured = OC_LOCKED;
2100
+
2101
+    buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2102
+    for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2103
+        if (buf[buf_offset])
2104
+            break;
2105
+
2106
+    return buf_size > buf_offset ? buf_consumed : buf_size;
2107
+}
2108
+
2109
+static av_cold int aac_decode_close(AVCodecContext *avctx)
2110
+{
2111
+    AACContext *ac = avctx->priv_data;
2112
+    int i, type;
2113
+
2114
+    for (i = 0; i < MAX_ELEM_ID; i++) {
2115
+        for (type = 0; type < 4; type++) {
2116
+            if (ac->che[type][i])
2117
+                ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2118
+            av_freep(&ac->che[type][i]);
2119
+        }
2120
+    }
2121
+
2122
+    ff_mdct_end(&ac->mdct);
2123
+    ff_mdct_end(&ac->mdct_small);
2124
+    return 0;
2125
+}
2126
+
2127
+AVCodec aac_decoder = {
2128
+    "aac",
2129
+    AVMEDIA_TYPE_AUDIO,
2130
+    CODEC_ID_AAC,
2131
+    sizeof(AACContext),
2132
+    aac_decode_init,
2133
+    NULL,
2134
+    aac_decode_close,
2135
+    aac_decode_frame,
2136
+    .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
2137
+    .sample_fmts = (const enum SampleFormat[]) {
2138
+        SAMPLE_FMT_S16,SAMPLE_FMT_NONE
2139
+    },
2140
+    .channel_layouts = aac_channel_layout,
2141
+};
... ...
@@ -201,13 +201,11 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
201 201
     lengths[1] = ff_aac_num_swb_128[i];
202 202
     ff_psy_init(&s->psy, avctx, 2, sizes, lengths);
203 203
     s->psypp = ff_psy_preprocess_init(avctx);
204
-    s->coder = &ff_aac_coders[0];
204
+    s->coder = &ff_aac_coders[2];
205 205
 
206 206
     s->lambda = avctx->global_quality ? avctx->global_quality : 120;
207
-#if !CONFIG_HARDCODED_TABLES
208
-    for (i = 0; i < 428; i++)
209
-        ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
210
-#endif /* CONFIG_HARDCODED_TABLES */
207
+
208
+    ff_aac_tableinit();
211 209
 
212 210
     if (avctx->channels > 5)
213 211
         av_log(avctx, AV_LOG_ERROR, "This encoder does not yet enforce the restrictions on LFEs. "
... ...
@@ -234,25 +232,21 @@ static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
234 234
                 s->output[i] = sce->saved[i];
235 235
         }
236 236
         if (sce->ics.window_sequence[0] != LONG_START_SEQUENCE) {
237
-            j = channel;
238
-            for (i = 0; i < 1024; i++, j += avctx->channels) {
237
+            for (i = 0, j = channel; i < 1024; i++, j += avctx->channels) {
239 238
                 s->output[i+1024]         = audio[j] * lwindow[1024 - i - 1];
240 239
                 sce->saved[i] = audio[j] * lwindow[i];
241 240
             }
242 241
         } else {
243
-            j = channel;
244
-            for (i = 0; i < 448; i++, j += avctx->channels)
242
+            for (i = 0, j = channel; i < 448; i++, j += avctx->channels)
245 243
                 s->output[i+1024]         = audio[j];
246
-            for (i = 448; i < 576; i++, j += avctx->channels)
244
+            for (; i < 576; i++, j += avctx->channels)
247 245
                 s->output[i+1024]         = audio[j] * swindow[576 - i - 1];
248 246
             memset(s->output+1024+576, 0, sizeof(s->output[0]) * 448);
249
-            j = channel;
250
-            for (i = 0; i < 1024; i++, j += avctx->channels)
247
+            for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
251 248
                 sce->saved[i] = audio[j];
252 249
         }
253 250
         ff_mdct_calc(&s->mdct1024, sce->coeffs, s->output);
254 251
     } else {
255
-        j = channel;
256 252
         for (k = 0; k < 1024; k += 128) {
257 253
             for (i = 448 + k; i < 448 + k + 256; i++)
258 254
                 s->output[i - 448 - k] = (i < 1024)
... ...
@@ -262,8 +256,7 @@ static void apply_window_and_mdct(AVCodecContext *avctx, AACEncContext *s,
262 262
             s->dsp.vector_fmul_reverse(s->output+128, s->output+128, swindow, 128);
263 263
             ff_mdct_calc(&s->mdct128, sce->coeffs + k, s->output);
264 264
         }
265
-        j = channel;
266
-        for (i = 0; i < 1024; i++, j += avctx->channels)
265
+        for (i = 0, j = channel; i < 1024; i++, j += avctx->channels)
267 266
             sce->saved[i] = audio[j];
268 267
     }
269 268
 }
... ...
@@ -562,6 +555,7 @@ static int aac_encode_frame(AVCodecContext *avctx,
562 562
             cpe      = &s->cpe[i];
563 563
             for (j = 0; j < chans; j++) {
564 564
                 s->cur_channel = start_ch + j;
565
+                ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
565 566
                 s->coder->search_for_quantizers(avctx, s, &cpe->ch[j], s->lambda);
566 567
             }
567 568
             cpe->common_window = 0;
... ...
@@ -592,7 +586,6 @@ static int aac_encode_frame(AVCodecContext *avctx,
592 592
             }
593 593
             for (j = 0; j < chans; j++) {
594 594
                 s->cur_channel = start_ch + j;
595
-                ff_psy_set_band_info(&s->psy, s->cur_channel, cpe->ch[j].coeffs, &wi[j]);
596 595
                 encode_individual_channel(avctx, s, &cpe->ch[j], cpe->common_window);
597 596
             }
598 597
             start_ch += chans;
... ...
@@ -64,7 +64,7 @@ typedef struct AACEncContext {
64 64
     int cur_channel;
65 65
     int last_frame;
66 66
     float lambda;
67
-    DECLARE_ALIGNED(16, int,   qcoefs)[96][2];   ///< quantized coefficients
67
+    DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
68 68
     DECLARE_ALIGNED(16, float, scoefs)[1024];    ///< scaled coefficients
69 69
 } AACEncContext;
70 70
 
71 71
new file mode 100644
... ...
@@ -0,0 +1,1037 @@
0
+/*
1
+ * MPEG-4 Parametric Stereo decoding functions
2
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdint.h>
22
+#include "libavutil/common.h"
23
+#include "libavutil/mathematics.h"
24
+#include "avcodec.h"
25
+#include "get_bits.h"
26
+#include "aacps.h"
27
+#include "aacps_tablegen.h"
28
+#include "aacpsdata.c"
29
+
30
+#define PS_BASELINE 0  //< Operate in Baseline PS mode
31
+                       //< Baseline implies 10 or 20 stereo bands,
32
+                       //< mixing mode A, and no ipd/opd
33
+
34
+#define numQMFSlots 32 //numTimeSlots * RATE
35
+
36
+static const int8_t num_env_tab[2][4] = {
37
+    { 0, 1, 2, 4, },
38
+    { 1, 2, 3, 4, },
39
+};
40
+
41
+static const int8_t nr_iidicc_par_tab[] = {
42
+    10, 20, 34, 10, 20, 34,
43
+};
44
+
45
+static const int8_t nr_iidopd_par_tab[] = {
46
+     5, 11, 17,  5, 11, 17,
47
+};
48
+
49
+enum {
50
+    huff_iid_df1,
51
+    huff_iid_dt1,
52
+    huff_iid_df0,
53
+    huff_iid_dt0,
54
+    huff_icc_df,
55
+    huff_icc_dt,
56
+    huff_ipd_df,
57
+    huff_ipd_dt,
58
+    huff_opd_df,
59
+    huff_opd_dt,
60
+};
61
+
62
+static const int huff_iid[] = {
63
+    huff_iid_df0,
64
+    huff_iid_df1,
65
+    huff_iid_dt0,
66
+    huff_iid_dt1,
67
+};
68
+
69
+static VLC vlc_ps[10];
70
+
71
+/**
72
+ * Read Inter-channel Intensity Difference/Inter-Channel Coherence/
73
+ * Inter-channel Phase Difference/Overall Phase Difference parameters from the
74
+ * bitstream.
75
+ *
76
+ * @param avctx contains the current codec context
77
+ * @param gb    pointer to the input bitstream
78
+ * @param ps    pointer to the Parametric Stereo context
79
+ * @param par   pointer to the parameter to be read
80
+ * @param e     envelope to decode
81
+ * @param dt    1: time delta-coded, 0: frequency delta-coded
82
+ */
83
+#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
84
+static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
85
+                        int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
86
+{ \
87
+    int b, num = ps->nr_ ## PAR ## _par; \
88
+    VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
89
+    if (dt) { \
90
+        int e_prev = e ? e - 1 : ps->num_env_old - 1; \
91
+        e_prev = FFMAX(e_prev, 0); \
92
+        for (b = 0; b < num; b++) { \
93
+            int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
94
+            if (MASK) val &= MASK; \
95
+            PAR[e][b] = val; \
96
+            if (ERR_CONDITION) \
97
+                goto err; \
98
+        } \
99
+    } else { \
100
+        int val = 0; \
101
+        for (b = 0; b < num; b++) { \
102
+            val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
103
+            if (MASK) val &= MASK; \
104
+            PAR[e][b] = val; \
105
+            if (ERR_CONDITION) \
106
+                goto err; \
107
+        } \
108
+    } \
109
+    return 0; \
110
+err: \
111
+    av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
112
+    return -1; \
113
+}
114
+
115
+READ_PAR_DATA(iid,    huff_offset[table_idx],    0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
116
+READ_PAR_DATA(icc,    huff_offset[table_idx],    0, ps->icc_par[e][b] > 7U)
117
+READ_PAR_DATA(ipdopd,                      0, 0x07, 0)
118
+
119
+static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
120
+{
121
+    int e;
122
+    int count = get_bits_count(gb);
123
+
124
+    if (ps_extension_id)
125
+        return 0;
126
+
127
+    ps->enable_ipdopd = get_bits1(gb);
128
+    if (ps->enable_ipdopd) {
129
+        for (e = 0; e < ps->num_env; e++) {
130
+            int dt = get_bits1(gb);
131
+            read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
132
+            dt = get_bits1(gb);
133
+            read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
134
+        }
135
+    }
136
+    skip_bits1(gb);      //reserved_ps
137
+    return get_bits_count(gb) - count;
138
+}
139
+
140
+static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist)
141
+{
142
+    int i;
143
+    for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
144
+        opd_hist[i] = 0;
145
+        ipd_hist[i] = 0;
146
+    }
147
+}
148
+
149
+int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
150
+{
151
+    int e;
152
+    int bit_count_start = get_bits_count(gb_host);
153
+    int header;
154
+    int bits_consumed;
155
+    GetBitContext gbc = *gb_host, *gb = &gbc;
156
+
157
+    header = get_bits1(gb);
158
+    if (header) {     //enable_ps_header
159
+        ps->enable_iid = get_bits1(gb);
160
+        if (ps->enable_iid) {
161
+            int iid_mode = get_bits(gb, 3);
162
+            if (iid_mode > 5) {
163
+                av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
164
+                       iid_mode);
165
+                goto err;
166
+            }
167
+            ps->nr_iid_par    = nr_iidicc_par_tab[iid_mode];
168
+            ps->iid_quant     = iid_mode > 2;
169
+            ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
170
+        }
171
+        ps->enable_icc = get_bits1(gb);
172
+        if (ps->enable_icc) {
173
+            ps->icc_mode = get_bits(gb, 3);
174
+            if (ps->icc_mode > 5) {
175
+                av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
176
+                       ps->icc_mode);
177
+                goto err;
178
+            }
179
+            ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
180
+        }
181
+        ps->enable_ext = get_bits1(gb);
182
+    }
183
+
184
+    ps->frame_class = get_bits1(gb);
185
+    ps->num_env_old = ps->num_env;
186
+    ps->num_env     = num_env_tab[ps->frame_class][get_bits(gb, 2)];
187
+
188
+    ps->border_position[0] = -1;
189
+    if (ps->frame_class) {
190
+        for (e = 1; e <= ps->num_env; e++)
191
+            ps->border_position[e] = get_bits(gb, 5);
192
+    } else
193
+        for (e = 1; e <= ps->num_env; e++)
194
+            ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
195
+
196
+    if (ps->enable_iid) {
197
+        for (e = 0; e < ps->num_env; e++) {
198
+            int dt = get_bits1(gb);
199
+            if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
200
+                goto err;
201
+        }
202
+    } else
203
+        memset(ps->iid_par, 0, sizeof(ps->iid_par));
204
+
205
+    if (ps->enable_icc)
206
+        for (e = 0; e < ps->num_env; e++) {
207
+            int dt = get_bits1(gb);
208
+            if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
209
+                goto err;
210
+        }
211
+    else
212
+        memset(ps->icc_par, 0, sizeof(ps->icc_par));
213
+
214
+    if (ps->enable_ext) {
215
+        int cnt = get_bits(gb, 4);
216
+        if (cnt == 15) {
217
+            cnt += get_bits(gb, 8);
218
+        }
219
+        cnt *= 8;
220
+        while (cnt > 7) {
221
+            int ps_extension_id = get_bits(gb, 2);
222
+            cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
223
+        }
224
+        if (cnt < 0) {
225
+            av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d", cnt);
226
+            goto err;
227
+        }
228
+        skip_bits(gb, cnt);
229
+    }
230
+
231
+    ps->enable_ipdopd &= !PS_BASELINE;
232
+
233
+    //Fix up envelopes
234
+    if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
235
+        //Create a fake envelope
236
+        int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
237
+        if (source >= 0 && source != ps->num_env) {
238
+            if (ps->enable_iid) {
239
+                memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
240
+            }
241
+            if (ps->enable_icc) {
242
+                memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
243
+            }
244
+            if (ps->enable_ipdopd) {
245
+                memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
246
+                memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
247
+            }
248
+        }
249
+        ps->num_env++;
250
+        ps->border_position[ps->num_env] = numQMFSlots - 1;
251
+    }
252
+
253
+
254
+    ps->is34bands_old = ps->is34bands;
255
+    if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
256
+        ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
257
+                        (ps->enable_icc && ps->nr_icc_par == 34);
258
+
259
+    //Baseline
260
+    if (!ps->enable_ipdopd) {
261
+        memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
262
+        memset(ps->opd_par, 0, sizeof(ps->opd_par));
263
+    }
264
+
265
+    if (header)
266
+        ps->start = 1;
267
+
268
+    bits_consumed = get_bits_count(gb) - bit_count_start;
269
+    if (bits_consumed <= bits_left) {
270
+        skip_bits_long(gb_host, bits_consumed);
271
+        return bits_consumed;
272
+    }
273
+    av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
274
+err:
275
+    ps->start = 0;
276
+    skip_bits_long(gb_host, bits_left);
277
+    return bits_left;
278
+}
279
+
280
+/** Split one subband into 2 subsubbands with a symmetric real filter.
281
+ * The filter must have its non-center even coefficients equal to zero. */
282
+static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse)
283
+{
284
+    int i, j;
285
+    for (i = 0; i < len; i++, in++) {
286
+        float re_in = filter[6] * in[6][0];          //real inphase
287
+        float re_op = 0.0f;                          //real out of phase
288
+        float im_in = filter[6] * in[6][1];          //imag inphase
289
+        float im_op = 0.0f;                          //imag out of phase
290
+        for (j = 0; j < 6; j += 2) {
291
+            re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
292
+            im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
293
+        }
294
+        out[ reverse][i][0] = re_in + re_op;
295
+        out[ reverse][i][1] = im_in + im_op;
296
+        out[!reverse][i][0] = re_in - re_op;
297
+        out[!reverse][i][1] = im_in - im_op;
298
+    }
299
+}
300
+
301
+/** Split one subband into 6 subsubbands with a complex filter */
302
+static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len)
303
+{
304
+    int i, j, ssb;
305
+    int N = 8;
306
+    float temp[8][2];
307
+
308
+    for (i = 0; i < len; i++, in++) {
309
+        for (ssb = 0; ssb < N; ssb++) {
310
+            float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
311
+            for (j = 0; j < 6; j++) {
312
+                float in0_re = in[j][0];
313
+                float in0_im = in[j][1];
314
+                float in1_re = in[12-j][0];
315
+                float in1_im = in[12-j][1];
316
+                sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
317
+                sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
318
+            }
319
+            temp[ssb][0] = sum_re;
320
+            temp[ssb][1] = sum_im;
321
+        }
322
+        out[0][i][0] = temp[6][0];
323
+        out[0][i][1] = temp[6][1];
324
+        out[1][i][0] = temp[7][0];
325
+        out[1][i][1] = temp[7][1];
326
+        out[2][i][0] = temp[0][0];
327
+        out[2][i][1] = temp[0][1];
328
+        out[3][i][0] = temp[1][0];
329
+        out[3][i][1] = temp[1][1];
330
+        out[4][i][0] = temp[2][0] + temp[5][0];
331
+        out[4][i][1] = temp[2][1] + temp[5][1];
332
+        out[5][i][0] = temp[3][0] + temp[4][0];
333
+        out[5][i][1] = temp[3][1] + temp[4][1];
334
+    }
335
+}
336
+
337
+static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len)
338
+{
339
+    int i, j, ssb;
340
+
341
+    for (i = 0; i < len; i++, in++) {
342
+        for (ssb = 0; ssb < N; ssb++) {
343
+            float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
344
+            for (j = 0; j < 6; j++) {
345
+                float in0_re = in[j][0];
346
+                float in0_im = in[j][1];
347
+                float in1_re = in[12-j][0];
348
+                float in1_im = in[12-j][1];
349
+                sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
350
+                sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
351
+            }
352
+            out[ssb][i][0] = sum_re;
353
+            out[ssb][i][1] = sum_im;
354
+        }
355
+    }
356
+}
357
+
358
+static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len)
359
+{
360
+    int i, j;
361
+    for (i = 0; i < 5; i++) {
362
+        for (j = 0; j < 38; j++) {
363
+            in[i][j+6][0] = L[0][j][i];
364
+            in[i][j+6][1] = L[1][j][i];
365
+        }
366
+    }
367
+    if (is34) {
368
+        hybrid4_8_12_cx(in[0], out,    f34_0_12, 12, len);
369
+        hybrid4_8_12_cx(in[1], out+12, f34_1_8,   8, len);
370
+        hybrid4_8_12_cx(in[2], out+20, f34_2_4,   4, len);
371
+        hybrid4_8_12_cx(in[3], out+24, f34_2_4,   4, len);
372
+        hybrid4_8_12_cx(in[4], out+28, f34_2_4,   4, len);
373
+        for (i = 0; i < 59; i++) {
374
+            for (j = 0; j < len; j++) {
375
+                out[i+32][j][0] = L[0][j][i+5];
376
+                out[i+32][j][1] = L[1][j][i+5];
377
+            }
378
+        }
379
+    } else {
380
+        hybrid6_cx(in[0], out, f20_0_8, len);
381
+        hybrid2_re(in[1], out+6, g1_Q2, len, 1);
382
+        hybrid2_re(in[2], out+8, g1_Q2, len, 0);
383
+        for (i = 0; i < 61; i++) {
384
+            for (j = 0; j < len; j++) {
385
+                out[i+10][j][0] = L[0][j][i+3];
386
+                out[i+10][j][1] = L[1][j][i+3];
387
+            }
388
+        }
389
+    }
390
+    //update in_buf
391
+    for (i = 0; i < 5; i++) {
392
+        memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
393
+    }
394
+}
395
+
396
+static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)
397
+{
398
+    int i, n;
399
+    if (is34) {
400
+        for (n = 0; n < len; n++) {
401
+            memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
402
+            memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
403
+            for (i = 0; i < 12; i++) {
404
+                out[0][n][0] += in[   i][n][0];
405
+                out[1][n][0] += in[   i][n][1];
406
+            }
407
+            for (i = 0; i < 8; i++) {
408
+                out[0][n][1] += in[12+i][n][0];
409
+                out[1][n][1] += in[12+i][n][1];
410
+            }
411
+            for (i = 0; i < 4; i++) {
412
+                out[0][n][2] += in[20+i][n][0];
413
+                out[1][n][2] += in[20+i][n][1];
414
+                out[0][n][3] += in[24+i][n][0];
415
+                out[1][n][3] += in[24+i][n][1];
416
+                out[0][n][4] += in[28+i][n][0];
417
+                out[1][n][4] += in[28+i][n][1];
418
+            }
419
+        }
420
+        for (i = 0; i < 59; i++) {
421
+            for (n = 0; n < len; n++) {
422
+                out[0][n][i+5] = in[i+32][n][0];
423
+                out[1][n][i+5] = in[i+32][n][1];
424
+            }
425
+        }
426
+    } else {
427
+        for (n = 0; n < len; n++) {
428
+            out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
429
+                           in[3][n][0] + in[4][n][0] + in[5][n][0];
430
+            out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
431
+                           in[3][n][1] + in[4][n][1] + in[5][n][1];
432
+            out[0][n][1] = in[6][n][0] + in[7][n][0];
433
+            out[1][n][1] = in[6][n][1] + in[7][n][1];
434
+            out[0][n][2] = in[8][n][0] + in[9][n][0];
435
+            out[1][n][2] = in[8][n][1] + in[9][n][1];
436
+        }
437
+        for (i = 0; i < 61; i++) {
438
+            for (n = 0; n < len; n++) {
439
+                out[0][n][i+3] = in[i+10][n][0];
440
+                out[1][n][i+3] = in[i+10][n][1];
441
+            }
442
+        }
443
+    }
444
+}
445
+
446
+/// All-pass filter decay slope
447
+#define DECAY_SLOPE      0.05f
448
+/// Number of frequency bands that can be addressed by the parameter index, b(k)
449
+static const int   NR_PAR_BANDS[]      = { 20, 34 };
450
+/// Number of frequency bands that can be addressed by the sub subband index, k
451
+static const int   NR_BANDS[]          = { 71, 91 };
452
+/// Start frequency band for the all-pass filter decay slope
453
+static const int   DECAY_CUTOFF[]      = { 10, 32 };
454
+/// Number of all-pass filer bands
455
+static const int   NR_ALLPASS_BANDS[]  = { 30, 50 };
456
+/// First stereo band using the short one sample delay
457
+static const int   SHORT_DELAY_BAND[]  = { 42, 62 };
458
+
459
+/** Table 8.46 */
460
+static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
461
+{
462
+    int b;
463
+    if (full)
464
+        b = 9;
465
+    else {
466
+        b = 4;
467
+        par_mapped[10] = 0;
468
+    }
469
+    for (; b >= 0; b--) {
470
+        par_mapped[2*b+1] = par_mapped[2*b] = par[b];
471
+    }
472
+}
473
+
474
+static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
475
+{
476
+    par_mapped[ 0] = (2*par[ 0] +   par[ 1]) / 3;
477
+    par_mapped[ 1] = (  par[ 1] + 2*par[ 2]) / 3;
478
+    par_mapped[ 2] = (2*par[ 3] +   par[ 4]) / 3;
479
+    par_mapped[ 3] = (  par[ 4] + 2*par[ 5]) / 3;
480
+    par_mapped[ 4] = (  par[ 6] +   par[ 7]) / 2;
481
+    par_mapped[ 5] = (  par[ 8] +   par[ 9]) / 2;
482
+    par_mapped[ 6] =    par[10];
483
+    par_mapped[ 7] =    par[11];
484
+    par_mapped[ 8] = (  par[12] +   par[13]) / 2;
485
+    par_mapped[ 9] = (  par[14] +   par[15]) / 2;
486
+    par_mapped[10] =    par[16];
487
+    if (full) {
488
+        par_mapped[11] =    par[17];
489
+        par_mapped[12] =    par[18];
490
+        par_mapped[13] =    par[19];
491
+        par_mapped[14] = (  par[20] +   par[21]) / 2;
492
+        par_mapped[15] = (  par[22] +   par[23]) / 2;
493
+        par_mapped[16] = (  par[24] +   par[25]) / 2;
494
+        par_mapped[17] = (  par[26] +   par[27]) / 2;
495
+        par_mapped[18] = (  par[28] +   par[29] +   par[30] +   par[31]) / 4;
496
+        par_mapped[19] = (  par[32] +   par[33]) / 2;
497
+    }
498
+}
499
+
500
+static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
501
+{
502
+    par[ 0] = (2*par[ 0] +   par[ 1]) * 0.33333333f;
503
+    par[ 1] = (  par[ 1] + 2*par[ 2]) * 0.33333333f;
504
+    par[ 2] = (2*par[ 3] +   par[ 4]) * 0.33333333f;
505
+    par[ 3] = (  par[ 4] + 2*par[ 5]) * 0.33333333f;
506
+    par[ 4] = (  par[ 6] +   par[ 7]) * 0.5f;
507
+    par[ 5] = (  par[ 8] +   par[ 9]) * 0.5f;
508
+    par[ 6] =    par[10];
509
+    par[ 7] =    par[11];
510
+    par[ 8] = (  par[12] +   par[13]) * 0.5f;
511
+    par[ 9] = (  par[14] +   par[15]) * 0.5f;
512
+    par[10] =    par[16];
513
+    par[11] =    par[17];
514
+    par[12] =    par[18];
515
+    par[13] =    par[19];
516
+    par[14] = (  par[20] +   par[21]) * 0.5f;
517
+    par[15] = (  par[22] +   par[23]) * 0.5f;
518
+    par[16] = (  par[24] +   par[25]) * 0.5f;
519
+    par[17] = (  par[26] +   par[27]) * 0.5f;
520
+    par[18] = (  par[28] +   par[29] +   par[30] +   par[31]) * 0.25f;
521
+    par[19] = (  par[32] +   par[33]) * 0.5f;
522
+}
523
+
524
+static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
525
+{
526
+    if (full) {
527
+        par_mapped[33] = par[9];
528
+        par_mapped[32] = par[9];
529
+        par_mapped[31] = par[9];
530
+        par_mapped[30] = par[9];
531
+        par_mapped[29] = par[9];
532
+        par_mapped[28] = par[9];
533
+        par_mapped[27] = par[8];
534
+        par_mapped[26] = par[8];
535
+        par_mapped[25] = par[8];
536
+        par_mapped[24] = par[8];
537
+        par_mapped[23] = par[7];
538
+        par_mapped[22] = par[7];
539
+        par_mapped[21] = par[7];
540
+        par_mapped[20] = par[7];
541
+        par_mapped[19] = par[6];
542
+        par_mapped[18] = par[6];
543
+        par_mapped[17] = par[5];
544
+        par_mapped[16] = par[5];
545
+    } else {
546
+        par_mapped[16] =      0;
547
+    }
548
+    par_mapped[15] = par[4];
549
+    par_mapped[14] = par[4];
550
+    par_mapped[13] = par[4];
551
+    par_mapped[12] = par[4];
552
+    par_mapped[11] = par[3];
553
+    par_mapped[10] = par[3];
554
+    par_mapped[ 9] = par[2];
555
+    par_mapped[ 8] = par[2];
556
+    par_mapped[ 7] = par[2];
557
+    par_mapped[ 6] = par[2];
558
+    par_mapped[ 5] = par[1];
559
+    par_mapped[ 4] = par[1];
560
+    par_mapped[ 3] = par[1];
561
+    par_mapped[ 2] = par[0];
562
+    par_mapped[ 1] = par[0];
563
+    par_mapped[ 0] = par[0];
564
+}
565
+
566
+static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
567
+{
568
+    if (full) {
569
+        par_mapped[33] =  par[19];
570
+        par_mapped[32] =  par[19];
571
+        par_mapped[31] =  par[18];
572
+        par_mapped[30] =  par[18];
573
+        par_mapped[29] =  par[18];
574
+        par_mapped[28] =  par[18];
575
+        par_mapped[27] =  par[17];
576
+        par_mapped[26] =  par[17];
577
+        par_mapped[25] =  par[16];
578
+        par_mapped[24] =  par[16];
579
+        par_mapped[23] =  par[15];
580
+        par_mapped[22] =  par[15];
581
+        par_mapped[21] =  par[14];
582
+        par_mapped[20] =  par[14];
583
+        par_mapped[19] =  par[13];
584
+        par_mapped[18] =  par[12];
585
+        par_mapped[17] =  par[11];
586
+    }
587
+    par_mapped[16] =  par[10];
588
+    par_mapped[15] =  par[ 9];
589
+    par_mapped[14] =  par[ 9];
590
+    par_mapped[13] =  par[ 8];
591
+    par_mapped[12] =  par[ 8];
592
+    par_mapped[11] =  par[ 7];
593
+    par_mapped[10] =  par[ 6];
594
+    par_mapped[ 9] =  par[ 5];
595
+    par_mapped[ 8] =  par[ 5];
596
+    par_mapped[ 7] =  par[ 4];
597
+    par_mapped[ 6] =  par[ 4];
598
+    par_mapped[ 5] =  par[ 3];
599
+    par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
600
+    par_mapped[ 3] =  par[ 2];
601
+    par_mapped[ 2] =  par[ 1];
602
+    par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
603
+    par_mapped[ 0] =  par[ 0];
604
+}
605
+
606
+static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
607
+{
608
+    par[33] =  par[19];
609
+    par[32] =  par[19];
610
+    par[31] =  par[18];
611
+    par[30] =  par[18];
612
+    par[29] =  par[18];
613
+    par[28] =  par[18];
614
+    par[27] =  par[17];
615
+    par[26] =  par[17];
616
+    par[25] =  par[16];
617
+    par[24] =  par[16];
618
+    par[23] =  par[15];
619
+    par[22] =  par[15];
620
+    par[21] =  par[14];
621
+    par[20] =  par[14];
622
+    par[19] =  par[13];
623
+    par[18] =  par[12];
624
+    par[17] =  par[11];
625
+    par[16] =  par[10];
626
+    par[15] =  par[ 9];
627
+    par[14] =  par[ 9];
628
+    par[13] =  par[ 8];
629
+    par[12] =  par[ 8];
630
+    par[11] =  par[ 7];
631
+    par[10] =  par[ 6];
632
+    par[ 9] =  par[ 5];
633
+    par[ 8] =  par[ 5];
634
+    par[ 7] =  par[ 4];
635
+    par[ 6] =  par[ 4];
636
+    par[ 5] =  par[ 3];
637
+    par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
638
+    par[ 3] =  par[ 2];
639
+    par[ 2] =  par[ 1];
640
+    par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
641
+    par[ 0] =  par[ 0];
642
+}
643
+
644
+static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
645
+{
646
+    float power[34][PS_QMF_TIME_SLOTS] = {{0}};
647
+    float transient_gain[34][PS_QMF_TIME_SLOTS];
648
+    float *peak_decay_nrg = ps->peak_decay_nrg;
649
+    float *power_smooth = ps->power_smooth;
650
+    float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
651
+    float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
652
+    float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
653
+    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
654
+    const float peak_decay_factor = 0.76592833836465f;
655
+    const float transient_impact  = 1.5f;
656
+    const float a_smooth          = 0.25f; //< Smoothing coefficient
657
+    int i, k, m, n;
658
+    int n0 = 0, nL = 32;
659
+    static const int link_delay[] = { 3, 4, 5 };
660
+    static const float a[] = { 0.65143905753106f,
661
+                               0.56471812200776f,
662
+                               0.48954165955695f };
663
+
664
+    if (is34 != ps->is34bands_old) {
665
+        memset(ps->peak_decay_nrg,         0, sizeof(ps->peak_decay_nrg));
666
+        memset(ps->power_smooth,           0, sizeof(ps->power_smooth));
667
+        memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
668
+        memset(ps->delay,                  0, sizeof(ps->delay));
669
+        memset(ps->ap_delay,               0, sizeof(ps->ap_delay));
670
+    }
671
+
672
+    for (n = n0; n < nL; n++) {
673
+        for (k = 0; k < NR_BANDS[is34]; k++) {
674
+            int i = k_to_i[k];
675
+            power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];
676
+        }
677
+    }
678
+
679
+    //Transient detection
680
+    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
681
+        for (n = n0; n < nL; n++) {
682
+            float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
683
+            float denom;
684
+            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
685
+            power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
686
+            peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
687
+            denom = transient_impact * peak_decay_diff_smooth[i];
688
+            transient_gain[i][n]   = (denom > power_smooth[i]) ?
689
+                                         power_smooth[i] / denom : 1.0f;
690
+        }
691
+    }
692
+
693
+    //Decorrelation and transient reduction
694
+    //                         PS_AP_LINKS - 1
695
+    //                               -----
696
+    //                                | |  Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
697
+    //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
698
+    //                                | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
699
+    //                               m = 0
700
+    //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
701
+    for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
702
+        int b = k_to_i[k];
703
+        float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
704
+        float ag[PS_AP_LINKS];
705
+        g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
706
+        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
707
+        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
708
+        for (m = 0; m < PS_AP_LINKS; m++) {
709
+            memcpy(ap_delay[k][m],   ap_delay[k][m]+numQMFSlots,           5*sizeof(ap_delay[k][m][0]));
710
+            ag[m] = a[m] * g_decay_slope;
711
+        }
712
+        for (n = n0; n < nL; n++) {
713
+            float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -
714
+                          delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];
715
+            float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +
716
+                          delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];
717
+            for (m = 0; m < PS_AP_LINKS; m++) {
718
+                float a_re                = ag[m] * in_re;
719
+                float a_im                = ag[m] * in_im;
720
+                float link_delay_re       = ap_delay[k][m][n+5-link_delay[m]][0];
721
+                float link_delay_im       = ap_delay[k][m][n+5-link_delay[m]][1];
722
+                float fractional_delay_re = Q_fract_allpass[is34][k][m][0];
723
+                float fractional_delay_im = Q_fract_allpass[is34][k][m][1];
724
+                ap_delay[k][m][n+5][0] = in_re;
725
+                ap_delay[k][m][n+5][1] = in_im;
726
+                in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;
727
+                in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;
728
+                ap_delay[k][m][n+5][0] += ag[m] * in_re;
729
+                ap_delay[k][m][n+5][1] += ag[m] * in_im;
730
+            }
731
+            out[k][n][0] = transient_gain[b][n] * in_re;
732
+            out[k][n][1] = transient_gain[b][n] * in_im;
733
+        }
734
+    }
735
+    for (; k < SHORT_DELAY_BAND[is34]; k++) {
736
+        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
737
+        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
738
+        for (n = n0; n < nL; n++) {
739
+            //H = delay 14
740
+            out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];
741
+            out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];
742
+        }
743
+    }
744
+    for (; k < NR_BANDS[is34]; k++) {
745
+        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
746
+        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
747
+        for (n = n0; n < nL; n++) {
748
+            //H = delay 1
749
+            out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];
750
+            out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];
751
+        }
752
+    }
753
+}
754
+
755
+static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
756
+                    int8_t           (*par)[PS_MAX_NR_IIDICC],
757
+                    int num_par, int num_env, int full)
758
+{
759
+    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
760
+    int e;
761
+    if (num_par == 20 || num_par == 11) {
762
+        for (e = 0; e < num_env; e++) {
763
+            map_idx_20_to_34(par_mapped[e], par[e], full);
764
+        }
765
+    } else if (num_par == 10 || num_par == 5) {
766
+        for (e = 0; e < num_env; e++) {
767
+            map_idx_10_to_34(par_mapped[e], par[e], full);
768
+        }
769
+    } else {
770
+        *p_par_mapped = par;
771
+    }
772
+}
773
+
774
+static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
775
+                    int8_t           (*par)[PS_MAX_NR_IIDICC],
776
+                    int num_par, int num_env, int full)
777
+{
778
+    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
779
+    int e;
780
+    if (num_par == 34 || num_par == 17) {
781
+        for (e = 0; e < num_env; e++) {
782
+            map_idx_34_to_20(par_mapped[e], par[e], full);
783
+        }
784
+    } else if (num_par == 10 || num_par == 5) {
785
+        for (e = 0; e < num_env; e++) {
786
+            map_idx_10_to_20(par_mapped[e], par[e], full);
787
+        }
788
+    } else {
789
+        *p_par_mapped = par;
790
+    }
791
+}
792
+
793
+static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
794
+{
795
+    int e, b, k, n;
796
+
797
+    float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
798
+    float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
799
+    float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
800
+    float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
801
+    int8_t *opd_hist = ps->opd_hist;
802
+    int8_t *ipd_hist = ps->ipd_hist;
803
+    int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
804
+    int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
805
+    int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
806
+    int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
807
+    int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
808
+    int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
809
+    int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
810
+    int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
811
+    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
812
+    const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
813
+
814
+    //Remapping
815
+    memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
816
+    memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
817
+    memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
818
+    memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
819
+    memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
820
+    memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
821
+    memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
822
+    memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
823
+    if (is34) {
824
+        remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
825
+        remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
826
+        if (ps->enable_ipdopd) {
827
+            remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
828
+            remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
829
+        }
830
+        if (!ps->is34bands_old) {
831
+            map_val_20_to_34(H11[0][0]);
832
+            map_val_20_to_34(H11[1][0]);
833
+            map_val_20_to_34(H12[0][0]);
834
+            map_val_20_to_34(H12[1][0]);
835
+            map_val_20_to_34(H21[0][0]);
836
+            map_val_20_to_34(H21[1][0]);
837
+            map_val_20_to_34(H22[0][0]);
838
+            map_val_20_to_34(H22[1][0]);
839
+            ipdopd_reset(ipd_hist, opd_hist);
840
+        }
841
+    } else {
842
+        remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
843
+        remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
844
+        if (ps->enable_ipdopd) {
845
+            remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
846
+            remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
847
+        }
848
+        if (ps->is34bands_old) {
849
+            map_val_34_to_20(H11[0][0]);
850
+            map_val_34_to_20(H11[1][0]);
851
+            map_val_34_to_20(H12[0][0]);
852
+            map_val_34_to_20(H12[1][0]);
853
+            map_val_34_to_20(H21[0][0]);
854
+            map_val_34_to_20(H21[1][0]);
855
+            map_val_34_to_20(H22[0][0]);
856
+            map_val_34_to_20(H22[1][0]);
857
+            ipdopd_reset(ipd_hist, opd_hist);
858
+        }
859
+    }
860
+
861
+    //Mixing
862
+    for (e = 0; e < ps->num_env; e++) {
863
+        for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
864
+            float h11, h12, h21, h22;
865
+            h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
866
+            h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
867
+            h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
868
+            h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
869
+            if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
870
+                //The spec say says to only run this smoother when enable_ipdopd
871
+                //is set but the reference decoder appears to run it constantly
872
+                float h11i, h12i, h21i, h22i;
873
+                float ipd_adj_re, ipd_adj_im;
874
+                int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
875
+                int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
876
+                float opd_re = pd_re_smooth[opd_idx];
877
+                float opd_im = pd_im_smooth[opd_idx];
878
+                float ipd_re = pd_re_smooth[ipd_idx];
879
+                float ipd_im = pd_im_smooth[ipd_idx];
880
+                opd_hist[b] = opd_idx & 0x3F;
881
+                ipd_hist[b] = ipd_idx & 0x3F;
882
+
883
+                ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
884
+                ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
885
+                h11i = h11 * opd_im;
886
+                h11  = h11 * opd_re;
887
+                h12i = h12 * ipd_adj_im;
888
+                h12  = h12 * ipd_adj_re;
889
+                h21i = h21 * opd_im;
890
+                h21  = h21 * opd_re;
891
+                h22i = h22 * ipd_adj_im;
892
+                h22  = h22 * ipd_adj_re;
893
+                H11[1][e+1][b] = h11i;
894
+                H12[1][e+1][b] = h12i;
895
+                H21[1][e+1][b] = h21i;
896
+                H22[1][e+1][b] = h22i;
897
+            }
898
+            H11[0][e+1][b] = h11;
899
+            H12[0][e+1][b] = h12;
900
+            H21[0][e+1][b] = h21;
901
+            H22[0][e+1][b] = h22;
902
+        }
903
+        for (k = 0; k < NR_BANDS[is34]; k++) {
904
+            float h11r, h12r, h21r, h22r;
905
+            float h11i, h12i, h21i, h22i;
906
+            float h11r_step, h12r_step, h21r_step, h22r_step;
907
+            float h11i_step, h12i_step, h21i_step, h22i_step;
908
+            int start = ps->border_position[e];
909
+            int stop  = ps->border_position[e+1];
910
+            float width = 1.f / (stop - start);
911
+            b = k_to_i[k];
912
+            h11r = H11[0][e][b];
913
+            h12r = H12[0][e][b];
914
+            h21r = H21[0][e][b];
915
+            h22r = H22[0][e][b];
916
+            if (!PS_BASELINE && ps->enable_ipdopd) {
917
+            //Is this necessary? ps_04_new seems unchanged
918
+            if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
919
+                h11i = -H11[1][e][b];
920
+                h12i = -H12[1][e][b];
921
+                h21i = -H21[1][e][b];
922
+                h22i = -H22[1][e][b];
923
+            } else {
924
+                h11i = H11[1][e][b];
925
+                h12i = H12[1][e][b];
926
+                h21i = H21[1][e][b];
927
+                h22i = H22[1][e][b];
928
+            }
929
+            }
930
+            //Interpolation
931
+            h11r_step = (H11[0][e+1][b] - h11r) * width;
932
+            h12r_step = (H12[0][e+1][b] - h12r) * width;
933
+            h21r_step = (H21[0][e+1][b] - h21r) * width;
934
+            h22r_step = (H22[0][e+1][b] - h22r) * width;
935
+            if (!PS_BASELINE && ps->enable_ipdopd) {
936
+                h11i_step = (H11[1][e+1][b] - h11i) * width;
937
+                h12i_step = (H12[1][e+1][b] - h12i) * width;
938
+                h21i_step = (H21[1][e+1][b] - h21i) * width;
939
+                h22i_step = (H22[1][e+1][b] - h22i) * width;
940
+            }
941
+            for (n = start + 1; n <= stop; n++) {
942
+                //l is s, r is d
943
+                float l_re = l[k][n][0];
944
+                float l_im = l[k][n][1];
945
+                float r_re = r[k][n][0];
946
+                float r_im = r[k][n][1];
947
+                h11r += h11r_step;
948
+                h12r += h12r_step;
949
+                h21r += h21r_step;
950
+                h22r += h22r_step;
951
+                if (!PS_BASELINE && ps->enable_ipdopd) {
952
+                    h11i += h11i_step;
953
+                    h12i += h12i_step;
954
+                    h21i += h21i_step;
955
+                    h22i += h22i_step;
956
+
957
+                    l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im;
958
+                    l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re;
959
+                    r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im;
960
+                    r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re;
961
+                } else {
962
+                    l[k][n][0] = h11r*l_re + h21r*r_re;
963
+                    l[k][n][1] = h11r*l_im + h21r*r_im;
964
+                    r[k][n][0] = h12r*l_re + h22r*r_re;
965
+                    r[k][n][1] = h12r*l_im + h22r*r_im;
966
+                }
967
+            }
968
+        }
969
+    }
970
+}
971
+
972
+int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
973
+{
974
+    float Lbuf[91][32][2];
975
+    float Rbuf[91][32][2];
976
+    const int len = 32;
977
+    int is34 = ps->is34bands;
978
+
979
+    top += NR_BANDS[is34] - 64;
980
+    memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
981
+    if (top < NR_ALLPASS_BANDS[is34])
982
+        memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
983
+
984
+    hybrid_analysis(Lbuf, ps->in_buf, L, is34, len);
985
+    decorrelation(ps, Rbuf, Lbuf, is34);
986
+    stereo_processing(ps, Lbuf, Rbuf, is34);
987
+    hybrid_synthesis(L, Lbuf, is34, len);
988
+    hybrid_synthesis(R, Rbuf, is34, len);
989
+
990
+    return 0;
991
+}
992
+
993
+#define PS_INIT_VLC_STATIC(num, size) \
994
+    INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size,    \
995
+                    ps_tmp[num].ps_bits, 1, 1,                                          \
996
+                    ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
997
+                    size);
998
+
999
+#define PS_VLC_ROW(name) \
1000
+    { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
1001
+
1002
+av_cold void ff_ps_init(void) {
1003
+    // Syntax initialization
1004
+    static const struct {
1005
+        const void *ps_codes, *ps_bits;
1006
+        const unsigned int table_size, elem_size;
1007
+    } ps_tmp[] = {
1008
+        PS_VLC_ROW(huff_iid_df1),
1009
+        PS_VLC_ROW(huff_iid_dt1),
1010
+        PS_VLC_ROW(huff_iid_df0),
1011
+        PS_VLC_ROW(huff_iid_dt0),
1012
+        PS_VLC_ROW(huff_icc_df),
1013
+        PS_VLC_ROW(huff_icc_dt),
1014
+        PS_VLC_ROW(huff_ipd_df),
1015
+        PS_VLC_ROW(huff_ipd_dt),
1016
+        PS_VLC_ROW(huff_opd_df),
1017
+        PS_VLC_ROW(huff_opd_dt),
1018
+    };
1019
+
1020
+    PS_INIT_VLC_STATIC(0, 1544);
1021
+    PS_INIT_VLC_STATIC(1,  832);
1022
+    PS_INIT_VLC_STATIC(2, 1024);
1023
+    PS_INIT_VLC_STATIC(3, 1036);
1024
+    PS_INIT_VLC_STATIC(4,  544);
1025
+    PS_INIT_VLC_STATIC(5,  544);
1026
+    PS_INIT_VLC_STATIC(6,  512);
1027
+    PS_INIT_VLC_STATIC(7,  512);
1028
+    PS_INIT_VLC_STATIC(8,  512);
1029
+    PS_INIT_VLC_STATIC(9,  512);
1030
+
1031
+    ps_tableinit();
1032
+}
1033
+
1034
+av_cold void ff_ps_ctx_init(PSContext *ps)
1035
+{
1036
+}
0 1037
new file mode 100644
... ...
@@ -0,0 +1,82 @@
0
+/*
1
+ * MPEG-4 Parametric Stereo definitions and declarations
2
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVCODEC_PS_H
22
+#define AVCODEC_PS_H
23
+
24
+#include <stdint.h>
25
+
26
+#include "avcodec.h"
27
+#include "get_bits.h"
28
+
29
+#define PS_MAX_NUM_ENV 5
30
+#define PS_MAX_NR_IIDICC 34
31
+#define PS_MAX_NR_IPDOPD 17
32
+#define PS_MAX_SSB 91
33
+#define PS_MAX_AP_BANDS 50
34
+#define PS_QMF_TIME_SLOTS 32
35
+#define PS_MAX_DELAY 14
36
+#define PS_AP_LINKS 3
37
+#define PS_MAX_AP_DELAY 5
38
+
39
+typedef struct {
40
+    int    start;
41
+    int    enable_iid;
42
+    int    iid_quant;
43
+    int    nr_iid_par;
44
+    int    nr_ipdopd_par;
45
+    int    enable_icc;
46
+    int    icc_mode;
47
+    int    nr_icc_par;
48
+    int    enable_ext;
49
+    int    frame_class;
50
+    int    num_env_old;
51
+    int    num_env;
52
+    int    enable_ipdopd;
53
+    int    border_position[PS_MAX_NUM_ENV+1];
54
+    int8_t iid_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Inter-channel Intensity Difference Parameters
55
+    int8_t icc_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Inter-Channel Coherence Parameters
56
+    /* ipd/opd is iid/icc sized so that the same functions can handle both */
57
+    int8_t ipd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Inter-channel Phase Difference Parameters
58
+    int8_t opd_par[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC]; //<Overall Phase Difference Parameters
59
+    int    is34bands;
60
+    int    is34bands_old;
61
+
62
+    float  in_buf[5][44][2];
63
+    float  delay[PS_MAX_SSB][PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2];
64
+    float  ap_delay[PS_MAX_AP_BANDS][PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2];
65
+    float  peak_decay_nrg[34];
66
+    float  power_smooth[34];
67
+    float  peak_decay_diff_smooth[34];
68
+    float  H11[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
69
+    float  H12[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
70
+    float  H21[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
71
+    float  H22[2][PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC];
72
+    int8_t opd_hist[PS_MAX_NR_IIDICC];
73
+    int8_t ipd_hist[PS_MAX_NR_IIDICC];
74
+} PSContext;
75
+
76
+void ff_ps_init(void);
77
+void ff_ps_ctx_init(PSContext *ps);
78
+int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, int bits_left);
79
+int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top);
80
+
81
+#endif /* AVCODEC_PS_H */
0 82
new file mode 100644
... ...
@@ -0,0 +1,93 @@
0
+/*
1
+ * Generate a header file for hardcoded Parametric Stereo tables
2
+ *
3
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include <stdlib.h>
23
+#define CONFIG_HARDCODED_TABLES 0
24
+#include "aacps_tablegen.h"
25
+#include "tableprint.h"
26
+
27
+void write_float_3d_array (const void *p, int b, int c, int d)
28
+{
29
+    int i;
30
+    const float *f = p;
31
+    for (i = 0; i < b; i++) {
32
+        printf("{\n");
33
+        write_float_2d_array(f, c, d);
34
+        printf("},\n");
35
+        f += c * d;
36
+    }
37
+}
38
+
39
+void write_float_4d_array (const void *p, int a, int b, int c, int d)
40
+{
41
+    int i;
42
+    const float *f = p;
43
+    for (i = 0; i < a; i++) {
44
+        printf("{\n");
45
+        write_float_3d_array(f, b, c, d);
46
+        printf("},\n");
47
+        f += b * c * d;
48
+    }
49
+}
50
+
51
+int main(void)
52
+{
53
+    ps_tableinit();
54
+
55
+    write_fileheader();
56
+
57
+    printf("static const float pd_re_smooth[8*8*8] = {\n");
58
+    write_float_array(pd_re_smooth, 8*8*8);
59
+    printf("};\n");
60
+    printf("static const float pd_im_smooth[8*8*8] = {\n");
61
+    write_float_array(pd_im_smooth, 8*8*8);
62
+    printf("};\n");
63
+
64
+    printf("static const float HA[46][8][4] = {\n");
65
+    write_float_3d_array(HA, 46, 8, 4);
66
+    printf("};\n");
67
+    printf("static const float HB[46][8][4] = {\n");
68
+    write_float_3d_array(HB, 46, 8, 4);
69
+    printf("};\n");
70
+
71
+    printf("static const float f20_0_8[8][7][2] = {\n");
72
+    write_float_3d_array(f20_0_8, 8, 7, 2);
73
+    printf("};\n");
74
+    printf("static const float f34_0_12[12][7][2] = {\n");
75
+    write_float_3d_array(f34_0_12, 12, 7, 2);
76
+    printf("};\n");
77
+    printf("static const float f34_1_8[8][7][2] = {\n");
78
+    write_float_3d_array(f34_1_8, 8, 7, 2);
79
+    printf("};\n");
80
+    printf("static const float f34_2_4[4][7][2] = {\n");
81
+    write_float_3d_array(f34_2_4, 4, 7, 2);
82
+    printf("};\n");
83
+
84
+    printf("static const float Q_fract_allpass[2][50][3][2] = {\n");
85
+    write_float_4d_array(Q_fract_allpass, 2, 50, 3, 2);
86
+    printf("};\n");
87
+    printf("static const float phi_fract[2][50][2] = {\n");
88
+    write_float_3d_array(phi_fract, 2, 50, 2);
89
+    printf("};\n");
90
+
91
+    return 0;
92
+}
0 93
new file mode 100644
... ...
@@ -0,0 +1,212 @@
0
+/*
1
+ * Header file for hardcoded Parametric Stereo tables
2
+ *
3
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AACPS_TABLEGEN_H
23
+#define AACPS_TABLEGEN_H
24
+
25
+#include <stdint.h>
26
+
27
+#if CONFIG_HARDCODED_TABLES
28
+#define ps_tableinit()
29
+#include "libavcodec/aacps_tables.h"
30
+#else
31
+#include "../libavutil/common.h"
32
+#include "../libavutil/mathematics.h"
33
+#define NR_ALLPASS_BANDS20 30
34
+#define NR_ALLPASS_BANDS34 50
35
+#define PS_AP_LINKS 3
36
+static float pd_re_smooth[8*8*8];
37
+static float pd_im_smooth[8*8*8];
38
+static float HA[46][8][4];
39
+static float HB[46][8][4];
40
+static float f20_0_8 [ 8][7][2];
41
+static float f34_0_12[12][7][2];
42
+static float f34_1_8 [ 8][7][2];
43
+static float f34_2_4 [ 4][7][2];
44
+static float Q_fract_allpass[2][50][3][2];
45
+static float phi_fract[2][50][2];
46
+
47
+static const float g0_Q8[] = {
48
+    0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f,
49
+    0.09885108575264f, 0.11793710567217f, 0.125f
50
+};
51
+
52
+static const float g0_Q12[] = {
53
+    0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f,
54
+    0.07428313801106f, 0.08100347892914f, 0.08333333333333f
55
+};
56
+
57
+static const float g1_Q8[] = {
58
+    0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f,
59
+    0.10307344158036f, 0.12222452249753f, 0.125f
60
+};
61
+
62
+static const float g2_Q4[] = {
63
+    -0.05908211155639f, -0.04871498374946f, 0.0f,   0.07778723915851f,
64
+     0.16486303567403f,  0.23279856662996f, 0.25f
65
+};
66
+
67
+static void make_filters_from_proto(float (*filter)[7][2], const float *proto, int bands)
68
+{
69
+    int q, n;
70
+    for (q = 0; q < bands; q++) {
71
+        for (n = 0; n < 7; n++) {
72
+            double theta = 2 * M_PI * (q + 0.5) * (n - 6) / bands;
73
+            filter[q][n][0] = proto[n] *  cos(theta);
74
+            filter[q][n][1] = proto[n] * -sin(theta);
75
+        }
76
+    }
77
+}
78
+
79
+static void ps_tableinit(void)
80
+{
81
+    static const float ipdopd_sin[] = { 0, M_SQRT1_2, 1,  M_SQRT1_2,  0, -M_SQRT1_2, -1, -M_SQRT1_2 };
82
+    static const float ipdopd_cos[] = { 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2,  0,  M_SQRT1_2 };
83
+    int pd0, pd1, pd2;
84
+
85
+    static const float iid_par_dequant[] = {
86
+        //iid_par_dequant_default
87
+        0.05623413251903, 0.12589254117942, 0.19952623149689, 0.31622776601684,
88
+        0.44668359215096, 0.63095734448019, 0.79432823472428, 1,
89
+        1.25892541179417, 1.58489319246111, 2.23872113856834, 3.16227766016838,
90
+        5.01187233627272, 7.94328234724282, 17.7827941003892,
91
+        //iid_par_dequant_fine
92
+        0.00316227766017, 0.00562341325190, 0.01,             0.01778279410039,
93
+        0.03162277660168, 0.05623413251903, 0.07943282347243, 0.11220184543020,
94
+        0.15848931924611, 0.22387211385683, 0.31622776601684, 0.39810717055350,
95
+        0.50118723362727, 0.63095734448019, 0.79432823472428, 1,
96
+        1.25892541179417, 1.58489319246111, 1.99526231496888, 2.51188643150958,
97
+        3.16227766016838, 4.46683592150963, 6.30957344480193, 8.91250938133745,
98
+        12.5892541179417, 17.7827941003892, 31.6227766016838, 56.2341325190349,
99
+        100,              177.827941003892, 316.227766016837,
100
+    };
101
+    static const float icc_invq[] = {
102
+        1, 0.937,      0.84118,    0.60092,    0.36764,   0,      -0.589,    -1
103
+    };
104
+    static const float acos_icc_invq[] = {
105
+        0, 0.35685527, 0.57133466, 0.92614472, 1.1943263, M_PI/2, 2.2006171, M_PI
106
+    };
107
+    int iid, icc;
108
+
109
+    int k, m;
110
+    static const int8_t f_center_20[] = {
111
+        -3, -1, 1, 3, 5, 7, 10, 14, 18, 22,
112
+    };
113
+    static const int8_t f_center_34[] = {
114
+         2,  6, 10, 14, 18, 22, 26, 30,
115
+        34,-10, -6, -2, 51, 57, 15, 21,
116
+        27, 33, 39, 45, 54, 66, 78, 42,
117
+       102, 66, 78, 90,102,114,126, 90,
118
+    };
119
+    static const float fractional_delay_links[] = { 0.43f, 0.75f, 0.347f };
120
+    const float fractional_delay_gain = 0.39f;
121
+
122
+    for (pd0 = 0; pd0 < 8; pd0++) {
123
+        float pd0_re = ipdopd_cos[pd0];
124
+        float pd0_im = ipdopd_sin[pd0];
125
+        for (pd1 = 0; pd1 < 8; pd1++) {
126
+            float pd1_re = ipdopd_cos[pd1];
127
+            float pd1_im = ipdopd_sin[pd1];
128
+            for (pd2 = 0; pd2 < 8; pd2++) {
129
+                float pd2_re = ipdopd_cos[pd2];
130
+                float pd2_im = ipdopd_sin[pd2];
131
+                float re_smooth = 0.25f * pd0_re + 0.5f * pd1_re + pd2_re;
132
+                float im_smooth = 0.25f * pd0_im + 0.5f * pd1_im + pd2_im;
133
+                float pd_mag = 1 / sqrt(im_smooth * im_smooth + re_smooth * re_smooth);
134
+                pd_re_smooth[pd0*64+pd1*8+pd2] = re_smooth * pd_mag;
135
+                pd_im_smooth[pd0*64+pd1*8+pd2] = im_smooth * pd_mag;
136
+            }
137
+        }
138
+    }
139
+
140
+    for (iid = 0; iid < 46; iid++) {
141
+        float c = iid_par_dequant[iid]; //<Linear Inter-channel Intensity Difference
142
+        float c1 = (float)M_SQRT2 / sqrtf(1.0f + c*c);
143
+        float c2 = c * c1;
144
+        for (icc = 0; icc < 8; icc++) {
145
+            /*if (PS_BASELINE || ps->icc_mode < 3)*/ {
146
+                float alpha = 0.5f * acos_icc_invq[icc];
147
+                float beta  = alpha * (c1 - c2) * (float)M_SQRT1_2;
148
+                HA[iid][icc][0] = c2 * cosf(beta + alpha);
149
+                HA[iid][icc][1] = c1 * cosf(beta - alpha);
150
+                HA[iid][icc][2] = c2 * sinf(beta + alpha);
151
+                HA[iid][icc][3] = c1 * sinf(beta - alpha);
152
+            } /* else */ {
153
+                float alpha, gamma, mu, rho;
154
+                float alpha_c, alpha_s, gamma_c, gamma_s;
155
+                rho = FFMAX(icc_invq[icc], 0.05f);
156
+                alpha = 0.5f * atan2f(2.0f * c * rho, c*c - 1.0f);
157
+                mu = c + 1.0f / c;
158
+                mu = sqrtf(1 + (4 * rho * rho - 4)/(mu * mu));
159
+                gamma = atanf(sqrtf((1.0f - mu)/(1.0f + mu)));
160
+                if (alpha < 0) alpha += M_PI/2;
161
+                alpha_c = cosf(alpha);
162
+                alpha_s = sinf(alpha);
163
+                gamma_c = cosf(gamma);
164
+                gamma_s = sinf(gamma);
165
+                HB[iid][icc][0] =  M_SQRT2 * alpha_c * gamma_c;
166
+                HB[iid][icc][1] =  M_SQRT2 * alpha_s * gamma_c;
167
+                HB[iid][icc][2] = -M_SQRT2 * alpha_s * gamma_s;
168
+                HB[iid][icc][3] =  M_SQRT2 * alpha_c * gamma_s;
169
+            }
170
+        }
171
+    }
172
+
173
+    for (k = 0; k < NR_ALLPASS_BANDS20; k++) {
174
+        double f_center, theta;
175
+        if (k < FF_ARRAY_ELEMS(f_center_20))
176
+            f_center = f_center_20[k] * 0.125;
177
+        else
178
+            f_center = k - 6.5f;
179
+        for (m = 0; m < PS_AP_LINKS; m++) {
180
+            theta = -M_PI * fractional_delay_links[m] * f_center;
181
+            Q_fract_allpass[0][k][m][0] = cos(theta);
182
+            Q_fract_allpass[0][k][m][1] = sin(theta);
183
+        }
184
+        theta = -M_PI*fractional_delay_gain*f_center;
185
+        phi_fract[0][k][0] = cos(theta);
186
+        phi_fract[0][k][1] = sin(theta);
187
+    }
188
+    for (k = 0; k < NR_ALLPASS_BANDS34; k++) {
189
+        double f_center, theta;
190
+        if (k < FF_ARRAY_ELEMS(f_center_34))
191
+            f_center = f_center_34[k] / 24.;
192
+        else
193
+            f_center = k - 26.5f;
194
+        for (m = 0; m < PS_AP_LINKS; m++) {
195
+            theta = -M_PI * fractional_delay_links[m] * f_center;
196
+            Q_fract_allpass[1][k][m][0] = cos(theta);
197
+            Q_fract_allpass[1][k][m][1] = sin(theta);
198
+        }
199
+        theta = -M_PI*fractional_delay_gain*f_center;
200
+        phi_fract[1][k][0] = cos(theta);
201
+        phi_fract[1][k][1] = sin(theta);
202
+    }
203
+
204
+    make_filters_from_proto(f20_0_8,  g0_Q8,   8);
205
+    make_filters_from_proto(f34_0_12, g0_Q12, 12);
206
+    make_filters_from_proto(f34_1_8,  g1_Q8,   8);
207
+    make_filters_from_proto(f34_2_4,  g2_Q4,   4);
208
+}
209
+#endif /* CONFIG_HARDCODED_TABLES */
210
+
211
+#endif /* AACPS_TABLEGEN_H */
0 212
new file mode 100644
... ...
@@ -0,0 +1,163 @@
0
+/*
1
+ * MPEG-4 Parametric Stereo data tables
2
+ * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+static const uint8_t huff_iid_df1_bits[] = {
22
+    18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 16, 16, 15, 14, 14,
23
+    13, 12, 12, 11, 10, 10,  8,  7,  6,  5,  4,  3,  1,  3,  4,  5,  6,  7,
24
+     8,  9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 17, 18, 17, 18, 18,
25
+    18, 18, 18, 18, 18, 18, 18,
26
+};
27
+
28
+static const uint32_t huff_iid_df1_codes[] = {
29
+    0x01FEB4, 0x01FEB5, 0x01FD76, 0x01FD77, 0x01FD74, 0x01FD75, 0x01FE8A,
30
+    0x01FE8B, 0x01FE88, 0x00FE80, 0x01FEB6, 0x00FE82, 0x00FEB8, 0x007F42,
31
+    0x007FAE, 0x003FAF, 0x001FD1, 0x001FE9, 0x000FE9, 0x0007EA, 0x0007FB,
32
+    0x0003FB, 0x0001FB, 0x0001FF, 0x00007C, 0x00003C, 0x00001C, 0x00000C,
33
+    0x000000, 0x000001, 0x000001, 0x000002, 0x000001, 0x00000D, 0x00001D,
34
+    0x00003D, 0x00007D, 0x0000FC, 0x0001FC, 0x0003FC, 0x0003F4, 0x0007EB,
35
+    0x000FEA, 0x001FEA, 0x001FD6, 0x003FD0, 0x007FAF, 0x007F43, 0x00FEB9,
36
+    0x00FE83, 0x01FEB7, 0x00FE81, 0x01FE89, 0x01FE8E, 0x01FE8F, 0x01FE8C,
37
+    0x01FE8D, 0x01FEB2, 0x01FEB3, 0x01FEB0, 0x01FEB1,
38
+};
39
+
40
+static const uint8_t huff_iid_dt1_bits[] = {
41
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 13,
42
+    13, 13, 12, 12, 11, 10,  9,  9,  7,  6,  5,  3,  1,  2,  5,  6,  7,  8,
43
+     9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
44
+    16, 16, 16, 16, 16, 16, 16,
45
+};
46
+
47
+static const uint16_t huff_iid_dt1_codes[] = {
48
+    0x004ED4, 0x004ED5, 0x004ECE, 0x004ECF, 0x004ECC, 0x004ED6, 0x004ED8,
49
+    0x004F46, 0x004F60, 0x002718, 0x002719, 0x002764, 0x002765, 0x00276D,
50
+    0x0027B1, 0x0013B7, 0x0013D6, 0x0009C7, 0x0009E9, 0x0009ED, 0x0004EE,
51
+    0x0004F7, 0x000278, 0x000139, 0x00009A, 0x00009F, 0x000020, 0x000011,
52
+    0x00000A, 0x000003, 0x000001, 0x000000, 0x00000B, 0x000012, 0x000021,
53
+    0x00004C, 0x00009B, 0x00013A, 0x000279, 0x000270, 0x0004EF, 0x0004E2,
54
+    0x0009EA, 0x0009D8, 0x0013D7, 0x0013D0, 0x0027B2, 0x0027A2, 0x00271A,
55
+    0x00271B, 0x004F66, 0x004F67, 0x004F61, 0x004F47, 0x004ED9, 0x004ED7,
56
+    0x004ECD, 0x004ED2, 0x004ED3, 0x004ED0, 0x004ED1,
57
+};
58
+
59
+static const uint8_t huff_iid_df0_bits[] = {
60
+    17, 17, 17, 17, 16, 15, 13, 10,  9,  7,  6,  5,  4,  3,  1,  3,  4,  5,
61
+     6,  6,  8, 11, 13, 14, 14, 15, 17, 18, 18,
62
+};
63
+
64
+static const uint32_t huff_iid_df0_codes[] = {
65
+    0x01FFFB, 0x01FFFC, 0x01FFFD, 0x01FFFA, 0x00FFFC, 0x007FFC, 0x001FFD,
66
+    0x0003FE, 0x0001FE, 0x00007E, 0x00003C, 0x00001D, 0x00000D, 0x000005,
67
+    0x000000, 0x000004, 0x00000C, 0x00001C, 0x00003D, 0x00003E, 0x0000FE,
68
+    0x0007FE, 0x001FFC, 0x003FFC, 0x003FFD, 0x007FFD, 0x01FFFE, 0x03FFFE,
69
+    0x03FFFF,
70
+};
71
+
72
+static const uint8_t huff_iid_dt0_bits[] = {
73
+    19, 19, 19, 20, 20, 20, 17, 15, 12, 10,  8,  6,  4,  2,  1,  3,  5,  7,
74
+     9, 11, 13, 14, 17, 19, 20, 20, 20, 20, 20,
75
+};
76
+
77
+static const uint32_t huff_iid_dt0_codes[] = {
78
+    0x07FFF9, 0x07FFFA, 0x07FFFB, 0x0FFFF8, 0x0FFFF9, 0x0FFFFA, 0x01FFFD,
79
+    0x007FFE, 0x000FFE, 0x0003FE, 0x0000FE, 0x00003E, 0x00000E, 0x000002,
80
+    0x000000, 0x000006, 0x00001E, 0x00007E, 0x0001FE, 0x0007FE, 0x001FFE,
81
+    0x003FFE, 0x01FFFC, 0x07FFF8, 0x0FFFFB, 0x0FFFFC, 0x0FFFFD, 0x0FFFFE,
82
+    0x0FFFFF,
83
+};
84
+
85
+static const uint8_t huff_icc_df_bits[] = {
86
+    14, 14, 12, 10, 7, 5, 3, 1, 2, 4, 6, 8, 9, 11, 13,
87
+};
88
+
89
+static const uint16_t huff_icc_df_codes[] = {
90
+    0x3FFF, 0x3FFE, 0x0FFE, 0x03FE, 0x007E, 0x001E, 0x0006, 0x0000,
91
+    0x0002, 0x000E, 0x003E, 0x00FE, 0x01FE, 0x07FE, 0x1FFE,
92
+};
93
+
94
+static const uint8_t huff_icc_dt_bits[] = {
95
+    14, 13, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12, 14,
96
+};
97
+
98
+static const uint16_t huff_icc_dt_codes[] = {
99
+    0x3FFE, 0x1FFE, 0x07FE, 0x01FE, 0x007E, 0x001E, 0x0006, 0x0000,
100
+    0x0002, 0x000E, 0x003E, 0x00FE, 0x03FE, 0x0FFE, 0x3FFF,
101
+};
102
+
103
+static const uint8_t huff_ipd_df_bits[] = {
104
+    1, 3, 4, 4, 4, 4, 4, 4,
105
+};
106
+
107
+static const uint8_t huff_ipd_df_codes[] = {
108
+    0x01, 0x00, 0x06, 0x04, 0x02, 0x03, 0x05, 0x07,
109
+};
110
+
111
+static const uint8_t huff_ipd_dt_bits[] = {
112
+    1, 3, 4, 5, 5, 4, 4, 3,
113
+};
114
+
115
+static const uint8_t huff_ipd_dt_codes[] = {
116
+    0x01, 0x02, 0x02, 0x03, 0x02, 0x00, 0x03, 0x03,
117
+};
118
+
119
+static const uint8_t huff_opd_df_bits[] = {
120
+    1, 3, 4, 4, 5, 5, 4, 3,
121
+};
122
+
123
+static const uint8_t huff_opd_df_codes[] = {
124
+    0x01, 0x01, 0x06, 0x04, 0x0F, 0x0E, 0x05, 0x00,
125
+};
126
+
127
+static const uint8_t huff_opd_dt_bits[] = {
128
+    1, 3, 4, 5, 5, 4, 4, 3,
129
+};
130
+
131
+static const uint8_t huff_opd_dt_codes[] = {
132
+    0x01, 0x02, 0x01, 0x07, 0x06, 0x00, 0x02, 0x03,
133
+};
134
+
135
+static const int8_t huff_offset[] = {
136
+    30, 30,
137
+    14, 14,
138
+    7, 7,
139
+    0, 0,
140
+    0, 0,
141
+};
142
+
143
+///Table 8.48
144
+static const int8_t k_to_i_20[] = {
145
+     1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 14, 15,
146
+    15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
147
+    18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
148
+    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19
149
+};
150
+///Table 8.49
151
+static const int8_t k_to_i_34[] = {
152
+     0,  1,  2,  3,  4,  5,  6,  6,  7,  2,  1,  0, 10, 10,  4,  5,  6,  7,  8,
153
+     9, 10, 11, 12,  9, 14, 11, 12, 13, 14, 15, 16, 13, 16, 17, 18, 19, 20, 21,
154
+    22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29,
155
+    30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33,
156
+    33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33
157
+};
158
+
159
+static const float g1_Q2[] = {
160
+    0.0f,  0.01899487526049f, 0.0f, -0.07293139167538f,
161
+    0.0f,  0.30596630545168f, 0.5f
162
+};
... ...
@@ -31,6 +31,7 @@
31 31
 #include "aacsbr.h"
32 32
 #include "aacsbrdata.h"
33 33
 #include "fft.h"
34
+#include "aacps.h"
34 35
 
35 36
 #include <stdint.h>
36 37
 #include <float.h>
... ...
@@ -71,9 +72,6 @@ enum {
71 71
 static VLC vlc_sbr[10];
72 72
 static const int8_t vlc_sbr_lav[10] =
73 73
     { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
74
-static DECLARE_ALIGNED(16, float, analysis_cos_pre)[64];
75
-static DECLARE_ALIGNED(16, float, analysis_sin_pre)[64];
76
-static DECLARE_ALIGNED(16, float, analysis_cossin_post)[32][2];
77 74
 static const DECLARE_ALIGNED(16, float, zero64)[64];
78 75
 
79 76
 #define SBR_INIT_VLC_STATIC(num, size) \
... ...
@@ -87,7 +85,7 @@ static const DECLARE_ALIGNED(16, float, zero64)[64];
87 87
 
88 88
 av_cold void ff_aac_sbr_init(void)
89 89
 {
90
-    int n, k;
90
+    int n;
91 91
     static const struct {
92 92
         const void *sbr_codes, *sbr_bits;
93 93
         const unsigned int table_size, elem_size;
... ...
@@ -116,16 +114,6 @@ av_cold void ff_aac_sbr_init(void)
116 116
     SBR_INIT_VLC_STATIC(8, 592);
117 117
     SBR_INIT_VLC_STATIC(9, 512);
118 118
 
119
-    for (n = 0; n < 64; n++) {
120
-        float pre = M_PI * n / 64;
121
-        analysis_cos_pre[n] = cosf(pre);
122
-        analysis_sin_pre[n] = sinf(pre);
123
-    }
124
-    for (k = 0; k < 32; k++) {
125
-        float post = M_PI * (k + 0.5) / 128;
126
-        analysis_cossin_post[k][0] =  4.0 * cosf(post);
127
-        analysis_cossin_post[k][1] = -4.0 * sinf(post);
128
-    }
129 119
     for (n = 1; n < 320; n++)
130 120
         sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
131 121
     sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
... ...
@@ -133,6 +121,8 @@ av_cold void ff_aac_sbr_init(void)
133 133
 
134 134
     for (n = 0; n < 320; n++)
135 135
         sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
136
+
137
+    ff_ps_init();
136 138
 }
137 139
 
138 140
 av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr)
... ...
@@ -142,13 +132,14 @@ av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr)
142 142
     sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
143 143
     sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
144 144
     ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64);
145
-    ff_rdft_init(&sbr->rdft, 6, IDFT_R2C);
145
+    ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0);
146
+    ff_ps_ctx_init(&sbr->ps);
146 147
 }
147 148
 
148 149
 av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
149 150
 {
150 151
     ff_mdct_end(&sbr->mdct);
151
-    ff_rdft_end(&sbr->rdft);
152
+    ff_mdct_end(&sbr->mdct_ana);
152 153
 }
153 154
 
154 155
 static int qsort_comparison_function_int16(const void *a, const void *b)
... ...
@@ -293,15 +284,15 @@ static void make_bands(int16_t* bands, int start, int stop, int num_bands)
293 293
     bands[num_bands-1] = stop - previous;
294 294
 }
295 295
 
296
-static int check_n_master(AVCodecContext *avccontext, int n_master, int bs_xover_band)
296
+static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
297 297
 {
298 298
     // Requirements (14496-3 sp04 p205)
299 299
     if (n_master <= 0) {
300
-        av_log(avccontext, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
300
+        av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
301 301
         return -1;
302 302
     }
303 303
     if (bs_xover_band >= n_master) {
304
-        av_log(avccontext, AV_LOG_ERROR,
304
+        av_log(avctx, AV_LOG_ERROR,
305 305
                "Invalid bitstream, crossover band index beyond array bounds: %d\n",
306 306
                bs_xover_band);
307 307
         return -1;
... ...
@@ -349,7 +340,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
349 349
         sbr_offset_ptr = sbr_offset[5];
350 350
         break;
351 351
     default:
352
-        av_log(ac->avccontext, AV_LOG_ERROR,
352
+        av_log(ac->avctx, AV_LOG_ERROR,
353 353
                "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
354 354
         return -1;
355 355
     }
... ...
@@ -367,7 +358,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
367 367
     } else if (spectrum->bs_stop_freq == 15) {
368 368
         sbr->k[2] = 3*sbr->k[0];
369 369
     } else {
370
-        av_log(ac->avccontext, AV_LOG_ERROR,
370
+        av_log(ac->avctx, AV_LOG_ERROR,
371 371
                "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
372 372
         return -1;
373 373
     }
... ...
@@ -382,18 +373,17 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
382 382
         max_qmf_subbands = 32;
383 383
 
384 384
     if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
385
-        av_log(ac->avccontext, AV_LOG_ERROR,
385
+        av_log(ac->avctx, AV_LOG_ERROR,
386 386
                "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
387 387
         return -1;
388 388
     }
389 389
 
390 390
     if (!spectrum->bs_freq_scale) {
391
-        unsigned int dk;
392
-        int k2diff;
391
+        int dk, k2diff;
393 392
 
394 393
         dk = spectrum->bs_alter_scale + 1;
395 394
         sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
396
-        if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
395
+        if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
397 396
             return -1;
398 397
 
399 398
         for (k = 1; k <= sbr->n_master; k++)
... ...
@@ -428,7 +418,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
428 428
         num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
429 429
 
430 430
         if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
431
-            av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
431
+            av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
432 432
             return -1;
433 433
         }
434 434
 
... ...
@@ -442,7 +432,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
442 442
         vk0[0] = sbr->k[0];
443 443
         for (k = 1; k <= num_bands_0; k++) {
444 444
             if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
445
-                av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
445
+                av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
446 446
                 return -1;
447 447
             }
448 448
             vk0[k] += vk0[k-1];
... ...
@@ -472,14 +462,14 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
472 472
             vk1[0] = sbr->k[1];
473 473
             for (k = 1; k <= num_bands_1; k++) {
474 474
                 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
475
-                    av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
475
+                    av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
476 476
                     return -1;
477 477
                 }
478 478
                 vk1[k] += vk1[k-1];
479 479
             }
480 480
 
481 481
             sbr->n_master = num_bands_0 + num_bands_1;
482
-            if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
482
+            if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
483 483
                 return -1;
484 484
             memcpy(&sbr->f_master[0],               vk0,
485 485
                    (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
... ...
@@ -488,7 +478,7 @@ static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
488 488
 
489 489
         } else {
490 490
             sbr->n_master = num_bands_0;
491
-            if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
491
+            if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
492 492
                 return -1;
493 493
             memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
494 494
         }
... ...
@@ -524,7 +514,7 @@ static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
524 524
         // illegal however the Coding Technologies decoder check stream has a final
525 525
         // count of 6 patches
526 526
         if (sbr->num_patches > 5) {
527
-            av_log(ac->avccontext, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
527
+            av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
528 528
             return -1;
529 529
         }
530 530
 
... ...
@@ -563,12 +553,12 @@ static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
563 563
 
564 564
     // Requirements (14496-3 sp04 p205)
565 565
     if (sbr->kx[1] + sbr->m[1] > 64) {
566
-        av_log(ac->avccontext, AV_LOG_ERROR,
566
+        av_log(ac->avctx, AV_LOG_ERROR,
567 567
                "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
568 568
         return -1;
569 569
     }
570 570
     if (sbr->kx[1] > 32) {
571
-        av_log(ac->avccontext, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
571
+        av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
572 572
         return -1;
573 573
     }
574 574
 
... ...
@@ -580,7 +570,7 @@ static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
580 580
     sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
581 581
                                log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
582 582
     if (sbr->n_q > 5) {
583
-        av_log(ac->avccontext, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
583
+        av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
584 584
         return -1;
585 585
     }
586 586
 
... ...
@@ -638,7 +628,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
638 638
             ch_data->bs_amp_res = 0;
639 639
 
640 640
         if (ch_data->bs_num_env > 4) {
641
-            av_log(ac->avccontext, AV_LOG_ERROR,
641
+            av_log(ac->avctx, AV_LOG_ERROR,
642 642
                    "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
643 643
                    ch_data->bs_num_env);
644 644
             return -1;
... ...
@@ -693,7 +683,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
693 693
         ch_data->bs_num_env                 = num_rel_lead + num_rel_trail + 1;
694 694
 
695 695
         if (ch_data->bs_num_env > 5) {
696
-            av_log(ac->avccontext, AV_LOG_ERROR,
696
+            av_log(ac->avctx, AV_LOG_ERROR,
697 697
                    "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
698 698
                    ch_data->bs_num_env);
699 699
             return -1;
... ...
@@ -714,7 +704,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
714 714
     }
715 715
 
716 716
     if (bs_pointer > ch_data->bs_num_env + 1) {
717
-        av_log(ac->avccontext, AV_LOG_ERROR,
717
+        av_log(ac->avctx, AV_LOG_ERROR,
718 718
                "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
719 719
                bs_pointer);
720 720
         return -1;
... ...
@@ -722,7 +712,7 @@ static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
722 722
 
723 723
     for (i = 1; i <= ch_data->bs_num_env; i++) {
724 724
         if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
725
-            av_log(ac->avccontext, AV_LOG_ERROR, "Non monotone time borders\n");
725
+            av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
726 726
             return -1;
727 727
         }
728 728
     }
... ...
@@ -903,25 +893,24 @@ static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
903 903
                                GetBitContext *gb,
904 904
                                int bs_extension_id, int *num_bits_left)
905 905
 {
906
-//TODO - implement ps_data for parametric stereo parsing
907 906
     switch (bs_extension_id) {
908 907
     case EXTENSION_ID_PS:
909 908
         if (!ac->m4ac.ps) {
910
-            av_log(ac->avccontext, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
909
+            av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
911 910
             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
912 911
             *num_bits_left = 0;
913 912
         } else {
914
-#if 0
915
-            *num_bits_left -= ff_ps_data(gb, ps);
913
+#if 1
914
+            *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
916 915
 #else
917
-            av_log_missing_feature(ac->avccontext, "Parametric Stereo is", 0);
916
+            av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
918 917
             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
919 918
             *num_bits_left = 0;
920 919
 #endif
921 920
         }
922 921
         break;
923 922
     default:
924
-        av_log_missing_feature(ac->avccontext, "Reserved SBR extensions are", 1);
923
+        av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
925 924
         skip_bits_long(gb, *num_bits_left); // bs_fill_bits
926 925
         *num_bits_left = 0;
927 926
         break;
... ...
@@ -1006,7 +995,7 @@ static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
1006 1006
             return get_bits_count(gb) - cnt;
1007 1007
         }
1008 1008
     } else {
1009
-        av_log(ac->avccontext, AV_LOG_ERROR,
1009
+        av_log(ac->avctx, AV_LOG_ERROR,
1010 1010
             "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1011 1011
         sbr->start = 0;
1012 1012
         return get_bits_count(gb) - cnt;
... ...
@@ -1021,6 +1010,11 @@ static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
1021 1021
             num_bits_left -= 2;
1022 1022
             read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1023 1023
         }
1024
+        if (num_bits_left < 0) {
1025
+            av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1026
+        }
1027
+        if (num_bits_left > 0)
1028
+            skip_bits(gb, num_bits_left);
1024 1029
     }
1025 1030
 
1026 1031
     return get_bits_count(gb) - cnt;
... ...
@@ -1033,7 +1027,7 @@ static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
1033 1033
     if (err >= 0)
1034 1034
         err = sbr_make_f_derived(ac, sbr);
1035 1035
     if (err < 0) {
1036
-        av_log(ac->avccontext, AV_LOG_ERROR,
1036
+        av_log(ac->avctx, AV_LOG_ERROR,
1037 1037
                "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1038 1038
         sbr->start = 0;
1039 1039
     }
... ...
@@ -1085,7 +1079,7 @@ int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
1085 1085
     bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1086 1086
 
1087 1087
     if (bytes_read > cnt) {
1088
-        av_log(ac->avccontext, AV_LOG_ERROR,
1088
+        av_log(ac->avctx, AV_LOG_ERROR,
1089 1089
                "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1090 1090
     }
1091 1091
     return cnt;
... ...
@@ -1139,7 +1133,7 @@ static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
1139 1139
  * @param   x       pointer to the beginning of the first sample window
1140 1140
  * @param   W       array of complex-valued samples split into subbands
1141 1141
  */
1142
-static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in, float *x,
1142
+static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
1143 1143
                              float z[320], float W[2][32][32][2],
1144 1144
                              float scale)
1145 1145
 {
... ...
@@ -1152,23 +1146,23 @@ static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in
1152 1152
         memcpy(x+288, in, 1024*sizeof(*x));
1153 1153
     for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1154 1154
                                // are not supported
1155
-        float re, im;
1156 1155
         dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1157 1156
         for (k = 0; k < 64; k++) {
1158 1157
             float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
1159
-            z[k] = f * analysis_cos_pre[k];
1160
-            z[k+64] = f;
1158
+            z[k] = f;
1161 1159
         }
1162
-        ff_rdft_calc(rdft, z);
1163
-        re = z[0] * 0.5f;
1164
-        im = 0.5f * dsp->scalarproduct_float(z+64, analysis_sin_pre, 64);
1165
-        W[1][i][0][0] = re * analysis_cossin_post[0][0] - im * analysis_cossin_post[0][1];
1166
-        W[1][i][0][1] = re * analysis_cossin_post[0][1] + im * analysis_cossin_post[0][0];
1160
+        //Shuffle to IMDCT
1161
+        z[64] = z[0];
1167 1162
         for (k = 1; k < 32; k++) {
1168
-            re = z[2*k  ] - re;
1169
-            im = z[2*k+1] - im;
1170
-            W[1][i][k][0] = re * analysis_cossin_post[k][0] - im * analysis_cossin_post[k][1];
1171
-            W[1][i][k][1] = re * analysis_cossin_post[k][1] + im * analysis_cossin_post[k][0];
1163
+            z[64+2*k-1] =  z[   k];
1164
+            z[64+2*k  ] = -z[64-k];
1165
+        }
1166
+        z[64+63] = z[32];
1167
+
1168
+        ff_imdct_half(mdct, z, z+64);
1169
+        for (k = 0; k < 32; k++) {
1170
+            W[1][i][k][0] = -z[63-k];
1171
+            W[1][i][k][1] = z[k];
1172 1172
         }
1173 1173
         x += 32;
1174 1174
     }
... ...
@@ -1179,7 +1173,7 @@ static void sbr_qmf_analysis(DSPContext *dsp, RDFTContext *rdft, const float *in
1179 1179
  * (14496-3 sp04 p206)
1180 1180
  */
1181 1181
 static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
1182
-                              float *out, float X[2][32][64],
1182
+                              float *out, float X[2][38][64],
1183 1183
                               float mdct_buf[2][64],
1184 1184
                               float *v0, int *v_off, const unsigned int div,
1185 1185
                               float bias, float scale)
... ...
@@ -1197,21 +1191,22 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
1197 1197
             *v_off -= 128 >> div;
1198 1198
         }
1199 1199
         v = v0 + *v_off;
1200
-        for (n = 1; n < 64 >> div; n+=2) {
1201
-            X[1][i][n] = -X[1][i][n];
1202
-        }
1203
-        if (div) {
1204
-            memset(X[0][i]+32, 0, 32*sizeof(float));
1205
-            memset(X[1][i]+32, 0, 32*sizeof(float));
1206
-        }
1207
-        ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
1208
-        ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
1209 1200
         if (div) {
1210 1201
             for (n = 0; n < 32; n++) {
1211
-                v[      n] = -mdct_buf[0][63 - 2*n] + mdct_buf[1][2*n    ];
1212
-                v[ 63 - n] =  mdct_buf[0][62 - 2*n] + mdct_buf[1][2*n + 1];
1202
+                X[0][i][   n] = -X[0][i][n];
1203
+                X[0][i][32+n] =  X[1][i][31-n];
1204
+            }
1205
+            ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
1206
+            for (n = 0; n < 32; n++) {
1207
+                v[     n] =  mdct_buf[0][63 - 2*n];
1208
+                v[63 - n] = -mdct_buf[0][62 - 2*n];
1213 1209
             }
1214 1210
         } else {
1211
+            for (n = 1; n < 64; n+=2) {
1212
+                X[1][i][n] = -X[1][i][n];
1213
+            }
1214
+            ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
1215
+            ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
1215 1216
             for (n = 0; n < 64; n++) {
1216 1217
                 v[      n] = -mdct_buf[0][63 -   n] + mdct_buf[1][  n    ];
1217 1218
                 v[127 - n] =  mdct_buf[0][63 -   n] + mdct_buf[1][  n    ];
... ...
@@ -1380,7 +1375,7 @@ static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
1380 1380
             g--;
1381 1381
 
1382 1382
             if (g < 0) {
1383
-                av_log(ac->avccontext, AV_LOG_ERROR,
1383
+                av_log(ac->avctx, AV_LOG_ERROR,
1384 1384
                        "ERROR : no subband found for frequency %d\n", k);
1385 1385
                 return -1;
1386 1386
             }
... ...
@@ -1414,7 +1409,7 @@ static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
1414 1414
 }
1415 1415
 
1416 1416
 /// Generate the subband filtered lowband
1417
-static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64],
1417
+static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
1418 1418
                      const float X_low[32][40][2], const float Y[2][38][64][2],
1419 1419
                      int ch)
1420 1420
 {
... ...
@@ -1436,7 +1431,7 @@ static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][32][64],
1436 1436
     }
1437 1437
 
1438 1438
     for (k = 0; k < sbr->kx[1]; k++) {
1439
-        for (i = i_Temp; i < i_f; i++) {
1439
+        for (i = i_Temp; i < 38; i++) {
1440 1440
             X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1441 1441
             X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1442 1442
         }
... ...
@@ -1730,7 +1725,7 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
1730 1730
     }
1731 1731
     for (ch = 0; ch < nch; ch++) {
1732 1732
         /* decode channel */
1733
-        sbr_qmf_analysis(&ac->dsp, &sbr->rdft, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1733
+        sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1734 1734
                          (float*)sbr->qmf_filter_scratch,
1735 1735
                          sbr->data[ch].W, 1/(-1024 * ac->sf_scale));
1736 1736
         sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
... ...
@@ -1752,6 +1747,16 @@ void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
1752 1752
         /* synthesis */
1753 1753
         sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch);
1754 1754
     }
1755
+
1756
+    if (ac->m4ac.ps == 1) {
1757
+        if (sbr->ps.start) {
1758
+            ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1759
+        } else {
1760
+            memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1761
+        }
1762
+        nch = 2;
1763
+    }
1764
+
1755 1765
     sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,
1756 1766
                       sbr->data[0].synthesis_filterbank_samples,
1757 1767
                       &sbr->data[0].synthesis_filterbank_samples_offset,
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "libavutil/mem.h"
31 31
 #include "aac.h"
32
+#include "aac_tablegen.h"
32 33
 
33 34
 #include <stdint.h>
34 35
 
... ...
@@ -1204,129 +1205,3 @@ const uint8_t ff_tns_max_bands_128[] = {
1204 1204
     9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14
1205 1205
 };
1206 1206
 // @}
1207
-
1208
-
1209
-#if CONFIG_HARDCODED_TABLES
1210
-
1211
-/**
1212
- * Table of pow(2, (i - 200)/4.) used for different purposes depending on the
1213
- * range of indices to the table:
1214
- * [ 0, 255] scale factor decoding when using C dsp.float_to_int16
1215
- * [60, 315] scale factor decoding when using SIMD dsp.float_to_int16
1216
- * [45, 300] intensity stereo position decoding mapped in reverse order i.e. 0->300, 1->299, ..., 254->46, 255->45
1217
- */
1218
-const float ff_aac_pow2sf_tab[428] = {
1219
-    8.88178420e-16, 1.05622810e-15, 1.25607397e-15, 1.49373210e-15,
1220
-    1.77635684e-15, 2.11245619e-15, 2.51214793e-15, 2.98746420e-15,
1221
-    3.55271368e-15, 4.22491238e-15, 5.02429587e-15, 5.97492839e-15,
1222
-    7.10542736e-15, 8.44982477e-15, 1.00485917e-14, 1.19498568e-14,
1223
-    1.42108547e-14, 1.68996495e-14, 2.00971835e-14, 2.38997136e-14,
1224
-    2.84217094e-14, 3.37992991e-14, 4.01943669e-14, 4.77994272e-14,
1225
-    5.68434189e-14, 6.75985982e-14, 8.03887339e-14, 9.55988543e-14,
1226
-    1.13686838e-13, 1.35197196e-13, 1.60777468e-13, 1.91197709e-13,
1227
-    2.27373675e-13, 2.70394393e-13, 3.21554936e-13, 3.82395417e-13,
1228
-    4.54747351e-13, 5.40788785e-13, 6.43109871e-13, 7.64790834e-13,
1229
-    9.09494702e-13, 1.08157757e-12, 1.28621974e-12, 1.52958167e-12,
1230
-    1.81898940e-12, 2.16315514e-12, 2.57243948e-12, 3.05916334e-12,
1231
-    3.63797881e-12, 4.32631028e-12, 5.14487897e-12, 6.11832668e-12,
1232
-    7.27595761e-12, 8.65262056e-12, 1.02897579e-11, 1.22366534e-11,
1233
-    1.45519152e-11, 1.73052411e-11, 2.05795159e-11, 2.44733067e-11,
1234
-    2.91038305e-11, 3.46104823e-11, 4.11590317e-11, 4.89466134e-11,
1235
-    5.82076609e-11, 6.92209645e-11, 8.23180635e-11, 9.78932268e-11,
1236
-    1.16415322e-10, 1.38441929e-10, 1.64636127e-10, 1.95786454e-10,
1237
-    2.32830644e-10, 2.76883858e-10, 3.29272254e-10, 3.91572907e-10,
1238
-    4.65661287e-10, 5.53767716e-10, 6.58544508e-10, 7.83145814e-10,
1239
-    9.31322575e-10, 1.10753543e-09, 1.31708902e-09, 1.56629163e-09,
1240
-    1.86264515e-09, 2.21507086e-09, 2.63417803e-09, 3.13258326e-09,
1241
-    3.72529030e-09, 4.43014173e-09, 5.26835606e-09, 6.26516652e-09,
1242
-    7.45058060e-09, 8.86028346e-09, 1.05367121e-08, 1.25303330e-08,
1243
-    1.49011612e-08, 1.77205669e-08, 2.10734243e-08, 2.50606661e-08,
1244
-    2.98023224e-08, 3.54411338e-08, 4.21468485e-08, 5.01213321e-08,
1245
-    5.96046448e-08, 7.08822677e-08, 8.42936970e-08, 1.00242664e-07,
1246
-    1.19209290e-07, 1.41764535e-07, 1.68587394e-07, 2.00485328e-07,
1247
-    2.38418579e-07, 2.83529071e-07, 3.37174788e-07, 4.00970657e-07,
1248
-    4.76837158e-07, 5.67058141e-07, 6.74349576e-07, 8.01941314e-07,
1249
-    9.53674316e-07, 1.13411628e-06, 1.34869915e-06, 1.60388263e-06,
1250
-    1.90734863e-06, 2.26823256e-06, 2.69739830e-06, 3.20776526e-06,
1251
-    3.81469727e-06, 4.53646513e-06, 5.39479661e-06, 6.41553051e-06,
1252
-    7.62939453e-06, 9.07293026e-06, 1.07895932e-05, 1.28310610e-05,
1253
-    1.52587891e-05, 1.81458605e-05, 2.15791864e-05, 2.56621220e-05,
1254
-    3.05175781e-05, 3.62917210e-05, 4.31583729e-05, 5.13242441e-05,
1255
-    6.10351562e-05, 7.25834421e-05, 8.63167458e-05, 1.02648488e-04,
1256
-    1.22070312e-04, 1.45166884e-04, 1.72633492e-04, 2.05296976e-04,
1257
-    2.44140625e-04, 2.90333768e-04, 3.45266983e-04, 4.10593953e-04,
1258
-    4.88281250e-04, 5.80667537e-04, 6.90533966e-04, 8.21187906e-04,
1259
-    9.76562500e-04, 1.16133507e-03, 1.38106793e-03, 1.64237581e-03,
1260
-    1.95312500e-03, 2.32267015e-03, 2.76213586e-03, 3.28475162e-03,
1261
-    3.90625000e-03, 4.64534029e-03, 5.52427173e-03, 6.56950324e-03,
1262
-    7.81250000e-03, 9.29068059e-03, 1.10485435e-02, 1.31390065e-02,
1263
-    1.56250000e-02, 1.85813612e-02, 2.20970869e-02, 2.62780130e-02,
1264
-    3.12500000e-02, 3.71627223e-02, 4.41941738e-02, 5.25560260e-02,
1265
-    6.25000000e-02, 7.43254447e-02, 8.83883476e-02, 1.05112052e-01,
1266
-    1.25000000e-01, 1.48650889e-01, 1.76776695e-01, 2.10224104e-01,
1267
-    2.50000000e-01, 2.97301779e-01, 3.53553391e-01, 4.20448208e-01,
1268
-    5.00000000e-01, 5.94603558e-01, 7.07106781e-01, 8.40896415e-01,
1269
-    1.00000000e+00, 1.18920712e+00, 1.41421356e+00, 1.68179283e+00,
1270
-    2.00000000e+00, 2.37841423e+00, 2.82842712e+00, 3.36358566e+00,
1271
-    4.00000000e+00, 4.75682846e+00, 5.65685425e+00, 6.72717132e+00,
1272
-    8.00000000e+00, 9.51365692e+00, 1.13137085e+01, 1.34543426e+01,
1273
-    1.60000000e+01, 1.90273138e+01, 2.26274170e+01, 2.69086853e+01,
1274
-    3.20000000e+01, 3.80546277e+01, 4.52548340e+01, 5.38173706e+01,
1275
-    6.40000000e+01, 7.61092554e+01, 9.05096680e+01, 1.07634741e+02,
1276
-    1.28000000e+02, 1.52218511e+02, 1.81019336e+02, 2.15269482e+02,
1277
-    2.56000000e+02, 3.04437021e+02, 3.62038672e+02, 4.30538965e+02,
1278
-    5.12000000e+02, 6.08874043e+02, 7.24077344e+02, 8.61077929e+02,
1279
-    1.02400000e+03, 1.21774809e+03, 1.44815469e+03, 1.72215586e+03,
1280
-    2.04800000e+03, 2.43549617e+03, 2.89630938e+03, 3.44431172e+03,
1281
-    4.09600000e+03, 4.87099234e+03, 5.79261875e+03, 6.88862343e+03,
1282
-    8.19200000e+03, 9.74198469e+03, 1.15852375e+04, 1.37772469e+04,
1283
-    1.63840000e+04, 1.94839694e+04, 2.31704750e+04, 2.75544937e+04,
1284
-    3.27680000e+04, 3.89679387e+04, 4.63409500e+04, 5.51089875e+04,
1285
-    6.55360000e+04, 7.79358775e+04, 9.26819000e+04, 1.10217975e+05,
1286
-    1.31072000e+05, 1.55871755e+05, 1.85363800e+05, 2.20435950e+05,
1287
-    2.62144000e+05, 3.11743510e+05, 3.70727600e+05, 4.40871900e+05,
1288
-    5.24288000e+05, 6.23487020e+05, 7.41455200e+05, 8.81743800e+05,
1289
-    1.04857600e+06, 1.24697404e+06, 1.48291040e+06, 1.76348760e+06,
1290
-    2.09715200e+06, 2.49394808e+06, 2.96582080e+06, 3.52697520e+06,
1291
-    4.19430400e+06, 4.98789616e+06, 5.93164160e+06, 7.05395040e+06,
1292
-    8.38860800e+06, 9.97579232e+06, 1.18632832e+07, 1.41079008e+07,
1293
-    1.67772160e+07, 1.99515846e+07, 2.37265664e+07, 2.82158016e+07,
1294
-    3.35544320e+07, 3.99031693e+07, 4.74531328e+07, 5.64316032e+07,
1295
-    6.71088640e+07, 7.98063385e+07, 9.49062656e+07, 1.12863206e+08,
1296
-    1.34217728e+08, 1.59612677e+08, 1.89812531e+08, 2.25726413e+08,
1297
-    2.68435456e+08, 3.19225354e+08, 3.79625062e+08, 4.51452825e+08,
1298
-    5.36870912e+08, 6.38450708e+08, 7.59250125e+08, 9.02905651e+08,
1299
-    1.07374182e+09, 1.27690142e+09, 1.51850025e+09, 1.80581130e+09,
1300
-    2.14748365e+09, 2.55380283e+09, 3.03700050e+09, 3.61162260e+09,
1301
-    4.29496730e+09, 5.10760567e+09, 6.07400100e+09, 7.22324521e+09,
1302
-    8.58993459e+09, 1.02152113e+10, 1.21480020e+10, 1.44464904e+10,
1303
-    1.71798692e+10, 2.04304227e+10, 2.42960040e+10, 2.88929808e+10,
1304
-    3.43597384e+10, 4.08608453e+10, 4.85920080e+10, 5.77859616e+10,
1305
-    6.87194767e+10, 8.17216907e+10, 9.71840160e+10, 1.15571923e+11,
1306
-    1.37438953e+11, 1.63443381e+11, 1.94368032e+11, 2.31143847e+11,
1307
-    2.74877907e+11, 3.26886763e+11, 3.88736064e+11, 4.62287693e+11,
1308
-    5.49755814e+11, 6.53773525e+11, 7.77472128e+11, 9.24575386e+11,
1309
-    1.09951163e+12, 1.30754705e+12, 1.55494426e+12, 1.84915077e+12,
1310
-    2.19902326e+12, 2.61509410e+12, 3.10988851e+12, 3.69830155e+12,
1311
-    4.39804651e+12, 5.23018820e+12, 6.21977702e+12, 7.39660309e+12,
1312
-    8.79609302e+12, 1.04603764e+13, 1.24395540e+13, 1.47932062e+13,
1313
-    1.75921860e+13, 2.09207528e+13, 2.48791081e+13, 2.95864124e+13,
1314
-    3.51843721e+13, 4.18415056e+13, 4.97582162e+13, 5.91728247e+13,
1315
-    7.03687442e+13, 8.36830112e+13, 9.95164324e+13, 1.18345649e+14,
1316
-    1.40737488e+14, 1.67366022e+14, 1.99032865e+14, 2.36691299e+14,
1317
-    2.81474977e+14, 3.34732045e+14, 3.98065730e+14, 4.73382598e+14,
1318
-    5.62949953e+14, 6.69464090e+14, 7.96131459e+14, 9.46765196e+14,
1319
-    1.12589991e+15, 1.33892818e+15, 1.59226292e+15, 1.89353039e+15,
1320
-    2.25179981e+15, 2.67785636e+15, 3.18452584e+15, 3.78706078e+15,
1321
-    4.50359963e+15, 5.35571272e+15, 6.36905167e+15, 7.57412156e+15,
1322
-    9.00719925e+15, 1.07114254e+16, 1.27381033e+16, 1.51482431e+16,
1323
-    1.80143985e+16, 2.14228509e+16, 2.54762067e+16, 3.02964863e+16,
1324
-    3.60287970e+16, 4.28457018e+16, 5.09524134e+16, 6.05929725e+16,
1325
-    7.20575940e+16, 8.56914035e+16, 1.01904827e+17, 1.21185945e+17,
1326
-};
1327
-
1328
-#else
1329
-
1330
-float ff_aac_pow2sf_tab[428];
1331
-
1332
-#endif /* CONFIG_HARDCODED_TABLES */
... ...
@@ -32,6 +32,7 @@
32 32
 
33 33
 #include "libavutil/mem.h"
34 34
 #include "aac.h"
35
+#include "aac_tablegen_decl.h"
35 36
 
36 37
 #include <stdint.h>
37 38
 
... ...
@@ -73,10 +74,4 @@ extern const uint16_t * const ff_swb_offset_128 [13];
73 73
 extern const uint8_t ff_tns_max_bands_1024[13];
74 74
 extern const uint8_t ff_tns_max_bands_128 [13];
75 75
 
76
-#if CONFIG_HARDCODED_TABLES
77
-extern const float ff_aac_pow2sf_tab[428];
78
-#else
79
-extern       float ff_aac_pow2sf_tab[428];
80
-#endif /* CONFIG_HARDCODED_TABLES */
81
-
82 76
 #endif /* AVCODEC_AACTAB_H */
... ...
@@ -31,6 +31,7 @@
31 31
 
32 32
 #include <stdint.h>
33 33
 #include "fft.h"
34
+#include "aacps.h"
34 35
 
35 36
 /**
36 37
  * Spectral Band Replication header - spectrum parameters that invoke a reset if they differ from the previous header.
... ...
@@ -133,6 +134,7 @@ typedef struct {
133 133
     ///The number of frequency bands in f_master
134 134
     unsigned           n_master;
135 135
     SBRData            data[2];
136
+    PSContext          ps;
136 137
     ///N_Low and N_High respectively, the number of frequency bands for low and high resolution
137 138
     unsigned           n[2];
138 139
     ///Number of noise floor bands
... ...
@@ -157,7 +159,7 @@ typedef struct {
157 157
     ///QMF output of the HF generator
158 158
     float              X_high[64][40][2];
159 159
     ///QMF values of the reconstructed signal
160
-    DECLARE_ALIGNED(16, float, X)[2][2][32][64];
160
+    DECLARE_ALIGNED(16, float, X)[2][2][38][64];
161 161
     ///Zeroth coefficient used to filter the subband signals
162 162
     float              alpha0[64][2];
163 163
     ///First coefficient used to filter the subband signals
... ...
@@ -176,7 +178,7 @@ typedef struct {
176 176
     float              s_m[7][48];
177 177
     float              gain[7][48];
178 178
     DECLARE_ALIGNED(16, float, qmf_filter_scratch)[5][64];
179
-    RDFTContext        rdft;
179
+    FFTContext         mdct_ana;
180 180
     FFTContext         mdct;
181 181
 } SpectralBandReplication;
182 182