atrac3.c 34.3 KB
Newer Older
Benjamin Larsson's avatar
Benjamin Larsson committed
1
/*
2
 * ATRAC3 compatible decoder
3 4
 * Copyright (c) 2006-2008 Maxim Poliakovski
 * Copyright (c) 2006-2008 Benjamin Larsson
Benjamin Larsson's avatar
Benjamin Larsson committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
24
 * @file
25
 * ATRAC3 compatible decoder.
26 27
 * This decoder handles Sony's ATRAC3 data.
 *
28
 * Container formats used to store ATRAC3 data:
29
 * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
Benjamin Larsson's avatar
Benjamin Larsson committed
30 31
 *
 * To use this decoder, a calling application must supply the extradata
32
 * bytes provided in the containers above.
Benjamin Larsson's avatar
Benjamin Larsson committed
33 34 35 36 37 38
 */

#include <math.h>
#include <stddef.h>
#include <stdio.h>

39
#include "libavutil/attributes.h"
40
#include "libavutil/float_dsp.h"
41
#include "libavutil/libm.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
42 43
#include "avcodec.h"
#include "bytestream.h"
44
#include "fft.h"
45
#include "get_bits.h"
46
#include "internal.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
47

48
#include "atrac.h"
Benjamin Larsson's avatar
Benjamin Larsson committed
49 50
#include "atrac3data.h"

51 52 53 54
#define MIN_CHANNELS    1
#define MAX_CHANNELS    8
#define MAX_JS_PAIRS    8 / 2

Benjamin Larsson's avatar
Benjamin Larsson committed
55
#define JOINT_STEREO    0x12
56
#define SINGLE          0x2
Benjamin Larsson's avatar
Benjamin Larsson committed
57

58 59
#define SAMPLES_PER_FRAME 1024
#define MDCT_SIZE          512
Benjamin Larsson's avatar
Benjamin Larsson committed
60

61
typedef struct GainBlock {
62
    AtracGainInfo g_block[4];
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
} GainBlock;

typedef struct TonalComponent {
    int pos;
    int num_coefs;
    float coef[8];
} TonalComponent;

typedef struct ChannelUnit {
    int            bands_coded;
    int            num_components;
    float          prev_frame[SAMPLES_PER_FRAME];
    int            gc_blk_switch;
    TonalComponent components[64];
    GainBlock      gain_block[2];
Benjamin Larsson's avatar
Benjamin Larsson committed
78

79
    DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
80
    DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
Benjamin Larsson's avatar
Benjamin Larsson committed
81

82 83 84 85
    float          delay_buf1[46]; ///<qmf delay buffers
    float          delay_buf2[46];
    float          delay_buf3[46];
} ChannelUnit;
Benjamin Larsson's avatar
Benjamin Larsson committed
86

87 88
typedef struct ATRAC3Context {
    GetBitContext gb;
Benjamin Larsson's avatar
Benjamin Larsson committed
89 90
    //@{
    /** stream data */
91 92 93
    int coding_mode;

    ChannelUnit *units;
Benjamin Larsson's avatar
Benjamin Larsson committed
94 95 96
    //@}
    //@{
    /** joint-stereo related variables */
97 98 99 100
    int matrix_coeff_index_prev[MAX_JS_PAIRS][4];
    int matrix_coeff_index_now[MAX_JS_PAIRS][4];
    int matrix_coeff_index_next[MAX_JS_PAIRS][4];
    int weighting_delay[MAX_JS_PAIRS][6];
Benjamin Larsson's avatar
Benjamin Larsson committed
101 102 103
    //@}
    //@{
    /** data buffers */
104 105
    uint8_t *decoded_bytes_buffer;
    float temp_buf[1070];
Benjamin Larsson's avatar
Benjamin Larsson committed
106 107 108
    //@}
    //@{
    /** extradata */
109
    int scrambled_stream;
Benjamin Larsson's avatar
Benjamin Larsson committed
110
    //@}
111

112 113
    AtracGCContext    gainc_ctx;
    FFTContext        mdct_ctx;
114
    AVFloatDSPContext *fdsp;
Benjamin Larsson's avatar
Benjamin Larsson committed
115 116
} ATRAC3Context;

117
static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
118
static VLC_TYPE atrac3_vlc_table[4096][2];
119
static VLC   spectral_coeff_tab[7];
Benjamin Larsson's avatar
Benjamin Larsson committed
120

121
/**
122 123
 * Regular 512 points IMDCT without overlapping, with the exception of the
 * swapping of odd bands caused by the reverse spectra of the QMF.
Benjamin Larsson's avatar
Benjamin Larsson committed
124 125 126
 *
 * @param odd_band  1 if the band is an odd band
 */
127
static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
Benjamin Larsson's avatar
Benjamin Larsson committed
128
{
129
    int i;
Benjamin Larsson's avatar
Benjamin Larsson committed
130 131 132

    if (odd_band) {
        /**
133 134 135 136 137 138 139 140 141
         * Reverse the odd bands before IMDCT, this is an effect of the QMF
         * transform or it gives better compression to do it this way.
         * FIXME: It should be possible to handle this in imdct_calc
         * for that to happen a modification of the prerotation step of
         * all SIMD code and C code is needed.
         * Or fix the functions before so they generate a pre reversed spectrum.
         */
        for (i = 0; i < 128; i++)
            FFSWAP(float, input[i], input[255 - i]);
Benjamin Larsson's avatar
Benjamin Larsson committed
142 143
    }

144
    q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
Benjamin Larsson's avatar
Benjamin Larsson committed
145 146

    /* Perform windowing on the output. */
147
    q->fdsp->vector_fmul(output, output, mdct_window, MDCT_SIZE);
Benjamin Larsson's avatar
Benjamin Larsson committed
148 149
}

150 151
/*
 * indata descrambling, only used for data coming from the rm container
Benjamin Larsson's avatar
Benjamin Larsson committed
152
 */
153 154
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
{
Benjamin Larsson's avatar
Benjamin Larsson committed
155 156
    int i, off;
    uint32_t c;
157 158
    const uint32_t *buf;
    uint32_t *output = (uint32_t *)out;
Benjamin Larsson's avatar
Benjamin Larsson committed
159

160 161
    off = (intptr_t)input & 3;
    buf = (const uint32_t *)(input - off);
162 163 164 165
    if (off)
        c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
    else
        c = av_be2ne32(0x537F6103U);
Benjamin Larsson's avatar
Benjamin Larsson committed
166
    bytes += 3 + off;
167 168
    for (i = 0; i < bytes / 4; i++)
        output[i] = c ^ buf[i];
Benjamin Larsson's avatar
Benjamin Larsson committed
169 170

    if (off)
171
        avpriv_request_sample(NULL, "Offset of %d", off);
Benjamin Larsson's avatar
Benjamin Larsson committed
172 173 174 175

    return off;
}

176
static av_cold void init_imdct_window(void)
177
{
178
    int i, j;
Benjamin Larsson's avatar
Benjamin Larsson committed
179

180
    /* generate the mdct window, for details see
Benjamin Larsson's avatar
Benjamin Larsson committed
181
     * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
182 183 184 185 186 187
    for (i = 0, j = 255; i < 128; i++, j--) {
        float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
        float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
        float w  = 0.5 * (wi * wi + wj * wj);
        mdct_window[i] = mdct_window[511 - i] = wi / w;
        mdct_window[j] = mdct_window[511 - j] = wj / w;
188
    }
Benjamin Larsson's avatar
Benjamin Larsson committed
189 190
}

191
static av_cold int atrac3_decode_close(AVCodecContext *avctx)
Benjamin Larsson's avatar
Benjamin Larsson committed
192 193 194
{
    ATRAC3Context *q = avctx->priv_data;

195 196
    av_freep(&q->units);
    av_freep(&q->decoded_bytes_buffer);
197
    av_freep(&q->fdsp);
198

199
    ff_mdct_end(&q->mdct_ctx);
Benjamin Larsson's avatar
Benjamin Larsson committed
200 201 202 203

    return 0;
}

204
/**
205
 * Mantissa decoding
Benjamin Larsson's avatar
Benjamin Larsson committed
206
 *
207 208 209 210
 * @param selector     which table the output values are coded with
 * @param coding_flag  constant length coding or variable length coding
 * @param mantissas    mantissa output table
 * @param num_codes    number of values to get
Benjamin Larsson's avatar
Benjamin Larsson committed
211
 */
212 213 214
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
                                       int coding_flag, int *mantissas,
                                       int num_codes)
Benjamin Larsson's avatar
Benjamin Larsson committed
215
{
216
    int i, code, huff_symb;
Benjamin Larsson's avatar
Benjamin Larsson committed
217 218

    if (selector == 1)
219
        num_codes /= 2;
Benjamin Larsson's avatar
Benjamin Larsson committed
220

221
    if (coding_flag != 0) {
Benjamin Larsson's avatar
Benjamin Larsson committed
222
        /* constant length coding (CLC) */
223
        int num_bits = clc_length_tab[selector];
Benjamin Larsson's avatar
Benjamin Larsson committed
224 225

        if (selector > 1) {
226 227 228
            for (i = 0; i < num_codes; i++) {
                if (num_bits)
                    code = get_sbits(gb, num_bits);
Benjamin Larsson's avatar
Benjamin Larsson committed
229 230
                else
                    code = 0;
231
                mantissas[i] = code;
Benjamin Larsson's avatar
Benjamin Larsson committed
232 233
            }
        } else {
234 235 236
            for (i = 0; i < num_codes; i++) {
                if (num_bits)
                    code = get_bits(gb, num_bits); // num_bits is always 4 in this case
Benjamin Larsson's avatar
Benjamin Larsson committed
237 238
                else
                    code = 0;
239 240
                mantissas[i * 2    ] = mantissa_clc_tab[code >> 2];
                mantissas[i * 2 + 1] = mantissa_clc_tab[code &  3];
Benjamin Larsson's avatar
Benjamin Larsson committed
241 242 243 244 245
            }
        }
    } else {
        /* variable length coding (VLC) */
        if (selector != 1) {
246 247 248 249 250 251
            for (i = 0; i < num_codes; i++) {
                huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
                                     spectral_coeff_tab[selector-1].bits, 3);
                huff_symb += 1;
                code = huff_symb >> 1;
                if (huff_symb & 1)
Benjamin Larsson's avatar
Benjamin Larsson committed
252
                    code = -code;
253
                mantissas[i] = code;
Benjamin Larsson's avatar
Benjamin Larsson committed
254 255
            }
        } else {
256 257 258 259 260
            for (i = 0; i < num_codes; i++) {
                huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
                                     spectral_coeff_tab[selector - 1].bits, 3);
                mantissas[i * 2    ] = mantissa_vlc_tab[huff_symb * 2    ];
                mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
Benjamin Larsson's avatar
Benjamin Larsson committed
261 262 263 264 265
            }
        }
    }
}

266
/**
Benjamin Larsson's avatar
Benjamin Larsson committed
267 268
 * Restore the quantized band spectrum coefficients
 *
269
 * @return subband count, fix for broken specification/files
Benjamin Larsson's avatar
Benjamin Larsson committed
270
 */
271
static int decode_spectrum(GetBitContext *gb, float *output)
Benjamin Larsson's avatar
Benjamin Larsson committed
272
{
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    int num_subbands, coding_mode, i, j, first, last, subband_size;
    int subband_vlc_index[32], sf_index[32];
    int mantissas[128];
    float scale_factor;

    num_subbands = get_bits(gb, 5);  // number of coded subbands
    coding_mode  = get_bits1(gb);    // coding Mode: 0 - VLC/ 1-CLC

    /* get the VLC selector table for the subbands, 0 means not coded */
    for (i = 0; i <= num_subbands; i++)
        subband_vlc_index[i] = get_bits(gb, 3);

    /* read the scale factor indexes from the stream */
    for (i = 0; i <= num_subbands; i++) {
        if (subband_vlc_index[i] != 0)
            sf_index[i] = get_bits(gb, 6);
Benjamin Larsson's avatar
Benjamin Larsson committed
289 290
    }

291 292 293
    for (i = 0; i <= num_subbands; i++) {
        first = subband_tab[i    ];
        last  = subband_tab[i + 1];
Benjamin Larsson's avatar
Benjamin Larsson committed
294

295
        subband_size = last - first;
Benjamin Larsson's avatar
Benjamin Larsson committed
296

297 298
        if (subband_vlc_index[i] != 0) {
            /* decode spectral coefficients for this subband */
Benjamin Larsson's avatar
Benjamin Larsson committed
299 300
            /* TODO: This can be done faster is several blocks share the
             * same VLC selector (subband_vlc_index) */
301 302
            read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
                                       mantissas, subband_size);
Benjamin Larsson's avatar
Benjamin Larsson committed
303

304 305 306
            /* decode the scale factor for this subband */
            scale_factor = ff_atrac_sf_table[sf_index[i]] *
                           inv_max_quant[subband_vlc_index[i]];
Benjamin Larsson's avatar
Benjamin Larsson committed
307

308 309 310
            /* inverse quantize the coefficients */
            for (j = 0; first < last; first++, j++)
                output[first] = mantissas[j] * scale_factor;
Benjamin Larsson's avatar
Benjamin Larsson committed
311
        } else {
312
            /* this subband was not coded, so zero the entire subband */
313
            memset(output + first, 0, subband_size * sizeof(*output));
Benjamin Larsson's avatar
Benjamin Larsson committed
314 315 316
        }
    }

317 318
    /* clear the subbands that were not coded */
    first = subband_tab[i];
319
    memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
320
    return num_subbands;
Benjamin Larsson's avatar
Benjamin Larsson committed
321 322
}

323
/**
Benjamin Larsson's avatar
Benjamin Larsson committed
324 325
 * Restore the quantized tonal components
 *
326 327
 * @param components tonal components
 * @param num_bands  number of coded bands
Benjamin Larsson's avatar
Benjamin Larsson committed
328
 */
329 330
static int decode_tonal_components(GetBitContext *gb,
                                   TonalComponent *components, int num_bands)
Benjamin Larsson's avatar
Benjamin Larsson committed
331
{
332 333 334 335
    int i, b, c, m;
    int nb_components, coding_mode_selector, coding_mode;
    int band_flags[4], mantissa[8];
    int component_count = 0;
Benjamin Larsson's avatar
Benjamin Larsson committed
336

337
    nb_components = get_bits(gb, 5);
Benjamin Larsson's avatar
Benjamin Larsson committed
338 339

    /* no tonal components */
340
    if (nb_components == 0)
Benjamin Larsson's avatar
Benjamin Larsson committed
341 342
        return 0;

343
    coding_mode_selector = get_bits(gb, 2);
Benjamin Larsson's avatar
Benjamin Larsson committed
344
    if (coding_mode_selector == 2)
345
        return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
346 347 348

    coding_mode = coding_mode_selector & 1;

349 350 351 352 353
    for (i = 0; i < nb_components; i++) {
        int coded_values_per_component, quant_step_index;

        for (b = 0; b <= num_bands; b++)
            band_flags[b] = get_bits1(gb);
Benjamin Larsson's avatar
Benjamin Larsson committed
354

355
        coded_values_per_component = get_bits(gb, 3);
Benjamin Larsson's avatar
Benjamin Larsson committed
356

357
        quant_step_index = get_bits(gb, 3);
Benjamin Larsson's avatar
Benjamin Larsson committed
358
        if (quant_step_index <= 1)
359
            return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
360 361 362 363

        if (coding_mode_selector == 3)
            coding_mode = get_bits1(gb);

364 365 366 367
        for (b = 0; b < (num_bands + 1) * 4; b++) {
            int coded_components;

            if (band_flags[b >> 2] == 0)
Benjamin Larsson's avatar
Benjamin Larsson committed
368 369
                continue;

370 371 372 373 374 375
            coded_components = get_bits(gb, 3);

            for (c = 0; c < coded_components; c++) {
                TonalComponent *cmp = &components[component_count];
                int sf_index, coded_values, max_coded_values;
                float scale_factor;
Benjamin Larsson's avatar
Benjamin Larsson committed
376

377
                sf_index = get_bits(gb, 6);
378
                if (component_count >= 64)
379
                    return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
380

381 382 383 384 385
                cmp->pos = b * 64 + get_bits(gb, 6);

                max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
                coded_values     = coded_values_per_component + 1;
                coded_values     = FFMIN(max_coded_values, coded_values);
Benjamin Larsson's avatar
Benjamin Larsson committed
386

387 388
                scale_factor = ff_atrac_sf_table[sf_index] *
                               inv_max_quant[quant_step_index];
Benjamin Larsson's avatar
Benjamin Larsson committed
389

390 391 392 393
                read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
                                           mantissa, coded_values);

                cmp->num_coefs = coded_values;
Benjamin Larsson's avatar
Benjamin Larsson committed
394 395

                /* inverse quant */
396 397
                for (m = 0; m < coded_values; m++)
                    cmp->coef[m] = mantissa[m] * scale_factor;
Benjamin Larsson's avatar
Benjamin Larsson committed
398 399 400 401 402 403

                component_count++;
            }
        }
    }

404
    return component_count;
Benjamin Larsson's avatar
Benjamin Larsson committed
405 406
}

407
/**
Benjamin Larsson's avatar
Benjamin Larsson committed
408 409
 * Decode gain parameters for the coded bands
 *
410 411
 * @param block      the gainblock for the current band
 * @param num_bands  amount of coded bands
Benjamin Larsson's avatar
Benjamin Larsson committed
412
 */
413 414
static int decode_gain_control(GetBitContext *gb, GainBlock *block,
                               int num_bands)
Benjamin Larsson's avatar
Benjamin Larsson committed
415
{
416
    int b, j;
417 418
    int *level, *loc;

419
    AtracGainInfo *gain = block->g_block;
420

421 422
    for (b = 0; b <= num_bands; b++) {
        gain[b].num_points = get_bits(gb, 3);
423 424
        level              = gain[b].lev_code;
        loc                = gain[b].loc_code;
425

426
        for (j = 0; j < gain[b].num_points; j++) {
427 428 429
            level[j] = get_bits(gb, 4);
            loc[j]   = get_bits(gb, 5);
            if (j && loc[j] <= loc[j - 1])
430
                return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
431 432 433 434
        }
    }

    /* Clear the unused blocks. */
435 436
    for (; b < 4 ; b++)
        gain[b].num_points = 0;
Benjamin Larsson's avatar
Benjamin Larsson committed
437 438 439 440

    return 0;
}

441
/**
Benjamin Larsson's avatar
Benjamin Larsson committed
442 443
 * Combine the tonal band spectrum and regular band spectrum
 *
444 445 446 447
 * @param spectrum        output spectrum buffer
 * @param num_components  number of tonal components
 * @param components      tonal components for this band
 * @return                position of the last tonal coefficient
Benjamin Larsson's avatar
Benjamin Larsson committed
448
 */
449 450
static int add_tonal_components(float *spectrum, int num_components,
                                TonalComponent *components)
Benjamin Larsson's avatar
Benjamin Larsson committed
451
{
452 453
    int i, j, last_pos = -1;
    float *input, *output;
Benjamin Larsson's avatar
Benjamin Larsson committed
454

455 456 457 458
    for (i = 0; i < num_components; i++) {
        last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
        input    = components[i].coef;
        output   = &spectrum[components[i].pos];
Benjamin Larsson's avatar
Benjamin Larsson committed
459

460
        for (j = 0; j < components[i].num_coefs; j++)
461
            output[j] += input[j];
Benjamin Larsson's avatar
Benjamin Larsson committed
462
    }
463

464
    return last_pos;
Benjamin Larsson's avatar
Benjamin Larsson committed
465 466
}

467 468
#define INTERPOLATE(old, new, nsample) \
    ((old) + (nsample) * 0.125 * ((new) - (old)))
Benjamin Larsson's avatar
Benjamin Larsson committed
469

470 471
static void reverse_matrixing(float *su1, float *su2, int *prev_code,
                              int *curr_code)
Benjamin Larsson's avatar
Benjamin Larsson committed
472
{
473 474
    int i, nsample, band;
    float mc1_l, mc1_r, mc2_l, mc2_r;
Benjamin Larsson's avatar
Benjamin Larsson committed
475

476 477 478
    for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
        int s1 = prev_code[i];
        int s2 = curr_code[i];
479
        nsample = band;
Benjamin Larsson's avatar
Benjamin Larsson committed
480 481 482

        if (s1 != s2) {
            /* Selector value changed, interpolation needed. */
483 484 485 486
            mc1_l = matrix_coeffs[s1 * 2    ];
            mc1_r = matrix_coeffs[s1 * 2 + 1];
            mc2_l = matrix_coeffs[s2 * 2    ];
            mc2_r = matrix_coeffs[s2 * 2 + 1];
Benjamin Larsson's avatar
Benjamin Larsson committed
487 488

            /* Interpolation is done over the first eight samples. */
489 490 491 492 493 494 495
            for (; nsample < band + 8; nsample++) {
                float c1 = su1[nsample];
                float c2 = su2[nsample];
                c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
                     c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
                su1[nsample] = c2;
                su2[nsample] = c1 * 2.0 - c2;
Benjamin Larsson's avatar
Benjamin Larsson committed
496 497 498 499 500
            }
        }

        /* Apply the matrix without interpolation. */
        switch (s2) {
501
        case 0:     /* M/S decoding */
502 503 504 505 506
            for (; nsample < band + 256; nsample++) {
                float c1 = su1[nsample];
                float c2 = su2[nsample];
                su1[nsample] =  c2       * 2.0;
                su2[nsample] = (c1 - c2) * 2.0;
507 508 509
            }
            break;
        case 1:
510 511 512 513 514
            for (; nsample < band + 256; nsample++) {
                float c1 = su1[nsample];
                float c2 = su2[nsample];
                su1[nsample] = (c1 + c2) *  2.0;
                su2[nsample] =  c2       * -2.0;
515 516 517 518
            }
            break;
        case 2:
        case 3:
519 520 521 522 523
            for (; nsample < band + 256; nsample++) {
                float c1 = su1[nsample];
                float c2 = su2[nsample];
                su1[nsample] = c1 + c2;
                su2[nsample] = c1 - c2;
524 525 526
            }
            break;
        default:
527
            av_assert1(0);
Benjamin Larsson's avatar
Benjamin Larsson committed
528 529 530 531
        }
    }
}

532 533 534
static void get_channel_weights(int index, int flag, float ch[2])
{
    if (index == 7) {
Benjamin Larsson's avatar
Benjamin Larsson committed
535 536 537
        ch[0] = 1.0;
        ch[1] = 1.0;
    } else {
538 539 540
        ch[0] = (index & 7) / 7.0;
        ch[1] = sqrt(2 - ch[0] * ch[0]);
        if (flag)
Benjamin Larsson's avatar
Benjamin Larsson committed
541 542 543 544
            FFSWAP(float, ch[0], ch[1]);
    }
}

545
static void channel_weighting(float *su1, float *su2, int *p3)
Benjamin Larsson's avatar
Benjamin Larsson committed
546
{
547
    int band, nsample;
Benjamin Larsson's avatar
Benjamin Larsson committed
548 549 550
    /* w[x][y] y=0 is left y=1 is right */
    float w[2][2];

551 552 553
    if (p3[1] != 7 || p3[3] != 7) {
        get_channel_weights(p3[1], p3[0], w[0]);
        get_channel_weights(p3[3], p3[2], w[1]);
Benjamin Larsson's avatar
Benjamin Larsson committed
554

555 556 557 558
        for (band = 256; band < 4 * 256; band += 256) {
            for (nsample = band; nsample < band + 8; nsample++) {
                su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
                su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
Benjamin Larsson's avatar
Benjamin Larsson committed
559
            }
560 561 562
            for(; nsample < band + 256; nsample++) {
                su1[nsample] *= w[1][0];
                su2[nsample] *= w[1][1];
Benjamin Larsson's avatar
Benjamin Larsson committed
563 564 565 566 567
            }
        }
    }
}

568
/**
Benjamin Larsson's avatar
Benjamin Larsson committed
569 570
 * Decode a Sound Unit
 *
571 572 573
 * @param snd           the channel unit to be used
 * @param output        the decoded samples before IQMF in float representation
 * @param channel_num   channel number
574
 * @param coding_mode   the coding mode (JOINT_STEREO or single channels)
Benjamin Larsson's avatar
Benjamin Larsson committed
575
 */
576 577 578
static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
                                     ChannelUnit *snd, float *output,
                                     int channel_num, int coding_mode)
Benjamin Larsson's avatar
Benjamin Larsson committed
579
{
580 581 582
    int band, ret, num_subbands, last_tonal, num_bands;
    GainBlock *gain1 = &snd->gain_block[    snd->gc_blk_switch];
    GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
Benjamin Larsson's avatar
Benjamin Larsson committed
583

584
    if (coding_mode == JOINT_STEREO && (channel_num % 2) == 1) {
585
        if (get_bits(gb, 2) != 3) {
Benjamin Larsson's avatar
Benjamin Larsson committed
586
            av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
587
            return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
588 589
        }
    } else {
590
        if (get_bits(gb, 6) != 0x28) {
Benjamin Larsson's avatar
Benjamin Larsson committed
591
            av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
592
            return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
593 594 595 596
        }
    }

    /* number of coded QMF bands */
597
    snd->bands_coded = get_bits(gb, 2);
Benjamin Larsson's avatar
Benjamin Larsson committed
598

599 600 601
    ret = decode_gain_control(gb, gain2, snd->bands_coded);
    if (ret)
        return ret;
Benjamin Larsson's avatar
Benjamin Larsson committed
602

603 604
    snd->num_components = decode_tonal_components(gb, snd->components,
                                                  snd->bands_coded);
605 606
    if (snd->num_components < 0)
        return snd->num_components;
Benjamin Larsson's avatar
Benjamin Larsson committed
607

608
    num_subbands = decode_spectrum(gb, snd->spectrum);
Benjamin Larsson's avatar
Benjamin Larsson committed
609 610

    /* Merge the decoded spectrum and tonal components. */
611 612
    last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
                                      snd->components);
Benjamin Larsson's avatar
Benjamin Larsson committed
613 614


615 616 617 618 619
    /* calculate number of used MLT/QMF bands according to the amount of coded
       spectral lines */
    num_bands = (subband_tab[num_subbands] - 1) >> 8;
    if (last_tonal >= 0)
        num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
Benjamin Larsson's avatar
Benjamin Larsson committed
620 621 622


    /* Reconstruct time domain samples. */
623
    for (band = 0; band < 4; band++) {
Benjamin Larsson's avatar
Benjamin Larsson committed
624
        /* Perform the IMDCT step without overlapping. */
625 626 627
        if (band <= num_bands)
            imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
        else
628
            memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
Benjamin Larsson's avatar
Benjamin Larsson committed
629 630

        /* gain compensation and overlapping */
631 632 633 634
        ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
                                   &snd->prev_frame[band * 256],
                                   &gain1->g_block[band], &gain2->g_block[band],
                                   256, &output[band * 256]);
Benjamin Larsson's avatar
Benjamin Larsson committed
635 636 637
    }

    /* Swap the gain control buffers for the next frame. */
638
    snd->gc_blk_switch ^= 1;
Benjamin Larsson's avatar
Benjamin Larsson committed
639 640 641 642

    return 0;
}

643
static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
644
                        float **out_samples)
Benjamin Larsson's avatar
Benjamin Larsson committed
645
{
646
    ATRAC3Context *q = avctx->priv_data;
647
    int ret, i, ch;
648
    uint8_t *ptr1;
Benjamin Larsson's avatar
Benjamin Larsson committed
649

650
    if (q->coding_mode == JOINT_STEREO) {
Benjamin Larsson's avatar
Benjamin Larsson committed
651 652
        /* channel coupling mode */

653 654
        /* Decode sound unit pairs (channels are expected to be even).
         * Multichannel joint stereo interleaves pairs (6ch: 2ch + 2ch + 2ch) */
655
        const uint8_t *js_databuf;
656
        int js_pair, js_block_align;
Benjamin Larsson's avatar
Benjamin Larsson committed
657

658
        js_block_align = (avctx->block_align / avctx->channels) * 2; /* block pair */
Benjamin Larsson's avatar
Benjamin Larsson committed
659

660 661 662
        for (ch = 0; ch < avctx->channels; ch = ch + 2) {
            js_pair = ch/2;
            js_databuf = databuf + js_pair * js_block_align; /* align to current pair */
Benjamin Larsson's avatar
Benjamin Larsson committed
663

664 665 666
            /* Set the bitstream reader at the start of first channel sound unit. */
            init_get_bits(&q->gb,
                          js_databuf, js_block_align * 8);
Benjamin Larsson's avatar
Benjamin Larsson committed
667

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
            /* decode Sound Unit 1 */
            ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch],
                                            out_samples[ch], ch, JOINT_STEREO);
            if (ret != 0)
                return ret;

            /* Framedata of the su2 in the joint-stereo mode is encoded in
             * reverse byte order so we need to swap it first. */
            if (js_databuf == q->decoded_bytes_buffer) {
                uint8_t *ptr2 = q->decoded_bytes_buffer + js_block_align - 1;
                ptr1          = q->decoded_bytes_buffer;
                for (i = 0; i < js_block_align / 2; i++, ptr1++, ptr2--)
                    FFSWAP(uint8_t, *ptr1, *ptr2);
            } else {
                const uint8_t *ptr2 = js_databuf + js_block_align - 1;
                for (i = 0; i < js_block_align; i++)
                    q->decoded_bytes_buffer[i] = *ptr2--;
            }

            /* Skip the sync codes (0xF8). */
            ptr1 = q->decoded_bytes_buffer;
            for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
                if (i >= js_block_align)
                    return AVERROR_INVALIDDATA;
            }
Benjamin Larsson's avatar
Benjamin Larsson committed
693 694


695
            /* set the bitstream reader at the start of the second Sound Unit */
696
            ret = init_get_bits8(&q->gb,
697
                           ptr1, q->decoded_bytes_buffer + js_block_align - ptr1);
698 699
            if (ret < 0)
                return ret;
Benjamin Larsson's avatar
Benjamin Larsson committed
700

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
            /* Fill the Weighting coeffs delay buffer */
            memmove(q->weighting_delay[js_pair], &q->weighting_delay[js_pair][2],
                    4 * sizeof(*q->weighting_delay[js_pair]));
            q->weighting_delay[js_pair][4] = get_bits1(&q->gb);
            q->weighting_delay[js_pair][5] = get_bits(&q->gb, 3);

            for (i = 0; i < 4; i++) {
                q->matrix_coeff_index_prev[js_pair][i] = q->matrix_coeff_index_now[js_pair][i];
                q->matrix_coeff_index_now[js_pair][i]  = q->matrix_coeff_index_next[js_pair][i];
                q->matrix_coeff_index_next[js_pair][i] = get_bits(&q->gb, 2);
            }

            /* Decode Sound Unit 2. */
            ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch+1],
                                            out_samples[ch+1], ch+1, JOINT_STEREO);
            if (ret != 0)
                return ret;

            /* Reconstruct the channel coefficients. */
            reverse_matrixing(out_samples[ch], out_samples[ch+1],
                              q->matrix_coeff_index_prev[js_pair],
                              q->matrix_coeff_index_now[js_pair]);

            channel_weighting(out_samples[ch], out_samples[ch+1], q->weighting_delay[js_pair]);
        }
Benjamin Larsson's avatar
Benjamin Larsson committed
726
    } else {
727
        /* single channels */
Benjamin Larsson's avatar
Benjamin Larsson committed
728
        /* Decode the channel sound units. */
729
        for (i = 0; i < avctx->channels; i++) {
Benjamin Larsson's avatar
Benjamin Larsson committed
730
            /* Set the bitstream reader at the start of a channel sound unit. */
731
            init_get_bits(&q->gb,
732 733
                          databuf + i * avctx->block_align / avctx->channels,
                          avctx->block_align * 8 / avctx->channels);
Benjamin Larsson's avatar
Benjamin Larsson committed
734

735 736 737 738
            ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
                                            out_samples[i], i, q->coding_mode);
            if (ret != 0)
                return ret;
Benjamin Larsson's avatar
Benjamin Larsson committed
739 740 741 742
        }
    }

    /* Apply the iQMF synthesis filter. */
743
    for (i = 0; i < avctx->channels; i++) {
744 745 746 747 748 749 750
        float *p1 = out_samples[i];
        float *p2 = p1 + 256;
        float *p3 = p2 + 256;
        float *p4 = p3 + 256;
        ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
        ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
        ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
Benjamin Larsson's avatar
Benjamin Larsson committed
751 752 753 754 755
    }

    return 0;
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
static int al_decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
                           int size, float **out_samples)
{
    ATRAC3Context *q = avctx->priv_data;
    int ret, i;

    /* Set the bitstream reader at the start of a channel sound unit. */
    init_get_bits(&q->gb, databuf, size * 8);
    /* single channels */
    /* Decode the channel sound units. */
    for (i = 0; i < avctx->channels; i++) {
        ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
                                        out_samples[i], i, q->coding_mode);
        if (ret != 0)
            return ret;
        while (i < avctx->channels && get_bits_left(&q->gb) > 6 && show_bits(&q->gb, 6) != 0x28) {
            skip_bits(&q->gb, 1);
        }
    }

    /* Apply the iQMF synthesis filter. */
    for (i = 0; i < avctx->channels; i++) {
        float *p1 = out_samples[i];
        float *p2 = p1 + 256;
        float *p3 = p2 + 256;
        float *p4 = p3 + 256;
        ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
        ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
        ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
    }

    return 0;
}

790 791 792
static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
                               int *got_frame_ptr, AVPacket *avpkt)
{
793
    AVFrame *frame     = data;
794 795
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
Benjamin Larsson's avatar
Benjamin Larsson committed
796
    ATRAC3Context *q = avctx->priv_data;
797 798
    int ret;
    const uint8_t *databuf;
Benjamin Larsson's avatar
Benjamin Larsson committed
799

800 801 802
    if (buf_size < avctx->block_align) {
        av_log(avctx, AV_LOG_ERROR,
               "Frame too small (%d bytes). Truncated file?\n", buf_size);
803
        return AVERROR_INVALIDDATA;
804
    }
Benjamin Larsson's avatar
Benjamin Larsson committed
805

806
    /* get output buffer */
807
    frame->nb_samples = SAMPLES_PER_FRAME;
808
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
809
        return ret;
Benjamin Larsson's avatar
Benjamin Larsson committed
810 811 812 813 814 815 816 817 818

    /* Check if we need to descramble and what buffer to pass on. */
    if (q->scrambled_stream) {
        decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
        databuf = q->decoded_bytes_buffer;
    } else {
        databuf = buf;
    }

819
    ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
820
    if (ret) {
821
        av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n");
822
        return ret;
Benjamin Larsson's avatar
Benjamin Larsson committed
823 824
    }

825
    *got_frame_ptr = 1;
Benjamin Larsson's avatar
Benjamin Larsson committed
826 827 828 829

    return avctx->block_align;
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
static int atrac3al_decode_frame(AVCodecContext *avctx, void *data,
                                 int *got_frame_ptr, AVPacket *avpkt)
{
    AVFrame *frame = data;
    int ret;

    frame->nb_samples = SAMPLES_PER_FRAME;
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
        return ret;

    ret = al_decode_frame(avctx, avpkt->data, avpkt->size,
                          (float **)frame->extended_data);
    if (ret) {
        av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n");
        return ret;
    }

    *got_frame_ptr = 1;

    return avpkt->size;
}

852
static av_cold void atrac3_init_static_data(void)
853 854 855
{
    int i;

856
    init_imdct_window();
857 858 859 860 861 862 863 864 865 866 867 868 869
    ff_atrac_generate_tables();

    /* Initialize the VLC tables. */
    for (i = 0; i < 7; i++) {
        spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
        spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
                                                atrac3_vlc_offs[i    ];
        init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
                 huff_bits[i],  1, 1,
                 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
    }
}

870
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
Benjamin Larsson's avatar
Benjamin Larsson committed
871
{
872
    static int static_init_done;
873
    int i, js_pair, ret;
874
    int version, delay, samples_per_frame, frame_factor;
Michael Niedermayer's avatar
Michael Niedermayer committed
875
    const uint8_t *edata_ptr = avctx->extradata;
Benjamin Larsson's avatar
Benjamin Larsson committed
876 877
    ATRAC3Context *q = avctx->priv_data;

878
    if (avctx->channels < MIN_CHANNELS || avctx->channels > MAX_CHANNELS) {
879 880 881 882
        av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
        return AVERROR(EINVAL);
    }

883 884 885 886
    if (!static_init_done)
        atrac3_init_static_data();
    static_init_done = 1;

Benjamin Larsson's avatar
Benjamin Larsson committed
887
    /* Take care of the codec-specific extradata. */
888 889 890 891 892 893
    if (avctx->codec_id == AV_CODEC_ID_ATRAC3AL) {
        version           = 4;
        samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
        delay             = 0x88E;
        q->coding_mode    = SINGLE;
    } else if (avctx->extradata_size == 14) {
Benjamin Larsson's avatar
Benjamin Larsson committed
894
        /* Parse the extradata, WAV format */
895 896
        av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
               bytestream_get_le16(&edata_ptr));  // Unknown value always 1
897
        edata_ptr += 4;                             // samples per channel
898 899 900
        q->coding_mode = bytestream_get_le16(&edata_ptr);
        av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
               bytestream_get_le16(&edata_ptr));  //Dupe of coding mode
901
        frame_factor = bytestream_get_le16(&edata_ptr);  // Unknown always 1
902 903
        av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
               bytestream_get_le16(&edata_ptr));  // Unknown always 0
Benjamin Larsson's avatar
Benjamin Larsson committed
904 905

        /* setup */
906
        samples_per_frame    = SAMPLES_PER_FRAME * avctx->channels;
907
        version              = 4;
908
        delay                = 0x88E;
909
        q->coding_mode       = q->coding_mode ? JOINT_STEREO : SINGLE;
910 911
        q->scrambled_stream  = 0;

912 913 914
        if (avctx->block_align !=  96 * avctx->channels * frame_factor &&
            avctx->block_align != 152 * avctx->channels * frame_factor &&
            avctx->block_align != 192 * avctx->channels * frame_factor) {
915
            av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
916
                   "configuration %d/%d/%d\n", avctx->block_align,
917
                   avctx->channels, frame_factor);
918
            return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
919
        }
920
    } else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) {
Benjamin Larsson's avatar
Benjamin Larsson committed
921
        /* Parse the extradata, RM format. */
922
        version                = bytestream_get_be32(&edata_ptr);
923
        samples_per_frame      = bytestream_get_be16(&edata_ptr);
924
        delay                  = bytestream_get_be16(&edata_ptr);
925 926
        q->coding_mode         = bytestream_get_be16(&edata_ptr);
        q->scrambled_stream    = 1;
Benjamin Larsson's avatar
Benjamin Larsson committed
927 928

    } else {
929
        av_log(avctx, AV_LOG_ERROR, "Unknown extradata size %d.\n",
930
               avctx->extradata_size);
931
        return AVERROR(EINVAL);
Benjamin Larsson's avatar
Benjamin Larsson committed
932 933
    }

934 935
    /* Check the extradata */

936 937
    if (version != 4) {
        av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
938
        return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
939 940
    }

941
    if (samples_per_frame != SAMPLES_PER_FRAME * avctx->channels) {
942
        av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
943
               samples_per_frame);
944
        return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
945 946
    }

947
    if (delay != 0x88E) {
948
        av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
949
               delay);
950
        return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
951 952
    }

953 954
    if (q->coding_mode == SINGLE)
        av_log(avctx, AV_LOG_DEBUG, "Single channels detected.\n");
955
    else if (q->coding_mode == JOINT_STEREO) {
956 957
        if (avctx->channels % 2 == 1) { /* Joint stereo channels must be even */
            av_log(avctx, AV_LOG_ERROR, "Invalid joint stereo channel configuration.\n");
958
            return AVERROR_INVALIDDATA;
959
        }
960
        av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
961
    } else {
962 963
        av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
               q->coding_mode);
964
        return AVERROR_INVALIDDATA;
Benjamin Larsson's avatar
Benjamin Larsson committed
965 966
    }

967
    if (avctx->block_align >= UINT_MAX / 2)
968
        return AVERROR(EINVAL);
Benjamin Larsson's avatar
Benjamin Larsson committed
969

970
    q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
971
                                         AV_INPUT_BUFFER_PADDING_SIZE);
972
    if (!q->decoded_bytes_buffer)
973
        return AVERROR(ENOMEM);
Benjamin Larsson's avatar
Benjamin Larsson committed
974

975
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
976

977 978
    /* initialize the MDCT transform */
    if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
979 980 981 982
        av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
        av_freep(&q->decoded_bytes_buffer);
        return ret;
    }
Benjamin Larsson's avatar
Benjamin Larsson committed
983 984

    /* init the joint-stereo decoding data */
985 986 987 988 989 990 991 992 993 994 995 996 997
    for (js_pair = 0; js_pair < MAX_JS_PAIRS; js_pair++) {
        q->weighting_delay[js_pair][0] = 0;
        q->weighting_delay[js_pair][1] = 7;
        q->weighting_delay[js_pair][2] = 0;
        q->weighting_delay[js_pair][3] = 7;
        q->weighting_delay[js_pair][4] = 0;
        q->weighting_delay[js_pair][5] = 7;

        for (i = 0; i < 4; i++) {
            q->matrix_coeff_index_prev[js_pair][i] = 3;
            q->matrix_coeff_index_now[js_pair][i]  = 3;
            q->matrix_coeff_index_next[js_pair][i] = 3;
        }
Benjamin Larsson's avatar
Benjamin Larsson committed
998 999
    }

1000
    ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
1001
    q->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
Benjamin Larsson's avatar
Benjamin Larsson committed
1002

1003
    q->units = av_mallocz_array(avctx->channels, sizeof(*q->units));
1004
    if (!q->units || !q->fdsp) {
1005
        atrac3_decode_close(avctx);
1006 1007
        return AVERROR(ENOMEM);
    }
Benjamin Larsson's avatar
Benjamin Larsson committed
1008 1009 1010 1011

    return 0;
}

1012 1013
AVCodec ff_atrac3_decoder = {
    .name             = "atrac3",
1014
    .long_name        = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
1015 1016 1017 1018 1019 1020
    .type             = AVMEDIA_TYPE_AUDIO,
    .id               = AV_CODEC_ID_ATRAC3,
    .priv_data_size   = sizeof(ATRAC3Context),
    .init             = atrac3_decode_init,
    .close            = atrac3_decode_close,
    .decode           = atrac3_decode_frame,
1021
    .capabilities     = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1022 1023
    .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
                                                        AV_SAMPLE_FMT_NONE },
Benjamin Larsson's avatar
Benjamin Larsson committed
1024
};
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

AVCodec ff_atrac3al_decoder = {
    .name             = "atrac3al",
    .long_name        = NULL_IF_CONFIG_SMALL("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"),
    .type             = AVMEDIA_TYPE_AUDIO,
    .id               = AV_CODEC_ID_ATRAC3AL,
    .priv_data_size   = sizeof(ATRAC3Context),
    .init             = atrac3_decode_init,
    .close            = atrac3_decode_close,
    .decode           = atrac3al_decode_frame,
    .capabilities     = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
    .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
                                                        AV_SAMPLE_FMT_NONE },
};