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 "encode.h"
38 #include "g723_1.h"
39 #include "internal.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->channels != 1) {
103  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
104  return AVERROR(EINVAL);
105  }
106 
107  if (avctx->bit_rate == 6300) {
108  p->cur_rate = RATE_6300;
109  } else if (avctx->bit_rate == 5300) {
110  av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
111  avpriv_report_missing_feature(avctx, "Bitrate 5300");
112  return AVERROR_PATCHWELCOME;
113  } else {
114  av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
115  return AVERROR(EINVAL);
116  }
117  avctx->frame_size = 240;
118  memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
119 
120  return 0;
121 }
122 
123 /**
124  * Remove DC component from the input signal.
125  *
126  * @param buf input signal
127  * @param fir zero memory
128  * @param iir pole memory
129  */
130 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
131 {
132  int i;
133  for (i = 0; i < FRAME_LEN; i++) {
134  *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
135  *fir = buf[i];
136  buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
137  }
138 }
139 
140 /**
141  * Estimate autocorrelation of the input vector.
142  *
143  * @param buf input buffer
144  * @param autocorr autocorrelation coefficients vector
145  */
146 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
147 {
148  int i, scale, temp;
149  int16_t vector[LPC_FRAME];
150 
151  ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
152 
153  /* Apply the Hamming window */
154  for (i = 0; i < LPC_FRAME; i++)
155  vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
156 
157  /* Compute the first autocorrelation coefficient */
158  temp = ff_dot_product(vector, vector, LPC_FRAME);
159 
160  /* Apply a white noise correlation factor of (1025/1024) */
161  temp += temp >> 10;
162 
163  /* Normalize */
165  autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
166  (1 << 15)) >> 16;
167 
168  /* Compute the remaining coefficients */
169  if (!autocorr[0]) {
170  memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
171  } else {
172  for (i = 1; i <= LPC_ORDER; i++) {
173  temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
174  temp = MULL2((temp << scale), binomial_window[i - 1]);
175  autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
176  }
177  }
178 }
179 
180 /**
181  * Use Levinson-Durbin recursion to compute LPC coefficients from
182  * autocorrelation values.
183  *
184  * @param lpc LPC coefficients vector
185  * @param autocorr autocorrelation coefficients vector
186  * @param error prediction error
187  */
188 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
189 {
190  int16_t vector[LPC_ORDER];
191  int16_t partial_corr;
192  int i, j, temp;
193 
194  memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
195 
196  for (i = 0; i < LPC_ORDER; i++) {
197  /* Compute the partial correlation coefficient */
198  temp = 0;
199  for (j = 0; j < i; j++)
200  temp -= lpc[j] * autocorr[i - j - 1];
201  temp = ((autocorr[i] << 13) + temp) << 3;
202 
203  if (FFABS(temp) >= (error << 16))
204  break;
205 
206  partial_corr = temp / (error << 1);
207 
208  lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
209  (1 << 15)) >> 16;
210 
211  /* Update the prediction error */
212  temp = MULL2(temp, partial_corr);
213  error = av_clipl_int32((int64_t) (error << 16) - temp +
214  (1 << 15)) >> 16;
215 
216  memcpy(vector, lpc, i * sizeof(int16_t));
217  for (j = 0; j < i; j++) {
218  temp = partial_corr * vector[i - j - 1] << 1;
219  lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
220  (1 << 15)) >> 16;
221  }
222  }
223 }
224 
225 /**
226  * Calculate LPC coefficients for the current frame.
227  *
228  * @param buf current frame
229  * @param prev_data 2 trailing subframes of the previous frame
230  * @param lpc LPC coefficients vector
231  */
232 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
233 {
234  int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
235  int16_t *autocorr_ptr = autocorr;
236  int16_t *lpc_ptr = lpc;
237  int i, j;
238 
239  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
240  comp_autocorr(buf + i, autocorr_ptr);
241  levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
242 
243  lpc_ptr += LPC_ORDER;
244  autocorr_ptr += LPC_ORDER + 1;
245  }
246 }
247 
248 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
249 {
250  int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
251  ///< polynomials (F1, F2) ordered as
252  ///< f1[0], f2[0], ...., f1[5], f2[5]
253 
254  int max, shift, cur_val, prev_val, count, p;
255  int i, j;
256  int64_t temp;
257 
258  /* Initialize f1[0] and f2[0] to 1 in Q25 */
259  for (i = 0; i < LPC_ORDER; i++)
260  lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
261 
262  /* Apply bandwidth expansion on the LPC coefficients */
263  f[0] = f[1] = 1 << 25;
264 
265  /* Compute the remaining coefficients */
266  for (i = 0; i < LPC_ORDER / 2; i++) {
267  /* f1 */
268  f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
269  /* f2 */
270  f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
271  }
272 
273  /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
274  f[LPC_ORDER] >>= 1;
275  f[LPC_ORDER + 1] >>= 1;
276 
277  /* Normalize and shorten */
278  max = FFABS(f[0]);
279  for (i = 1; i < LPC_ORDER + 2; i++)
280  max = FFMAX(max, FFABS(f[i]));
281 
283 
284  for (i = 0; i < LPC_ORDER + 2; i++)
285  f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
286 
287  /**
288  * Evaluate F1 and F2 at uniform intervals of pi/256 along the
289  * unit circle and check for zero crossings.
290  */
291  p = 0;
292  temp = 0;
293  for (i = 0; i <= LPC_ORDER / 2; i++)
295  prev_val = av_clipl_int32(temp << 1);
296  count = 0;
297  for (i = 1; i < COS_TBL_SIZE / 2; i++) {
298  /* Evaluate */
299  temp = 0;
300  for (j = 0; j <= LPC_ORDER / 2; j++)
301  temp += f[LPC_ORDER - 2 * j + p] * ff_g723_1_cos_tab[i * j % COS_TBL_SIZE];
302  cur_val = av_clipl_int32(temp << 1);
303 
304  /* Check for sign change, indicating a zero crossing */
305  if ((cur_val ^ prev_val) < 0) {
306  int abs_cur = FFABS(cur_val);
307  int abs_prev = FFABS(prev_val);
308  int sum = abs_cur + abs_prev;
309 
310  shift = ff_g723_1_normalize_bits(sum, 31);
311  sum <<= shift;
312  abs_prev = abs_prev << shift >> 8;
313  lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
314 
315  if (count == LPC_ORDER)
316  break;
317 
318  /* Switch between sum and difference polynomials */
319  p ^= 1;
320 
321  /* Evaluate */
322  temp = 0;
323  for (j = 0; j <= LPC_ORDER / 2; j++)
324  temp += f[LPC_ORDER - 2 * j + p] *
326  cur_val = av_clipl_int32(temp << 1);
327  }
328  prev_val = cur_val;
329  }
330 
331  if (count != LPC_ORDER)
332  memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
333 }
334 
335 /**
336  * Quantize the current LSP subvector.
337  *
338  * @param num band number
339  * @param offset offset of the current subvector in an LPC_ORDER vector
340  * @param size size of the current subvector
341  */
342 #define get_index(num, offset, size) \
343 { \
344  int error, max = -1; \
345  int16_t temp[4]; \
346  int i, j; \
347  \
348  for (i = 0; i < LSP_CB_SIZE; i++) { \
349  for (j = 0; j < size; j++){ \
350  temp[j] = (weight[j + (offset)] * ff_g723_1_lsp_band##num[i][j] + \
351  (1 << 14)) >> 15; \
352  } \
353  error = ff_g723_1_dot_product(lsp + (offset), temp, size) << 1; \
354  error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \
355  if (error > max) { \
356  max = error; \
357  lsp_index[num] = i; \
358  } \
359  } \
360 }
361 
362 /**
363  * Vector quantize the LSP frequencies.
364  *
365  * @param lsp the current lsp vector
366  * @param prev_lsp the previous lsp vector
367  */
368 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
369 {
370  int16_t weight[LPC_ORDER];
371  int16_t min, max;
372  int shift, i;
373 
374  /* Calculate the VQ weighting vector */
375  weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
376  weight[LPC_ORDER - 1] = (1 << 20) /
377  (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
378 
379  for (i = 1; i < LPC_ORDER - 1; i++) {
380  min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
381  if (min > 0x20)
382  weight[i] = (1 << 20) / min;
383  else
384  weight[i] = INT16_MAX;
385  }
386 
387  /* Normalize */
388  max = 0;
389  for (i = 0; i < LPC_ORDER; i++)
390  max = FFMAX(weight[i], max);
391 
393  for (i = 0; i < LPC_ORDER; i++) {
394  weight[i] <<= shift;
395  }
396 
397  /* Compute the VQ target vector */
398  for (i = 0; i < LPC_ORDER; i++) {
399  lsp[i] -= dc_lsp[i] +
400  (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
401  }
402 
403  get_index(0, 0, 3);
404  get_index(1, 3, 3);
405  get_index(2, 6, 4);
406 }
407 
408 /**
409  * Perform IIR filtering.
410  *
411  * @param fir_coef FIR coefficients
412  * @param iir_coef IIR coefficients
413  * @param src source vector
414  * @param dest destination vector
415  */
416 static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
417  int16_t *src, int16_t *dest)
418 {
419  int m, n;
420 
421  for (m = 0; m < SUBFRAME_LEN; m++) {
422  int64_t filter = 0;
423  for (n = 1; n <= LPC_ORDER; n++) {
424  filter -= fir_coef[n - 1] * src[m - n] -
425  iir_coef[n - 1] * dest[m - n];
426  }
427 
428  dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
429  (1 << 15)) >> 16;
430  }
431 }
432 
433 /**
434  * Apply the formant perceptual weighting filter.
435  *
436  * @param flt_coef filter coefficients
437  * @param unq_lpc unquantized lpc vector
438  */
439 static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef,
440  int16_t *unq_lpc, int16_t *buf)
441 {
442  int16_t vector[FRAME_LEN + LPC_ORDER];
443  int i, j, k, l = 0;
444 
445  memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
446  memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
447  memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
448 
449  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
450  for (k = 0; k < LPC_ORDER; k++) {
451  flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
452  (1 << 14)) >> 15;
453  flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
454  percept_flt_tbl[1][k] +
455  (1 << 14)) >> 15;
456  }
457  iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
458  vector + i, buf + i);
459  l += LPC_ORDER;
460  }
461  memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
462  memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
463 }
464 
465 /**
466  * Estimate the open loop pitch period.
467  *
468  * @param buf perceptually weighted speech
469  * @param start estimation is carried out from this position
470  */
471 static int estimate_pitch(int16_t *buf, int start)
472 {
473  int max_exp = 32;
474  int max_ccr = 0x4000;
475  int max_eng = 0x7fff;
476  int index = PITCH_MIN;
477  int offset = start - PITCH_MIN + 1;
478 
479  int ccr, eng, orig_eng, ccr_eng, exp;
480  int diff, temp;
481 
482  int i;
483 
484  orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
485 
486  for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
487  offset--;
488 
489  /* Update energy and compute correlation */
490  orig_eng += buf[offset] * buf[offset] -
491  buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
492  ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
493  if (ccr <= 0)
494  continue;
495 
496  /* Split into mantissa and exponent to maintain precision */
497  exp = ff_g723_1_normalize_bits(ccr, 31);
498  ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
499  exp <<= 1;
500  ccr *= ccr;
501  temp = ff_g723_1_normalize_bits(ccr, 31);
502  ccr = ccr << temp >> 16;
503  exp += temp;
504 
505  temp = ff_g723_1_normalize_bits(orig_eng, 31);
506  eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
507  exp -= temp;
508 
509  if (ccr >= eng) {
510  exp--;
511  ccr >>= 1;
512  }
513  if (exp > max_exp)
514  continue;
515 
516  if (exp + 1 < max_exp)
517  goto update;
518 
519  /* Equalize exponents before comparison */
520  if (exp + 1 == max_exp)
521  temp = max_ccr >> 1;
522  else
523  temp = max_ccr;
524  ccr_eng = ccr * max_eng;
525  diff = ccr_eng - eng * temp;
526  if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
527 update:
528  index = i;
529  max_exp = exp;
530  max_ccr = ccr;
531  max_eng = eng;
532  }
533  }
534  return index;
535 }
536 
537 /**
538  * Compute harmonic noise filter parameters.
539  *
540  * @param buf perceptually weighted speech
541  * @param pitch_lag open loop pitch period
542  * @param hf harmonic filter parameters
543  */
544 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
545 {
546  int ccr, eng, max_ccr, max_eng;
547  int exp, max, diff;
548  int energy[15];
549  int i, j;
550 
551  for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
552  /* Compute residual energy */
553  energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
554  /* Compute correlation */
555  energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
556  }
557 
558  /* Compute target energy */
559  energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
560 
561  /* Normalize */
562  max = 0;
563  for (i = 0; i < 15; i++)
564  max = FFMAX(max, FFABS(energy[i]));
565 
567  for (i = 0; i < 15; i++) {
568  energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
569  (1 << 15)) >> 16;
570  }
571 
572  hf->index = -1;
573  hf->gain = 0;
574  max_ccr = 1;
575  max_eng = 0x7fff;
576 
577  for (i = 0; i <= 6; i++) {
578  eng = energy[i << 1];
579  ccr = energy[(i << 1) + 1];
580 
581  if (ccr <= 0)
582  continue;
583 
584  ccr = (ccr * ccr + (1 << 14)) >> 15;
585  diff = ccr * max_eng - eng * max_ccr;
586  if (diff > 0) {
587  max_ccr = ccr;
588  max_eng = eng;
589  hf->index = i;
590  }
591  }
592 
593  if (hf->index == -1) {
594  hf->index = pitch_lag;
595  return;
596  }
597 
598  eng = energy[14] * max_eng;
599  eng = (eng >> 2) + (eng >> 3);
600  ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
601  if (eng < ccr) {
602  eng = energy[(hf->index << 1) + 1];
603 
604  if (eng >= max_eng)
605  hf->gain = 0x2800;
606  else
607  hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
608  }
609  hf->index += pitch_lag - 3;
610 }
611 
612 /**
613  * Apply the harmonic noise shaping filter.
614  *
615  * @param hf filter parameters
616  */
617 static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
618 {
619  int i;
620 
621  for (i = 0; i < SUBFRAME_LEN; i++) {
622  int64_t temp = hf->gain * src[i - hf->index] << 1;
623  dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
624  }
625 }
626 
627 static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
628 {
629  int i;
630  for (i = 0; i < SUBFRAME_LEN; i++) {
631  int64_t temp = hf->gain * src[i - hf->index] << 1;
632  dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
633  (1 << 15)) >> 16;
634  }
635 }
636 
637 /**
638  * Combined synthesis and formant perceptual weighting filer.
639  *
640  * @param qnt_lpc quantized lpc coefficients
641  * @param perf_lpc perceptual filter coefficients
642  * @param perf_fir perceptual filter fir memory
643  * @param perf_iir perceptual filter iir memory
644  * @param scale the filter output will be scaled by 2^scale
645  */
646 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
647  int16_t *perf_fir, int16_t *perf_iir,
648  const int16_t *src, int16_t *dest, int scale)
649 {
650  int i, j;
651  int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
652  int64_t buf[SUBFRAME_LEN];
653 
654  int16_t *bptr_16 = buf_16 + LPC_ORDER;
655 
656  memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
657  memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
658 
659  for (i = 0; i < SUBFRAME_LEN; i++) {
660  int64_t temp = 0;
661  for (j = 1; j <= LPC_ORDER; j++)
662  temp -= qnt_lpc[j - 1] * bptr_16[i - j];
663 
664  buf[i] = (src[i] << 15) + (temp << 3);
665  bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
666  }
667 
668  for (i = 0; i < SUBFRAME_LEN; i++) {
669  int64_t fir = 0, iir = 0;
670  for (j = 1; j <= LPC_ORDER; j++) {
671  fir -= perf_lpc[j - 1] * bptr_16[i - j];
672  iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
673  }
674  dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
675  (1 << 15)) >> 16;
676  }
677  memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
678  memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
679  sizeof(int16_t) * LPC_ORDER);
680 }
681 
682 /**
683  * Compute the adaptive codebook contribution.
684  *
685  * @param buf input signal
686  * @param index the current subframe index
687  */
688 static void acb_search(G723_1_ChannelContext *p, int16_t *residual,
689  int16_t *impulse_resp, const int16_t *buf,
690  int index)
691 {
692  int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
693 
694  const int16_t *cb_tbl = ff_g723_1_adaptive_cb_gain85;
695 
696  int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
697 
698  int pitch_lag = p->pitch_lag[index >> 1];
699  int acb_lag = 1;
700  int acb_gain = 0;
701  int odd_frame = index & 1;
702  int iter = 3 + odd_frame;
703  int count = 0;
704  int tbl_size = 85;
705 
706  int i, j, k, l, max;
707  int64_t temp;
708 
709  if (!odd_frame) {
710  if (pitch_lag == PITCH_MIN)
711  pitch_lag++;
712  else
713  pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
714  }
715 
716  for (i = 0; i < iter; i++) {
717  ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
718 
719  for (j = 0; j < SUBFRAME_LEN; j++) {
720  temp = 0;
721  for (k = 0; k <= j; k++)
722  temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
723  flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
724  (1 << 15)) >> 16;
725  }
726 
727  for (j = PITCH_ORDER - 2; j >= 0; j--) {
728  flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
729  for (k = 1; k < SUBFRAME_LEN; k++) {
730  temp = (flt_buf[j + 1][k - 1] << 15) +
731  residual[j] * impulse_resp[k];
732  flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
733  }
734  }
735 
736  /* Compute crosscorrelation with the signal */
737  for (j = 0; j < PITCH_ORDER; j++) {
738  temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
739  ccr_buf[count++] = av_clipl_int32(temp << 1);
740  }
741 
742  /* Compute energies */
743  for (j = 0; j < PITCH_ORDER; j++) {
744  ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
745  SUBFRAME_LEN);
746  }
747 
748  for (j = 1; j < PITCH_ORDER; j++) {
749  for (k = 0; k < j; k++) {
750  temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
751  ccr_buf[count++] = av_clipl_int32(temp << 2);
752  }
753  }
754  }
755 
756  /* Normalize and shorten */
757  max = 0;
758  for (i = 0; i < 20 * iter; i++)
759  max = FFMAX(max, FFABS(ccr_buf[i]));
760 
762 
763  for (i = 0; i < 20 * iter; i++)
764  ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
765  (1 << 15)) >> 16;
766 
767  max = 0;
768  for (i = 0; i < iter; i++) {
769  /* Select quantization table */
770  if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
771  odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
773  tbl_size = 170;
774  }
775 
776  for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
777  temp = 0;
778  for (l = 0; l < 20; l++)
779  temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
781 
782  if (temp > max) {
783  max = temp;
784  acb_gain = j;
785  acb_lag = i;
786  }
787  }
788  }
789 
790  if (!odd_frame) {
791  pitch_lag += acb_lag - 1;
792  acb_lag = 1;
793  }
794 
795  p->pitch_lag[index >> 1] = pitch_lag;
796  p->subframe[index].ad_cb_lag = acb_lag;
797  p->subframe[index].ad_cb_gain = acb_gain;
798 }
799 
800 /**
801  * Subtract the adaptive codebook contribution from the input
802  * to obtain the residual.
803  *
804  * @param buf target vector
805  */
806 static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
807  int16_t *buf)
808 {
809  int i, j;
810  /* Subtract adaptive CB contribution to obtain the residual */
811  for (i = 0; i < SUBFRAME_LEN; i++) {
812  int64_t temp = buf[i] << 14;
813  for (j = 0; j <= i; j++)
814  temp -= residual[j] * impulse_resp[i - j];
815 
816  buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
817  }
818 }
819 
820 /**
821  * Quantize the residual signal using the fixed codebook (MP-MLQ).
822  *
823  * @param optim optimized fixed codebook parameters
824  * @param buf excitation vector
825  */
826 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
827  int16_t *buf, int pulse_cnt, int pitch_lag)
828 {
829  FCBParam param;
830  int16_t impulse_r[SUBFRAME_LEN];
831  int16_t temp_corr[SUBFRAME_LEN];
832  int16_t impulse_corr[SUBFRAME_LEN];
833 
834  int ccr1[SUBFRAME_LEN];
835  int ccr2[SUBFRAME_LEN];
836  int amp, err, max, max_amp_index, min, scale, i, j, k, l;
837 
838  int64_t temp;
839 
840  /* Update impulse response */
841  memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
842  param.dirac_train = 0;
843  if (pitch_lag < SUBFRAME_LEN - 2) {
844  param.dirac_train = 1;
845  ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
846  }
847 
848  for (i = 0; i < SUBFRAME_LEN; i++)
849  temp_corr[i] = impulse_r[i] >> 1;
850 
851  /* Compute impulse response autocorrelation */
852  temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
853 
855  impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
856 
857  for (i = 1; i < SUBFRAME_LEN; i++) {
858  temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
859  SUBFRAME_LEN - i);
860  impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
861  }
862 
863  /* Compute crosscorrelation of impulse response with residual signal */
864  scale -= 4;
865  for (i = 0; i < SUBFRAME_LEN; i++) {
866  temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
867  if (scale < 0)
868  ccr1[i] = temp >> -scale;
869  else
870  ccr1[i] = av_clipl_int32(temp << scale);
871  }
872 
873  /* Search loop */
874  for (i = 0; i < GRID_SIZE; i++) {
875  /* Maximize the crosscorrelation */
876  max = 0;
877  for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
878  temp = FFABS(ccr1[j]);
879  if (temp >= max) {
880  max = temp;
881  param.pulse_pos[0] = j;
882  }
883  }
884 
885  /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
886  amp = max;
887  min = 1 << 30;
888  max_amp_index = GAIN_LEVELS - 2;
889  for (j = max_amp_index; j >= 2; j--) {
891  impulse_corr[0] << 1);
892  temp = FFABS(temp - amp);
893  if (temp < min) {
894  min = temp;
895  max_amp_index = j;
896  }
897  }
898 
899  max_amp_index--;
900  /* Select additional gain values */
901  for (j = 1; j < 5; j++) {
902  for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
903  temp_corr[k] = 0;
904  ccr2[k] = ccr1[k];
905  }
906  param.amp_index = max_amp_index + j - 2;
907  amp = ff_g723_1_fixed_cb_gain[param.amp_index];
908 
909  param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
910  temp_corr[param.pulse_pos[0]] = 1;
911 
912  for (k = 1; k < pulse_cnt; k++) {
913  max = INT_MIN;
914  for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
915  if (temp_corr[l])
916  continue;
917  temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
918  temp = av_clipl_int32((int64_t) temp *
919  param.pulse_sign[k - 1] << 1);
920  ccr2[l] -= temp;
921  temp = FFABS(ccr2[l]);
922  if (temp > max) {
923  max = temp;
924  param.pulse_pos[k] = l;
925  }
926  }
927 
928  param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
929  -amp : amp;
930  temp_corr[param.pulse_pos[k]] = 1;
931  }
932 
933  /* Create the error vector */
934  memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
935 
936  for (k = 0; k < pulse_cnt; k++)
937  temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
938 
939  for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
940  temp = 0;
941  for (l = 0; l <= k; l++) {
942  int prod = av_clipl_int32((int64_t) temp_corr[l] *
943  impulse_r[k - l] << 1);
944  temp = av_clipl_int32(temp + prod);
945  }
946  temp_corr[k] = temp << 2 >> 16;
947  }
948 
949  /* Compute square of error */
950  err = 0;
951  for (k = 0; k < SUBFRAME_LEN; k++) {
952  int64_t prod;
953  prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
954  err = av_clipl_int32(err - prod);
955  prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
956  err = av_clipl_int32(err + prod);
957  }
958 
959  /* Minimize */
960  if (err < optim->min_err) {
961  optim->min_err = err;
962  optim->grid_index = i;
963  optim->amp_index = param.amp_index;
964  optim->dirac_train = param.dirac_train;
965 
966  for (k = 0; k < pulse_cnt; k++) {
967  optim->pulse_sign[k] = param.pulse_sign[k];
968  optim->pulse_pos[k] = param.pulse_pos[k];
969  }
970  }
971  }
972  }
973 }
974 
975 /**
976  * Encode the pulse position and gain of the current subframe.
977  *
978  * @param optim optimized fixed CB parameters
979  * @param buf excitation vector
980  */
981 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
982  int16_t *buf, int pulse_cnt)
983 {
984  int i, j;
985 
986  j = PULSE_MAX - pulse_cnt;
987 
988  subfrm->pulse_sign = 0;
989  subfrm->pulse_pos = 0;
990 
991  for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
992  int val = buf[optim->grid_index + (i << 1)];
993  if (!val) {
995  } else {
996  subfrm->pulse_sign <<= 1;
997  if (val < 0)
998  subfrm->pulse_sign++;
999  j++;
1000 
1001  if (j == PULSE_MAX)
1002  break;
1003  }
1004  }
1005  subfrm->amp_index = optim->amp_index;
1006  subfrm->grid_index = optim->grid_index;
1007  subfrm->dirac_train = optim->dirac_train;
1008 }
1009 
1010 /**
1011  * Compute the fixed codebook excitation.
1012  *
1013  * @param buf target vector
1014  * @param impulse_resp impulse response of the combined filter
1015  */
1016 static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp,
1017  int16_t *buf, int index)
1018 {
1019  FCBParam optim;
1020  int pulse_cnt = pulses[index];
1021  int i;
1022 
1023  optim.min_err = 1 << 30;
1024  get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
1025 
1026  if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
1027  get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
1028  p->pitch_lag[index >> 1]);
1029  }
1030 
1031  /* Reconstruct the excitation */
1032  memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
1033  for (i = 0; i < pulse_cnt; i++)
1034  buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
1035 
1036  pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
1037 
1038  if (optim.dirac_train)
1039  ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
1040 }
1041 
1042 /**
1043  * Pack the frame parameters into output bitstream.
1044  *
1045  * @param frame output buffer
1046  * @param size size of the buffer
1047  */
1048 static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
1049 {
1050  PutBitContext pb;
1051  int i, temp;
1052 
1053  init_put_bits(&pb, avpkt->data, avpkt->size);
1054 
1055  put_bits(&pb, 2, info_bits);
1056 
1057  put_bits(&pb, 8, p->lsp_index[2]);
1058  put_bits(&pb, 8, p->lsp_index[1]);
1059  put_bits(&pb, 8, p->lsp_index[0]);
1060 
1061  put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
1062  put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1063  put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
1064  put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1065 
1066  /* Write 12 bit combined gain */
1067  for (i = 0; i < SUBFRAMES; i++) {
1069  p->subframe[i].amp_index;
1070  if (p->cur_rate == RATE_6300)
1071  temp += p->subframe[i].dirac_train << 11;
1072  put_bits(&pb, 12, temp);
1073  }
1074 
1075  put_bits(&pb, 1, p->subframe[0].grid_index);
1076  put_bits(&pb, 1, p->subframe[1].grid_index);
1077  put_bits(&pb, 1, p->subframe[2].grid_index);
1078  put_bits(&pb, 1, p->subframe[3].grid_index);
1079 
1080  if (p->cur_rate == RATE_6300) {
1081  put_bits(&pb, 1, 0); /* reserved bit */
1082 
1083  /* Write 13 bit combined position index */
1084  temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1085  (p->subframe[1].pulse_pos >> 14) * 90 +
1086  (p->subframe[2].pulse_pos >> 16) * 9 +
1087  (p->subframe[3].pulse_pos >> 14);
1088  put_bits(&pb, 13, temp);
1089 
1090  put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1091  put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1092  put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1093  put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1094 
1095  put_bits(&pb, 6, p->subframe[0].pulse_sign);
1096  put_bits(&pb, 5, p->subframe[1].pulse_sign);
1097  put_bits(&pb, 6, p->subframe[2].pulse_sign);
1098  put_bits(&pb, 5, p->subframe[3].pulse_sign);
1099  }
1100 
1101  flush_put_bits(&pb);
1102 }
1103 
1104 static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1105  const AVFrame *frame, int *got_packet_ptr)
1106 {
1107  G723_1_Context *s = avctx->priv_data;
1108  G723_1_ChannelContext *p = &s->ch[0];
1109  int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
1110  int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
1111  int16_t cur_lsp[LPC_ORDER];
1112  int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
1113  int16_t vector[FRAME_LEN + PITCH_MAX];
1114  int offset, ret, i, j, info_bits = 0;
1115  int16_t *in, *start;
1116  HFParam hf[4];
1117 
1118  /* duplicate input */
1119  start = in = av_memdup(frame->data[0], frame->nb_samples * sizeof(int16_t));
1120  if (!in)
1121  return AVERROR(ENOMEM);
1122 
1123  highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
1124 
1125  memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
1126  memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
1127 
1128  comp_lpc_coeff(vector, unq_lpc);
1129  lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
1130  lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
1131 
1132  /* Update memory */
1133  memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
1134  sizeof(int16_t) * SUBFRAME_LEN);
1135  memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
1136  sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
1137  memcpy(p->prev_data, in + HALF_FRAME_LEN,
1138  sizeof(int16_t) * HALF_FRAME_LEN);
1139  memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1140 
1141  perceptual_filter(p, weighted_lpc, unq_lpc, vector);
1142 
1143  memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1144  memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1145  memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1146 
1147  ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
1148 
1149  p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
1150  p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
1151 
1152  for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1153  comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
1154 
1155  memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1156  memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1157  memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
1158 
1159  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1160  harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
1161 
1162  ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
1163  ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
1164 
1165  memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
1166 
1167  offset = 0;
1168  for (i = 0; i < SUBFRAMES; i++) {
1169  int16_t impulse_resp[SUBFRAME_LEN];
1170  int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
1171  int16_t flt_in[SUBFRAME_LEN];
1172  int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
1173 
1174  /**
1175  * Compute the combined impulse response of the synthesis filter,
1176  * formant perceptual weighting filter and harmonic noise shaping filter
1177  */
1178  memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
1179  memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
1180  memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
1181 
1182  flt_in[0] = 1 << 13; /* Unit impulse */
1183  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1184  zero, zero, flt_in, vector + PITCH_MAX, 1);
1185  harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
1186 
1187  /* Compute the combined zero input response */
1188  flt_in[0] = 0;
1189  memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
1190  memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
1191 
1192  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1193  fir, iir, flt_in, vector + PITCH_MAX, 0);
1194  memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
1195  harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
1196 
1197  acb_search(p, residual, impulse_resp, in, i);
1199  p->pitch_lag[i >> 1], &p->subframe[i],
1200  p->cur_rate);
1201  sub_acb_contrib(residual, impulse_resp, in);
1202 
1203  fcb_search(p, impulse_resp, in, i);
1204 
1205  /* Reconstruct the excitation */
1207  p->pitch_lag[i >> 1], &p->subframe[i],
1208  RATE_6300);
1209 
1210  memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
1211  sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1212  for (j = 0; j < SUBFRAME_LEN; j++)
1213  in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
1214  memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
1215  sizeof(int16_t) * SUBFRAME_LEN);
1216 
1217  /* Update filter memories */
1218  synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1219  p->perf_fir_mem, p->perf_iir_mem,
1220  in, vector + PITCH_MAX, 0);
1221  memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
1222  sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1223  memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
1224  sizeof(int16_t) * SUBFRAME_LEN);
1225 
1226  in += SUBFRAME_LEN;
1227  offset += LPC_ORDER;
1228  }
1229 
1230  av_free(start);
1231 
1232  ret = ff_get_encode_buffer(avctx, avpkt, frame_size[info_bits], 0);
1233  if (ret < 0)
1234  return ret;
1235 
1236  *got_packet_ptr = 1;
1237  pack_bitstream(p, avpkt, info_bits);
1238  return 0;
1239 }
1240 
1241 static const AVCodecDefault defaults[] = {
1242  { "b", "6300" },
1243  { NULL },
1244 };
1245 
1247  .name = "g723_1",
1248  .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1249  .type = AVMEDIA_TYPE_AUDIO,
1250  .id = AV_CODEC_ID_G723_1,
1251  .capabilities = AV_CODEC_CAP_DR1,
1252  .priv_data_size = sizeof(G723_1_Context),
1254  .encode2 = g723_1_encode_frame,
1255  .defaults = defaults,
1256  .sample_fmts = (const enum AVSampleFormat[]) {
1258  },
1259  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1260 };
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:1012
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
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:992
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
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:61
FCBParam::amp_index
int amp_index
Definition: g723_1.h:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
index
fg index
Definition: ffmpeg_filter.c:167
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
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
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:342
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
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:311
harmonic_noise_sub
static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
Definition: g723_1enc.c:627
init
static int init
Definition: av_tx.c:47
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
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:439
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:76
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:78
PITCH_ORDER
#define PITCH_ORDER
Definition: g723_1.h:45
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
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:544
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:257
frame_size
int frame_size
Definition: mxfenc.c:2199
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
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:232
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:1048
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
f
#define f(width, name)
Definition: cbs_vp9.c:255
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:1016
PutBitContext
Definition: put_bits.h:49
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
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:688
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
AVCodecDefault
Definition: internal.h:215
FCBParam::pulse_pos
int pulse_pos[PULSE_MAX]
Definition: g723_1.h:115
av_clip_int16
#define av_clip_int16
Definition: common.h:111
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:433
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:475
src
#define src
Definition: vp8dsp.c:255
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:368
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:72
ff_g723_1_encoder
const AVCodec ff_g723_1_encoder
Definition: g723_1enc.c:1246
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
weight
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1561
SUBFRAMES
#define SUBFRAMES
Definition: dcaenc.c:51
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
GAIN_LEVELS
#define GAIN_LEVELS
Definition: g723_1.h:48
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_clipl_int32
#define av_clipl_int32
Definition: common.h:114
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
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
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
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:188
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
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
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:981
comp_autocorr
static void comp_autocorr(int16_t *buf, int16_t *autocorr)
Estimate autocorrelation of the input vector.
Definition: g723_1enc.c:146
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
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:100
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:58
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:1104
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
HFParam::gain
int gain
Definition: g723_1.h:104
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:646
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
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:130
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:383
estimate_pitch
static int estimate_pitch(int16_t *buf, int start)
Estimate the open loop pitch period.
Definition: g723_1enc.c:471
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:78
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:416
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:806
G723_1_ChannelContext::iir_mem
int iir_mem[LPC_ORDER]
Definition: g723_1.h:134
temp
else temp
Definition: vf_mcdeint.c:248
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
shift
static int shift(int a, int b)
Definition: sonic.c:83
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:142
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
lpc2lsp
static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
Definition: g723_1enc.c:248
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
G723_1_ChannelContext::harmonic_mem
int16_t harmonic_mem[PITCH_MAX]
Definition: g723_1.h:156
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:28
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:826
COS_TBL_SIZE
#define COS_TBL_SIZE
Definition: g723_1.h:49
defaults
static const AVCodecDefault defaults[]
Definition: g723_1enc.c:1241
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:617
min
float min
Definition: vorbis_enc_data.h:429