35#define ATRAC9_SF_VLC_BITS 8
36#define ATRAC9_COEFF_VLC_BITS 9
117 int values, sign,
base;
129 grad_range[1] =
get_bits(gb, 6) + 1;
135 if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
138 if (
b->grad_boundary >
b->q_unit_cnt)
141 values = grad_value[1] - grad_value[0];
142 sign = 1 - 2*(values < 0);
143 base = grad_value[0] + sign;
145 curve =
s->alloc_curve[grad_range[1] - grad_range[0] - 1];
147 for (
int i = 0;
i <=
b->q_unit_cnt;
i++)
148 b->gradient[
i] = grad_value[
i >= grad_range[0]];
150 for (
int i = grad_range[0];
i < grad_range[1];
i++)
151 b->gradient[
i] =
base + sign*((
int)(
scale*curve[
i - grad_range[0]]));
159 memset(
c->precision_mask, 0,
sizeof(
c->precision_mask));
160 for (
int i = 1;
i <
b->q_unit_cnt;
i++) {
161 const int delta =
FFABS(
c->scalefactors[
i] -
c->scalefactors[
i - 1]) - 1;
163 const int neg =
c->scalefactors[
i - 1] >
c->scalefactors[
i];
169 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
170 c->precision_coarse[
i] =
c->scalefactors[
i];
171 c->precision_coarse[
i] +=
c->precision_mask[
i] -
b->gradient[
i];
172 if (
c->precision_coarse[
i] < 0)
174 switch (
b->grad_mode) {
176 c->precision_coarse[
i] >>= 1;
179 c->precision_coarse[
i] = (3 *
c->precision_coarse[
i]) >> 3;
182 c->precision_coarse[
i] >>= 2;
187 for (
int i = 0;
i <
b->q_unit_cnt;
i++)
188 c->precision_coarse[
i] =
c->scalefactors[
i] -
b->gradient[
i];
192 for (
int i = 0;
i <
b->q_unit_cnt;
i++)
193 c->precision_coarse[
i] =
FFMAX(
c->precision_coarse[
i], 1);
195 for (
int i = 0;
i <
b->grad_boundary;
i++)
196 c->precision_coarse[
i]++;
198 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
199 c->precision_fine[
i] = 0;
200 if (
c->precision_coarse[
i] > 15) {
201 c->precision_fine[
i] =
FFMIN(
c->precision_coarse[
i], 30) - 15;
202 c->precision_coarse[
i] = 15;
212 if (
b->has_band_ext) {
213 if (
b->q_unit_cnt < 13 ||
b->q_unit_cnt > 20)
217 b->channel[1].band_ext =
get_bits(gb, 2);
218 b->channel[1].band_ext = ext_band > 2 ?
b->channel[1].band_ext : 4;
225 if (!
b->has_band_ext_data)
228 if (!
b->has_band_ext) {
234 b->channel[0].band_ext =
get_bits(gb, 2);
235 b->channel[0].band_ext = ext_band > 2 ?
b->channel[0].band_ext : 4;
238 for (
int i = 0;
i <= stereo;
i++) {
241 for (
int j = 0; j < count; j++) {
250 for (
int i = 0;
i <= stereo;
i++) {
253 for (
int j = 0; j < count; j++) {
264 int channel_idx,
int first_in_pkt)
266 static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
267 const int mode = mode_map[channel_idx][
get_bits(gb, 2)];
269 memset(
c->scalefactors, 0,
sizeof(
c->scalefactors));
271 if (first_in_pkt && (
mode == 4 || ((
mode == 3) && !channel_idx))) {
285 for (
int i = 1;
i <
b->band_ext_q_unit;
i++) {
291 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
292 c->scalefactors[
i] +=
base - sf_weights[
i];
299 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
305 const int *baseline =
mode == 4 ?
c->scalefactors_prev :
306 channel_idx ?
b->channel[0].scalefactors :
307 c->scalefactors_prev;
308 const int baseline_len =
mode == 4 ?
b->q_unit_cnt_prev :
309 channel_idx ?
b->band_ext_q_unit :
313 const int unit_cnt =
FFMIN(
b->band_ext_q_unit, baseline_len);
316 for (
int i = 0;
i < unit_cnt;
i++) {
318 c->scalefactors[
i] = baseline[
i] + dist;
321 for (
int i = unit_cnt;
i <
b->band_ext_q_unit;
i++)
327 const int *baseline = channel_idx ?
b->channel[0].scalefactors :
328 c->scalefactors_prev;
329 const int baseline_len = channel_idx ?
b->band_ext_q_unit :
334 const int unit_cnt =
FFMIN(
b->band_ext_q_unit, baseline_len);
339 for (
int i = 1;
i < unit_cnt;
i++) {
345 for (
int i = 0;
i < unit_cnt;
i++)
346 c->scalefactors[
i] +=
base + baseline[
i];
348 for (
int i = unit_cnt;
i <
b->band_ext_q_unit;
i++)
354 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
355 if (
c->scalefactors[
i] < 0 ||
c->scalefactors[
i] > 31)
358 memcpy(
c->scalefactors_prev,
c->scalefactors,
sizeof(
c->scalefactors));
367 const int last_sf =
c->scalefactors[
c->q_unit_cnt];
369 memset(
c->codebookset, 0,
sizeof(
c->codebookset));
371 if (
c->q_unit_cnt <= 1)
373 if (
s->samplerate_idx > 7)
376 c->scalefactors[
c->q_unit_cnt] =
c->scalefactors[
c->q_unit_cnt - 1];
378 if (
c->q_unit_cnt > 12) {
379 for (
int i = 0;
i < 12;
i++)
380 avg +=
c->scalefactors[
i];
384 for (
int i = 8;
i <
c->q_unit_cnt;
i++) {
385 const int prev =
c->scalefactors[
i - 1];
386 const int cur =
c->scalefactors[
i ];
387 const int next =
c->scalefactors[
i + 1];
389 if ((cur -
min >= 3 || 2*cur - prev - next >= 3))
390 c->codebookset[
i] = 1;
394 for (
int i = 12;
i <
c->q_unit_cnt;
i++) {
395 const int cur =
c->scalefactors[
i];
397 const int min =
FFMIN(
c->scalefactors[
i + 1],
c->scalefactors[
i - 1]);
398 if (
c->codebookset[
i])
401 c->codebookset[
i] = (((cur -
min) >= 2) && (cur >= (
avg - cnd)));
404 c->scalefactors[
c->q_unit_cnt] = last_sf;
410 const int max_prec =
s->samplerate_idx > 7 ? 1 : 7;
412 memset(
c->q_coeffs_coarse, 0,
sizeof(
c->q_coeffs_coarse));
414 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
417 const int prec =
c->precision_coarse[
i] + 1;
419 if (prec <= max_prec) {
420 const int cb =
c->codebookset[
i];
426 for (
int j = 0; j <
groups; j++) {
429 for (
int k = 0; k < huff->
value_cnt; k++) {
437 for (
int j = 0; j <
bands; j++)
446 memset(
c->q_coeffs_fine, 0,
sizeof(
c->q_coeffs_fine));
448 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
451 const int len =
c->precision_fine[
i] + 1;
453 if (
c->precision_fine[
i] <= 0)
456 for (
int j = start; j < end; j++)
464 memset(
c->coeffs, 0,
sizeof(
c->coeffs));
466 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
473 for (
int j = start; j < end; j++) {
474 const float vc =
c->q_coeffs_coarse[j] * coarse_c;
475 const float vf =
c->q_coeffs_fine[j] * fine_c;
476 c->coeffs[j] = vc +
vf;
484 float *
src =
b->channel[
b->cpe_base_channel].coeffs;
485 float *
dst =
b->channel[!
b->cpe_base_channel].coeffs;
490 if (
b->q_unit_cnt <=
b->stereo_q_unit)
493 for (
int i =
b->stereo_q_unit;
i < b->q_unit_cnt;
i++) {
494 const int sign =
b->is_signs[
i];
497 for (
int j = start; j < end; j++)
505 for (
int i = 0;
i <= stereo;
i++) {
506 float *coeffs =
b->channel[
i].coeffs;
507 for (
int j = 0; j <
b->q_unit_cnt; j++) {
510 const int scalefactor =
b->channel[
i].scalefactors[j];
512 for (
int k = start; k < end; k++)
519 int start,
int count)
522 for (
int i = 0;
i < count;
i += 2) {
525 c->coeffs[start +
i + 0] =
tmp[0];
526 c->coeffs[start +
i + 1] =
tmp[1];
530 for (
int i = 0;
i < count;
i++)
531 c->coeffs[start +
i] /= maxval;
535 const int s_unit,
const int e_unit)
537 for (
int i = s_unit;
i < e_unit;
i++) {
540 for (
int j = start; j < end; j++)
541 c->coeffs[j] *= sf[
i - s_unit];
548 const int g_units[4] = {
552 FFMAX(g_units[2], 22),
555 const int g_bins[4] = {
562 for (
int ch = 0; ch <= stereo; ch++) {
566 for (
int i = 0;
i < 3;
i++)
567 for (
int j = 0; j < (g_bins[
i + 1] - g_bins[
i + 0]); j++)
568 c->coeffs[g_bins[
i] + j] =
c->coeffs[g_bins[
i] - j - 1];
570 switch (
c->band_ext) {
572 float sf[6] = { 0.0f };
573 const int l = g_units[3] - g_units[0] - 1;
606 for (
int i = g_units[0];
i < g_units[3];
i++)
614 const float g_sf[2] = {
619 for (
int i = 0;
i < 2;
i++)
620 for (
int j = g_bins[
i + 0]; j < g_bins[
i + 1]; j++)
621 c->coeffs[j] *= g_sf[
i];
628 for (
int i = g_bins[0];
i < g_bins[3];
i++) {
636 const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
638 for (
int i = 0;
i < 3;
i++)
639 for (
int j = g_bins[
i + 0]; j < g_bins[
i + 1]; j++)
640 c->coeffs[j] *= g_sf[
i];
649 int frame_idx,
int block_idx)
657 const int precision = reuse_params ? 8 : 4;
658 c->q_unit_cnt =
b->q_unit_cnt = 2;
660 memset(
c->scalefactors, 0,
sizeof(
c->scalefactors));
661 memset(
c->q_coeffs_fine, 0,
sizeof(
c->q_coeffs_fine));
662 memset(
c->q_coeffs_coarse, 0,
sizeof(
c->q_coeffs_coarse));
664 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
666 c->precision_coarse[
i] = precision;
667 c->precision_fine[
i] = 0;
670 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
673 for (
int j = start; j < end; j++)
674 c->q_coeffs_coarse[j] =
get_bits(gb,
c->precision_coarse[
i] + 1);
683 if (first_in_pkt && reuse_params) {
690 int stereo_band, ext_band;
691 const int min_band_count =
s->samplerate_idx > 7 ? 1 : 3;
693 b->band_count =
get_bits(gb, 4) + min_band_count;
696 b->band_ext_q_unit =
b->stereo_q_unit =
b->q_unit_cnt;
705 stereo_band =
get_bits(gb, 4) + min_band_count;
706 if (stereo_band >
b->band_count) {
715 if (
b->has_band_ext) {
716 ext_band =
get_bits(gb, 4) + min_band_count;
717 if (ext_band < b->band_count) {
736 b->cpe_base_channel = 0;
740 for (
int i =
b->stereo_q_unit;
i < b->q_unit_cnt;
i++)
753 for (
int i = 0;
i <= stereo;
i++) {
755 c->q_unit_cnt =
i ==
b->cpe_base_channel ?
b->q_unit_cnt :
767 b->q_unit_cnt_prev =
b->has_band_ext ?
b->band_ext_q_unit :
b->q_unit_cnt;
772 if (
b->has_band_ext &&
b->has_band_ext_data)
776 for (
int i = 0;
i <= stereo;
i++) {
778 const int dst_idx =
s->block_config->plane_map[block_idx][
i];
779 const int wsize = 1 <<
s->frame_log2;
780 const ptrdiff_t
offset = wsize*frame_idx*
sizeof(
float);
783 s->tx_fn(
s->tx,
s->temp,
c->coeffs,
sizeof(
float));
784 s->fdsp->vector_fmul_window(
dst,
c->prev_win,
s->temp,
785 s->imdct_win, wsize >> 1);
786 memcpy(
c->prev_win,
s->temp + (wsize >> 1),
sizeof(
float)*wsize >> 1);
793 int *got_frame_ptr,
AVPacket *avpkt)
810 for (
int j = 0; j <
s->block_config->count; j++) {
827 for (
int j = 0; j <
s->block_config->count; j++) {
830 for (
int i = 0;
i <= stereo;
i++) {
832 memset(
c->prev_win, 0,
sizeof(
c->prev_win));
848 int nb_bits,
int nb_codes,
863 const uint8_t (*
tab)[2];
867 for (
int i = 1;
i < 7;
i++) {
876 for (
int i = 2;
i < 6;
i++) {
883 hf->size, &
tab, -16);
888 for (
int i = 0;
i < 2;
i++) {
889 for (
int j = 2; j < 8; j++) {
890 for (
int k =
i; k < 4; k++) {
905 int err,
version, block_config_idx, superframe_idx, alloc_c_len;
939 block_config_idx =
get_bits(&gb, 3);
940 if (block_config_idx > 5) {
947 avctx->
ch_layout =
s->block_config->channel_layout;
956 s->avg_frame_size =
get_bits(&gb, 11) + 1;
959 if (superframe_idx & 1) {
964 s->frame_count = 1 << superframe_idx;
967 scale = 1.0f / 32768.0;
969 1 <<
s->frame_log2, &
scale, 0);
978 for (
int i = 0;
i < (1 <<
s->frame_log2);
i++) {
979 const int len = 1 <<
s->frame_log2;
980 const float sidx = (
i + 0.5f) /
len;
981 const float eidx = (
len -
i - 0.5f) /
len;
984 s->imdct_win[
i] = s_c / ((s_c * s_c) + (e_c * e_c));
989 for (
int i = 1;
i <= alloc_c_len;
i++)
990 for (
int j = 0; j <
i; j++)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t const int8_t * vf
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t * hf
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
static const float bands[]
const FFCodec ff_atrac9_decoder
static av_cold void close(AVCodecParserContext *s)
static int atrac9_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6], const int s_unit, const int e_unit)
static int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb, int stereo)
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
static const VLCElem * sf_vlc[2][8]
static void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
static void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
static av_cold void atrac9_init_static(void)
static void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
#define ATRAC9_SF_VLC_BITS
static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb, ATRAC9BlockData *b, AVFrame *frame, int frame_idx, int block_idx)
static void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
static void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
static av_cold void atrac9_decode_flush(AVCodecContext *avctx)
static const VLCElem * coeff_vlc[2][8][4]
static int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb)
static void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
static void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c, int start, int count)
static av_cold int atrac9_decode_init(AVCodecContext *avctx)
static av_cold const VLCElem * atrac9_init_vlc(VLCInitState *state, int nb_bits, int nb_codes, const uint8_t(**tab)[2], int offset)
static int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
static void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
#define ATRAC9_COEFF_VLC_BITS
static void dequantize(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
static const ATRAC9BlockConfig at9_block_layout[]
static const uint8_t at9_tab_band_ext_group[][3]
static const HuffmanCodebook at9_huffman_coeffs[][8][4]
static const uint8_t at9_tab_sri_max_bands[]
static const float at9_scalefactor_c[]
static const uint8_t at9_tab_sf_weights[][32]
static const uint8_t at9_tab_b_dist[]
static const int at9_q_unit_to_coeff_idx[]
static const uint8_t at9_tab_band_ext_lengths[][6][4]
static const uint8_t at9_tab_band_ext_cnt[][6]
static const uint8_t at9_tab_band_q_unit_map[]
static const uint8_t at9_tab_sri_frame_log2[]
static const int at9_tab_samplerates[]
static const uint8_t at9_q_unit_to_coeff_cnt[]
static const HuffmanCodebook at9_huffman_sf_signed[]
static const float at9_band_ext_scales_m4[]
static const uint8_t at9_q_unit_to_codebookidx[]
static const float at9_band_ext_scales_m3[][2]
static const float at9_band_ext_scales_m0[][5][32]
static const uint8_t at9_sfb_b_tab[][2]
static const HuffmanCodebook at9_huffman_sf_unsigned[]
static const float at9_quant_step_coarse[]
static const uint8_t at9_sfb_a_tab[][2]
static const uint8_t at9_coeffs_tab[][2]
static const float at9_band_ext_scales_m2[]
static const float at9_quant_step_fine[]
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
static struct @346255127015250356166251341105367306144006377143 state
static const OptionGroupDef groups[]
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static const uint8_t * align_get_bits(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_SAMPLE_FMT_FLTP
float, planar
static void scale(int *out, const int *in, const int w, const int h, const int shift)
void av_bmg_get(AVLFG *lfg, double out[2])
Get the next two numbers generated by a Box-Muller Gaussian generator using the random numbers issued...
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
static int ff_thread_once(char *control, void(*routine)(void))
static av_const int sign_extend(int val, unsigned bits)
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static const uint16_t table[]
#define FF_ARRAY_ELEMS(a)
ATRAC9ChannelData channel[2]
int32_t q_coeffs_coarse[256]
int32_t q_coeffs_fine[256]
int32_t scalefactors_prev[31]
uint8_t alloc_curve[48][48]
const ATRAC9BlockConfig * block_config
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
This structure describes decoded (raw) audio or video data.
Context structure for the Lagged Fibonacci PRNG.
This structure stores compressed data.
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
static const struct twinvq_data tab
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static double cb(void *priv, double x, double y)
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
#define VLC_INIT_STATE(_table)