2 * ATRAC3+ compatible decoder
4 * Copyright (c) 2010-2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
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.
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.
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
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"
42 #include "libavutil/thread.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 ATRAC3PContext *ctx = avctx->priv_data;
73 av_freep(&ctx->ch_units);
76 ff_mdct_end(&ctx->mdct_ctx);
77 ff_mdct_end(&ctx->ipqf_dct_ctx);
82 static av_cold int set_channel_params(ATRAC3PContext *ctx,
83 AVCodecContext *avctx)
85 memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
87 switch (avctx->channels) {
89 if (avctx->channel_layout != AV_CH_FRONT_LEFT)
90 avctx->channel_layout = AV_CH_LAYOUT_MONO;
92 ctx->num_channel_blocks = 1;
93 ctx->channel_blocks[0] = CH_UNIT_MONO;
96 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
97 ctx->num_channel_blocks = 1;
98 ctx->channel_blocks[0] = CH_UNIT_STEREO;
101 avctx->channel_layout = AV_CH_LAYOUT_SURROUND;
102 ctx->num_channel_blocks = 2;
103 ctx->channel_blocks[0] = CH_UNIT_STEREO;
104 ctx->channel_blocks[1] = CH_UNIT_MONO;
107 avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
108 ctx->num_channel_blocks = 3;
109 ctx->channel_blocks[0] = CH_UNIT_STEREO;
110 ctx->channel_blocks[1] = CH_UNIT_MONO;
111 ctx->channel_blocks[2] = CH_UNIT_MONO;
114 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
115 ctx->num_channel_blocks = 4;
116 ctx->channel_blocks[0] = CH_UNIT_STEREO;
117 ctx->channel_blocks[1] = CH_UNIT_MONO;
118 ctx->channel_blocks[2] = CH_UNIT_STEREO;
119 ctx->channel_blocks[3] = CH_UNIT_MONO;
122 avctx->channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
123 ctx->num_channel_blocks = 5;
124 ctx->channel_blocks[0] = CH_UNIT_STEREO;
125 ctx->channel_blocks[1] = CH_UNIT_MONO;
126 ctx->channel_blocks[2] = CH_UNIT_STEREO;
127 ctx->channel_blocks[3] = CH_UNIT_MONO;
128 ctx->channel_blocks[4] = CH_UNIT_MONO;
131 avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
132 ctx->num_channel_blocks = 5;
133 ctx->channel_blocks[0] = CH_UNIT_STEREO;
134 ctx->channel_blocks[1] = CH_UNIT_MONO;
135 ctx->channel_blocks[2] = CH_UNIT_STEREO;
136 ctx->channel_blocks[3] = CH_UNIT_STEREO;
137 ctx->channel_blocks[4] = CH_UNIT_MONO;
140 av_log(avctx, AV_LOG_ERROR,
141 "Unsupported channel count: %d!\n", avctx->channels);
142 return AVERROR_INVALIDDATA;
148 static av_cold void atrac3p_init_static(void)
150 ff_atrac3p_init_vlcs();
151 ff_atrac3p_init_dsp_static();
154 static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
156 static AVOnce init_static_once = AV_ONCE_INIT;
157 ATRAC3PContext *ctx = avctx->priv_data;
160 if (!avctx->block_align) {
161 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
162 return AVERROR(EINVAL);
165 /* initialize IPQF */
166 ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
168 ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
170 ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
172 if ((ret = set_channel_params(ctx, avctx)) < 0)
175 ctx->my_channel_layout = avctx->channel_layout;
177 ctx->ch_units = av_mallocz_array(ctx->num_channel_blocks, sizeof(*ctx->ch_units));
178 ctx->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
180 if (!ctx->ch_units || !ctx->fdsp) {
181 return AVERROR(ENOMEM);
184 for (i = 0; i < ctx->num_channel_blocks; i++) {
185 for (ch = 0; ch < 2; ch++) {
186 ctx->ch_units[i].channels[ch].ch_num = ch;
187 ctx->ch_units[i].channels[ch].wnd_shape = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
188 ctx->ch_units[i].channels[ch].wnd_shape_prev = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
189 ctx->ch_units[i].channels[ch].gain_data = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
190 ctx->ch_units[i].channels[ch].gain_data_prev = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
191 ctx->ch_units[i].channels[ch].tones_info = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
192 ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
195 ctx->ch_units[i].waves_info = &ctx->ch_units[i].wave_synth_hist[0];
196 ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
199 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
201 ff_thread_once(&init_static_once, atrac3p_init_static);
206 static void decode_residual_spectrum(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
207 float out[2][ATRAC3P_FRAME_SAMPLES],
209 AVCodecContext *avctx)
211 int i, sb, ch, qu, nspeclines, RNG_index;
214 /* calculate RNG table index for each subband */
215 int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
217 if (ch_unit->mute_flag) {
218 for (ch = 0; ch < num_channels; ch++)
219 memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
223 for (qu = 0, RNG_index = 0; qu < ch_unit->used_quant_units; qu++)
224 RNG_index += ch_unit->channels[0].qu_sf_idx[qu] +
225 ch_unit->channels[1].qu_sf_idx[qu];
227 for (sb = 0; sb < ch_unit->num_coded_subbands; sb++, RNG_index += 128)
228 sb_RNG_index[sb] = RNG_index & 0x3FC;
230 /* inverse quant and power compensation */
231 for (ch = 0; ch < num_channels; ch++) {
232 /* clear channel's residual spectrum */
233 memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
235 for (qu = 0; qu < ch_unit->used_quant_units; qu++) {
236 src = &ch_unit->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
237 dst = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
238 nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
239 ff_atrac3p_qu_to_spec_pos[qu];
241 if (ch_unit->channels[ch].qu_wordlen[qu] > 0) {
242 q = ff_atrac3p_sf_tab[ch_unit->channels[ch].qu_sf_idx[qu]] *
243 ff_atrac3p_mant_tab[ch_unit->channels[ch].qu_wordlen[qu]];
244 for (i = 0; i < nspeclines; i++)
249 for (sb = 0; sb < ch_unit->num_coded_subbands; sb++)
250 ff_atrac3p_power_compensation(ch_unit, ctx->fdsp, ch, &out[ch][0],
251 sb_RNG_index[sb], sb);
254 if (ch_unit->unit_type == CH_UNIT_STEREO) {
255 for (sb = 0; sb < ch_unit->num_coded_subbands; sb++) {
256 if (ch_unit->swap_channels[sb]) {
257 for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
258 FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
259 out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
262 /* flip coefficients' sign if requested */
263 if (ch_unit->negate_coeffs[sb])
264 for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
265 out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
270 static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
271 int num_channels, AVCodecContext *avctx)
275 for (ch = 0; ch < num_channels; ch++) {
276 for (sb = 0; sb < ch_unit->num_subbands; sb++) {
277 /* inverse transform and windowing */
278 ff_atrac3p_imdct(ctx->fdsp, &ctx->mdct_ctx,
279 &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
280 &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
281 (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
282 ch_unit->channels[ch].wnd_shape[sb], sb);
284 /* gain compensation and overlapping */
285 ff_atrac_gain_compensation(&ctx->gainc_ctx,
286 &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
287 &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
288 &ch_unit->channels[ch].gain_data_prev[sb],
289 &ch_unit->channels[ch].gain_data[sb],
290 ATRAC3P_SUBBAND_SAMPLES,
291 &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
294 /* zero unused subbands in both output and overlapping buffers */
295 memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
297 (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
298 ATRAC3P_SUBBAND_SAMPLES *
299 sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
300 memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
302 (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
303 ATRAC3P_SUBBAND_SAMPLES *
304 sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
306 /* resynthesize and add tonal signal */
307 if (ch_unit->waves_info->tones_present ||
308 ch_unit->waves_info_prev->tones_present) {
309 for (sb = 0; sb < ch_unit->num_subbands; sb++)
310 if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
311 ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
312 ff_atrac3p_generate_tones(ch_unit, ctx->fdsp, ch, sb,
313 &ctx->time_buf[ch][sb * 128]);
317 /* subband synthesis and acoustic signal output */
318 ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
319 &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
322 /* swap window shape and gain control buffers. */
323 for (ch = 0; ch < num_channels; ch++) {
324 FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
325 ch_unit->channels[ch].wnd_shape_prev);
326 FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
327 ch_unit->channels[ch].gain_data_prev);
328 FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
329 ch_unit->channels[ch].tones_info_prev);
332 FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
335 static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
336 int *got_frame_ptr, AVPacket *avpkt)
338 ATRAC3PContext *ctx = avctx->priv_data;
339 AVFrame *frame = data;
340 int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
341 float **samples_p = (float **)frame->extended_data;
343 frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
344 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
347 if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
350 if (get_bits1(&ctx->gb)) {
351 av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
352 return AVERROR_INVALIDDATA;
355 while (get_bits_left(&ctx->gb) >= 2 &&
356 (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
357 if (ch_unit_id == CH_UNIT_EXTENSION) {
358 avpriv_report_missing_feature(avctx, "Channel unit extension");
359 return AVERROR_PATCHWELCOME;
361 if (ch_block >= ctx->num_channel_blocks ||
362 ctx->channel_blocks[ch_block] != ch_unit_id) {
363 av_log(avctx, AV_LOG_ERROR,
364 "Frame data doesn't match channel configuration!\n");
365 return AVERROR_INVALIDDATA;
368 ctx->ch_units[ch_block].unit_type = ch_unit_id;
369 channels_to_process = ch_unit_id + 1;
371 if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
372 &ctx->ch_units[ch_block],
377 decode_residual_spectrum(ctx, &ctx->ch_units[ch_block], ctx->samples,
378 channels_to_process, avctx);
379 reconstruct_frame(ctx, &ctx->ch_units[ch_block],
380 channels_to_process, avctx);
382 for (i = 0; i < channels_to_process; i++)
383 memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
384 ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
387 out_ch_index += channels_to_process;
392 return avctx->codec_id == AV_CODEC_ID_ATRAC3P ? FFMIN(avctx->block_align, avpkt->size) : avpkt->size;
395 AVCodec ff_atrac3p_decoder = {
396 .name = "atrac3plus",
397 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
398 .type = AVMEDIA_TYPE_AUDIO,
399 .id = AV_CODEC_ID_ATRAC3P,
400 .capabilities = AV_CODEC_CAP_DR1,
401 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
402 .priv_data_size = sizeof(ATRAC3PContext),
403 .init = atrac3p_decode_init,
404 .close = atrac3p_decode_close,
405 .decode = atrac3p_decode_frame,
408 AVCodec ff_atrac3pal_decoder = {
409 .name = "atrac3plusal",
410 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ AL (Adaptive TRansform Acoustic Coding 3+ Advanced Lossless)"),
411 .type = AVMEDIA_TYPE_AUDIO,
412 .id = AV_CODEC_ID_ATRAC3PAL,
413 .capabilities = AV_CODEC_CAP_DR1,
414 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
415 .priv_data_size = sizeof(ATRAC3PContext),
416 .init = atrac3p_decode_init,
417 .close = atrac3p_decode_close,
418 .decode = atrac3p_decode_frame,