2 * ATRAC3 compatible decoder
3 * Copyright (c) 2006-2008 Maxim Poliakovski
4 * Copyright (c) 2006-2008 Benjamin Larsson
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 * ATRAC3 compatible decoder.
26 * This decoder handles Sony's ATRAC3 data.
28 * Container formats used to store ATRAC3 data:
29 * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
31 * To use this decoder, a calling application must supply the extradata
32 * bytes provided in the containers above.
39 #include "libavutil/attributes.h"
40 #include "libavutil/float_dsp.h"
43 #include "bitstream.h"
44 #include "bytestream.h"
49 #include "atrac3data.h"
51 #define JOINT_STEREO 0x12
54 #define SAMPLES_PER_FRAME 1024
57 typedef struct GainBlock {
58 AtracGainInfo g_block[4];
61 typedef struct TonalComponent {
67 typedef struct ChannelUnit {
70 float prev_frame[SAMPLES_PER_FRAME];
72 TonalComponent components[64];
73 GainBlock gain_block[2];
75 DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
76 DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
78 float delay_buf1[46]; ///<qmf delay buffers
83 typedef struct ATRAC3Context {
92 /** joint-stereo related variables */
93 int matrix_coeff_index_prev[4];
94 int matrix_coeff_index_now[4];
95 int matrix_coeff_index_next[4];
96 int weighting_delay[6];
100 uint8_t *decoded_bytes_buffer;
101 float temp_buf[1070];
105 int scrambled_stream;
108 AtracGCContext gainc_ctx;
110 AVFloatDSPContext fdsp;
113 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
114 static VLC_TYPE atrac3_vlc_table[4096][2];
115 static VLC spectral_coeff_tab[7];
118 * Regular 512 points IMDCT without overlapping, with the exception of the
119 * swapping of odd bands caused by the reverse spectra of the QMF.
121 * @param odd_band 1 if the band is an odd band
123 static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
129 * Reverse the odd bands before IMDCT, this is an effect of the QMF
130 * transform or it gives better compression to do it this way.
131 * FIXME: It should be possible to handle this in imdct_calc
132 * for that to happen a modification of the prerotation step of
133 * all SIMD code and C code is needed.
134 * Or fix the functions before so they generate a pre reversed spectrum.
136 for (i = 0; i < 128; i++)
137 FFSWAP(float, input[i], input[255 - i]);
140 q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
142 /* Perform windowing on the output. */
143 q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE);
147 * indata descrambling, only used for data coming from the rm container
149 static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
154 uint32_t *output = (uint32_t *)out;
156 off = (intptr_t)input & 3;
157 buf = (const uint32_t *)(input - off);
159 c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
161 c = av_be2ne32(0x537F6103U);
163 for (i = 0; i < bytes / 4; i++)
164 output[i] = c ^ buf[i];
167 avpriv_request_sample(NULL, "Offset of %d", off);
172 static av_cold void init_imdct_window(void)
176 /* generate the mdct window, for details see
177 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
178 for (i = 0, j = 255; i < 128; i++, j--) {
179 float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
180 float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
181 float w = 0.5 * (wi * wi + wj * wj);
182 mdct_window[i] = mdct_window[511 - i] = wi / w;
183 mdct_window[j] = mdct_window[511 - j] = wj / w;
187 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
189 ATRAC3Context *q = avctx->priv_data;
192 av_free(q->decoded_bytes_buffer);
194 ff_mdct_end(&q->mdct_ctx);
202 * @param selector which table the output values are coded with
203 * @param coding_flag constant length coding or variable length coding
204 * @param mantissas mantissa output table
205 * @param num_codes number of values to get
207 static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector,
208 int coding_flag, int *mantissas,
211 int i, code, huff_symb;
216 if (coding_flag != 0) {
217 /* constant length coding (CLC) */
218 int num_bits = clc_length_tab[selector];
221 for (i = 0; i < num_codes; i++) {
223 code = bitstream_read_signed(bc, num_bits);
229 for (i = 0; i < num_codes; i++) {
231 code = bitstream_read(bc, num_bits); // num_bits is always 4 in this case
234 mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
235 mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
239 /* variable length coding (VLC) */
241 for (i = 0; i < num_codes; i++) {
242 huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector-1].table,
243 spectral_coeff_tab[selector-1].bits, 3);
245 code = huff_symb >> 1;
251 for (i = 0; i < num_codes; i++) {
252 huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector - 1].table,
253 spectral_coeff_tab[selector - 1].bits, 3);
254 mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
255 mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
262 * Restore the quantized band spectrum coefficients
264 * @return subband count, fix for broken specification/files
266 static int decode_spectrum(BitstreamContext *bc, float *output)
268 int num_subbands, coding_mode, i, j, first, last, subband_size;
269 int subband_vlc_index[32], sf_index[32];
273 num_subbands = bitstream_read(bc, 5); // number of coded subbands
274 coding_mode = bitstream_read_bit(bc); // coding Mode: 0 - VLC/ 1 - CLC
276 /* get the VLC selector table for the subbands, 0 means not coded */
277 for (i = 0; i <= num_subbands; i++)
278 subband_vlc_index[i] = bitstream_read(bc, 3);
280 /* read the scale factor indexes from the stream */
281 for (i = 0; i <= num_subbands; i++) {
282 if (subband_vlc_index[i] != 0)
283 sf_index[i] = bitstream_read(bc, 6);
286 for (i = 0; i <= num_subbands; i++) {
287 first = subband_tab[i ];
288 last = subband_tab[i + 1];
290 subband_size = last - first;
292 if (subband_vlc_index[i] != 0) {
293 /* decode spectral coefficients for this subband */
294 /* TODO: This can be done faster is several blocks share the
295 * same VLC selector (subband_vlc_index) */
296 read_quant_spectral_coeffs(bc, subband_vlc_index[i], coding_mode,
297 mantissas, subband_size);
299 /* decode the scale factor for this subband */
300 scale_factor = ff_atrac_sf_table[sf_index[i]] *
301 inv_max_quant[subband_vlc_index[i]];
303 /* inverse quantize the coefficients */
304 for (j = 0; first < last; first++, j++)
305 output[first] = mantissas[j] * scale_factor;
307 /* this subband was not coded, so zero the entire subband */
308 memset(output + first, 0, subband_size * sizeof(*output));
312 /* clear the subbands that were not coded */
313 first = subband_tab[i];
314 memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
319 * Restore the quantized tonal components
321 * @param components tonal components
322 * @param num_bands number of coded bands
324 static int decode_tonal_components(BitstreamContext *bc,
325 TonalComponent *components, int num_bands)
328 int nb_components, coding_mode_selector, coding_mode;
329 int band_flags[4], mantissa[8];
330 int component_count = 0;
332 nb_components = bitstream_read(bc, 5);
334 /* no tonal components */
335 if (nb_components == 0)
338 coding_mode_selector = bitstream_read(bc, 2);
339 if (coding_mode_selector == 2)
340 return AVERROR_INVALIDDATA;
342 coding_mode = coding_mode_selector & 1;
344 for (i = 0; i < nb_components; i++) {
345 int coded_values_per_component, quant_step_index;
347 for (b = 0; b <= num_bands; b++)
348 band_flags[b] = bitstream_read_bit(bc);
350 coded_values_per_component = bitstream_read(bc, 3);
352 quant_step_index = bitstream_read(bc, 3);
353 if (quant_step_index <= 1)
354 return AVERROR_INVALIDDATA;
356 if (coding_mode_selector == 3)
357 coding_mode = bitstream_read_bit(bc);
359 for (b = 0; b < (num_bands + 1) * 4; b++) {
360 int coded_components;
362 if (band_flags[b >> 2] == 0)
365 coded_components = bitstream_read(bc, 3);
367 for (c = 0; c < coded_components; c++) {
368 TonalComponent *cmp = &components[component_count];
369 int sf_index, coded_values, max_coded_values;
372 sf_index = bitstream_read(bc, 6);
373 if (component_count >= 64)
374 return AVERROR_INVALIDDATA;
376 cmp->pos = b * 64 + bitstream_read(bc, 6);
378 max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
379 coded_values = coded_values_per_component + 1;
380 coded_values = FFMIN(max_coded_values, coded_values);
382 scale_factor = ff_atrac_sf_table[sf_index] *
383 inv_max_quant[quant_step_index];
385 read_quant_spectral_coeffs(bc, quant_step_index, coding_mode,
386 mantissa, coded_values);
388 cmp->num_coefs = coded_values;
391 for (m = 0; m < coded_values; m++)
392 cmp->coef[m] = mantissa[m] * scale_factor;
399 return component_count;
403 * Decode gain parameters for the coded bands
405 * @param block the gainblock for the current band
406 * @param num_bands amount of coded bands
408 static int decode_gain_control(BitstreamContext *bc, GainBlock *block,
414 AtracGainInfo *gain = block->g_block;
416 for (i = 0; i <= num_bands; i++) {
417 gain[i].num_points = bitstream_read(bc, 3);
418 level = gain[i].lev_code;
419 loc = gain[i].loc_code;
421 for (j = 0; j < gain[i].num_points; j++) {
422 level[j] = bitstream_read(bc, 4);
423 loc[j] = bitstream_read(bc, 5);
424 if (j && loc[j] <= loc[j - 1])
425 return AVERROR_INVALIDDATA;
429 /* Clear the unused blocks. */
431 gain[i].num_points = 0;
437 * Combine the tonal band spectrum and regular band spectrum
439 * @param spectrum output spectrum buffer
440 * @param num_components number of tonal components
441 * @param components tonal components for this band
442 * @return position of the last tonal coefficient
444 static int add_tonal_components(float *spectrum, int num_components,
445 TonalComponent *components)
447 int i, j, last_pos = -1;
448 float *input, *output;
450 for (i = 0; i < num_components; i++) {
451 last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
452 input = components[i].coef;
453 output = &spectrum[components[i].pos];
455 for (j = 0; j < components[i].num_coefs; j++)
456 output[j] += input[j];
462 #define INTERPOLATE(old, new, nsample) \
463 ((old) + (nsample) * 0.125 * ((new) - (old)))
465 static void reverse_matrixing(float *su1, float *su2, int *prev_code,
468 int i, nsample, band;
469 float mc1_l, mc1_r, mc2_l, mc2_r;
471 for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
472 int s1 = prev_code[i];
473 int s2 = curr_code[i];
477 /* Selector value changed, interpolation needed. */
478 mc1_l = matrix_coeffs[s1 * 2 ];
479 mc1_r = matrix_coeffs[s1 * 2 + 1];
480 mc2_l = matrix_coeffs[s2 * 2 ];
481 mc2_r = matrix_coeffs[s2 * 2 + 1];
483 /* Interpolation is done over the first eight samples. */
484 for (; nsample < band + 8; nsample++) {
485 float c1 = su1[nsample];
486 float c2 = su2[nsample];
487 c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
488 c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
490 su2[nsample] = c1 * 2.0 - c2;
494 /* Apply the matrix without interpolation. */
496 case 0: /* M/S decoding */
497 for (; nsample < band + 256; nsample++) {
498 float c1 = su1[nsample];
499 float c2 = su2[nsample];
500 su1[nsample] = c2 * 2.0;
501 su2[nsample] = (c1 - c2) * 2.0;
505 for (; nsample < band + 256; nsample++) {
506 float c1 = su1[nsample];
507 float c2 = su2[nsample];
508 su1[nsample] = (c1 + c2) * 2.0;
509 su2[nsample] = c2 * -2.0;
514 for (; nsample < band + 256; nsample++) {
515 float c1 = su1[nsample];
516 float c2 = su2[nsample];
517 su1[nsample] = c1 + c2;
518 su2[nsample] = c1 - c2;
527 static void get_channel_weights(int index, int flag, float ch[2])
533 ch[0] = (index & 7) / 7.0;
534 ch[1] = sqrt(2 - ch[0] * ch[0]);
536 FFSWAP(float, ch[0], ch[1]);
540 static void channel_weighting(float *su1, float *su2, int *p3)
543 /* w[x][y] y=0 is left y=1 is right */
546 if (p3[1] != 7 || p3[3] != 7) {
547 get_channel_weights(p3[1], p3[0], w[0]);
548 get_channel_weights(p3[3], p3[2], w[1]);
550 for (band = 256; band < 4 * 256; band += 256) {
551 for (nsample = band; nsample < band + 8; nsample++) {
552 su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
553 su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
555 for(; nsample < band + 256; nsample++) {
556 su1[nsample] *= w[1][0];
557 su2[nsample] *= w[1][1];
564 * Decode a Sound Unit
566 * @param snd the channel unit to be used
567 * @param output the decoded samples before IQMF in float representation
568 * @param channel_num channel number
569 * @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono)
571 static int decode_channel_sound_unit(ATRAC3Context *q, BitstreamContext *bc,
572 ChannelUnit *snd, float *output,
573 int channel_num, int coding_mode)
575 int band, ret, num_subbands, last_tonal, num_bands;
576 GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
577 GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
579 if (coding_mode == JOINT_STEREO && channel_num == 1) {
580 if (bitstream_read(bc, 2) != 3) {
581 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
582 return AVERROR_INVALIDDATA;
585 if (bitstream_read(bc, 6) != 0x28) {
586 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
587 return AVERROR_INVALIDDATA;
591 /* number of coded QMF bands */
592 snd->bands_coded = bitstream_read(bc, 2);
594 ret = decode_gain_control(bc, gain2, snd->bands_coded);
598 snd->num_components = decode_tonal_components(bc, snd->components,
600 if (snd->num_components < 0)
601 return snd->num_components;
603 num_subbands = decode_spectrum(bc, snd->spectrum);
605 /* Merge the decoded spectrum and tonal components. */
606 last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
610 /* calculate number of used MLT/QMF bands according to the amount of coded
612 num_bands = (subband_tab[num_subbands] - 1) >> 8;
614 num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
617 /* Reconstruct time domain samples. */
618 for (band = 0; band < 4; band++) {
619 /* Perform the IMDCT step without overlapping. */
620 if (band <= num_bands)
621 imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
623 memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
625 /* gain compensation and overlapping */
626 ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
627 &snd->prev_frame[band * 256],
628 &gain1->g_block[band], &gain2->g_block[band],
629 256, &output[band * 256]);
632 /* Swap the gain control buffers for the next frame. */
633 snd->gc_blk_switch ^= 1;
638 static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
641 ATRAC3Context *q = avctx->priv_data;
645 if (q->coding_mode == JOINT_STEREO) {
646 /* channel coupling mode */
647 /* decode Sound Unit 1 */
648 bitstream_init(&q->bc, databuf, avctx->block_align * 8);
650 ret = decode_channel_sound_unit(q, &q->bc, q->units, out_samples[0], 0,
655 /* Framedata of the su2 in the joint-stereo mode is encoded in
656 * reverse byte order so we need to swap it first. */
657 if (databuf == q->decoded_bytes_buffer) {
658 uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1;
659 ptr1 = q->decoded_bytes_buffer;
660 for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--)
661 FFSWAP(uint8_t, *ptr1, *ptr2);
663 const uint8_t *ptr2 = databuf + avctx->block_align - 1;
664 for (i = 0; i < avctx->block_align; i++)
665 q->decoded_bytes_buffer[i] = *ptr2--;
668 /* Skip the sync codes (0xF8). */
669 ptr1 = q->decoded_bytes_buffer;
670 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
671 if (i >= avctx->block_align)
672 return AVERROR_INVALIDDATA;
676 /* set the bitstream reader at the start of the second Sound Unit*/
677 bitstream_init(&q->bc, ptr1, (avctx->block_align - i) * 8);
679 /* Fill the Weighting coeffs delay buffer */
680 memmove(q->weighting_delay, &q->weighting_delay[2],
681 4 * sizeof(*q->weighting_delay));
682 q->weighting_delay[4] = bitstream_read_bit(&q->bc);
683 q->weighting_delay[5] = bitstream_read(&q->bc, 3);
685 for (i = 0; i < 4; i++) {
686 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
687 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
688 q->matrix_coeff_index_next[i] = bitstream_read(&q->bc, 2);
691 /* Decode Sound Unit 2. */
692 ret = decode_channel_sound_unit(q, &q->bc, &q->units[1],
693 out_samples[1], 1, JOINT_STEREO);
697 /* Reconstruct the channel coefficients. */
698 reverse_matrixing(out_samples[0], out_samples[1],
699 q->matrix_coeff_index_prev,
700 q->matrix_coeff_index_now);
702 channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
704 /* normal stereo mode or mono */
705 /* Decode the channel sound units. */
706 for (i = 0; i < avctx->channels; i++) {
707 /* Set the bitstream reader at the start of a channel sound unit. */
708 bitstream_init(&q->bc,
709 databuf + i * avctx->block_align / avctx->channels,
710 avctx->block_align * 8 / avctx->channels);
712 ret = decode_channel_sound_unit(q, &q->bc, &q->units[i],
713 out_samples[i], i, q->coding_mode);
719 /* Apply the iQMF synthesis filter. */
720 for (i = 0; i < avctx->channels; i++) {
721 float *p1 = out_samples[i];
722 float *p2 = p1 + 256;
723 float *p3 = p2 + 256;
724 float *p4 = p3 + 256;
725 ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
726 ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
727 ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
733 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
734 int *got_frame_ptr, AVPacket *avpkt)
736 AVFrame *frame = data;
737 const uint8_t *buf = avpkt->data;
738 int buf_size = avpkt->size;
739 ATRAC3Context *q = avctx->priv_data;
741 const uint8_t *databuf;
743 if (buf_size < avctx->block_align) {
744 av_log(avctx, AV_LOG_ERROR,
745 "Frame too small (%d bytes). Truncated file?\n", buf_size);
746 return AVERROR_INVALIDDATA;
749 /* get output buffer */
750 frame->nb_samples = SAMPLES_PER_FRAME;
751 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
752 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
756 /* Check if we need to descramble and what buffer to pass on. */
757 if (q->scrambled_stream) {
758 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
759 databuf = q->decoded_bytes_buffer;
764 ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
766 av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
772 return avctx->block_align;
775 static av_cold void atrac3_init_static_data(AVCodec *codec)
780 ff_atrac_generate_tables();
782 /* Initialize the VLC tables. */
783 for (i = 0; i < 7; i++) {
784 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
785 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
787 init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
789 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
793 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
796 int version, delay, samples_per_frame, frame_factor;
797 const uint8_t *edata_ptr = avctx->extradata;
798 ATRAC3Context *q = avctx->priv_data;
800 if (avctx->channels <= 0 || avctx->channels > 2) {
801 av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
802 return AVERROR(EINVAL);
805 /* Take care of the codec-specific extradata. */
806 if (avctx->extradata_size == 14) {
807 /* Parse the extradata, WAV format */
808 av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
809 bytestream_get_le16(&edata_ptr)); // Unknown value always 1
810 edata_ptr += 4; // samples per channel
811 q->coding_mode = bytestream_get_le16(&edata_ptr);
812 av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
813 bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
814 frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
815 av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
816 bytestream_get_le16(&edata_ptr)); // Unknown always 0
819 samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
822 q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;
823 q->scrambled_stream = 0;
825 if (avctx->block_align != 96 * avctx->channels * frame_factor &&
826 avctx->block_align != 152 * avctx->channels * frame_factor &&
827 avctx->block_align != 192 * avctx->channels * frame_factor) {
828 av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
829 "configuration %d/%d/%d\n", avctx->block_align,
830 avctx->channels, frame_factor);
831 return AVERROR_INVALIDDATA;
833 } else if (avctx->extradata_size == 10) {
834 /* Parse the extradata, RM format. */
835 version = bytestream_get_be32(&edata_ptr);
836 samples_per_frame = bytestream_get_be16(&edata_ptr);
837 delay = bytestream_get_be16(&edata_ptr);
838 q->coding_mode = bytestream_get_be16(&edata_ptr);
839 q->scrambled_stream = 1;
842 av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
843 avctx->extradata_size);
844 return AVERROR(EINVAL);
847 /* Check the extradata */
850 av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
851 return AVERROR_INVALIDDATA;
854 if (samples_per_frame != SAMPLES_PER_FRAME &&
855 samples_per_frame != SAMPLES_PER_FRAME * 2) {
856 av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
858 return AVERROR_INVALIDDATA;
861 if (delay != 0x88E) {
862 av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
864 return AVERROR_INVALIDDATA;
867 if (q->coding_mode == STEREO)
868 av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
869 else if (q->coding_mode == JOINT_STEREO) {
870 if (avctx->channels != 2)
871 return AVERROR_INVALIDDATA;
872 av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
874 av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
876 return AVERROR_INVALIDDATA;
879 if (avctx->block_align >= UINT_MAX / 2)
880 return AVERROR(EINVAL);
882 q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
883 AV_INPUT_BUFFER_PADDING_SIZE);
884 if (!q->decoded_bytes_buffer)
885 return AVERROR(ENOMEM);
887 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
889 /* initialize the MDCT transform */
890 if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
891 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
892 av_freep(&q->decoded_bytes_buffer);
896 /* init the joint-stereo decoding data */
897 q->weighting_delay[0] = 0;
898 q->weighting_delay[1] = 7;
899 q->weighting_delay[2] = 0;
900 q->weighting_delay[3] = 7;
901 q->weighting_delay[4] = 0;
902 q->weighting_delay[5] = 7;
904 for (i = 0; i < 4; i++) {
905 q->matrix_coeff_index_prev[i] = 3;
906 q->matrix_coeff_index_now[i] = 3;
907 q->matrix_coeff_index_next[i] = 3;
910 ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
911 avpriv_float_dsp_init(&q->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
913 q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
915 atrac3_decode_close(avctx);
916 return AVERROR(ENOMEM);
922 AVCodec ff_atrac3_decoder = {
924 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
925 .type = AVMEDIA_TYPE_AUDIO,
926 .id = AV_CODEC_ID_ATRAC3,
927 .priv_data_size = sizeof(ATRAC3Context),
928 .init = atrac3_decode_init,
929 .init_static_data = atrac3_init_static_data,
930 .close = atrac3_decode_close,
931 .decode = atrac3_decode_frame,
932 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
933 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
934 AV_SAMPLE_FMT_NONE },