32 #define ATRAC9_SF_VLC_BITS 8
33 #define ATRAC9_COEFF_VLC_BITS 9
113 int values, sign,
base;
125 grad_range[1] =
get_bits(gb, 6) + 1;
131 if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
134 if (
b->grad_boundary >
b->q_unit_cnt)
137 values = grad_value[1] - grad_value[0];
138 sign = 1 - 2*(values < 0);
139 base = grad_value[0] + sign;
140 scale = (
FFABS(values) - 1) / 31.0f;
141 curve =
s->alloc_curve[grad_range[1] - grad_range[0] - 1];
143 for (
int i = 0;
i <=
b->q_unit_cnt;
i++)
144 b->gradient[
i] = grad_value[
i >= grad_range[0]];
146 for (
int i = grad_range[0];
i < grad_range[1];
i++)
147 b->gradient[
i] =
base + sign*((
int)(scale*curve[
i - grad_range[0]]));
155 memset(
c->precision_mask, 0,
sizeof(
c->precision_mask));
156 for (
int i = 1;
i <
b->q_unit_cnt;
i++) {
157 const int delta =
FFABS(
c->scalefactors[
i] -
c->scalefactors[
i - 1]) - 1;
159 const int neg =
c->scalefactors[
i - 1] >
c->scalefactors[
i];
165 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
166 c->precision_coarse[
i] =
c->scalefactors[
i];
167 c->precision_coarse[
i] +=
c->precision_mask[
i] -
b->gradient[
i];
168 if (
c->precision_coarse[
i] < 0)
170 switch (
b->grad_mode) {
172 c->precision_coarse[
i] >>= 1;
175 c->precision_coarse[
i] = (3 *
c->precision_coarse[
i]) >> 3;
178 c->precision_coarse[
i] >>= 2;
183 for (
int i = 0;
i <
b->q_unit_cnt;
i++)
184 c->precision_coarse[
i] =
c->scalefactors[
i] -
b->gradient[
i];
188 for (
int i = 0;
i <
b->q_unit_cnt;
i++)
189 c->precision_coarse[
i] =
FFMAX(
c->precision_coarse[
i], 1);
191 for (
int i = 0;
i <
b->grad_boundary;
i++)
192 c->precision_coarse[
i]++;
194 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
195 c->precision_fine[
i] = 0;
196 if (
c->precision_coarse[
i] > 15) {
197 c->precision_fine[
i] =
FFMIN(
c->precision_coarse[
i], 30) - 15;
198 c->precision_coarse[
i] = 15;
208 if (
b->has_band_ext) {
209 if (
b->q_unit_cnt < 13 ||
b->q_unit_cnt > 20)
213 b->channel[1].band_ext =
get_bits(gb, 2);
214 b->channel[1].band_ext = ext_band > 2 ?
b->channel[1].band_ext : 4;
221 if (!
b->has_band_ext_data)
224 if (!
b->has_band_ext) {
230 b->channel[0].band_ext =
get_bits(gb, 2);
231 b->channel[0].band_ext = ext_band > 2 ?
b->channel[0].band_ext : 4;
234 for (
int i = 0;
i <= stereo;
i++) {
237 for (
int j = 0; j < count; j++) {
246 for (
int i = 0;
i <= stereo;
i++) {
249 for (
int j = 0; j < count; j++) {
260 int channel_idx,
int first_in_pkt)
262 static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
263 const int mode = mode_map[channel_idx][
get_bits(gb, 2)];
265 memset(
c->scalefactors, 0,
sizeof(
c->scalefactors));
267 if (first_in_pkt && (
mode == 4 || ((
mode == 3) && !channel_idx))) {
281 for (
int i = 1;
i <
b->band_ext_q_unit;
i++) {
284 c->scalefactors[
i] =
val & ((1 <<
len) - 1);
287 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
288 c->scalefactors[
i] +=
base - sf_weights[
i];
295 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
301 const int *baseline =
mode == 4 ?
c->scalefactors_prev :
302 channel_idx ?
b->channel[0].scalefactors :
303 c->scalefactors_prev;
304 const int baseline_len =
mode == 4 ?
b->q_unit_cnt_prev :
305 channel_idx ?
b->band_ext_q_unit :
309 const int unit_cnt =
FFMIN(
b->band_ext_q_unit, baseline_len);
312 for (
int i = 0;
i < unit_cnt;
i++) {
314 c->scalefactors[
i] = baseline[
i] + dist;
317 for (
int i = unit_cnt;
i <
b->band_ext_q_unit;
i++)
323 const int *baseline = channel_idx ?
b->channel[0].scalefactors :
324 c->scalefactors_prev;
325 const int baseline_len = channel_idx ?
b->band_ext_q_unit :
330 const int unit_cnt =
FFMIN(
b->band_ext_q_unit, baseline_len);
335 for (
int i = 1;
i < unit_cnt;
i++) {
338 c->scalefactors[
i] =
val & ((1 <<
len) - 1);
341 for (
int i = 0;
i < unit_cnt;
i++)
342 c->scalefactors[
i] +=
base + baseline[
i];
344 for (
int i = unit_cnt;
i <
b->band_ext_q_unit;
i++)
350 for (
int i = 0;
i <
b->band_ext_q_unit;
i++)
351 if (
c->scalefactors[
i] < 0 ||
c->scalefactors[
i] > 31)
354 memcpy(
c->scalefactors_prev,
c->scalefactors,
sizeof(
c->scalefactors));
363 const int last_sf =
c->scalefactors[
c->q_unit_cnt];
365 memset(
c->codebookset, 0,
sizeof(
c->codebookset));
367 if (
c->q_unit_cnt <= 1)
369 if (
s->samplerate_idx > 7)
372 c->scalefactors[
c->q_unit_cnt] =
c->scalefactors[
c->q_unit_cnt - 1];
374 if (
c->q_unit_cnt > 12) {
375 for (
int i = 0;
i < 12;
i++)
376 avg +=
c->scalefactors[
i];
380 for (
int i = 8;
i <
c->q_unit_cnt;
i++) {
381 const int prev =
c->scalefactors[
i - 1];
382 const int cur =
c->scalefactors[
i ];
383 const int next =
c->scalefactors[
i + 1];
385 if ((cur -
min >= 3 || 2*cur - prev - next >= 3))
386 c->codebookset[
i] = 1;
390 for (
int i = 12;
i <
c->q_unit_cnt;
i++) {
391 const int cur =
c->scalefactors[
i];
393 const int min =
FFMIN(
c->scalefactors[
i + 1],
c->scalefactors[
i - 1]);
394 if (
c->codebookset[
i])
397 c->codebookset[
i] = (((cur -
min) >= 2) && (cur >= (
avg - cnd)));
400 c->scalefactors[
c->q_unit_cnt] = last_sf;
406 const int max_prec =
s->samplerate_idx > 7 ? 1 : 7;
408 memset(
c->q_coeffs_coarse, 0,
sizeof(
c->q_coeffs_coarse));
410 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
413 const int prec =
c->precision_coarse[
i] + 1;
415 if (prec <= max_prec) {
416 const int cb =
c->codebookset[
i];
422 for (
int j = 0; j < groups; j++) {
425 for (
int k = 0; k < huff->
value_cnt; k++) {
433 for (
int j = 0; j <
bands; j++)
442 memset(
c->q_coeffs_fine, 0,
sizeof(
c->q_coeffs_fine));
444 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
447 const int len =
c->precision_fine[
i] + 1;
449 if (
c->precision_fine[
i] <= 0)
452 for (
int j = start; j < end; j++)
460 memset(
c->coeffs, 0,
sizeof(
c->coeffs));
462 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
469 for (
int j = start; j < end; j++) {
470 const float vc =
c->q_coeffs_coarse[j] * coarse_c;
471 const float vf =
c->q_coeffs_fine[j] * fine_c;
472 c->coeffs[j] = vc + vf;
480 float *
src =
b->channel[
b->cpe_base_channel].coeffs;
481 float *dst =
b->channel[!
b->cpe_base_channel].coeffs;
486 if (
b->q_unit_cnt <=
b->stereo_q_unit)
489 for (
int i =
b->stereo_q_unit; i < b->q_unit_cnt;
i++) {
490 const int sign =
b->is_signs[
i];
493 for (
int j = start; j < end; j++)
494 dst[j] = sign*
src[j];
501 for (
int i = 0;
i <= stereo;
i++) {
502 float *coeffs =
b->channel[
i].coeffs;
503 for (
int j = 0; j <
b->q_unit_cnt; j++) {
506 const int scalefactor =
b->channel[
i].scalefactors[j];
508 for (
int k = start; k < end; k++)
515 int start,
int count)
518 for (
int i = 0;
i < count;
i += 2) {
521 c->coeffs[start +
i + 0] =
tmp[0];
522 c->coeffs[start +
i + 1] =
tmp[1];
526 for (
int i = 0;
i < count;
i++)
527 c->coeffs[start +
i] /= maxval;
531 const int s_unit,
const int e_unit)
533 for (
int i = s_unit;
i < e_unit;
i++) {
536 for (
int j = start; j < end; j++)
537 c->coeffs[j] *= sf[
i - s_unit];
544 const int g_units[4] = {
548 FFMAX(g_units[2], 22),
551 const int g_bins[4] = {
558 for (
int ch = 0; ch <= stereo; ch++) {
562 for (
int i = 0;
i < 3;
i++)
563 for (
int j = 0; j < (g_bins[
i + 1] - g_bins[
i + 0]); j++)
564 c->coeffs[g_bins[
i] + j] =
c->coeffs[g_bins[
i] - j - 1];
566 switch (
c->band_ext) {
568 float sf[6] = { 0.0f };
569 const int l = g_units[3] - g_units[0] - 1;
602 for (
int i = g_units[0];
i < g_units[3];
i++)
610 const float g_sf[2] = {
615 for (
int i = 0;
i < 2;
i++)
616 for (
int j = g_bins[
i + 0]; j < g_bins[
i + 1]; j++)
617 c->coeffs[j] *= g_sf[
i];
624 for (
int i = g_bins[0];
i < g_bins[3];
i++) {
626 c->coeffs[
i] *= scale;
632 const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
634 for (
int i = 0;
i < 3;
i++)
635 for (
int j = g_bins[
i + 0]; j < g_bins[
i + 1]; j++)
636 c->coeffs[j] *= g_sf[
i];
645 int frame_idx,
int block_idx)
653 const int precision = reuse_params ? 8 : 4;
654 c->q_unit_cnt =
b->q_unit_cnt = 2;
656 memset(
c->scalefactors, 0,
sizeof(
c->scalefactors));
657 memset(
c->q_coeffs_fine, 0,
sizeof(
c->q_coeffs_fine));
658 memset(
c->q_coeffs_coarse, 0,
sizeof(
c->q_coeffs_coarse));
660 for (
int i = 0;
i <
b->q_unit_cnt;
i++) {
662 c->precision_coarse[
i] = precision;
663 c->precision_fine[
i] = 0;
666 for (
int i = 0;
i <
c->q_unit_cnt;
i++) {
669 for (
int j = start; j < end; j++)
670 c->q_coeffs_coarse[j] =
get_bits(gb,
c->precision_coarse[
i] + 1);
679 if (first_in_pkt && reuse_params) {
686 int stereo_band, ext_band;
687 const int min_band_count =
s->samplerate_idx > 7 ? 1 : 3;
689 b->band_count =
get_bits(gb, 4) + min_band_count;
692 b->band_ext_q_unit =
b->stereo_q_unit =
b->q_unit_cnt;
701 stereo_band =
get_bits(gb, 4) + min_band_count;
702 if (stereo_band >
b->band_count) {
711 if (
b->has_band_ext) {
712 ext_band =
get_bits(gb, 4) + min_band_count;
713 if (ext_band < b->band_count) {
732 b->cpe_base_channel = 0;
736 for (
int i =
b->stereo_q_unit; i < b->q_unit_cnt;
i++)
749 for (
int i = 0;
i <= stereo;
i++) {
751 c->q_unit_cnt =
i ==
b->cpe_base_channel ?
b->q_unit_cnt :
763 b->q_unit_cnt_prev =
b->has_band_ext ?
b->band_ext_q_unit :
b->q_unit_cnt;
768 if (
b->has_band_ext &&
b->has_band_ext_data)
772 for (
int i = 0;
i <= stereo;
i++) {
774 const int dst_idx =
s->block_config->plane_map[block_idx][
i];
775 const int wsize = 1 <<
s->frame_log2;
776 const ptrdiff_t
offset = wsize*frame_idx*
sizeof(float);
779 s->imdct.imdct_half(&
s->imdct,
s->temp,
c->coeffs);
780 s->fdsp->vector_fmul_window(dst,
c->prev_win,
s->temp,
781 s->imdct_win, wsize >> 1);
782 memcpy(
c->prev_win,
s->temp + (wsize >> 1),
sizeof(
float)*wsize >> 1);
789 int *got_frame_ptr,
AVPacket *avpkt)
805 for (
int j = 0; j <
s->block_config->count; j++) {
822 for (
int j = 0; j <
s->block_config->count; j++) {
825 for (
int i = 0;
i <= stereo;
i++) {
827 memset(
c->prev_win, 0,
sizeof(
c->prev_win));
844 unsigned *buf_offset,
int offset)
851 &(*
tab)[0][1], 2, &(*
tab)[0][0], 2, 1,
864 for (
int i = 1;
i < 7;
i++) {
873 for (
int i = 2;
i < 6;
i++) {
885 for (
int i = 0;
i < 2;
i++) {
886 for (
int j = 2; j < 8; j++) {
887 for (
int k =
i; k < 4; k++) {
901 int version, block_config_idx, superframe_idx, alloc_c_len;
933 block_config_idx =
get_bits(&gb, 3);
934 if (block_config_idx > 5) {
950 s->avg_frame_size =
get_bits(&gb, 11) + 1;
953 if (superframe_idx & 1) {
958 s->frame_count = 1 << superframe_idx;
961 if (
ff_mdct_init(&
s->imdct,
s->frame_log2 + 1, 1, 1.0f / 32768.0f))
969 for (
int i = 0;
i < (1 <<
s->frame_log2);
i++) {
970 const int len = 1 <<
s->frame_log2;
971 const float sidx = (
i + 0.5f) /
len;
972 const float eidx = (
len -
i - 0.5f) /
len;
975 s->imdct_win[
i] = s_c / ((s_c * s_c) + (e_c * e_c));
980 for (
int i = 1;
i <= alloc_c_len;
i++)
981 for (
int j = 0; j <
i; j++)
static void flush(AVCodecContext *avctx)
static double val(void *priv, double ch)
static const float bands[]
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)
AVCodec ff_atrac9_decoder
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
static void atrac9_decode_flush(AVCodecContext *avctx)
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)
static av_cold void atrac9_init_vlc(VLC *vlc, int nb_bits, int nb_codes, const uint8_t(**tab)[2], unsigned *buf_offset, int offset)
#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 VLC 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 int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
static int atrac9_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
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[]
static av_cold int init(AVCodecContext *avctx)
int ff_init_vlc_from_lengths(VLC *vlc_arg, 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, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
static VLC_TYPE vlc_buf[16716][2]
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.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
mode
Use these values in ebur128_init (or'ed).
channel
Use these values when setting the channel map with ebur128_set_channel().
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], 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 void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static const uint8_t * align_get_bits(GetBitContext *s)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#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_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
@ AV_SAMPLE_FMT_FLTP
float, planar
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)
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static int ff_thread_once(char *control, void(*routine)(void))
static av_const int sign_extend(int val, unsigned bits)
#define FF_ARRAY_ELEMS(a)
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.
enum AVSampleFormat sample_fmt
audio sample format
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
uint64_t channel_layout
Audio channel layout.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t ** extended_data
pointers to the data planes/channels.
Context structure for the Lagged Fibonacci PRNG.
This structure stores compressed data.
VLC_TYPE(* table)[2]
code, bits
static const struct twinvq_data tab
static double cb(void *priv, double x, double y)
static const uint8_t offset[127][2]
#define INIT_VLC_STATIC_OVERLONG