39 #include "libavutil/attributes.h"
52 #define JOINT_STEREO 0x12
55 #define SAMPLES_PER_FRAME 1024
138 for (i = 0; i < 128; i++)
139 FFSWAP(
float, input[i], input[255 - i]);
156 uint32_t *output = (uint32_t *)out;
158 off = (intptr_t)input & 3;
159 buf = (
const uint32_t *)(input - off);
161 c =
av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
165 for (i = 0; i < bytes / 4; i++)
166 output[i] = c ^ buf[i];
180 for (i = 0, j = 255; i < 128; i++, j--) {
181 float wi = sin(((i + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
182 float wj = sin(((j + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
183 float w = 0.5 * (wi * wi + wj * wj);
210 int coding_flag,
int *mantissas,
213 int i, code, huff_symb;
218 if (coding_flag != 0) {
223 for (i = 0; i < num_codes; i++) {
231 for (i = 0; i < num_codes; i++) {
243 for (i = 0; i < num_codes; i++) {
244 huff_symb =
get_vlc2(gb, spectral_coeff_tab[selector-1].
table,
245 spectral_coeff_tab[selector-1].
bits, 3);
247 code = huff_symb >> 1;
253 for (i = 0; i < num_codes; i++) {
254 huff_symb =
get_vlc2(gb, spectral_coeff_tab[selector - 1].
table,
255 spectral_coeff_tab[selector - 1].
bits, 3);
270 int num_subbands, coding_mode, i, j, first, last, subband_size;
271 int subband_vlc_index[32], sf_index[32];
279 for (i = 0; i <= num_subbands; i++)
280 subband_vlc_index[i] =
get_bits(gb, 3);
283 for (i = 0; i <= num_subbands; i++) {
284 if (subband_vlc_index[i] != 0)
288 for (i = 0; i <= num_subbands; i++) {
292 subband_size = last - first;
294 if (subband_vlc_index[i] != 0) {
299 mantissas, subband_size);
306 for (j = 0; first < last; first++, j++)
307 output[first] = mantissas[j] * scale_factor;
310 memset(output + first, 0, subband_size *
sizeof(*output));
330 int nb_components, coding_mode_selector, coding_mode;
331 int band_flags[4], mantissa[8];
332 int component_count = 0;
337 if (nb_components == 0)
340 coding_mode_selector =
get_bits(gb, 2);
341 if (coding_mode_selector == 2)
344 coding_mode = coding_mode_selector & 1;
346 for (i = 0; i < nb_components; i++) {
347 int coded_values_per_component, quant_step_index;
349 for (b = 0; b <= num_bands; b++)
352 coded_values_per_component =
get_bits(gb, 3);
355 if (quant_step_index <= 1)
358 if (coding_mode_selector == 3)
361 for (b = 0; b < (num_bands + 1) * 4; b++) {
362 int coded_components;
364 if (band_flags[b >> 2] == 0)
369 for (c = 0; c < coded_components; c++) {
371 int sf_index, coded_values, max_coded_values;
375 if (component_count >= 64)
381 coded_values = coded_values_per_component + 1;
382 coded_values =
FFMIN(max_coded_values, coded_values);
388 mantissa, coded_values);
393 for (m = 0; m < coded_values; m++)
394 cmp->
coef[m] = mantissa[m] * scale_factor;
401 return component_count;
418 for (b = 0; b <= num_bands; b++) {
426 if (j && loc[j] <= loc[j - 1])
433 gain[b].num_points = 0;
449 int i, j, last_pos = -1;
450 float *input, *output;
452 for (i = 0; i < num_components; i++) {
453 last_pos =
FFMAX(components[i].pos + components[i].num_coefs, last_pos);
454 input = components[i].
coef;
455 output = &spectrum[components[i].
pos];
457 for (j = 0; j < components[i].num_coefs; j++)
458 output[j] += input[j];
464 #define INTERPOLATE(old, new, nsample) \
465 ((old) + (nsample) * 0.125 * ((new) - (old)))
470 int i, nsample,
band;
471 float mc1_l, mc1_r, mc2_l, mc2_r;
473 for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
474 int s1 = prev_code[i];
475 int s2 = curr_code[i];
486 for (; nsample < band + 8; nsample++) {
487 float c1 = su1[nsample];
488 float c2 = su2[nsample];
489 c2 = c1 *
INTERPOLATE(mc1_l, mc2_l, nsample - band) +
492 su2[nsample] = c1 * 2.0 -
c2;
499 for (; nsample < band + 256; nsample++) {
500 float c1 = su1[nsample];
501 float c2 = su2[nsample];
502 su1[nsample] = c2 * 2.0;
503 su2[nsample] = (c1 -
c2) * 2.0;
507 for (; nsample < band + 256; nsample++) {
508 float c1 = su1[nsample];
509 float c2 = su2[nsample];
510 su1[nsample] = (c1 +
c2) * 2.0;
511 su2[nsample] = c2 * -2.0;
516 for (; nsample < band + 256; nsample++) {
517 float c1 = su1[nsample];
518 float c2 = su2[nsample];
519 su1[nsample] = c1 +
c2;
520 su2[nsample] = c1 -
c2;
535 ch[0] = (index & 7) / 7.0;
536 ch[1] = sqrt(2 - ch[0] * ch[0]);
538 FFSWAP(
float, ch[0], ch[1]);
548 if (p3[1] != 7 || p3[3] != 7) {
552 for (band = 256; band < 4 * 256; band += 256) {
553 for (nsample = band; nsample < band + 8; nsample++) {
554 su1[nsample] *=
INTERPOLATE(w[0][0], w[0][1], nsample - band);
555 su2[nsample] *=
INTERPOLATE(w[1][0], w[1][1], nsample - band);
557 for(; nsample < band + 256; nsample++) {
558 su1[nsample] *= w[1][0];
559 su2[nsample] *= w[1][1];
575 int channel_num,
int coding_mode)
577 int band,
ret, num_subbands, last_tonal, num_bands;
616 num_bands =
FFMAX((last_tonal + 256) >> 8, num_bands);
620 for (band = 0; band < 4; band++) {
622 if (band <= num_bands)
631 256, &output[band * 256]);
662 for (i = 0; i < avctx->
block_align / 2; i++, ptr1++, ptr2--)
672 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
687 for (i = 0; i < 4; i++) {
708 for (i = 0; i < avctx->
channels; i++) {
722 for (i = 0; i < avctx->
channels; i++) {
723 float *p1 = out_samples[i];
724 float *p2 = p1 + 256;
725 float *p3 = p2 + 256;
726 float *p4 = p3 + 256;
736 int *got_frame_ptr,
AVPacket *avpkt)
740 int buf_size = avpkt->
size;
745 if (buf_size < avctx->block_align) {
747 "Frame too small (%d bytes). Truncated file?\n", buf_size);
783 for (i = 0; i < 7; i++) {
795 static int static_init_done;
797 int version, delay, samples_per_frame, frame_factor;
806 if (!static_init_done)
808 static_init_done = 1;
814 bytestream_get_le16(&edata_ptr));
818 bytestream_get_le16(&edata_ptr));
819 frame_factor = bytestream_get_le16(&edata_ptr);
821 bytestream_get_le16(&edata_ptr));
840 version = bytestream_get_be32(&edata_ptr);
841 samples_per_frame = bytestream_get_be16(&edata_ptr);
842 delay = bytestream_get_be16(&edata_ptr);
866 if (delay != 0x88E) {
911 for (i = 0; i < 4; i++) {
static const uint16_t atrac3_vlc_offs[9]
This structure describes decoded (raw) audio or video data.
static void reverse_matrixing(float *su1, float *su2, int *prev_code, int *curr_code)
int matrix_coeff_index_next[4]
uint8_t * decoded_bytes_buffer
data buffers
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int add_tonal_components(float *spectrum, int num_components, TonalComponent *components)
Combine the tonal band spectrum and regular band spectrum.
static const uint8_t clc_length_tab[8]
static av_cold int init(AVCodecContext *avctx)
static struct endianess table[]
static const uint8_t *const huff_codes[7]
#define SAMPLES_PER_FRAME
TonalComponent components[64]
void ff_atrac_iqmf(float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
Quadrature mirror synthesis filter.
uint8_t ** extended_data
pointers to the data planes/channels.
#define DECLARE_ALIGNED(n, t, v)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands caused ...
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
static int get_sbits(GetBitContext *s, int n)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int lev_code[7]
level at corresponding control point
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
float ff_atrac_sf_table[64]
static const uint8_t *const huff_bits[7]
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
enum AVSampleFormat sample_fmt
audio sample format
static void channel_weighting(float *su1, float *su2, int *p3)
static float mdct_window[MDCT_SIZE]
const char * name
Name of the codec implementation.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static const int8_t mantissa_clc_tab[4]
static const float inv_max_quant[8]
bitstream reader API header.
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define FFSWAP(type, a, b)
int loc_code[7]
location of gain control points
static int atrac3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static int decode_spectrum(GetBitContext *gb, float *output)
Restore the quantized band spectrum coefficients.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Gain compensation context structure.
Libavcodec external API header.
av_cold void ff_atrac_init_gain_compensation(AtracGCContext *gctx, int id2exp_offset, int loc_scale)
Initialize gain compensation context.
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
float spectrum[SAMPLES_PER_FRAME]
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static const uint16_t subband_tab[33]
int matrix_coeff_index_now[4]
static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, float **out_samples)
static void get_channel_weights(int index, int flag, float ch[2])
static const int8_t mantissa_vlc_tab[18]
float prev_frame[SAMPLES_PER_FRAME]
float imdct_buf[SAMPLES_PER_FRAME]
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
#define INIT_VLC_USE_NEW_STATIC
static VLC_TYPE atrac3_vlc_table[4096][2]
AVSampleFormat
Audio Sample Formats.
static int decode_gain_control(GetBitContext *gb, GainBlock *block, int num_bands)
Decode gain parameters for the coded bands.
Gain control parameters for one subband.
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, int coding_flag, int *mantissas, int num_codes)
Mantissa decoding.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
main external API structure.
static void close(AVCodecParserContext *s)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
int coding_mode
stream data
Replacements for frequently missing libm functions.
static unsigned int get_bits1(GetBitContext *s)
static av_cold int atrac3_decode_close(AVCodecContext *avctx)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static av_cold void init_imdct_window(void)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
int num_points
number of gain control points
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
common internal api header.
int scrambled_stream
extradata
static VLC spectral_coeff_tab[7]
float delay_buf1[46]
qmf delay buffers
AVCodec ff_atrac3_decoder
#define AVERROR_INVALIDDATA
static const float matrix_coeffs[8]
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int channels
number of audio channels
VLC_TYPE(* table)[2]
code, bits
#define INTERPOLATE(old, new, nsample)
void ff_atrac_gain_compensation(AtracGCContext *gctx, float *in, float *prev, AtracGainInfo *gc_now, AtracGainInfo *gc_next, int num_samples, float *out)
Apply gain compensation and perform the MDCT overlapping part.
av_cold void ff_atrac_generate_tables(void)
Generate common tables.
static enum AVSampleFormat sample_fmts[]
static av_cold void atrac3_init_static_data(void)
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
int matrix_coeff_index_prev[4]
joint-stereo related variables
static const uint8_t huff_tab_sizes[7]
ATRAC3 AKA RealAudio 8 compatible decoder data.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
static int decode_tonal_components(GetBitContext *gb, TonalComponent *components, int num_bands)
Restore the quantized tonal components.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, ChannelUnit *snd, float *output, int channel_num, int coding_mode)
Decode a Sound Unit.
FmtConvertContext fmt_conv