45#define LPC_FILTERORDER 10
49#define ST_MEM_L_TBL 85
51#define STATE_SHORT_LEN_20MS 57
52#define STATE_SHORT_LEN_30MS 58
57#define CB_HALFFILTERLEN 4
60#define ENH_NBLOCKS_TOT 8
62#define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
63#define ENH_BUFL_FILTEROVERHEAD 3
72#define STATE_SHORT_LEN_30MS 58
73#define STATE_SHORT_LEN_20MS 57
75#define SPL_MUL_16_16(a, b) ((int32_t) (((int16_t)(a)) * ((int16_t)(b))))
76#define SPL_MUL_16_16_RSFT(a, b, c) (SPL_MUL_16_16(a, b) >> (c))
168 for (j = 0; j < 48; j++)
203 for (j = 0; j < 56; j++)
276 for (k = 4; k < 6; k++) {
279 }
else if (
index[k] >= 108 &&
index[k] < 128) {
287 int i, j,
pos = 0, cb_pos = 0;
315 for (
int n = 0; n < 2; n++) {
316 for (
int m = 0; m < nb_vectors; m++) {
317 for (
int k = 0; k <
dim - 1; k++) {
320 if ((lsf[
i + 1] - lsf[
i]) < 319) {
321 if (lsf[
i + 1] < lsf[
i]) {
322 lsf[
i + 1] = lsf[
i] + 160;
323 lsf[
i] = lsf[
i + 1] - 160;
337 const int16_t *in2, int16_t coef,
340 int invcoef = 16384 - coef,
i;
343 out[
i] = (coef * in1[
i] + invcoef * in2[
i] + 8192) >> 14;
346static void lsf2lsp(
const int16_t *lsf, int16_t *lsp,
int order)
352 for (
i = 0;
i < order;
i++) {
353 freq = (lsf[
i] * 20861) >> 15;
360 k =
FFMIN(freq >> 8, 63);
376 f[1] = lsp[0] * -1024;
378 for (
i = 2, k = 2, l = 2;
i <= 5;
i++, k += 2) {
381 for (j =
i; j > 1; j--, l--) {
382 high =
f[l - 1] >> 16;
383 low = (
f[l - 1] - (
high * (1 << 16))) >> 1;
385 tmp = ((
high * lsp[k]) * 4) + (((low * lsp[k]) >> 15) * 4);
388 f[l] -= (unsigned)
tmp;
391 f[l] -= lsp[k] * (1 << 10);
408 for (
i = 5;
i > 0;
i--) {
409 f[0][
i] += (unsigned)
f[0][
i - 1];
410 f[1][
i] -= (unsigned)
f[1][
i - 1];
414 for (
i = 5;
i > 0;
i--) {
415 tmp =
f[0][6 -
i] + (unsigned)
f[1][6 -
i] + 4096;
416 a[6 -
i] =
tmp >> 13;
418 tmp =
f[0][6 -
i] - (unsigned)
f[1][6 -
i] + 4096;
419 a[5 +
i] =
tmp >> 13;
424 const int16_t *lsf2,
int coef,
int length)
432static void bw_expand(int16_t *
out,
const int16_t *in,
const int16_t *coef,
int length)
437 for (
i = 1;
i < length;
i++)
438 out[
i] = (coef[
i] * in[
i] + 16384) >> 15;
442 const int16_t *lsfdeq, int16_t length,
446 const int16_t *
const lsfdeq2 = lsfdeq + length;
447 int i,
pos, lp_length;
449 lp_length = length + 1;
453 memcpy(syntdenum, lp, lp_length * 2);
457 for (
i = 1;
i < 6;
i++) {
461 memcpy(syntdenum +
pos, lp, lp_length * 2);
467 for (
i = 0;
i <
s->nsub;
i++) {
470 memcpy(syntdenum +
pos, lp, lp_length * 2);
477 memcpy(
s->lsfdeqold, lsfdeq2, length * 2);
479 memcpy(
s->lsfdeqold, lsfdeq, length * 2);
484 const int16_t *
B, int16_t B_length,
489 for (
i = 0;
i < length;
i++) {
490 const int16_t *b_ptr = &
B[0];
491 const int16_t *x_ptr = &in_ptr[
i];
494 for (j = 0; j < B_length; j++)
495 o += b_ptr[j] * *x_ptr--;
497 o =
av_clip(o, -134217728, 134215679);
499 out_ptr[
i] = ((o + 2048) >> 12);
506 int coefficients_length,
511 for (
i = 0;
i < data_length;
i++) {
512 int output = 0, sum = 0;
514 for (j = coefficients_length - 1; j > 0; j--) {
519 output =
av_clip(output, -134217728, 134215679);
521 data_out[
i] = (output + 2048) >> 12;
526 const int16_t *synt_denum, int16_t *Out_fix,
531 int16_t *tmp1, *tmp3;
553 tmp2 = &idx[
len - 1];
556 for (k = 0; k <
len; k++) {
563 }
else if (ifm < 59) {
564 for (k = 0; k <
len; k++) {
572 for (k = 0; k <
len; k++) {
582 memset(&sampleVal[
len], 0,
len * 2);
594 tmp1 = &sampleAr[
len - 1];
595 tmp2 = &sampleAr[2 *
len - 1];
597 for (k = 0; k <
len; k++) {
598 (*tmp3) = (*tmp1) + (*tmp2);
614 int length,
int shift)
616 for (
int i = 0;
i < length;
i++)
621 const int16_t *
win,
int length,
624 for (
int i = 0;
i < length;
i++)
629 const int16_t *in2,
int length,
632 for (
int i = 0;
i < length;
i++)
640 int16_t ilow =
index - interpolation_length;
658 int16_t k, base_size;
661 int16_t tempbuff2[
SUBL + 5] = {0};
664 base_size = lMem - cbveclen + 1;
666 if (cbveclen ==
SUBL) {
667 base_size += cbveclen / 2;
671 if (
index < lMem - cbveclen + 1) {
674 k =
index + cbveclen;
676 memcpy(cbvec, mem + lMem - k, cbveclen * 2);
677 }
else if (
index < base_size) {
678 memset(cbvec, 0, cbveclen * 2);
692 if (
index - base_size < lMem - cbveclen + 1) {
696 memIndTest = lMem - (
index - base_size + cbveclen);
705 memset(cbvec, 0, cbveclen * 2);
715 lag = (cbveclen << 1) - 20 +
index - base_size - lMem - 1;
724 const int16_t *
index,
725 const int16_t *gain_index,
731 int16_t cbvec0[
SUBL];
732 int16_t cbvec1[
SUBL];
733 int16_t cbvec2[
SUBL];
752 for (j = 0; j < veclen; j++) {
757 decvector[j] = (int)(a32 + 8192) >> 14;
763 int16_t* destPtr = dest;
764 const int16_t *sourcePtr = source;
767 for (j = 0; j < length; j++)
768 *destPtr-- = *sourcePtr++;
773 int16_t *decresidual,
774 const int16_t *syntdenum)
776 int16_t meml_gotten, Nfor, Nback,
diff, start_pos;
777 int16_t subcount, subframe;
778 int16_t *reverseDecresidual =
s->enh_buf;
779 int16_t *memVec =
s->prevResidual;
796 memset(mem, 0, (int16_t) (
CB_MEML -
s->state_short_len) * 2);
797 memcpy(mem +
CB_MEML -
s->state_short_len, decresidual + start_pos,
s->state_short_len * 2);
805 meml_gotten =
s->state_short_len;
807 memset(mem, 0, (int16_t) (
CB_MEML - meml_gotten) * 2);
820 Nfor =
s->nsub - encbits->
start - 1;
828 for (subframe = 0; subframe < Nfor; subframe++) {
842 Nback = encbits->
start - 1;
846 meml_gotten =
SUBL * (
s->nsub + 1 - encbits->
start);
852 memset(mem, 0, (int16_t) (
CB_MEML - meml_gotten) * 2);
855 for (subframe = 0; subframe < Nback; subframe++) {
876 if (vector ==
NULL || length <= 0) {
880 for (
i = 0;
i < length;
i++) {
887 return FFMIN(maximum, INT16_MAX);
894 if (0xFFFF0000 & n) {
900 if (0x0000FF00 & (n >>
bits))
bits += 8;
901 if (0x000000F0 & (n >>
bits))
bits += 4;
902 if (0x0000000C & (n >>
bits))
bits += 2;
903 if (0x00000002 & (n >>
bits))
bits += 1;
904 if (0x00000001 & (n >>
bits))
bits += 1;
913 for (
int i = 0;
i < length;
i++)
914 sum += (v1[
i] * v2[
i]) >> scaling;
920 int16_t lag, int16_t blen, int16_t srange, int16_t
scale)
922 const int16_t *w16ptr = &
buffer[blen - srange - lag];
933#define SPL_SHIFT_W32(x, c) (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
958 const int16_t *decresidual,
964 int32_t cross, ener, cross_comp, ener_comp = 0;
965 int32_t measure, max_measure, energy;
966 int16_t
max, cross_square_max, cross_square;
967 int16_t j, lag, tmp1, tmp2, randlag;
974 int16_t max_perSquare;
975 int16_t scale1, scale2;
987 s->consPLICount += 1;
992 if (
s->prevPLI != 1) {
1004 s->prevScale = scale3;
1011 corrLen =
FFMIN(60,
s->block_samples - (inlag + 3));
1013 correlation(&cross, &ener,
s->prevResidual, lag,
s->block_samples, corrLen, scale3);
1019 for (j = inlag - 2; j <= inlag + 3; j++) {
1020 correlation(&cross_comp, &ener_comp,
s->prevResidual, j,
s->block_samples, corrLen, scale3);
1036 if (((shift_max << 1) + shift3) > ((
shift1 << 1) +
shift2)) {
1044 if ((measure >> tmp1) > (max_measure >> tmp2)) {
1047 cross_square_max = cross_square;
1062 tmp2W32 =
scale_dot_product(&
s->prevResidual[
s->block_samples - corrLen], &
s->prevResidual[
s->block_samples - corrLen], corrLen, scale3);
1064 if ((tmp2W32 > 0) && (ener_comp > 0)) {
1078 totscale = scale1 + scale2 - 1;
1090 max_perSquare =
s->per_square;
1098 if (
s->consPLICount *
s->block_samples > 320) {
1108 if (max_perSquare > 7868) {
1110 }
else if (max_perSquare > 839) {
1113 while ((max_perSquare <
kPlcPerSqr[ind]) && (ind > 0)) {
1119 pitchfact =
FFMIN(tmpW32, 32767);
1134 for (
i = 0;
i <
s->block_samples;
i++) {
1137 randlag = 53 + (
s->seed & 63);
1142 randvec[
i] =
s->prevResidual[
s->block_samples + pick];
1144 randvec[
i] =
s->prevResidual[pick];
1151 plc_residual[
i] =
s->prevResidual[
s->block_samples + pick];
1153 plc_residual[
i] = plc_residual[pick];
1158 tot_gain = use_gain;
1159 }
else if (
i < 160) {
1166 plc_residual[
i] =
SPL_MUL_16_16_RSFT(tot_gain, (pitchfact * plc_residual[
i] + (32767 - pitchfact) * randvec[
i] + 16384) >> 15, 15);
1175 if (energy <
SPL_SHIFT_W32(
s->block_samples * 900, -
s->prevScale - 1)) {
1177 for (
i = 0;
i <
s->block_samples;
i++) {
1178 plc_residual[
i] = randvec[
i];
1187 s->per_square = max_perSquare;
1189 memcpy(plc_residual, decresidual,
s->block_samples * 2);
1191 s->consPLICount = 0;
1197 memcpy(
s->prevResidual, plc_residual,
s->block_samples * 2);
1202static int xcorr_coeff(
const int16_t *target,
const int16_t *regressor,
1203 int16_t subl, int16_t searchLen,
1204 int16_t
offset, int16_t step)
1209 int16_t cross_corr_scale, energy_scale;
1210 int16_t cross_corr_sg_mod, cross_corr_sg_mod_max;
1212 int16_t cross_corr_mod, energy_mod, enery_mod_max;
1214 const int16_t *rp_beg, *rp_end;
1215 int16_t totscale, totscale_max;
1222 cross_corr_sg_mod_max = 0;
1223 enery_mod_max = INT16_MAX;
1224 totscale_max = -500;
1232 rp_end = ®ressor[subl];
1235 rp_beg = ®ressor[-1];
1236 rp_end = ®ressor[subl - 1];
1252 for (k = 0; k < searchLen; k++) {
1253 rp = ®ressor[
pos];
1257 if ((energy > 0) && (cross_corr > 0)) {
1259 cross_corr_scale =
norm_w32(cross_corr) - 16;
1260 cross_corr_mod = (int16_t)
SPL_SHIFT_W32(cross_corr, cross_corr_scale);
1261 energy_scale =
norm_w32(energy) - 16;
1265 cross_corr_sg_mod = (int16_t)
SPL_MUL_16_16_RSFT(cross_corr_mod, cross_corr_mod, 16);
1270 totscale = energy_scale - (cross_corr_scale * 2);
1275 scalediff = totscale - totscale_max;
1276 scalediff =
FFMIN(scalediff, 31);
1277 scalediff =
FFMAX(scalediff, -31);
1283 if (scalediff < 0) {
1284 new_crit = ((
int32_t) cross_corr_sg_mod * enery_mod_max) >> (-scalediff);
1285 max_crit = ((
int32_t) cross_corr_sg_mod_max * energy_mod);
1287 new_crit = ((
int32_t) cross_corr_sg_mod * enery_mod_max);
1288 max_crit = ((
int32_t) cross_corr_sg_mod_max * energy_mod) >> scalediff;
1294 if (new_crit > max_crit) {
1295 cross_corr_sg_mod_max = cross_corr_sg_mod;
1296 enery_mod_max = energy_mod;
1297 totscale_max = totscale;
1304 energy += (unsigned)step * ((*rp_end * *rp_end - *rp_beg * *rp_beg) >>
shifts);
1313static void hp_output(int16_t *signal,
const int16_t *ba, int16_t *y,
1314 int16_t *x, int16_t
len)
1318 for (
int i = 0;
i <
len;
i++) {
1342 if (
tmp > 268435455) {
1344 }
else if (
tmp < -268435456) {
1351 y[1] = (
tmp - (y[0] * (1 << 16))) >> 1;
1356 int *got_frame_ptr,
AVPacket *avpkt)
1359 int mode =
s->mode, ret;
1369 frame->nb_samples =
s->block_samples;
1373 if (
s->frame.start < 1 ||
s->frame.start > 5)
1385 do_plc(
s->plc_residual,
s->plc_lpc, 0,
1389 memcpy(
s->decresidual,
s->plc_residual,
s->block_samples * 2);
1398 if (
s->mode == 20) {
1399 lag =
xcorr_coeff(&
s->decresidual[
s->block_samples-60], &
s->decresidual[
s->block_samples-80],
1411 memcpy(plc_data,
s->decresidual,
s->block_samples * 2);
1416 for (
i = 0;
i <
s->nsub;
i++) {
1426 memcpy(
frame->data[0], plc_data,
s->block_samples * 2);
1429 s->hpimemy,
s->hpimemx,
s->block_samples);
1451 s->mode = avctx->
bit_rate <= 14000 ? 30 : 20;
1460 if (
s->mode == 30) {
1461 s->block_samples = 240;
1467 s->block_samples = 160;
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_ilbc_decoder
Libavcodec external API header.
static const uint8_t shifts[2][12]
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
static IPT absolute(const CmsCtx *ctx, IPT ipt)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static double coefficients[8 *8]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static const uint8_t bits[8]
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#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.
#define AV_CHANNEL_LAYOUT_MONO
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.
@ AV_SAMPLE_FMT_S16
signed 16 bits
static const int16_t hp_out_coeffs[]
static const int16_t kPlcPfSlope[]
static const int16_t lsf_weight_20ms[]
static const uint8_t lsf_dim_codebook[]
static const int16_t frg_quant_mod[64]
static const int16_t ilbc_state[8]
static const int16_t kPlcPitchFact[]
static const int16_t alpha[]
static const int16_t kCbFiltersRev[]
static const int16_t cos_tbl[64]
static const int16_t kLpcChirpSyntDenum[]
static const int16_t lsf_codebook[64 *3+128 *3+128 *4]
static const int16_t *const ilbc_gain[]
static const int16_t lsf_weight_30ms[]
static const int16_t cos_derivative_tbl[64]
static const uint8_t lsf_size_codebook[]
static const int16_t kPlcPerSqr[]
static void state_construct(int16_t ifm, const int16_t *idx, const int16_t *synt_denum, int16_t *Out_fix, int16_t len)
static void add_vector_and_shift(int16_t *out, const int16_t *in1, const int16_t *in2, int length, int shift)
static void create_augmented_vector(int index, const int16_t *buffer, int16_t *cbVec)
static int32_t scale_dot_product(const int16_t *v1, const int16_t *v2, int length, int scaling)
static int16_t max_abs_value_w16(const int16_t *vector, int length)
static int unpack_frame(ILBCContext *s, const uint8_t *buf, int size)
static void get_lsp_poly(const int16_t *lsp, int32_t *f)
static void lsf2poly(int16_t *a, const int16_t *lsf)
static void vector_rmultiplication(int16_t *out, const int16_t *in, const int16_t *win, int length, int shift)
static void get_codebook(int16_t *cbvec, int16_t *mem, int16_t index, int16_t lMem, int16_t cbveclen)
#define STATE_SHORT_LEN_20MS
static void lsf_interpolate(int16_t *out, const int16_t *in1, const int16_t *in2, int16_t coef, int size)
#define ENH_BUFL_FILTEROVERHEAD
static void lsf_dequantization(int16_t *lsfdeq, int16_t *index, int16_t lpc_n)
static void lsf_check_stability(int16_t *lsf, int dim, int nb_vectors)
static int16_t get_size_in_bits(uint32_t n)
#define SPL_MUL_16_16(a, b)
#define STATE_SHORT_LEN_30MS
static void bw_expand(int16_t *out, const int16_t *in, const int16_t *coef, int length)
static void do_plc(int16_t *plc_residual, int16_t *plc_lpc, int16_t PLI, const int16_t *decresidual, const int16_t *lpc, int16_t inlag, ILBCContext *s)
static av_cold int ilbc_decode_init(AVCodecContext *avctx)
static int16_t gain_dequantization(int index, int max_in, int stage)
static void construct_vector(int16_t *decvector, const int16_t *index, const int16_t *gain_index, int16_t *mem, int16_t lMem, int16_t veclen)
static void vector_multiplication(int16_t *out, const int16_t *in, const int16_t *win, int length, int shift)
static void lsp_interpolate(int16_t *syntdenum, int16_t *weightdenum, const int16_t *lsfdeq, int16_t length, ILBCContext *s)
static void filter_arfq12(const int16_t *data_in, int16_t *data_out, const int16_t *coefficients, int coefficients_length, int data_length)
#define SPL_SHIFT_W32(x, c)
static void correlation(int32_t *corr, int32_t *ener, const int16_t *buffer, int16_t lag, int16_t blen, int16_t srange, int16_t scale)
static int32_t div_w32_w16(int32_t num, int16_t den)
static void decode_residual(ILBCContext *s, ILBCFrame *encbits, int16_t *decresidual, const int16_t *syntdenum)
static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static void filter_mafq12(const int16_t *in_ptr, int16_t *out_ptr, const int16_t *B, int16_t B_length, int16_t length)
#define SPL_MUL_16_16_RSFT(a, b, c)
static void index_conv(int16_t *index)
static int16_t norm_w32(int32_t a)
static void reverse_memcpy(int16_t *dest, const int16_t *source, int length)
static void lsf2lsp(const int16_t *lsf, int16_t *lsp, int order)
static int xcorr_coeff(const int16_t *target, const int16_t *regressor, int16_t subl, int16_t searchLen, int16_t offset, int16_t step)
static void hp_output(int16_t *signal, const int16_t *ba, int16_t *y, int16_t *x, int16_t len)
static void lsp_interpolate2polydec(int16_t *a, const int16_t *lsf1, const int16_t *lsf2, int coef, int length)
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static int shift(int a, int b)
static const uint8_t shift1[6]
static const uint8_t shift2[6]
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
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 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.
This structure stores compressed data.
int16_t syntMem[LPC_FILTERORDER]
int16_t old_syntdenum[NSUB_MAX *(LPC_FILTERORDER+1)]
int16_t prevResidual[NSUB_MAX *SUBL]
int16_t enh_buf[ENH_BUFL+ENH_BUFL_FILTEROVERHEAD]
int16_t lsfold[LPC_FILTERORDER]
int16_t decresidual[BLOCKL_MAX]
int16_t lsfdeq[LPC_FILTERORDER *LPC_N_MAX]
int16_t syntdenum[NSUB_MAX *(LPC_FILTERORDER+1)]
int16_t weightdenum[(LPC_FILTERORDER+1) *NSUB_MAX]
int16_t prev_lpc[LPC_FILTERORDER+1]
int16_t plc_lpc[LPC_FILTERORDER+1]
int16_t lsfdeqold[LPC_FILTERORDER]
int16_t enh_period[ENH_NBLOCKS_TOT]
int16_t plc_residual[BLOCKL_MAX+LPC_FILTERORDER]
int16_t lsf[LSF_NSPLIT *LPC_N_MAX]
int16_t idx[STATE_SHORT_LEN_30MS]
int16_t gain_index[CB_NSTAGES *(NASUB_MAX+1)]
int16_t cb_index[CB_NSTAGES *(NASUB_MAX+1)]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)