28 #define BITSTREAM_READER_LE
41 #define CNG_RANDOM_SEED 12345
43 typedef struct g723_1_context {
116 int temp, info_bits, i;
123 if (info_bits == 3) {
133 if (info_bits == 2) {
230 return (
ff_sqrt(val << 1) >> 1) & (~1);
241 return width -
av_log2(num) - 1;
244 #define normalize_bits_int16(num) normalize_bits(num, 15)
245 #define normalize_bits_int32(num) normalize_bits(num, 31)
255 for (i = 0; i <
length; i++)
256 max |=
FFABS(vector[i]);
259 bits=
FFMAX(bits, 0);
261 for (i = 0; i <
length; i++)
262 dst[i] = vector[i] << bits >> 3;
276 uint8_t *lsp_index,
int bad_frame)
279 int i, j,
temp, stable;
288 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
305 temp = ((prev_lsp[i] -
dc_lsp[i]) * pred + (1 << 14)) >> 15;
310 cur_lsp[0] =
FFMAX(cur_lsp[0], 0x180);
311 cur_lsp[LPC_ORDER - 1] =
FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
315 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
318 cur_lsp[j - 1] -=
temp;
324 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
334 memcpy(cur_lsp, prev_lsp, LPC_ORDER *
sizeof(*cur_lsp));
343 #define MULL2(a, b) \
359 int index = (lpc[j] >> 7) & 0x1FF;
360 int offset = lpc[j] & 0x7f;
363 ((offset << 8) + 0x80) << 1;
365 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
374 f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
375 f1[2] = lpc[0] * lpc[2] + (2 << 28);
378 f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
379 f2[2] = lpc[1] * lpc[3] + (2 << 28);
385 for (i = 2; i < LPC_ORDER / 2; i++) {
386 f1[i + 1] = f1[i - 1] +
MULL2(f1[i], lpc[2 * i]);
387 f2[i + 1] = f2[i - 1] +
MULL2(f2[i], lpc[2 * i + 1]);
389 for (j = i; j >= 2; j--) {
390 f1[j] =
MULL2(f1[j - 1], lpc[2 * i]) +
391 (f1[j] >> 1) + (f1[j - 2] >> 1);
392 f2[j] =
MULL2(f2[j - 1], lpc[2 * i + 1]) +
393 (f2[j] >> 1) + (f2[j - 2] >> 1);
398 f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
399 f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
403 for (i = 0; i < LPC_ORDER / 2; i++) {
404 int64_t ff1 = f1[i + 1] + f1[i];
405 int64_t ff2 = f2[i + 1] - f2[i];
407 lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
408 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
424 int16_t *lpc_ptr = lpc;
451 for (j = 0; j < SUBFRAME_LEN - i; j++)
452 buf[i + j] += vector[j];
466 enum Rate cur_rate,
int pitch_lag,
int index)
503 for (i = 0; i < 8; i += 2) {
504 offset = ((cb_pos & 7) << 3) + cb_shift + i;
505 vector[
offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
517 vector[i] += beta * vector[i - lag] >> 15;
525 static void get_residual(int16_t *residual, int16_t *prev_excitation,
int lag)
530 residual[0] = prev_excitation[
offset];
531 residual[1] = prev_excitation[offset + 1];
535 residual[i] = prev_excitation[offset + (i - 2) % lag];
541 return av_sat_add32(sum, sum);
552 const int16_t *cb_ptr;
553 int lag = pitch_lag + subfrm->
ad_cb_lag - 1;
570 vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
585 int pitch_lag,
int length,
int dir)
587 int limit, ccr, lag = 0;
594 limit = pitch_lag + 3;
596 for (i = pitch_lag - 3; i <= limit; i++) {
599 if (ccr > *ccr_max) {
618 int tgt_eng,
int ccr,
int res_eng)
625 temp1 = tgt_eng * res_eng >> 1;
626 temp2 = ccr * ccr << 1;
629 if (ccr >= res_eng) {
632 ppf->
opt_gain = (ccr << 15) / res_eng *
636 temp1 = (tgt_eng << 15) + (ccr * ppf->
opt_gain << 1);
638 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
640 if (tgt_eng >= pf_residual << 1) {
643 temp1 = (tgt_eng << 14) / pf_residual;
680 int energy[5] = {0, 0, 0, 0, 0};
682 int fwd_lag =
autocorr_max(buf, offset, &energy[1], pitch_lag,
684 int back_lag =
autocorr_max(buf, offset, &energy[3], pitch_lag,
692 if (!back_lag && !fwd_lag)
708 for (i = 0; i < 5; i++)
709 temp1 =
FFMAX(energy[i], temp1);
712 for (i = 0; i < 5; i++)
713 energy[i] = (energy[i] << scale) >> 16;
715 if (fwd_lag && !back_lag) {
718 }
else if (!fwd_lag) {
727 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
728 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
729 if (temp1 >= temp2) {
750 int *exc_eng,
int *
scale)
762 index =
autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
763 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
767 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
773 best_eng =
dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
774 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
776 temp = best_eng * *exc_eng >> 3;
778 if (temp < ccr * ccr) {
794 int gain,
int *rseed)
800 for (i = 0; i < lag; i++)
801 out[i] = vector_ptr[i - lag] * 3 >> 2;
806 *rseed = *rseed * 521 + 259;
807 out[i] = gain * *rseed >> 15;
809 memset(buf, 0, (FRAME_LEN +
PITCH_MAX) *
sizeof(*buf));
822 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
825 int res_shift = 16 & ~-(width);\
826 int in_shift = 16 - res_shift;\
828 for (m = 0; m < SUBFRAME_LEN; m++) {\
830 for (n = 1; n <= LPC_ORDER; n++) {\
831 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
832 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
835 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
836 (1 << 15)) >> res_shift;\
855 int temp = buf[i] >> 2;
857 denom = av_sat_dadd32(denom, temp);
863 num = num << bits1 >> 1;
866 bits2 = 5 + bits1 -
bits2;
867 bits2 =
FFMAX(0, bits2);
869 gain = (num >> 1) / (denom >> 16);
877 buf[i] = av_clip_int16((buf[i] * (p->
pf_gain + (p->
pf_gain >> 4)) +
891 int16_t *
buf, int16_t *dst)
907 iir_filter(filter_coef[0], filter_coef[1], buf + i,
908 filter_signal + i, 1);
930 temp = auto_corr[1] >> 16;
932 temp = (auto_corr[0] >> 2) / temp;
939 dst[j] = av_sat_dadd32(signal_ptr[j],
940 (signal_ptr[j - 1] >> 16) * temp) >> 16;
944 temp = 2 * scale + 4;
946 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
948 energy = auto_corr[1] >>
temp;
962 else if (gain < 0x20)
963 return gain - 8 << 7;
965 return gain - 20 << 8;
970 *state = (*state * 521 + 259) & 0xFFFF;
971 return (*state & 0x7FFF) * base >> 15;
976 int i,
shift, seg, seg2,
t,
val, val_add, x,
y;
995 seg2 =
FFMIN(seg, 3);
999 for (i = 0; i <
shift; i++) {
1000 t = seg * 32 + (val << seg2);
1009 t = seg * 32 + (val << seg2);
1012 t = seg * 32 + (val + 1 << seg2);
1014 val = (seg2 - 1 << 4) + val;
1018 t = seg * 32 + (val - 1 << seg2);
1020 val = (seg2 - 1 << 4) + val;
1034 int16_t *vector_ptr;
1046 for (i = 0; i < SUBFRAMES / 2; i++) {
1051 for (j = 0; j < 11; j++) {
1052 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
1062 for (j = 0; j <
pulses[i]; j++, idx++) {
1065 pos[idx] = tmp[idx2] * 2 + off[i];
1066 tmp[idx2] = tmp[--
t];
1084 t |=
FFABS(vector_ptr[j]);
1085 t =
FFMIN(t, 0x7FFF);
1095 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1096 t = vector_ptr[j] << -
shift;
1101 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1102 t = vector_ptr[j] >>
shift;
1109 for (j = 0; j < 11; j++)
1110 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
1111 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30;
1114 if (shift * 2 + 3 >= 0)
1115 c >>= shift * 2 + 3;
1117 c <<= -(shift * 2 + 3);
1118 c = (av_clipl_int32(sum << 1) -
c) * 2979LL >> 15;
1120 delta = b0 * b0 * 2 -
c;
1135 x = av_clip(x, -10000, 10000);
1137 for (j = 0; j < 11; j++) {
1138 idx = (i / 2) * 11 + j;
1139 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
1140 (x * signs[idx] >> 15));
1144 memcpy(vector_ptr +
PITCH_MAX, vector_ptr,
1145 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
1146 vector_ptr += SUBFRAME_LEN * 2;
1154 int *got_frame_ptr,
AVPacket *avpkt)
1159 int buf_size = avpkt->
size;
1160 int dec_mode = buf[0] & 3;
1167 int bad_frame = 0, i, j,
ret;
1168 int16_t *audio = p->
audio;
1173 "Expected %d bytes, got %d - skipping packet\n",
1191 out = (int16_t *)frame->
data[0];
1222 int v = av_clip_int16(vector_ptr[j] << 1);
1223 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
1243 vector_ptr + i + ppf[j].
index,
1262 memset(frame->
data[0], 0,
1308 out[i] = av_clip_int16(p->
audio[LPC_ORDER + i] << 1);
1316 #define OFFSET(x) offsetof(G723_1_Context, x)
1317 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1321 { .i64 = 1 }, 0, 1,
AD },
1345 #if CONFIG_G723_1_ENCODER
1346 #define BITSTREAM_WRITER_LE
1365 }
else if (avctx->
bit_rate == 5300) {
1370 "Bitrate not supported, use 6.3k\n");
1386 static void highpass_filter(int16_t *
buf, int16_t *fir,
int *iir)
1390 *iir = (buf[i] << 15) + ((-*fir) << 15) +
MULL2(*iir, 0x7f00);
1392 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
1402 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
1421 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
1426 memset(autocorr + 1, 0,
LPC_ORDER *
sizeof(int16_t));
1431 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
1444 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
1447 int16_t partial_corr;
1450 memset(lpc, 0,
LPC_ORDER *
sizeof(int16_t));
1455 for (j = 0; j < i; j++)
1456 temp -= lpc[j] * autocorr[i - j - 1];
1457 temp = ((autocorr[i] << 13) + temp) << 3;
1459 if (
FFABS(temp) >= (error << 16))
1462 partial_corr = temp / (error << 1);
1464 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
1468 temp =
MULL2(temp, partial_corr);
1469 error = av_clipl_int32((int64_t)(error << 16) - temp +
1472 memcpy(vector, lpc, i *
sizeof(int16_t));
1473 for (j = 0; j < i; j++) {
1474 temp = partial_corr * vector[i - j - 1] << 1;
1475 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
1488 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
1490 int16_t autocorr[(LPC_ORDER + 1) *
SUBFRAMES];
1491 int16_t *autocorr_ptr = autocorr;
1492 int16_t *lpc_ptr = lpc;
1496 comp_autocorr(buf + i, autocorr_ptr);
1497 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
1500 autocorr_ptr += LPC_ORDER + 1;
1504 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
1506 int f[LPC_ORDER + 2];
1519 f[0] = f[1] = 1 << 25;
1522 for (i = 0; i < LPC_ORDER / 2; i++) {
1524 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
1526 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
1531 f[LPC_ORDER + 1] >>= 1;
1535 for (i = 1; i < LPC_ORDER + 2; i++)
1540 for (i = 0; i < LPC_ORDER + 2; i++)
1541 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
1549 for (i = 0; i <= LPC_ORDER / 2; i++)
1550 temp += f[2 * i] *
cos_tab[0];
1551 prev_val = av_clipl_int32(temp << 1);
1556 for (j = 0; j <= LPC_ORDER / 2; j++)
1558 cur_val = av_clipl_int32(temp << 1);
1561 if ((cur_val ^ prev_val) < 0) {
1562 int abs_cur =
FFABS(cur_val);
1563 int abs_prev =
FFABS(prev_val);
1564 int sum = abs_cur + abs_prev;
1568 abs_prev = abs_prev << shift >> 8;
1569 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
1571 if (count == LPC_ORDER)
1579 for (j = 0; j <= LPC_ORDER / 2; j++){
1580 temp += f[LPC_ORDER - 2 * j + p] *
1583 cur_val = av_clipl_int32(temp<<1);
1588 if (count != LPC_ORDER)
1589 memcpy(lsp, prev_lsp, LPC_ORDER *
sizeof(int16_t));
1599 #define get_index(num, offset, size) \
1601 int error, max = -1;\
1604 for (i = 0; i < LSP_CB_SIZE; i++) {\
1605 for (j = 0; j < size; j++){\
1606 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
1609 error = dot_product(lsp + (offset), temp, size) << 1;\
1610 error -= dot_product(lsp_band##num[i], temp, size);\
1613 lsp_index[num] = i;\
1624 static void lsp_quantize(
uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
1631 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
1632 weight[LPC_ORDER - 1] = (1 << 20) /
1633 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
1635 for (i = 1; i < LPC_ORDER - 1; i++) {
1636 min =
FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
1638 weight[i] = (1 << 20) / min;
1640 weight[i] = INT16_MAX;
1646 max =
FFMAX(weight[i], max);
1650 weight[i] <<=
shift;
1656 (((prev_lsp[i] -
dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
1670 static void perceptual_filter(
G723_1_Context *p, int16_t *flt_coef,
1671 int16_t *unq_lpc, int16_t *buf)
1676 memcpy(buf, p->
iir_mem,
sizeof(int16_t) * LPC_ORDER);
1677 memcpy(vector, p->
fir_mem,
sizeof(int16_t) * LPC_ORDER);
1678 memcpy(vector + LPC_ORDER, buf + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
1684 flt_coef[k + 2 * l +
LPC_ORDER] = (unq_lpc[k + l] *
1688 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
1692 memcpy(p->
iir_mem, buf + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1693 memcpy(p->
fir_mem, vector + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1702 static int estimate_pitch(int16_t *buf,
int start)
1705 int max_ccr = 0x4000;
1706 int max_eng = 0x7fff;
1710 int ccr, eng, orig_eng, ccr_eng, exp;
1717 for (i = PITCH_MIN; i <=
PITCH_MAX - 3; i++) {
1729 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
1733 ccr = ccr << temp >> 16;
1737 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
1747 if (exp + 1 < max_exp)
1751 if (exp + 1 == max_exp)
1752 temp = max_ccr >> 1;
1755 ccr_eng = ccr * max_eng;
1756 diff = ccr_eng - eng *
temp;
1757 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
1775 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag,
HFParam *hf)
1777 int ccr, eng, max_ccr, max_eng;
1782 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
1794 for (i = 0; i < 15; i++)
1798 for (i = 0; i < 15; i++) {
1799 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
1808 for (i = 0; i <= 6; i++) {
1809 eng = energy[i << 1];
1810 ccr = energy[(i << 1) + 1];
1815 ccr = (ccr * ccr + (1 << 14)) >> 15;
1816 diff = ccr * max_eng - eng * max_ccr;
1824 if (hf->
index == -1) {
1825 hf->
index = pitch_lag;
1829 eng = energy[14] * max_eng;
1830 eng = (eng >> 2) + (eng >> 3);
1831 ccr = energy[(hf->
index << 1) + 1] * energy[(hf->
index << 1) + 1];
1833 eng = energy[(hf->
index << 1) + 1];
1838 hf->
gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
1840 hf->
index += pitch_lag - 3;
1848 static void harmonic_filter(
HFParam *hf,
const int16_t *
src, int16_t *dest)
1853 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1854 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
1858 static void harmonic_noise_sub(
HFParam *hf,
const int16_t *src, int16_t *dest)
1862 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1863 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
1878 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
1879 int16_t *perf_fir, int16_t *perf_iir,
1880 const int16_t *src, int16_t *dest,
int scale)
1883 int16_t buf_16[SUBFRAME_LEN +
LPC_ORDER];
1888 memcpy(buf_16, perf_fir,
sizeof(int16_t) * LPC_ORDER);
1889 memcpy(dest - LPC_ORDER, perf_iir,
sizeof(int16_t) * LPC_ORDER);
1894 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
1896 buf[i] = (src[i] << 15) + (temp << 3);
1897 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
1901 int64_t fir = 0, iir = 0;
1903 fir -= perf_lpc[j - 1] * bptr_16[i - j];
1904 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
1906 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
1909 memcpy(perf_fir, buf_16 + SUBFRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1910 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
1911 sizeof(int16_t) * LPC_ORDER);
1921 int16_t *impulse_resp,
const int16_t *buf,
1931 int pitch_lag = p->
pitch_lag[index >> 1];
1934 int odd_frame = index & 1;
1935 int iter = 3 + odd_frame;
1939 int i, j, k, l, max;
1943 if (pitch_lag == PITCH_MIN)
1949 for (i = 0; i < iter; i++) {
1954 for (k = 0; k <= j; k++)
1955 temp += residual[
PITCH_ORDER - 1 + k] * impulse_resp[j - k];
1956 flt_buf[
PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
1961 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
1963 temp = (flt_buf[j + 1][k - 1] << 15) +
1964 residual[j] * impulse_resp[k];
1965 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
1972 ccr_buf[count++] = av_clipl_int32(temp << 1);
1977 ccr_buf[count++] =
dot_product(flt_buf[j], flt_buf[j],
1982 for (k = 0; k < j; k++) {
1984 ccr_buf[count++] = av_clipl_int32(temp<<2);
1991 for (i = 0; i < 20 * iter; i++)
1996 for (i = 0; i < 20 * iter; i++){
1997 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
2002 for (i = 0; i < iter; i++) {
2004 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
2005 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
2010 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
2012 for (l = 0; l < 20; l++)
2013 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
2014 temp = av_clipl_int32(temp);
2025 pitch_lag += acb_lag - 1;
2040 static void sub_acb_contrib(
const int16_t *residual,
const int16_t *impulse_resp,
2046 int64_t temp = buf[i] << 14;
2047 for (j = 0; j <= i; j++)
2048 temp -= residual[j] * impulse_resp[i - j];
2050 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
2060 static void get_fcb_param(
FCBParam *optim, int16_t *impulse_resp,
2061 int16_t *buf,
int pulse_cnt,
int pitch_lag)
2070 int amp, err, max, max_amp_index,
min,
scale, i, j, k, l;
2075 memcpy(impulse_r, impulse_resp,
sizeof(int16_t) * SUBFRAME_LEN);
2077 if (pitch_lag < SUBFRAME_LEN - 2) {
2083 temp_corr[i] = impulse_r[i] >> 1;
2086 temp =
dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
2089 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2092 temp =
dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
2093 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2099 temp =
dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
2101 ccr1[i] = temp >> -
scale;
2103 ccr1[i] = av_clipl_int32(temp << scale);
2111 temp =
FFABS(ccr1[j]);
2122 for (j = max_amp_index; j >= 2; j--) {
2124 impulse_corr[0] << 1);
2125 temp =
FFABS(temp - amp);
2134 for (j = 1; j < 5; j++) {
2139 param.
amp_index = max_amp_index + j - 2;
2145 for (k = 1; k < pulse_cnt; k++) {
2151 temp = av_clipl_int32((int64_t)temp *
2154 temp =
FFABS(ccr2[l]);
2167 memset(temp_corr, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2169 for (k = 0; k < pulse_cnt; k++)
2172 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
2174 for (l = 0; l <= k; l++) {
2175 int prod = av_clipl_int32((int64_t)temp_corr[l] *
2176 impulse_r[k - l] << 1);
2177 temp = av_clipl_int32(temp + prod);
2179 temp_corr[k] = temp << 2 >> 16;
2186 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
2187 err = av_clipl_int32(err - prod);
2188 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
2189 err = av_clipl_int32(err + prod);
2193 if (err < optim->min_err) {
2199 for (k = 0; k < pulse_cnt; k++) {
2215 int16_t *buf,
int pulse_cnt)
2224 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
2248 int16_t *buf,
int index)
2255 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
2257 if (p->
pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
2258 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
2263 memset(buf, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2264 for (i = 0; i < pulse_cnt; i++)
2267 pack_fcb_param(&p->
subframe[index], &optim, buf, pulse_cnt);
2282 int info_bits, i,
temp;
2340 const AVFrame *frame,
int *got_packet_ptr)
2346 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
2349 int16_t *
in = (
const int16_t *)frame->
data[0];
2357 memcpy(vector +
HALF_FRAME_LEN, in, FRAME_LEN *
sizeof(int16_t));
2359 comp_lpc_coeff(vector, unq_lpc);
2360 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->
prev_lsp, cur_lsp);
2364 memcpy(vector + LPC_ORDER, p->
prev_data + SUBFRAME_LEN,
2365 sizeof(int16_t) * SUBFRAME_LEN);
2366 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
2370 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2372 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
2374 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2376 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2384 comp_harmonic_coeff(vector + i, p->
pitch_lag[j >> 1], hf + j);
2387 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2391 harmonic_filter(hf + j, vector +
PITCH_MAX + i, in + i);
2396 memcpy(p->
prev_lsp, cur_lsp,
sizeof(int16_t) * LPC_ORDER);
2401 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
2409 memset(zero, 0,
sizeof(int16_t) * LPC_ORDER);
2410 memset(vector, 0,
sizeof(int16_t) *
PITCH_MAX);
2411 memset(flt_in, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2413 flt_in[0] = 1 << 13;
2414 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2415 zero, zero, flt_in, vector +
PITCH_MAX, 1);
2416 harmonic_filter(hf + i, vector +
PITCH_MAX, impulse_resp);
2420 memcpy(fir, p->
perf_fir_mem,
sizeof(int16_t) * LPC_ORDER);
2421 memcpy(iir, p->
perf_iir_mem,
sizeof(int16_t) * LPC_ORDER);
2423 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2424 fir, iir, flt_in, vector +
PITCH_MAX, 0);
2426 harmonic_noise_sub(hf + i, vector +
PITCH_MAX, in);
2428 acb_search(p, residual, impulse_resp, in, i);
2431 sub_acb_contrib(residual, impulse_resp, in);
2433 fcb_search(p, impulse_resp, in, i);
2440 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2442 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
2444 sizeof(int16_t) * SUBFRAME_LEN);
2447 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2451 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2453 sizeof(int16_t) * SUBFRAME_LEN);
2462 *got_packet_ptr = 1;
2463 avpkt->
size = pack_bitstream(p, avpkt->
data, avpkt->
size);
2472 .
init = g723_1_encode_init,
2473 .encode2 = g723_1_encode_frame,