2007-04-17 22:53:39 +02:00
|
|
|
/*
|
2013-09-02 20:21:49 +02:00
|
|
|
* ATRAC3 compatible decoder
|
2008-05-22 21:42:02 +02:00
|
|
|
* Copyright (c) 2006-2008 Maxim Poliakovski
|
|
|
|
* Copyright (c) 2006-2008 Benjamin Larsson
|
2007-04-17 22:53:39 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2013-09-02 20:21:49 +02:00
|
|
|
* ATRAC3 compatible decoder.
|
2008-05-22 21:42:02 +02:00
|
|
|
* This decoder handles Sony's ATRAC3 data.
|
|
|
|
*
|
2013-09-02 20:21:49 +02:00
|
|
|
* Container formats used to store ATRAC3 data:
|
2008-05-22 21:42:02 +02:00
|
|
|
* RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
|
2007-04-17 22:53:39 +02:00
|
|
|
*
|
|
|
|
* To use this decoder, a calling application must supply the extradata
|
2008-05-22 21:42:02 +02:00
|
|
|
* bytes provided in the containers above.
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
#include "libavutil/attributes.h"
|
2012-05-21 18:58:41 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2012-09-10 22:32:56 +02:00
|
|
|
#include "libavutil/libm.h"
|
2007-04-17 22:53:39 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2010-03-06 15:34:46 +01:00
|
|
|
#include "fft.h"
|
2011-10-14 22:47:03 +02:00
|
|
|
#include "fmtconvert.h"
|
2012-10-17 17:59:40 +02:00
|
|
|
#include "get_bits.h"
|
2012-11-10 13:22:56 +01:00
|
|
|
#include "internal.h"
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2009-09-08 21:25:54 +02:00
|
|
|
#include "atrac.h"
|
2007-04-17 22:53:39 +02:00
|
|
|
#include "atrac3data.h"
|
|
|
|
|
|
|
|
#define JOINT_STEREO 0x12
|
|
|
|
#define STEREO 0x2
|
|
|
|
|
2011-10-14 23:17:46 +02:00
|
|
|
#define SAMPLES_PER_FRAME 1024
|
|
|
|
#define MDCT_SIZE 512
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
typedef struct GainBlock {
|
2013-10-02 02:23:16 +02:00
|
|
|
AtracGainInfo g_block[4];
|
2012-10-17 17:59:40 +02:00
|
|
|
} GainBlock;
|
|
|
|
|
|
|
|
typedef struct TonalComponent {
|
|
|
|
int pos;
|
|
|
|
int num_coefs;
|
|
|
|
float coef[8];
|
|
|
|
} TonalComponent;
|
|
|
|
|
|
|
|
typedef struct ChannelUnit {
|
|
|
|
int bands_coded;
|
|
|
|
int num_components;
|
|
|
|
float prev_frame[SAMPLES_PER_FRAME];
|
|
|
|
int gc_blk_switch;
|
|
|
|
TonalComponent components[64];
|
|
|
|
GainBlock gain_block[2];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2011-10-14 23:17:46 +02:00
|
|
|
DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
|
2012-10-17 17:59:40 +02:00
|
|
|
DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
float delay_buf1[46]; ///<qmf delay buffers
|
|
|
|
float delay_buf2[46];
|
|
|
|
float delay_buf3[46];
|
|
|
|
} ChannelUnit;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
typedef struct ATRAC3Context {
|
|
|
|
GetBitContext gb;
|
2007-04-17 22:53:39 +02:00
|
|
|
//@{
|
|
|
|
/** stream data */
|
2012-10-17 17:59:40 +02:00
|
|
|
int coding_mode;
|
|
|
|
|
|
|
|
ChannelUnit *units;
|
2007-04-17 22:53:39 +02:00
|
|
|
//@}
|
|
|
|
//@{
|
|
|
|
/** joint-stereo related variables */
|
2012-10-17 17:59:40 +02:00
|
|
|
int matrix_coeff_index_prev[4];
|
|
|
|
int matrix_coeff_index_now[4];
|
|
|
|
int matrix_coeff_index_next[4];
|
|
|
|
int weighting_delay[6];
|
2007-04-17 22:53:39 +02:00
|
|
|
//@}
|
|
|
|
//@{
|
|
|
|
/** data buffers */
|
2012-10-17 17:59:40 +02:00
|
|
|
uint8_t *decoded_bytes_buffer;
|
|
|
|
float temp_buf[1070];
|
2007-04-17 22:53:39 +02:00
|
|
|
//@}
|
|
|
|
//@{
|
|
|
|
/** extradata */
|
2012-10-17 17:59:40 +02:00
|
|
|
int scrambled_stream;
|
2007-04-17 22:53:39 +02:00
|
|
|
//@}
|
2010-07-20 17:08:54 +02:00
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
AtracGCContext gainc_ctx;
|
|
|
|
FFTContext mdct_ctx;
|
2012-10-17 17:59:40 +02:00
|
|
|
FmtConvertContext fmt_conv;
|
|
|
|
AVFloatDSPContext fdsp;
|
2007-04-17 22:53:39 +02:00
|
|
|
} ATRAC3Context;
|
|
|
|
|
2011-10-14 23:17:46 +02:00
|
|
|
static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
|
2012-10-19 05:04:03 +02:00
|
|
|
static VLC_TYPE atrac3_vlc_table[4096][2];
|
2012-10-17 17:59:40 +02:00
|
|
|
static VLC spectral_coeff_tab[7];
|
|
|
|
static float gain_tab1[16];
|
|
|
|
static float gain_tab2[31];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2012-10-17 17:59:40 +02:00
|
|
|
* Regular 512 points IMDCT without overlapping, with the exception of the
|
|
|
|
* swapping of odd bands caused by the reverse spectra of the QMF.
|
2007-04-17 22:53:39 +02:00
|
|
|
*
|
|
|
|
* @param odd_band 1 if the band is an odd band
|
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int i;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (odd_band) {
|
|
|
|
/**
|
2012-10-17 17:59:40 +02:00
|
|
|
* Reverse the odd bands before IMDCT, this is an effect of the QMF
|
|
|
|
* transform or it gives better compression to do it this way.
|
|
|
|
* FIXME: It should be possible to handle this in imdct_calc
|
|
|
|
* for that to happen a modification of the prerotation step of
|
|
|
|
* all SIMD code and C code is needed.
|
|
|
|
* Or fix the functions before so they generate a pre reversed spectrum.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 128; i++)
|
|
|
|
FFSWAP(float, input[i], input[255 - i]);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Perform windowing on the output. */
|
2012-10-17 17:59:40 +02:00
|
|
|
q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/*
|
|
|
|
* indata descrambling, only used for data coming from the rm container
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
|
|
|
|
{
|
2007-04-17 22:53:39 +02:00
|
|
|
int i, off;
|
|
|
|
uint32_t c;
|
2012-10-17 17:59:40 +02:00
|
|
|
const uint32_t *buf;
|
|
|
|
uint32_t *output = (uint32_t *)out;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
off = (intptr_t)input & 3;
|
|
|
|
buf = (const uint32_t *)(input - off);
|
2013-03-15 11:31:21 +01:00
|
|
|
if (off)
|
|
|
|
c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
|
|
|
|
else
|
|
|
|
c = av_be2ne32(0x537F6103U);
|
2007-04-17 22:53:39 +02:00
|
|
|
bytes += 3 + off;
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < bytes / 4; i++)
|
|
|
|
output[i] = c ^ buf[i];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (off)
|
2013-03-13 21:17:05 +01:00
|
|
|
avpriv_request_sample(NULL, "Offset of %d", off);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
2012-10-19 04:56:10 +02:00
|
|
|
static av_cold void init_atrac3_window(void)
|
2012-10-17 17:59:40 +02:00
|
|
|
{
|
2012-10-19 05:11:20 +02:00
|
|
|
int i, j;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* generate the mdct window, for details see
|
2007-04-17 22:53:39 +02:00
|
|
|
* http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
|
2012-10-19 05:11:20 +02:00
|
|
|
for (i = 0, j = 255; i < 128; i++, j--) {
|
|
|
|
float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
|
|
|
|
float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
|
|
|
|
float w = 0.5 * (wi * wi + wj * wj);
|
|
|
|
mdct_window[i] = mdct_window[511 - i] = wi / w;
|
|
|
|
mdct_window[j] = mdct_window[511 - j] = wj / w;
|
2012-10-17 17:59:40 +02:00
|
|
|
}
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 14:48:55 +01:00
|
|
|
static av_cold int atrac3_decode_close(AVCodecContext *avctx)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
|
|
|
ATRAC3Context *q = avctx->priv_data;
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
av_free(q->units);
|
2007-04-17 22:53:39 +02:00
|
|
|
av_free(q->decoded_bytes_buffer);
|
2011-10-14 22:47:03 +02:00
|
|
|
|
2010-07-20 17:08:54 +02:00
|
|
|
ff_mdct_end(&q->mdct_ctx);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2012-10-17 17:59:40 +02:00
|
|
|
* Mantissa decoding
|
2007-04-17 22:53:39 +02:00
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @param selector which table the output values are coded with
|
|
|
|
* @param coding_flag constant length coding or variable length coding
|
|
|
|
* @param mantissas mantissa output table
|
|
|
|
* @param num_codes number of values to get
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
|
|
|
|
int coding_flag, int *mantissas,
|
|
|
|
int num_codes)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int i, code, huff_symb;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (selector == 1)
|
2012-10-17 17:59:40 +02:00
|
|
|
num_codes /= 2;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (coding_flag != 0) {
|
2007-04-17 22:53:39 +02:00
|
|
|
/* constant length coding (CLC) */
|
2012-10-17 17:59:40 +02:00
|
|
|
int num_bits = clc_length_tab[selector];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (selector > 1) {
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < num_codes; i++) {
|
|
|
|
if (num_bits)
|
|
|
|
code = get_sbits(gb, num_bits);
|
2007-04-17 22:53:39 +02:00
|
|
|
else
|
|
|
|
code = 0;
|
2012-10-17 17:59:40 +02:00
|
|
|
mantissas[i] = code;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < num_codes; i++) {
|
|
|
|
if (num_bits)
|
|
|
|
code = get_bits(gb, num_bits); // num_bits is always 4 in this case
|
2007-04-17 22:53:39 +02:00
|
|
|
else
|
|
|
|
code = 0;
|
2012-10-17 17:59:40 +02:00
|
|
|
mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
|
|
|
|
mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* variable length coding (VLC) */
|
|
|
|
if (selector != 1) {
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < num_codes; i++) {
|
|
|
|
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)
|
2007-04-17 22:53:39 +02:00
|
|
|
code = -code;
|
2012-10-17 17:59:40 +02:00
|
|
|
mantissas[i] = code;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < num_codes; i++) {
|
|
|
|
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];
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2007-04-17 22:53:39 +02:00
|
|
|
* Restore the quantized band spectrum coefficients
|
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @return subband count, fix for broken specification/files
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int decode_spectrum(GetBitContext *gb, float *output)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
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 = 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] = 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] = get_bits(gb, 6);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i <= num_subbands; i++) {
|
|
|
|
first = subband_tab[i ];
|
|
|
|
last = subband_tab[i + 1];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
subband_size = last - first;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (subband_vlc_index[i] != 0) {
|
|
|
|
/* decode spectral coefficients for this subband */
|
2007-04-17 22:53:39 +02:00
|
|
|
/* TODO: This can be done faster is several blocks share the
|
|
|
|
* same VLC selector (subband_vlc_index) */
|
2012-10-17 17:59:40 +02:00
|
|
|
read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
|
|
|
|
mantissas, subband_size);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* decode the scale factor for this subband */
|
|
|
|
scale_factor = ff_atrac_sf_table[sf_index[i]] *
|
|
|
|
inv_max_quant[subband_vlc_index[i]];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* inverse quantize the coefficients */
|
|
|
|
for (j = 0; first < last; first++, j++)
|
|
|
|
output[first] = mantissas[j] * scale_factor;
|
2007-04-17 22:53:39 +02:00
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
/* this subband was not coded, so zero the entire subband */
|
2012-10-19 05:25:44 +02:00
|
|
|
memset(output + first, 0, subband_size * sizeof(*output));
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* clear the subbands that were not coded */
|
|
|
|
first = subband_tab[i];
|
2012-10-19 05:25:44 +02:00
|
|
|
memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
|
2012-10-17 17:59:40 +02:00
|
|
|
return num_subbands;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2007-04-17 22:53:39 +02:00
|
|
|
* Restore the quantized tonal components
|
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @param components tonal components
|
|
|
|
* @param num_bands number of coded bands
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int decode_tonal_components(GetBitContext *gb,
|
|
|
|
TonalComponent *components, int num_bands)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int i, b, c, m;
|
|
|
|
int nb_components, coding_mode_selector, coding_mode;
|
|
|
|
int band_flags[4], mantissa[8];
|
|
|
|
int component_count = 0;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
nb_components = get_bits(gb, 5);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* no tonal components */
|
2012-10-17 17:59:40 +02:00
|
|
|
if (nb_components == 0)
|
2007-04-17 22:53:39 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
coding_mode_selector = get_bits(gb, 2);
|
2007-04-17 22:53:39 +02:00
|
|
|
if (coding_mode_selector == 2)
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
coding_mode = coding_mode_selector & 1;
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < nb_components; i++) {
|
|
|
|
int coded_values_per_component, quant_step_index;
|
|
|
|
|
|
|
|
for (b = 0; b <= num_bands; b++)
|
|
|
|
band_flags[b] = get_bits1(gb);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
coded_values_per_component = get_bits(gb, 3);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
quant_step_index = get_bits(gb, 3);
|
2007-04-17 22:53:39 +02:00
|
|
|
if (quant_step_index <= 1)
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (coding_mode_selector == 3)
|
|
|
|
coding_mode = get_bits1(gb);
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (b = 0; b < (num_bands + 1) * 4; b++) {
|
|
|
|
int coded_components;
|
|
|
|
|
|
|
|
if (band_flags[b >> 2] == 0)
|
2007-04-17 22:53:39 +02:00
|
|
|
continue;
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
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;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
sf_index = get_bits(gb, 6);
|
2011-12-17 03:18:58 +01:00
|
|
|
if (component_count >= 64)
|
2011-12-17 03:18:58 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
cmp->pos = b * 64 + get_bits(gb, 6);
|
|
|
|
|
|
|
|
max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
|
|
|
|
coded_values = coded_values_per_component + 1;
|
|
|
|
coded_values = FFMIN(max_coded_values, coded_values);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
scale_factor = ff_atrac_sf_table[sf_index] *
|
|
|
|
inv_max_quant[quant_step_index];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
|
|
|
|
mantissa, coded_values);
|
|
|
|
|
|
|
|
cmp->num_coefs = coded_values;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* inverse quant */
|
2012-10-17 17:59:40 +02:00
|
|
|
for (m = 0; m < coded_values; m++)
|
|
|
|
cmp->coef[m] = mantissa[m] * scale_factor;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
component_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-18 21:58:20 +02:00
|
|
|
return component_count;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2007-04-17 22:53:39 +02:00
|
|
|
* Decode gain parameters for the coded bands
|
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @param block the gainblock for the current band
|
|
|
|
* @param num_bands amount of coded bands
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int decode_gain_control(GetBitContext *gb, GainBlock *block,
|
|
|
|
int num_bands)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2013-10-02 02:23:16 +02:00
|
|
|
int i, b;
|
2012-10-17 17:59:40 +02:00
|
|
|
int *level, *loc;
|
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
AtracGainInfo *gain = block->g_block;
|
2012-10-17 17:59:40 +02:00
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
for (b = 0; b <= num_bands; b++) {
|
|
|
|
gain[b].num_points = get_bits(gb, 3);
|
|
|
|
level = gain[b].levcode;
|
|
|
|
loc = gain[b].loccode;
|
2012-10-17 17:59:40 +02:00
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
for (i = 0; i < gain[b].num_points; i++) {
|
|
|
|
level[i] = get_bits(gb, 4);
|
|
|
|
loc [i] = get_bits(gb, 5);
|
|
|
|
if (i && loc[i] <= loc[i-1])
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
/* Clear unused blocks. */
|
|
|
|
for (; b < 4 ; b++)
|
|
|
|
gain[b].num_points = 0;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2007-04-17 22:53:39 +02:00
|
|
|
* Combine the tonal band spectrum and regular band spectrum
|
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @param spectrum output spectrum buffer
|
|
|
|
* @param num_components number of tonal components
|
|
|
|
* @param components tonal components for this band
|
|
|
|
* @return position of the last tonal coefficient
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int add_tonal_components(float *spectrum, int num_components,
|
|
|
|
TonalComponent *components)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int i, j, last_pos = -1;
|
|
|
|
float *input, *output;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < num_components; i++) {
|
|
|
|
last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
|
|
|
|
input = components[i].coef;
|
|
|
|
output = &spectrum[components[i].pos];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (j = 0; j < components[i].num_coefs; j++)
|
2013-01-25 20:44:50 +01:00
|
|
|
output[j] += input[j];
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
2008-05-22 21:16:28 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
return last_pos;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
#define INTERPOLATE(old, new, nsample) \
|
|
|
|
((old) + (nsample) * 0.125 * ((new) - (old)))
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
static void reverse_matrixing(float *su1, float *su2, int *prev_code,
|
|
|
|
int *curr_code)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int i, nsample, band;
|
|
|
|
float mc1_l, mc1_r, mc2_l, mc2_r;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
|
|
|
|
int s1 = prev_code[i];
|
|
|
|
int s2 = curr_code[i];
|
2012-10-18 23:07:04 +02:00
|
|
|
nsample = band;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
if (s1 != s2) {
|
|
|
|
/* Selector value changed, interpolation needed. */
|
2012-10-17 17:59:40 +02:00
|
|
|
mc1_l = matrix_coeffs[s1 * 2 ];
|
|
|
|
mc1_r = matrix_coeffs[s1 * 2 + 1];
|
|
|
|
mc2_l = matrix_coeffs[s2 * 2 ];
|
|
|
|
mc2_r = matrix_coeffs[s2 * 2 + 1];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Interpolation is done over the first eight samples. */
|
2012-10-18 23:07:04 +02:00
|
|
|
for (; nsample < band + 8; nsample++) {
|
|
|
|
float c1 = su1[nsample];
|
|
|
|
float c2 = su2[nsample];
|
|
|
|
c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
|
|
|
|
c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
|
|
|
|
su1[nsample] = c2;
|
|
|
|
su2[nsample] = c1 * 2.0 - c2;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply the matrix without interpolation. */
|
|
|
|
switch (s2) {
|
2012-10-17 17:59:40 +02:00
|
|
|
case 0: /* M/S decoding */
|
2012-10-18 23:07:04 +02:00
|
|
|
for (; nsample < band + 256; nsample++) {
|
|
|
|
float c1 = su1[nsample];
|
|
|
|
float c2 = su2[nsample];
|
|
|
|
su1[nsample] = c2 * 2.0;
|
|
|
|
su2[nsample] = (c1 - c2) * 2.0;
|
2012-10-17 17:59:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2012-10-18 23:07:04 +02:00
|
|
|
for (; nsample < band + 256; nsample++) {
|
|
|
|
float c1 = su1[nsample];
|
|
|
|
float c2 = su2[nsample];
|
|
|
|
su1[nsample] = (c1 + c2) * 2.0;
|
|
|
|
su2[nsample] = c2 * -2.0;
|
2012-10-17 17:59:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2012-10-18 23:07:04 +02:00
|
|
|
for (; nsample < band + 256; nsample++) {
|
|
|
|
float c1 = su1[nsample];
|
|
|
|
float c2 = su2[nsample];
|
|
|
|
su1[nsample] = c1 + c2;
|
|
|
|
su2[nsample] = c1 - c2;
|
2012-10-17 17:59:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-10-23 12:36:16 +02:00
|
|
|
av_assert1(0);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
static void get_channel_weights(int index, int flag, float ch[2])
|
|
|
|
{
|
|
|
|
if (index == 7) {
|
2007-04-17 22:53:39 +02:00
|
|
|
ch[0] = 1.0;
|
|
|
|
ch[1] = 1.0;
|
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
ch[0] = (index & 7) / 7.0;
|
|
|
|
ch[1] = sqrt(2 - ch[0] * ch[0]);
|
|
|
|
if (flag)
|
2007-04-17 22:53:39 +02:00
|
|
|
FFSWAP(float, ch[0], ch[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
static void channel_weighting(float *su1, float *su2, int *p3)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int band, nsample;
|
2007-04-17 22:53:39 +02:00
|
|
|
/* w[x][y] y=0 is left y=1 is right */
|
|
|
|
float w[2][2];
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (p3[1] != 7 || p3[3] != 7) {
|
|
|
|
get_channel_weights(p3[1], p3[0], w[0]);
|
|
|
|
get_channel_weights(p3[3], p3[2], w[1]);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-18 23:07:04 +02:00
|
|
|
for (band = 256; band < 4 * 256; band += 256) {
|
|
|
|
for (nsample = band; nsample < band + 8; nsample++) {
|
|
|
|
su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
|
|
|
|
su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
2012-10-18 23:07:04 +02:00
|
|
|
for(; nsample < band + 256; nsample++) {
|
|
|
|
su1[nsample] *= w[1][0];
|
|
|
|
su2[nsample] *= w[1][1];
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 09:53:08 +01:00
|
|
|
/**
|
2007-04-17 22:53:39 +02:00
|
|
|
* Decode a Sound Unit
|
|
|
|
*
|
2012-10-17 17:59:40 +02:00
|
|
|
* @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)
|
2007-04-17 22:53:39 +02:00
|
|
|
*/
|
2012-10-17 17:59:40 +02:00
|
|
|
static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
|
|
|
|
ChannelUnit *snd, float *output,
|
|
|
|
int channel_num, int coding_mode)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-17 17:59:40 +02:00
|
|
|
int band, ret, num_subbands, last_tonal, num_bands;
|
|
|
|
GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
|
|
|
|
GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (coding_mode == JOINT_STEREO && channel_num == 1) {
|
|
|
|
if (get_bits(gb, 2) != 3) {
|
2007-04-17 22:53:39 +02:00
|
|
|
av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
if (get_bits(gb, 6) != 0x28) {
|
2007-04-17 22:53:39 +02:00
|
|
|
av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* number of coded QMF bands */
|
2012-10-17 17:59:40 +02:00
|
|
|
snd->bands_coded = get_bits(gb, 2);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
ret = decode_gain_control(gb, gain2, snd->bands_coded);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
snd->num_components = decode_tonal_components(gb, snd->components,
|
|
|
|
snd->bands_coded);
|
2013-07-09 01:03:13 +02:00
|
|
|
if (snd->num_components < 0)
|
|
|
|
return snd->num_components;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
num_subbands = decode_spectrum(gb, snd->spectrum);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Merge the decoded spectrum and tonal components. */
|
2012-10-17 17:59:40 +02:00
|
|
|
last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
|
|
|
|
snd->components);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* calculate number of used MLT/QMF bands according to the amount of coded
|
|
|
|
spectral lines */
|
|
|
|
num_bands = (subband_tab[num_subbands] - 1) >> 8;
|
|
|
|
if (last_tonal >= 0)
|
|
|
|
num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Reconstruct time domain samples. */
|
2012-10-17 17:59:40 +02:00
|
|
|
for (band = 0; band < 4; band++) {
|
2007-04-17 22:53:39 +02:00
|
|
|
/* Perform the IMDCT step without overlapping. */
|
2012-10-17 17:59:40 +02:00
|
|
|
if (band <= num_bands)
|
|
|
|
imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
|
|
|
|
else
|
2012-10-19 05:25:44 +02:00
|
|
|
memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* gain compensation and overlapping */
|
2013-10-02 02:23:16 +02:00
|
|
|
ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
|
|
|
|
&snd->prev_frame[band * 256],
|
|
|
|
&gain1->g_block[band], &gain2->g_block[band],
|
|
|
|
256, &output[band * 256]);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap the gain control buffers for the next frame. */
|
2012-10-17 17:59:40 +02:00
|
|
|
snd->gc_blk_switch ^= 1;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-18 23:13:22 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
|
2012-10-17 17:59:40 +02:00
|
|
|
float **out_samples)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-10-18 23:13:22 +02:00
|
|
|
ATRAC3Context *q = avctx->priv_data;
|
2012-10-17 17:59:40 +02:00
|
|
|
int ret, i;
|
2008-09-30 00:22:46 +02:00
|
|
|
uint8_t *ptr1;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (q->coding_mode == JOINT_STEREO) {
|
2007-04-17 22:53:39 +02:00
|
|
|
/* channel coupling mode */
|
|
|
|
/* decode Sound Unit 1 */
|
2012-10-19 04:45:48 +02:00
|
|
|
init_get_bits(&q->gb, databuf, avctx->block_align * 8);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
|
|
|
|
JOINT_STEREO);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Framedata of the su2 in the joint-stereo mode is encoded in
|
|
|
|
* reverse byte order so we need to swap it first. */
|
2008-09-30 00:22:46 +02:00
|
|
|
if (databuf == q->decoded_bytes_buffer) {
|
2012-10-19 04:45:48 +02:00
|
|
|
uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1;
|
2012-10-17 17:59:40 +02:00
|
|
|
ptr1 = q->decoded_bytes_buffer;
|
2012-10-19 04:45:48 +02:00
|
|
|
for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--)
|
2012-10-17 17:59:40 +02:00
|
|
|
FFSWAP(uint8_t, *ptr1, *ptr2);
|
2008-09-30 00:22:46 +02:00
|
|
|
} else {
|
2012-10-19 04:45:48 +02:00
|
|
|
const uint8_t *ptr2 = databuf + avctx->block_align - 1;
|
|
|
|
for (i = 0; i < avctx->block_align; i++)
|
2008-09-30 00:22:46 +02:00
|
|
|
q->decoded_bytes_buffer[i] = *ptr2--;
|
|
|
|
}
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Skip the sync codes (0xF8). */
|
2008-09-30 00:22:46 +02:00
|
|
|
ptr1 = q->decoded_bytes_buffer;
|
2007-04-17 22:53:39 +02:00
|
|
|
for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
|
2012-10-19 04:45:48 +02:00
|
|
|
if (i >= avctx->block_align)
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set the bitstream reader at the start of the second Sound Unit*/
|
2013-01-26 04:27:23 +01:00
|
|
|
init_get_bits8(&q->gb, ptr1, q->decoded_bytes_buffer + avctx->block_align - ptr1);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Fill the Weighting coeffs delay buffer */
|
2012-10-19 05:25:44 +02:00
|
|
|
memmove(q->weighting_delay, &q->weighting_delay[2],
|
|
|
|
4 * sizeof(*q->weighting_delay));
|
2007-08-09 02:13:31 +02:00
|
|
|
q->weighting_delay[4] = get_bits1(&q->gb);
|
2012-10-17 17:59:40 +02:00
|
|
|
q->weighting_delay[5] = get_bits(&q->gb, 3);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
|
2012-10-17 17:59:40 +02:00
|
|
|
q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
|
|
|
|
q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode Sound Unit 2. */
|
2012-10-17 17:59:40 +02:00
|
|
|
ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
|
|
|
|
out_samples[1], 1, JOINT_STEREO);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Reconstruct the channel coefficients. */
|
2012-10-17 17:59:40 +02:00
|
|
|
reverse_matrixing(out_samples[0], out_samples[1],
|
|
|
|
q->matrix_coeff_index_prev,
|
|
|
|
q->matrix_coeff_index_now);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
|
2007-04-17 22:53:39 +02:00
|
|
|
} else {
|
|
|
|
/* normal stereo mode or mono */
|
|
|
|
/* Decode the channel sound units. */
|
2012-10-18 23:13:22 +02:00
|
|
|
for (i = 0; i < avctx->channels; i++) {
|
2007-04-17 22:53:39 +02:00
|
|
|
/* Set the bitstream reader at the start of a channel sound unit. */
|
2011-12-07 13:48:58 +01:00
|
|
|
init_get_bits(&q->gb,
|
2012-10-19 04:45:48 +02:00
|
|
|
databuf + i * avctx->block_align / avctx->channels,
|
|
|
|
avctx->block_align * 8 / avctx->channels);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
|
|
|
|
out_samples[i], i, q->coding_mode);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply the iQMF synthesis filter. */
|
2012-10-18 23:13:22 +02:00
|
|
|
for (i = 0; i < avctx->channels; i++) {
|
2012-10-17 17:59:40 +02:00
|
|
|
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);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-06 18:17:45 +02:00
|
|
|
static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame_ptr, AVPacket *avpkt)
|
|
|
|
{
|
2012-12-23 23:24:53 +01:00
|
|
|
AVFrame *frame = data;
|
2009-04-07 17:59:50 +02:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2007-04-17 22:53:39 +02:00
|
|
|
ATRAC3Context *q = avctx->priv_data;
|
2012-10-17 17:59:40 +02:00
|
|
|
int ret;
|
|
|
|
const uint8_t *databuf;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2010-08-09 09:17:01 +02:00
|
|
|
if (buf_size < avctx->block_align) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Frame too small (%d bytes). Truncated file?\n", buf_size);
|
2011-10-14 23:10:33 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-08-09 09:17:01 +02:00
|
|
|
}
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2011-09-06 18:17:45 +02:00
|
|
|
/* get output buffer */
|
2012-12-23 23:24:53 +01:00
|
|
|
frame->nb_samples = SAMPLES_PER_FRAME;
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 08:41:53 +01:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-10-17 17:59:40 +02:00
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* Check if we need to descramble and what buffer to pass on. */
|
|
|
|
if (q->scrambled_stream) {
|
|
|
|
decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
|
|
|
|
databuf = q->decoded_bytes_buffer;
|
|
|
|
} else {
|
|
|
|
databuf = buf;
|
|
|
|
}
|
|
|
|
|
2012-12-23 23:24:53 +01:00
|
|
|
ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
|
2012-10-17 17:59:40 +02:00
|
|
|
if (ret) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
|
|
|
|
return ret;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-12-23 23:24:53 +01:00
|
|
|
*got_frame_ptr = 1;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return avctx->block_align;
|
|
|
|
}
|
|
|
|
|
2013-05-05 11:34:29 +02:00
|
|
|
static av_cold void atrac3_init_static_data(void)
|
2012-10-19 05:04:03 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
init_atrac3_window();
|
|
|
|
ff_atrac_generate_tables();
|
|
|
|
|
|
|
|
/* Initialize the VLC tables. */
|
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
|
|
|
|
spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
|
|
|
|
atrac3_vlc_offs[i ];
|
|
|
|
init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
|
|
|
|
huff_bits[i], 1, 1,
|
|
|
|
huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate gain tables */
|
|
|
|
for (i = 0; i < 16; i++)
|
2012-10-23 12:51:13 +02:00
|
|
|
gain_tab1[i] = exp2f (4 - i);
|
2012-10-19 05:04:03 +02:00
|
|
|
|
|
|
|
for (i = -15; i < 16; i++)
|
2012-10-23 12:51:13 +02:00
|
|
|
gain_tab2[i + 15] = exp2f (i * -0.125);
|
2012-10-19 05:04:03 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 14:48:55 +01:00
|
|
|
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
|
2007-04-17 22:53:39 +02:00
|
|
|
{
|
2012-11-27 20:48:17 +01:00
|
|
|
static int static_init_done;
|
2011-10-28 18:53:11 +02:00
|
|
|
int i, ret;
|
2012-10-19 04:53:13 +02:00
|
|
|
int version, delay, samples_per_frame, frame_factor;
|
2008-02-01 04:54:08 +01:00
|
|
|
const uint8_t *edata_ptr = avctx->extradata;
|
2007-04-17 22:53:39 +02:00
|
|
|
ATRAC3Context *q = avctx->priv_data;
|
|
|
|
|
2012-10-18 23:13:22 +02:00
|
|
|
if (avctx->channels <= 0 || avctx->channels > 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-11-27 20:48:17 +01:00
|
|
|
if (!static_init_done)
|
|
|
|
atrac3_init_static_data();
|
|
|
|
static_init_done = 1;
|
|
|
|
|
2007-04-17 22:53:39 +02:00
|
|
|
/* Take care of the codec-specific extradata. */
|
|
|
|
if (avctx->extradata_size == 14) {
|
|
|
|
/* Parse the extradata, WAV format */
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
|
|
|
|
bytestream_get_le16(&edata_ptr)); // Unknown value always 1
|
2012-10-19 04:48:45 +02:00
|
|
|
edata_ptr += 4; // samples per channel
|
2012-10-17 17:59:40 +02:00
|
|
|
q->coding_mode = bytestream_get_le16(&edata_ptr);
|
|
|
|
av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
|
|
|
|
bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
|
2012-10-19 04:53:13 +02:00
|
|
|
frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
|
|
|
|
bytestream_get_le16(&edata_ptr)); // Unknown always 0
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* setup */
|
2012-10-19 04:50:40 +02:00
|
|
|
samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
|
2012-10-18 23:14:59 +02:00
|
|
|
version = 4;
|
2012-10-19 04:42:21 +02:00
|
|
|
delay = 0x88E;
|
2012-10-17 17:59:40 +02:00
|
|
|
q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;
|
|
|
|
q->scrambled_stream = 0;
|
|
|
|
|
2012-10-19 04:53:13 +02:00
|
|
|
if (avctx->block_align != 96 * avctx->channels * frame_factor &&
|
|
|
|
avctx->block_align != 152 * avctx->channels * frame_factor &&
|
|
|
|
avctx->block_align != 192 * avctx->channels * frame_factor) {
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
|
2012-10-19 04:45:48 +02:00
|
|
|
"configuration %d/%d/%d\n", avctx->block_align,
|
2012-10-19 04:53:13 +02:00
|
|
|
avctx->channels, frame_factor);
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
2013-02-10 14:41:55 +01:00
|
|
|
} else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) {
|
2007-04-17 22:53:39 +02:00
|
|
|
/* Parse the extradata, RM format. */
|
2012-10-18 23:14:59 +02:00
|
|
|
version = bytestream_get_be32(&edata_ptr);
|
2012-10-19 04:50:40 +02:00
|
|
|
samples_per_frame = bytestream_get_be16(&edata_ptr);
|
2012-10-19 04:42:21 +02:00
|
|
|
delay = bytestream_get_be16(&edata_ptr);
|
2012-10-17 17:59:40 +02:00
|
|
|
q->coding_mode = bytestream_get_be16(&edata_ptr);
|
|
|
|
q->scrambled_stream = 1;
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
|
|
|
|
avctx->extradata_size);
|
2012-10-22 16:44:55 +02:00
|
|
|
return AVERROR(EINVAL);
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
/* Check the extradata */
|
|
|
|
|
2012-10-18 23:14:59 +02:00
|
|
|
if (version != 4) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 04:50:40 +02:00
|
|
|
if (samples_per_frame != SAMPLES_PER_FRAME &&
|
|
|
|
samples_per_frame != SAMPLES_PER_FRAME * 2) {
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
|
2012-10-19 04:50:40 +02:00
|
|
|
samples_per_frame);
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 04:42:21 +02:00
|
|
|
if (delay != 0x88E) {
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
|
2012-10-19 04:42:21 +02:00
|
|
|
delay);
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (q->coding_mode == STEREO)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
|
2013-07-09 04:44:26 +02:00
|
|
|
else if (q->coding_mode == JOINT_STEREO) {
|
2013-07-10 12:24:58 +02:00
|
|
|
if (avctx->channels != 2) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid coding mode\n");
|
2013-07-09 04:44:26 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-07-10 12:24:58 +02:00
|
|
|
}
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
|
2013-07-09 04:44:26 +02:00
|
|
|
} else {
|
2012-10-17 17:59:40 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
|
|
|
|
q->coding_mode);
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-17 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
if (avctx->block_align >= UINT_MAX / 2)
|
2011-10-28 19:00:53 +02:00
|
|
|
return AVERROR(EINVAL);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-19 05:51:37 +02:00
|
|
|
q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
|
2012-10-17 17:59:40 +02:00
|
|
|
FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (q->decoded_bytes_buffer == NULL)
|
2007-07-27 18:31:54 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-08-26 18:48:07 +02:00
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
|
2011-10-28 22:36:47 +02:00
|
|
|
|
2012-10-19 04:56:10 +02:00
|
|
|
/* initialize the MDCT transform */
|
|
|
|
if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
|
2011-10-28 18:53:11 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
|
|
|
|
av_freep(&q->decoded_bytes_buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
for (i = 0; i < 4; i++) {
|
2007-04-17 22:53:39 +02:00
|
|
|
q->matrix_coeff_index_prev[i] = 3;
|
2012-10-17 17:59:40 +02:00
|
|
|
q->matrix_coeff_index_now[i] = 3;
|
2007-04-17 22:53:39 +02:00
|
|
|
q->matrix_coeff_index_next[i] = 3;
|
|
|
|
}
|
|
|
|
|
2013-10-02 02:23:16 +02:00
|
|
|
ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
|
2012-05-21 18:58:41 +02:00
|
|
|
avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
|
2011-10-14 22:47:03 +02:00
|
|
|
ff_fmt_convert_init(&q->fmt_conv, avctx);
|
2007-04-17 22:53:39 +02:00
|
|
|
|
2012-10-19 05:25:44 +02:00
|
|
|
q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
|
2012-10-17 17:59:40 +02:00
|
|
|
if (!q->units) {
|
2011-10-28 18:53:11 +02:00
|
|
|
atrac3_decode_close(avctx);
|
2007-07-30 15:58:17 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2007-04-17 22:53:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-17 17:59:40 +02:00
|
|
|
AVCodec ff_atrac3_decoder = {
|
|
|
|
.name = "atrac3",
|
2013-09-02 20:21:49 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
|
2012-10-17 17:59:40 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = AV_CODEC_ID_ATRAC3,
|
|
|
|
.priv_data_size = sizeof(ATRAC3Context),
|
|
|
|
.init = atrac3_decode_init,
|
|
|
|
.close = atrac3_decode_close,
|
|
|
|
.decode = atrac3_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
|
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
2007-04-17 22:53:39 +02:00
|
|
|
};
|