FFmpeg
g723_1dec.c
Go to the documentation of this file.
1 /*
2  * G.723.1 compatible decoder
3  * Copyright (c) 2006 Benjamin Larsson
4  * Copyright (c) 2010 Mohamed Naufal Basheer
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * G.723.1 compatible decoder
26  */
27 
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
31 
32 #define BITSTREAM_READER_LE
33 #include "acelp_vectors.h"
34 #include "avcodec.h"
35 #include "celp_filters.h"
36 #include "celp_math.h"
37 #include "get_bits.h"
38 #include "internal.h"
39 #include "g723_1.h"
40 
41 #define CNG_RANDOM_SEED 12345
42 
44 {
45  G723_1_Context *s = avctx->priv_data;
46 
48  if (avctx->channels < 1 || avctx->channels > 2) {
49  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n", avctx->channels);
50  return AVERROR(EINVAL);
51  }
53  for (int ch = 0; ch < avctx->channels; ch++) {
54  G723_1_ChannelContext *p = &s->ch[ch];
55 
56  p->pf_gain = 1 << 12;
57 
58  memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
59  memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp));
60 
63  }
64 
65  return 0;
66 }
67 
68 /**
69  * Unpack the frame into parameters.
70  *
71  * @param p the context
72  * @param buf pointer to the input buffer
73  * @param buf_size size of the input buffer
74  */
76  int buf_size)
77 {
78  GetBitContext gb;
79  int ad_cb_len;
80  int temp, info_bits, i;
81  int ret;
82 
83  ret = init_get_bits8(&gb, buf, buf_size);
84  if (ret < 0)
85  return ret;
86 
87  /* Extract frame type and rate info */
88  info_bits = get_bits(&gb, 2);
89 
90  if (info_bits == 3) {
92  return 0;
93  }
94 
95  /* Extract 24 bit lsp indices, 8 bit for each band */
96  p->lsp_index[2] = get_bits(&gb, 8);
97  p->lsp_index[1] = get_bits(&gb, 8);
98  p->lsp_index[0] = get_bits(&gb, 8);
99 
100  if (info_bits == 2) {
102  p->subframe[0].amp_index = get_bits(&gb, 6);
103  return 0;
104  }
105 
106  /* Extract the info common to both rates */
107  p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
109 
110  p->pitch_lag[0] = get_bits(&gb, 7);
111  if (p->pitch_lag[0] > 123) /* test if forbidden code */
112  return -1;
113  p->pitch_lag[0] += PITCH_MIN;
114  p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
115 
116  p->pitch_lag[1] = get_bits(&gb, 7);
117  if (p->pitch_lag[1] > 123)
118  return -1;
119  p->pitch_lag[1] += PITCH_MIN;
120  p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
121  p->subframe[0].ad_cb_lag = 1;
122  p->subframe[2].ad_cb_lag = 1;
123 
124  for (i = 0; i < SUBFRAMES; i++) {
125  /* Extract combined gain */
126  temp = get_bits(&gb, 12);
127  ad_cb_len = 170;
128  p->subframe[i].dirac_train = 0;
129  if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
130  p->subframe[i].dirac_train = temp >> 11;
131  temp &= 0x7FF;
132  ad_cb_len = 85;
133  }
135  if (p->subframe[i].ad_cb_gain < ad_cb_len) {
137  GAIN_LEVELS;
138  } else {
139  return -1;
140  }
141  }
142 
143  p->subframe[0].grid_index = get_bits1(&gb);
144  p->subframe[1].grid_index = get_bits1(&gb);
145  p->subframe[2].grid_index = get_bits1(&gb);
146  p->subframe[3].grid_index = get_bits1(&gb);
147 
148  if (p->cur_rate == RATE_6300) {
149  skip_bits1(&gb); /* skip reserved bit */
150 
151  /* Compute pulse_pos index using the 13-bit combined position index */
152  temp = get_bits(&gb, 13);
153  p->subframe[0].pulse_pos = temp / 810;
154 
155  temp -= p->subframe[0].pulse_pos * 810;
156  p->subframe[1].pulse_pos = FASTDIV(temp, 90);
157 
158  temp -= p->subframe[1].pulse_pos * 90;
159  p->subframe[2].pulse_pos = FASTDIV(temp, 9);
160  p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
161 
162  p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
163  get_bits(&gb, 16);
164  p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
165  get_bits(&gb, 14);
166  p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
167  get_bits(&gb, 16);
168  p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
169  get_bits(&gb, 14);
170 
171  p->subframe[0].pulse_sign = get_bits(&gb, 6);
172  p->subframe[1].pulse_sign = get_bits(&gb, 5);
173  p->subframe[2].pulse_sign = get_bits(&gb, 6);
174  p->subframe[3].pulse_sign = get_bits(&gb, 5);
175  } else { /* 5300 bps */
176  p->subframe[0].pulse_pos = get_bits(&gb, 12);
177  p->subframe[1].pulse_pos = get_bits(&gb, 12);
178  p->subframe[2].pulse_pos = get_bits(&gb, 12);
179  p->subframe[3].pulse_pos = get_bits(&gb, 12);
180 
181  p->subframe[0].pulse_sign = get_bits(&gb, 4);
182  p->subframe[1].pulse_sign = get_bits(&gb, 4);
183  p->subframe[2].pulse_sign = get_bits(&gb, 4);
184  p->subframe[3].pulse_sign = get_bits(&gb, 4);
185  }
186 
187  return 0;
188 }
189 
190 /**
191  * Bitexact implementation of sqrt(val/2).
192  */
193 static int16_t square_root(unsigned val)
194 {
195  av_assert2(!(val & 0x80000000));
196 
197  return (ff_sqrt(val << 1) >> 1) & (~1);
198 }
199 
200 /**
201  * Generate fixed codebook excitation vector.
202  *
203  * @param vector decoded excitation vector
204  * @param subfrm current subframe
205  * @param cur_rate current bitrate
206  * @param pitch_lag closed loop pitch lag
207  * @param index current subframe index
208  */
209 static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
210  enum Rate cur_rate, int pitch_lag, int index)
211 {
212  int temp, i, j;
213 
214  memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
215 
216  if (cur_rate == RATE_6300) {
217  if (subfrm->pulse_pos >= max_pos[index])
218  return;
219 
220  /* Decode amplitudes and positions */
221  j = PULSE_MAX - pulses[index];
222  temp = subfrm->pulse_pos;
223  for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
224  temp -= combinatorial_table[j][i];
225  if (temp >= 0)
226  continue;
227  temp += combinatorial_table[j++][i];
228  if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
229  vector[subfrm->grid_index + GRID_SIZE * i] =
230  -fixed_cb_gain[subfrm->amp_index];
231  } else {
232  vector[subfrm->grid_index + GRID_SIZE * i] =
233  fixed_cb_gain[subfrm->amp_index];
234  }
235  if (j == PULSE_MAX)
236  break;
237  }
238  if (subfrm->dirac_train == 1)
239  ff_g723_1_gen_dirac_train(vector, pitch_lag);
240  } else { /* 5300 bps */
241  int cb_gain = fixed_cb_gain[subfrm->amp_index];
242  int cb_shift = subfrm->grid_index;
243  int cb_sign = subfrm->pulse_sign;
244  int cb_pos = subfrm->pulse_pos;
245  int offset, beta, lag;
246 
247  for (i = 0; i < 8; i += 2) {
248  offset = ((cb_pos & 7) << 3) + cb_shift + i;
249  vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
250  cb_pos >>= 3;
251  cb_sign >>= 1;
252  }
253 
254  /* Enhance harmonic components */
255  lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
256  subfrm->ad_cb_lag - 1;
257  beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
258 
259  if (lag < SUBFRAME_LEN - 2) {
260  for (i = lag; i < SUBFRAME_LEN; i++)
261  vector[i] += beta * vector[i - lag] >> 15;
262  }
263  }
264 }
265 
266 /**
267  * Estimate maximum auto-correlation around pitch lag.
268  *
269  * @param buf buffer with offset applied
270  * @param offset offset of the excitation vector
271  * @param ccr_max pointer to the maximum auto-correlation
272  * @param pitch_lag decoded pitch lag
273  * @param length length of autocorrelation
274  * @param dir forward lag(1) / backward lag(-1)
275  */
276 static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
277  int pitch_lag, int length, int dir)
278 {
279  int limit, ccr, lag = 0;
280  int i;
281 
282  pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
283  if (dir > 0)
284  limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
285  else
286  limit = pitch_lag + 3;
287 
288  for (i = pitch_lag - 3; i <= limit; i++) {
289  ccr = ff_g723_1_dot_product(buf, buf + dir * i, length);
290 
291  if (ccr > *ccr_max) {
292  *ccr_max = ccr;
293  lag = i;
294  }
295  }
296  return lag;
297 }
298 
299 /**
300  * Calculate pitch postfilter optimal and scaling gains.
301  *
302  * @param lag pitch postfilter forward/backward lag
303  * @param ppf pitch postfilter parameters
304  * @param cur_rate current bitrate
305  * @param tgt_eng target energy
306  * @param ccr cross-correlation
307  * @param res_eng residual energy
308  */
309 static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
310  int tgt_eng, int ccr, int res_eng)
311 {
312  int pf_residual; /* square of postfiltered residual */
313  int temp1, temp2;
314 
315  ppf->index = lag;
316 
317  temp1 = tgt_eng * res_eng >> 1;
318  temp2 = ccr * ccr << 1;
319 
320  if (temp2 > temp1) {
321  if (ccr >= res_eng) {
322  ppf->opt_gain = ppf_gain_weight[cur_rate];
323  } else {
324  ppf->opt_gain = (ccr << 15) / res_eng *
325  ppf_gain_weight[cur_rate] >> 15;
326  }
327  /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
328  temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
329  temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
330  pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
331 
332  if (tgt_eng >= pf_residual << 1) {
333  temp1 = 0x7fff;
334  } else {
335  temp1 = (tgt_eng << 14) / pf_residual;
336  }
337 
338  /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
339  ppf->sc_gain = square_root(temp1 << 16);
340  } else {
341  ppf->opt_gain = 0;
342  ppf->sc_gain = 0x7fff;
343  }
344 
345  ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
346 }
347 
348 /**
349  * Calculate pitch postfilter parameters.
350  *
351  * @param p the context
352  * @param offset offset of the excitation vector
353  * @param pitch_lag decoded pitch lag
354  * @param ppf pitch postfilter parameters
355  * @param cur_rate current bitrate
356  */
357 static void comp_ppf_coeff(G723_1_ChannelContext *p, int offset, int pitch_lag,
358  PPFParam *ppf, enum Rate cur_rate)
359 {
360 
361  int16_t scale;
362  int i;
363  int temp1, temp2;
364 
365  /*
366  * 0 - target energy
367  * 1 - forward cross-correlation
368  * 2 - forward residual energy
369  * 3 - backward cross-correlation
370  * 4 - backward residual energy
371  */
372  int energy[5] = {0, 0, 0, 0, 0};
373  int16_t *buf = p->audio + LPC_ORDER + offset;
374  int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
375  SUBFRAME_LEN, 1);
376  int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
377  SUBFRAME_LEN, -1);
378 
379  ppf->index = 0;
380  ppf->opt_gain = 0;
381  ppf->sc_gain = 0x7fff;
382 
383  /* Case 0, Section 3.6 */
384  if (!back_lag && !fwd_lag)
385  return;
386 
387  /* Compute target energy */
388  energy[0] = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN);
389 
390  /* Compute forward residual energy */
391  if (fwd_lag)
392  energy[2] = ff_g723_1_dot_product(buf + fwd_lag, buf + fwd_lag,
393  SUBFRAME_LEN);
394 
395  /* Compute backward residual energy */
396  if (back_lag)
397  energy[4] = ff_g723_1_dot_product(buf - back_lag, buf - back_lag,
398  SUBFRAME_LEN);
399 
400  /* Normalize and shorten */
401  temp1 = 0;
402  for (i = 0; i < 5; i++)
403  temp1 = FFMAX(energy[i], temp1);
404 
405  scale = ff_g723_1_normalize_bits(temp1, 31);
406  for (i = 0; i < 5; i++)
407  energy[i] = (energy[i] << scale) >> 16;
408 
409  if (fwd_lag && !back_lag) { /* Case 1 */
410  comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
411  energy[2]);
412  } else if (!fwd_lag) { /* Case 2 */
413  comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
414  energy[4]);
415  } else { /* Case 3 */
416 
417  /*
418  * Select the largest of energy[1]^2/energy[2]
419  * and energy[3]^2/energy[4]
420  */
421  temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
422  temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
423  if (temp1 >= temp2) {
424  comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
425  energy[2]);
426  } else {
427  comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
428  energy[4]);
429  }
430  }
431 }
432 
433 /**
434  * Classify frames as voiced/unvoiced.
435  *
436  * @param p the context
437  * @param pitch_lag decoded pitch_lag
438  * @param exc_eng excitation energy estimation
439  * @param scale scaling factor of exc_eng
440  *
441  * @return residual interpolation index if voiced, 0 otherwise
442  */
443 static int comp_interp_index(G723_1_ChannelContext *p, int pitch_lag,
444  int *exc_eng, int *scale)
445 {
446  int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
447  int16_t *buf = p->audio + LPC_ORDER;
448 
449  int index, ccr, tgt_eng, best_eng, temp;
450 
452  buf += offset;
453 
454  /* Compute maximum backward cross-correlation */
455  ccr = 0;
456  index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
457  ccr = av_sat_add32(ccr, 1 << 15) >> 16;
458 
459  /* Compute target energy */
460  tgt_eng = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN * 2);
461  *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
462 
463  if (ccr <= 0)
464  return 0;
465 
466  /* Compute best energy */
467  best_eng = ff_g723_1_dot_product(buf - index, buf - index,
468  SUBFRAME_LEN * 2);
469  best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
470 
471  temp = best_eng * *exc_eng >> 3;
472 
473  if (temp < ccr * ccr) {
474  return index;
475  } else
476  return 0;
477 }
478 
479 /**
480  * Perform residual interpolation based on frame classification.
481  *
482  * @param buf decoded excitation vector
483  * @param out output vector
484  * @param lag decoded pitch lag
485  * @param gain interpolated gain
486  * @param rseed seed for random number generator
487  */
488 static void residual_interp(int16_t *buf, int16_t *out, int lag,
489  int gain, int *rseed)
490 {
491  int i;
492  if (lag) { /* Voiced */
493  int16_t *vector_ptr = buf + PITCH_MAX;
494  /* Attenuate */
495  for (i = 0; i < lag; i++)
496  out[i] = vector_ptr[i - lag] * 3 >> 2;
497  av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
498  (FRAME_LEN - lag) * sizeof(*out));
499  } else { /* Unvoiced */
500  for (i = 0; i < FRAME_LEN; i++) {
501  *rseed = (int16_t)(*rseed * 521 + 259);
502  out[i] = gain * *rseed >> 15;
503  }
504  memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
505  }
506 }
507 
508 /**
509  * Perform IIR filtering.
510  *
511  * @param fir_coef FIR coefficients
512  * @param iir_coef IIR coefficients
513  * @param src source vector
514  * @param dest destination vector
515  * @param width width of the output, 16 bits(0) / 32 bits(1)
516  */
517 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
518 {\
519  int m, n;\
520  int res_shift = 16 & ~-(width);\
521  int in_shift = 16 - res_shift;\
522 \
523  for (m = 0; m < SUBFRAME_LEN; m++) {\
524  int64_t filter = 0;\
525  for (n = 1; n <= LPC_ORDER; n++) {\
526  filter -= (fir_coef)[n - 1] * (src)[m - n] -\
527  (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
528  }\
529 \
530  (dest)[m] = av_clipl_int32(((src)[m] * 65536) + (filter * 8) +\
531  (1 << 15)) >> res_shift;\
532  }\
533 }
534 
535 /**
536  * Adjust gain of postfiltered signal.
537  *
538  * @param p the context
539  * @param buf postfiltered output vector
540  * @param energy input energy coefficient
541  */
542 static void gain_scale(G723_1_ChannelContext *p, int16_t * buf, int energy)
543 {
544  int num, denom, gain, bits1, bits2;
545  int i;
546 
547  num = energy;
548  denom = 0;
549  for (i = 0; i < SUBFRAME_LEN; i++) {
550  int temp = buf[i] >> 2;
551  temp *= temp;
552  denom = av_sat_dadd32(denom, temp);
553  }
554 
555  if (num && denom) {
556  bits1 = ff_g723_1_normalize_bits(num, 31);
557  bits2 = ff_g723_1_normalize_bits(denom, 31);
558  num = num << bits1 >> 1;
559  denom <<= bits2;
560 
561  bits2 = 5 + bits1 - bits2;
562  bits2 = av_clip_uintp2(bits2, 5);
563 
564  gain = (num >> 1) / (denom >> 16);
565  gain = square_root(gain << 16 >> bits2);
566  } else {
567  gain = 1 << 12;
568  }
569 
570  for (i = 0; i < SUBFRAME_LEN; i++) {
571  p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
572  buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
573  (1 << 10)) >> 11);
574  }
575 }
576 
577 /**
578  * Perform formant filtering.
579  *
580  * @param p the context
581  * @param lpc quantized lpc coefficients
582  * @param buf input buffer
583  * @param dst output buffer
584  */
585 static void formant_postfilter(G723_1_ChannelContext *p, int16_t *lpc,
586  int16_t *buf, int16_t *dst)
587 {
588  int16_t filter_coef[2][LPC_ORDER];
589  int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
590  int i, j, k;
591 
592  memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
593  memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
594 
595  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
596  for (k = 0; k < LPC_ORDER; k++) {
597  filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
598  (1 << 14)) >> 15;
599  filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
600  (1 << 14)) >> 15;
601  }
602  iir_filter(filter_coef[0], filter_coef[1], buf + i, filter_signal + i, 1);
603  lpc += LPC_ORDER;
604  }
605 
606  memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
607  memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int));
608 
609  buf += LPC_ORDER;
610  signal_ptr = filter_signal + LPC_ORDER;
611  for (i = 0; i < SUBFRAMES; i++) {
612  int temp;
613  int auto_corr[2];
614  int scale, energy;
615 
616  /* Normalize */
617  scale = ff_g723_1_scale_vector(dst, buf, SUBFRAME_LEN);
618 
619  /* Compute auto correlation coefficients */
620  auto_corr[0] = ff_g723_1_dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
621  auto_corr[1] = ff_g723_1_dot_product(dst, dst, SUBFRAME_LEN);
622 
623  /* Compute reflection coefficient */
624  temp = auto_corr[1] >> 16;
625  if (temp) {
626  temp = (auto_corr[0] >> 2) / temp;
627  }
628  p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
629  temp = -p->reflection_coef >> 1 & ~3;
630 
631  /* Compensation filter */
632  for (j = 0; j < SUBFRAME_LEN; j++) {
633  dst[j] = av_sat_dadd32(signal_ptr[j],
634  (signal_ptr[j - 1] >> 16) * temp) >> 16;
635  }
636 
637  /* Compute normalized signal energy */
638  temp = 2 * scale + 4;
639  if (temp < 0) {
640  energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
641  } else
642  energy = auto_corr[1] >> temp;
643 
644  gain_scale(p, dst, energy);
645 
646  buf += SUBFRAME_LEN;
647  signal_ptr += SUBFRAME_LEN;
648  dst += SUBFRAME_LEN;
649  }
650 }
651 
652 static int sid_gain_to_lsp_index(int gain)
653 {
654  if (gain < 0x10)
655  return gain << 6;
656  else if (gain < 0x20)
657  return gain - 8 << 7;
658  else
659  return gain - 20 << 8;
660 }
661 
662 static inline int cng_rand(int *state, int base)
663 {
664  *state = (*state * 521 + 259) & 0xFFFF;
665  return (*state & 0x7FFF) * base >> 15;
666 }
667 
669 {
670  int i, shift, seg, seg2, t, val, val_add, x, y;
671 
672  shift = 16 - p->cur_gain * 2;
673  if (shift > 0) {
674  if (p->sid_gain == 0) {
675  t = 0;
676  } else if (shift >= 31 || (int32_t)((uint32_t)p->sid_gain << shift) >> shift != p->sid_gain) {
677  if (p->sid_gain < 0) t = INT32_MIN;
678  else t = INT32_MAX;
679  } else
680  t = p->sid_gain * (1 << shift);
681  } else if(shift < -31) {
682  t = (p->sid_gain < 0) ? -1 : 0;
683  }else
684  t = p->sid_gain >> -shift;
685  x = av_clipl_int32(t * (int64_t)cng_filt[0] >> 16);
686 
687  if (x >= cng_bseg[2])
688  return 0x3F;
689 
690  if (x >= cng_bseg[1]) {
691  shift = 4;
692  seg = 3;
693  } else {
694  shift = 3;
695  seg = (x >= cng_bseg[0]);
696  }
697  seg2 = FFMIN(seg, 3);
698 
699  val = 1 << shift;
700  val_add = val >> 1;
701  for (i = 0; i < shift; i++) {
702  t = seg * 32 + (val << seg2);
703  t *= t;
704  if (x >= t)
705  val += val_add;
706  else
707  val -= val_add;
708  val_add >>= 1;
709  }
710 
711  t = seg * 32 + (val << seg2);
712  y = t * t - x;
713  if (y <= 0) {
714  t = seg * 32 + (val + 1 << seg2);
715  t = t * t - x;
716  val = (seg2 - 1) * 16 + val;
717  if (t >= y)
718  val++;
719  } else {
720  t = seg * 32 + (val - 1 << seg2);
721  t = t * t - x;
722  val = (seg2 - 1) * 16 + val;
723  if (t >= y)
724  val--;
725  }
726 
727  return val;
728 }
729 
731 {
732  int i, j, idx, t;
733  int off[SUBFRAMES];
734  int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11];
735  int tmp[SUBFRAME_LEN * 2];
736  int16_t *vector_ptr;
737  int64_t sum;
738  int b0, c, delta, x, shift;
739 
740  p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123;
741  p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123;
742 
743  for (i = 0; i < SUBFRAMES; i++) {
744  p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1;
746  }
747 
748  for (i = 0; i < SUBFRAMES / 2; i++) {
749  t = cng_rand(&p->cng_random_seed, 1 << 13);
750  off[i * 2] = t & 1;
751  off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN;
752  t >>= 2;
753  for (j = 0; j < 11; j++) {
754  signs[i * 11 + j] = ((t & 1) * 2 - 1) * (1 << 14);
755  t >>= 1;
756  }
757  }
758 
759  idx = 0;
760  for (i = 0; i < SUBFRAMES; i++) {
761  for (j = 0; j < SUBFRAME_LEN / 2; j++)
762  tmp[j] = j;
763  t = SUBFRAME_LEN / 2;
764  for (j = 0; j < pulses[i]; j++, idx++) {
765  int idx2 = cng_rand(&p->cng_random_seed, t);
766 
767  pos[idx] = tmp[idx2] * 2 + off[i];
768  tmp[idx2] = tmp[--t];
769  }
770  }
771 
772  vector_ptr = p->audio + LPC_ORDER;
773  memcpy(vector_ptr, p->prev_excitation,
774  PITCH_MAX * sizeof(*p->excitation));
775  for (i = 0; i < SUBFRAMES; i += 2) {
776  ff_g723_1_gen_acb_excitation(vector_ptr, vector_ptr,
777  p->pitch_lag[i >> 1], &p->subframe[i],
778  p->cur_rate);
780  vector_ptr + SUBFRAME_LEN,
781  p->pitch_lag[i >> 1], &p->subframe[i + 1],
782  p->cur_rate);
783 
784  t = 0;
785  for (j = 0; j < SUBFRAME_LEN * 2; j++)
786  t |= FFABS(vector_ptr[j]);
787  t = FFMIN(t, 0x7FFF);
788  if (!t) {
789  shift = 0;
790  } else {
791  shift = -10 + av_log2(t);
792  if (shift < -2)
793  shift = -2;
794  }
795  sum = 0;
796  if (shift < 0) {
797  for (j = 0; j < SUBFRAME_LEN * 2; j++) {
798  t = vector_ptr[j] * (1 << -shift);
799  sum += t * t;
800  tmp[j] = t;
801  }
802  } else {
803  for (j = 0; j < SUBFRAME_LEN * 2; j++) {
804  t = vector_ptr[j] >> shift;
805  sum += t * t;
806  tmp[j] = t;
807  }
808  }
809 
810  b0 = 0;
811  for (j = 0; j < 11; j++)
812  b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
813  b0 = b0 * 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11
814 
815  c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5);
816  if (shift * 2 + 3 >= 0)
817  c >>= shift * 2 + 3;
818  else
819  c <<= -(shift * 2 + 3);
820  c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15;
821 
822  delta = b0 * b0 * 2 - c;
823  if (delta <= 0) {
824  x = -b0;
825  } else {
827  x = delta - b0;
828  t = delta + b0;
829  if (FFABS(t) < FFABS(x))
830  x = -t;
831  }
832  shift++;
833  if (shift < 0)
834  x >>= -shift;
835  else
836  x *= 1 << shift;
837  x = av_clip(x, -10000, 10000);
838 
839  for (j = 0; j < 11; j++) {
840  idx = (i / 2) * 11 + j;
841  vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
842  (x * signs[idx] >> 15));
843  }
844 
845  /* copy decoded data to serve as a history for the next decoded subframes */
846  memcpy(vector_ptr + PITCH_MAX, vector_ptr,
847  sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
848  vector_ptr += SUBFRAME_LEN * 2;
849  }
850  /* Save the excitation for the next frame */
851  memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN,
852  PITCH_MAX * sizeof(*p->excitation));
853 }
854 
855 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
856  int *got_frame_ptr, AVPacket *avpkt)
857 {
858  G723_1_Context *s = avctx->priv_data;
859  AVFrame *frame = data;
860  const uint8_t *buf = avpkt->data;
861  int buf_size = avpkt->size;
862  int dec_mode = buf[0] & 3;
863 
864  PPFParam ppf[SUBFRAMES];
865  int16_t cur_lsp[LPC_ORDER];
866  int16_t lpc[SUBFRAMES * LPC_ORDER];
867  int16_t acb_vector[SUBFRAME_LEN];
868  int16_t *out;
869  int bad_frame = 0, i, j, ret;
870 
871  if (buf_size < frame_size[dec_mode] * avctx->channels) {
872  if (buf_size)
873  av_log(avctx, AV_LOG_WARNING,
874  "Expected %d bytes, got %d - skipping packet\n",
875  frame_size[dec_mode], buf_size);
876  *got_frame_ptr = 0;
877  return buf_size;
878  }
879 
880  frame->nb_samples = FRAME_LEN;
881  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
882  return ret;
883 
884  for (int ch = 0; ch < avctx->channels; ch++) {
885  G723_1_ChannelContext *p = &s->ch[ch];
886  int16_t *audio = p->audio;
887 
888  if (unpack_bitstream(p, buf + ch * (buf_size / avctx->channels),
889  buf_size / avctx->channels) < 0) {
890  bad_frame = 1;
891  if (p->past_frame_type == ACTIVE_FRAME)
893  else
895  }
896 
897  out = (int16_t *)frame->extended_data[ch];
898 
899  if (p->cur_frame_type == ACTIVE_FRAME) {
900  if (!bad_frame)
901  p->erased_frames = 0;
902  else if (p->erased_frames != 3)
903  p->erased_frames++;
904 
905  ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
906  ff_g723_1_lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
907 
908  /* Save the lsp_vector for the next frame */
909  memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
910 
911  /* Generate the excitation for the frame */
912  memcpy(p->excitation, p->prev_excitation,
913  PITCH_MAX * sizeof(*p->excitation));
914  if (!p->erased_frames) {
915  int16_t *vector_ptr = p->excitation + PITCH_MAX;
916 
917  /* Update interpolation gain memory */
919  p->subframe[3].amp_index) >> 1];
920  for (i = 0; i < SUBFRAMES; i++) {
921  gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
922  p->pitch_lag[i >> 1], i);
923  ff_g723_1_gen_acb_excitation(acb_vector,
924  &p->excitation[SUBFRAME_LEN * i],
925  p->pitch_lag[i >> 1],
926  &p->subframe[i], p->cur_rate);
927  /* Get the total excitation */
928  for (j = 0; j < SUBFRAME_LEN; j++) {
929  int v = av_clip_int16(vector_ptr[j] * 2);
930  vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
931  }
932  vector_ptr += SUBFRAME_LEN;
933  }
934 
935  vector_ptr = p->excitation + PITCH_MAX;
936 
938  &p->sid_gain, &p->cur_gain);
939 
940  /* Perform pitch postfiltering */
941  if (s->postfilter) {
942  i = PITCH_MAX;
943  for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
944  comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
945  ppf + j, p->cur_rate);
946 
947  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
949  vector_ptr + i,
950  vector_ptr + i + ppf[j].index,
951  ppf[j].sc_gain,
952  ppf[j].opt_gain,
953  1 << 14, 15, SUBFRAME_LEN);
954  } else {
955  audio = vector_ptr - LPC_ORDER;
956  }
957 
958  /* Save the excitation for the next frame */
959  memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
960  PITCH_MAX * sizeof(*p->excitation));
961  } else {
962  p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
963  if (p->erased_frames == 3) {
964  /* Mute output */
965  memset(p->excitation, 0,
966  (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
967  memset(p->prev_excitation, 0,
968  PITCH_MAX * sizeof(*p->excitation));
969  memset(frame->data[0], 0,
970  (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
971  } else {
972  int16_t *buf = p->audio + LPC_ORDER;
973 
974  /* Regenerate frame */
976  p->interp_gain, &p->random_seed);
977 
978  /* Save the excitation for the next frame */
979  memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
980  PITCH_MAX * sizeof(*p->excitation));
981  }
982  }
984  } else {
985  if (p->cur_frame_type == SID_FRAME) {
988  } else if (p->past_frame_type == ACTIVE_FRAME) {
989  p->sid_gain = estimate_sid_gain(p);
990  }
991 
992  if (p->past_frame_type == ACTIVE_FRAME)
993  p->cur_gain = p->sid_gain;
994  else
995  p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3;
996  generate_noise(p);
998  /* Save the lsp_vector for the next frame */
999  memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
1000  }
1001 
1003 
1004  memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
1005  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1006  ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
1007  audio + i, SUBFRAME_LEN, LPC_ORDER,
1008  0, 1, 1 << 12);
1009  memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
1010 
1011  if (s->postfilter) {
1012  formant_postfilter(p, lpc, p->audio, out);
1013  } else { // if output is not postfiltered it should be scaled by 2
1014  for (i = 0; i < FRAME_LEN; i++)
1015  out[i] = av_clip_int16(2 * p->audio[LPC_ORDER + i]);
1016  }
1017  }
1018 
1019  *got_frame_ptr = 1;
1020 
1021  return frame_size[dec_mode] * avctx->channels;
1022 }
1023 
1024 #define OFFSET(x) offsetof(G723_1_Context, x)
1025 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1026 
1027 static const AVOption options[] = {
1028  { "postfilter", "enable postfilter", OFFSET(postfilter), AV_OPT_TYPE_BOOL,
1029  { .i64 = 1 }, 0, 1, AD },
1030  { NULL }
1031 };
1032 
1033 
1034 static const AVClass g723_1dec_class = {
1035  .class_name = "G.723.1 decoder",
1036  .item_name = av_default_item_name,
1037  .option = options,
1038  .version = LIBAVUTIL_VERSION_INT,
1039 };
1040 
1042  .name = "g723_1",
1043  .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1044  .type = AVMEDIA_TYPE_AUDIO,
1045  .id = AV_CODEC_ID_G723_1,
1046  .priv_data_size = sizeof(G723_1_Context),
1049  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1050  .priv_class = &g723_1dec_class,
1051 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
square_root
static int16_t square_root(unsigned val)
Bitexact implementation of sqrt(val/2).
Definition: g723_1dec.c:193
g723_1dec_class
static const AVClass g723_1dec_class
Definition: g723_1dec.c:1034
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
acelp_vectors.h
PPFParam::index
int index
postfilter backward/forward lag
Definition: g723_1.h:94
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
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
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
G723_1_ChannelContext::interp_gain
int interp_gain
Definition: g723_1.h:139
out
FILE * out
Definition: movenc.c:54
ff_g723_1_decoder
AVCodec ff_g723_1_decoder
Definition: g723_1dec.c:1041
cng_filt
static const int cng_filt[4]
Definition: g723_1.h:1440
G723_1_ChannelContext::sid_gain
int sid_gain
Definition: g723_1.h:140
G723_1_ChannelContext::interp_index
int interp_index
Definition: g723_1.h:138
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
G723_1_ChannelContext::pitch_lag
int pitch_lag[2]
Definition: g723_1.h:125
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
gen_fcb_excitation
static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm, enum Rate cur_rate, int pitch_lag, int index)
Generate fixed codebook excitation vector.
Definition: g723_1dec.c:209
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
Rate
Rate
G723.1 rate values.
Definition: g723_1.h:72
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
max_pos
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
Definition: g723_1.h:728
AVOption
AVOption.
Definition: opt.h:246
G723_1_Subframe::pulse_sign
int pulse_sign
Definition: g723_1.h:84
AD
#define AD
Definition: g723_1dec.c:1025
data
const char data[16]
Definition: mxf.c:91
G723_1_Subframe::ad_cb_lag
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.h:81
ff_celp_lp_synthesis_filter
int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs, const int16_t *in, int buffer_length, int filter_length, int stop_on_overflow, int shift, int rounder)
LP synthesis filter.
Definition: celp_filters.c:60
generate_noise
static void generate_noise(G723_1_ChannelContext *p)
Definition: g723_1dec.c:730
base
uint8_t base
Definition: vp3data.h:202
G723_1_Context
Definition: g723_1.h:159
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:201
G723_1_ChannelContext::prev_excitation
int16_t prev_excitation[PITCH_MAX]
Definition: g723_1.h:130
combinatorial_table
static const int32_t combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.
Definition: g723_1.h:630
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:49
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
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:74
G723_1_ChannelContext::synth_mem
int16_t synth_mem[LPC_ORDER]
Definition: g723_1.h:132
GetBitContext
Definition: get_bits.h:61
GRID_SIZE
#define GRID_SIZE
Definition: g723_1.h:46
G723_1_ChannelContext::cur_frame_type
enum FrameType cur_frame_type
Definition: g723_1.h:121
ff_sqrt
#define ff_sqrt
Definition: mathops.h:206
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
G723_1_ChannelContext::cur_rate
enum Rate cur_rate
Definition: g723_1.h:123
options
static const AVOption options[]
Definition: g723_1dec.c:1027
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
state
static struct @313 state
buf
void * buf
Definition: avisynth_c.h:766
av_cold
#define av_cold
Definition: attributes.h:84
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
comp_ppf_gains
static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate, int tgt_eng, int ccr, int res_eng)
Calculate pitch postfilter optimal and scaling gains.
Definition: g723_1dec.c:309
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:86
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:426
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
G723_1_ChannelContext::pf_gain
int pf_gain
formant postfilter gain scaling unit memory
Definition: g723_1.h:143
postfilter_tbl
static const int16_t postfilter_tbl[2][LPC_ORDER]
0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
Definition: g723_1.h:1383
postfilter
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition: amrnbdec.c:904
s
#define s(width, name)
Definition: cbs_vp9.c:257
bits1
static const uint8_t bits1[81]
Definition: aactab.c:117
G723_1_ChannelContext::cng_random_seed
int cng_random_seed
Definition: g723_1.h:137
frame_size
int frame_size
Definition: mxfenc.c:2215
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
G723_1_ChannelContext::random_seed
int random_seed
Definition: g723_1.h:136
G723_1_ChannelContext::audio
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
Definition: g723_1.h:145
get_bits.h
UNTRANSMITTED_FRAME
@ UNTRANSMITTED_FRAME
Definition: g723_1.h:66
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
int32_t
int32_t
Definition: audio_convert.c:194
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
if
if(ret)
Definition: filter_design.txt:179
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
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:180
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: avcodec.h:616
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
G723_1_ChannelContext::sid_lsp
int16_t sid_lsp[LPC_ORDER]
Definition: g723_1.h:129
RATE_6300
@ RATE_6300
Definition: g723_1.h:73
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:54
gain_scale
static void gain_scale(G723_1_ChannelContext *p, int16_t *buf, int energy)
Adjust gain of postfiltered signal.
Definition: g723_1dec.c:542
G723_1_ChannelContext::subframe
G723_1_Subframe subframe[4]
Definition: g723_1.h:120
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:202
celp_filters.h
G723_1_Subframe::amp_index
int amp_index
Definition: g723_1.h:86
formant_postfilter
static void formant_postfilter(G723_1_ChannelContext *p, int16_t *lpc, int16_t *buf, int16_t *dst)
Perform formant filtering.
Definition: g723_1dec.c:585
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
G723_1_ChannelContext::past_frame_type
enum FrameType past_frame_type
Definition: g723_1.h:122
SUBFRAMES
#define SUBFRAMES
Definition: dcaenc.c:50
GAIN_LEVELS
#define GAIN_LEVELS
Definition: g723_1.h:48
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1965
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
g723_1.h
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
OFFSET
#define OFFSET(x)
Definition: g723_1dec.c:1024
G723_1_ChannelContext::lsp_index
uint8_t lsp_index[LSP_BANDS]
Definition: g723_1.h:124
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
celp_math.h
pitch_contrib
static const int16_t pitch_contrib[340]
Definition: g723_1.h:674
ACTIVE_FRAME
@ ACTIVE_FRAME
Active speech.
Definition: g723_1.h:64
SUBFRAME_LEN
#define SUBFRAME_LEN
Definition: g723_1.h:36
G723_1_ChannelContext::excitation
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
Definition: g723_1.h:131
val
const char const char void * val
Definition: avisynth_c.h:863
G723_1_Subframe
G723.1 unpacked data subframe.
Definition: g723_1.h:80
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
fixed_cb_gain
static const int16_t fixed_cb_gain[GAIN_LEVELS]
Definition: g723_1.h:730
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
G723_1_ChannelContext::reflection_coef
int reflection_coef
Definition: g723_1.h:142
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
cng_adaptive_cb_lag
static const int cng_adaptive_cb_lag[4]
Definition: g723_1.h:1438
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
PPFParam::opt_gain
int16_t opt_gain
optimal gain
Definition: g723_1.h:95
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:32
delta
float delta
Definition: vorbis_enc_data.h:457
cng_rand
static int cng_rand(int *state, int base)
Definition: g723_1dec.c:662
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
estimate_sid_gain
static int estimate_sid_gain(G723_1_ChannelContext *p)
Definition: g723_1dec.c:668
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
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
comp_interp_index
static int comp_interp_index(G723_1_ChannelContext *p, int pitch_lag, int *exc_eng, int *scale)
Classify frames as voiced/unvoiced.
Definition: g723_1dec.c:443
dc_lsp
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
Definition: g723_1.h:232
RATE_5300
@ RATE_5300
Definition: g723_1.h:74
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
residual_interp
static void residual_interp(int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
Perform residual interpolation based on frame classification.
Definition: g723_1dec.c:488
channel_layout.h
G723_1_ChannelContext::erased_frames
int erased_frames
Definition: g723_1.h:126
ppf_gain_weight
static const int16_t ppf_gain_weight[2]
Postfilter gain weighting factors scaled by 2^15.
Definition: g723_1.h:227
SID_FRAME
@ SID_FRAME
Silence Insertion Descriptor frame.
Definition: g723_1.h:65
G723_1_ChannelContext::iir_mem
int iir_mem[LPC_ORDER]
Definition: g723_1.h:134
temp
else temp
Definition: vf_mcdeint.c:256
comp_ppf_coeff
static void comp_ppf_coeff(G723_1_ChannelContext *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
Calculate pitch postfilter parameters.
Definition: g723_1dec.c:357
PULSE_MAX
#define PULSE_MAX
Definition: dss_sp.c:32
ff_acelp_weighted_vector_sum
void ff_acelp_weighted_vector_sum(int16_t *out, const int16_t *in_a, const int16_t *in_b, int16_t weight_coeff_a, int16_t weight_coeff_b, int16_t rounder, int shift, int length)
weighted sum of two vectors with rounding.
Definition: acelp_vectors.c:173
shift
static int shift(int a, int b)
Definition: sonic.c:82
mem.h
AV_CODEC_CAP_SUBFRAMES
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Definition: avcodec.h:1024
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:240
G723_1_ChannelContext::cur_gain
int cur_gain
Definition: g723_1.h:141
CNG_RANDOM_SEED
#define CNG_RANDOM_SEED
Definition: g723_1dec.c:41
G723_1_Subframe::grid_index
int grid_index
Definition: g723_1.h:85
cng_bseg
static const int cng_bseg[3]
Definition: g723_1.h:1442
iir_filter
#define iir_filter(fir_coef, iir_coef, src, dest, width)
Perform IIR filtering.
Definition: g723_1dec.c:517
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
autocorr_max
static int autocorr_max(const int16_t *buf, int offset, int *ccr_max, int pitch_lag, int length, int dir)
Estimate maximum auto-correlation around pitch lag.
Definition: g723_1dec.c:276
length
const char int length
Definition: avisynth_c.h:860
sid_gain_to_lsp_index
static int sid_gain_to_lsp_index(int gain)
Definition: g723_1dec.c:652
g723_1_decode_init
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
Definition: g723_1dec.c:43
G723_1_Subframe::dirac_train
int dirac_train
Definition: g723_1.h:83
pulses
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Definition: g723_1.h:723
bits2
static const uint8_t bits2[81]
Definition: aactab.c:140
G723_1_ChannelContext
Definition: g723_1.h:119
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
unpack_bitstream
static int unpack_bitstream(G723_1_ChannelContext *p, const uint8_t *buf, int buf_size)
Unpack the frame into parameters.
Definition: g723_1dec.c:75
PPFParam::sc_gain
int16_t sc_gain
scaling gain
Definition: g723_1.h:96
PPFParam
Pitch postfilter parameters.
Definition: g723_1.h:93
g723_1_decode_frame
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: g723_1dec.c:855