1/*
2 * ATRAC3+ compatible decoder
3 *
4 * Copyright (c) 2010-2013 Maxim Poliakovski
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
24 * @file
25 * Sony ATRAC3+ compatible decoder.
26 *
27 * Container formats used to store its data:
28 * RIFF WAV (.at3) and Sony OpenMG (.oma, .aa3).
29 *
30 * Technical description of this codec can be found here:
31 * http://wiki.multimedia.cx/index.php?title=ATRAC3plus
32 *
33 * Kudos to Benjamin Larsson and Michael Karcher
34 * for their precious technical help!
35 */
36
37#include <stdint.h>
38#include <string.h>
39
40#include "libavutil/channel_layout.h"
41#include "libavutil/float_dsp.h"
42#include "avcodec.h"
43#include "get_bits.h"
44#include "internal.h"
45#include "atrac.h"
46#include "atrac3plus.h"
47
48typedef struct ATRAC3PContext {
49    GetBitContext gb;
50    AVFloatDSPContext fdsp;
51
52    DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES];  ///< quantized MDCT spectrum
53    DECLARE_ALIGNED(32, float, mdct_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the IMDCT
54    DECLARE_ALIGNED(32, float, time_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the gain compensation
55    DECLARE_ALIGNED(32, float, outp_buf)[2][ATRAC3P_FRAME_SAMPLES];
56
57    AtracGCContext gainc_ctx;   ///< gain compensation context
58    FFTContext mdct_ctx;
59    FFTContext ipqf_dct_ctx;    ///< IDCT context used by IPQF
60
61    Atrac3pChanUnitCtx *ch_units;   ///< global channel units
62
63    int num_channel_blocks;     ///< number of channel blocks
64    uint8_t channel_blocks[5];  ///< channel configuration descriptor
65    uint64_t my_channel_layout; ///< current channel layout
66} ATRAC3PContext;
67
68static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
69{
70    av_free(((ATRAC3PContext *)(avctx->priv_data))->ch_units);
71
72    return 0;
73}
74
75static av_cold int set_channel_params(ATRAC3PContext *ctx,
76                                      AVCodecContext *avctx)
77{
78    memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
79
80    switch (avctx->channels) {
81    case 1:
82        if (avctx->channel_layout != AV_CH_FRONT_LEFT)
83            avctx->channel_layout = AV_CH_LAYOUT_MONO;
84
85        ctx->num_channel_blocks = 1;
86        ctx->channel_blocks[0]  = CH_UNIT_MONO;
87        break;
88    case 2:
89        avctx->channel_layout   = AV_CH_LAYOUT_STEREO;
90        ctx->num_channel_blocks = 1;
91        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
92        break;
93    case 3:
94        avctx->channel_layout   = AV_CH_LAYOUT_SURROUND;
95        ctx->num_channel_blocks = 2;
96        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
97        ctx->channel_blocks[1]  = CH_UNIT_MONO;
98        break;
99    case 4:
100        avctx->channel_layout   = AV_CH_LAYOUT_4POINT0;
101        ctx->num_channel_blocks = 3;
102        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
103        ctx->channel_blocks[1]  = CH_UNIT_MONO;
104        ctx->channel_blocks[2]  = CH_UNIT_MONO;
105        break;
106    case 6:
107        avctx->channel_layout   = AV_CH_LAYOUT_5POINT1_BACK;
108        ctx->num_channel_blocks = 4;
109        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
110        ctx->channel_blocks[1]  = CH_UNIT_MONO;
111        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
112        ctx->channel_blocks[3]  = CH_UNIT_MONO;
113        break;
114    case 7:
115        avctx->channel_layout   = AV_CH_LAYOUT_6POINT1_BACK;
116        ctx->num_channel_blocks = 5;
117        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
118        ctx->channel_blocks[1]  = CH_UNIT_MONO;
119        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
120        ctx->channel_blocks[3]  = CH_UNIT_MONO;
121        ctx->channel_blocks[4]  = CH_UNIT_MONO;
122        break;
123    case 8:
124        avctx->channel_layout   = AV_CH_LAYOUT_7POINT1;
125        ctx->num_channel_blocks = 5;
126        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
127        ctx->channel_blocks[1]  = CH_UNIT_MONO;
128        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
129        ctx->channel_blocks[3]  = CH_UNIT_STEREO;
130        ctx->channel_blocks[4]  = CH_UNIT_MONO;
131        break;
132    default:
133        av_log(avctx, AV_LOG_ERROR,
134               "Unsupported channel count: %d!\n", avctx->channels);
135        return AVERROR_INVALIDDATA;
136    }
137
138    return 0;
139}
140
141static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
142{
143    ATRAC3PContext *ctx = avctx->priv_data;
144    int i, ch, ret;
145
146    if (!avctx->block_align) {
147        av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
148        return AVERROR(EINVAL);
149    }
150
151    ff_atrac3p_init_vlcs();
152
153    avpriv_float_dsp_init(&ctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
154
155    /* initialize IPQF */
156    ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
157
158    ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
159
160    ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
161
162    ff_atrac3p_init_wave_synth();
163
164    if ((ret = set_channel_params(ctx, avctx)) < 0)
165        return ret;
166
167    ctx->my_channel_layout = avctx->channel_layout;
168
169    ctx->ch_units = av_mallocz_array(ctx->num_channel_blocks, sizeof(*ctx->ch_units));
170
171    if (!ctx->ch_units) {
172        atrac3p_decode_close(avctx);
173        return AVERROR(ENOMEM);
174    }
175
176    for (i = 0; i < ctx->num_channel_blocks; i++) {
177        for (ch = 0; ch < 2; ch++) {
178            ctx->ch_units[i].channels[ch].ch_num          = ch;
179            ctx->ch_units[i].channels[ch].wnd_shape       = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
180            ctx->ch_units[i].channels[ch].wnd_shape_prev  = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
181            ctx->ch_units[i].channels[ch].gain_data       = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
182            ctx->ch_units[i].channels[ch].gain_data_prev  = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
183            ctx->ch_units[i].channels[ch].tones_info      = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
184            ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
185        }
186
187        ctx->ch_units[i].waves_info      = &ctx->ch_units[i].wave_synth_hist[0];
188        ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
189    }
190
191    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
192
193    return 0;
194}
195
196static void decode_residual_spectrum(Atrac3pChanUnitCtx *ctx,
197                                     float out[2][ATRAC3P_FRAME_SAMPLES],
198                                     int num_channels,
199                                     AVCodecContext *avctx)
200{
201    int i, sb, ch, qu, nspeclines, RNG_index;
202    float *dst, q;
203    int16_t *src;
204    /* calculate RNG table index for each subband */
205    int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
206
207    if (ctx->mute_flag) {
208        for (ch = 0; ch < num_channels; ch++)
209            memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
210        return;
211    }
212
213    for (qu = 0, RNG_index = 0; qu < ctx->used_quant_units; qu++)
214        RNG_index += ctx->channels[0].qu_sf_idx[qu] +
215                     ctx->channels[1].qu_sf_idx[qu];
216
217    for (sb = 0; sb < ctx->num_coded_subbands; sb++, RNG_index += 128)
218        sb_RNG_index[sb] = RNG_index & 0x3FC;
219
220    /* inverse quant and power compensation */
221    for (ch = 0; ch < num_channels; ch++) {
222        /* clear channel's residual spectrum */
223        memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
224
225        for (qu = 0; qu < ctx->used_quant_units; qu++) {
226            src        = &ctx->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
227            dst        = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
228            nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
229                         ff_atrac3p_qu_to_spec_pos[qu];
230
231            if (ctx->channels[ch].qu_wordlen[qu] > 0) {
232                q = ff_atrac3p_sf_tab[ctx->channels[ch].qu_sf_idx[qu]] *
233                    ff_atrac3p_mant_tab[ctx->channels[ch].qu_wordlen[qu]];
234                for (i = 0; i < nspeclines; i++)
235                    dst[i] = src[i] * q;
236            }
237        }
238
239        for (sb = 0; sb < ctx->num_coded_subbands; sb++)
240            ff_atrac3p_power_compensation(ctx, ch, &out[ch][0],
241                                          sb_RNG_index[sb], sb);
242    }
243
244    if (ctx->unit_type == CH_UNIT_STEREO) {
245        for (sb = 0; sb < ctx->num_coded_subbands; sb++) {
246            if (ctx->swap_channels[sb]) {
247                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
248                    FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
249                                  out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
250            }
251
252            /* flip coefficients' sign if requested */
253            if (ctx->negate_coeffs[sb])
254                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
255                    out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
256        }
257    }
258}
259
260static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
261                              int num_channels, AVCodecContext *avctx)
262{
263    int ch, sb;
264
265    for (ch = 0; ch < num_channels; ch++) {
266        for (sb = 0; sb < ch_unit->num_subbands; sb++) {
267            /* inverse transform and windowing */
268            ff_atrac3p_imdct(&ctx->fdsp, &ctx->mdct_ctx,
269                             &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
270                             &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
271                             (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
272                             ch_unit->channels[ch].wnd_shape[sb], sb);
273
274            /* gain compensation and overlapping */
275            ff_atrac_gain_compensation(&ctx->gainc_ctx,
276                                       &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
277                                       &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
278                                       &ch_unit->channels[ch].gain_data_prev[sb],
279                                       &ch_unit->channels[ch].gain_data[sb],
280                                       ATRAC3P_SUBBAND_SAMPLES,
281                                       &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
282        }
283
284        /* zero unused subbands in both output and overlapping buffers */
285        memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
286               0,
287               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
288               ATRAC3P_SUBBAND_SAMPLES *
289               sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
290        memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
291               0,
292               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
293               ATRAC3P_SUBBAND_SAMPLES *
294               sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
295
296        /* resynthesize and add tonal signal */
297        if (ch_unit->waves_info->tones_present ||
298            ch_unit->waves_info_prev->tones_present) {
299            for (sb = 0; sb < ch_unit->num_subbands; sb++)
300                if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
301                    ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
302                    ff_atrac3p_generate_tones(ch_unit, &ctx->fdsp, ch, sb,
303                                              &ctx->time_buf[ch][sb * 128]);
304                }
305        }
306
307        /* subband synthesis and acoustic signal output */
308        ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
309                        &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
310    }
311
312    /* swap window shape and gain control buffers. */
313    for (ch = 0; ch < num_channels; ch++) {
314        FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
315               ch_unit->channels[ch].wnd_shape_prev);
316        FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
317               ch_unit->channels[ch].gain_data_prev);
318        FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
319               ch_unit->channels[ch].tones_info_prev);
320    }
321
322    FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
323}
324
325static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
326                                int *got_frame_ptr, AVPacket *avpkt)
327{
328    ATRAC3PContext *ctx = avctx->priv_data;
329    AVFrame *frame      = data;
330    int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
331    float **samples_p = (float **)frame->extended_data;
332
333    frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
334    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
335        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
336        return ret;
337    }
338
339    if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
340        return ret;
341
342    if (get_bits1(&ctx->gb)) {
343        av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
344        return AVERROR_INVALIDDATA;
345    }
346
347    while (get_bits_left(&ctx->gb) >= 2 &&
348           (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
349        if (ch_unit_id == CH_UNIT_EXTENSION) {
350            avpriv_report_missing_feature(avctx, "Channel unit extension");
351            return AVERROR_PATCHWELCOME;
352        }
353        if (ch_block >= ctx->num_channel_blocks ||
354            ctx->channel_blocks[ch_block] != ch_unit_id) {
355            av_log(avctx, AV_LOG_ERROR,
356                   "Frame data doesn't match channel configuration!\n");
357            return AVERROR_INVALIDDATA;
358        }
359
360        ctx->ch_units[ch_block].unit_type = ch_unit_id;
361        channels_to_process               = ch_unit_id + 1;
362
363        if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
364                                                  &ctx->ch_units[ch_block],
365                                                  channels_to_process,
366                                                  avctx)) < 0)
367            return ret;
368
369        decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples,
370                                 channels_to_process, avctx);
371        reconstruct_frame(ctx, &ctx->ch_units[ch_block],
372                          channels_to_process, avctx);
373
374        for (i = 0; i < channels_to_process; i++)
375            memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
376                   ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
377
378        ch_block++;
379        out_ch_index += channels_to_process;
380    }
381
382    *got_frame_ptr = 1;
383
384    return avctx->block_align;
385}
386
387AVCodec ff_atrac3p_decoder = {
388    .name           = "atrac3plus",
389    .long_name      = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
390    .type           = AVMEDIA_TYPE_AUDIO,
391    .id             = AV_CODEC_ID_ATRAC3P,
392    .priv_data_size = sizeof(ATRAC3PContext),
393    .init           = atrac3p_decode_init,
394    .close          = atrac3p_decode_close,
395    .decode         = atrac3p_decode_frame,
396};
397