28 #define BITSTREAM_READER_LE
40 #define CNG_RANDOM_SEED 12345
42 typedef struct g723_1_context {
115 int temp, info_bits, i;
122 if (info_bits == 3) {
132 if (info_bits == 2) {
229 return (
ff_sqrt(val << 1) >> 1) & (~1);
240 return width -
av_log2(num) - 1;
243 #define normalize_bits_int16(num) normalize_bits(num, 15)
244 #define normalize_bits_int32(num) normalize_bits(num, 31)
254 for (i = 0; i <
length; i++)
255 max |=
FFABS(vector[i]);
258 bits=
FFMAX(bits, 0);
260 for (i = 0; i <
length; i++)
261 dst[i] = vector[i] << bits >> 3;
275 uint8_t *lsp_index,
int bad_frame)
278 int i, j,
temp, stable;
287 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
304 temp = ((prev_lsp[i] -
dc_lsp[i]) * pred + (1 << 14)) >> 15;
309 cur_lsp[0] =
FFMAX(cur_lsp[0], 0x180);
310 cur_lsp[LPC_ORDER - 1] =
FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
314 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
317 cur_lsp[j - 1] -=
temp;
323 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
333 memcpy(cur_lsp, prev_lsp, LPC_ORDER *
sizeof(*cur_lsp));
342 #define MULL2(a, b) \
358 int index = (lpc[j] >> 7) & 0x1FF;
359 int offset = lpc[j] & 0x7f;
362 ((offset << 8) + 0x80) << 1;
364 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
373 f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
374 f1[2] = lpc[0] * lpc[2] + (2 << 28);
377 f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
378 f2[2] = lpc[1] * lpc[3] + (2 << 28);
384 for (i = 2; i < LPC_ORDER / 2; i++) {
385 f1[i + 1] = f1[i - 1] +
MULL2(f1[i], lpc[2 * i]);
386 f2[i + 1] = f2[i - 1] +
MULL2(f2[i], lpc[2 * i + 1]);
388 for (j = i; j >= 2; j--) {
389 f1[j] =
MULL2(f1[j - 1], lpc[2 * i]) +
390 (f1[j] >> 1) + (f1[j - 2] >> 1);
391 f2[j] =
MULL2(f2[j - 1], lpc[2 * i + 1]) +
392 (f2[j] >> 1) + (f2[j - 2] >> 1);
397 f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
398 f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
402 for (i = 0; i < LPC_ORDER / 2; i++) {
403 int64_t ff1 = f1[i + 1] + f1[i];
404 int64_t ff2 = f2[i + 1] - f2[i];
406 lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
407 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
423 int16_t *lpc_ptr = lpc;
450 for (j = 0; j < SUBFRAME_LEN - i; j++)
451 buf[i + j] += vector[j];
465 enum Rate cur_rate,
int pitch_lag,
int index)
502 for (i = 0; i < 8; i += 2) {
503 offset = ((cb_pos & 7) << 3) + cb_shift + i;
504 vector[
offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
516 vector[i] += beta * vector[i - lag] >> 15;
524 static void get_residual(int16_t *residual, int16_t *prev_excitation,
int lag)
529 residual[0] = prev_excitation[
offset];
530 residual[1] = prev_excitation[offset + 1];
534 residual[i] = prev_excitation[offset + (i - 2) % lag];
540 return av_sat_add32(sum, sum);
551 const int16_t *cb_ptr;
552 int lag = pitch_lag + subfrm->
ad_cb_lag - 1;
569 vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
584 int pitch_lag,
int length,
int dir)
586 int limit, ccr, lag = 0;
593 limit = pitch_lag + 3;
595 for (i = pitch_lag - 3; i <= limit; i++) {
598 if (ccr > *ccr_max) {
617 int tgt_eng,
int ccr,
int res_eng)
624 temp1 = tgt_eng * res_eng >> 1;
625 temp2 = ccr * ccr << 1;
628 if (ccr >= res_eng) {
631 ppf->
opt_gain = (ccr << 15) / res_eng *
635 temp1 = (tgt_eng << 15) + (ccr * ppf->
opt_gain << 1);
637 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
639 if (tgt_eng >= pf_residual << 1) {
642 temp1 = (tgt_eng << 14) / pf_residual;
679 int energy[5] = {0, 0, 0, 0, 0};
681 int fwd_lag =
autocorr_max(buf, offset, &energy[1], pitch_lag,
683 int back_lag =
autocorr_max(buf, offset, &energy[3], pitch_lag,
691 if (!back_lag && !fwd_lag)
707 for (i = 0; i < 5; i++)
708 temp1 =
FFMAX(energy[i], temp1);
711 for (i = 0; i < 5; i++)
712 energy[i] = (energy[i] << scale) >> 16;
714 if (fwd_lag && !back_lag) {
717 }
else if (!fwd_lag) {
726 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
727 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
728 if (temp1 >= temp2) {
749 int *exc_eng,
int *
scale)
761 index =
autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
762 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
766 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
772 best_eng =
dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
773 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
775 temp = best_eng * *exc_eng >> 3;
777 if (temp < ccr * ccr) {
793 int gain,
int *rseed)
799 for (i = 0; i < lag; i++)
800 out[i] = vector_ptr[i - lag] * 3 >> 2;
805 *rseed = *rseed * 521 + 259;
806 out[i] = gain * *rseed >> 15;
808 memset(buf, 0, (FRAME_LEN +
PITCH_MAX) *
sizeof(*buf));
821 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
824 int res_shift = 16 & ~-(width);\
825 int in_shift = 16 - res_shift;\
827 for (m = 0; m < SUBFRAME_LEN; m++) {\
829 for (n = 1; n <= LPC_ORDER; n++) {\
830 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
831 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
834 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
835 (1 << 15)) >> res_shift;\
854 int temp = buf[i] >> 2;
856 denom = av_sat_dadd32(denom, temp);
862 num = num << bits1 >> 1;
865 bits2 = 5 + bits1 -
bits2;
866 bits2 =
FFMAX(0, bits2);
868 gain = (num >> 1) / (denom >> 16);
876 buf[i] = av_clip_int16((buf[i] * (p->
pf_gain + (p->
pf_gain >> 4)) +
890 int16_t *
buf, int16_t *dst)
906 iir_filter(filter_coef[0], filter_coef[1], buf + i,
907 filter_signal + i, 1);
929 temp = auto_corr[1] >> 16;
931 temp = (auto_corr[0] >> 2) / temp;
938 dst[j] = av_sat_dadd32(signal_ptr[j],
939 (signal_ptr[j - 1] >> 16) * temp) >> 16;
943 temp = 2 * scale + 4;
945 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
947 energy = auto_corr[1] >>
temp;
961 else if (gain < 0x20)
962 return gain - 8 << 7;
964 return gain - 20 << 8;
969 *state = (*state * 521 + 259) & 0xFFFF;
970 return (*state & 0x7FFF) * base >> 15;
975 int i,
shift, seg, seg2,
t,
val, val_add, x,
y;
994 seg2 =
FFMIN(seg, 3);
998 for (i = 0; i <
shift; i++) {
999 t = seg * 32 + (val << seg2);
1008 t = seg * 32 + (val << seg2);
1011 t = seg * 32 + (val + 1 << seg2);
1013 val = (seg2 - 1 << 4) + val;
1017 t = seg * 32 + (val - 1 << seg2);
1019 val = (seg2 - 1 << 4) + val;
1033 int16_t *vector_ptr;
1045 for (i = 0; i < SUBFRAMES / 2; i++) {
1050 for (j = 0; j < 11; j++) {
1051 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
1061 for (j = 0; j <
pulses[i]; j++, idx++) {
1064 pos[idx] = tmp[idx2] * 2 + off[i];
1065 tmp[idx2] = tmp[--
t];
1083 t |=
FFABS(vector_ptr[j]);
1084 t =
FFMIN(t, 0x7FFF);
1094 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1095 t = vector_ptr[j] << -
shift;
1100 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1101 t = vector_ptr[j] >>
shift;
1108 for (j = 0; j < 11; j++)
1109 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
1110 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30;
1113 if (shift * 2 + 3 >= 0)
1114 c >>= shift * 2 + 3;
1116 c <<= -(shift * 2 + 3);
1117 c = (av_clipl_int32(sum << 1) -
c) * 2979LL >> 15;
1119 delta = b0 * b0 * 2 -
c;
1134 x = av_clip(x, -10000, 10000);
1136 for (j = 0; j < 11; j++) {
1137 idx = (i / 2) * 11 + j;
1138 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
1139 (x * signs[idx] >> 15));
1143 memcpy(vector_ptr +
PITCH_MAX, vector_ptr,
1144 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
1145 vector_ptr += SUBFRAME_LEN * 2;
1153 int *got_frame_ptr,
AVPacket *avpkt)
1158 int buf_size = avpkt->
size;
1159 int dec_mode = buf[0] & 3;
1166 int bad_frame = 0, i, j,
ret;
1167 int16_t *audio = p->
audio;
1172 "Expected %d bytes, got %d - skipping packet\n",
1190 out = (int16_t *)frame->
data[0];
1221 int v = av_clip_int16(vector_ptr[j] << 1);
1222 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
1242 vector_ptr + i + ppf[j].
index,
1261 memset(frame->
data[0], 0,
1307 out[i] = av_clip_int16(p->
audio[LPC_ORDER + i] << 1);
1315 #define OFFSET(x) offsetof(G723_1_Context, x)
1316 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1320 { .i64 = 1 }, 0, 1,
AD },
1341 .priv_class = &g723_1dec_class,
1344 #if CONFIG_G723_1_ENCODER
1345 #define BITSTREAM_WRITER_LE
1364 }
else if (avctx->
bit_rate == 5300) {
1369 "Bitrate not supported, use 6.3k\n");
1385 static void highpass_filter(int16_t *
buf, int16_t *fir,
int *iir)
1389 *iir = (buf[i] << 15) + ((-*fir) << 15) +
MULL2(*iir, 0x7f00);
1391 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
1401 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
1420 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
1425 memset(autocorr + 1, 0,
LPC_ORDER *
sizeof(int16_t));
1430 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
1443 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
1446 int16_t partial_corr;
1449 memset(lpc, 0,
LPC_ORDER *
sizeof(int16_t));
1454 for (j = 0; j < i; j++)
1455 temp -= lpc[j] * autocorr[i - j - 1];
1456 temp = ((autocorr[i] << 13) + temp) << 3;
1458 if (
FFABS(temp) >= (error << 16))
1461 partial_corr = temp / (error << 1);
1463 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
1467 temp =
MULL2(temp, partial_corr);
1468 error = av_clipl_int32((int64_t)(error << 16) - temp +
1471 memcpy(vector, lpc, i *
sizeof(int16_t));
1472 for (j = 0; j < i; j++) {
1473 temp = partial_corr * vector[i - j - 1] << 1;
1474 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
1487 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
1490 int16_t *autocorr_ptr = autocorr;
1491 int16_t *lpc_ptr = lpc;
1495 comp_autocorr(buf + i, autocorr_ptr);
1496 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
1503 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
1518 f[0] = f[1] = 1 << 25;
1521 for (i = 0; i < LPC_ORDER / 2; i++) {
1523 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
1525 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
1530 f[LPC_ORDER + 1] >>= 1;
1534 for (i = 1; i < LPC_ORDER + 2; i++)
1539 for (i = 0; i < LPC_ORDER + 2; i++)
1540 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
1548 for (i = 0; i <= LPC_ORDER / 2; i++)
1549 temp += f[2 * i] *
cos_tab[0];
1550 prev_val = av_clipl_int32(temp << 1);
1555 for (j = 0; j <= LPC_ORDER / 2; j++)
1557 cur_val = av_clipl_int32(temp << 1);
1560 if ((cur_val ^ prev_val) < 0) {
1561 int abs_cur =
FFABS(cur_val);
1562 int abs_prev =
FFABS(prev_val);
1563 int sum = abs_cur + abs_prev;
1567 abs_prev = abs_prev << shift >> 8;
1568 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
1570 if (count == LPC_ORDER)
1578 for (j = 0; j <= LPC_ORDER / 2; j++){
1579 temp += f[LPC_ORDER - 2 * j + p] *
1582 cur_val = av_clipl_int32(temp<<1);
1587 if (count != LPC_ORDER)
1588 memcpy(lsp, prev_lsp, LPC_ORDER *
sizeof(int16_t));
1598 #define get_index(num, offset, size) \
1600 int error, max = -1;\
1603 for (i = 0; i < LSP_CB_SIZE; i++) {\
1604 for (j = 0; j < size; j++){\
1605 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
1608 error = dot_product(lsp + (offset), temp, size) << 1;\
1609 error -= dot_product(lsp_band##num[i], temp, size);\
1612 lsp_index[num] = i;\
1623 static void lsp_quantize(
uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
1630 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
1635 min =
FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
1637 weight[i] = (1 << 20) / min;
1639 weight[i] = INT16_MAX;
1645 max =
FFMAX(weight[i], max);
1649 weight[i] <<=
shift;
1655 (((prev_lsp[i] -
dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
1669 static void perceptual_filter(
G723_1_Context *p, int16_t *flt_coef,
1670 int16_t *unq_lpc, int16_t *buf)
1676 memcpy(vector, p->
fir_mem,
sizeof(int16_t) * LPC_ORDER);
1677 memcpy(vector + LPC_ORDER, buf + LPC_ORDER,
sizeof(int16_t) *
FRAME_LEN);
1683 flt_coef[k + 2 * l +
LPC_ORDER] = (unq_lpc[k + l] *
1687 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
1691 memcpy(p->
iir_mem, buf + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1692 memcpy(p->
fir_mem, vector + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1701 static int estimate_pitch(int16_t *buf,
int start)
1704 int max_ccr = 0x4000;
1705 int max_eng = 0x7fff;
1709 int ccr, eng, orig_eng, ccr_eng, exp;
1716 for (i = PITCH_MIN; i <=
PITCH_MAX - 3; i++) {
1728 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
1732 ccr = ccr << temp >> 16;
1736 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
1746 if (exp + 1 < max_exp)
1750 if (exp + 1 == max_exp)
1751 temp = max_ccr >> 1;
1754 ccr_eng = ccr * max_eng;
1755 diff = ccr_eng - eng *
temp;
1756 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
1774 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag,
HFParam *hf)
1776 int ccr, eng, max_ccr, max_eng;
1781 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
1793 for (i = 0; i < 15; i++)
1797 for (i = 0; i < 15; i++) {
1798 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
1807 for (i = 0; i <= 6; i++) {
1808 eng = energy[i << 1];
1809 ccr = energy[(i << 1) + 1];
1814 ccr = (ccr * ccr + (1 << 14)) >> 15;
1815 diff = ccr * max_eng - eng * max_ccr;
1823 if (hf->
index == -1) {
1824 hf->
index = pitch_lag;
1828 eng = energy[14] * max_eng;
1829 eng = (eng >> 2) + (eng >> 3);
1830 ccr = energy[(hf->
index << 1) + 1] * energy[(hf->
index << 1) + 1];
1832 eng = energy[(hf->
index << 1) + 1];
1837 hf->
gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
1839 hf->
index += pitch_lag - 3;
1847 static void harmonic_filter(
HFParam *hf,
const int16_t *
src, int16_t *dest)
1853 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
1857 static void harmonic_noise_sub(
HFParam *hf,
const int16_t *src, int16_t *dest)
1862 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
1877 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
1878 int16_t *perf_fir, int16_t *perf_iir,
1879 const int16_t *src, int16_t *dest,
int scale)
1887 memcpy(buf_16, perf_fir,
sizeof(int16_t) * LPC_ORDER);
1888 memcpy(dest - LPC_ORDER, perf_iir,
sizeof(int16_t) * LPC_ORDER);
1893 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
1895 buf[i] = (src[i] << 15) + (temp << 3);
1896 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
1900 int64_t fir = 0, iir = 0;
1902 fir -= perf_lpc[j - 1] * bptr_16[i - j];
1903 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
1905 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
1908 memcpy(perf_fir, buf_16 + SUBFRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1909 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
1910 sizeof(int16_t) * LPC_ORDER);
1920 int16_t *impulse_resp,
const int16_t *buf,
1930 int pitch_lag = p->
pitch_lag[index >> 1];
1933 int odd_frame = index & 1;
1934 int iter = 3 + odd_frame;
1938 int i, j, k, l, max;
1948 for (i = 0; i < iter; i++) {
1953 for (k = 0; k <= j; k++)
1954 temp += residual[
PITCH_ORDER - 1 + k] * impulse_resp[j - k];
1955 flt_buf[
PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
1960 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
1962 temp = (flt_buf[j + 1][k - 1] << 15) +
1963 residual[j] * impulse_resp[k];
1964 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
1971 ccr_buf[count++] = av_clipl_int32(temp << 1);
1976 ccr_buf[count++] =
dot_product(flt_buf[j], flt_buf[j],
1981 for (k = 0; k < j; k++) {
1983 ccr_buf[count++] = av_clipl_int32(temp<<2);
1990 for (i = 0; i < 20 * iter; i++)
1995 for (i = 0; i < 20 * iter; i++){
1996 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
2001 for (i = 0; i < iter; i++) {
2003 if (!odd_frame && pitch_lag + i - 1 >=
SUBFRAME_LEN - 2 ||
2009 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
2011 for (l = 0; l < 20; l++)
2012 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
2013 temp = av_clipl_int32(temp);
2024 pitch_lag += acb_lag - 1;
2039 static void sub_acb_contrib(
const int16_t *residual,
const int16_t *impulse_resp,
2045 int64_t
temp = buf[i] << 14;
2046 for (j = 0; j <= i; j++)
2047 temp -= residual[j] * impulse_resp[i - j];
2049 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
2059 static void get_fcb_param(
FCBParam *optim, int16_t *impulse_resp,
2060 int16_t *buf,
int pulse_cnt,
int pitch_lag)
2069 int amp, err, max, max_amp_index,
min,
scale, i, j, k, l;
2074 memcpy(impulse_r, impulse_resp,
sizeof(int16_t) *
SUBFRAME_LEN);
2076 if (pitch_lag < SUBFRAME_LEN - 2) {
2082 temp_corr[i] = impulse_r[i] >> 1;
2085 temp =
dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
2088 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2091 temp =
dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
2092 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2098 temp =
dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
2100 ccr1[i] = temp >> -
scale;
2102 ccr1[i] = av_clipl_int32(temp << scale);
2110 temp =
FFABS(ccr1[j]);
2121 for (j = max_amp_index; j >= 2; j--) {
2123 impulse_corr[0] << 1);
2124 temp =
FFABS(temp - amp);
2133 for (j = 1; j < 5; j++) {
2138 param.
amp_index = max_amp_index + j - 2;
2144 for (k = 1; k < pulse_cnt; k++) {
2150 temp = av_clipl_int32((int64_t)temp *
2153 temp =
FFABS(ccr2[l]);
2166 memset(temp_corr, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2168 for (k = 0; k < pulse_cnt; k++)
2171 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
2173 for (l = 0; l <= k; l++) {
2174 int prod = av_clipl_int32((int64_t)temp_corr[l] *
2175 impulse_r[k - l] << 1);
2176 temp = av_clipl_int32(temp + prod);
2178 temp_corr[k] = temp << 2 >> 16;
2185 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
2186 err = av_clipl_int32(err - prod);
2187 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
2188 err = av_clipl_int32(err + prod);
2192 if (err < optim->min_err) {
2198 for (k = 0; k < pulse_cnt; k++) {
2214 int16_t *buf,
int pulse_cnt)
2223 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
2247 int16_t *buf,
int index)
2254 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
SUBFRAME_LEN);
2257 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
2263 for (i = 0; i < pulse_cnt; i++)
2266 pack_fcb_param(&p->
subframe[index], &optim, buf, pulse_cnt);
2281 int info_bits, i,
temp;
2339 const AVFrame *frame,
int *got_packet_ptr)
2348 int16_t *
in = (
const int16_t *)frame->
data[0];
2358 comp_lpc_coeff(vector, unq_lpc);
2365 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
2369 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) *
FRAME_LEN);
2371 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
2373 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2375 memcpy(vector + PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2379 p->
pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
2380 p->
pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
2383 comp_harmonic_coeff(vector + i, p->
pitch_lag[j >> 1], hf + j);
2386 memcpy(vector + PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2387 memcpy(p->
prev_weight_sig, vector + FRAME_LEN,
sizeof(int16_t) * PITCH_MAX);
2390 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
2395 memcpy(p->
prev_lsp, cur_lsp,
sizeof(int16_t) * LPC_ORDER);
2408 memset(zero, 0,
sizeof(int16_t) * LPC_ORDER);
2409 memset(vector, 0,
sizeof(int16_t) * PITCH_MAX);
2410 memset(flt_in, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2412 flt_in[0] = 1 << 13;
2413 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2414 zero, zero, flt_in, vector + PITCH_MAX, 1);
2415 harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
2419 memcpy(fir, p->
perf_fir_mem,
sizeof(int16_t) * LPC_ORDER);
2420 memcpy(iir, p->
perf_iir_mem,
sizeof(int16_t) * LPC_ORDER);
2422 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2423 fir, iir, flt_in, vector + PITCH_MAX, 0);
2424 memcpy(vector, p->
harmonic_mem,
sizeof(int16_t) * PITCH_MAX);
2425 harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
2427 acb_search(p, residual, impulse_resp, in, i);
2430 sub_acb_contrib(residual, impulse_resp, in);
2432 fcb_search(p, impulse_resp, in, i);
2439 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
2441 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
2443 sizeof(int16_t) * SUBFRAME_LEN);
2446 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2448 in, vector + PITCH_MAX, 0);
2450 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
2451 memcpy(p->
harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
2452 sizeof(int16_t) * SUBFRAME_LEN);
2461 *got_packet_ptr = 1;
2462 avpkt->
size = pack_bitstream(p, avpkt->
data, avpkt->
size);
2472 .
init = g723_1_encode_init,
2473 .encode2 = g723_1_encode_frame,