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 "libavutil/mem_internal.h"
43#include "libavutil/thread.h"
44#include "avcodec.h"
45#include "codec_internal.h"
46#include "get_bits.h"
47#include "internal.h"
48#include "atrac.h"
49#include "atrac3plus.h"
50
51static const uint8_t channel_map[8][8] = {
52    { 0, },
53    { 0, 1, },
54    { 0, 1, 2, },
55    { 0, 1, 2, 3, },
56    { 0, },
57    { 0, 1, 2, 4, 5, 3, },
58    { 0, 1, 2, 4, 5, 6, 3, },
59    { 0, 1, 2, 4, 5, 6, 7, 3, },
60};
61
62typedef struct ATRAC3PContext {
63    GetBitContext gb;
64    AVFloatDSPContext *fdsp;
65
66    DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES];  ///< quantized MDCT spectrum
67    DECLARE_ALIGNED(32, float, mdct_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the IMDCT
68    DECLARE_ALIGNED(32, float, time_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the gain compensation
69    DECLARE_ALIGNED(32, float, outp_buf)[2][ATRAC3P_FRAME_SAMPLES];
70
71    AtracGCContext gainc_ctx;   ///< gain compensation context
72    FFTContext mdct_ctx;
73    FFTContext ipqf_dct_ctx;    ///< IDCT context used by IPQF
74
75    Atrac3pChanUnitCtx *ch_units;   ///< global channel units
76
77    int num_channel_blocks;     ///< number of channel blocks
78    uint8_t channel_blocks[5];  ///< channel configuration descriptor
79    const uint8_t *channel_map; ///< channel layout map
80} ATRAC3PContext;
81
82static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
83{
84    ATRAC3PContext *ctx = avctx->priv_data;
85
86    av_freep(&ctx->ch_units);
87    av_freep(&ctx->fdsp);
88
89    ff_mdct_end(&ctx->mdct_ctx);
90    ff_mdct_end(&ctx->ipqf_dct_ctx);
91
92    return 0;
93}
94
95static av_cold int set_channel_params(ATRAC3PContext *ctx,
96                                      AVCodecContext *avctx)
97{
98    int channels = avctx->ch_layout.nb_channels;
99    memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
100
101    av_channel_layout_uninit(&avctx->ch_layout);
102    switch (channels) {
103    case 1:
104        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
105        ctx->num_channel_blocks = 1;
106        ctx->channel_blocks[0]  = CH_UNIT_MONO;
107        break;
108    case 2:
109        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
110        ctx->num_channel_blocks = 1;
111        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
112        break;
113    case 3:
114        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND;
115        ctx->num_channel_blocks = 2;
116        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
117        ctx->channel_blocks[1]  = CH_UNIT_MONO;
118        break;
119    case 4:
120        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT0;
121        ctx->num_channel_blocks = 3;
122        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
123        ctx->channel_blocks[1]  = CH_UNIT_MONO;
124        ctx->channel_blocks[2]  = CH_UNIT_MONO;
125        break;
126    case 6:
127        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK;
128        ctx->num_channel_blocks = 4;
129        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
130        ctx->channel_blocks[1]  = CH_UNIT_MONO;
131        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
132        ctx->channel_blocks[3]  = CH_UNIT_MONO;
133        break;
134    case 7:
135        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1_BACK;
136        ctx->num_channel_blocks = 5;
137        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
138        ctx->channel_blocks[1]  = CH_UNIT_MONO;
139        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
140        ctx->channel_blocks[3]  = CH_UNIT_MONO;
141        ctx->channel_blocks[4]  = CH_UNIT_MONO;
142        break;
143    case 8:
144        avctx->ch_layout        = (AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1;
145        ctx->num_channel_blocks = 5;
146        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
147        ctx->channel_blocks[1]  = CH_UNIT_MONO;
148        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
149        ctx->channel_blocks[3]  = CH_UNIT_STEREO;
150        ctx->channel_blocks[4]  = CH_UNIT_MONO;
151        break;
152    default:
153        av_log(avctx, AV_LOG_ERROR,
154               "Unsupported channel count: %d!\n", channels);
155        return AVERROR_INVALIDDATA;
156    }
157
158    ctx->channel_map = channel_map[channels - 1];
159
160    return 0;
161}
162
163static av_cold void atrac3p_init_static(void)
164{
165    ff_atrac3p_init_vlcs();
166    ff_atrac3p_init_dsp_static();
167}
168
169static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
170{
171    static AVOnce init_static_once = AV_ONCE_INIT;
172    ATRAC3PContext *ctx = avctx->priv_data;
173    int i, ch, ret;
174
175    if (!avctx->block_align) {
176        av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
177        return AVERROR(EINVAL);
178    }
179
180    /* initialize IPQF */
181    ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
182
183    ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
184
185    ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
186
187    if ((ret = set_channel_params(ctx, avctx)) < 0)
188        return ret;
189
190    ctx->ch_units = av_calloc(ctx->num_channel_blocks, sizeof(*ctx->ch_units));
191    ctx->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
192
193    if (!ctx->ch_units || !ctx->fdsp) {
194        return AVERROR(ENOMEM);
195    }
196
197    for (i = 0; i < ctx->num_channel_blocks; i++) {
198        for (ch = 0; ch < 2; ch++) {
199            ctx->ch_units[i].channels[ch].ch_num          = ch;
200            ctx->ch_units[i].channels[ch].wnd_shape       = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
201            ctx->ch_units[i].channels[ch].wnd_shape_prev  = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
202            ctx->ch_units[i].channels[ch].gain_data       = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
203            ctx->ch_units[i].channels[ch].gain_data_prev  = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
204            ctx->ch_units[i].channels[ch].tones_info      = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
205            ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
206        }
207
208        ctx->ch_units[i].waves_info      = &ctx->ch_units[i].wave_synth_hist[0];
209        ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
210    }
211
212    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
213
214    ff_thread_once(&init_static_once, atrac3p_init_static);
215
216    return 0;
217}
218
219static void decode_residual_spectrum(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
220                                     float out[2][ATRAC3P_FRAME_SAMPLES],
221                                     int num_channels,
222                                     AVCodecContext *avctx)
223{
224    int i, sb, ch, qu, nspeclines, RNG_index;
225    float *dst, q;
226    int16_t *src;
227    /* calculate RNG table index for each subband */
228    int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
229
230    if (ch_unit->mute_flag) {
231        for (ch = 0; ch < num_channels; ch++)
232            memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
233        return;
234    }
235
236    for (qu = 0, RNG_index = 0; qu < ch_unit->used_quant_units; qu++)
237        RNG_index += ch_unit->channels[0].qu_sf_idx[qu] +
238                     ch_unit->channels[1].qu_sf_idx[qu];
239
240    for (sb = 0; sb < ch_unit->num_coded_subbands; sb++, RNG_index += 128)
241        sb_RNG_index[sb] = RNG_index & 0x3FC;
242
243    /* inverse quant and power compensation */
244    for (ch = 0; ch < num_channels; ch++) {
245        /* clear channel's residual spectrum */
246        memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
247
248        for (qu = 0; qu < ch_unit->used_quant_units; qu++) {
249            src        = &ch_unit->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
250            dst        = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
251            nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
252                         ff_atrac3p_qu_to_spec_pos[qu];
253
254            if (ch_unit->channels[ch].qu_wordlen[qu] > 0) {
255                q = ff_atrac3p_sf_tab[ch_unit->channels[ch].qu_sf_idx[qu]] *
256                    ff_atrac3p_mant_tab[ch_unit->channels[ch].qu_wordlen[qu]];
257                for (i = 0; i < nspeclines; i++)
258                    dst[i] = src[i] * q;
259            }
260        }
261
262        for (sb = 0; sb < ch_unit->num_coded_subbands; sb++)
263            ff_atrac3p_power_compensation(ch_unit, ctx->fdsp, ch, &out[ch][0],
264                                          sb_RNG_index[sb], sb);
265    }
266
267    if (ch_unit->unit_type == CH_UNIT_STEREO) {
268        for (sb = 0; sb < ch_unit->num_coded_subbands; sb++) {
269            if (ch_unit->swap_channels[sb]) {
270                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
271                    FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
272                                  out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
273            }
274
275            /* flip coefficients' sign if requested */
276            if (ch_unit->negate_coeffs[sb])
277                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
278                    out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
279        }
280    }
281}
282
283static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
284                              int num_channels, AVCodecContext *avctx)
285{
286    int ch, sb;
287
288    for (ch = 0; ch < num_channels; ch++) {
289        for (sb = 0; sb < ch_unit->num_subbands; sb++) {
290            /* inverse transform and windowing */
291            ff_atrac3p_imdct(ctx->fdsp, &ctx->mdct_ctx,
292                             &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
293                             &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
294                             (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
295                             ch_unit->channels[ch].wnd_shape[sb], sb);
296
297            /* gain compensation and overlapping */
298            ff_atrac_gain_compensation(&ctx->gainc_ctx,
299                                       &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
300                                       &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
301                                       &ch_unit->channels[ch].gain_data_prev[sb],
302                                       &ch_unit->channels[ch].gain_data[sb],
303                                       ATRAC3P_SUBBAND_SAMPLES,
304                                       &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
305        }
306
307        /* zero unused subbands in both output and overlapping buffers */
308        memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
309               0,
310               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
311               ATRAC3P_SUBBAND_SAMPLES *
312               sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
313        memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
314               0,
315               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
316               ATRAC3P_SUBBAND_SAMPLES *
317               sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
318
319        /* resynthesize and add tonal signal */
320        if (ch_unit->waves_info->tones_present ||
321            ch_unit->waves_info_prev->tones_present) {
322            for (sb = 0; sb < ch_unit->num_subbands; sb++)
323                if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
324                    ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
325                    ff_atrac3p_generate_tones(ch_unit, ctx->fdsp, ch, sb,
326                                              &ctx->time_buf[ch][sb * 128]);
327                }
328        }
329
330        /* subband synthesis and acoustic signal output */
331        ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
332                        &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
333    }
334
335    /* swap window shape and gain control buffers. */
336    for (ch = 0; ch < num_channels; ch++) {
337        FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
338               ch_unit->channels[ch].wnd_shape_prev);
339        FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
340               ch_unit->channels[ch].gain_data_prev);
341        FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
342               ch_unit->channels[ch].tones_info_prev);
343    }
344
345    FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
346}
347
348static int atrac3p_decode_frame(AVCodecContext *avctx, AVFrame *frame,
349                                int *got_frame_ptr, AVPacket *avpkt)
350{
351    ATRAC3PContext *ctx = avctx->priv_data;
352    int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
353    float **samples_p = (float **)frame->extended_data;
354
355    frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
356    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
357        return ret;
358
359    if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
360        return ret;
361
362    if (get_bits1(&ctx->gb)) {
363        av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
364        return AVERROR_INVALIDDATA;
365    }
366
367    while (get_bits_left(&ctx->gb) >= 2 &&
368           (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
369        if (ch_unit_id == CH_UNIT_EXTENSION) {
370            avpriv_report_missing_feature(avctx, "Channel unit extension");
371            return AVERROR_PATCHWELCOME;
372        }
373        if (ch_block >= ctx->num_channel_blocks ||
374            ctx->channel_blocks[ch_block] != ch_unit_id) {
375            av_log(avctx, AV_LOG_ERROR,
376                   "Frame data doesn't match channel configuration!\n");
377            return AVERROR_INVALIDDATA;
378        }
379
380        ctx->ch_units[ch_block].unit_type = ch_unit_id;
381        channels_to_process               = ch_unit_id + 1;
382
383        if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
384                                                  &ctx->ch_units[ch_block],
385                                                  channels_to_process,
386                                                  avctx)) < 0)
387            return ret;
388
389        decode_residual_spectrum(ctx, &ctx->ch_units[ch_block], ctx->samples,
390                                 channels_to_process, avctx);
391        reconstruct_frame(ctx, &ctx->ch_units[ch_block],
392                          channels_to_process, avctx);
393
394        for (i = 0; i < channels_to_process; i++)
395            memcpy(samples_p[ctx->channel_map[out_ch_index + i]], ctx->outp_buf[i],
396                   ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
397
398        ch_block++;
399        out_ch_index += channels_to_process;
400    }
401
402    *got_frame_ptr = 1;
403
404    return avctx->codec_id == AV_CODEC_ID_ATRAC3P ? FFMIN(avctx->block_align, avpkt->size) : avpkt->size;
405}
406
407const FFCodec ff_atrac3p_decoder = {
408    .p.name         = "atrac3plus",
409    .p.long_name    = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
410    .p.type         = AVMEDIA_TYPE_AUDIO,
411    .p.id           = AV_CODEC_ID_ATRAC3P,
412    .p.capabilities = AV_CODEC_CAP_DR1,
413    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
414    .priv_data_size = sizeof(ATRAC3PContext),
415    .init           = atrac3p_decode_init,
416    .close          = atrac3p_decode_close,
417    FF_CODEC_DECODE_CB(atrac3p_decode_frame),
418};
419
420const FFCodec ff_atrac3pal_decoder = {
421    .p.name         = "atrac3plusal",
422    .p.long_name    = NULL_IF_CONFIG_SMALL("ATRAC3+ AL (Adaptive TRansform Acoustic Coding 3+ Advanced Lossless)"),
423    .p.type         = AVMEDIA_TYPE_AUDIO,
424    .p.id           = AV_CODEC_ID_ATRAC3PAL,
425    .p.capabilities = AV_CODEC_CAP_DR1,
426    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
427    .priv_data_size = sizeof(ATRAC3PContext),
428    .init           = atrac3p_decode_init,
429    .close          = atrac3p_decode_close,
430    FF_CODEC_DECODE_CB(atrac3p_decode_frame),
431};
432