~ubuntu-branches/debian/experimental/libav/experimental

« back to all changes in this revision

Viewing changes to libavcodec/atrac3plusdec.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-01-18 15:46:55 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20140118154655-iz6u00yevkat1jqi
Tags: 6:10~alpha2-1
New Upstream release 10_alpha2. This upstream git snapshot has too many
changes to list here, cf. to the upstream Changelog:
http://git.libav.org/?p=libav.git;a=blob;f=Changelog;hb=refs/tags/v10_alpha2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ATRAC3+ compatible decoder
 
3
 *
 
4
 * Copyright (c) 2010-2013 Maxim Poliakovski
 
5
 *
 
6
 * This file is part of Libav.
 
7
 *
 
8
 * Libav 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
 * Libav 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 Libav; 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
 
 
48
typedef 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
 
 
68
static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
 
69
{
 
70
    av_free(((ATRAC3PContext *)(avctx->priv_data))->ch_units);
 
71
 
 
72
    return 0;
 
73
}
 
74
 
 
75
static 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->channel_layout) {
 
81
    case AV_CH_FRONT_LEFT:
 
82
    case AV_CH_LAYOUT_MONO:
 
83
        ctx->num_channel_blocks = 1;
 
84
        ctx->channel_blocks[0]  = CH_UNIT_MONO;
 
85
        break;
 
86
    case AV_CH_LAYOUT_STEREO:
 
87
        ctx->num_channel_blocks = 1;
 
88
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
89
        break;
 
90
    case AV_CH_LAYOUT_SURROUND:
 
91
        ctx->num_channel_blocks = 2;
 
92
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
93
        ctx->channel_blocks[1]  = CH_UNIT_MONO;
 
94
        break;
 
95
    case AV_CH_LAYOUT_4POINT0:
 
96
        ctx->num_channel_blocks = 3;
 
97
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
98
        ctx->channel_blocks[1]  = CH_UNIT_MONO;
 
99
        ctx->channel_blocks[2]  = CH_UNIT_MONO;
 
100
        break;
 
101
    case AV_CH_LAYOUT_5POINT1_BACK:
 
102
        ctx->num_channel_blocks = 4;
 
103
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
104
        ctx->channel_blocks[1]  = CH_UNIT_MONO;
 
105
        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
 
106
        ctx->channel_blocks[3]  = CH_UNIT_MONO;
 
107
        break;
 
108
    case AV_CH_LAYOUT_6POINT1_BACK:
 
109
        ctx->num_channel_blocks = 5;
 
110
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
111
        ctx->channel_blocks[1]  = CH_UNIT_MONO;
 
112
        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
 
113
        ctx->channel_blocks[3]  = CH_UNIT_MONO;
 
114
        ctx->channel_blocks[4]  = CH_UNIT_MONO;
 
115
        break;
 
116
    case AV_CH_LAYOUT_7POINT1:
 
117
        ctx->num_channel_blocks = 5;
 
118
        ctx->channel_blocks[0]  = CH_UNIT_STEREO;
 
119
        ctx->channel_blocks[1]  = CH_UNIT_MONO;
 
120
        ctx->channel_blocks[2]  = CH_UNIT_STEREO;
 
121
        ctx->channel_blocks[3]  = CH_UNIT_STEREO;
 
122
        ctx->channel_blocks[4]  = CH_UNIT_MONO;
 
123
        break;
 
124
    default:
 
125
        av_log(avctx, AV_LOG_ERROR,
 
126
               "Unsupported channel layout: %"PRIx64"!\n", avctx->channel_layout);
 
127
        return AVERROR_INVALIDDATA;
 
128
    }
 
129
 
 
130
    return 0;
 
131
}
 
132
 
 
133
static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
 
134
{
 
135
    ATRAC3PContext *ctx = avctx->priv_data;
 
136
    int i, ch, ret;
 
137
 
 
138
    if (!avctx->block_align) {
 
139
        av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
 
140
        return AVERROR(EINVAL);
 
141
    }
 
142
 
 
143
    avpriv_float_dsp_init(&ctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
 
144
 
 
145
    /* initialize IPQF */
 
146
    ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
 
147
 
 
148
    ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
 
149
 
 
150
    ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
 
151
 
 
152
    ff_atrac3p_init_wave_synth();
 
153
 
 
154
    if ((ret = set_channel_params(ctx, avctx)) < 0)
 
155
        return ret;
 
156
 
 
157
    ctx->my_channel_layout = avctx->channel_layout;
 
158
 
 
159
    ctx->ch_units = av_mallocz(sizeof(*ctx->ch_units) *
 
160
                               ctx->num_channel_blocks);
 
161
    if (!ctx->ch_units) {
 
162
        atrac3p_decode_close(avctx);
 
163
        return AVERROR(ENOMEM);
 
164
    }
 
165
 
 
166
    for (i = 0; i < ctx->num_channel_blocks; i++) {
 
167
        for (ch = 0; ch < 2; ch++) {
 
168
            ctx->ch_units[i].channels[ch].ch_num          = ch;
 
169
            ctx->ch_units[i].channels[ch].wnd_shape       = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
 
170
            ctx->ch_units[i].channels[ch].wnd_shape_prev  = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
 
171
            ctx->ch_units[i].channels[ch].gain_data       = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
 
172
            ctx->ch_units[i].channels[ch].gain_data_prev  = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
 
173
            ctx->ch_units[i].channels[ch].tones_info      = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
 
174
            ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
 
175
        }
 
176
 
 
177
        ctx->ch_units[i].waves_info      = &ctx->ch_units[i].wave_synth_hist[0];
 
178
        ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
 
179
    }
 
180
 
 
181
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
 
182
 
 
183
    return 0;
 
184
}
 
185
 
 
186
static void decode_residual_spectrum(Atrac3pChanUnitCtx *ctx,
 
187
                                     float out[2][ATRAC3P_FRAME_SAMPLES],
 
188
                                     int num_channels,
 
189
                                     AVCodecContext *avctx)
 
190
{
 
191
    int i, sb, ch, qu, nspeclines, RNG_index;
 
192
    float *dst, q;
 
193
    int16_t *src;
 
194
    /* calculate RNG table index for each subband */
 
195
    int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
 
196
 
 
197
    if (ctx->mute_flag) {
 
198
        for (ch = 0; ch < num_channels; ch++)
 
199
            memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
 
200
        return;
 
201
    }
 
202
 
 
203
    for (qu = 0, RNG_index = 0; qu < ctx->used_quant_units; qu++)
 
204
        RNG_index += ctx->channels[0].qu_sf_idx[qu] +
 
205
                     ctx->channels[1].qu_sf_idx[qu];
 
206
 
 
207
    for (sb = 0; sb < ctx->num_coded_subbands; sb++, RNG_index += 128)
 
208
        sb_RNG_index[sb] = RNG_index & 0x3FC;
 
209
 
 
210
    /* inverse quant and power compensation */
 
211
    for (ch = 0; ch < num_channels; ch++) {
 
212
        /* clear channel's residual spectrum */
 
213
        memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
 
214
 
 
215
        for (qu = 0; qu < ctx->used_quant_units; qu++) {
 
216
            src        = &ctx->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
 
217
            dst        = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
 
218
            nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
 
219
                         ff_atrac3p_qu_to_spec_pos[qu];
 
220
 
 
221
            if (ctx->channels[ch].qu_wordlen[qu] > 0) {
 
222
                q = ff_atrac3p_sf_tab[ctx->channels[ch].qu_sf_idx[qu]] *
 
223
                    ff_atrac3p_mant_tab[ctx->channels[ch].qu_wordlen[qu]];
 
224
                for (i = 0; i < nspeclines; i++)
 
225
                    dst[i] = src[i] * q;
 
226
            }
 
227
        }
 
228
 
 
229
        for (sb = 0; sb < ctx->num_coded_subbands; sb++)
 
230
            ff_atrac3p_power_compensation(ctx, ch, &out[ch][0],
 
231
                                          sb_RNG_index[sb], sb);
 
232
    }
 
233
 
 
234
    if (ctx->unit_type == CH_UNIT_STEREO) {
 
235
        for (sb = 0; sb < ctx->num_coded_subbands; sb++) {
 
236
            if (ctx->swap_channels[sb]) {
 
237
                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
 
238
                    FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
 
239
                                  out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
 
240
            }
 
241
 
 
242
            /* flip coefficients' sign if requested */
 
243
            if (ctx->negate_coeffs[sb])
 
244
                for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
 
245
                    out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
 
246
        }
 
247
    }
 
248
}
 
249
 
 
250
static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
 
251
                              int num_channels, AVCodecContext *avctx)
 
252
{
 
253
    int ch, sb;
 
254
 
 
255
    for (ch = 0; ch < num_channels; ch++) {
 
256
        for (sb = 0; sb < ch_unit->num_subbands; sb++) {
 
257
            /* inverse transform and windowing */
 
258
            ff_atrac3p_imdct(&ctx->fdsp, &ctx->mdct_ctx,
 
259
                             &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
 
260
                             &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
 
261
                             (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
 
262
                             ch_unit->channels[ch].wnd_shape[sb], sb);
 
263
 
 
264
            /* gain compensation and overlapping */
 
265
            ff_atrac_gain_compensation(&ctx->gainc_ctx,
 
266
                                       &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
 
267
                                       &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
 
268
                                       &ch_unit->channels[ch].gain_data_prev[sb],
 
269
                                       &ch_unit->channels[ch].gain_data[sb],
 
270
                                       ATRAC3P_SUBBAND_SAMPLES,
 
271
                                       &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
 
272
        }
 
273
 
 
274
        /* zero unused subbands in both output and overlapping buffers */
 
275
        memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
 
276
               0,
 
277
               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
 
278
               ATRAC3P_SUBBAND_SAMPLES *
 
279
               sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
 
280
        memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
 
281
               0,
 
282
               (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
 
283
               ATRAC3P_SUBBAND_SAMPLES *
 
284
               sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
 
285
 
 
286
        /* resynthesize and add tonal signal */
 
287
        if (ch_unit->waves_info->tones_present ||
 
288
            ch_unit->waves_info_prev->tones_present) {
 
289
            for (sb = 0; sb < ch_unit->num_subbands; sb++)
 
290
                if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
 
291
                    ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
 
292
                    ff_atrac3p_generate_tones(ch_unit, &ctx->fdsp, ch, sb,
 
293
                                              &ctx->time_buf[ch][sb * 128]);
 
294
                }
 
295
        }
 
296
 
 
297
        /* subband synthesis and acoustic signal output */
 
298
        ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
 
299
                        &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
 
300
    }
 
301
 
 
302
    /* swap window shape and gain control buffers. */
 
303
    for (ch = 0; ch < num_channels; ch++) {
 
304
        FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
 
305
               ch_unit->channels[ch].wnd_shape_prev);
 
306
        FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
 
307
               ch_unit->channels[ch].gain_data_prev);
 
308
        FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
 
309
               ch_unit->channels[ch].tones_info_prev);
 
310
    }
 
311
 
 
312
    FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
 
313
}
 
314
 
 
315
static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
 
316
                                int *got_frame_ptr, AVPacket *avpkt)
 
317
{
 
318
    ATRAC3PContext *ctx = avctx->priv_data;
 
319
    AVFrame *frame      = data;
 
320
    int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
 
321
    float **samples_p = (float **)frame->extended_data;
 
322
 
 
323
    frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
 
324
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
 
325
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
326
        return ret;
 
327
    }
 
328
 
 
329
    if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
 
330
        return ret;
 
331
 
 
332
    if (get_bits1(&ctx->gb)) {
 
333
        av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
 
334
        return AVERROR_INVALIDDATA;
 
335
    }
 
336
 
 
337
    while (get_bits_left(&ctx->gb) >= 2 &&
 
338
           (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
 
339
        if (ch_unit_id == CH_UNIT_EXTENSION) {
 
340
            avpriv_report_missing_feature(avctx, "Channel unit extension");
 
341
            return AVERROR_PATCHWELCOME;
 
342
        }
 
343
        if (ch_block >= ctx->num_channel_blocks ||
 
344
            ctx->channel_blocks[ch_block] != ch_unit_id) {
 
345
            av_log(avctx, AV_LOG_ERROR,
 
346
                   "Frame data doesn't match channel configuration!\n");
 
347
            return AVERROR_INVALIDDATA;
 
348
        }
 
349
 
 
350
        ctx->ch_units[ch_block].unit_type = ch_unit_id;
 
351
        channels_to_process               = ch_unit_id + 1;
 
352
 
 
353
        if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
 
354
                                                  &ctx->ch_units[ch_block],
 
355
                                                  channels_to_process,
 
356
                                                  avctx)) < 0)
 
357
            return ret;
 
358
 
 
359
        decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples,
 
360
                                 channels_to_process, avctx);
 
361
        reconstruct_frame(ctx, &ctx->ch_units[ch_block],
 
362
                          channels_to_process, avctx);
 
363
 
 
364
        for (i = 0; i < channels_to_process; i++)
 
365
            memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
 
366
                   ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
 
367
 
 
368
        ch_block++;
 
369
        out_ch_index += channels_to_process;
 
370
    }
 
371
 
 
372
    *got_frame_ptr = 1;
 
373
 
 
374
    return avctx->block_align;
 
375
}
 
376
 
 
377
AVCodec ff_atrac3p_decoder = {
 
378
    .name             = "atrac3plus",
 
379
    .long_name        = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
 
380
    .type             = AVMEDIA_TYPE_AUDIO,
 
381
    .id               = AV_CODEC_ID_ATRAC3P,
 
382
    .priv_data_size   = sizeof(ATRAC3PContext),
 
383
    .init             = atrac3p_decode_init,
 
384
    .init_static_data = ff_atrac3p_init_vlcs,
 
385
    .close            = atrac3p_decode_close,
 
386
    .decode           = atrac3p_decode_frame,
 
387
};