diff options
Diffstat (limited to 'libavcodec/atrac3.c')
-rw-r--r-- | libavcodec/atrac3.c | 396 |
1 files changed, 249 insertions, 147 deletions
diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c index d0661c8..6cdcdf1 100644 --- a/libavcodec/atrac3.c +++ b/libavcodec/atrac3.c @@ -3,20 +3,20 @@ * Copyright (c) 2006-2008 Maxim Poliakovski * Copyright (c) 2006-2008 Benjamin Larsson * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -38,19 +38,22 @@ #include "libavutil/attributes.h" #include "libavutil/float_dsp.h" - +#include "libavutil/libm.h" #include "avcodec.h" -#include "bitstream.h" #include "bytestream.h" #include "fft.h" +#include "get_bits.h" #include "internal.h" -#include "vlc.h" #include "atrac.h" #include "atrac3data.h" +#define MIN_CHANNELS 1 +#define MAX_CHANNELS 8 +#define MAX_JS_PAIRS 8 / 2 + #define JOINT_STEREO 0x12 -#define STEREO 0x2 +#define SINGLE 0x2 #define SAMPLES_PER_FRAME 1024 #define MDCT_SIZE 512 @@ -82,7 +85,7 @@ typedef struct ChannelUnit { } ChannelUnit; typedef struct ATRAC3Context { - BitstreamContext bc; + GetBitContext gb; //@{ /** stream data */ int coding_mode; @@ -91,10 +94,10 @@ typedef struct ATRAC3Context { //@} //@{ /** joint-stereo related variables */ - int matrix_coeff_index_prev[4]; - int matrix_coeff_index_now[4]; - int matrix_coeff_index_next[4]; - int weighting_delay[6]; + int matrix_coeff_index_prev[MAX_JS_PAIRS][4]; + int matrix_coeff_index_now[MAX_JS_PAIRS][4]; + int matrix_coeff_index_next[MAX_JS_PAIRS][4]; + int weighting_delay[MAX_JS_PAIRS][6]; //@} //@{ /** data buffers */ @@ -106,9 +109,9 @@ typedef struct ATRAC3Context { int scrambled_stream; //@} - AtracGCContext gainc_ctx; - FFTContext mdct_ctx; - AVFloatDSPContext fdsp; + AtracGCContext gainc_ctx; + FFTContext mdct_ctx; + AVFloatDSPContext *fdsp; } ATRAC3Context; static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE]; @@ -141,7 +144,7 @@ static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band) q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input); /* Perform windowing on the output. */ - q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE); + q->fdsp->vector_fmul(output, output, mdct_window, MDCT_SIZE); } /* @@ -189,8 +192,9 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx) { ATRAC3Context *q = avctx->priv_data; - av_free(q->units); - av_free(q->decoded_bytes_buffer); + av_freep(&q->units); + av_freep(&q->decoded_bytes_buffer); + av_freep(&q->fdsp); ff_mdct_end(&q->mdct_ctx); @@ -205,7 +209,7 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx) * @param mantissas mantissa output table * @param num_codes number of values to get */ -static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, +static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, int coding_flag, int *mantissas, int num_codes) { @@ -221,7 +225,7 @@ static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, if (selector > 1) { for (i = 0; i < num_codes; i++) { if (num_bits) - code = bitstream_read_signed(bc, num_bits); + code = get_sbits(gb, num_bits); else code = 0; mantissas[i] = code; @@ -229,7 +233,7 @@ static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, } else { for (i = 0; i < num_codes; i++) { if (num_bits) - code = bitstream_read(bc, num_bits); // num_bits is always 4 in this case + code = get_bits(gb, num_bits); // num_bits is always 4 in this case else code = 0; mantissas[i * 2 ] = mantissa_clc_tab[code >> 2]; @@ -240,8 +244,8 @@ static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, /* variable length coding (VLC) */ if (selector != 1) { for (i = 0; i < num_codes; i++) { - huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector-1].table, - spectral_coeff_tab[selector-1].bits, 3); + huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, + spectral_coeff_tab[selector-1].bits, 3); huff_symb += 1; code = huff_symb >> 1; if (huff_symb & 1) @@ -250,8 +254,8 @@ static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, } } else { for (i = 0; i < num_codes; i++) { - huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector - 1].table, - spectral_coeff_tab[selector - 1].bits, 3); + huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table, + spectral_coeff_tab[selector - 1].bits, 3); mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ]; mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1]; } @@ -264,24 +268,24 @@ static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector, * * @return subband count, fix for broken specification/files */ -static int decode_spectrum(BitstreamContext *bc, float *output) +static int decode_spectrum(GetBitContext *gb, float *output) { int num_subbands, coding_mode, i, j, first, last, subband_size; int subband_vlc_index[32], sf_index[32]; int mantissas[128]; float scale_factor; - num_subbands = bitstream_read(bc, 5); // number of coded subbands - coding_mode = bitstream_read_bit(bc); // coding Mode: 0 - VLC/ 1 - CLC + num_subbands = get_bits(gb, 5); // number of coded subbands + coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC /* get the VLC selector table for the subbands, 0 means not coded */ for (i = 0; i <= num_subbands; i++) - subband_vlc_index[i] = bitstream_read(bc, 3); + subband_vlc_index[i] = get_bits(gb, 3); /* read the scale factor indexes from the stream */ for (i = 0; i <= num_subbands; i++) { if (subband_vlc_index[i] != 0) - sf_index[i] = bitstream_read(bc, 6); + sf_index[i] = get_bits(gb, 6); } for (i = 0; i <= num_subbands; i++) { @@ -294,7 +298,7 @@ static int decode_spectrum(BitstreamContext *bc, float *output) /* decode spectral coefficients for this subband */ /* TODO: This can be done faster is several blocks share the * same VLC selector (subband_vlc_index) */ - read_quant_spectral_coeffs(bc, subband_vlc_index[i], coding_mode, + read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode, mantissas, subband_size); /* decode the scale factor for this subband */ @@ -322,7 +326,7 @@ static int decode_spectrum(BitstreamContext *bc, float *output) * @param components tonal components * @param num_bands number of coded bands */ -static int decode_tonal_components(BitstreamContext *bc, +static int decode_tonal_components(GetBitContext *gb, TonalComponent *components, int num_bands) { int i, b, c, m; @@ -330,13 +334,13 @@ static int decode_tonal_components(BitstreamContext *bc, int band_flags[4], mantissa[8]; int component_count = 0; - nb_components = bitstream_read(bc, 5); + nb_components = get_bits(gb, 5); /* no tonal components */ if (nb_components == 0) return 0; - coding_mode_selector = bitstream_read(bc, 2); + coding_mode_selector = get_bits(gb, 2); if (coding_mode_selector == 2) return AVERROR_INVALIDDATA; @@ -346,16 +350,16 @@ static int decode_tonal_components(BitstreamContext *bc, int coded_values_per_component, quant_step_index; for (b = 0; b <= num_bands; b++) - band_flags[b] = bitstream_read_bit(bc); + band_flags[b] = get_bits1(gb); - coded_values_per_component = bitstream_read(bc, 3); + coded_values_per_component = get_bits(gb, 3); - quant_step_index = bitstream_read(bc, 3); + quant_step_index = get_bits(gb, 3); if (quant_step_index <= 1) return AVERROR_INVALIDDATA; if (coding_mode_selector == 3) - coding_mode = bitstream_read_bit(bc); + coding_mode = get_bits1(gb); for (b = 0; b < (num_bands + 1) * 4; b++) { int coded_components; @@ -363,18 +367,18 @@ static int decode_tonal_components(BitstreamContext *bc, if (band_flags[b >> 2] == 0) continue; - coded_components = bitstream_read(bc, 3); + coded_components = get_bits(gb, 3); for (c = 0; c < coded_components; c++) { TonalComponent *cmp = &components[component_count]; int sf_index, coded_values, max_coded_values; float scale_factor; - sf_index = bitstream_read(bc, 6); + sf_index = get_bits(gb, 6); if (component_count >= 64) return AVERROR_INVALIDDATA; - cmp->pos = b * 64 + bitstream_read(bc, 6); + cmp->pos = b * 64 + get_bits(gb, 6); max_coded_values = SAMPLES_PER_FRAME - cmp->pos; coded_values = coded_values_per_component + 1; @@ -383,7 +387,7 @@ static int decode_tonal_components(BitstreamContext *bc, scale_factor = ff_atrac_sf_table[sf_index] * inv_max_quant[quant_step_index]; - read_quant_spectral_coeffs(bc, quant_step_index, coding_mode, + read_quant_spectral_coeffs(gb, quant_step_index, coding_mode, mantissa, coded_values); cmp->num_coefs = coded_values; @@ -406,30 +410,30 @@ static int decode_tonal_components(BitstreamContext *bc, * @param block the gainblock for the current band * @param num_bands amount of coded bands */ -static int decode_gain_control(BitstreamContext *bc, GainBlock *block, +static int decode_gain_control(GetBitContext *gb, GainBlock *block, int num_bands) { - int i, j; + int b, j; int *level, *loc; AtracGainInfo *gain = block->g_block; - for (i = 0; i <= num_bands; i++) { - gain[i].num_points = bitstream_read(bc, 3); - level = gain[i].lev_code; - loc = gain[i].loc_code; + for (b = 0; b <= num_bands; b++) { + gain[b].num_points = get_bits(gb, 3); + level = gain[b].lev_code; + loc = gain[b].loc_code; - for (j = 0; j < gain[i].num_points; j++) { - level[j] = bitstream_read(bc, 4); - loc[j] = bitstream_read(bc, 5); + for (j = 0; j < gain[b].num_points; j++) { + level[j] = get_bits(gb, 4); + loc[j] = get_bits(gb, 5); if (j && loc[j] <= loc[j - 1]) return AVERROR_INVALIDDATA; } } /* Clear the unused blocks. */ - for (; i < 4 ; i++) - gain[i].num_points = 0; + for (; b < 4 ; b++) + gain[b].num_points = 0; return 0; } @@ -520,7 +524,7 @@ static void reverse_matrixing(float *su1, float *su2, int *prev_code, } break; default: - assert(0); + av_assert1(0); } } } @@ -567,9 +571,9 @@ static void channel_weighting(float *su1, float *su2, int *p3) * @param snd the channel unit to be used * @param output the decoded samples before IQMF in float representation * @param channel_num channel number - * @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono) + * @param coding_mode the coding mode (JOINT_STEREO or single channels) */ -static int decode_channel_sound_unit(ATRAC3Context *q, BitstreamContext *bc, +static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, ChannelUnit *snd, float *output, int channel_num, int coding_mode) { @@ -577,31 +581,31 @@ static int decode_channel_sound_unit(ATRAC3Context *q, BitstreamContext *bc, GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch]; GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch]; - if (coding_mode == JOINT_STEREO && channel_num == 1) { - if (bitstream_read(bc, 2) != 3) { + if (coding_mode == JOINT_STEREO && (channel_num % 2) == 1) { + if (get_bits(gb, 2) != 3) { av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n"); return AVERROR_INVALIDDATA; } } else { - if (bitstream_read(bc, 6) != 0x28) { + if (get_bits(gb, 6) != 0x28) { av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n"); return AVERROR_INVALIDDATA; } } /* number of coded QMF bands */ - snd->bands_coded = bitstream_read(bc, 2); + snd->bands_coded = get_bits(gb, 2); - ret = decode_gain_control(bc, gain2, snd->bands_coded); + ret = decode_gain_control(gb, gain2, snd->bands_coded); if (ret) return ret; - snd->num_components = decode_tonal_components(bc, snd->components, + snd->num_components = decode_tonal_components(gb, snd->components, snd->bands_coded); if (snd->num_components < 0) return snd->num_components; - num_subbands = decode_spectrum(bc, snd->spectrum); + num_subbands = decode_spectrum(gb, snd->spectrum); /* Merge the decoded spectrum and tonal components. */ last_tonal = add_tonal_components(snd->spectrum, snd->num_components, @@ -640,77 +644,95 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, float **out_samples) { ATRAC3Context *q = avctx->priv_data; - int ret, i; + int ret, i, ch; uint8_t *ptr1; if (q->coding_mode == JOINT_STEREO) { /* channel coupling mode */ - /* decode Sound Unit 1 */ - bitstream_init8(&q->bc, databuf, avctx->block_align); - ret = decode_channel_sound_unit(q, &q->bc, q->units, out_samples[0], 0, - JOINT_STEREO); - if (ret != 0) - return ret; + /* Decode sound unit pairs (channels are expected to be even). + * Multichannel joint stereo interleaves pairs (6ch: 2ch + 2ch + 2ch) */ + const uint8_t *js_databuf; + int js_pair, js_block_align; - /* Framedata of the su2 in the joint-stereo mode is encoded in - * reverse byte order so we need to swap it first. */ - if (databuf == q->decoded_bytes_buffer) { - uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1; - ptr1 = q->decoded_bytes_buffer; - for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--) - FFSWAP(uint8_t, *ptr1, *ptr2); - } else { - const uint8_t *ptr2 = databuf + avctx->block_align - 1; - for (i = 0; i < avctx->block_align; i++) - q->decoded_bytes_buffer[i] = *ptr2--; - } + js_block_align = (avctx->block_align / avctx->channels) * 2; /* block pair */ - /* Skip the sync codes (0xF8). */ - ptr1 = q->decoded_bytes_buffer; - for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { - if (i >= avctx->block_align) - return AVERROR_INVALIDDATA; - } + for (ch = 0; ch < avctx->channels; ch = ch + 2) { + js_pair = ch/2; + js_databuf = databuf + js_pair * js_block_align; /* align to current pair */ + /* Set the bitstream reader at the start of first channel sound unit. */ + init_get_bits(&q->gb, + js_databuf, js_block_align * 8); - /* set the bitstream reader at the start of the second Sound Unit*/ - bitstream_init8(&q->bc, ptr1, avctx->block_align - i); + /* decode Sound Unit 1 */ + ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch], + out_samples[ch], ch, JOINT_STEREO); + if (ret != 0) + return ret; - /* Fill the Weighting coeffs delay buffer */ - memmove(q->weighting_delay, &q->weighting_delay[2], - 4 * sizeof(*q->weighting_delay)); - q->weighting_delay[4] = bitstream_read_bit(&q->bc); - q->weighting_delay[5] = bitstream_read(&q->bc, 3); + /* Framedata of the su2 in the joint-stereo mode is encoded in + * reverse byte order so we need to swap it first. */ + if (js_databuf == q->decoded_bytes_buffer) { + uint8_t *ptr2 = q->decoded_bytes_buffer + js_block_align - 1; + ptr1 = q->decoded_bytes_buffer; + for (i = 0; i < js_block_align / 2; i++, ptr1++, ptr2--) + FFSWAP(uint8_t, *ptr1, *ptr2); + } else { + const uint8_t *ptr2 = js_databuf + js_block_align - 1; + for (i = 0; i < js_block_align; i++) + q->decoded_bytes_buffer[i] = *ptr2--; + } - for (i = 0; i < 4; i++) { - q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i]; - q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i]; - q->matrix_coeff_index_next[i] = bitstream_read(&q->bc, 2); - } + /* Skip the sync codes (0xF8). */ + ptr1 = q->decoded_bytes_buffer; + for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { + if (i >= js_block_align) + return AVERROR_INVALIDDATA; + } - /* Decode Sound Unit 2. */ - ret = decode_channel_sound_unit(q, &q->bc, &q->units[1], - out_samples[1], 1, JOINT_STEREO); - if (ret != 0) - return ret; - /* Reconstruct the channel coefficients. */ - reverse_matrixing(out_samples[0], out_samples[1], - q->matrix_coeff_index_prev, - q->matrix_coeff_index_now); + /* set the bitstream reader at the start of the second Sound Unit */ + ret = init_get_bits8(&q->gb, + ptr1, q->decoded_bytes_buffer + js_block_align - ptr1); + if (ret < 0) + return ret; + + /* Fill the Weighting coeffs delay buffer */ + memmove(q->weighting_delay[js_pair], &q->weighting_delay[js_pair][2], + 4 * sizeof(*q->weighting_delay[js_pair])); + q->weighting_delay[js_pair][4] = get_bits1(&q->gb); + q->weighting_delay[js_pair][5] = get_bits(&q->gb, 3); - channel_weighting(out_samples[0], out_samples[1], q->weighting_delay); + for (i = 0; i < 4; i++) { + q->matrix_coeff_index_prev[js_pair][i] = q->matrix_coeff_index_now[js_pair][i]; + q->matrix_coeff_index_now[js_pair][i] = q->matrix_coeff_index_next[js_pair][i]; + q->matrix_coeff_index_next[js_pair][i] = get_bits(&q->gb, 2); + } + + /* Decode Sound Unit 2. */ + ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch+1], + out_samples[ch+1], ch+1, JOINT_STEREO); + if (ret != 0) + return ret; + + /* Reconstruct the channel coefficients. */ + reverse_matrixing(out_samples[ch], out_samples[ch+1], + q->matrix_coeff_index_prev[js_pair], + q->matrix_coeff_index_now[js_pair]); + + channel_weighting(out_samples[ch], out_samples[ch+1], q->weighting_delay[js_pair]); + } } else { - /* normal stereo mode or mono */ + /* single channels */ /* Decode the channel sound units. */ for (i = 0; i < avctx->channels; i++) { /* Set the bitstream reader at the start of a channel sound unit. */ - bitstream_init8(&q->bc, - databuf + i * avctx->block_align / avctx->channels, - avctx->block_align / avctx->channels); + init_get_bits(&q->gb, + databuf + i * avctx->block_align / avctx->channels, + avctx->block_align * 8 / avctx->channels); - ret = decode_channel_sound_unit(q, &q->bc, &q->units[i], + ret = decode_channel_sound_unit(q, &q->gb, &q->units[i], out_samples[i], i, q->coding_mode); if (ret != 0) return ret; @@ -731,6 +753,40 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, return 0; } +static int al_decode_frame(AVCodecContext *avctx, const uint8_t *databuf, + int size, float **out_samples) +{ + ATRAC3Context *q = avctx->priv_data; + int ret, i; + + /* Set the bitstream reader at the start of a channel sound unit. */ + init_get_bits(&q->gb, databuf, size * 8); + /* single channels */ + /* Decode the channel sound units. */ + for (i = 0; i < avctx->channels; i++) { + ret = decode_channel_sound_unit(q, &q->gb, &q->units[i], + out_samples[i], i, q->coding_mode); + if (ret != 0) + return ret; + while (i < avctx->channels && get_bits_left(&q->gb) > 6 && show_bits(&q->gb, 6) != 0x28) { + skip_bits(&q->gb, 1); + } + } + + /* Apply the iQMF synthesis filter. */ + for (i = 0; i < avctx->channels; i++) { + float *p1 = out_samples[i]; + float *p2 = p1 + 256; + float *p3 = p2 + 256; + float *p4 = p3 + 256; + ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf); + ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf); + ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf); + } + + return 0; +} + static int atrac3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { @@ -749,10 +805,8 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data, /* get output buffer */ frame->nb_samples = SAMPLES_PER_FRAME; - if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) return ret; - } /* Check if we need to descramble and what buffer to pass on. */ if (q->scrambled_stream) { @@ -764,7 +818,7 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data, ret = decode_frame(avctx, databuf, (float **)frame->extended_data); if (ret) { - av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n"); + av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n"); return ret; } @@ -773,7 +827,29 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data, return avctx->block_align; } -static av_cold void atrac3_init_static_data(AVCodec *codec) +static int atrac3al_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *avpkt) +{ + AVFrame *frame = data; + int ret; + + frame->nb_samples = SAMPLES_PER_FRAME; + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + return ret; + + ret = al_decode_frame(avctx, avpkt->data, avpkt->size, + (float **)frame->extended_data); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n"); + return ret; + } + + *got_frame_ptr = 1; + + return avpkt->size; +} + +static av_cold void atrac3_init_static_data(void) { int i; @@ -793,18 +869,28 @@ static av_cold void atrac3_init_static_data(AVCodec *codec) static av_cold int atrac3_decode_init(AVCodecContext *avctx) { - int i, ret; + static int static_init_done; + int i, js_pair, ret; int version, delay, samples_per_frame, frame_factor; const uint8_t *edata_ptr = avctx->extradata; ATRAC3Context *q = avctx->priv_data; - if (avctx->channels <= 0 || avctx->channels > 2) { + if (avctx->channels < MIN_CHANNELS || avctx->channels > MAX_CHANNELS) { av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n"); return AVERROR(EINVAL); } + if (!static_init_done) + atrac3_init_static_data(); + static_init_done = 1; + /* Take care of the codec-specific extradata. */ - if (avctx->extradata_size == 14) { + if (avctx->codec_id == AV_CODEC_ID_ATRAC3AL) { + version = 4; + samples_per_frame = SAMPLES_PER_FRAME * avctx->channels; + delay = 0x88E; + q->coding_mode = SINGLE; + } else if (avctx->extradata_size == 14) { /* Parse the extradata, WAV format */ av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n", bytestream_get_le16(&edata_ptr)); // Unknown value always 1 @@ -820,7 +906,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) samples_per_frame = SAMPLES_PER_FRAME * avctx->channels; version = 4; delay = 0x88E; - q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO; + q->coding_mode = q->coding_mode ? JOINT_STEREO : SINGLE; q->scrambled_stream = 0; if (avctx->block_align != 96 * avctx->channels * frame_factor && @@ -831,7 +917,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) avctx->channels, frame_factor); return AVERROR_INVALIDDATA; } - } else if (avctx->extradata_size == 10) { + } else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) { /* Parse the extradata, RM format. */ version = bytestream_get_be32(&edata_ptr); samples_per_frame = bytestream_get_be16(&edata_ptr); @@ -840,7 +926,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) q->scrambled_stream = 1; } else { - av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n", + av_log(avctx, AV_LOG_ERROR, "Unknown extradata size %d.\n", avctx->extradata_size); return AVERROR(EINVAL); } @@ -852,8 +938,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - if (samples_per_frame != SAMPLES_PER_FRAME && - samples_per_frame != SAMPLES_PER_FRAME * 2) { + if (samples_per_frame != SAMPLES_PER_FRAME * avctx->channels) { av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n", samples_per_frame); return AVERROR_INVALIDDATA; @@ -865,11 +950,13 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - if (q->coding_mode == STEREO) - av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n"); + if (q->coding_mode == SINGLE) + av_log(avctx, AV_LOG_DEBUG, "Single channels detected.\n"); else if (q->coding_mode == JOINT_STEREO) { - if (avctx->channels != 2) + if (avctx->channels % 2 == 1) { /* Joint stereo channels must be even */ + av_log(avctx, AV_LOG_ERROR, "Invalid joint stereo channel configuration.\n"); return AVERROR_INVALIDDATA; + } av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n"); } else { av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n", @@ -895,24 +982,26 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx) } /* init the joint-stereo decoding data */ - q->weighting_delay[0] = 0; - q->weighting_delay[1] = 7; - q->weighting_delay[2] = 0; - q->weighting_delay[3] = 7; - q->weighting_delay[4] = 0; - q->weighting_delay[5] = 7; - - for (i = 0; i < 4; i++) { - q->matrix_coeff_index_prev[i] = 3; - q->matrix_coeff_index_now[i] = 3; - q->matrix_coeff_index_next[i] = 3; + for (js_pair = 0; js_pair < MAX_JS_PAIRS; js_pair++) { + q->weighting_delay[js_pair][0] = 0; + q->weighting_delay[js_pair][1] = 7; + q->weighting_delay[js_pair][2] = 0; + q->weighting_delay[js_pair][3] = 7; + q->weighting_delay[js_pair][4] = 0; + q->weighting_delay[js_pair][5] = 7; + + for (i = 0; i < 4; i++) { + q->matrix_coeff_index_prev[js_pair][i] = 3; + q->matrix_coeff_index_now[js_pair][i] = 3; + q->matrix_coeff_index_next[js_pair][i] = 3; + } } ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3); - avpriv_float_dsp_init(&q->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT); + q->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); - q->units = av_mallocz(sizeof(*q->units) * avctx->channels); - if (!q->units) { + q->units = av_mallocz_array(avctx->channels, sizeof(*q->units)); + if (!q->units || !q->fdsp) { atrac3_decode_close(avctx); return AVERROR(ENOMEM); } @@ -927,10 +1016,23 @@ AVCodec ff_atrac3_decoder = { .id = AV_CODEC_ID_ATRAC3, .priv_data_size = sizeof(ATRAC3Context), .init = atrac3_decode_init, - .init_static_data = atrac3_init_static_data, .close = atrac3_decode_close, .decode = atrac3_decode_frame, .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, }; + +AVCodec ff_atrac3al_decoder = { + .name = "atrac3al", + .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_ATRAC3AL, + .priv_data_size = sizeof(ATRAC3Context), + .init = atrac3_decode_init, + .close = atrac3_decode_close, + .decode = atrac3al_decode_frame, + .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1, + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, + AV_SAMPLE_FMT_NONE }, +}; |