FFmpeg
g723_1enc.c
Go to the documentation of this file.
1 /*
2  * G.723.1 compatible encoder
3  * Copyright (c) Mohamed Naufal <naufal22@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * G.723.1 compatible encoder
25  */
26 
27 #include <stdint.h>
28 #include <string.h>
29 
31 #include "libavutil/common.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 
35 #include "avcodec.h"
36 #include "celp_math.h"
37 #include "codec_internal.h"
38 #include "encode.h"
39 #include "g723_1.h"
40 
41 #define BITSTREAM_WRITER_LE
42 #include "put_bits.h"
43 
44 /**
45  * Hamming window coefficients scaled by 2^15
46  */
47 static const int16_t hamming_window[LPC_FRAME] = {
48  2621, 2631, 2659, 2705, 2770, 2853, 2955, 3074, 3212, 3367,
49  3541, 3731, 3939, 4164, 4405, 4663, 4937, 5226, 5531, 5851,
50  6186, 6534, 6897, 7273, 7661, 8062, 8475, 8899, 9334, 9780,
51  10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
52  15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
53  20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
54  25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
55  29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
56  31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
57  32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
58  31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
59  29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
60  24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
61  19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
62  14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
63  9780, 9334, 8899, 8475, 8062, 7661, 7273, 6897, 6534, 6186,
64  5851, 5531, 5226, 4937, 4663, 4405, 4164, 3939, 3731, 3541,
65  3367, 3212, 3074, 2955, 2853, 2770, 2705, 2659, 2631, 2621
66 };
67 
68 /**
69  * Binomial window coefficients scaled by 2^15
70  */
71 static const int16_t binomial_window[LPC_ORDER] = {
72  32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
73 };
74 
75 /**
76  * 0.994^i scaled by 2^15
77  */
78 static const int16_t bandwidth_expand[LPC_ORDER] = {
79  32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
80 };
81 
82 /**
83  * 0.5^i scaled by 2^15
84  */
85 static const int16_t percept_flt_tbl[2][LPC_ORDER] = {
86  /* Zero part */
87  {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
88  /* Pole part */
89  {16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32}
90 };
91 
93 {
94  G723_1_Context *s = avctx->priv_data;
95  G723_1_ChannelContext *p = &s->ch[0];
96 
97  if (avctx->sample_rate != 8000) {
98  av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
99  return AVERROR(EINVAL);
100  }
101 
102  if (avctx->bit_rate == 6300) {
103  p->cur_rate = RATE_6300;
104  } else if (avctx->bit_rate == 5300) {
105  av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
106  avpriv_report_missing_feature(avctx, "Bitrate 5300");
107  return AVERROR_PATCHWELCOME;
108  } else {
109  av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
110  return AVERROR(EINVAL);
111  }
112  avctx->frame_size = 240;
113  memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
114 
115  return 0;
116 }
117 
118 /**
119  * Remove DC component from the input signal.
120  *
121  * @param buf input signal
122  * @param fir zero memory
123  * @param iir pole memory
124  */
125 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
126 {
127  int i;
128  for (i = 0; i < FRAME_LEN; i++) {
129  *iir = (buf[i] - *fir) * (1 << 15) + MULL2(*iir, 0x7f00);
130  *fir = buf[i];
131  buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
132  }
133 }
134 
135 /**
136  * Estimate autocorrelation of the input vector.
137  *
138  * @param buf input buffer
139  * @param autocorr autocorrelation coefficients vector
140  */
141 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
142 {
143  int i, scale, temp;
144  int16_t vector[LPC_FRAME];
145 
146  ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
147 
148  /* Apply the Hamming window */
149  for (i = 0; i < LPC_FRAME; i++)
150  vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
151 
152  /* Compute the first autocorrelation coefficient */
153  temp = ff_dot_product(vector, vector, LPC_FRAME);
154 
155  /* Apply a white noise correlation factor of (1025/1024) */
156  temp += temp >> 10;
157 
158  /* Normalize */
160  autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
161  (1 << 15)) >> 16;
162 
163  /* Compute the remaining coefficients */
164  if (!autocorr[0]) {
165  memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
166  } else {
167  for (i = 1; i <= LPC_ORDER; i++) {
168  temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
169  temp = MULL2(temp * (1 << scale), binomial_window[i - 1]);
170  autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
171  }
172  }
173 }
174 
175 /**
176  * Use Levinson-Durbin recursion to compute LPC coefficients from
177  * autocorrelation values.
178  *
179  * @param lpc LPC coefficients vector
180  * @param autocorr autocorrelation coefficients vector
181  * @param error prediction error
182  */
183 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
184 {
185  int16_t vector[LPC_ORDER];
186  int16_t partial_corr;
187  int i, j, temp;
188 
189  memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
190 
191  for (i = 0; i < LPC_ORDER; i++) {
192  /* Compute the partial correlation coefficient */
193  temp = 0;
194  for (j = 0; j < i; j++)
195  temp -= lpc[j] * autocorr[i - j - 1];
196  temp = (autocorr[i] * (1 << 13) + temp) * (1 << 3);
197 
198  if (FFABS(temp) >= (error << 16))
199  break;
200 
201  partial_corr = temp / (error << 1);
202 
203  lpc[i] = (partial_corr + (1 << 1)) >> 2;
204 
205  /* Update the prediction error */
206  temp = MULL2(temp, partial_corr);
207  error = av_clipl_int32((int64_t) (error << 16) - temp +
208  (1 << 15)) >> 16;
209 
210  memcpy(vector, lpc, i * sizeof(int16_t));
211  for (j = 0; j < i; j++) {
212  temp = partial_corr * vector[i - j - 1] * 2;
213  lpc[j] = av_clipl_int32((int64_t) (lpc[j] * (1 << 16)) - temp +
214  (1 << 15)) >> 16;
215  }
216  }
217 }
218 
219 /**
220  * Calculate LPC coefficients for the current frame.
221  *
222  * @param buf current frame
223  * @param prev_data 2 trailing subframes of the previous frame
224  * @param lpc LPC coefficients vector
225  */
226 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
227 {
228  int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
229  int16_t *autocorr_ptr = autocorr;
230  int16_t *lpc_ptr = lpc;
231  int i, j;
232 
233  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
234  comp_autocorr(buf + i, autocorr_ptr);
235  levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
236 
237  lpc_ptr += LPC_ORDER;
238  autocorr_ptr += LPC_ORDER + 1;
239  }
240 }
241 
242 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
243 {
244  int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
245  ///< polynomials (F1, F2) ordered as
246  ///< f1[0], f2[0], ...., f1[5], f2[5]
247 
248  int max, shift, cur_val, prev_val, count, p;
249  int i, j;
250  int64_t temp;
251 
252  /* Initialize f1[0] and f2[0] to 1 in Q25 */
253  for (i = 0; i < LPC_ORDER; i++)
254  lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
255 
256  /* Apply bandwidth expansion on the LPC coefficients */
257  f[0] = f[1] = 1 << 25;
258 
259  /* Compute the remaining coefficients */
260  for (i = 0; i < LPC_ORDER / 2; i++) {
261  /* f1 */
262  f[2 * i + 2] = -f[2 * i] - (lsp[i] + lsp[LPC_ORDER - 1 - i]) * (1 << 12);
263  /* f2 */
264  f[2 * i + 3] = f[2 * i + 1] - (lsp[i] - lsp[LPC_ORDER - 1 - i]) * (1 << 12);
265  }
266 
267  /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
268  f[LPC_ORDER] >>= 1;
269  f[LPC_ORDER + 1] >>= 1;
270 
271  /* Normalize and shorten */
272  max = FFABS(f[0]);
273  for (i = 1; i < LPC_ORDER + 2; i++)
274  max = FFMAX(max, FFABS(f[i]));
275 
277 
278  for (i = 0; i < LPC_ORDER + 2; i++)
279  f[i] = av_clipl_int32((int64_t) (f[i] * (1 << shift)) + (1 << 15)) >> 16;
280 
281  /**
282  * Evaluate F1 and F2 at uniform intervals of pi/256 along the
283  * unit circle and check for zero crossings.
284  */
285  p = 0;
286  temp = 0;
287  for (i = 0; i <= LPC_ORDER / 2; i++)
289  prev_val = av_clipl_int32(temp << 1);
290  count = 0;
291  for (i = 1; i < COS_TBL_SIZE / 2; i++) {
292  /* Evaluate */
293  temp = 0;
294  for (j = 0; j <= LPC_ORDER / 2; j++)
295  temp += f[LPC_ORDER - 2 * j + p] * ff_g723_1_cos_tab[i * j % COS_TBL_SIZE];
296  cur_val = av_clipl_int32(temp * 2);
297 
298  /* Check for sign change, indicating a zero crossing */
299  if ((cur_val ^ prev_val) < 0) {
300  int abs_cur = FFABS(cur_val);
301  int abs_prev = FFABS(prev_val);
302  int sum = abs_cur + abs_prev;
303 
304  shift = ff_g723_1_normalize_bits(sum, 31);
305  sum <<= shift;
306  abs_prev = abs_prev << shift >> 8;
307  lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
308 
309  if (count == LPC_ORDER)
310  break;
311 
312  /* Switch between sum and difference polynomials */
313  p ^= 1;
314 
315  /* Evaluate */
316  temp = 0;
317  for (j = 0; j <= LPC_ORDER / 2; j++)
318  temp += f[LPC_ORDER - 2 * j + p] *
320  cur_val = av_clipl_int32(temp * 2);
321  }
322  prev_val = cur_val;
323  }
324 
325  if (count != LPC_ORDER)
326  memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
327 }
328 
329 /**
330  * Quantize the current LSP subvector.
331  *
332  * @param num band number
333  * @param offset offset of the current subvector in an LPC_ORDER vector
334  * @param size size of the current subvector
335  */
336 #define get_index(num, offset, size) \
337 { \
338  int error, max = -1; \
339  int16_t temp[4]; \
340  int i, j; \
341  \
342  for (i = 0; i < LSP_CB_SIZE; i++) { \
343  for (j = 0; j < size; j++){ \
344  temp[j] = (weight[j + (offset)] * ff_g723_1_lsp_band##num[i][j] + \
345  (1 << 14)) >> 15; \
346  } \
347  error = ff_g723_1_dot_product(lsp + (offset), temp, size) * 2; \
348  error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \
349  if (error > max) { \
350  max = error; \
351  lsp_index[num] = i; \
352  } \
353  } \
354 }
355 
356 /**
357  * Vector quantize the LSP frequencies.
358  *
359  * @param lsp the current lsp vector
360  * @param prev_lsp the previous lsp vector
361  */
362 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
363 {
364  int16_t weight[LPC_ORDER];
365  int16_t min, max;
366  int shift, i;
367 
368  /* Calculate the VQ weighting vector */
369  weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
370  weight[LPC_ORDER - 1] = (1 << 20) /
371  (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
372 
373  for (i = 1; i < LPC_ORDER - 1; i++) {
374  min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
375  if (min > 0x20)
376  weight[i] = (1 << 20) / min;
377  else
378  weight[i] = INT16_MAX;
379  }
380 
381  /* Normalize */
382  max = 0;
383  for (i = 0; i < LPC_ORDER; i++)
384  max = FFMAX(weight[i], max);
385 
387  for (i = 0; i < LPC_ORDER; i++) {
388  weight[i] <<= shift;
389  }
390 
391  /* Compute the VQ target vector */
392  for (i = 0; i < LPC_ORDER; i++) {
393  lsp[i] -= dc_lsp[i] +
394  (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
395  }
396 
397  get_index(0, 0, 3);
398  get_index(1, 3, 3);
399  get_index(2, 6, 4);
400 }
401 
402 /**
403  * Perform IIR filtering.
404  *
405  * @param fir_coef FIR coefficients
406  * @param iir_coef IIR coefficients
407  * @param src source vector
408  * @param dest destination vector
409  */
410 static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
411  int16_t *src, int16_t *dest)
412 {
413  int m, n;
414 
415  for (m = 0; m < SUBFRAME_LEN; m++) {
416  int64_t filter = 0;
417  for (n = 1; n <= LPC_ORDER; n++) {
418  filter -= fir_coef[n - 1] * src[m - n] -
419  iir_coef[n - 1] * dest[m - n];
420  }
421 
422  dest[m] = av_clipl_int32(src[m] * (1 << 16) + filter * (1 << 3) +
423  (1 << 15)) >> 16;
424  }
425 }
426 
427 /**
428  * Apply the formant perceptual weighting filter.
429  *
430  * @param flt_coef filter coefficients
431  * @param unq_lpc unquantized lpc vector
432  */
433 static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef,
434  int16_t *unq_lpc, int16_t *buf)
435 {
436  int16_t vector[FRAME_LEN + LPC_ORDER];
437  int i, j, k, l = 0;
438 
439  memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
440  memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
441  memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
442 
443  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
444  for (k = 0; k < LPC_ORDER; k++) {
445  flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
446  (1 << 14)) >> 15;
447  flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
448  percept_flt_tbl[1][k] +
449  (1 << 14)) >> 15;
450  }
451  iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
452  vector + i, buf + i);
453  l += LPC_ORDER;
454  }
455  memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
456  memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
457 }
458 
459 /**
460  * Estimate the open loop pitch period.
461  *
462  * @param buf perceptually weighted speech
463  * @param start estimation is carried out from this position
464  */
465 static int estimate_pitch(int16_t *buf, int start)
466 {
467  int max_exp = 32;
468  int max_ccr = 0x4000;
469  int max_eng = 0x7fff;
470  int index = PITCH_MIN;
471  int offset = start - PITCH_MIN + 1;
472 
473  int ccr, eng, orig_eng, ccr_eng, exp;
474  int diff, temp;
475 
476  int i;
477 
478  orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
479 
480  for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
481  offset--;
482 
483  /* Update energy and compute correlation */
484  orig_eng += buf[offset] * buf[offset] -
485  buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
486  ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
487  if (ccr <= 0)
488  continue;
489 
490  /* Split into mantissa and exponent to maintain precision */
491  exp = ff_g723_1_normalize_bits(ccr, 31);
492  ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
493  exp <<= 1;
494  ccr *= ccr;
495  temp = ff_g723_1_normalize_bits(ccr, 31);
496  ccr = ccr << temp >> 16;
497  exp += temp;
498 
499  temp = ff_g723_1_normalize_bits(orig_eng, 31);
500  eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
501  exp -= temp;
502 
503  if (ccr >= eng) {
504  exp--;
505  ccr >>= 1;
506  }
507  if (exp > max_exp)
508  continue;
509 
510  if (exp + 1 < max_exp)
511  goto update;
512 
513  /* Equalize exponents before comparison */
514  if (exp + 1 == max_exp)
515  temp = max_ccr >> 1;
516  else
517  temp = max_ccr;
518  ccr_eng = ccr * max_eng;
519  diff = ccr_eng - eng * temp;
520  if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
521 update:
522  index = i;
523  max_exp = exp;
524  max_ccr = ccr;
525  max_eng = eng;
526  }
527  }
528  return index;
529 }
530 
531 /**
532  * Compute harmonic noise filter parameters.
533  *
534  * @param buf perceptually weighted speech
535  * @param pitch_lag open loop pitch period
536  * @param hf harmonic filter parameters
537  */
538 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
539 {
540  int ccr, eng, max_ccr, max_eng;
541  int exp, max, diff;
542  int energy[15];
543  int i, j;
544 
545  for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
546  /* Compute residual energy */
547  energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
548  /* Compute correlation */
549  energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
550  }
551 
552  /* Compute target energy */
553  energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
554 
555  /* Normalize */
556  max = 0;
557  for (i = 0; i < 15; i++)
558  max = FFMAX(max, FFABS(energy[i]));
559 
561  for (i = 0; i < 15; i++) {
562  energy[i] = av_clipl_int32((int64_t)(energy[i] * (1 << exp)) +
563  (1 << 15)) >> 16;
564  }
565 
566  hf->index = -1;
567  hf->gain = 0;
568  max_ccr = 1;
569  max_eng = 0x7fff;
570 
571  for (i = 0; i <= 6; i++) {
572  eng = energy[i << 1];
573  ccr = energy[(i << 1) + 1];
574 
575  if (ccr <= 0)
576  continue;
577 
578  ccr = (ccr * ccr + (1 << 14)) >> 15;
579  diff = ccr * max_eng - eng * max_ccr;
580  if (diff > 0) {
581  max_ccr = ccr;
582  max_eng = eng;
583  hf->index = i;
584  }
585  }
586 
587  if (hf->index == -1) {
588  hf->index = pitch_lag;
589  return;
590  }
591 
592  eng = energy[14] * max_eng;
593  eng = (eng >> 2) + (eng >> 3);
594  ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
595  if (eng < ccr) {
596  eng = energy[(hf->index << 1) + 1];
597 
598  if (eng >= max_eng)
599  hf->gain = 0x2800;
600  else
601  hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
602  }
603  hf->index += pitch_lag - 3;
604 }
605 
606 /**
607  * Apply the harmonic noise shaping filter.
608  *
609  * @param hf filter parameters
610  */
611 static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
612 {
613  int i;
614 
615  for (i = 0; i < SUBFRAME_LEN; i++) {
616  int64_t temp = hf->gain * src[i - hf->index] * 2;
617  dest[i] = av_clipl_int32(src[i] * (1 << 16) - temp + (1 << 15)) >> 16;
618  }
619 }
620 
621 static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
622 {
623  int i;
624  for (i = 0; i < SUBFRAME_LEN; i++) {
625  int64_t temp = hf->gain * src[i - hf->index] * 2;
626  dest[i] = av_clipl_int32((dest[i] - src[i]) * (1 << 16) + temp +
627  (1 << 15)) >> 16;
628  }
629 }
630 
631 /**
632  * Combined synthesis and formant perceptual weighting filer.
633  *
634  * @param qnt_lpc quantized lpc coefficients
635  * @param perf_lpc perceptual filter coefficients
636  * @param perf_fir perceptual filter fir memory
637  * @param perf_iir perceptual filter iir memory
638  * @param scale the filter output will be scaled by 2^scale
639  */
640 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
641  int16_t *perf_fir, int16_t *perf_iir,
642  const int16_t *src, int16_t *dest, int scale)
643 {
644  int i, j;
645  int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
646  int64_t buf[SUBFRAME_LEN];
647 
648  int16_t *bptr_16 = buf_16 + LPC_ORDER;
649 
650  memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
651  memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
652 
653  for (i = 0; i < SUBFRAME_LEN; i++) {
654  int64_t temp = 0;
655  for (j = 1; j <= LPC_ORDER; j++)
656  temp -= qnt_lpc[j - 1] * bptr_16[i - j];
657 
658  buf[i] = src[i] * (1 << 15) + temp * (1 << 3);
659  bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
660  }
661 
662  for (i = 0; i < SUBFRAME_LEN; i++) {
663  int64_t fir = 0, iir = 0;
664  for (j = 1; j <= LPC_ORDER; j++) {
665  fir -= perf_lpc[j - 1] * bptr_16[i - j];
666  iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
667  }
668  dest[i] = av_clipl_int32((buf[i] + fir * (1 << 3)) * (1 << scale) + iir * (1 << 3) +
669  (1 << 15)) >> 16;
670  }
671  memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
672  memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
673  sizeof(int16_t) * LPC_ORDER);
674 }
675 
676 /**
677  * Compute the adaptive codebook contribution.
678  *
679  * @param buf input signal
680  * @param index the current subframe index
681  */
682 static void acb_search(G723_1_ChannelContext *p, int16_t *residual,
683  int16_t *impulse_resp, const int16_t *buf,
684  int index)
685 {
686  int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
687 
688  const int16_t *cb_tbl = ff_g723_1_adaptive_cb_gain85;
689 
690  int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
691 
692  int pitch_lag = p->pitch_lag[index >> 1];
693  int acb_lag = 1;
694  int acb_gain = 0;
695  int odd_frame = index & 1;
696  int iter = 3 + odd_frame;
697  int count = 0;
698  int tbl_size = 85;
699 
700  int i, j, k, l, max;
701  int64_t temp;
702 
703  if (!odd_frame) {
704  if (pitch_lag == PITCH_MIN)
705  pitch_lag++;
706  else
707  pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
708  }
709 
710  for (i = 0; i < iter; i++) {
711  ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
712 
713  for (j = 0; j < SUBFRAME_LEN; j++) {
714  temp = 0;
715  for (k = 0; k <= j; k++)
716  temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
717  flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32(temp * 2 + (1 << 15)) >> 16;
718  }
719 
720  for (j = PITCH_ORDER - 2; j >= 0; j--) {
721  flt_buf[j][0] = (residual[j] + (1 << 1)) >> 2;
722  for (k = 1; k < SUBFRAME_LEN; k++) {
723  temp = flt_buf[j + 1][k - 1] * (1 << 15) +
724  residual[j] * impulse_resp[k];
725  flt_buf[j][k] = av_clipl_int32(temp * 2 + (1 << 15)) >> 16;
726  }
727  }
728 
729  /* Compute crosscorrelation with the signal */
730  for (j = 0; j < PITCH_ORDER; j++) {
731  temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
732  ccr_buf[count++] = av_clipl_int32(temp * 2);
733  }
734 
735  /* Compute energies */
736  for (j = 0; j < PITCH_ORDER; j++) {
737  ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
738  SUBFRAME_LEN);
739  }
740 
741  for (j = 1; j < PITCH_ORDER; j++) {
742  for (k = 0; k < j; k++) {
743  temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
744  ccr_buf[count++] = av_clipl_int32(temp * (1 << 2));
745  }
746  }
747  }
748 
749  /* Normalize and shorten */
750  max = 0;
751  for (i = 0; i < 20 * iter; i++)
752  max = FFMAX(max, FFABS(ccr_buf[i]));
753 
755 
756  for (i = 0; i < 20 * iter; i++)
757  ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] * (1 << temp)) +
758  (1 << 15)) >> 16;
759 
760  max = 0;
761  for (i = 0; i < iter; i++) {
762  /* Select quantization table */
763  if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
764  odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
766  tbl_size = 170;
767  }
768 
769  for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
770  temp = 0;
771  for (l = 0; l < 20; l++)
772  temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
774 
775  if (temp > max) {
776  max = temp;
777  acb_gain = j;
778  acb_lag = i;
779  }
780  }
781  }
782 
783  if (!odd_frame) {
784  pitch_lag += acb_lag - 1;
785  acb_lag = 1;
786  }
787 
788  p->pitch_lag[index >> 1] = pitch_lag;
789  p->subframe[index].ad_cb_lag = acb_lag;
790  p->subframe[index].ad_cb_gain = acb_gain;
791 }
792 
793 /**
794  * Subtract the adaptive codebook contribution from the input
795  * to obtain the residual.
796  *
797  * @param buf target vector
798  */
799 static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
800  int16_t *buf)
801 {
802  int i, j;
803  /* Subtract adaptive CB contribution to obtain the residual */
804  for (i = 0; i < SUBFRAME_LEN; i++) {
805  int64_t temp = buf[i] * (1 << 14);
806  for (j = 0; j <= i; j++)
807  temp -= residual[j] * impulse_resp[i - j];
808 
809  buf[i] = av_clipl_int32(temp * (1 << 2) + (1 << 15)) >> 16;
810  }
811 }
812 
813 /**
814  * Quantize the residual signal using the fixed codebook (MP-MLQ).
815  *
816  * @param optim optimized fixed codebook parameters
817  * @param buf excitation vector
818  */
819 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
820  int16_t *buf, int pulse_cnt, int pitch_lag)
821 {
822  FCBParam param;
823  int16_t impulse_r[SUBFRAME_LEN];
824  int16_t temp_corr[SUBFRAME_LEN];
825  int16_t impulse_corr[SUBFRAME_LEN];
826 
827  int ccr1[SUBFRAME_LEN];
828  int ccr2[SUBFRAME_LEN];
829  int amp, err, max, max_amp_index, min, scale, i, j, k, l;
830 
831  int64_t temp;
832 
833  /* Update impulse response */
834  memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
835  param.dirac_train = 0;
836  if (pitch_lag < SUBFRAME_LEN - 2) {
837  param.dirac_train = 1;
838  ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
839  }
840 
841  for (i = 0; i < SUBFRAME_LEN; i++)
842  temp_corr[i] = impulse_r[i] >> 1;
843 
844  /* Compute impulse response autocorrelation */
845  temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
846 
848  impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
849 
850  for (i = 1; i < SUBFRAME_LEN; i++) {
851  temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
852  SUBFRAME_LEN - i);
853  impulse_corr[i] = av_clipl_int32(temp * (1 << scale) + (1 << 15)) >> 16;
854  }
855 
856  /* Compute crosscorrelation of impulse response with residual signal */
857  scale -= 4;
858  for (i = 0; i < SUBFRAME_LEN; i++) {
859  temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
860  if (scale < 0)
861  ccr1[i] = temp >> -scale;
862  else
863  ccr1[i] = av_clipl_int32(temp * (1 << scale));
864  }
865 
866  /* Search loop */
867  for (i = 0; i < GRID_SIZE; i++) {
868  /* Maximize the crosscorrelation */
869  max = 0;
870  for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
871  temp = FFABS(ccr1[j]);
872  if (temp >= max) {
873  max = temp;
874  param.pulse_pos[0] = j;
875  }
876  }
877 
878  /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
879  amp = max;
880  min = 1 << 30;
881  max_amp_index = GAIN_LEVELS - 2;
882  for (j = max_amp_index; j >= 2; j--) {
884  impulse_corr[0] << 1);
885  temp = FFABS(temp - amp);
886  if (temp < min) {
887  min = temp;
888  max_amp_index = j;
889  }
890  }
891 
892  max_amp_index--;
893  /* Select additional gain values */
894  for (j = 1; j < 5; j++) {
895  for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
896  temp_corr[k] = 0;
897  ccr2[k] = ccr1[k];
898  }
899  param.amp_index = max_amp_index + j - 2;
900  amp = ff_g723_1_fixed_cb_gain[param.amp_index];
901 
902  param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
903  temp_corr[param.pulse_pos[0]] = 1;
904 
905  for (k = 1; k < pulse_cnt; k++) {
906  max = INT_MIN;
907  for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
908  if (temp_corr[l])
909  continue;
910  temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
911  temp = av_clipl_int32((int64_t) temp *
912  param.pulse_sign[k - 1] * 2);
913  ccr2[l] -= temp;
914  temp = FFABS(ccr2[l]);
915  if (temp > max) {
916  max = temp;
917  param.pulse_pos[k] = l;
918  }
919  }
920 
921  param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
922  -amp : amp;
923  temp_corr[param.pulse_pos[k]] = 1;
924  }
925 
926  /* Create the error vector */
927  memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
928 
929  for (k = 0; k < pulse_cnt; k++)
930  temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
931 
932  for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
933  temp = 0;
934  for (l = 0; l <= k; l++) {
935  int prod = av_clipl_int32((int64_t) temp_corr[l] *
936  impulse_r[k - l] * 2);
937  temp = av_clipl_int32(temp + prod);
938  }
939  temp_corr[k] = temp >> 14;
940  }
941 
942  /* Compute square of error */
943  err = 0;
944  for (k = 0; k < SUBFRAME_LEN; k++) {
945  int64_t prod;
946  prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] * 2);
947  err = av_clipl_int32(err - prod);
948  prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
949  err = av_clipl_int32(err + prod);
950  }
951 
952  /* Minimize */
953  if (err < optim->min_err) {
954  optim->min_err = err;
955  optim->grid_index = i;
956  optim->amp_index = param.amp_index;
957  optim->dirac_train = param.dirac_train;
958 
959  for (k = 0; k < pulse_cnt; k++) {
960  optim->pulse_sign[k] = param.pulse_sign[k];
961  optim->pulse_pos[k] = param.pulse_pos[k];
962  }
963  }
964  }
965  }
966 }
967 
968 /**
969  * Encode the pulse position and gain of the current subframe.
970  *
971  * @param optim optimized fixed CB parameters
972  * @param buf excitation vector
973  */
974 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
975  int16_t *buf, int pulse_cnt)
976 {
977  int i, j;
978 
979  j = PULSE_MAX - pulse_cnt;
980 
981  subfrm->pulse_sign = 0;
982  subfrm->pulse_pos = 0;
983 
984  for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
985  int val = buf[optim->grid_index + (i << 1)];
986  if (!val) {
988  } else {
989  subfrm->pulse_sign <<= 1;
990  if (val < 0)
991  subfrm->pulse_sign++;
992  j++;
993 
994  if (j == PULSE_MAX)
995  break;
996  }
997  }
998  subfrm->amp_index = optim->amp_index;
999  subfrm->grid_index = optim->grid_index;
1000  subfrm->dirac_train = optim->dirac_train;
1001 }
1002 
1003 /**
1004  * Compute the fixed codebook excitation.
1005  *
1006  * @param buf target vector
1007  * @param impulse_resp impulse response of the combined filter
1008  */
1009 static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp,
1010  int16_t *buf, int index)
1011 {
1012  FCBParam optim;
1013  int pulse_cnt = pulses[index];
1014  int i;
1015 
1016  optim.min_err = 1 << 30;
1017  get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
1018 
1019  if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
1020  get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
1021  p->pitch_lag[index >> 1]);
1022  }
1023 
1024  /* Reconstruct the excitation */
1025  memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
1026  for (i = 0; i < pulse_cnt; i++)
1027  buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
1028 
1029  pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
1030 
1031  if (optim.dirac_train)
1032  ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
1033 }
1034 
1035 /**
1036  * Pack the frame parameters into output bitstream.
1037  *
1038  * @param frame output buffer
1039  * @param size size of the buffer
1040  */
1041 static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
1042 {
1043  PutBitContext pb;
1044  int i, temp;
1045 
1046  init_put_bits(&pb, avpkt->data, avpkt->size);
1047 
1048  put_bits(&pb, 2, info_bits);
1049 
1050  put_bits(&pb, 8, p->lsp_index[2]);
1051  put_bits(&pb, 8, p->lsp_index[1]);
1052  put_bits(&pb, 8, p->lsp_index[0]);
1053 
1054  put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
1055  put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1056  put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
1057  put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1058 
1059  /* Write 12 bit combined gain */
1060  for (i = 0; i < SUBFRAMES; i++) {
1062  p->subframe[i].amp_index;
1063  if (p->cur_rate == RATE_6300)
1064  temp += p->subframe[i].dirac_train << 11;
1065  put_bits(&pb, 12, temp);
1066  }
1067 
1068  put_bits(&pb, 1, p->subframe[0].grid_index);
1069  put_bits(&pb, 1, p->subframe[1].grid_index);
1070  put_bits(&pb, 1, p->subframe[2].grid_index);
1071  put_bits(&pb, 1, p->subframe[3].grid_index);
1072 
1073  if (p->cur_rate == RATE_6300) {
1074  put_bits(&pb, 1, 0); /* reserved bit */
1075 
1076  /* Write 13 bit combined position index */
1077  temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1078  (p->subframe[1].pulse_pos >> 14) * 90 +
1079  (p->subframe[2].pulse_pos >> 16) * 9 +
1080  (p->subframe[3].pulse_pos >> 14);
1081  put_bits(&pb, 13, temp);
1082 
1083  put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1084  put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1085  put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1086  put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1087 
1088  put_bits(&pb, 6, p->subframe[0].pulse_sign);
1089  put_bits(&pb, 5, p->subframe[1].pulse_sign);
1090  put_bits(&pb, 6, p->subframe[2].pulse_sign);
1091  put_bits(&pb, 5, p->subframe[3].pulse_sign);
1092  }
1093 
1094  flush_put_bits(&pb);
1095 }
1096 
1097 static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1098  const AVFrame *frame, int *got_packet_ptr)
1099 {
1100  G723_1_Context *s = avctx->priv_data;
1101  G723_1_ChannelContext *p = &s->ch[0];
1102  int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
1103  int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
1104  int16_t cur_lsp[LPC_ORDER];
1105  int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
1106  int16_t vector[FRAME_LEN + PITCH_MAX];
1107  int offset, ret, i, j, info_bits = 0;
1108  int16_t *in, *start;
1109  HFParam hf[4];
1110 
1111  /* duplicate input */
1112  start = in = av_memdup(frame->data[0], frame->nb_samples * sizeof(int16_t));
1113  if (!in)
1114  return AVERROR(ENOMEM);
1115 
1116  highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
1117 
1118  memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
1119  memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
1120 
1121  comp_lpc_coeff(vector, unq_lpc);
1122  lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
1123  lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
1124 
1125  /* Update memory */
1126  memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
1127  sizeof(int16_t) * SUBFRAME_LEN);
1128  memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
1129  sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
1130  memcpy(p->prev_data, in + HALF_FRAME_LEN,
1131  sizeof(int16_t) * HALF_FRAME_LEN);
1132  memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1133 
1134  perceptual_filter(p, weighted_lpc, unq_lpc, vector);
1135 
1136  memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1137  memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1138  memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1139 
1140  ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
1141 
1142  p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
1143  p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
1144 
1145  for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1146  comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
1147 
1148  memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1149  memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1150  memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
1151 
1152  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1153  harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
1154 
1155  ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
1156  ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
1157 
1158  memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
1159 
1160  offset = 0;
1161  for (i = 0; i < SUBFRAMES; i++) {
1162  int16_t impulse_resp[SUBFRAME_LEN];
1163  int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
1164  int16_t flt_in[SUBFRAME_LEN];
1165  int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
1166 
1167  /**
1168  * Compute the combined impulse response of the synthesis filter,
1169  * formant perceptual weighting filter and harmonic noise shaping filter
1170  */
1171  memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
1172  memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
1173  memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
1174 
1175  flt_in[0] = 1 << 13; /* Unit impulse */
1176  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1177  zero, zero, flt_in, vector + PITCH_MAX, 1);
1178  harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
1179 
1180  /* Compute the combined zero input response */
1181  flt_in[0] = 0;
1182  memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
1183  memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
1184 
1185  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1186  fir, iir, flt_in, vector + PITCH_MAX, 0);
1187  memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
1188  harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
1189 
1190  acb_search(p, residual, impulse_resp, in, i);
1192  p->pitch_lag[i >> 1], &p->subframe[i],
1193  p->cur_rate);
1194  sub_acb_contrib(residual, impulse_resp, in);
1195 
1196  fcb_search(p, impulse_resp, in, i);
1197 
1198  /* Reconstruct the excitation */
1200  p->pitch_lag[i >> 1], &p->subframe[i],
1201  RATE_6300);
1202 
1203  memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
1204  sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1205  for (j = 0; j < SUBFRAME_LEN; j++)
1206  in[j] = av_clip_int16(in[j] * 2 + impulse_resp[j]);
1207  memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
1208  sizeof(int16_t) * SUBFRAME_LEN);
1209 
1210  /* Update filter memories */
1211  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1212  p->perf_fir_mem, p->perf_iir_mem,
1213  in, vector + PITCH_MAX, 0);
1214  memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
1215  sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1216  memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
1217  sizeof(int16_t) * SUBFRAME_LEN);
1218 
1219  in += SUBFRAME_LEN;
1220  offset += LPC_ORDER;
1221  }
1222 
1223  av_free(start);
1224 
1225  ret = ff_get_encode_buffer(avctx, avpkt, frame_size[info_bits], 0);
1226  if (ret < 0)
1227  return ret;
1228 
1229  *got_packet_ptr = 1;
1230  pack_bitstream(p, avpkt, info_bits);
1231  return 0;
1232 }
1233 
1234 static const FFCodecDefault defaults[] = {
1235  { "b", "6300" },
1236  { NULL },
1237 };
1238 
1240  .p.name = "g723_1",
1241  CODEC_LONG_NAME("G.723.1"),
1242  .p.type = AVMEDIA_TYPE_AUDIO,
1243  .p.id = AV_CODEC_ID_G723_1,
1245  .priv_data_size = sizeof(G723_1_Context),
1248  .defaults = defaults,
1249  .p.sample_fmts = (const enum AVSampleFormat[]) {
1251  },
1252  .p.ch_layouts = (const AVChannelLayout[]){
1253  AV_CHANNEL_LAYOUT_MONO, { 0 }
1254  },
1255 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
ff_g723_1_fixed_cb_gain
const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS]
Definition: g723_1.c:454
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1077
g723_1_encode_init
static av_cold int g723_1_encode_init(AVCodecContext *avctx)
Definition: g723_1enc.c:92
G723_1_ChannelContext::prev_data
int16_t prev_data[HALF_FRAME_LEN]
Definition: g723_1.h:148
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
G723_1_Subframe::ad_cb_gain
int ad_cb_gain
Definition: g723_1.h:82
FRAME_LEN
#define FRAME_LEN
Definition: g723_1.h:37
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
G723_1_ChannelContext::pitch_lag
int pitch_lag[2]
Definition: g723_1.h:125
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
FCBParam::amp_index
int amp_index
Definition: g723_1.h:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:222
AVPacket::data
uint8_t * data
Definition: packet.h:522
G723_1_Subframe::pulse_sign
int pulse_sign
Definition: g723_1.h:84
encode.h
G723_1_Subframe::ad_cb_lag
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.h:81
FFCodec
Definition: codec_internal.h:127
defaults
static const FFCodecDefault defaults[]
Definition: g723_1enc.c:1234
G723_1_Context
Definition: g723_1.h:159
max
#define max(a, b)
Definition: cuda_runtime.h:33
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
get_index
#define get_index(num, offset, size)
Quantize the current LSP subvector.
Definition: g723_1enc.c:336
ff_g723_1_inverse_quant
void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
Perform inverse quantization of LSP frequencies.
Definition: g723_1.c:1273
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
G723_1_ChannelContext::prev_excitation
int16_t prev_excitation[PITCH_MAX]
Definition: g723_1.h:130
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
harmonic_noise_sub
static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
Definition: g723_1enc.c:621
G723_1_Subframe::pulse_pos
int pulse_pos
Definition: g723_1.h:87
ff_g723_1_normalize_bits
int ff_g723_1_normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
Definition: g723_1.c:1121
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
perceptual_filter
static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef, int16_t *unq_lpc, int16_t *buf)
Apply the formant perceptual weighting filter.
Definition: g723_1enc.c:433
PITCH_MIN
#define PITCH_MIN
Definition: g723_1.h:43
ff_g723_1_gen_dirac_train
void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
Definition: g723_1.c:1146
G723_1_ChannelContext::hpf_fir_mem
int16_t hpf_fir_mem
highpass filter fir
Definition: g723_1.h:151
G723_1_ChannelContext::prev_weight_sig
int16_t prev_weight_sig[PITCH_MAX]
Definition: g723_1.h:149
FCBParam::grid_index
int grid_index
Definition: g723_1.h:113
GRID_SIZE
#define GRID_SIZE
Definition: g723_1.h:46
val
static double val(void *priv, double ch)
Definition: aeval.c:78
update
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
Definition: af_silencedetect.c:77
PITCH_ORDER
#define PITCH_ORDER
Definition: g723_1.h:45
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
G723_1_ChannelContext::cur_rate
enum Rate cur_rate
Definition: g723_1.h:123
comp_harmonic_coeff
static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
Compute harmonic noise filter parameters.
Definition: g723_1enc.c:538
bandwidth_expand
static const int16_t bandwidth_expand[LPC_ORDER]
0.994^i scaled by 2^15
Definition: g723_1enc.c:78
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
HFParam
Harmonic filter parameters.
Definition: g723_1.h:102
G723_1_COS_TAB_FIRST_ELEMENT
#define G723_1_COS_TAB_FIRST_ELEMENT
Definition: g723_1.h:242
ff_g723_1_gen_acb_excitation
void ff_g723_1_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.
Definition: g723_1.c:1158
G723_1_ChannelContext::perf_fir_mem
int16_t perf_fir_mem[LPC_ORDER]
perceptual filter fir
Definition: g723_1.h:153
s
#define s(width, name)
Definition: cbs_vp9.c:198
frame_size
int frame_size
Definition: mxfenc.c:2422
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
G723_1_ChannelContext::fir_mem
int16_t fir_mem[LPC_ORDER]
Definition: g723_1.h:133
FCBParam::dirac_train
int dirac_train
Definition: g723_1.h:114
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
hamming_window
static const int16_t hamming_window[LPC_FRAME]
Hamming window coefficients scaled by 2^15.
Definition: g723_1enc.c:47
comp_lpc_coeff
static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
Calculate LPC coefficients for the current frame.
Definition: g723_1enc.c:226
pack_bitstream
static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
Pack the frame parameters into output bitstream.
Definition: g723_1enc.c:1041
FCBParam
Optimized fixed codebook excitation parameters.
Definition: g723_1.h:110
ff_g723_1_adaptive_cb_gain170
const int16_t ff_g723_1_adaptive_cb_gain170[170 *20]
Definition: g723_1.c:676
fcb_search
static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp, int16_t *buf, int index)
Compute the fixed codebook excitation.
Definition: g723_1enc.c:1009
PutBitContext
Definition: put_bits.h:50
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
acb_search
static void acb_search(G723_1_ChannelContext *p, int16_t *residual, int16_t *impulse_resp, const int16_t *buf, int index)
Compute the adaptive codebook contribution.
Definition: g723_1enc.c:682
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
FCBParam::pulse_pos
int pulse_pos[PULSE_MAX]
Definition: g723_1.h:115
av_clip_int16
#define av_clip_int16
Definition: common.h:113
NULL
#define NULL
Definition: coverity.c:32
ff_g723_1_lsp_interpolate
void ff_g723_1_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.
Definition: g723_1.c:1252
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
LPC_FRAME
#define LPC_FRAME
Definition: g723_1.h:39
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:492
ff_g723_1_get_residual
void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
Get delayed contribution from the previous excitation vector.
Definition: g723_1.c:1132
RATE_6300
@ RATE_6300
Definition: g723_1.h:73
lsp_quantize
static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
Vector quantize the LSP frequencies.
Definition: g723_1enc.c:362
ff_g723_1_dot_product
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:1126
G723_1_ChannelContext::subframe
G723_1_Subframe subframe[4]
Definition: g723_1.h:120
G723_1_Subframe::amp_index
int amp_index
Definition: g723_1.h:86
exp
int8_t exp
Definition: eval.c:74
ff_g723_1_adaptive_cb_gain85
const int16_t ff_g723_1_adaptive_cb_gain85[85 *20]
Definition: g723_1.c:460
FCBParam::min_err
int min_err
Definition: g723_1.h:111
index
int index
Definition: gxfenc.c:89
weight
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1562
SUBFRAMES
#define SUBFRAMES
Definition: dcaenc.c:49
GAIN_LEVELS
#define GAIN_LEVELS
Definition: g723_1.h:48
f
f
Definition: af_crystalizer.c:121
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
g723_1.h
AVPacket::size
int size
Definition: packet.h:523
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
codec_internal.h
av_clipl_int32
#define av_clipl_int32
Definition: common.h:116
shift
static int shift(int a, int b)
Definition: bonk.c:262
FCBParam::pulse_sign
int pulse_sign[PULSE_MAX]
Definition: g723_1.h:116
G723_1_ChannelContext::lsp_index
uint8_t lsp_index[LSP_BANDS]
Definition: g723_1.h:124
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
celp_math.h
HALF_FRAME_LEN
#define HALF_FRAME_LEN
Definition: g723_1.h:38
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
SUBFRAME_LEN
#define SUBFRAME_LEN
Definition: g723_1.h:36
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
levinson_durbin
static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
Use Levinson-Durbin recursion to compute LPC coefficients from autocorrelation values.
Definition: g723_1enc.c:183
G723_1_Subframe
G723.1 unpacked data subframe.
Definition: g723_1.h:80
PITCH_MAX
#define PITCH_MAX
Definition: g723_1.h:44
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
HFParam::index
int index
Definition: g723_1.h:103
percept_flt_tbl
static const int16_t percept_flt_tbl[2][LPC_ORDER]
0.5^i scaled by 2^15
Definition: g723_1enc.c:85
G723_1_ChannelContext::perf_iir_mem
int16_t perf_iir_mem[LPC_ORDER]
and iir memories
Definition: g723_1.h:154
pack_fcb_param
static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim, int16_t *buf, int pulse_cnt)
Encode the pulse position and gain of the current subframe.
Definition: g723_1enc.c:974
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
comp_autocorr
static void comp_autocorr(int16_t *buf, int16_t *autocorr)
Estimate autocorrelation of the input vector.
Definition: g723_1enc.c:141
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_dot_product
int64_t ff_dot_product(const int16_t *a, const int16_t *b, int length)
Calculate the dot product of 2 int16_t vectors.
Definition: celp_math.c:99
ff_g723_1_scale_vector
int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
Scale vector contents based on the largest of their absolutes.
Definition: g723_1.c:1104
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
g723_1_encode_frame
static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: g723_1enc.c:1097
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
HFParam::gain
int gain
Definition: g723_1.h:104
ff_g723_1_encoder
const FFCodec ff_g723_1_encoder
Definition: g723_1enc.c:1239
avcodec.h
ff_g723_1_combinatorial_table
const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.
Definition: g723_1.c:410
ret
ret
Definition: filter_design.txt:187
synth_percept_filter
static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc, int16_t *perf_fir, int16_t *perf_iir, const int16_t *src, int16_t *dest, int scale)
Combined synthesis and formant perceptual weighting filer.
Definition: g723_1enc.c:640
G723_1_ChannelContext::prev_lsp
int16_t prev_lsp[LPC_ORDER]
Definition: g723_1.h:128
highpass_filter
static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
Remove DC component from the input signal.
Definition: g723_1enc.c:125
dc_lsp
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:227
AVCodecContext
main external API structure.
Definition: avcodec.h:445
estimate_pitch
static int estimate_pitch(int16_t *buf, int start)
Estimate the open loop pitch period.
Definition: g723_1enc.c:465
channel_layout.h
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
iir_filter
static void iir_filter(int16_t *fir_coef, int16_t *iir_coef, int16_t *src, int16_t *dest)
Perform IIR filtering.
Definition: g723_1enc.c:410
sub_acb_contrib
static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp, int16_t *buf)
Subtract the adaptive codebook contribution from the input to obtain the residual.
Definition: g723_1enc.c:799
G723_1_ChannelContext::iir_mem
int iir_mem[LPC_ORDER]
Definition: g723_1.h:134
temp
else temp
Definition: vf_mcdeint.c:263
PULSE_MAX
#define PULSE_MAX
Definition: dss_sp.c:32
ff_g723_1_cos_tab
const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE+1]
Definition: g723_1.c:33
zero
#define zero
Definition: regdef.h:64
mem.h
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
lpc2lsp
static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
Definition: g723_1enc.c:242
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
G723_1_ChannelContext::harmonic_mem
int16_t harmonic_mem[PITCH_MAX]
Definition: g723_1.h:156
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
binomial_window
static const int16_t binomial_window[LPC_ORDER]
Binomial window coefficients scaled by 2^15.
Definition: g723_1enc.c:71
G723_1_Subframe::grid_index
int grid_index
Definition: g723_1.h:85
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MULL2
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
Definition: g723_1.h:57
G723_1_Subframe::dirac_train
int dirac_train
Definition: g723_1.h:83
G723_1_ChannelContext::hpf_iir_mem
int hpf_iir_mem
and iir memories
Definition: g723_1.h:152
put_bits.h
pulses
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Definition: g723_1.h:260
G723_1_ChannelContext
Definition: g723_1.h:119
get_fcb_param
static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp, int16_t *buf, int pulse_cnt, int pitch_lag)
Quantize the residual signal using the fixed codebook (MP-MLQ).
Definition: g723_1enc.c:819
COS_TBL_SIZE
#define COS_TBL_SIZE
Definition: g723_1.h:49
harmonic_filter
static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
Apply the harmonic noise shaping filter.
Definition: g723_1enc.c:611
min
float min
Definition: vorbis_enc_data.h:429