52 float two_cos_w = 2.0f * cos_val;
54 for (j = 0; j + 1 < order; j += 2 * 2) {
56 q *= lsp[j] - two_cos_w;
57 p *= lsp[j + 1] - two_cos_w;
59 q *= lsp[j + 2] - two_cos_w;
60 p *= lsp[j + 3] - two_cos_w;
63 p *= p * (2.0f - two_cos_w);
64 q *= q * (2.0f + two_cos_w);
78 for (
i = 0;
i < size_s / 2;
i++) {
88 float step = (v1 - v2) / (
size + 1);
118 float *
out,
const float *in,
119 int size,
int step,
int part)
126 for (
i = 0;
i <
size;
i += step)
133 for (
i = step;
i <=
size - 2 * step;
i += step) {
143 out[
i - step], step / 2 - 1);
145 out[
i - step / 2], step / 2 - 1);
150 out[
size - 2 * step], step - 1);
154 const float *buf,
float *lpc,
162 lpc[
size / 2 - step], step);
175 const int16_t *cb0,
const int16_t *cb1,
int cb_len)
184 const int16_t *tab0, *
tab1;
204 tab0 = cb0 + tmp0 * cb_len;
205 tab1 = cb1 + tmp1 * cb_len;
207 for (j = 0; j < length; j++)
228 out[
i] = (1.0 / (1 << 13)) *
233 float val = (1.0 / (1 << 23)) *
237 for (j = 0; j < sub; j++)
240 sub_step *
bits->sub_gain_bits[
i * sub + j],
255 float min_dist2 = min_dist * 0.5;
256 for (
i = 1;
i < order;
i++)
257 if (lsp[
i] - lsp[
i - 1] < min_dist) {
258 float avg = (lsp[
i] + lsp[
i - 1]) * 0.5;
260 lsp[
i - 1] =
avg - min_dist2;
261 lsp[
i] =
avg + min_dist2;
266 int lpc_hist_idx,
float *lsp,
float *hist)
273 const float *cb3 = cb2 + (1 << mtab->
lsp_bit2) * mtab->
n_lsp;
275 const int8_t funny_rounding[4] = {
287 lsp[j] =
cb[lpc_idx1 * mtab->
n_lsp + j] +
288 cb2[lpc_idx2[
i] * mtab->
n_lsp + j];
294 float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->
n_lsp +
i];
295 float tmp2 = hist[
i] * cb3[lpc_hist_idx * mtab->
n_lsp +
i];
297 lsp[
i] = lsp[
i] * tmp1 + tmp2;
312 lsp[
i] = 2 * cos(lsp[
i]);
330 int wtype,
float *in,
float *prev,
int ch)
338 int j, first_wsize, wsize;
342 int types_sizes[] = {
350 prev_buf = prev + (
size - bsize) / 2;
355 if (!j && wtype == 4)
362 tx_fn(tx, buf1 + bsize * j, in + bsize * j,
sizeof(
float));
370 memcpy(out2, buf1 + bsize * j + wsize / 2,
371 (bsize - wsize / 2) *
sizeof(
float));
375 prev_buf = buf1 + bsize * j + bsize / 2;
393 prev_buf + 2 *
i * mtab->
size,
400 size1 = mtab->
size - size2;
403 memcpy(out1, prev_buf, size1 *
sizeof(*out1));
404 memcpy(out1 + size1, tctx->
curr_frame, size2 *
sizeof(*out1));
408 memcpy(out2, &prev_buf[2 * mtab->
size],
409 size1 *
sizeof(*out2));
411 size2 *
sizeof(*out2));
423 int block_size = mtab->
size / sub;
445 float *chunk =
out + mtab->
size *
i;
448 for (j = 0; j < sub; j++) {
450 bits->bark_use_hist[
i][j],
i,
454 chunk + block_size * j,
481 int *got_frame_ptr,
AVPacket *avpkt)
483 const uint8_t *buf = avpkt->
data;
484 int buf_size = avpkt->
size;
495 out = (
float **)
frame->extended_data;
498 if (buf_size < avctx->block_align) {
500 "Frame too small (%d bytes). Truncated file?\n", buf_size);
543 float norm =
channels == 1 ? 2.0 : 1.0;
546 for (
i = 0;
i < 3;
i++) {
548 const float scale = -sqrt(norm / bsize) / (1 << 15);
550 1, bsize, &
scale, 0)))
560 for (
i = 0;
i < 3;
i++) {
562 double freq = 2 *
M_PI / m;
565 for (j = 0; j <= m / 8; j++)
566 tctx->
cos_tabs[
i][j] = cos((2 * j + 1) * freq);
567 for (j = 1; j < m / 8; j++)
586 const uint8_t line_len[2],
int length_div,
591 for (
i = 0;
i < line_len[0];
i++) {
594 if (num_blocks == 1 ||
604 for (j = 0; j < num_vect && (j + num_vect *
i < block_size * num_blocks); j++)
605 tab[
i * num_vect + j] =
i * num_vect + (j +
shift) % num_vect;
625 const uint8_t line_len[2],
int length_div)
630 for (
i = 0;
i < num_vect;
i++)
631 for (j = 0; j < line_len[i >= length_div]; j++)
632 out[cont++] = in[j * num_vect +
i];
637 int block_size =
size / n_blocks;
641 out[
i] = block_size * (in[
i] % n_blocks) + in[
i] / n_blocks;
647 int block_size,
size;
649 int16_t *tmp_perm = (int16_t *)tctx->
tmp_buf;
683 int bsize_no_main_cb[3], bse_bits[3],
i;
686 for (
i = 0;
i < 3;
i++)
692 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
695 for (
i = 0;
i < 2;
i++)
696 bsize_no_main_cb[
i] =
702 bsize_no_main_cb[1] += 2;
703 bsize_no_main_cb[2] += 2;
707 for (
i = 0;
i < 4;
i++) {
708 int bit_size, vect_size;
709 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
714 bit_size = total_fr_bits - bsize_no_main_cb[
i];
715 vect_size = n_ch * mtab->
size;
718 tctx->
n_div[
i] = (bit_size + 13) / 14;
720 rounded_up = (bit_size + tctx->
n_div[
i] - 1) /
722 rounded_down = (bit_size) / tctx->
n_div[
i];
723 num_rounded_down = rounded_up * tctx->
n_div[
i] - bit_size;
724 num_rounded_up = tctx->
n_div[
i] - num_rounded_down;
731 rounded_up = (vect_size + tctx->
n_div[
i] - 1) /
733 rounded_down = (vect_size) / tctx->
n_div[
i];
734 num_rounded_down = rounded_up * tctx->
n_div[
i] - vect_size;
735 num_rounded_up = tctx->
n_div[
i] - num_rounded_down;
736 tctx->
length[
i][0] = rounded_up;
737 tctx->
length[
i][1] = rounded_down;
750 for (
i = 0;
i < 3;
i++) {
777 if (frames_per_packet <= 0) {
static double val(void *priv, double ch)
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
static float cos_tab[256]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static const uint8_t bits[8]
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#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)
static int shift(int a, int b)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
void ff_sort_nearly_sorted_floats(float *vals, int len)
Sort values in ascending order.
#define FFSWAP(type, a, b)
Memory handling functions.
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
SINETABLE_CONST float *const ff_sine_windows[]
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int64_t bit_rate
the average bitrate
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
void(* butterflies_float)(float *restrict v1, float *restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
float bark_hist[3][2][40]
BSE coefficients of last frame.
uint8_t length[4][2]
main codebook stride
const TwinVQModeTab * mtab
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
void(* dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
float lsp_hist[2][20]
LSP coefficients of the last frame.
float * prev_frame
non-interleaved previous frame
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
float * curr_frame
non-interleaved output
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
int bits_main_spec_change[4]
enum TwinVQFrameType ftype
uint8_t bark_n_bit
number of bits of the BSE coefs
uint8_t bark_n_coef
number of BSE CB coefficients to read
uint8_t sub
Number subblocks in each frame.
uint8_t cb_len_read
number of spectrum coefficients to read
const int16_t * cb0
main codebooks for spectrum data
Parameters and tables that are different for every combination of bitrate/sample rate.
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
uint8_t ppc_shape_len
size of PPC shape CB
uint8_t lsp_split
number of CB entries for the LSP decoding
uint8_t n_lsp
number of lsp coefficients
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
const float * lspcodebook
uint8_t ppc_period_bit
number of the bits for the PPC period value
const int16_t * ppc_shape_cb
PPC shape CB.
uint16_t size
frame size in samples
uint8_t pgain_bit
bits for PPC gain
static av_cold void construct_perm_table(TwinVQContext *tctx, enum TwinVQFrameType ftype)
static av_cold int init_mdct_win(TwinVQContext *tctx)
Init IMDCT and windowing tables.
static void transpose_perm(int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div)
Interpret the input data as in the following table:
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp, enum TwinVQFrameType ftype, float *lpc)
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
static void eval_lpcenv_or_interp(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out, const float *in, int size, int step, int part)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
int ff_twinvq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out, enum TwinVQFrameType ftype, const int16_t *cb0, const int16_t *cb1, int cb_len)
Inverse quantization.
static void dec_gain(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out)
static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks, int block_size, const uint8_t line_len[2], int length_div, enum TwinVQFrameType ftype)
Interpret the data as if it were a num_blocks x line_len[0] matrix and for each line do a cyclic perm...
static av_cold void init_bitstream_params(TwinVQContext *tctx)
static float get_cos(int idx, int part, const float *cos_tab, int size)
static const uint8_t wtype_to_wsize[]
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float **out, int offset)
static void interpolate(float *out, float v1, float v2, int size)
static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
Evaluate a single LPC amplitude spectrum envelope coefficient from the line spectrum pairs.
static void rearrange_lsp(int order, float *lsp, float min_dist)
Rearrange the LSP coefficients so that they have a minimum distance of min_dist.
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Requires the caller to call ff_twinvq_decode_close() upon failure.
static void read_and_decode_spectrum(TwinVQContext *tctx, float *out, enum TwinVQFrameType ftype)
static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype, const float *buf, float *lpc, int size, int step)
#define TWINVQ_PPC_SHAPE_LEN_MAX
#define TWINVQ_CHANNELS_MAX
#define TWINVQ_MAX_FRAMES_PER_PACKET
#define TWINVQ_SUB_GAIN_BITS
#define TWINVQ_WINDOW_TYPE_BITS
#define TWINVQ_PPC_SHAPE_CB_SIZE
#define TWINVQ_LSP_COEFS_MAX
@ TWINVQ_FT_MEDIUM
Medium frame (divided in m<n sub-blocks)
@ TWINVQ_FT_LONG
Long frame (single sub-block + PPC)
@ TWINVQ_FT_SHORT
Short frame (divided in n sub-blocks)
@ TWINVQ_FT_PPC
Periodic Peak Component (part of the long frame)
#define TWINVQ_SUBBLOCKS_MAX
#define TWINVQ_SUB_AMP_MAX
static float twinvq_mulawinv(float y, float clip, float mu)
static void twinvq_memset_float(float *buf, float val, int size)
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)
static int chunk_end(AVFormatContext *s, int flush)