Libav
g723_1.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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #define BITSTREAM_READER_LE
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "acelp_vectors.h"
35 #include "celp_filters.h"
36 #include "g723_1_data.h"
37 #include "internal.h"
38 
39 #define CNG_RANDOM_SEED 12345
40 
44 enum FrameType {
48 };
49 
50 enum Rate {
53 };
54 
58 typedef struct {
59  int ad_cb_lag;
64  int amp_index;
65  int pulse_pos;
67 
71 typedef struct {
72  int index;
73  int16_t opt_gain;
74  int16_t sc_gain;
75 } PPFParam;
76 
77 typedef struct g723_1_context {
78  AVClass *class;
79 
80  G723_1_Subframe subframe[4];
81  enum FrameType cur_frame_type;
82  enum FrameType past_frame_type;
83  enum Rate cur_rate;
84  uint8_t lsp_index[LSP_BANDS];
85  int pitch_lag[2];
87 
88  int16_t prev_lsp[LPC_ORDER];
89  int16_t sid_lsp[LPC_ORDER];
90  int16_t prev_excitation[PITCH_MAX];
91  int16_t excitation[PITCH_MAX + FRAME_LEN + 4];
92  int16_t synth_mem[LPC_ORDER];
93  int16_t fir_mem[LPC_ORDER];
94  int iir_mem[LPC_ORDER];
95 
100  int sid_gain;
101  int cur_gain;
103  int pf_gain;
105 
106  int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX + 4];
108 
110 {
111  G723_1_Context *p = avctx->priv_data;
112 
114  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
115  avctx->channels = 1;
116  avctx->sample_rate = 8000;
117  p->pf_gain = 1 << 12;
118 
119  memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
120  memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp));
121 
124 
125  return 0;
126 }
127 
135 static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
136  int buf_size)
137 {
138  GetBitContext gb;
139  int ad_cb_len;
140  int temp, info_bits, i;
141 
142  init_get_bits(&gb, buf, buf_size * 8);
143 
144  /* Extract frame type and rate info */
145  info_bits = get_bits(&gb, 2);
146 
147  if (info_bits == 3) {
149  return 0;
150  }
151 
152  /* Extract 24 bit lsp indices, 8 bit for each band */
153  p->lsp_index[2] = get_bits(&gb, 8);
154  p->lsp_index[1] = get_bits(&gb, 8);
155  p->lsp_index[0] = get_bits(&gb, 8);
156 
157  if (info_bits == 2) {
159  p->subframe[0].amp_index = get_bits(&gb, 6);
160  return 0;
161  }
162 
163  /* Extract the info common to both rates */
164  p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
166 
167  p->pitch_lag[0] = get_bits(&gb, 7);
168  if (p->pitch_lag[0] > 123) /* test if forbidden code */
169  return -1;
170  p->pitch_lag[0] += PITCH_MIN;
171  p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
172 
173  p->pitch_lag[1] = get_bits(&gb, 7);
174  if (p->pitch_lag[1] > 123)
175  return -1;
176  p->pitch_lag[1] += PITCH_MIN;
177  p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
178  p->subframe[0].ad_cb_lag = 1;
179  p->subframe[2].ad_cb_lag = 1;
180 
181  for (i = 0; i < SUBFRAMES; i++) {
182  /* Extract combined gain */
183  temp = get_bits(&gb, 12);
184  ad_cb_len = 170;
185  p->subframe[i].dirac_train = 0;
186  if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
187  p->subframe[i].dirac_train = temp >> 11;
188  temp &= 0x7FF;
189  ad_cb_len = 85;
190  }
191  p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
192  if (p->subframe[i].ad_cb_gain < ad_cb_len) {
193  p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
194  GAIN_LEVELS;
195  } else {
196  return -1;
197  }
198  }
199 
200  p->subframe[0].grid_index = get_bits(&gb, 1);
201  p->subframe[1].grid_index = get_bits(&gb, 1);
202  p->subframe[2].grid_index = get_bits(&gb, 1);
203  p->subframe[3].grid_index = get_bits(&gb, 1);
204 
205  if (p->cur_rate == RATE_6300) {
206  skip_bits(&gb, 1); /* skip reserved bit */
207 
208  /* Compute pulse_pos index using the 13-bit combined position index */
209  temp = get_bits(&gb, 13);
210  p->subframe[0].pulse_pos = temp / 810;
211 
212  temp -= p->subframe[0].pulse_pos * 810;
213  p->subframe[1].pulse_pos = FASTDIV(temp, 90);
214 
215  temp -= p->subframe[1].pulse_pos * 90;
216  p->subframe[2].pulse_pos = FASTDIV(temp, 9);
217  p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
218 
219  p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
220  get_bits(&gb, 16);
221  p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
222  get_bits(&gb, 14);
223  p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
224  get_bits(&gb, 16);
225  p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
226  get_bits(&gb, 14);
227 
228  p->subframe[0].pulse_sign = get_bits(&gb, 6);
229  p->subframe[1].pulse_sign = get_bits(&gb, 5);
230  p->subframe[2].pulse_sign = get_bits(&gb, 6);
231  p->subframe[3].pulse_sign = get_bits(&gb, 5);
232  } else { /* 5300 bps */
233  p->subframe[0].pulse_pos = get_bits(&gb, 12);
234  p->subframe[1].pulse_pos = get_bits(&gb, 12);
235  p->subframe[2].pulse_pos = get_bits(&gb, 12);
236  p->subframe[3].pulse_pos = get_bits(&gb, 12);
237 
238  p->subframe[0].pulse_sign = get_bits(&gb, 4);
239  p->subframe[1].pulse_sign = get_bits(&gb, 4);
240  p->subframe[2].pulse_sign = get_bits(&gb, 4);
241  p->subframe[3].pulse_sign = get_bits(&gb, 4);
242  }
243 
244  return 0;
245 }
246 
250 static int16_t square_root(int val)
251 {
252  int16_t res = 0;
253  int16_t exp = 0x4000;
254  int i;
255 
256  for (i = 0; i < 14; i ++) {
257  int res_exp = res + exp;
258  if (val >= res_exp * res_exp << 1)
259  res += exp;
260  exp >>= 1;
261  }
262  return res;
263 }
264 
271 static int normalize_bits(int num, int width)
272 {
273  return width - av_log2(num) - 1;
274 }
275 
279 static int scale_vector(int16_t *dst, const int16_t *vector, int length)
280 {
281  int bits, max = 0;
282  int i;
283 
284 
285  for (i = 0; i < length; i++)
286  max |= FFABS(vector[i]);
287 
288  max = FFMIN(max, 0x7FFF);
289  bits = normalize_bits(max, 15);
290 
291  for (i = 0; i < length; i++)
292  dst[i] = vector[i] << bits >> 3;
293 
294  return bits - 3;
295 }
296 
305 static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
306  uint8_t *lsp_index, int bad_frame)
307 {
308  int min_dist, pred;
309  int i, j, temp, stable;
310 
311  /* Check for frame erasure */
312  if (!bad_frame) {
313  min_dist = 0x100;
314  pred = 12288;
315  } else {
316  min_dist = 0x200;
317  pred = 23552;
318  lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
319  }
320 
321  /* Get the VQ table entry corresponding to the transmitted index */
322  cur_lsp[0] = lsp_band0[lsp_index[0]][0];
323  cur_lsp[1] = lsp_band0[lsp_index[0]][1];
324  cur_lsp[2] = lsp_band0[lsp_index[0]][2];
325  cur_lsp[3] = lsp_band1[lsp_index[1]][0];
326  cur_lsp[4] = lsp_band1[lsp_index[1]][1];
327  cur_lsp[5] = lsp_band1[lsp_index[1]][2];
328  cur_lsp[6] = lsp_band2[lsp_index[2]][0];
329  cur_lsp[7] = lsp_band2[lsp_index[2]][1];
330  cur_lsp[8] = lsp_band2[lsp_index[2]][2];
331  cur_lsp[9] = lsp_band2[lsp_index[2]][3];
332 
333  /* Add predicted vector & DC component to the previously quantized vector */
334  for (i = 0; i < LPC_ORDER; i++) {
335  temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
336  cur_lsp[i] += dc_lsp[i] + temp;
337  }
338 
339  for (i = 0; i < LPC_ORDER; i++) {
340  cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
341  cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
342 
343  /* Stability check */
344  for (j = 1; j < LPC_ORDER; j++) {
345  temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
346  if (temp > 0) {
347  temp >>= 1;
348  cur_lsp[j - 1] -= temp;
349  cur_lsp[j] += temp;
350  }
351  }
352  stable = 1;
353  for (j = 1; j < LPC_ORDER; j++) {
354  temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
355  if (temp > 0) {
356  stable = 0;
357  break;
358  }
359  }
360  if (stable)
361  break;
362  }
363  if (!stable)
364  memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
365 }
366 
373 #define MULL2(a, b) \
374  ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
375 
381 static void lsp2lpc(int16_t *lpc)
382 {
383  int f1[LPC_ORDER / 2 + 1];
384  int f2[LPC_ORDER / 2 + 1];
385  int i, j;
386 
387  /* Calculate negative cosine */
388  for (j = 0; j < LPC_ORDER; j++) {
389  int index = lpc[j] >> 7;
390  int offset = lpc[j] & 0x7f;
391  int temp1 = cos_tab[index] << 16;
392  int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
393  ((offset << 8) + 0x80) << 1;
394 
395  lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
396  }
397 
398  /*
399  * Compute sum and difference polynomial coefficients
400  * (bitexact alternative to lsp2poly() in lsp.c)
401  */
402  /* Initialize with values in Q28 */
403  f1[0] = 1 << 28;
404  f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
405  f1[2] = lpc[0] * lpc[2] + (2 << 28);
406 
407  f2[0] = 1 << 28;
408  f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
409  f2[2] = lpc[1] * lpc[3] + (2 << 28);
410 
411  /*
412  * Calculate and scale the coefficients by 1/2 in
413  * each iteration for a final scaling factor of Q25
414  */
415  for (i = 2; i < LPC_ORDER / 2; i++) {
416  f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
417  f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
418 
419  for (j = i; j >= 2; j--) {
420  f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
421  (f1[j] >> 1) + (f1[j - 2] >> 1);
422  f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
423  (f2[j] >> 1) + (f2[j - 2] >> 1);
424  }
425 
426  f1[0] >>= 1;
427  f2[0] >>= 1;
428  f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
429  f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
430  }
431 
432  /* Convert polynomial coefficients to LPC coefficients */
433  for (i = 0; i < LPC_ORDER / 2; i++) {
434  int64_t ff1 = f1[i + 1] + f1[i];
435  int64_t ff2 = f2[i + 1] - f2[i];
436 
437  lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
438  lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
439  (1 << 15)) >> 16;
440  }
441 }
442 
451 static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
452 {
453  int i;
454  int16_t *lpc_ptr = lpc;
455 
456  /* cur_lsp * 0.25 + prev_lsp * 0.75 */
457  ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
458  4096, 12288, 1 << 13, 14, LPC_ORDER);
459  ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
460  8192, 8192, 1 << 13, 14, LPC_ORDER);
461  ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
462  12288, 4096, 1 << 13, 14, LPC_ORDER);
463  memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
464 
465  for (i = 0; i < SUBFRAMES; i++) {
466  lsp2lpc(lpc_ptr);
467  lpc_ptr += LPC_ORDER;
468  }
469 }
470 
474 static void gen_dirac_train(int16_t *buf, int pitch_lag)
475 {
476  int16_t vector[SUBFRAME_LEN];
477  int i, j;
478 
479  memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
480  for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
481  for (j = 0; j < SUBFRAME_LEN - i; j++)
482  buf[i + j] += vector[j];
483  }
484 }
485 
495 static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
496  enum Rate cur_rate, int pitch_lag, int index)
497 {
498  int temp, i, j;
499 
500  memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
501 
502  if (cur_rate == RATE_6300) {
503  if (subfrm->pulse_pos >= max_pos[index])
504  return;
505 
506  /* Decode amplitudes and positions */
507  j = PULSE_MAX - pulses[index];
508  temp = subfrm->pulse_pos;
509  for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
510  temp -= combinatorial_table[j][i];
511  if (temp >= 0)
512  continue;
513  temp += combinatorial_table[j++][i];
514  if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
515  vector[subfrm->grid_index + GRID_SIZE * i] =
516  -fixed_cb_gain[subfrm->amp_index];
517  } else {
518  vector[subfrm->grid_index + GRID_SIZE * i] =
519  fixed_cb_gain[subfrm->amp_index];
520  }
521  if (j == PULSE_MAX)
522  break;
523  }
524  if (subfrm->dirac_train == 1)
525  gen_dirac_train(vector, pitch_lag);
526  } else { /* 5300 bps */
527  int cb_gain = fixed_cb_gain[subfrm->amp_index];
528  int cb_shift = subfrm->grid_index;
529  int cb_sign = subfrm->pulse_sign;
530  int cb_pos = subfrm->pulse_pos;
531  int offset, beta, lag;
532 
533  for (i = 0; i < 8; i += 2) {
534  offset = ((cb_pos & 7) << 3) + cb_shift + i;
535  vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
536  cb_pos >>= 3;
537  cb_sign >>= 1;
538  }
539 
540  /* Enhance harmonic components */
541  lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
542  subfrm->ad_cb_lag - 1;
543  beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
544 
545  if (lag < SUBFRAME_LEN - 2) {
546  for (i = lag; i < SUBFRAME_LEN; i++)
547  vector[i] += beta * vector[i - lag] >> 15;
548  }
549  }
550 }
551 
555 static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
556 {
557  int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
558  int i;
559 
560  residual[0] = prev_excitation[offset];
561  residual[1] = prev_excitation[offset + 1];
562 
563  offset += 2;
564  for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
565  residual[i] = prev_excitation[offset + (i - 2) % lag];
566 }
567 
568 static int dot_product(const int16_t *a, const int16_t *b, int length)
569 {
570  int i, sum = 0;
571 
572  for (i = 0; i < length; i++) {
573  int prod = a[i] * b[i];
574  sum = av_sat_dadd32(sum, prod);
575  }
576  return sum;
577 }
578 
582 static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
583  int pitch_lag, G723_1_Subframe *subfrm,
584  enum Rate cur_rate)
585 {
586  int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
587  const int16_t *cb_ptr;
588  int lag = pitch_lag + subfrm->ad_cb_lag - 1;
589 
590  int i;
591  int sum;
592 
593  get_residual(residual, prev_excitation, lag);
594 
595  /* Select quantization table */
596  if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2)
597  cb_ptr = adaptive_cb_gain85;
598  else
599  cb_ptr = adaptive_cb_gain170;
600 
601  /* Calculate adaptive vector */
602  cb_ptr += subfrm->ad_cb_gain * 20;
603  for (i = 0; i < SUBFRAME_LEN; i++) {
604  sum = dot_product(residual + i, cb_ptr, PITCH_ORDER);
605  vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
606  }
607 }
608 
619 static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
620  int pitch_lag, int length, int dir)
621 {
622  int limit, ccr, lag = 0;
623  int i;
624 
625  pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
626  if (dir > 0)
627  limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
628  else
629  limit = pitch_lag + 3;
630 
631  for (i = pitch_lag - 3; i <= limit; i++) {
632  ccr = dot_product(buf, buf + dir * i, length);
633 
634  if (ccr > *ccr_max) {
635  *ccr_max = ccr;
636  lag = i;
637  }
638  }
639  return lag;
640 }
641 
652 static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
653  int tgt_eng, int ccr, int res_eng)
654 {
655  int pf_residual; /* square of postfiltered residual */
656  int temp1, temp2;
657 
658  ppf->index = lag;
659 
660  temp1 = tgt_eng * res_eng >> 1;
661  temp2 = ccr * ccr << 1;
662 
663  if (temp2 > temp1) {
664  if (ccr >= res_eng) {
665  ppf->opt_gain = ppf_gain_weight[cur_rate];
666  } else {
667  ppf->opt_gain = (ccr << 15) / res_eng *
668  ppf_gain_weight[cur_rate] >> 15;
669  }
670  /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
671  temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
672  temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
673  pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
674 
675  if (tgt_eng >= pf_residual << 1) {
676  temp1 = 0x7fff;
677  } else {
678  temp1 = (tgt_eng << 14) / pf_residual;
679  }
680 
681  /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
682  ppf->sc_gain = square_root(temp1 << 16);
683  } else {
684  ppf->opt_gain = 0;
685  ppf->sc_gain = 0x7fff;
686  }
687 
688  ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
689 }
690 
700 static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
701  PPFParam *ppf, enum Rate cur_rate)
702 {
703 
704  int16_t scale;
705  int i;
706  int temp1, temp2;
707 
708  /*
709  * 0 - target energy
710  * 1 - forward cross-correlation
711  * 2 - forward residual energy
712  * 3 - backward cross-correlation
713  * 4 - backward residual energy
714  */
715  int energy[5] = {0, 0, 0, 0, 0};
716  int16_t *buf = p->audio + LPC_ORDER + offset;
717  int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
718  SUBFRAME_LEN, 1);
719  int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
720  SUBFRAME_LEN, -1);
721 
722  ppf->index = 0;
723  ppf->opt_gain = 0;
724  ppf->sc_gain = 0x7fff;
725 
726  /* Case 0, Section 3.6 */
727  if (!back_lag && !fwd_lag)
728  return;
729 
730  /* Compute target energy */
731  energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
732 
733  /* Compute forward residual energy */
734  if (fwd_lag)
735  energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
736 
737  /* Compute backward residual energy */
738  if (back_lag)
739  energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
740 
741  /* Normalize and shorten */
742  temp1 = 0;
743  for (i = 0; i < 5; i++)
744  temp1 = FFMAX(energy[i], temp1);
745 
746  scale = normalize_bits(temp1, 31);
747  for (i = 0; i < 5; i++)
748  energy[i] = (energy[i] << scale) >> 16;
749 
750  if (fwd_lag && !back_lag) { /* Case 1 */
751  comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
752  energy[2]);
753  } else if (!fwd_lag) { /* Case 2 */
754  comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
755  energy[4]);
756  } else { /* Case 3 */
757 
758  /*
759  * Select the largest of energy[1]^2/energy[2]
760  * and energy[3]^2/energy[4]
761  */
762  temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
763  temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
764  if (temp1 >= temp2) {
765  comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
766  energy[2]);
767  } else {
768  comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
769  energy[4]);
770  }
771  }
772 }
773 
784 static int comp_interp_index(G723_1_Context *p, int pitch_lag,
785  int *exc_eng, int *scale)
786 {
787  int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
788  int16_t *buf = p->audio + LPC_ORDER;
789 
790  int index, ccr, tgt_eng, best_eng, temp;
791 
792  *scale = scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
793  buf += offset;
794 
795  /* Compute maximum backward cross-correlation */
796  ccr = 0;
797  index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
798  ccr = av_sat_add32(ccr, 1 << 15) >> 16;
799 
800  /* Compute target energy */
801  tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
802  *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
803 
804  if (ccr <= 0)
805  return 0;
806 
807  /* Compute best energy */
808  best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
809  best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
810 
811  temp = best_eng * *exc_eng >> 3;
812 
813  if (temp < ccr * ccr)
814  return index;
815  else
816  return 0;
817 }
818 
828 static void residual_interp(int16_t *buf, int16_t *out, int lag,
829  int gain, int *rseed)
830 {
831  int i;
832  if (lag) { /* Voiced */
833  int16_t *vector_ptr = buf + PITCH_MAX;
834  /* Attenuate */
835  for (i = 0; i < lag; i++)
836  out[i] = vector_ptr[i - lag] * 3 >> 2;
837  av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
838  (FRAME_LEN - lag) * sizeof(*out));
839  } else { /* Unvoiced */
840  for (i = 0; i < FRAME_LEN; i++) {
841  *rseed = *rseed * 521 + 259;
842  out[i] = gain * *rseed >> 15;
843  }
844  memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
845  }
846 }
847 
856 static inline void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
857  int16_t *src, int *dest)
858 {
859  int m, n;
860 
861  for (m = 0; m < SUBFRAME_LEN; m++) {
862  int64_t filter = 0;
863  for (n = 1; n <= LPC_ORDER; n++) {
864  filter -= fir_coef[n - 1] * src[m - n] -
865  iir_coef[n - 1] * (dest[m - n] >> 16);
866  }
867 
868  dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) + (1 << 15));
869  }
870 }
871 
879 static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
880 {
881  int num, denom, gain, bits1, bits2;
882  int i;
883 
884  num = energy;
885  denom = 0;
886  for (i = 0; i < SUBFRAME_LEN; i++) {
887  int temp = buf[i] >> 2;
888  temp *= temp;
889  denom = av_sat_dadd32(denom, temp);
890  }
891 
892  if (num && denom) {
893  bits1 = normalize_bits(num, 31);
894  bits2 = normalize_bits(denom, 31);
895  num = num << bits1 >> 1;
896  denom <<= bits2;
897 
898  bits2 = 5 + bits1 - bits2;
899  bits2 = FFMAX(0, bits2);
900 
901  gain = (num >> 1) / (denom >> 16);
902  gain = square_root(gain << 16 >> bits2);
903  } else {
904  gain = 1 << 12;
905  }
906 
907  for (i = 0; i < SUBFRAME_LEN; i++) {
908  p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
909  buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
910  (1 << 10)) >> 11);
911  }
912 }
913 
922 static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
923  int16_t *buf, int16_t *dst)
924 {
925  int16_t filter_coef[2][LPC_ORDER];
926  int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
927  int i, j, k;
928 
929  memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
930  memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
931 
932  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
933  for (k = 0; k < LPC_ORDER; k++) {
934  filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
935  (1 << 14)) >> 15;
936  filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
937  (1 << 14)) >> 15;
938  }
939  iir_filter(filter_coef[0], filter_coef[1], buf + i,
940  filter_signal + i);
941  lpc += LPC_ORDER;
942  }
943 
944  memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(*p->fir_mem));
945  memcpy(p->iir_mem, filter_signal + FRAME_LEN,
946  LPC_ORDER * sizeof(*p->iir_mem));
947 
948  buf += LPC_ORDER;
949  signal_ptr = filter_signal + LPC_ORDER;
950  for (i = 0; i < SUBFRAMES; i++) {
951  int temp;
952  int auto_corr[2];
953  int scale, energy;
954 
955  /* Normalize */
956  scale = scale_vector(dst, buf, SUBFRAME_LEN);
957 
958  /* Compute auto correlation coefficients */
959  auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
960  auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
961 
962  /* Compute reflection coefficient */
963  temp = auto_corr[1] >> 16;
964  if (temp) {
965  temp = (auto_corr[0] >> 2) / temp;
966  }
967  p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
968  temp = -p->reflection_coef >> 1 & ~3;
969 
970  /* Compensation filter */
971  for (j = 0; j < SUBFRAME_LEN; j++) {
972  dst[j] = av_sat_dadd32(signal_ptr[j],
973  (signal_ptr[j - 1] >> 16) * temp) >> 16;
974  }
975 
976  /* Compute normalized signal energy */
977  temp = 2 * scale + 4;
978  if (temp < 0) {
979  energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
980  } else
981  energy = auto_corr[1] >> temp;
982 
983  gain_scale(p, dst, energy);
984 
985  buf += SUBFRAME_LEN;
986  signal_ptr += SUBFRAME_LEN;
987  dst += SUBFRAME_LEN;
988  }
989 }
990 
991 static int sid_gain_to_lsp_index(int gain)
992 {
993  if (gain < 0x10)
994  return gain << 6;
995  else if (gain < 0x20)
996  return gain - 8 << 7;
997  else
998  return gain - 20 << 8;
999 }
1000 
1001 static inline int cng_rand(int *state, int base)
1002 {
1003  *state = (*state * 521 + 259) & 0xFFFF;
1004  return (*state & 0x7FFF) * base >> 15;
1005 }
1006 
1008 {
1009  int i, shift, seg, seg2, t, val, val_add, x, y;
1010 
1011  shift = 16 - p->cur_gain * 2;
1012  if (shift > 0)
1013  t = p->sid_gain << shift;
1014  else
1015  t = p->sid_gain >> -shift;
1016  x = t * cng_filt[0] >> 16;
1017 
1018  if (x >= cng_bseg[2])
1019  return 0x3F;
1020 
1021  if (x >= cng_bseg[1]) {
1022  shift = 4;
1023  seg = 3;
1024  } else {
1025  shift = 3;
1026  seg = (x >= cng_bseg[0]);
1027  }
1028  seg2 = FFMIN(seg, 3);
1029 
1030  val = 1 << shift;
1031  val_add = val >> 1;
1032  for (i = 0; i < shift; i++) {
1033  t = seg * 32 + (val << seg2);
1034  t *= t;
1035  if (x >= t)
1036  val += val_add;
1037  else
1038  val -= val_add;
1039  val_add >>= 1;
1040  }
1041 
1042  t = seg * 32 + (val << seg2);
1043  y = t * t - x;
1044  if (y <= 0) {
1045  t = seg * 32 + (val + 1 << seg2);
1046  t = t * t - x;
1047  val = (seg2 - 1 << 4) + val;
1048  if (t >= y)
1049  val++;
1050  } else {
1051  t = seg * 32 + (val - 1 << seg2);
1052  t = t * t - x;
1053  val = (seg2 - 1 << 4) + val;
1054  if (t >= y)
1055  val--;
1056  }
1057 
1058  return val;
1059 }
1060 
1062 {
1063  int i, j, idx, t;
1064  int off[SUBFRAMES];
1065  int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11];
1066  int tmp[SUBFRAME_LEN * 2];
1067  int16_t *vector_ptr;
1068  int64_t sum;
1069  int b0, c, delta, x, shift;
1070 
1071  p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123;
1072  p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123;
1073 
1074  for (i = 0; i < SUBFRAMES; i++) {
1075  p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1;
1077  }
1078 
1079  for (i = 0; i < SUBFRAMES / 2; i++) {
1080  t = cng_rand(&p->cng_random_seed, 1 << 13);
1081  off[i * 2] = t & 1;
1082  off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN;
1083  t >>= 2;
1084  for (j = 0; j < 11; j++) {
1085  signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
1086  t >>= 1;
1087  }
1088  }
1089 
1090  idx = 0;
1091  for (i = 0; i < SUBFRAMES; i++) {
1092  for (j = 0; j < SUBFRAME_LEN / 2; j++)
1093  tmp[j] = j;
1094  t = SUBFRAME_LEN / 2;
1095  for (j = 0; j < pulses[i]; j++, idx++) {
1096  int idx2 = cng_rand(&p->cng_random_seed, t);
1097 
1098  pos[idx] = tmp[idx2] * 2 + off[i];
1099  tmp[idx2] = tmp[--t];
1100  }
1101  }
1102 
1103  vector_ptr = p->audio + LPC_ORDER;
1104  memcpy(vector_ptr, p->prev_excitation,
1105  PITCH_MAX * sizeof(*p->excitation));
1106  for (i = 0; i < SUBFRAMES; i += 2) {
1107  gen_acb_excitation(vector_ptr, vector_ptr,
1108  p->pitch_lag[i >> 1], &p->subframe[i],
1109  p->cur_rate);
1110  gen_acb_excitation(vector_ptr + SUBFRAME_LEN,
1111  vector_ptr + SUBFRAME_LEN,
1112  p->pitch_lag[i >> 1], &p->subframe[i + 1],
1113  p->cur_rate);
1114 
1115  t = 0;
1116  for (j = 0; j < SUBFRAME_LEN * 2; j++)
1117  t |= FFABS(vector_ptr[j]);
1118  t = FFMIN(t, 0x7FFF);
1119  if (!t) {
1120  shift = 0;
1121  } else {
1122  shift = -10 + av_log2(t);
1123  if (shift < -2)
1124  shift = -2;
1125  }
1126  sum = 0;
1127  if (shift < 0) {
1128  for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1129  t = vector_ptr[j] << -shift;
1130  sum += t * t;
1131  tmp[j] = t;
1132  }
1133  } else {
1134  for (j = 0; j < SUBFRAME_LEN * 2; j++) {
1135  t = vector_ptr[j] >> shift;
1136  sum += t * t;
1137  tmp[j] = t;
1138  }
1139  }
1140 
1141  b0 = 0;
1142  for (j = 0; j < 11; j++)
1143  b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
1144  b0 = b0 * 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11
1145 
1146  c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5);
1147  if (shift * 2 + 3 >= 0)
1148  c >>= shift * 2 + 3;
1149  else
1150  c <<= -(shift * 2 + 3);
1151  c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15;
1152 
1153  delta = b0 * b0 * 2 - c;
1154  if (delta <= 0) {
1155  x = -b0;
1156  } else {
1157  delta = square_root(delta);
1158  x = delta - b0;
1159  t = delta + b0;
1160  if (FFABS(t) < FFABS(x))
1161  x = -t;
1162  }
1163  shift++;
1164  if (shift < 0)
1165  x >>= -shift;
1166  else
1167  x <<= shift;
1168  x = av_clip(x, -10000, 10000);
1169 
1170  for (j = 0; j < 11; j++) {
1171  idx = (i / 2) * 11 + j;
1172  vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
1173  (x * signs[idx] >> 15));
1174  }
1175 
1176  /* copy decoded data to serve as a history for the next decoded subframes */
1177  memcpy(vector_ptr + PITCH_MAX, vector_ptr,
1178  sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
1179  vector_ptr += SUBFRAME_LEN * 2;
1180  }
1181  /* Save the excitation for the next frame */
1182  memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN,
1183  PITCH_MAX * sizeof(*p->excitation));
1184 }
1185 
1186 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
1187  int *got_frame_ptr, AVPacket *avpkt)
1188 {
1189  G723_1_Context *p = avctx->priv_data;
1190  AVFrame *frame = data;
1191  const uint8_t *buf = avpkt->data;
1192  int buf_size = avpkt->size;
1193  int dec_mode = buf[0] & 3;
1194 
1195  PPFParam ppf[SUBFRAMES];
1196  int16_t cur_lsp[LPC_ORDER];
1197  int16_t lpc[SUBFRAMES * LPC_ORDER];
1198  int16_t acb_vector[SUBFRAME_LEN];
1199  int16_t *out;
1200  int bad_frame = 0, i, j, ret;
1201  int16_t *audio = p->audio;
1202 
1203  if (buf_size < frame_size[dec_mode]) {
1204  if (buf_size)
1205  av_log(avctx, AV_LOG_WARNING,
1206  "Expected %d bytes, got %d - skipping packet\n",
1207  frame_size[dec_mode], buf_size);
1208  *got_frame_ptr = 0;
1209  return buf_size;
1210  }
1211 
1212  if (unpack_bitstream(p, buf, buf_size) < 0) {
1213  bad_frame = 1;
1214  if (p->past_frame_type == ACTIVE_FRAME)
1216  else
1218  }
1219 
1220  frame->nb_samples = FRAME_LEN;
1221  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1222  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1223  return ret;
1224  }
1225 
1226  out = (int16_t *)frame->data[0];
1227 
1228  if (p->cur_frame_type == ACTIVE_FRAME) {
1229  if (!bad_frame)
1230  p->erased_frames = 0;
1231  else if (p->erased_frames != 3)
1232  p->erased_frames++;
1233 
1234  inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
1235  lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
1236 
1237  /* Save the lsp_vector for the next frame */
1238  memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
1239 
1240  /* Generate the excitation for the frame */
1241  memcpy(p->excitation, p->prev_excitation,
1242  PITCH_MAX * sizeof(*p->excitation));
1243  if (!p->erased_frames) {
1244  int16_t *vector_ptr = p->excitation + PITCH_MAX;
1245 
1246  /* Update interpolation gain memory */
1248  p->subframe[3].amp_index) >> 1];
1249  for (i = 0; i < SUBFRAMES; i++) {
1250  gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
1251  p->pitch_lag[i >> 1], i);
1252  gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
1253  p->pitch_lag[i >> 1], &p->subframe[i],
1254  p->cur_rate);
1255  /* Get the total excitation */
1256  for (j = 0; j < SUBFRAME_LEN; j++) {
1257  int v = av_clip_int16(vector_ptr[j] << 1);
1258  vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
1259  }
1260  vector_ptr += SUBFRAME_LEN;
1261  }
1262 
1263  vector_ptr = p->excitation + PITCH_MAX;
1264 
1266  &p->sid_gain, &p->cur_gain);
1267 
1268  /* Peform pitch postfiltering */
1269  if (p->postfilter) {
1270  i = PITCH_MAX;
1271  for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1272  comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
1273  ppf + j, p->cur_rate);
1274 
1275  for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1277  vector_ptr + i,
1278  vector_ptr + i + ppf[j].index,
1279  ppf[j].sc_gain,
1280  ppf[j].opt_gain,
1281  1 << 14, 15, SUBFRAME_LEN);
1282  } else {
1283  audio = vector_ptr - LPC_ORDER;
1284  }
1285 
1286  /* Save the excitation for the next frame */
1287  memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
1288  PITCH_MAX * sizeof(*p->excitation));
1289  } else {
1290  p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
1291  if (p->erased_frames == 3) {
1292  /* Mute output */
1293  memset(p->excitation, 0,
1294  (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
1295  memset(p->prev_excitation, 0,
1296  PITCH_MAX * sizeof(*p->excitation));
1297  memset(frame->data[0], 0,
1298  (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
1299  } else {
1300  int16_t *buf = p->audio + LPC_ORDER;
1301 
1302  /* Regenerate frame */
1304  p->interp_gain, &p->random_seed);
1305 
1306  /* Save the excitation for the next frame */
1307  memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
1308  PITCH_MAX * sizeof(*p->excitation));
1309  }
1310  }
1312  } else {
1313  if (p->cur_frame_type == SID_FRAME) {
1315  inverse_quant(p->sid_lsp, p->prev_lsp, p->lsp_index, 0);
1316  } else if (p->past_frame_type == ACTIVE_FRAME) {
1317  p->sid_gain = estimate_sid_gain(p);
1318  }
1319 
1320  if (p->past_frame_type == ACTIVE_FRAME)
1321  p->cur_gain = p->sid_gain;
1322  else
1323  p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3;
1324  generate_noise(p);
1325  lsp_interpolate(lpc, p->sid_lsp, p->prev_lsp);
1326  /* Save the lsp_vector for the next frame */
1327  memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
1328  }
1329 
1331 
1332  memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
1333  for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1334  ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
1335  audio + i, SUBFRAME_LEN, LPC_ORDER,
1336  0, 1, 1 << 12);
1337  memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
1338 
1339  if (p->postfilter) {
1340  formant_postfilter(p, lpc, p->audio, out);
1341  } else { // if output is not postfiltered it should be scaled by 2
1342  for (i = 0; i < FRAME_LEN; i++)
1343  out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
1344  }
1345 
1346  *got_frame_ptr = 1;
1347 
1348  return frame_size[dec_mode];
1349 }
1350 
1351 #define OFFSET(x) offsetof(G723_1_Context, x)
1352 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1353 
1354 static const AVOption options[] = {
1355  { "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
1356  { .i64 = 1 }, 0, 1, AD },
1357  { NULL }
1358 };
1359 
1360 
1361 static const AVClass g723_1dec_class = {
1362  .class_name = "G.723.1 decoder",
1363  .item_name = av_default_item_name,
1364  .option = options,
1365  .version = LIBAVUTIL_VERSION_INT,
1366 };
1367 
1369  .name = "g723_1",
1370  .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1371  .type = AVMEDIA_TYPE_AUDIO,
1372  .id = AV_CODEC_ID_G723_1,
1373  .priv_data_size = sizeof(G723_1_Context),
1376  .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1377  .priv_class = &g723_1dec_class,
1378 };
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
Definition: g723_1.c:106
static void lsp2lpc(int16_t *lpc)
Convert LSP frequencies to LPC coefficients.
Definition: g723_1.c:381
int cur_gain
Definition: g723_1.c:101
int erased_frames
Definition: g723_1.c:86
int dirac_train
Definition: g723_1.c:61
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static const int16_t lsp_band0[LSP_CB_SIZE][3]
Definition: g723_1_data.h:127
int reflection_coef
Definition: g723_1.c:102
int ad_cb_gain
Definition: g723_1.c:60
static const int cng_bseg[3]
Definition: g723_1_data.h:1198
AVOption.
Definition: opt.h:234
FrameType
G723.1 frame types.
Definition: g723_1.c:44
int pitch_lag[2]
Definition: g723_1.c:85
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static const int16_t lsp_band2[LSP_CB_SIZE][4]
Definition: g723_1_data.h:305
memory handling functions
G723.1 unpacked data subframe.
Definition: g723_1.c:58
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:59
static int 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:279
int16_t fir_mem[LPC_ORDER]
Definition: g723_1.c:93
static const int cng_filt[4]
Definition: g723_1_data.h:1196
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
Definition: g723_1.c:91
static int normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
Definition: g723_1.c:271
int size
Definition: avcodec.h:974
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_1.c:619
static void iir_filter(int16_t *fir_coef, int16_t *iir_coef, int16_t *src, int *dest)
Perform IIR filtering.
Definition: g723_1.c:856
#define PITCH_MAX
Definition: g723_1_data.h:40
static int comp_interp_index(G723_1_Context *p, int pitch_lag, int *exc_eng, int *scale)
Classify frames as voiced/unvoiced.
Definition: g723_1.c:784
#define SUBFRAMES
Definition: g723_1_data.h:33
static const AVOption options[]
Definition: g723_1.c:1354
static void gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
Definition: g723_1.c:474
static const AVClass g723_1dec_class
Definition: g723_1.c:1361
static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
Calculate pitch postfilter parameters.
Definition: g723_1.c:700
static const uint8_t frame_size[4]
Definition: g723_1.c:32
AVCodec.
Definition: avcodec.h:2812
#define LSP_BANDS
Definition: g723_1_data.h:37
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition: amrnbdec.c:886
#define GRID_SIZE
Definition: g723_1_data.h:42
enum FrameType past_frame_type
Definition: g723_1.c:82
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:38
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: g723_1.c:1186
#define SUBFRAME_LEN
Definition: g723_1_data.h:34
uint8_t bits
Definition: crc.c:251
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
uint8_t
#define av_cold
Definition: attributes.h:66
float delta
AVOptions.
static const int16_t lsp_band1[LSP_CB_SIZE][3]
Definition: g723_1_data.h:216
static void residual_interp(int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
Peform residual interpolation based on frame classification.
Definition: g723_1.c:828
#define b
Definition: input.c:52
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
int pulse_sign
Definition: g723_1.c:62
const char data[16]
Definition: mxf.c:70
static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf, int16_t *dst)
Perform formant filtering.
Definition: g723_1.c:922
uint8_t * data
Definition: avcodec.h:973
static const uint8_t bits2[81]
Definition: aactab.c:122
bitstream reader API header.
#define CNG_RANDOM_SEED
Definition: g723_1.c:39
static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
Get delayed contribution from the previous excitation vector.
Definition: g723_1.c:555
int16_t sid_lsp[LPC_ORDER]
Definition: g723_1.c:89
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int amp_index
Definition: g723_1.c:64
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
static const int16_t postfilter_tbl[2][LPC_ORDER]
Definition: g723_1_data.h:1187
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int grid_index
Definition: g723_1.c:63
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
Quantize LSP frequencies by interpolation and convert them to the corresponding LPC coefficients...
Definition: g723_1.c:451
G.723.1 compatible decoder data tables.
int16_t prev_excitation[PITCH_MAX]
Definition: g723_1.c:90
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
Definition: g723_1.c:109
#define FFMAX(a, b)
Definition: common.h:55
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_1.c:652
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
int interp_index
Definition: g723_1.c:98
static int estimate_sid_gain(G723_1_Context *p)
Definition: g723_1.c:1007
G723_1_Subframe subframe[4]
Definition: g723_1.c:80
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
static const int16_t fixed_cb_gain[GAIN_LEVELS]
Definition: g723_1_data.h:536
enum Rate cur_rate
Definition: g723_1.c:83
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.
audio channel layout utility functions
int16_t synth_mem[LPC_ORDER]
Definition: g723_1.c:92
#define FFMIN(a, b)
Definition: common.h:57
static const int16_t ppf_gain_weight[2]
Definition: g723_1_data.h:50
#define PITCH_MIN
Definition: g723_1_data.h:39
#define FFABS(a)
Definition: common.h:52
int index
postfilter backward/forward lag
Definition: g723_1.c:72
#define OFFSET(x)
Definition: g723_1.c:1351
int sid_gain
Definition: g723_1.c:100
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const int16_t adaptive_cb_gain85[85 *20]
Definition: g723_1_data.h:542
static int16_t square_root(int val)
Bitexact implementation of sqrt(val/2).
Definition: g723_1.c:250
if(ac->has_optimized_func)
static const float pred[4]
Definition: siprdata.h:259
static const int16_t adaptive_cb_gain170[170 *20]
Definition: g723_1_data.h:758
int16_t opt_gain
optimal gain
Definition: g723_1.c:73
int postfilter
Definition: g723_1.c:104
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
Libavcodec external API header.
static const int16_t cos_tab[COS_TBL_SIZE]
Definition: g723_1_data.h:59
#define PITCH_ORDER
Definition: g723_1_data.h:41
int sample_rate
samples per second
Definition: avcodec.h:1807
av_default_item_name
Definition: dnxhdenc.c:52
#define AD
Definition: g723_1.c:1352
main external API structure.
Definition: avcodec.h:1050
#define FASTDIV(a, b)
Definition: mathops.h:199
#define PULSE_MAX
Definition: g723_1_data.h:43
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation, int pitch_lag, G723_1_Subframe *subfrm, enum Rate cur_rate)
Generate adaptive codebook excitation.
Definition: g723_1.c:582
Active speech.
Definition: g723_1.c:45
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
Definition: g723_1.c:373
Describe the class of an AVClass context structure.
Definition: log.h:33
int16_t sc_gain
scaling gain
Definition: g723_1.c:74
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
int index
Definition: gxfenc.c:72
int cng_random_seed
Definition: g723_1.c:97
int random_seed
Definition: g723_1.c:96
static const int16_t pitch_contrib[340]
Definition: g723_1_data.h:484
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
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_1.c:495
static int dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:568
static int sid_gain_to_lsp_index(int gain)
Definition: g723_1.c:991
enum FrameType cur_frame_type
Definition: g723_1.c:81
static const int cng_adaptive_cb_lag[4]
Definition: g723_1_data.h:1194
static uint32_t state
Definition: trasher.c:27
int16_t prev_lsp[LPC_ORDER]
Definition: g723_1.c:88
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
#define LPC_ORDER
Definition: g723_1_data.h:36
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:736
AVCodec ff_g723_1_decoder
Definition: g723_1.c:1368
common internal api header.
Pitch postfilter parameters.
Definition: g723_1.c:71
static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, int buf_size)
Unpack the frame into parameters.
Definition: g723_1.c:135
signed 16 bits
Definition: samplefmt.h:64
#define FRAME_LEN
Definition: g723_1_data.h:35
static const int32_t max_pos[4]
Definition: g723_1_data.h:534
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
static const int16_t dc_lsp[LPC_ORDER]
Definition: g723_1_data.h:53
static void gain_scale(G723_1_Context *p, int16_t *buf, int energy)
Adjust gain of postfiltered signal.
Definition: g723_1.c:879
static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
Perform inverse quantization of LSP frequencies.
Definition: g723_1.c:305
void * priv_data
Definition: avcodec.h:1092
static int cng_rand(int *state, int base)
Definition: g723_1.c:1001
int channels
number of audio channels
Definition: avcodec.h:1808
#define av_log2
Definition: intmath.h:85
uint8_t lsp_index[LSP_BANDS]
Definition: g723_1.c:84
int pulse_pos
Definition: g723_1.c:65
#define GAIN_LEVELS
Definition: g723_1_data.h:44
int iir_mem[LPC_ORDER]
Definition: g723_1.c:94
static void generate_noise(G723_1_Context *p)
Definition: g723_1.c:1061
static const int8_t pulses[4]
Definition: g723_1_data.h:531
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:319
int interp_gain
Definition: g723_1.c:99
Silence Insertion Descriptor frame.
Definition: g723_1.c:46
Rate
Definition: g723_1.c:50
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
static const uint8_t bits1[81]
Definition: aactab.c:99
int ad_cb_lag
adaptive codebook lag
Definition: g723_1.c:59
static const int32_t combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Definition: g723_1_data.h:440