28 #define BITSTREAM_READER_LE
41 #define CNG_RANDOM_SEED 12345
43 typedef struct g723_1_context {
120 int temp, info_bits, i;
127 if (info_bits == 3) {
137 if (info_bits == 2) {
234 return (
ff_sqrt(val << 1) >> 1) & (~1);
245 return width -
av_log2(num) - 1;
248 #define normalize_bits_int16(num) normalize_bits(num, 15)
249 #define normalize_bits_int32(num) normalize_bits(num, 31)
259 for (i = 0; i < length; i++)
260 max |=
FFABS(vector[i]);
263 bits=
FFMAX(bits, 0);
265 for (i = 0; i < length; i++)
266 dst[i] = vector[i] << bits >> 3;
280 uint8_t *lsp_index,
int bad_frame)
283 int i, j,
temp, stable;
292 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
309 temp = ((prev_lsp[i] -
dc_lsp[i]) * pred + (1 << 14)) >> 15;
314 cur_lsp[0] =
FFMAX(cur_lsp[0], 0x180);
315 cur_lsp[LPC_ORDER - 1] =
FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
319 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
322 cur_lsp[j - 1] -=
temp;
328 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
338 memcpy(cur_lsp, prev_lsp, LPC_ORDER *
sizeof(*cur_lsp));
347 #define MULL2(a, b) \
363 int index = (lpc[j] >> 7) & 0x1FF;
364 int offset = lpc[j] & 0x7f;
367 ((offset << 8) + 0x80) << 1;
369 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
378 f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
379 f1[2] = lpc[0] * lpc[2] + (2 << 28);
382 f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
383 f2[2] = lpc[1] * lpc[3] + (2 << 28);
389 for (i = 2; i < LPC_ORDER / 2; i++) {
390 f1[i + 1] = f1[i - 1] +
MULL2(f1[i], lpc[2 * i]);
391 f2[i + 1] = f2[i - 1] +
MULL2(f2[i], lpc[2 * i + 1]);
393 for (j = i; j >= 2; j--) {
394 f1[j] =
MULL2(f1[j - 1], lpc[2 * i]) +
395 (f1[j] >> 1) + (f1[j - 2] >> 1);
396 f2[j] =
MULL2(f2[j - 1], lpc[2 * i + 1]) +
397 (f2[j] >> 1) + (f2[j - 2] >> 1);
402 f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
403 f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
407 for (i = 0; i < LPC_ORDER / 2; i++) {
408 int64_t ff1 = f1[i + 1] + f1[i];
409 int64_t ff2 = f2[i + 1] - f2[i];
411 lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
412 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
428 int16_t *lpc_ptr = lpc;
455 for (j = 0; j < SUBFRAME_LEN - i; j++)
456 buf[i + j] += vector[j];
470 enum Rate cur_rate,
int pitch_lag,
int index)
507 for (i = 0; i < 8; i += 2) {
508 offset = ((cb_pos & 7) << 3) + cb_shift + i;
509 vector[
offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
521 vector[i] += beta * vector[i - lag] >> 15;
529 static void get_residual(int16_t *residual, int16_t *prev_excitation,
int lag)
534 residual[0] = prev_excitation[
offset];
535 residual[1] = prev_excitation[offset + 1];
539 residual[i] = prev_excitation[offset + (i - 2) % lag];
545 return av_sat_add32(sum, sum);
556 const int16_t *cb_ptr;
557 int lag = pitch_lag + subfrm->
ad_cb_lag - 1;
574 vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
589 int pitch_lag,
int length,
int dir)
591 int limit, ccr, lag = 0;
598 limit = pitch_lag + 3;
600 for (i = pitch_lag - 3; i <= limit; i++) {
603 if (ccr > *ccr_max) {
622 int tgt_eng,
int ccr,
int res_eng)
629 temp1 = tgt_eng * res_eng >> 1;
630 temp2 = ccr * ccr << 1;
633 if (ccr >= res_eng) {
636 ppf->
opt_gain = (ccr << 15) / res_eng *
640 temp1 = (tgt_eng << 15) + (ccr * ppf->
opt_gain << 1);
642 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
644 if (tgt_eng >= pf_residual << 1) {
647 temp1 = (tgt_eng << 14) / pf_residual;
684 int energy[5] = {0, 0, 0, 0, 0};
686 int fwd_lag =
autocorr_max(buf, offset, &energy[1], pitch_lag,
688 int back_lag =
autocorr_max(buf, offset, &energy[3], pitch_lag,
696 if (!back_lag && !fwd_lag)
712 for (i = 0; i < 5; i++)
713 temp1 =
FFMAX(energy[i], temp1);
716 for (i = 0; i < 5; i++)
717 energy[i] = (energy[i] << scale) >> 16;
719 if (fwd_lag && !back_lag) {
722 }
else if (!fwd_lag) {
731 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
732 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
733 if (temp1 >= temp2) {
754 int *exc_eng,
int *
scale)
766 index =
autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
767 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
771 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
777 best_eng =
dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
778 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
780 temp = best_eng * *exc_eng >> 3;
782 if (temp < ccr * ccr) {
798 int gain,
int *rseed)
804 for (i = 0; i < lag; i++)
805 out[i] = vector_ptr[i - lag] * 3 >> 2;
810 *rseed = *rseed * 521 + 259;
811 out[i] = gain * *rseed >> 15;
813 memset(buf, 0, (FRAME_LEN +
PITCH_MAX) *
sizeof(*buf));
826 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
829 int res_shift = 16 & ~-(width);\
830 int in_shift = 16 - res_shift;\
832 for (m = 0; m < SUBFRAME_LEN; m++) {\
834 for (n = 1; n <= LPC_ORDER; n++) {\
835 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
836 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
839 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
840 (1 << 15)) >> res_shift;\
859 int temp = buf[i] >> 2;
861 denom = av_sat_dadd32(denom, temp);
867 num = num << bits1 >> 1;
870 bits2 = 5 + bits1 -
bits2;
871 bits2 =
FFMAX(0, bits2);
873 gain = (num >> 1) / (denom >> 16);
881 buf[i] = av_clip_int16((buf[i] * (p->
pf_gain + (p->
pf_gain >> 4)) +
895 int16_t *buf, int16_t *
dst)
911 iir_filter(filter_coef[0], filter_coef[1], buf + i,
912 filter_signal + i, 1);
934 temp = auto_corr[1] >> 16;
936 temp = (auto_corr[0] >> 2) / temp;
943 dst[j] = av_sat_dadd32(signal_ptr[j],
944 (signal_ptr[j - 1] >> 16) * temp) >> 16;
948 temp = 2 * scale + 4;
950 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
952 energy = auto_corr[1] >>
temp;
966 else if (gain < 0x20)
967 return gain - 8 << 7;
969 return gain - 20 << 8;
974 *state = (*state * 521 + 259) & 0xFFFF;
975 return (*state & 0x7FFF) * base >> 15;
980 int i,
shift, seg, seg2,
t, val, val_add, x, y;
999 seg2 =
FFMIN(seg, 3);
1003 for (i = 0; i <
shift; i++) {
1004 t = seg * 32 + (val << seg2);
1013 t = seg * 32 + (val << seg2);
1016 t = seg * 32 + (val + 1 << seg2);
1018 val = (seg2 - 1 << 4) + val;
1022 t = seg * 32 + (val - 1 << seg2);
1024 val = (seg2 - 1 << 4) + val;
1038 int16_t *vector_ptr;
1050 for (i = 0; i < SUBFRAMES / 2; i++) {
1055 for (j = 0; j < 11; j++) {
1056 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
1066 for (j = 0; j <
pulses[i]; j++, idx++) {
1069 pos[idx] = tmp[idx2] * 2 + off[i];
1070 tmp[idx2] = tmp[--
t];
1088 t |=
FFABS(vector_ptr[j]);
1089 t =
FFMIN(t, 0x7FFF);
1099 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1100 t = vector_ptr[j] << -
shift;
1105 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1106 t = vector_ptr[j] >>
shift;
1113 for (j = 0; j < 11; j++)
1114 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
1115 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30;
1118 if (shift * 2 + 3 >= 0)
1119 c >>= shift * 2 + 3;
1121 c <<= -(shift * 2 + 3);
1122 c = (av_clipl_int32(sum << 1) -
c) * 2979LL >> 15;
1124 delta = b0 * b0 * 2 -
c;
1139 x = av_clip(x, -10000, 10000);
1141 for (j = 0; j < 11; j++) {
1142 idx = (i / 2) * 11 + j;
1143 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
1144 (x * signs[idx] >> 15));
1148 memcpy(vector_ptr +
PITCH_MAX, vector_ptr,
1149 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
1150 vector_ptr += SUBFRAME_LEN * 2;
1158 int *got_frame_ptr,
AVPacket *avpkt)
1162 int buf_size = avpkt->
size;
1163 int dec_mode = buf[0] & 3;
1170 int bad_frame = 0, i, j, ret;
1171 int16_t *audio = p->
audio;
1176 "Expected %d bytes, got %d - skipping packet\n",
1227 int v = av_clip_int16(vector_ptr[j] << 1);
1228 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
1248 vector_ptr + i + ppf[j].
index,
1313 out[i] = av_clip_int16(p->
audio[LPC_ORDER + i] << 1);
1322 #define OFFSET(x) offsetof(G723_1_Context, x)
1323 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1327 { .i64 = 1 }, 0, 1,
AD },
1351 #if CONFIG_G723_1_ENCODER
1352 #define BITSTREAM_WRITER_LE
1371 }
else if (avctx->
bit_rate == 5300) {
1376 "Bitrate not supported, use 6.3k\n");
1392 static void highpass_filter(int16_t *buf, int16_t *fir,
int *iir)
1396 *iir = (buf[i] << 15) + ((-*fir) << 15) +
MULL2(*iir, 0x7f00);
1398 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
1408 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
1427 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
1432 memset(autocorr + 1, 0,
LPC_ORDER *
sizeof(int16_t));
1437 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
1450 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
1453 int16_t partial_corr;
1456 memset(lpc, 0,
LPC_ORDER *
sizeof(int16_t));
1461 for (j = 0; j < i; j++)
1462 temp -= lpc[j] * autocorr[i - j - 1];
1463 temp = ((autocorr[i] << 13) + temp) << 3;
1465 if (
FFABS(temp) >= (error << 16))
1468 partial_corr = temp / (error << 1);
1470 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
1474 temp =
MULL2(temp, partial_corr);
1475 error = av_clipl_int32((int64_t)(error << 16) - temp +
1478 memcpy(vector, lpc, i *
sizeof(int16_t));
1479 for (j = 0; j < i; j++) {
1480 temp = partial_corr * vector[i - j - 1] << 1;
1481 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
1494 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
1496 int16_t autocorr[(LPC_ORDER + 1) *
SUBFRAMES];
1497 int16_t *autocorr_ptr = autocorr;
1498 int16_t *lpc_ptr = lpc;
1502 comp_autocorr(buf + i, autocorr_ptr);
1503 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
1506 autocorr_ptr += LPC_ORDER + 1;
1510 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
1512 int f[LPC_ORDER + 2];
1516 int max,
shift, cur_val, prev_val, count, p;
1525 f[0] = f[1] = 1 << 25;
1528 for (i = 0; i < LPC_ORDER / 2; i++) {
1530 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
1532 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
1537 f[LPC_ORDER + 1] >>= 1;
1541 for (i = 1; i < LPC_ORDER + 2; i++)
1546 for (i = 0; i < LPC_ORDER + 2; i++)
1547 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
1555 for (i = 0; i <= LPC_ORDER / 2; i++)
1556 temp += f[2 * i] *
cos_tab[0];
1557 prev_val = av_clipl_int32(temp << 1);
1562 for (j = 0; j <= LPC_ORDER / 2; j++)
1564 cur_val = av_clipl_int32(temp << 1);
1567 if ((cur_val ^ prev_val) < 0) {
1568 int abs_cur =
FFABS(cur_val);
1569 int abs_prev =
FFABS(prev_val);
1570 int sum = abs_cur + abs_prev;
1574 abs_prev = abs_prev << shift >> 8;
1575 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
1577 if (count == LPC_ORDER)
1585 for (j = 0; j <= LPC_ORDER / 2; j++){
1586 temp += f[LPC_ORDER - 2 * j + p] *
1589 cur_val = av_clipl_int32(temp<<1);
1594 if (count != LPC_ORDER)
1595 memcpy(lsp, prev_lsp, LPC_ORDER *
sizeof(int16_t));
1605 #define get_index(num, offset, size) \
1607 int error, max = -1;\
1610 for (i = 0; i < LSP_CB_SIZE; i++) {\
1611 for (j = 0; j < size; j++){\
1612 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
1615 error = dot_product(lsp + (offset), temp, size) << 1;\
1616 error -= dot_product(lsp_band##num[i], temp, size);\
1619 lsp_index[num] = i;\
1630 static void lsp_quantize(
uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
1637 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
1638 weight[LPC_ORDER - 1] = (1 << 20) /
1639 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
1641 for (i = 1; i < LPC_ORDER - 1; i++) {
1642 min =
FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
1644 weight[i] = (1 << 20) / min;
1646 weight[i] = INT16_MAX;
1652 max =
FFMAX(weight[i], max);
1656 weight[i] <<=
shift;
1662 (((prev_lsp[i] -
dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
1676 static void perceptual_filter(
G723_1_Context *p, int16_t *flt_coef,
1677 int16_t *unq_lpc, int16_t *buf)
1682 memcpy(buf, p->
iir_mem,
sizeof(int16_t) * LPC_ORDER);
1683 memcpy(vector, p->
fir_mem,
sizeof(int16_t) * LPC_ORDER);
1684 memcpy(vector + LPC_ORDER, buf + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
1690 flt_coef[k + 2 * l +
LPC_ORDER] = (unq_lpc[k + l] *
1694 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
1698 memcpy(p->
iir_mem, buf + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1699 memcpy(p->
fir_mem, vector + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1708 static int estimate_pitch(int16_t *buf,
int start)
1711 int max_ccr = 0x4000;
1712 int max_eng = 0x7fff;
1716 int ccr, eng, orig_eng, ccr_eng, exp;
1723 for (i = PITCH_MIN; i <=
PITCH_MAX - 3; i++) {
1735 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
1739 ccr = ccr << temp >> 16;
1743 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
1753 if (exp + 1 < max_exp)
1757 if (exp + 1 == max_exp)
1758 temp = max_ccr >> 1;
1761 ccr_eng = ccr * max_eng;
1762 diff = ccr_eng - eng *
temp;
1763 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
1781 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag,
HFParam *hf)
1783 int ccr, eng, max_ccr, max_eng;
1788 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
1800 for (i = 0; i < 15; i++)
1804 for (i = 0; i < 15; i++) {
1805 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
1814 for (i = 0; i <= 6; i++) {
1815 eng = energy[i << 1];
1816 ccr = energy[(i << 1) + 1];
1821 ccr = (ccr * ccr + (1 << 14)) >> 15;
1822 diff = ccr * max_eng - eng * max_ccr;
1830 if (hf->
index == -1) {
1831 hf->
index = pitch_lag;
1835 eng = energy[14] * max_eng;
1836 eng = (eng >> 2) + (eng >> 3);
1837 ccr = energy[(hf->
index << 1) + 1] * energy[(hf->
index << 1) + 1];
1839 eng = energy[(hf->
index << 1) + 1];
1844 hf->
gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
1846 hf->
index += pitch_lag - 3;
1854 static void harmonic_filter(
HFParam *hf,
const int16_t *src, int16_t *dest)
1859 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1860 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
1864 static void harmonic_noise_sub(
HFParam *hf,
const int16_t *src, int16_t *dest)
1868 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1869 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
1884 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
1885 int16_t *perf_fir, int16_t *perf_iir,
1886 const int16_t *src, int16_t *dest,
int scale)
1889 int16_t buf_16[SUBFRAME_LEN +
LPC_ORDER];
1894 memcpy(buf_16, perf_fir,
sizeof(int16_t) * LPC_ORDER);
1895 memcpy(dest - LPC_ORDER, perf_iir,
sizeof(int16_t) * LPC_ORDER);
1900 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
1902 buf[i] = (src[i] << 15) + (temp << 3);
1903 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
1907 int64_t fir = 0, iir = 0;
1909 fir -= perf_lpc[j - 1] * bptr_16[i - j];
1910 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
1912 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
1915 memcpy(perf_fir, buf_16 + SUBFRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1916 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
1917 sizeof(int16_t) * LPC_ORDER);
1927 int16_t *impulse_resp,
const int16_t *buf,
1937 int pitch_lag = p->
pitch_lag[index >> 1];
1940 int odd_frame = index & 1;
1941 int iter = 3 + odd_frame;
1945 int i, j, k, l, max;
1949 if (pitch_lag == PITCH_MIN)
1955 for (i = 0; i < iter; i++) {
1960 for (k = 0; k <= j; k++)
1961 temp += residual[
PITCH_ORDER - 1 + k] * impulse_resp[j - k];
1962 flt_buf[
PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
1967 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
1969 temp = (flt_buf[j + 1][k - 1] << 15) +
1970 residual[j] * impulse_resp[k];
1971 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
1978 ccr_buf[count++] = av_clipl_int32(temp << 1);
1983 ccr_buf[count++] =
dot_product(flt_buf[j], flt_buf[j],
1988 for (k = 0; k < j; k++) {
1990 ccr_buf[count++] = av_clipl_int32(temp<<2);
1997 for (i = 0; i < 20 * iter; i++)
2002 for (i = 0; i < 20 * iter; i++){
2003 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
2008 for (i = 0; i < iter; i++) {
2010 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
2011 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
2016 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
2018 for (l = 0; l < 20; l++)
2019 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
2020 temp = av_clipl_int32(temp);
2031 pitch_lag += acb_lag - 1;
2046 static void sub_acb_contrib(
const int16_t *residual,
const int16_t *impulse_resp,
2052 int64_t temp = buf[i] << 14;
2053 for (j = 0; j <= i; j++)
2054 temp -= residual[j] * impulse_resp[i - j];
2056 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
2066 static void get_fcb_param(
FCBParam *optim, int16_t *impulse_resp,
2067 int16_t *buf,
int pulse_cnt,
int pitch_lag)
2076 int amp, err, max, max_amp_index,
min,
scale, i, j, k, l;
2081 memcpy(impulse_r, impulse_resp,
sizeof(int16_t) * SUBFRAME_LEN);
2083 if (pitch_lag < SUBFRAME_LEN - 2) {
2089 temp_corr[i] = impulse_r[i] >> 1;
2092 temp =
dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
2095 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2098 temp =
dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
2099 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2105 temp =
dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
2107 ccr1[i] = temp >> -
scale;
2109 ccr1[i] = av_clipl_int32(temp << scale);
2117 temp =
FFABS(ccr1[j]);
2128 for (j = max_amp_index; j >= 2; j--) {
2130 impulse_corr[0] << 1);
2131 temp =
FFABS(temp - amp);
2140 for (j = 1; j < 5; j++) {
2145 param.
amp_index = max_amp_index + j - 2;
2151 for (k = 1; k < pulse_cnt; k++) {
2157 temp = av_clipl_int32((int64_t)temp *
2160 temp =
FFABS(ccr2[l]);
2173 memset(temp_corr, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2175 for (k = 0; k < pulse_cnt; k++)
2178 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
2180 for (l = 0; l <= k; l++) {
2181 int prod = av_clipl_int32((int64_t)temp_corr[l] *
2182 impulse_r[k - l] << 1);
2183 temp = av_clipl_int32(temp + prod);
2185 temp_corr[k] = temp << 2 >> 16;
2192 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
2193 err = av_clipl_int32(err - prod);
2194 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
2195 err = av_clipl_int32(err + prod);
2199 if (err < optim->min_err) {
2205 for (k = 0; k < pulse_cnt; k++) {
2221 int16_t *buf,
int pulse_cnt)
2230 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
2254 int16_t *buf,
int index)
2261 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
2263 if (p->
pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
2264 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
2269 memset(buf, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2270 for (i = 0; i < pulse_cnt; i++)
2273 pack_fcb_param(&p->
subframe[index], &optim, buf, pulse_cnt);
2288 int info_bits, i,
temp;
2346 const AVFrame *frame,
int *got_packet_ptr)
2352 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
2355 int16_t *in = (
const int16_t *)frame->
data[0];
2363 memcpy(vector +
HALF_FRAME_LEN, in, FRAME_LEN *
sizeof(int16_t));
2365 comp_lpc_coeff(vector, unq_lpc);
2366 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->
prev_lsp, cur_lsp);
2370 memcpy(vector + LPC_ORDER, p->
prev_data + SUBFRAME_LEN,
2371 sizeof(int16_t) * SUBFRAME_LEN);
2372 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
2376 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2378 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
2380 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2382 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2390 comp_harmonic_coeff(vector + i, p->
pitch_lag[j >> 1], hf + j);
2393 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2397 harmonic_filter(hf + j, vector +
PITCH_MAX + i, in + i);
2402 memcpy(p->
prev_lsp, cur_lsp,
sizeof(int16_t) * LPC_ORDER);
2407 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
2415 memset(zero, 0,
sizeof(int16_t) * LPC_ORDER);
2416 memset(vector, 0,
sizeof(int16_t) *
PITCH_MAX);
2417 memset(flt_in, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2419 flt_in[0] = 1 << 13;
2420 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2421 zero, zero, flt_in, vector +
PITCH_MAX, 1);
2422 harmonic_filter(hf + i, vector +
PITCH_MAX, impulse_resp);
2426 memcpy(fir, p->
perf_fir_mem,
sizeof(int16_t) * LPC_ORDER);
2427 memcpy(iir, p->
perf_iir_mem,
sizeof(int16_t) * LPC_ORDER);
2429 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2430 fir, iir, flt_in, vector +
PITCH_MAX, 0);
2432 harmonic_noise_sub(hf + i, vector +
PITCH_MAX, in);
2434 acb_search(p, residual, impulse_resp, in, i);
2437 sub_acb_contrib(residual, impulse_resp, in);
2439 fcb_search(p, impulse_resp, in, i);
2446 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2448 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
2450 sizeof(int16_t) * SUBFRAME_LEN);
2453 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2457 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2459 sizeof(int16_t) * SUBFRAME_LEN);
2468 *got_packet_ptr = 1;
2469 avpkt->
size = pack_bitstream(p, avpkt->
data, avpkt->
size);
2478 .
init = g723_1_encode_init,
2479 .encode2 = g723_1_encode_frame,