2 * ATRAC3+ compatible decoder
4 * Copyright (c) 2010-2013 Maxim Poliakovski
6 * This file is part of Libav.
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.
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.
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
25 * Sony ATRAC3+ compatible decoder.
27 * Container formats used to store its data:
28 * RIFF WAV (.at3) and Sony OpenMG (.oma, .aa3).
30 * Technical description of this codec can be found here:
31 * http://wiki.multimedia.cx/index.php?title=ATRAC3plus
33 * Kudos to Benjamin Larsson and Michael Karcher
34 * for their precious technical help!
40 #include "libavutil/channel_layout.h"
41 #include "libavutil/float_dsp.h"
44 #include "bitstream.h"
47 #include "atrac3plus.h"
49 typedef struct ATRAC3PContext {
51 AVFloatDSPContext fdsp;
53 DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES]; ///< quantized MDCT spectrum
54 DECLARE_ALIGNED(32, float, mdct_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the IMDCT
55 DECLARE_ALIGNED(32, float, time_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the gain compensation
56 DECLARE_ALIGNED(32, float, outp_buf)[2][ATRAC3P_FRAME_SAMPLES];
58 AtracGCContext gainc_ctx; ///< gain compensation context
60 FFTContext ipqf_dct_ctx; ///< IDCT context used by IPQF
62 Atrac3pChanUnitCtx *ch_units; ///< global channel units
64 int num_channel_blocks; ///< number of channel blocks
65 uint8_t channel_blocks[5]; ///< channel configuration descriptor
66 uint64_t my_channel_layout; ///< current channel layout
69 static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
71 av_free(((ATRAC3PContext *)(avctx->priv_data))->ch_units);
76 static av_cold int set_channel_params(ATRAC3PContext *ctx,
77 AVCodecContext *avctx)
79 memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
81 switch (avctx->channels) {
83 if (avctx->channel_layout != AV_CH_FRONT_LEFT)
84 avctx->channel_layout = AV_CH_LAYOUT_MONO;
86 ctx->num_channel_blocks = 1;
87 ctx->channel_blocks[0] = CH_UNIT_MONO;
90 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
91 ctx->num_channel_blocks = 1;
92 ctx->channel_blocks[0] = CH_UNIT_STEREO;
95 avctx->channel_layout = AV_CH_LAYOUT_SURROUND;
96 ctx->num_channel_blocks = 2;
97 ctx->channel_blocks[0] = CH_UNIT_STEREO;
98 ctx->channel_blocks[1] = CH_UNIT_MONO;
101 avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
102 ctx->num_channel_blocks = 3;
103 ctx->channel_blocks[0] = CH_UNIT_STEREO;
104 ctx->channel_blocks[1] = CH_UNIT_MONO;
105 ctx->channel_blocks[2] = CH_UNIT_MONO;
108 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
109 ctx->num_channel_blocks = 4;
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;
116 avctx->channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
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_MONO;
122 ctx->channel_blocks[4] = CH_UNIT_MONO;
125 avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
126 ctx->num_channel_blocks = 5;
127 ctx->channel_blocks[0] = CH_UNIT_STEREO;
128 ctx->channel_blocks[1] = CH_UNIT_MONO;
129 ctx->channel_blocks[2] = CH_UNIT_STEREO;
130 ctx->channel_blocks[3] = CH_UNIT_STEREO;
131 ctx->channel_blocks[4] = CH_UNIT_MONO;
134 av_log(avctx, AV_LOG_ERROR,
135 "Unsupported channel count: %d!\n", avctx->channels);
136 return AVERROR_INVALIDDATA;
142 static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
144 ATRAC3PContext *ctx = avctx->priv_data;
147 if (!avctx->block_align) {
148 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
149 return AVERROR(EINVAL);
152 avpriv_float_dsp_init(&ctx->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
154 /* initialize IPQF */
155 ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
157 ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
159 ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
161 ff_atrac3p_init_wave_synth();
163 if ((ret = set_channel_params(ctx, avctx)) < 0)
166 ctx->my_channel_layout = avctx->channel_layout;
168 ctx->ch_units = av_mallocz(sizeof(*ctx->ch_units) *
169 ctx->num_channel_blocks);
170 if (!ctx->ch_units) {
171 atrac3p_decode_close(avctx);
172 return AVERROR(ENOMEM);
175 for (i = 0; i < ctx->num_channel_blocks; i++) {
176 for (ch = 0; ch < 2; ch++) {
177 ctx->ch_units[i].channels[ch].ch_num = ch;
178 ctx->ch_units[i].channels[ch].wnd_shape = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
179 ctx->ch_units[i].channels[ch].wnd_shape_prev = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
180 ctx->ch_units[i].channels[ch].gain_data = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
181 ctx->ch_units[i].channels[ch].gain_data_prev = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
182 ctx->ch_units[i].channels[ch].tones_info = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
183 ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
186 ctx->ch_units[i].waves_info = &ctx->ch_units[i].wave_synth_hist[0];
187 ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
190 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
195 static void decode_residual_spectrum(Atrac3pChanUnitCtx *ctx,
196 float out[2][ATRAC3P_FRAME_SAMPLES],
198 AVCodecContext *avctx)
200 int i, sb, ch, qu, nspeclines, RNG_index;
203 /* calculate RNG table index for each subband */
204 int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
206 if (ctx->mute_flag) {
207 for (ch = 0; ch < num_channels; ch++)
208 memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
212 for (qu = 0, RNG_index = 0; qu < ctx->used_quant_units; qu++)
213 RNG_index += ctx->channels[0].qu_sf_idx[qu] +
214 ctx->channels[1].qu_sf_idx[qu];
216 for (sb = 0; sb < ctx->num_coded_subbands; sb++, RNG_index += 128)
217 sb_RNG_index[sb] = RNG_index & 0x3FC;
219 /* inverse quant and power compensation */
220 for (ch = 0; ch < num_channels; ch++) {
221 /* clear channel's residual spectrum */
222 memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
224 for (qu = 0; qu < ctx->used_quant_units; qu++) {
225 src = &ctx->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
226 dst = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
227 nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
228 ff_atrac3p_qu_to_spec_pos[qu];
230 if (ctx->channels[ch].qu_wordlen[qu] > 0) {
231 q = ff_atrac3p_sf_tab[ctx->channels[ch].qu_sf_idx[qu]] *
232 ff_atrac3p_mant_tab[ctx->channels[ch].qu_wordlen[qu]];
233 for (i = 0; i < nspeclines; i++)
238 for (sb = 0; sb < ctx->num_coded_subbands; sb++)
239 ff_atrac3p_power_compensation(ctx, ch, &out[ch][0],
240 sb_RNG_index[sb], sb);
243 if (ctx->unit_type == CH_UNIT_STEREO) {
244 for (sb = 0; sb < ctx->num_coded_subbands; sb++) {
245 if (ctx->swap_channels[sb]) {
246 for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
247 FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
248 out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
251 /* flip coefficients' sign if requested */
252 if (ctx->negate_coeffs[sb])
253 for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
254 out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
259 static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
260 int num_channels, AVCodecContext *avctx)
264 for (ch = 0; ch < num_channels; ch++) {
265 for (sb = 0; sb < ch_unit->num_subbands; sb++) {
266 /* inverse transform and windowing */
267 ff_atrac3p_imdct(&ctx->fdsp, &ctx->mdct_ctx,
268 &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
269 &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
270 (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
271 ch_unit->channels[ch].wnd_shape[sb], sb);
273 /* gain compensation and overlapping */
274 ff_atrac_gain_compensation(&ctx->gainc_ctx,
275 &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
276 &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
277 &ch_unit->channels[ch].gain_data_prev[sb],
278 &ch_unit->channels[ch].gain_data[sb],
279 ATRAC3P_SUBBAND_SAMPLES,
280 &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
283 /* zero unused subbands in both output and overlapping buffers */
284 memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
286 (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
287 ATRAC3P_SUBBAND_SAMPLES *
288 sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
289 memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
291 (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
292 ATRAC3P_SUBBAND_SAMPLES *
293 sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
295 /* resynthesize and add tonal signal */
296 if (ch_unit->waves_info->tones_present ||
297 ch_unit->waves_info_prev->tones_present) {
298 for (sb = 0; sb < ch_unit->num_subbands; sb++)
299 if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
300 ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
301 ff_atrac3p_generate_tones(ch_unit, &ctx->fdsp, ch, sb,
302 &ctx->time_buf[ch][sb * 128]);
306 /* subband synthesis and acoustic signal output */
307 ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
308 &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
311 /* swap window shape and gain control buffers. */
312 for (ch = 0; ch < num_channels; ch++) {
313 FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
314 ch_unit->channels[ch].wnd_shape_prev);
315 FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
316 ch_unit->channels[ch].gain_data_prev);
317 FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
318 ch_unit->channels[ch].tones_info_prev);
321 FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
324 static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
325 int *got_frame_ptr, AVPacket *avpkt)
327 ATRAC3PContext *ctx = avctx->priv_data;
328 AVFrame *frame = data;
329 int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
330 float **samples_p = (float **)frame->extended_data;
332 frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
333 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
334 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
338 if ((ret = bitstream_init8(&ctx->bc, avpkt->data, avpkt->size)) < 0)
341 if (bitstream_read_bit(&ctx->bc)) {
342 av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
343 return AVERROR_INVALIDDATA;
346 while (bitstream_bits_left(&ctx->bc) >= 2 &&
347 (ch_unit_id = bitstream_read(&ctx->bc, 2)) != CH_UNIT_TERMINATOR) {
348 if (ch_unit_id == CH_UNIT_EXTENSION) {
349 avpriv_report_missing_feature(avctx, "Channel unit extension");
350 return AVERROR_PATCHWELCOME;
352 if (ch_block >= ctx->num_channel_blocks ||
353 ctx->channel_blocks[ch_block] != ch_unit_id) {
354 av_log(avctx, AV_LOG_ERROR,
355 "Frame data doesn't match channel configuration!\n");
356 return AVERROR_INVALIDDATA;
359 ctx->ch_units[ch_block].unit_type = ch_unit_id;
360 channels_to_process = ch_unit_id + 1;
362 if ((ret = ff_atrac3p_decode_channel_unit(&ctx->bc,
363 &ctx->ch_units[ch_block],
368 decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples,
369 channels_to_process, avctx);
370 reconstruct_frame(ctx, &ctx->ch_units[ch_block],
371 channels_to_process, avctx);
373 for (i = 0; i < channels_to_process; i++)
374 memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
375 ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
378 out_ch_index += channels_to_process;
383 return avctx->block_align;
386 AVCodec ff_atrac3p_decoder = {
387 .name = "atrac3plus",
388 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
389 .type = AVMEDIA_TYPE_AUDIO,
390 .id = AV_CODEC_ID_ATRAC3P,
391 .capabilities = AV_CODEC_CAP_DR1,
392 .priv_data_size = sizeof(ATRAC3PContext),
393 .init = atrac3p_decode_init,
394 .init_static_data = ff_atrac3p_init_vlcs,
395 .close = atrac3p_decode_close,
396 .decode = atrac3p_decode_frame,