2 * ATRAC3 compatible decoder
3 * Copyright (c) 2006-2008 Maxim Poliakovski
4 * Copyright (c) 2006-2008 Benjamin Larsson
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 * 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"
41 #include "libavutil/libm.h"
43 #include "bytestream.h"
49 #include "atrac3data.h"
51 #define MIN_CHANNELS 1
52 #define MAX_CHANNELS 8
53 #define MAX_JS_PAIRS 8 / 2
55 #define JOINT_STEREO 0x12
58 #define SAMPLES_PER_FRAME 1024
61 typedef struct GainBlock {
62 AtracGainInfo g_block[4];
65 typedef struct TonalComponent {
71 typedef struct ChannelUnit {
74 float prev_frame[SAMPLES_PER_FRAME];
76 TonalComponent components[64];
77 GainBlock gain_block[2];
79 DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
80 DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
82 float delay_buf1[46]; ///<qmf delay buffers
87 typedef struct ATRAC3Context {
96 /** joint-stereo related variables */
97 int matrix_coeff_index_prev[MAX_JS_PAIRS][4];
98 int matrix_coeff_index_now[MAX_JS_PAIRS][4];
99 int matrix_coeff_index_next[MAX_JS_PAIRS][4];
100 int weighting_delay[MAX_JS_PAIRS][6];
104 uint8_t *decoded_bytes_buffer;
105 float temp_buf[1070];
109 int scrambled_stream;
112 AtracGCContext gainc_ctx;
114 AVFloatDSPContext *fdsp;
117 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
118 static VLC_TYPE atrac3_vlc_table[4096][2];
119 static VLC spectral_coeff_tab[7];
122 * Regular 512 points IMDCT without overlapping, with the exception of the
123 * swapping of odd bands caused by the reverse spectra of the QMF.
125 * @param odd_band 1 if the band is an odd band
127 static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
133 * Reverse the odd bands before IMDCT, this is an effect of the QMF
134 * transform or it gives better compression to do it this way.
135 * FIXME: It should be possible to handle this in imdct_calc
136 * for that to happen a modification of the prerotation step of
137 * all SIMD code and C code is needed.
138 * Or fix the functions before so they generate a pre reversed spectrum.
140 for (i = 0; i < 128; i++)
141 FFSWAP(float, input[i], input[255 - i]);
144 q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
146 /* Perform windowing on the output. */
147 q->fdsp->vector_fmul(output, output, mdct_window, MDCT_SIZE);
151 * indata descrambling, only used for data coming from the rm container
153 static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
158 uint32_t *output = (uint32_t *)out;
160 off = (intptr_t)input & 3;
161 buf = (const uint32_t *)(input - off);
163 c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
165 c = av_be2ne32(0x537F6103U);
167 for (i = 0; i < bytes / 4; i++)
168 output[i] = c ^ buf[i];
171 avpriv_request_sample(NULL, "Offset of %d", off);
176 static av_cold void init_imdct_window(void)
180 /* generate the mdct window, for details see
181 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
182 for (i = 0, j = 255; i < 128; i++, j--) {
183 float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
184 float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
185 float w = 0.5 * (wi * wi + wj * wj);
186 mdct_window[i] = mdct_window[511 - i] = wi / w;
187 mdct_window[j] = mdct_window[511 - j] = wj / w;
191 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
193 ATRAC3Context *q = avctx->priv_data;
196 av_freep(&q->decoded_bytes_buffer);
199 ff_mdct_end(&q->mdct_ctx);
207 * @param selector which table the output values are coded with
208 * @param coding_flag constant length coding or variable length coding
209 * @param mantissas mantissa output table
210 * @param num_codes number of values to get
212 static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
213 int coding_flag, int *mantissas,
216 int i, code, huff_symb;
221 if (coding_flag != 0) {
222 /* constant length coding (CLC) */
223 int num_bits = clc_length_tab[selector];
226 for (i = 0; i < num_codes; i++) {
228 code = get_sbits(gb, num_bits);
234 for (i = 0; i < num_codes; i++) {
236 code = get_bits(gb, num_bits); // num_bits is always 4 in this case
239 mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
240 mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
244 /* variable length coding (VLC) */
246 for (i = 0; i < num_codes; i++) {
247 huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
248 spectral_coeff_tab[selector-1].bits, 3);
250 code = huff_symb >> 1;
256 for (i = 0; i < num_codes; i++) {
257 huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
258 spectral_coeff_tab[selector - 1].bits, 3);
259 mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
260 mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
267 * Restore the quantized band spectrum coefficients
269 * @return subband count, fix for broken specification/files
271 static int decode_spectrum(GetBitContext *gb, float *output)
273 int num_subbands, coding_mode, i, j, first, last, subband_size;
274 int subband_vlc_index[32], sf_index[32];
278 num_subbands = get_bits(gb, 5); // number of coded subbands
279 coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
281 /* get the VLC selector table for the subbands, 0 means not coded */
282 for (i = 0; i <= num_subbands; i++)
283 subband_vlc_index[i] = get_bits(gb, 3);
285 /* read the scale factor indexes from the stream */
286 for (i = 0; i <= num_subbands; i++) {
287 if (subband_vlc_index[i] != 0)
288 sf_index[i] = get_bits(gb, 6);
291 for (i = 0; i <= num_subbands; i++) {
292 first = subband_tab[i ];
293 last = subband_tab[i + 1];
295 subband_size = last - first;
297 if (subband_vlc_index[i] != 0) {
298 /* decode spectral coefficients for this subband */
299 /* TODO: This can be done faster is several blocks share the
300 * same VLC selector (subband_vlc_index) */
301 read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
302 mantissas, subband_size);
304 /* decode the scale factor for this subband */
305 scale_factor = ff_atrac_sf_table[sf_index[i]] *
306 inv_max_quant[subband_vlc_index[i]];
308 /* inverse quantize the coefficients */
309 for (j = 0; first < last; first++, j++)
310 output[first] = mantissas[j] * scale_factor;
312 /* this subband was not coded, so zero the entire subband */
313 memset(output + first, 0, subband_size * sizeof(*output));
317 /* clear the subbands that were not coded */
318 first = subband_tab[i];
319 memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
324 * Restore the quantized tonal components
326 * @param components tonal components
327 * @param num_bands number of coded bands
329 static int decode_tonal_components(GetBitContext *gb,
330 TonalComponent *components, int num_bands)
333 int nb_components, coding_mode_selector, coding_mode;
334 int band_flags[4], mantissa[8];
335 int component_count = 0;
337 nb_components = get_bits(gb, 5);
339 /* no tonal components */
340 if (nb_components == 0)
343 coding_mode_selector = get_bits(gb, 2);
344 if (coding_mode_selector == 2)
345 return AVERROR_INVALIDDATA;
347 coding_mode = coding_mode_selector & 1;
349 for (i = 0; i < nb_components; i++) {
350 int coded_values_per_component, quant_step_index;
352 for (b = 0; b <= num_bands; b++)
353 band_flags[b] = get_bits1(gb);
355 coded_values_per_component = get_bits(gb, 3);
357 quant_step_index = get_bits(gb, 3);
358 if (quant_step_index <= 1)
359 return AVERROR_INVALIDDATA;
361 if (coding_mode_selector == 3)
362 coding_mode = get_bits1(gb);
364 for (b = 0; b < (num_bands + 1) * 4; b++) {
365 int coded_components;
367 if (band_flags[b >> 2] == 0)
370 coded_components = get_bits(gb, 3);
372 for (c = 0; c < coded_components; c++) {
373 TonalComponent *cmp = &components[component_count];
374 int sf_index, coded_values, max_coded_values;
377 sf_index = get_bits(gb, 6);
378 if (component_count >= 64)
379 return AVERROR_INVALIDDATA;
381 cmp->pos = b * 64 + get_bits(gb, 6);
383 max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
384 coded_values = coded_values_per_component + 1;
385 coded_values = FFMIN(max_coded_values, coded_values);
387 scale_factor = ff_atrac_sf_table[sf_index] *
388 inv_max_quant[quant_step_index];
390 read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
391 mantissa, coded_values);
393 cmp->num_coefs = coded_values;
396 for (m = 0; m < coded_values; m++)
397 cmp->coef[m] = mantissa[m] * scale_factor;
404 return component_count;
408 * Decode gain parameters for the coded bands
410 * @param block the gainblock for the current band
411 * @param num_bands amount of coded bands
413 static int decode_gain_control(GetBitContext *gb, GainBlock *block,
419 AtracGainInfo *gain = block->g_block;
421 for (b = 0; b <= num_bands; b++) {
422 gain[b].num_points = get_bits(gb, 3);
423 level = gain[b].lev_code;
424 loc = gain[b].loc_code;
426 for (j = 0; j < gain[b].num_points; j++) {
427 level[j] = get_bits(gb, 4);
428 loc[j] = get_bits(gb, 5);
429 if (j && loc[j] <= loc[j - 1])
430 return AVERROR_INVALIDDATA;
434 /* Clear the unused blocks. */
436 gain[b].num_points = 0;
442 * Combine the tonal band spectrum and regular band spectrum
444 * @param spectrum output spectrum buffer
445 * @param num_components number of tonal components
446 * @param components tonal components for this band
447 * @return position of the last tonal coefficient
449 static int add_tonal_components(float *spectrum, int num_components,
450 TonalComponent *components)
452 int i, j, last_pos = -1;
453 float *input, *output;
455 for (i = 0; i < num_components; i++) {
456 last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
457 input = components[i].coef;
458 output = &spectrum[components[i].pos];
460 for (j = 0; j < components[i].num_coefs; j++)
461 output[j] += input[j];
467 #define INTERPOLATE(old, new, nsample) \
468 ((old) + (nsample) * 0.125 * ((new) - (old)))
470 static void reverse_matrixing(float *su1, float *su2, int *prev_code,
473 int i, nsample, band;
474 float mc1_l, mc1_r, mc2_l, mc2_r;
476 for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
477 int s1 = prev_code[i];
478 int s2 = curr_code[i];
482 /* Selector value changed, interpolation needed. */
483 mc1_l = matrix_coeffs[s1 * 2 ];
484 mc1_r = matrix_coeffs[s1 * 2 + 1];
485 mc2_l = matrix_coeffs[s2 * 2 ];
486 mc2_r = matrix_coeffs[s2 * 2 + 1];
488 /* Interpolation is done over the first eight samples. */
489 for (; nsample < band + 8; nsample++) {
490 float c1 = su1[nsample];
491 float c2 = su2[nsample];
492 c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
493 c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
495 su2[nsample] = c1 * 2.0 - c2;
499 /* Apply the matrix without interpolation. */
501 case 0: /* M/S decoding */
502 for (; nsample < band + 256; nsample++) {
503 float c1 = su1[nsample];
504 float c2 = su2[nsample];
505 su1[nsample] = c2 * 2.0;
506 su2[nsample] = (c1 - c2) * 2.0;
510 for (; nsample < band + 256; nsample++) {
511 float c1 = su1[nsample];
512 float c2 = su2[nsample];
513 su1[nsample] = (c1 + c2) * 2.0;
514 su2[nsample] = c2 * -2.0;
519 for (; nsample < band + 256; nsample++) {
520 float c1 = su1[nsample];
521 float c2 = su2[nsample];
522 su1[nsample] = c1 + c2;
523 su2[nsample] = c1 - c2;
532 static void get_channel_weights(int index, int flag, float ch[2])
538 ch[0] = (index & 7) / 7.0;
539 ch[1] = sqrt(2 - ch[0] * ch[0]);
541 FFSWAP(float, ch[0], ch[1]);
545 static void channel_weighting(float *su1, float *su2, int *p3)
548 /* w[x][y] y=0 is left y=1 is right */
551 if (p3[1] != 7 || p3[3] != 7) {
552 get_channel_weights(p3[1], p3[0], w[0]);
553 get_channel_weights(p3[3], p3[2], w[1]);
555 for (band = 256; band < 4 * 256; band += 256) {
556 for (nsample = band; nsample < band + 8; nsample++) {
557 su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
558 su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
560 for(; nsample < band + 256; nsample++) {
561 su1[nsample] *= w[1][0];
562 su2[nsample] *= w[1][1];
569 * Decode a Sound Unit
571 * @param snd the channel unit to be used
572 * @param output the decoded samples before IQMF in float representation
573 * @param channel_num channel number
574 * @param coding_mode the coding mode (JOINT_STEREO or single channels)
576 static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
577 ChannelUnit *snd, float *output,
578 int channel_num, int coding_mode)
580 int band, ret, num_subbands, last_tonal, num_bands;
581 GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
582 GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
584 if (coding_mode == JOINT_STEREO && (channel_num % 2) == 1) {
585 if (get_bits(gb, 2) != 3) {
586 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
587 return AVERROR_INVALIDDATA;
590 if (get_bits(gb, 6) != 0x28) {
591 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
592 return AVERROR_INVALIDDATA;
596 /* number of coded QMF bands */
597 snd->bands_coded = get_bits(gb, 2);
599 ret = decode_gain_control(gb, gain2, snd->bands_coded);
603 snd->num_components = decode_tonal_components(gb, snd->components,
605 if (snd->num_components < 0)
606 return snd->num_components;
608 num_subbands = decode_spectrum(gb, snd->spectrum);
610 /* Merge the decoded spectrum and tonal components. */
611 last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
615 /* calculate number of used MLT/QMF bands according to the amount of coded
617 num_bands = (subband_tab[num_subbands] - 1) >> 8;
619 num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
622 /* Reconstruct time domain samples. */
623 for (band = 0; band < 4; band++) {
624 /* Perform the IMDCT step without overlapping. */
625 if (band <= num_bands)
626 imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
628 memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
630 /* gain compensation and overlapping */
631 ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
632 &snd->prev_frame[band * 256],
633 &gain1->g_block[band], &gain2->g_block[band],
634 256, &output[band * 256]);
637 /* Swap the gain control buffers for the next frame. */
638 snd->gc_blk_switch ^= 1;
643 static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
646 ATRAC3Context *q = avctx->priv_data;
650 if (q->coding_mode == JOINT_STEREO) {
651 /* channel coupling mode */
653 /* Decode sound unit pairs (channels are expected to be even).
654 * Multichannel joint stereo interleaves pairs (6ch: 2ch + 2ch + 2ch) */
655 const uint8_t *js_databuf;
656 int js_pair, js_block_align;
658 js_block_align = (avctx->block_align / avctx->channels) * 2; /* block pair */
660 for (ch = 0; ch < avctx->channels; ch = ch + 2) {
662 js_databuf = databuf + js_pair * js_block_align; /* align to current pair */
664 /* Set the bitstream reader at the start of first channel sound unit. */
665 init_get_bits(&q->gb,
666 js_databuf, js_block_align * 8);
668 /* decode Sound Unit 1 */
669 ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch],
670 out_samples[ch], ch, JOINT_STEREO);
674 /* Framedata of the su2 in the joint-stereo mode is encoded in
675 * reverse byte order so we need to swap it first. */
676 if (js_databuf == q->decoded_bytes_buffer) {
677 uint8_t *ptr2 = q->decoded_bytes_buffer + js_block_align - 1;
678 ptr1 = q->decoded_bytes_buffer;
679 for (i = 0; i < js_block_align / 2; i++, ptr1++, ptr2--)
680 FFSWAP(uint8_t, *ptr1, *ptr2);
682 const uint8_t *ptr2 = js_databuf + js_block_align - 1;
683 for (i = 0; i < js_block_align; i++)
684 q->decoded_bytes_buffer[i] = *ptr2--;
687 /* Skip the sync codes (0xF8). */
688 ptr1 = q->decoded_bytes_buffer;
689 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
690 if (i >= js_block_align)
691 return AVERROR_INVALIDDATA;
695 /* set the bitstream reader at the start of the second Sound Unit */
696 ret = init_get_bits8(&q->gb,
697 ptr1, q->decoded_bytes_buffer + js_block_align - ptr1);
701 /* Fill the Weighting coeffs delay buffer */
702 memmove(q->weighting_delay[js_pair], &q->weighting_delay[js_pair][2],
703 4 * sizeof(*q->weighting_delay[js_pair]));
704 q->weighting_delay[js_pair][4] = get_bits1(&q->gb);
705 q->weighting_delay[js_pair][5] = get_bits(&q->gb, 3);
707 for (i = 0; i < 4; i++) {
708 q->matrix_coeff_index_prev[js_pair][i] = q->matrix_coeff_index_now[js_pair][i];
709 q->matrix_coeff_index_now[js_pair][i] = q->matrix_coeff_index_next[js_pair][i];
710 q->matrix_coeff_index_next[js_pair][i] = get_bits(&q->gb, 2);
713 /* Decode Sound Unit 2. */
714 ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch+1],
715 out_samples[ch+1], ch+1, JOINT_STEREO);
719 /* Reconstruct the channel coefficients. */
720 reverse_matrixing(out_samples[ch], out_samples[ch+1],
721 q->matrix_coeff_index_prev[js_pair],
722 q->matrix_coeff_index_now[js_pair]);
724 channel_weighting(out_samples[ch], out_samples[ch+1], q->weighting_delay[js_pair]);
727 /* single channels */
728 /* Decode the channel sound units. */
729 for (i = 0; i < avctx->channels; i++) {
730 /* Set the bitstream reader at the start of a channel sound unit. */
731 init_get_bits(&q->gb,
732 databuf + i * avctx->block_align / avctx->channels,
733 avctx->block_align * 8 / avctx->channels);
735 ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
736 out_samples[i], i, q->coding_mode);
742 /* Apply the iQMF synthesis filter. */
743 for (i = 0; i < avctx->channels; i++) {
744 float *p1 = out_samples[i];
745 float *p2 = p1 + 256;
746 float *p3 = p2 + 256;
747 float *p4 = p3 + 256;
748 ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
749 ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
750 ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
756 static int al_decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
757 int size, float **out_samples)
759 ATRAC3Context *q = avctx->priv_data;
762 /* Set the bitstream reader at the start of a channel sound unit. */
763 init_get_bits(&q->gb, databuf, size * 8);
764 /* single channels */
765 /* Decode the channel sound units. */
766 for (i = 0; i < avctx->channels; i++) {
767 ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
768 out_samples[i], i, q->coding_mode);
771 while (i < avctx->channels && get_bits_left(&q->gb) > 6 && show_bits(&q->gb, 6) != 0x28) {
772 skip_bits(&q->gb, 1);
776 /* Apply the iQMF synthesis filter. */
777 for (i = 0; i < avctx->channels; i++) {
778 float *p1 = out_samples[i];
779 float *p2 = p1 + 256;
780 float *p3 = p2 + 256;
781 float *p4 = p3 + 256;
782 ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
783 ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
784 ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
790 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
791 int *got_frame_ptr, AVPacket *avpkt)
793 AVFrame *frame = data;
794 const uint8_t *buf = avpkt->data;
795 int buf_size = avpkt->size;
796 ATRAC3Context *q = avctx->priv_data;
798 const uint8_t *databuf;
800 if (buf_size < avctx->block_align) {
801 av_log(avctx, AV_LOG_ERROR,
802 "Frame too small (%d bytes). Truncated file?\n", buf_size);
803 return AVERROR_INVALIDDATA;
806 /* get output buffer */
807 frame->nb_samples = SAMPLES_PER_FRAME;
808 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
811 /* Check if we need to descramble and what buffer to pass on. */
812 if (q->scrambled_stream) {
813 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
814 databuf = q->decoded_bytes_buffer;
819 ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
821 av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n");
827 return avctx->block_align;
830 static int atrac3al_decode_frame(AVCodecContext *avctx, void *data,
831 int *got_frame_ptr, AVPacket *avpkt)
833 AVFrame *frame = data;
836 frame->nb_samples = SAMPLES_PER_FRAME;
837 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
840 ret = al_decode_frame(avctx, avpkt->data, avpkt->size,
841 (float **)frame->extended_data);
843 av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n");
852 static av_cold void atrac3_init_static_data(void)
857 ff_atrac_generate_tables();
859 /* Initialize the VLC tables. */
860 for (i = 0; i < 7; i++) {
861 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
862 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
864 init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
866 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
870 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
872 static int static_init_done;
874 int version, delay, samples_per_frame, frame_factor;
875 const uint8_t *edata_ptr = avctx->extradata;
876 ATRAC3Context *q = avctx->priv_data;
878 if (avctx->channels < MIN_CHANNELS || avctx->channels > MAX_CHANNELS) {
879 av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
880 return AVERROR(EINVAL);
883 if (!static_init_done)
884 atrac3_init_static_data();
885 static_init_done = 1;
887 /* Take care of the codec-specific extradata. */
888 if (avctx->codec_id == AV_CODEC_ID_ATRAC3AL) {
890 samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
892 q->coding_mode = SINGLE;
893 } else if (avctx->extradata_size == 14) {
894 /* Parse the extradata, WAV format */
895 av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
896 bytestream_get_le16(&edata_ptr)); // Unknown value always 1
897 edata_ptr += 4; // samples per channel
898 q->coding_mode = bytestream_get_le16(&edata_ptr);
899 av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
900 bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
901 frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
902 av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
903 bytestream_get_le16(&edata_ptr)); // Unknown always 0
906 samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
909 q->coding_mode = q->coding_mode ? JOINT_STEREO : SINGLE;
910 q->scrambled_stream = 0;
912 if (avctx->block_align != 96 * avctx->channels * frame_factor &&
913 avctx->block_align != 152 * avctx->channels * frame_factor &&
914 avctx->block_align != 192 * avctx->channels * frame_factor) {
915 av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
916 "configuration %d/%d/%d\n", avctx->block_align,
917 avctx->channels, frame_factor);
918 return AVERROR_INVALIDDATA;
920 } else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) {
921 /* Parse the extradata, RM format. */
922 version = bytestream_get_be32(&edata_ptr);
923 samples_per_frame = bytestream_get_be16(&edata_ptr);
924 delay = bytestream_get_be16(&edata_ptr);
925 q->coding_mode = bytestream_get_be16(&edata_ptr);
926 q->scrambled_stream = 1;
929 av_log(avctx, AV_LOG_ERROR, "Unknown extradata size %d.\n",
930 avctx->extradata_size);
931 return AVERROR(EINVAL);
934 /* Check the extradata */
937 av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
938 return AVERROR_INVALIDDATA;
941 if (samples_per_frame != SAMPLES_PER_FRAME * avctx->channels) {
942 av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
944 return AVERROR_INVALIDDATA;
947 if (delay != 0x88E) {
948 av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
950 return AVERROR_INVALIDDATA;
953 if (q->coding_mode == SINGLE)
954 av_log(avctx, AV_LOG_DEBUG, "Single channels detected.\n");
955 else if (q->coding_mode == JOINT_STEREO) {
956 if (avctx->channels % 2 == 1) { /* Joint stereo channels must be even */
957 av_log(avctx, AV_LOG_ERROR, "Invalid joint stereo channel configuration.\n");
958 return AVERROR_INVALIDDATA;
960 av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
962 av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
964 return AVERROR_INVALIDDATA;
967 if (avctx->block_align >= UINT_MAX / 2)
968 return AVERROR(EINVAL);
970 q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
971 AV_INPUT_BUFFER_PADDING_SIZE);
972 if (!q->decoded_bytes_buffer)
973 return AVERROR(ENOMEM);
975 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
977 /* initialize the MDCT transform */
978 if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
979 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
980 av_freep(&q->decoded_bytes_buffer);
984 /* init the joint-stereo decoding data */
985 for (js_pair = 0; js_pair < MAX_JS_PAIRS; js_pair++) {
986 q->weighting_delay[js_pair][0] = 0;
987 q->weighting_delay[js_pair][1] = 7;
988 q->weighting_delay[js_pair][2] = 0;
989 q->weighting_delay[js_pair][3] = 7;
990 q->weighting_delay[js_pair][4] = 0;
991 q->weighting_delay[js_pair][5] = 7;
993 for (i = 0; i < 4; i++) {
994 q->matrix_coeff_index_prev[js_pair][i] = 3;
995 q->matrix_coeff_index_now[js_pair][i] = 3;
996 q->matrix_coeff_index_next[js_pair][i] = 3;
1000 ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
1001 q->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1003 q->units = av_mallocz_array(avctx->channels, sizeof(*q->units));
1004 if (!q->units || !q->fdsp) {
1005 atrac3_decode_close(avctx);
1006 return AVERROR(ENOMEM);
1012 AVCodec ff_atrac3_decoder = {
1014 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
1015 .type = AVMEDIA_TYPE_AUDIO,
1016 .id = AV_CODEC_ID_ATRAC3,
1017 .priv_data_size = sizeof(ATRAC3Context),
1018 .init = atrac3_decode_init,
1019 .close = atrac3_decode_close,
1020 .decode = atrac3_decode_frame,
1021 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1022 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1023 AV_SAMPLE_FMT_NONE },
1026 AVCodec ff_atrac3al_decoder = {
1028 .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"),
1029 .type = AVMEDIA_TYPE_AUDIO,
1030 .id = AV_CODEC_ID_ATRAC3AL,
1031 .priv_data_size = sizeof(ATRAC3Context),
1032 .init = atrac3_decode_init,
1033 .close = atrac3_decode_close,
1034 .decode = atrac3al_decode_frame,
1035 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1036 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1037 AV_SAMPLE_FMT_NONE },