103 512 , 12,
tab.
lsp08, 1, 5, 3, 3,
tab.
shape08 , 8, 28, 20, 6, 40
112 512 , 16,
tab.
lsp11, 1, 6, 4, 3,
tab.
shape11 , 9, 36, 30, 7, 90
121 512 , 16,
tab.
lsp11, 1, 6, 4, 3,
tab.
shape11 , 9, 36, 30, 7, 90
130 1024, 16,
tab.
lsp16, 1, 6, 4, 3,
tab.
shape16 , 9, 56, 60, 7, 180
139 1024, 16,
tab.
lsp22_1, 1, 6, 4, 3,
tab.
shape22_1, 9, 56, 36, 7, 144
148 1024, 16,
tab.
lsp22_1, 1, 6, 4, 3,
tab.
shape22_1, 9, 56, 36, 7, 144
157 512 , 16,
tab.
lsp22_2, 1, 6, 4, 4,
tab.
shape22_2, 9, 56, 36, 7, 72
166 2048, 20,
tab.
lsp44, 1, 6, 4, 4,
tab.
shape44 , 9, 84, 54, 7, 432
175 2048, 20,
tab.
lsp44, 1, 6, 4, 4,
tab.
shape44 , 9, 84, 54, 7, 432
211 #define PPC_SHAPE_CB_SIZE 64
212 #define PPC_SHAPE_LEN_MAX 60
213 #define SUB_AMP_MAX 4500.0
214 #define MULAW_MU 100.0
216 #define AMP_MAX 13000.0
217 #define SUB_GAIN_BITS 5
218 #define WINDOW_TYPE_BITS 4
220 #define LSP_COEFS_MAX 20
221 #define LSP_SPLIT_MAX 4
222 #define CHANNELS_MAX 2
223 #define SUBBLOCKS_MAX 16
224 #define BARK_N_COEF_MAX 4
250 float two_cos_w = 2.0f*cos_val;
252 for (j = 0; j + 1 < order; j += 2*2) {
254 q *= lsp[j ] - two_cos_w;
255 p *= lsp[j+1] - two_cos_w;
257 q *= lsp[j+2] - two_cos_w;
258 p *= lsp[j+3] - two_cos_w;
261 p *= p * (2.0f - two_cos_w);
262 q *= q * (2.0f + two_cos_w);
264 return 0.5 / (p + q);
276 for (i = 0; i < size_s/2; i++) {
286 float step = (v1 - v2)/(size + 1);
288 for (i = 0; i <
size; i++) {
296 return part ? -cos_tab[size - idx - 1] :
316 float *out,
const float *in,
327 get_cos(i, part, cos_tab, size),
331 for (i = step; i <= size - 2*
step; i +=
step) {
332 if (out[i + step] + out[i - step] > 1.95*out[i] ||
333 out[i + step] >= out[i - step]) {
334 interpolate(out + i - step + 1, out[i], out[i-step], step - 1);
338 get_cos(i-step/2, part, cos_tab, size),
340 interpolate(out + i - step + 1, out[i-step/2], out[i-step ], step/2 - 1);
341 interpolate(out + i - step/2 + 1, out[i ], out[i-step/2], step/2 - 1);
345 interpolate(out + size - 2*step + 1, out[size-step], out[size - 2*step], step - 1);
349 const float *buf,
float *lpc,
355 interpolate(lpc+size/2-step+1, lpc[size/2], lpc[size/2-step], step);
357 memset_float(lpc + size - 2*step + 1, lpc[size - 2*step], 2*step - 1);
367 const int16_t *cb0,
const int16_t *cb1,
int cb_len)
372 for (i = 0; i < tctx->
n_div[ftype]; i++) {
376 const int16_t *tab0, *
tab1;
398 tab0 = cb0 + tmp0*cb_len;
399 tab1 = cb1 + tmp1*cb_len;
401 for (j = 0; j < length; j++)
402 out[tctx->
permut[ftype][pos+j]] = sign0*tab0[j] + sign1*tab1[j];
411 y = av_clipf(y/clip, -1, 1);
412 return clip *
FFSIGN(y) * (exp(log(1+mu) * fabs(y)) - 1) / mu;
446 size =
tabs[b/5].size;
447 rtab =
tabs[b/5].tab;
448 return x - rtab[size*
av_log2(2*(x - 1)/size)+(x - 1)%size];
457 float ppc_gain,
float *speech,
int len)
461 const float *shape_end = shape +
len;
465 for (i = 0; i < width/2; i++)
466 speech[i] += ppc_gain * *shape++;
470 for (j = -width/2; j < (width+1)/2; j++)
471 speech[j+center] += ppc_gain * *shape++;
476 for (j = -width/2; j < (width + 1)/2 && shape < shape_end; j++)
477 speech[j+center] += ppc_gain * *shape++;
481 float ppc_gain,
float *speech)
488 int period_range = max_period - min_period;
492 int period = min_period +
496 if (isampf == 22 && ibps == 32) {
516 out[i] = (1./(1<<13)) *
521 float val = (1./(1<<23)) *
525 for (j = 0; j < sub; j++) {
544 float min_dist2 = min_dist * 0.5;
545 for (i = 1; i < order; i++)
546 if (lsp[i] - lsp[i-1] < min_dist) {
547 float avg = (lsp[i] + lsp[i-1]) * 0.5;
549 lsp[i-1] = avg - min_dist2;
550 lsp[i ] = avg + min_dist2;
555 int lpc_hist_idx,
float *lsp,
float *hist)
564 const int8_t funny_rounding[4] = {
573 int chunk_end = ((i + 1)*mtab->
n_lsp + funny_rounding[i])/mtab->
lsp_split;
574 for (; j < chunk_end; j++)
575 lsp[j] = cb [lpc_idx1 * mtab->
n_lsp + j] +
576 cb2[lpc_idx2[i] * mtab->
n_lsp + j];
581 for (i = 0; i < mtab->
n_lsp; i++) {
582 float tmp1 = 1. - cb3[lpc_hist_idx*mtab->
n_lsp + i];
583 float tmp2 = hist[i] * cb3[lpc_hist_idx*mtab->
n_lsp + i];
585 lsp[i] = lsp[i] * tmp1 + tmp2;
600 lsp[i] = 2*cos(lsp[i]);
616 float *in,
float *prev,
int ch)
630 static const uint8_t wtype_to_wsize[] = {0, 0, 2, 2, 2, 1, 0, 1, 1};
631 int types_sizes[] = {
637 wsize = types_sizes[wtype_to_wsize[wtype]];
639 prev_buf = prev + (size - bsize)/2;
641 for (j = 0; j < mtab->
fmode[ftype].
sub; j++) {
642 int sub_wtype = ftype ==
FT_MEDIUM ? 8 : wtype;
644 if (!j && wtype == 4)
646 else if (j == mtab->
fmode[ftype].
sub-1 && wtype == 7)
649 wsize = types_sizes[wtype_to_wsize[sub_wtype]];
651 mdct->
imdct_half(mdct, buf1 + bsize*j, in + bsize*j);
654 prev_buf + (bsize-wsize)/2,
660 memcpy(out2, buf1 + bsize*j + wsize/2, (bsize - wsize/2)*
sizeof(
float));
662 out2 += ftype ==
FT_MEDIUM ? (bsize-wsize)/2 : bsize - wsize;
664 prev_buf = buf1 + bsize*j + bsize/2;
681 prev_buf + 2*i*mtab->
size,
689 size1 = mtab->
size - size2;
691 memcpy(&out[0][0 ], prev_buf, size1 *
sizeof(out[0][0]));
692 memcpy(&out[0][size1], tctx->
curr_frame, size2 *
sizeof(out[0][0]));
695 memcpy(&out[1][0], &prev_buf[2*mtab->
size], size1 *
sizeof(out[1][0]));
696 memcpy(&out[1][size1], &tctx->
curr_frame[2*mtab->
size], size2 *
sizeof(out[1][0]));
702 int ch,
float *out,
float gain,
enum FrameType ftype)
706 float *hist = tctx->
bark_hist[ftype][ch];
707 float val = ((
const float []) {0.4, 0.35, 0.28})[ftype];
712 for (i = 0; i < fw_cb_len; i++)
713 for (j = 0; j < bark_n_coef; j++, idx++) {
715 mtab->
fmode[ftype].
bark_cb[fw_cb_len*in[j] + i] * (1./4096);
716 float st = use_hist ?
717 (1. - val) * tmp2 + val*hist[idx] + 1. : tmp2 + 1.;
720 if (st < -1.) st = 1.;
734 int block_size = mtab->
size / sub;
750 for (i = 0; i < channels; i++)
751 for (j = 0; j < sub; j++)
756 for (i = 0; i < channels; i++)
757 for (j = 0; j < sub; j++)
762 for (i = 0; i < channels; i++) {
777 for (i = 0; i < channels; i++) {
778 float *chunk = out + mtab->
size * i;
781 for (j = 0; j < sub; j++) {
783 tctx->
tmp_buf, gain[sub*i+j], ftype);
791 float pgain_step = 25000. / ((1 << mtab->
pgain_bit) - 1);
801 decode_lsp(tctx, lpc_idx1[i], lpc_idx2[i], lpc_hist_idx[i], lsp,
806 for (j = 0; j < mtab->
fmode[ftype].
sub; j++) {
814 int *got_frame_ptr,
AVPacket *avpkt)
817 int buf_size = avpkt->
size;
823 int window_type, ret;
824 static const enum FrameType wtype_to_ftype_table[] = {
831 "Frame too small (%d bytes). Truncated file?\n", buf_size);
849 if (window_type > 8) {
854 ftype = wtype_to_ftype_table[window_type];
884 float norm = channels == 1 ? 2. : 1.;
886 for (i = 0; i < 3; i++) {
889 -sqrt(norm/bsize) / (1<<15))))
906 for (i = 0; i < 3; i++) {
908 double freq = 2*M_PI/m;
910 (m / 4) *
sizeof(*tctx->
cos_tabs[i]), alloc_fail);
912 for (j = 0; j <= m/8; j++)
913 tctx->
cos_tabs[i][j] = cos((2*j + 1)*freq);
914 for (j = 1; j < m/8; j++)
936 const uint8_t line_len[2],
int length_div,
942 for (i = 0; i < line_len[0]; i++) {
945 if (num_blocks == 1 ||
946 (ftype ==
FT_LONG && num_vect % num_blocks) ||
947 (ftype !=
FT_LONG && num_vect & 1 ) ||
955 for (j = 0; j < num_vect && (j+num_vect*i < block_size*num_blocks); j++)
956 tab[i*num_vect+j] = i*num_vect + (j + shift) % num_vect;
976 const uint8_t line_len[2],
int length_div)
980 for (i = 0; i < num_vect; i++)
981 for (j = 0; j < line_len[i >= length_div]; j++)
982 out[cont++] = in[j*num_vect + i];
985 static void linear_perm(int16_t *out, int16_t *in,
int n_blocks,
int size)
987 int block_size = size/n_blocks;
990 for (i = 0; i <
size; i++)
991 out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
999 int16_t *tmp_perm = (int16_t *) tctx->
tmp_buf;
1010 block_size, tctx->
length[ftype],
1033 int bsize_no_main_cb[3];
1038 for (i = 0; i < 3; i++)
1040 bse_bits[i] = n_ch *
1043 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
1046 for (i = 0; i < 2; i++)
1047 bsize_no_main_cb[i] =
1052 for (i = 0; i < 4; i++) {
1055 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
1060 bit_size = total_fr_bits - bsize_no_main_cb[i];
1061 vect_size = n_ch * mtab->
size;
1064 tctx->
n_div[i] = (bit_size + 13) / 14;
1066 rounded_up = (bit_size + tctx->
n_div[i] - 1)/tctx->
n_div[i];
1067 rounded_down = (bit_size )/tctx->
n_div[i];
1068 num_rounded_down = rounded_up * tctx->
n_div[i] - bit_size;
1069 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
1076 rounded_up = (vect_size + tctx->
n_div[i] - 1)/tctx->
n_div[i];
1077 rounded_down = (vect_size )/tctx->
n_div[i];
1078 num_rounded_down = rounded_up * tctx->
n_div[i] - vect_size;
1079 num_rounded_up = tctx->
n_div[i] - num_rounded_down;
1080 tctx->
length[i][0] = rounded_up;
1081 tctx->
length[i][1] = rounded_down;
1094 for (i = 0; i < 3; i++) {
1114 tctx->
avctx = avctx;
1125 if (isampf < 8 || isampf > 44) {
1133 default: avctx->
sample_rate = isampf * 1000;
break;
1145 if (ibps < 8 || ibps > 48) {
1150 switch ((isampf << 8) + ibps) {
1161 av_log(avctx,
AV_LOG_ERROR,
"This version does not support %d kHz - %d kbit/s/ch mode.\n", isampf, isampf);
static const ModeTab mode_44_40
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
static av_cold void init_bitstream_params(TwinContext *tctx)
static void read_and_decode_spectrum(TwinContext *tctx, GetBitContext *gb, float *out, enum FrameType ftype)
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
static av_cold int twin_decode_close(AVCodecContext *avctx)
int bits_main_spec_change[4]
#define CODEC_FLAG_BITEXACT
static const ModeTab mode_11_10
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
This structure describes decoded (raw) audio or video data.
FrameType
G723.1 frame types.
static void dec_lpc_spectrum_inv(TwinContext *tctx, float *lsp, enum FrameType ftype, float *lpc)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Parameters and tables that are different for every combination of bitrate/sample rate.
AVFrame * coded_frame
the picture in the bitstream
const int16_t * ppc_shape_cb
PPC shape CB.
static const uint16_t bark_tab_m44_512[]
float * prev_frame
non-interleaved previous frame
static const ModeTab mode_22_32
static const uint16_t bark_tab_m08_256[]
uint8_t sub
Number subblocks in each frame.
#define AV_CH_LAYOUT_STEREO
static const ModeTab mode_16_16
uint8_t ppc_period_bit
number of the bits for the PPC period value
struct TwinContext TwinContext
const int16_t * cb0
main codebooks for spectrum data
uint16_t peak_per2wid
constant for peak period to peak width conversion
const uint16_t * bark_tab
Short frame (divided in n sub-blocks)
uint8_t ppc_shape_len
size of PPC shape CB
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
#define FFSWAP(type, a, b)
enum AVSampleFormat sample_fmt
audio sample format
uint8_t bark_n_coef
number of BSE CB coefficients to read
static const uint16_t bark_tab_s11_64[]
static const uint16_t bark_tab_s22_128[]
static const uint16_t bark_tab_l11_512[]
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
const float * lspcodebook
static void interpolate(float *out, float v1, float v2, int size)
static const uint16_t bark_tab_l22_1024[]
bitstream reader API header.
static const uint16_t bark_tab_l44_2048[]
static const uint16_t bark_tab_s08_64[]
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
static int init(AVCodecParserContext *s)
uint8_t n_lsp
number of lsp coefficients
static const ModeTab mode_08_08
uint8_t bark_n_bit
number of bits of the BSE coefs
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...
static const uint16_t bark_tab_m11_256[]
Long frame (single sub-block + PPC)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const ModeTab mode_11_08
static av_cold int init_mdct_win(TwinContext *tctx)
Init IMDCT and windowing tables.
static const uint16_t bark_tab_m22_256[]
static const uint16_t bark_tab_m22_512[]
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static const uint16_t bark_tab_s44_128[]
static const uint16_t bark_tab_l22_512[]
static const ModeTab mode_22_24
uint64_t channel_layout
Audio channel layout.
static void dequant(TwinContext *tctx, GetBitContext *gb, float *out, enum FrameType ftype, const int16_t *cb0, const int16_t *cb1, int cb_len)
Inverse quantization.
#define PPC_SHAPE_CB_SIZE
Parameters and tables that are different for each frame type.
int bit_rate
the average bitrate
audio channel layout utility functions
static void imdct_and_window(TwinContext *tctx, enum FrameType ftype, int wtype, float *in, float *prev, int ch)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
static float mulawinv(float y, float clip, float mu)
static int very_broken_op(int a, int b)
Evaluate a*b/400 rounded to the nearest integer.
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 FrameType 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 void rearrange_lsp(int order, float *lsp, float min_dist)
Rearrange the LSP coefficients so that they have a minimum distance of min_dist.
static void add_peak(int period, int width, const float *shape, float ppc_gain, float *speech, int len)
Sum to data a periodic peak of a given period, width and shape.
float * curr_frame
non-interleaved output
static const ModeTab mode_22_20
float bark_hist[3][2][40]
BSE coefficients of last frame.
static const ModeTab mode_44_48
static const int16_t cos_tab[COS_TBL_SIZE]
int sample_rate
samples per second
main external API structure.
static void close(AVCodecParserContext *s)
static const uint16_t bark_tab_m16_512[]
void(* butterflies_float)(float *restrict v1, float *restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
static void decode_lsp(TwinContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
static void dec_bark_env(TwinContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum FrameType ftype)
static const struct @47 tabs[]
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
uint8_t length[4][2]
main codebook stride
AVCodec ff_twinvq_decoder
static unsigned int get_bits1(GetBitContext *s)
float lsp_hist[2][20]
LSP coefficients of the last frame.
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
static void skip_bits(GetBitContext *s, int n)
#define FF_ARRAY_ELEMS(a)
Periodic Peak Component (part of the long frame)
static const uint16_t bark_tab_l16_1024[]
static int twin_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
SINETABLE_CONST float *const ff_sine_windows[14]
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define ROUNDED_DIV(a, b)
static void imdct_output(TwinContext *tctx, enum FrameType ftype, int wtype, float **out)
#define PPC_SHAPE_LEN_MAX
static void memset_float(float *buf, float val, 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...
uint8_t bark_env_size
number of distinct bark scale envelope values
static void decode_ppc(TwinContext *tctx, int period_coef, const float *shape, float ppc_gain, float *speech)
uint8_t pgain_bit
bits for PPC gain
static void eval_lpcenv_or_interp(TwinContext *tctx, enum FrameType ftype, float *out, const float *in, int size, int step, int part)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
common internal api header.
static const uint16_t bark_tab_s16_128[]
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
#define AVERROR_INVALIDDATA
AVSampleFormat
Audio Sample Formats.
void ff_sort_nearly_sorted_floats(float *vals, int len)
Sort values in ascending order.
static av_cold int twin_decode_init(AVCodecContext *avctx)
static void eval_lpcenv(TwinContext *tctx, const float *cos_vals, float *lpc)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
static void dec_gain(TwinContext *tctx, GetBitContext *gb, enum FrameType ftype, float *out)
int channels
number of audio channels
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:
static const struct twinvq_data tab
static const uint16_t bark_tab_l08_512[]
static float get_cos(int idx, int part, const float *cos_tab, int size)
uint16_t size
frame size in samples
void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Medium frame (divided in m<n sub-blocks)
static av_cold void construct_perm_table(TwinContext *tctx, enum FrameType ftype)
uint8_t ** extended_data
pointers to the data planes/channels.
#define AV_CH_LAYOUT_MONO
struct FrameMode fmode[3]
frame type-dependant parameters
This structure stores compressed data.
const int16_t * bark_cb
codebook for the bark scale envelope (BSE)
int nb_samples
number of audio samples (per channel) described by this frame
static void eval_lpcenv_2parts(TwinContext *tctx, enum FrameType ftype, const float *buf, float *lpc, int size, int step)
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
uint8_t lsp_split
number of CB entries for the LSP decoding
uint8_t cb_len_read
number of spectrum coefficients to read
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)