28 #define BITSTREAM_READER_LE 41 #define CNG_RANDOM_SEED 12345 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;
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];
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;
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;
570 vector[
i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
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);
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;
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;
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)
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]);
1504 static void lpc2lsp(int16_t *lpc, int16_t *
prev_lsp, int16_t *lsp)
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;\ 1631 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
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);
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)
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)) {
1777 int ccr, eng, max_ccr, max_eng;
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) {
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;
1848 static void harmonic_filter(
HFParam *hf,
const int16_t *
src, int16_t *
dest)
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)
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,
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,
1934 int odd_frame = index & 1;
1935 int iter = 3 + odd_frame;
1939 int i, j,
k, l,
max;
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 ||
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,
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;
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++) {
2258 get_fcb_param(&optim, impulse_resp,
buf, pulse_cnt,
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;
2349 int16_t *
in = (
const int16_t *)frame->
data[0];
2359 comp_lpc_coeff(vector, unq_lpc);
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);
2380 p->
pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
2381 p->
pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_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);
2388 memcpy(p->
prev_weight_sig, vector + FRAME_LEN,
sizeof(int16_t) * PITCH_MAX);
2391 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
2396 memcpy(p->
prev_lsp, cur_lsp,
sizeof(int16_t) * LPC_ORDER);
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);
2425 memcpy(vector, p->
harmonic_mem,
sizeof(int16_t) * PITCH_MAX);
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),
2449 in, vector + PITCH_MAX, 0);
2451 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
2452 memcpy(p->
harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
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,
static void lsp2lpc(int16_t *lpc)
Convert LSP frequencies to LPC coefficients.
int16_t hpf_fir_mem
highpass filter fir
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
static const int16_t lsp_band0[LSP_CB_SIZE][3]
LSP VQ tables.
static const int cng_bseg[3]
Silence Insertion Descriptor frame.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define normalize_bits_int32(num)
static const int16_t lsp_band2[LSP_CB_SIZE][4]
memory handling functions
if max(w)>1 w=0.9 *w/max(w)
static av_cold int init(AVCodecContext *avctx)
G723.1 unpacked data subframe.
#define AV_LOG_WARNING
Something somehow does not look correct.
int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs, const int16_t *in, int buffer_length, int filter_length, int stop_on_overflow, int shift, int rounder)
LP synthesis filter.
static int scale_vector(int16_t *dst, const int16_t *vector, int length)
Scale vector contents based on the largest of their absolutes.
static const int cng_filt[4]
uint8_t lsp_index[LSP_BANDS]
static int normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
static void skip_put_bits(PutBitContext *s, int n)
Skip the given number of bits.
static int autocorr_max(const int16_t *buf, int offset, int *ccr_max, int pitch_lag, int length, int dir)
Estimate maximum auto-correlation around pitch lag.
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
static int comp_interp_index(G723_1_Context *p, int pitch_lag, int *exc_eng, int *scale)
Classify frames as voiced/unvoiced.
static const AVOption options[]
static void gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
static const AVClass g723_1dec_class
int16_t perf_fir_mem[LPC_ORDER]
perceptual filter fir
static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
Calculate pitch postfilter parameters.
static const int16_t percept_flt_tbl[2][LPC_ORDER]
0.5^i scaled by 2^15
static int16_t square_root(unsigned val)
Bitexact implementation of sqrt(val/2).
int16_t prev_lsp[LPC_ORDER]
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
int16_t prev_excitation[PITCH_MAX]
enum AVSampleFormat sample_fmt
audio sample format
Optimized fixed codebook excitation parameters.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static const int16_t lsp_band1[LSP_CB_SIZE][3]
static void residual_interp(int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
Peform residual interpolation based on frame classification.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf, int16_t *dst)
Perform formant filtering.
static const uint8_t bits2[81]
bitstream reader API header.
static const int16_t cos_tab[COS_TBL_SIZE+1]
Cosine table scaled by 2^14.
static const uint8_t frame_size[4]
static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
Get delayed contribution from the previous excitation vector.
int16_t perf_iir_mem[LPC_ORDER]
and iir memories
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
enum FrameType cur_frame_type
#define iir_filter(fir_coef, iir_coef, src, dest, width)
Perform IIR filtering.
int pulse_sign[PULSE_MAX]
static const int16_t postfilter_tbl[2][LPC_ORDER]
0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
FrameType
G723.1 frame types.
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
Quantize LSP frequencies by interpolation and convert them to the corresponding LPC coefficients...
int64_t ff_dot_product(const int16_t *a, const int16_t *b, int length)
returns the dot product of 2 int16_t vectors.
G723.1 compatible decoder data tables.
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
static const uint8_t offset[127][2]
static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate, int tgt_eng, int ccr, int res_eng)
Calculate pitch postfilter optimal and scaling gains.
uint64_t channel_layout
Audio channel layout.
static int estimate_sid_gain(G723_1_Context *p)
int hpf_iir_mem
and iir memories
static const int16_t fixed_cb_gain[GAIN_LEVELS]
void ff_acelp_weighted_vector_sum(int16_t *out, const int16_t *in_a, const int16_t *in_b, int16_t weight_coeff_a, int16_t weight_coeff_b, int16_t rounder, int shift, int length)
weighted sum of two vectors with rounding.
int bit_rate
the average bitrate
audio channel layout utility functions
int16_t prev_data[HALF_FRAME_LEN]
#define normalize_bits_int16(num)
G723_1_Subframe subframe[4]
struct g723_1_context G723_1_Context
enum FrameType past_frame_type
static const int16_t ppf_gain_weight[2]
Postfilter gain weighting factors scaled by 2^15.
#define diff(a, as, b, bs)
int index
postfilter backward/forward lag
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
static const int16_t adaptive_cb_gain85[85 *20]
static const float pred[4]
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int pf_gain
formant postfilter gain scaling unit memory
static const int16_t adaptive_cb_gain170[170 *20]
int16_t opt_gain
optimal gain
int frame_size
Number of samples per channel in an audio frame.
static const int16_t binomial_window[LPC_ORDER]
Binomial window coefficients scaled by 2^15.
static av_const unsigned int ff_sqrt(unsigned int a)
int sample_rate
samples per second
int16_t sid_lsp[LPC_ORDER]
main external API structure.
int16_t prev_weight_sig[PITCH_MAX]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation, int pitch_lag, G723_1_Subframe *subfrm, enum Rate cur_rate)
Generate adaptive codebook excitation.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
Describe the class of an AVClass context structure.
int16_t sc_gain
scaling gain
synthesis window for stochastic i
static const int16_t bandwidth_expand[LPC_ORDER]
0.994^i scaled by 2^15
static const int16_t pitch_contrib[340]
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm, enum Rate cur_rate, int pitch_lag, int index)
Generate fixed codebook excitation vector.
static int dot_product(const int16_t *a, const int16_t *b, int length)
static int sid_gain_to_lsp_index(int gain)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static const int cng_adaptive_cb_lag[4]
static int weight(int i, int blen, int offset)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
int16_t harmonic_mem[PITCH_MAX]
AVCodec ff_g723_1_decoder
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Pitch postfilter parameters.
static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, int buf_size)
Unpack the frame into parameters.
AVSampleFormat
Audio Sample Formats.
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int16_t fir_mem[LPC_ORDER]
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
static void gain_scale(G723_1_Context *p, int16_t *buf, int energy)
Adjust gain of postfiltered signal.
static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
Perform inverse quantization of LSP frequencies.
Harmonic filter parameters.
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
static int cng_rand(int *state, int base)
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
int channels
number of audio channels
else dst[i][x+y *dst_stride[i]]
static const int16_t hamming_window[LPC_FRAME]
Hamming window coefficients scaled by 2^15.
int16_t synth_mem[LPC_ORDER]
static void generate_noise(G723_1_Context *p)
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
static const uint8_t bits1[81]
int ad_cb_lag
adaptive codebook lag
static const int32_t combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.