amrnbdec.c
Go to the documentation of this file.
1 /*
2  * AMR narrowband decoder
3  * Copyright (c) 2006-2007 Robert Swain
4  * Copyright (c) 2009 Colin McQuillan
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 
23 
43 #include <string.h>
44 #include <math.h>
45 
46 #include "avcodec.h"
47 #include "internal.h"
48 #include "get_bits.h"
49 #include "libavutil/common.h"
50 #include "celp_math.h"
51 #include "celp_filters.h"
52 #include "acelp_filters.h"
53 #include "acelp_vectors.h"
54 #include "acelp_pitch_delay.h"
55 #include "lsp.h"
56 #include "amr.h"
57 
58 #include "amrnbdata.h"
59 
60 #define AMR_BLOCK_SIZE 160
61 #define AMR_SAMPLE_BOUND 32768.0
62 
63 
72 #define AMR_SAMPLE_SCALE (2.0 / 32768.0)
73 
75 #define PRED_FAC_MODE_12k2 0.65
76 
77 #define LSF_R_FAC (8000.0 / 32768.0)
78 #define MIN_LSF_SPACING (50.0488 / 8000.0)
79 #define PITCH_LAG_MIN_MODE_12k2 18
80 
81 
82 #define MIN_ENERGY -14.0
83 
89 #define SHARP_MAX 0.79449462890625
90 
92 #define AMR_TILT_RESPONSE 22
93 
94 #define AMR_TILT_GAMMA_T 0.8
95 
96 #define AMR_AGC_ALPHA 0.9
97 
98 typedef struct AMRContext {
103 
105  double lsp[4][LP_FILTER_ORDER];
107 
108  float lsf_q[4][LP_FILTER_ORDER];
110 
111  float lpc[4][LP_FILTER_ORDER];
112 
113  uint8_t pitch_lag_int;
114 
116  float *excitation;
117 
120 
121  float prediction_error[4];
122  float pitch_gain[5];
123  float fixed_gain[5];
124 
125  float beta;
126  uint8_t diff_count;
127  uint8_t hang_count;
128 
131  uint8_t ir_filter_onset;
132 
133  float postfilter_mem[10];
134  float tilt_mem;
136  float high_pass_mem[2];
137 
139 
140 } AMRContext;
141 
143 static void weighted_vector_sumd(double *out, const double *in_a,
144  const double *in_b, double weight_coeff_a,
145  double weight_coeff_b, int length)
146 {
147  int i;
148 
149  for (i = 0; i < length; i++)
150  out[i] = weight_coeff_a * in_a[i]
151  + weight_coeff_b * in_b[i];
152 }
153 
155 {
156  AMRContext *p = avctx->priv_data;
157  int i;
158 
159  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
160 
161  // p->excitation always points to the same position in p->excitation_buf
163 
164  for (i = 0; i < LP_FILTER_ORDER; i++) {
165  p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15);
166  p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15);
167  }
168 
169  for (i = 0; i < 4; i++)
171 
173  avctx->coded_frame = &p->avframe;
174 
175  return 0;
176 }
177 
178 
190 static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf,
191  int buf_size)
192 {
193  GetBitContext gb;
194  enum Mode mode;
195 
196  init_get_bits(&gb, buf, buf_size * 8);
197 
198  // Decode the first octet.
199  skip_bits(&gb, 1); // padding bit
200  mode = get_bits(&gb, 4); // frame type
201  p->bad_frame_indicator = !get_bits1(&gb); // quality bit
202  skip_bits(&gb, 2); // two padding bits
203 
204  if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) {
205  return NO_DATA;
206  }
207 
208  if (mode < MODE_DTX)
209  ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1,
211 
212  return mode;
213 }
214 
215 
218 
226 static void interpolate_lsf(float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
227 {
228  int i;
229 
230  for (i = 0; i < 4; i++)
231  ff_weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new,
232  0.25 * (3 - i), 0.25 * (i + 1),
233  LP_FILTER_ORDER);
234 }
235 
247 static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER],
248  const float lsf_no_r[LP_FILTER_ORDER],
249  const int16_t *lsf_quantizer[5],
250  const int quantizer_offset,
251  const int sign, const int update)
252 {
253  int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector
254  float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector
255  int i;
256 
257  for (i = 0; i < LP_FILTER_ORDER >> 1; i++)
258  memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset],
259  2 * sizeof(*lsf_r));
260 
261  if (sign) {
262  lsf_r[4] *= -1;
263  lsf_r[5] *= -1;
264  }
265 
266  if (update)
267  memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
268 
269  for (i = 0; i < LP_FILTER_ORDER; i++)
270  lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0);
271 
272  ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
273 
274  if (update)
275  interpolate_lsf(p->lsf_q, lsf_q);
276 
277  ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER);
278 }
279 
285 static void lsf2lsp_5(AMRContext *p)
286 {
287  const uint16_t *lsf_param = p->frame.lsf;
288  float lsf_no_r[LP_FILTER_ORDER]; // LSFs without the residual vector
289  const int16_t *lsf_quantizer[5];
290  int i;
291 
292  lsf_quantizer[0] = lsf_5_1[lsf_param[0]];
293  lsf_quantizer[1] = lsf_5_2[lsf_param[1]];
294  lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1];
295  lsf_quantizer[3] = lsf_5_4[lsf_param[3]];
296  lsf_quantizer[4] = lsf_5_5[lsf_param[4]];
297 
298  for (i = 0; i < LP_FILTER_ORDER; i++)
299  lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i];
300 
301  lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0);
302  lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1);
303 
304  // interpolate LSP vectors at subframes 1 and 3
305  weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER);
306  weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);
307 }
308 
314 static void lsf2lsp_3(AMRContext *p)
315 {
316  const uint16_t *lsf_param = p->frame.lsf;
317  int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector
318  float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector
319  const int16_t *lsf_quantizer;
320  int i, j;
321 
322  lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]];
323  memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r));
324 
325  lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)];
326  memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r));
327 
328  lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]];
329  memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r));
330 
331  // calculate mean-removed LSF vector and add mean
332  for (i = 0; i < LP_FILTER_ORDER; i++)
333  lsf_q[i] = (lsf_r[i] + p->prev_lsf_r[i] * pred_fac[i]) * (LSF_R_FAC / 8000.0) + lsf_3_mean[i] * (1.0 / 8000.0);
334 
335  ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
336 
337  // store data for computing the next frame's LSFs
338  interpolate_lsf(p->lsf_q, lsf_q);
339  memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
340 
341  ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
342 
343  // interpolate LSP vectors at subframes 1, 2 and 3
344  for (i = 1; i <= 3; i++)
345  for(j = 0; j < LP_FILTER_ORDER; j++)
346  p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
347  (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
348 }
349 
351 
352 
355 
359 static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index,
360  const int prev_lag_int, const int subframe)
361 {
362  if (subframe == 0 || subframe == 2) {
363  if (pitch_index < 463) {
364  *lag_int = (pitch_index + 107) * 10923 >> 16;
365  *lag_frac = pitch_index - *lag_int * 6 + 105;
366  } else {
367  *lag_int = pitch_index - 368;
368  *lag_frac = 0;
369  }
370  } else {
371  *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1;
372  *lag_frac = pitch_index - *lag_int * 6 - 3;
373  *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2,
374  PITCH_DELAY_MAX - 9);
375  }
376 }
377 
379  const AMRNBSubframe *amr_subframe,
380  const int subframe)
381 {
382  int pitch_lag_int, pitch_lag_frac;
383  enum Mode mode = p->cur_frame_mode;
384 
385  if (p->cur_frame_mode == MODE_12k2) {
386  decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac,
387  amr_subframe->p_lag, p->pitch_lag_int,
388  subframe);
389  } else
390  ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac,
391  amr_subframe->p_lag,
392  p->pitch_lag_int, subframe,
393  mode != MODE_4k75 && mode != MODE_5k15,
394  mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6));
395 
396  p->pitch_lag_int = pitch_lag_int; // store previous lag in a uint8_t
397 
398  pitch_lag_frac <<= (p->cur_frame_mode != MODE_12k2);
399 
400  pitch_lag_int += pitch_lag_frac > 0;
401 
402  /* Calculate the pitch vector by interpolating the past excitation at the
403  pitch lag using a b60 hamming windowed sinc function. */
404  ff_acelp_interpolatef(p->excitation, p->excitation + 1 - pitch_lag_int,
405  ff_b60_sinc, 6,
406  pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0),
407  10, AMR_SUBFRAME_SIZE);
408 
409  memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));
410 }
411 
413 
414 
417 
421 static void decode_10bit_pulse(int code, int pulse_position[8],
422  int i1, int i2, int i3)
423 {
424  // coded using 7+3 bits with the 3 LSBs being, individually, the LSB of 1 of
425  // the 3 pulses and the upper 7 bits being coded in base 5
426  const uint8_t *positions = base_five_table[code >> 3];
427  pulse_position[i1] = (positions[2] << 1) + ( code & 1);
428  pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1);
429  pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);
430 }
431 
439 static void decode_8_pulses_31bits(const int16_t *fixed_index,
440  AMRFixed *fixed_sparse)
441 {
442  int pulse_position[8];
443  int i, temp;
444 
445  decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1);
446  decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5);
447 
448  // coded using 5+2 bits with the 2 LSBs being, individually, the LSB of 1 of
449  // the 2 pulses and the upper 5 bits being coded in base 5
450  temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5;
451  pulse_position[3] = temp % 5;
452  pulse_position[7] = temp / 5;
453  if (pulse_position[7] & 1)
454  pulse_position[3] = 4 - pulse_position[3];
455  pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1);
456  pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1);
457 
458  fixed_sparse->n = 8;
459  for (i = 0; i < 4; i++) {
460  const int pos1 = (pulse_position[i] << 2) + i;
461  const int pos2 = (pulse_position[i + 4] << 2) + i;
462  const float sign = fixed_index[i] ? -1.0 : 1.0;
463  fixed_sparse->x[i ] = pos1;
464  fixed_sparse->x[i + 4] = pos2;
465  fixed_sparse->y[i ] = sign;
466  fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign;
467  }
468 }
469 
485 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses,
486  const enum Mode mode, const int subframe)
487 {
488  assert(MODE_4k75 <= mode && mode <= MODE_12k2);
489 
490  if (mode == MODE_12k2) {
491  ff_decode_10_pulses_35bits(pulses, fixed_sparse, gray_decode, 5, 3);
492  } else if (mode == MODE_10k2) {
493  decode_8_pulses_31bits(pulses, fixed_sparse);
494  } else {
495  int *pulse_position = fixed_sparse->x;
496  int i, pulse_subset;
497  const int fixed_index = pulses[0];
498 
499  if (mode <= MODE_5k15) {
500  pulse_subset = ((fixed_index >> 3) & 8) + (subframe << 1);
501  pulse_position[0] = ( fixed_index & 7) * 5 + track_position[pulse_subset];
502  pulse_position[1] = ((fixed_index >> 3) & 7) * 5 + track_position[pulse_subset + 1];
503  fixed_sparse->n = 2;
504  } else if (mode == MODE_5k9) {
505  pulse_subset = ((fixed_index & 1) << 1) + 1;
506  pulse_position[0] = ((fixed_index >> 1) & 7) * 5 + pulse_subset;
507  pulse_subset = (fixed_index >> 4) & 3;
508  pulse_position[1] = ((fixed_index >> 6) & 7) * 5 + pulse_subset + (pulse_subset == 3 ? 1 : 0);
509  fixed_sparse->n = pulse_position[0] == pulse_position[1] ? 1 : 2;
510  } else if (mode == MODE_6k7) {
511  pulse_position[0] = (fixed_index & 7) * 5;
512  pulse_subset = (fixed_index >> 2) & 2;
513  pulse_position[1] = ((fixed_index >> 4) & 7) * 5 + pulse_subset + 1;
514  pulse_subset = (fixed_index >> 6) & 2;
515  pulse_position[2] = ((fixed_index >> 8) & 7) * 5 + pulse_subset + 2;
516  fixed_sparse->n = 3;
517  } else { // mode <= MODE_7k95
518  pulse_position[0] = gray_decode[ fixed_index & 7];
519  pulse_position[1] = gray_decode[(fixed_index >> 3) & 7] + 1;
520  pulse_position[2] = gray_decode[(fixed_index >> 6) & 7] + 2;
521  pulse_subset = (fixed_index >> 9) & 1;
522  pulse_position[3] = gray_decode[(fixed_index >> 10) & 7] + pulse_subset + 3;
523  fixed_sparse->n = 4;
524  }
525  for (i = 0; i < fixed_sparse->n; i++)
526  fixed_sparse->y[i] = (pulses[1] >> i) & 1 ? 1.0 : -1.0;
527  }
528 }
529 
538 static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode,
539  AMRFixed *fixed_sparse)
540 {
541  // The spec suggests the current pitch gain is always used, but in other
542  // modes the pitch and codebook gains are joinly quantized (sec 5.8.2)
543  // so the codebook gain cannot depend on the quantized pitch gain.
544  if (mode == MODE_12k2)
545  p->beta = FFMIN(p->pitch_gain[4], 1.0);
546 
547  fixed_sparse->pitch_lag = p->pitch_lag_int;
548  fixed_sparse->pitch_fac = p->beta;
549 
550  // Save pitch sharpening factor for the next subframe
551  // MODE_4k75 only updates on the 2nd and 4th subframes - this follows from
552  // the fact that the gains for two subframes are jointly quantized.
553  if (mode != MODE_4k75 || subframe & 1)
554  p->beta = av_clipf(p->pitch_gain[4], 0.0, SHARP_MAX);
555 }
557 
558 
561 
574 static float fixed_gain_smooth(AMRContext *p , const float *lsf,
575  const float *lsf_avg, const enum Mode mode)
576 {
577  float diff = 0.0;
578  int i;
579 
580  for (i = 0; i < LP_FILTER_ORDER; i++)
581  diff += fabs(lsf_avg[i] - lsf[i]) / lsf_avg[i];
582 
583  // If diff is large for ten subframes, disable smoothing for a 40-subframe
584  // hangover period.
585  p->diff_count++;
586  if (diff <= 0.65)
587  p->diff_count = 0;
588 
589  if (p->diff_count > 10) {
590  p->hang_count = 0;
591  p->diff_count--; // don't let diff_count overflow
592  }
593 
594  if (p->hang_count < 40) {
595  p->hang_count++;
596  } else if (mode < MODE_7k4 || mode == MODE_10k2) {
597  const float smoothing_factor = av_clipf(4.0 * diff - 1.6, 0.0, 1.0);
598  const float fixed_gain_mean = (p->fixed_gain[0] + p->fixed_gain[1] +
599  p->fixed_gain[2] + p->fixed_gain[3] +
600  p->fixed_gain[4]) * 0.2;
601  return smoothing_factor * p->fixed_gain[4] +
602  (1.0 - smoothing_factor) * fixed_gain_mean;
603  }
604  return p->fixed_gain[4];
605 }
606 
616 static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe,
617  const enum Mode mode, const int subframe,
618  float *fixed_gain_factor)
619 {
620  if (mode == MODE_12k2 || mode == MODE_7k95) {
621  p->pitch_gain[4] = qua_gain_pit [amr_subframe->p_gain ]
622  * (1.0 / 16384.0);
623  *fixed_gain_factor = qua_gain_code[amr_subframe->fixed_gain]
624  * (1.0 / 2048.0);
625  } else {
626  const uint16_t *gains;
627 
628  if (mode >= MODE_6k7) {
629  gains = gains_high[amr_subframe->p_gain];
630  } else if (mode >= MODE_5k15) {
631  gains = gains_low [amr_subframe->p_gain];
632  } else {
633  // gain index is only coded in subframes 0,2 for MODE_4k75
634  gains = gains_MODE_4k75[(p->frame.subframe[subframe & 2].p_gain << 1) + (subframe & 1)];
635  }
636 
637  p->pitch_gain[4] = gains[0] * (1.0 / 16384.0);
638  *fixed_gain_factor = gains[1] * (1.0 / 4096.0);
639  }
640 }
641 
643 
644 
647 
658 static void apply_ir_filter(float *out, const AMRFixed *in,
659  const float *filter)
660 {
661  float filter1[AMR_SUBFRAME_SIZE],
662  filter2[AMR_SUBFRAME_SIZE];
663  int lag = in->pitch_lag;
664  float fac = in->pitch_fac;
665  int i;
666 
667  if (lag < AMR_SUBFRAME_SIZE) {
668  ff_celp_circ_addf(filter1, filter, filter, lag, fac,
670 
671  if (lag < AMR_SUBFRAME_SIZE >> 1)
672  ff_celp_circ_addf(filter2, filter, filter1, lag, fac,
674  }
675 
676  memset(out, 0, sizeof(float) * AMR_SUBFRAME_SIZE);
677  for (i = 0; i < in->n; i++) {
678  int x = in->x[i];
679  float y = in->y[i];
680  const float *filterp;
681 
682  if (x >= AMR_SUBFRAME_SIZE - lag) {
683  filterp = filter;
684  } else if (x >= AMR_SUBFRAME_SIZE - (lag << 1)) {
685  filterp = filter1;
686  } else
687  filterp = filter2;
688 
689  ff_celp_circ_addf(out, out, filterp, x, y, AMR_SUBFRAME_SIZE);
690  }
691 }
692 
705 static const float *anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse,
706  const float *fixed_vector,
707  float fixed_gain, float *out)
708 {
709  int ir_filter_nr;
710 
711  if (p->pitch_gain[4] < 0.6) {
712  ir_filter_nr = 0; // strong filtering
713  } else if (p->pitch_gain[4] < 0.9) {
714  ir_filter_nr = 1; // medium filtering
715  } else
716  ir_filter_nr = 2; // no filtering
717 
718  // detect 'onset'
719  if (fixed_gain > 2.0 * p->prev_sparse_fixed_gain) {
720  p->ir_filter_onset = 2;
721  } else if (p->ir_filter_onset)
722  p->ir_filter_onset--;
723 
724  if (!p->ir_filter_onset) {
725  int i, count = 0;
726 
727  for (i = 0; i < 5; i++)
728  if (p->pitch_gain[i] < 0.6)
729  count++;
730  if (count > 2)
731  ir_filter_nr = 0;
732 
733  if (ir_filter_nr > p->prev_ir_filter_nr + 1)
734  ir_filter_nr--;
735  } else if (ir_filter_nr < 2)
736  ir_filter_nr++;
737 
738  // Disable filtering for very low level of fixed_gain.
739  // Note this step is not specified in the technical description but is in
740  // the reference source in the function Ph_disp.
741  if (fixed_gain < 5.0)
742  ir_filter_nr = 2;
743 
745  && ir_filter_nr < 2) {
746  apply_ir_filter(out, fixed_sparse,
747  (p->cur_frame_mode == MODE_7k95 ?
749  ir_filters_lookup)[ir_filter_nr]);
750  fixed_vector = out;
751  }
752 
753  // update ir filter strength history
754  p->prev_ir_filter_nr = ir_filter_nr;
755  p->prev_sparse_fixed_gain = fixed_gain;
756 
757  return fixed_vector;
758 }
759 
761 
762 
765 
776 static int synthesis(AMRContext *p, float *lpc,
777  float fixed_gain, const float *fixed_vector,
778  float *samples, uint8_t overflow)
779 {
780  int i;
781  float excitation[AMR_SUBFRAME_SIZE];
782 
783  // if an overflow has been detected, the pitch vector is scaled down by a
784  // factor of 4
785  if (overflow)
786  for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
787  p->pitch_vector[i] *= 0.25;
788 
789  ff_weighted_vector_sumf(excitation, p->pitch_vector, fixed_vector,
790  p->pitch_gain[4], fixed_gain, AMR_SUBFRAME_SIZE);
791 
792  // emphasize pitch vector contribution
793  if (p->pitch_gain[4] > 0.5 && !overflow) {
794  float energy = ff_dot_productf(excitation, excitation,
795  AMR_SUBFRAME_SIZE);
796  float pitch_factor =
797  p->pitch_gain[4] *
798  (p->cur_frame_mode == MODE_12k2 ?
799  0.25 * FFMIN(p->pitch_gain[4], 1.0) :
800  0.5 * FFMIN(p->pitch_gain[4], SHARP_MAX));
801 
802  for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
803  excitation[i] += pitch_factor * p->pitch_vector[i];
804 
805  ff_scale_vector_to_given_sum_of_squares(excitation, excitation, energy,
806  AMR_SUBFRAME_SIZE);
807  }
808 
809  ff_celp_lp_synthesis_filterf(samples, lpc, excitation, AMR_SUBFRAME_SIZE,
811 
812  // detect overflow
813  for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
814  if (fabsf(samples[i]) > AMR_SAMPLE_BOUND) {
815  return 1;
816  }
817 
818  return 0;
819 }
820 
822 
823 
826 
832 static void update_state(AMRContext *p)
833 {
834  memcpy(p->prev_lsp_sub4, p->lsp[3], LP_FILTER_ORDER * sizeof(p->lsp[3][0]));
835 
836  memmove(&p->excitation_buf[0], &p->excitation_buf[AMR_SUBFRAME_SIZE],
837  (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) * sizeof(float));
838 
839  memmove(&p->pitch_gain[0], &p->pitch_gain[1], 4 * sizeof(float));
840  memmove(&p->fixed_gain[0], &p->fixed_gain[1], 4 * sizeof(float));
841 
842  memmove(&p->samples_in[0], &p->samples_in[AMR_SUBFRAME_SIZE],
843  LP_FILTER_ORDER * sizeof(float));
844 }
845 
847 
848 
851 
858 static float tilt_factor(float *lpc_n, float *lpc_d)
859 {
860  float rh0, rh1; // autocorrelation at lag 0 and 1
861 
862  // LP_FILTER_ORDER prior zeros are needed for ff_celp_lp_synthesis_filterf
863  float impulse_buffer[LP_FILTER_ORDER + AMR_TILT_RESPONSE] = { 0 };
864  float *hf = impulse_buffer + LP_FILTER_ORDER; // start of impulse response
865 
866  hf[0] = 1.0;
867  memcpy(hf + 1, lpc_n, sizeof(float) * LP_FILTER_ORDER);
869  LP_FILTER_ORDER);
870 
871  rh0 = ff_dot_productf(hf, hf, AMR_TILT_RESPONSE);
872  rh1 = ff_dot_productf(hf, hf + 1, AMR_TILT_RESPONSE - 1);
873 
874  // The spec only specifies this check for 12.2 and 10.2 kbit/s
875  // modes. But in the ref source the tilt is always non-negative.
876  return rh1 >= 0.0 ? rh1 / rh0 * AMR_TILT_GAMMA_T : 0.0;
877 }
878 
887 static void postfilter(AMRContext *p, float *lpc, float *buf_out)
888 {
889  int i;
890  float *samples = p->samples_in + LP_FILTER_ORDER; // Start of input
891 
892  float speech_gain = ff_dot_productf(samples, samples,
894 
895  float pole_out[AMR_SUBFRAME_SIZE + LP_FILTER_ORDER]; // Output of pole filter
896  const float *gamma_n, *gamma_d; // Formant filter factor table
897  float lpc_n[LP_FILTER_ORDER], lpc_d[LP_FILTER_ORDER]; // Transfer function coefficients
898 
899  if (p->cur_frame_mode == MODE_12k2 || p->cur_frame_mode == MODE_10k2) {
900  gamma_n = ff_pow_0_7;
901  gamma_d = ff_pow_0_75;
902  } else {
903  gamma_n = ff_pow_0_55;
904  gamma_d = ff_pow_0_7;
905  }
906 
907  for (i = 0; i < LP_FILTER_ORDER; i++) {
908  lpc_n[i] = lpc[i] * gamma_n[i];
909  lpc_d[i] = lpc[i] * gamma_d[i];
910  }
911 
912  memcpy(pole_out, p->postfilter_mem, sizeof(float) * LP_FILTER_ORDER);
913  ff_celp_lp_synthesis_filterf(pole_out + LP_FILTER_ORDER, lpc_d, samples,
914  AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
915  memcpy(p->postfilter_mem, pole_out + AMR_SUBFRAME_SIZE,
916  sizeof(float) * LP_FILTER_ORDER);
917 
918  ff_celp_lp_zero_synthesis_filterf(buf_out, lpc_n,
919  pole_out + LP_FILTER_ORDER,
920  AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
921 
922  ff_tilt_compensation(&p->tilt_mem, tilt_factor(lpc_n, lpc_d), buf_out,
924 
925  ff_adaptive_gain_control(buf_out, buf_out, speech_gain, AMR_SUBFRAME_SIZE,
927 }
928 
930 
931 static int amrnb_decode_frame(AVCodecContext *avctx, void *data,
932  int *got_frame_ptr, AVPacket *avpkt)
933 {
934 
935  AMRContext *p = avctx->priv_data; // pointer to private data
936  const uint8_t *buf = avpkt->data;
937  int buf_size = avpkt->size;
938  float *buf_out; // pointer to the output data buffer
939  int i, subframe, ret;
940  float fixed_gain_factor;
941  AMRFixed fixed_sparse = {0}; // fixed vector up to anti-sparseness processing
942  float spare_vector[AMR_SUBFRAME_SIZE]; // extra stack space to hold result from anti-sparseness processing
943  float synth_fixed_gain; // the fixed gain that synthesis should use
944  const float *synth_fixed_vector; // pointer to the fixed vector that synthesis should use
945 
946  /* get output buffer */
948  if ((ret = ff_get_buffer(avctx, &p->avframe)) < 0) {
949  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
950  return ret;
951  }
952  buf_out = (float *)p->avframe.data[0];
953 
954  p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
955  if (p->cur_frame_mode == NO_DATA) {
956  av_log(avctx, AV_LOG_ERROR, "Corrupt bitstream\n");
957  return AVERROR_INVALIDDATA;
958  }
959  if (p->cur_frame_mode == MODE_DTX) {
960  av_log_missing_feature(avctx, "dtx mode", 1);
961  return -1;
962  }
963 
964  if (p->cur_frame_mode == MODE_12k2) {
965  lsf2lsp_5(p);
966  } else
967  lsf2lsp_3(p);
968 
969  for (i = 0; i < 4; i++)
970  ff_acelp_lspd2lpc(p->lsp[i], p->lpc[i], 5);
971 
972  for (subframe = 0; subframe < 4; subframe++) {
973  const AMRNBSubframe *amr_subframe = &p->frame.subframe[subframe];
974 
975  decode_pitch_vector(p, amr_subframe, subframe);
976 
977  decode_fixed_sparse(&fixed_sparse, amr_subframe->pulses,
978  p->cur_frame_mode, subframe);
979 
980  // The fixed gain (section 6.1.3) depends on the fixed vector
981  // (section 6.1.2), but the fixed vector calculation uses
982  // pitch sharpening based on the on the pitch gain (section 6.1.3).
983  // So the correct order is: pitch gain, pitch sharpening, fixed gain.
984  decode_gains(p, amr_subframe, p->cur_frame_mode, subframe,
985  &fixed_gain_factor);
986 
987  pitch_sharpening(p, subframe, p->cur_frame_mode, &fixed_sparse);
988 
989  if (fixed_sparse.pitch_lag == 0) {
990  av_log(avctx, AV_LOG_ERROR, "The file is corrupted, pitch_lag = 0 is not allowed\n");
991  return AVERROR_INVALIDDATA;
992  }
993  ff_set_fixed_vector(p->fixed_vector, &fixed_sparse, 1.0,
995 
996  p->fixed_gain[4] =
997  ff_amr_set_fixed_gain(fixed_gain_factor,
1000  p->prediction_error,
1002 
1003  // The excitation feedback is calculated without any processing such
1004  // as fixed gain smoothing. This isn't mentioned in the specification.
1005  for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1006  p->excitation[i] *= p->pitch_gain[4];
1007  ff_set_fixed_vector(p->excitation, &fixed_sparse, p->fixed_gain[4],
1008  AMR_SUBFRAME_SIZE);
1009 
1010  // In the ref decoder, excitation is stored with no fractional bits.
1011  // This step prevents buzz in silent periods. The ref encoder can
1012  // emit long sequences with pitch factor greater than one. This
1013  // creates unwanted feedback if the excitation vector is nonzero.
1014  // (e.g. test sequence T19_795.COD in 3GPP TS 26.074)
1015  for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1016  p->excitation[i] = truncf(p->excitation[i]);
1017 
1018  // Smooth fixed gain.
1019  // The specification is ambiguous, but in the reference source, the
1020  // smoothed value is NOT fed back into later fixed gain smoothing.
1021  synth_fixed_gain = fixed_gain_smooth(p, p->lsf_q[subframe],
1022  p->lsf_avg, p->cur_frame_mode);
1023 
1024  synth_fixed_vector = anti_sparseness(p, &fixed_sparse, p->fixed_vector,
1025  synth_fixed_gain, spare_vector);
1026 
1027  if (synthesis(p, p->lpc[subframe], synth_fixed_gain,
1028  synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 0))
1029  // overflow detected -> rerun synthesis scaling pitch vector down
1030  // by a factor of 4, skipping pitch vector contribution emphasis
1031  // and adaptive gain control
1032  synthesis(p, p->lpc[subframe], synth_fixed_gain,
1033  synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 1);
1034 
1035  postfilter(p, p->lpc[subframe], buf_out + subframe * AMR_SUBFRAME_SIZE);
1036 
1037  // update buffers and history
1038  ff_clear_fixed_vector(p->fixed_vector, &fixed_sparse, AMR_SUBFRAME_SIZE);
1039  update_state(p);
1040  }
1041 
1046 
1047  /* Update averaged lsf vector (used for fixed gain smoothing).
1048  *
1049  * Note that lsf_avg should not incorporate the current frame's LSFs
1050  * for fixed_gain_smooth.
1051  * The specification has an incorrect formula: the reference decoder uses
1052  * qbar(n-1) rather than qbar(n) in section 6.1(4) equation 71. */
1054  0.84, 0.16, LP_FILTER_ORDER);
1055 
1056  *got_frame_ptr = 1;
1057  *(AVFrame *)data = p->avframe;
1058 
1059  /* return the amount of bytes consumed if everything was OK */
1060  return frame_sizes_nb[p->cur_frame_mode] + 1; // +7 for rounding and +8 for TOC
1061 }
1062 
1063 
1065  .name = "amrnb",
1066  .type = AVMEDIA_TYPE_AUDIO,
1067  .id = CODEC_ID_AMR_NB,
1068  .priv_data_size = sizeof(AMRContext),
1071  .capabilities = CODEC_CAP_DR1,
1072  .long_name = NULL_IF_CONFIG_SMALL("Adaptive Multi-Rate NarrowBand"),
1073  .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
1074 };