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",
1193 out = (int16_t *)frame->
data[0];
1224 int v = av_clip_int16(vector_ptr[j] << 1);
1225 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
1245 vector_ptr + i + ppf[j].
index,
1264 memset(frame->
data[0], 0,
1310 out[i] = av_clip_int16(p->
audio[LPC_ORDER + i] << 1);
1318 #define OFFSET(x) offsetof(G723_1_Context, x)
1319 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1323 { .i64 = 1 }, 0, 1,
AD },
1347 #if CONFIG_G723_1_ENCODER
1348 #define BITSTREAM_WRITER_LE
1367 }
else if (avctx->
bit_rate == 5300) {
1372 "Bitrate not supported, use 6.3k\n");
1388 static void highpass_filter(int16_t *buf, int16_t *fir,
int *iir)
1392 *iir = (buf[i] << 15) + ((-*fir) << 15) +
MULL2(*iir, 0x7f00);
1394 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
1404 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
1423 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
1428 memset(autocorr + 1, 0,
LPC_ORDER *
sizeof(int16_t));
1433 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
1446 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
1449 int16_t partial_corr;
1452 memset(lpc, 0,
LPC_ORDER *
sizeof(int16_t));
1457 for (j = 0; j < i; j++)
1458 temp -= lpc[j] * autocorr[i - j - 1];
1459 temp = ((autocorr[i] << 13) + temp) << 3;
1461 if (
FFABS(temp) >= (error << 16))
1464 partial_corr = temp / (error << 1);
1466 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
1470 temp =
MULL2(temp, partial_corr);
1471 error = av_clipl_int32((int64_t)(error << 16) - temp +
1474 memcpy(vector, lpc, i *
sizeof(int16_t));
1475 for (j = 0; j < i; j++) {
1476 temp = partial_corr * vector[i - j - 1] << 1;
1477 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
1490 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
1492 int16_t autocorr[(LPC_ORDER + 1) *
SUBFRAMES];
1493 int16_t *autocorr_ptr = autocorr;
1494 int16_t *lpc_ptr = lpc;
1498 comp_autocorr(buf + i, autocorr_ptr);
1499 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
1502 autocorr_ptr += LPC_ORDER + 1;
1506 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
1508 int f[LPC_ORDER + 2];
1512 int max,
shift, cur_val, prev_val, count, p;
1521 f[0] = f[1] = 1 << 25;
1524 for (i = 0; i < LPC_ORDER / 2; i++) {
1526 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
1528 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
1533 f[LPC_ORDER + 1] >>= 1;
1537 for (i = 1; i < LPC_ORDER + 2; i++)
1542 for (i = 0; i < LPC_ORDER + 2; i++)
1543 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
1551 for (i = 0; i <= LPC_ORDER / 2; i++)
1552 temp += f[2 * i] *
cos_tab[0];
1553 prev_val = av_clipl_int32(temp << 1);
1558 for (j = 0; j <= LPC_ORDER / 2; j++)
1560 cur_val = av_clipl_int32(temp << 1);
1563 if ((cur_val ^ prev_val) < 0) {
1564 int abs_cur =
FFABS(cur_val);
1565 int abs_prev =
FFABS(prev_val);
1566 int sum = abs_cur + abs_prev;
1570 abs_prev = abs_prev << shift >> 8;
1571 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
1573 if (count == LPC_ORDER)
1581 for (j = 0; j <= LPC_ORDER / 2; j++){
1582 temp += f[LPC_ORDER - 2 * j + p] *
1585 cur_val = av_clipl_int32(temp<<1);
1590 if (count != LPC_ORDER)
1591 memcpy(lsp, prev_lsp, LPC_ORDER *
sizeof(int16_t));
1601 #define get_index(num, offset, size) \
1603 int error, max = -1;\
1606 for (i = 0; i < LSP_CB_SIZE; i++) {\
1607 for (j = 0; j < size; j++){\
1608 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
1611 error = dot_product(lsp + (offset), temp, size) << 1;\
1612 error -= dot_product(lsp_band##num[i], temp, size);\
1615 lsp_index[num] = i;\
1626 static void lsp_quantize(
uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
1633 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
1634 weight[LPC_ORDER - 1] = (1 << 20) /
1635 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
1637 for (i = 1; i < LPC_ORDER - 1; i++) {
1638 min =
FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
1640 weight[i] = (1 << 20) / min;
1642 weight[i] = INT16_MAX;
1648 max =
FFMAX(weight[i], max);
1652 weight[i] <<=
shift;
1658 (((prev_lsp[i] -
dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
1672 static void perceptual_filter(
G723_1_Context *p, int16_t *flt_coef,
1673 int16_t *unq_lpc, int16_t *buf)
1678 memcpy(buf, p->
iir_mem,
sizeof(int16_t) * LPC_ORDER);
1679 memcpy(vector, p->
fir_mem,
sizeof(int16_t) * LPC_ORDER);
1680 memcpy(vector + LPC_ORDER, buf + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
1686 flt_coef[k + 2 * l +
LPC_ORDER] = (unq_lpc[k + l] *
1690 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
1694 memcpy(p->
iir_mem, buf + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1695 memcpy(p->
fir_mem, vector + FRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1704 static int estimate_pitch(int16_t *buf,
int start)
1707 int max_ccr = 0x4000;
1708 int max_eng = 0x7fff;
1712 int ccr, eng, orig_eng, ccr_eng, exp;
1719 for (i = PITCH_MIN; i <=
PITCH_MAX - 3; i++) {
1731 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
1735 ccr = ccr << temp >> 16;
1739 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
1749 if (exp + 1 < max_exp)
1753 if (exp + 1 == max_exp)
1754 temp = max_ccr >> 1;
1757 ccr_eng = ccr * max_eng;
1758 diff = ccr_eng - eng *
temp;
1759 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
1777 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag,
HFParam *hf)
1779 int ccr, eng, max_ccr, max_eng;
1784 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
1796 for (i = 0; i < 15; i++)
1800 for (i = 0; i < 15; i++) {
1801 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
1810 for (i = 0; i <= 6; i++) {
1811 eng = energy[i << 1];
1812 ccr = energy[(i << 1) + 1];
1817 ccr = (ccr * ccr + (1 << 14)) >> 15;
1818 diff = ccr * max_eng - eng * max_ccr;
1826 if (hf->
index == -1) {
1827 hf->
index = pitch_lag;
1831 eng = energy[14] * max_eng;
1832 eng = (eng >> 2) + (eng >> 3);
1833 ccr = energy[(hf->
index << 1) + 1] * energy[(hf->
index << 1) + 1];
1835 eng = energy[(hf->
index << 1) + 1];
1840 hf->
gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
1842 hf->
index += pitch_lag - 3;
1850 static void harmonic_filter(
HFParam *hf,
const int16_t *src, int16_t *dest)
1855 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1856 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
1860 static void harmonic_noise_sub(
HFParam *hf,
const int16_t *src, int16_t *dest)
1864 int64_t temp = hf->
gain * src[i - hf->
index] << 1;
1865 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
1880 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
1881 int16_t *perf_fir, int16_t *perf_iir,
1882 const int16_t *src, int16_t *dest,
int scale)
1885 int16_t buf_16[SUBFRAME_LEN +
LPC_ORDER];
1890 memcpy(buf_16, perf_fir,
sizeof(int16_t) * LPC_ORDER);
1891 memcpy(dest - LPC_ORDER, perf_iir,
sizeof(int16_t) * LPC_ORDER);
1896 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
1898 buf[i] = (src[i] << 15) + (temp << 3);
1899 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
1903 int64_t fir = 0, iir = 0;
1905 fir -= perf_lpc[j - 1] * bptr_16[i - j];
1906 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
1908 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
1911 memcpy(perf_fir, buf_16 + SUBFRAME_LEN,
sizeof(int16_t) * LPC_ORDER);
1912 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
1913 sizeof(int16_t) * LPC_ORDER);
1923 int16_t *impulse_resp,
const int16_t *buf,
1933 int pitch_lag = p->
pitch_lag[index >> 1];
1936 int odd_frame = index & 1;
1937 int iter = 3 + odd_frame;
1941 int i, j, k, l, max;
1945 if (pitch_lag == PITCH_MIN)
1951 for (i = 0; i < iter; i++) {
1956 for (k = 0; k <= j; k++)
1957 temp += residual[
PITCH_ORDER - 1 + k] * impulse_resp[j - k];
1958 flt_buf[
PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
1963 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
1965 temp = (flt_buf[j + 1][k - 1] << 15) +
1966 residual[j] * impulse_resp[k];
1967 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
1974 ccr_buf[count++] = av_clipl_int32(temp << 1);
1979 ccr_buf[count++] =
dot_product(flt_buf[j], flt_buf[j],
1984 for (k = 0; k < j; k++) {
1986 ccr_buf[count++] = av_clipl_int32(temp<<2);
1993 for (i = 0; i < 20 * iter; i++)
1998 for (i = 0; i < 20 * iter; i++){
1999 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
2004 for (i = 0; i < iter; i++) {
2006 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
2007 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
2012 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
2014 for (l = 0; l < 20; l++)
2015 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
2016 temp = av_clipl_int32(temp);
2027 pitch_lag += acb_lag - 1;
2042 static void sub_acb_contrib(
const int16_t *residual,
const int16_t *impulse_resp,
2048 int64_t temp = buf[i] << 14;
2049 for (j = 0; j <= i; j++)
2050 temp -= residual[j] * impulse_resp[i - j];
2052 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
2062 static void get_fcb_param(
FCBParam *optim, int16_t *impulse_resp,
2063 int16_t *buf,
int pulse_cnt,
int pitch_lag)
2072 int amp, err, max, max_amp_index,
min,
scale, i, j, k, l;
2077 memcpy(impulse_r, impulse_resp,
sizeof(int16_t) * SUBFRAME_LEN);
2079 if (pitch_lag < SUBFRAME_LEN - 2) {
2085 temp_corr[i] = impulse_r[i] >> 1;
2088 temp =
dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
2091 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2094 temp =
dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
2095 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
2101 temp =
dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
2103 ccr1[i] = temp >> -
scale;
2105 ccr1[i] = av_clipl_int32(temp << scale);
2113 temp =
FFABS(ccr1[j]);
2124 for (j = max_amp_index; j >= 2; j--) {
2126 impulse_corr[0] << 1);
2127 temp =
FFABS(temp - amp);
2136 for (j = 1; j < 5; j++) {
2141 param.
amp_index = max_amp_index + j - 2;
2147 for (k = 1; k < pulse_cnt; k++) {
2153 temp = av_clipl_int32((int64_t)temp *
2156 temp =
FFABS(ccr2[l]);
2169 memset(temp_corr, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2171 for (k = 0; k < pulse_cnt; k++)
2174 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
2176 for (l = 0; l <= k; l++) {
2177 int prod = av_clipl_int32((int64_t)temp_corr[l] *
2178 impulse_r[k - l] << 1);
2179 temp = av_clipl_int32(temp + prod);
2181 temp_corr[k] = temp << 2 >> 16;
2188 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
2189 err = av_clipl_int32(err - prod);
2190 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
2191 err = av_clipl_int32(err + prod);
2195 if (err < optim->min_err) {
2201 for (k = 0; k < pulse_cnt; k++) {
2217 int16_t *buf,
int pulse_cnt)
2226 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
2250 int16_t *buf,
int index)
2257 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
2259 if (p->
pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
2260 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
2265 memset(buf, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2266 for (i = 0; i < pulse_cnt; i++)
2269 pack_fcb_param(&p->
subframe[index], &optim, buf, pulse_cnt);
2284 int info_bits, i,
temp;
2342 const AVFrame *frame,
int *got_packet_ptr)
2348 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
2351 int16_t *
in = (
const int16_t *)frame->
data[0];
2359 memcpy(vector +
HALF_FRAME_LEN, in, FRAME_LEN *
sizeof(int16_t));
2361 comp_lpc_coeff(vector, unq_lpc);
2362 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->
prev_lsp, cur_lsp);
2366 memcpy(vector + LPC_ORDER, p->
prev_data + SUBFRAME_LEN,
2367 sizeof(int16_t) * SUBFRAME_LEN);
2368 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
2372 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2374 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
2376 memcpy(in, vector + LPC_ORDER,
sizeof(int16_t) * FRAME_LEN);
2378 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2386 comp_harmonic_coeff(vector + i, p->
pitch_lag[j >> 1], hf + j);
2389 memcpy(vector +
PITCH_MAX, in,
sizeof(int16_t) * FRAME_LEN);
2393 harmonic_filter(hf + j, vector +
PITCH_MAX + i, in + i);
2398 memcpy(p->
prev_lsp, cur_lsp,
sizeof(int16_t) * LPC_ORDER);
2403 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
2411 memset(zero, 0,
sizeof(int16_t) * LPC_ORDER);
2412 memset(vector, 0,
sizeof(int16_t) *
PITCH_MAX);
2413 memset(flt_in, 0,
sizeof(int16_t) * SUBFRAME_LEN);
2415 flt_in[0] = 1 << 13;
2416 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2417 zero, zero, flt_in, vector +
PITCH_MAX, 1);
2418 harmonic_filter(hf + i, vector +
PITCH_MAX, impulse_resp);
2422 memcpy(fir, p->
perf_fir_mem,
sizeof(int16_t) * LPC_ORDER);
2423 memcpy(iir, p->
perf_iir_mem,
sizeof(int16_t) * LPC_ORDER);
2425 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2426 fir, iir, flt_in, vector +
PITCH_MAX, 0);
2428 harmonic_noise_sub(hf + i, vector +
PITCH_MAX, in);
2430 acb_search(p, residual, impulse_resp, in, i);
2433 sub_acb_contrib(residual, impulse_resp, in);
2435 fcb_search(p, impulse_resp, in, i);
2442 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2444 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
2446 sizeof(int16_t) * SUBFRAME_LEN);
2449 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2453 sizeof(int16_t) * (
PITCH_MAX - SUBFRAME_LEN));
2455 sizeof(int16_t) * SUBFRAME_LEN);
2464 *got_packet_ptr = 1;
2465 avpkt->
size = pack_bitstream(p, avpkt->
data, avpkt->
size);
2474 .
init = g723_1_encode_init,
2475 .encode2 = g723_1_encode_frame,