Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aacsbr.c @ 932963b8

History | View | Annotate | Download (65.2 KB)

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