FFmpeg
Loading...
Searching...
No Matches
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 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/**
25 * @file
26 * AMR narrowband decoder
27 *
28 * This decoder uses floats for simplicity and so is not bit-exact. One
29 * difference is that differences in phase can accumulate. The test sequences
30 * in 3GPP TS 26.074 can still be useful.
31 *
32 * - Comparing this file's output to the output of the ref decoder gives a
33 * PSNR of 30 to 80. Plotting the output samples shows a difference in
34 * phase in some areas.
35 *
36 * - Comparing both decoders against their input, this decoder gives a similar
37 * PSNR. If the test sequence homing frames are removed (this decoder does
38 * not detect them), the PSNR is at least as good as the reference on 140
39 * out of 169 tests.
40 */
41
42
43#include <string.h>
44#include <math.h>
45
47#include "avcodec.h"
48#include "libavutil/common.h"
49#include "libavutil/avassert.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 "get_bits.h"
56#include "lsp.h"
57#include "amr.h"
58#include "codec_internal.h"
59#include "decode.h"
60
61#include "amrnbdata.h"
62
63#define AMR_BLOCK_SIZE 160 ///< samples per frame
64#define AMR_SAMPLE_BOUND 32768.0 ///< threshold for synthesis overflow
65
66/**
67 * Scale from constructed speech to [-1,1]
68 *
69 * AMR is designed to produce 16-bit PCM samples (3GPP TS 26.090 4.2) but
70 * upscales by two (section 6.2.2).
71 *
72 * Fundamentally, this scale is determined by energy_mean through
73 * the fixed vector contribution to the excitation vector.
74 */
75#define AMR_SAMPLE_SCALE (2.0 / 32768.0)
76
77/** Prediction factor for 12.2kbit/s mode */
78#define PRED_FAC_MODE_12k2 0.65
79
80#define LSF_R_FAC (8000.0 / 32768.0) ///< LSF residual tables to Hertz
81#define MIN_LSF_SPACING (50.0488 / 8000.0) ///< Ensures stability of LPC filter
82#define PITCH_LAG_MIN_MODE_12k2 18 ///< Lower bound on decoded lag search in 12.2kbit/s mode
83
84/**
85 * Weights of the past LSFs and of their mean when concealing a lost frame,
86 * ALPHA and ONE_ALPHA of 3GPP TS 26.073. The 12.2 kbit/s mode uses a
87 * different pair than all the other modes do:
88 *
89 * D_plsf_3() d_plsf_3.c:51-52 29491 / 3277 (0.90 / 0.10)
90 * D_plsf_5() d_plsf_5.c:49-50 31128 / 1639 (0.95 / 0.05)
91 */
92#define LSF_ALPHA (29491.0f / 32768.0f)
93#define LSF_ONE_ALPHA ( 3277.0f / 32768.0f)
94#define LSF_ALPHA_5 (31128.0f / 32768.0f)
95#define LSF_ONE_ALPHA_5 ( 1639.0f / 32768.0f)
96
97/** Initial energy in dB. Also used for bad frames (unimplemented). */
98#define MIN_ENERGY -14.0
99
100/** Maximum sharpening factor
101 *
102 * The specification says 0.8, which should be 13107, but the reference C code
103 * uses 13017 instead. (Amusingly the same applies to SHARP_MAX in g729dec.c.)
104 */
105#define SHARP_MAX 0.79449462890625
106
107/** Number of impulse response coefficients used for tilt factor */
108#define AMR_TILT_RESPONSE 22
109/** Tilt factor = 1st reflection coefficient * gamma_t */
110#define AMR_TILT_GAMMA_T 0.8
111/** Adaptive gain control factor used in post-filter */
112#define AMR_AGC_ALPHA 0.9
113
114/**
115 * Scaling of the comfort noise excitation level, the pulses of the fixed
116 * codebook of the reference have an amplitude of 4096 in a Q15 domain
117 */
118#define CN_LEVEL_SCALE (1.0f / 8.0f)
119
120/** Received frame types used by the comfort noise (DTX) state machine */
131
132/** States of the comfort noise (DTX) state machine */
138
139typedef struct AMRContext {
140 AMRNBFrame frame; ///< decoded AMR parameters (lsf coefficients, codebook indexes, etc)
141 uint8_t bad_frame_indicator; ///< bad frame ? 1 : 0
143
144 int16_t prev_lsf_r[LP_FILTER_ORDER]; ///< residual LSF vector from previous subframe
145 double lsp[4][LP_FILTER_ORDER]; ///< lsp vectors from current frame
146 double prev_lsp_sub4[LP_FILTER_ORDER]; ///< lsp vector for the 4th subframe of the previous frame
147
148 float lsf_q[4][LP_FILTER_ORDER]; ///< Interpolated LSF vector for fixed gain smoothing
149 float lsf_avg[LP_FILTER_ORDER]; ///< vector of averaged lsf vector
150
151 float lpc[4][LP_FILTER_ORDER]; ///< lpc coefficient vectors for 4 subframes
152
153 uint8_t pitch_lag_int; ///< integer part of pitch lag from current subframe
154
155 float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE]; ///< current excitation and all necessary excitation history
156 float *excitation; ///< pointer to the current excitation vector in excitation_buf
157
158 float pitch_vector[AMR_SUBFRAME_SIZE]; ///< adaptive code book (pitch) vector
159 float fixed_vector[AMR_SUBFRAME_SIZE]; ///< algebraic codebook (fixed) vector (must be kept zero between frames)
160
161 float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes
162 float pitch_gain[5]; ///< quantified pitch gains for the current and previous four subframes
163 float fixed_gain[5]; ///< quantified fixed gains for the current and previous four subframes
164
165 float beta; ///< previous pitch_gain, bounded by [0.0,SHARP_MAX]
166 uint8_t diff_count; ///< the number of subframes for which diff has been above 0.65
167 uint8_t hang_count; ///< the number of subframes since a hangover period started
168
169 float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness processing to determine "onset"
170 uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none
171 uint8_t ir_filter_onset; ///< flag for impulse response filter strength
172
173 float postfilter_mem[10]; ///< previous intermediate values in the formant filter
174 float tilt_mem; ///< previous input to tilt compensation filter
175 float postfilter_agc; ///< previous factor used for adaptive gain control
176 float high_pass_mem[2]; ///< previous intermediate values in the high-pass filter
177
178 float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE]; ///< floating point samples
179
180 ACELPFContext acelpf_ctx; ///< context for filters for ACELP-based codecs
181 ACELPVContext acelpv_ctx; ///< context for vector operations for ACELP-based codecs
182 CELPFContext celpf_ctx; ///< context for filters for CELP-based codecs
183 CELPMContext celpm_ctx; ///< context for fixed point math operations
184
185 /* Comfort noise (DTX) state */
186 int dtx_state; ///< current DTX state (SPEECH, DTX or DTX_MUTE)
187 int since_last_sid; ///< frames since the last SID update
188 float sid_period_inv; ///< inverse of the SID update period
189 float cn_log_en; ///< comfort noise log2 energy of the last SID
190 float cn_log_en_old; ///< comfort noise log2 energy before the last SID
191 float cn_lsp[LP_FILTER_ORDER]; ///< comfort noise LSP vector of the last SID
192 float cn_lsp_old[LP_FILTER_ORDER]; ///< comfort noise LSP vector before the last SID
193 uint32_t cn_seed; ///< comfort noise generator shift register
194 float lsf_hist[DTX_HIST_SIZE][LP_FILTER_ORDER]; ///< LSF history used when a SID frame starts DTX
195 float lsf_hist_mean[DTX_HIST_SIZE][LP_FILTER_ORDER]; ///< deviation from the mean of the LSF history
196 float log_en_hist[DTX_HIST_SIZE]; ///< energy history used when a SID frame starts DTX
197 int lsf_hist_ptr; ///< next position to write in the LSF history
198 int log_en_hist_ptr; ///< next position to write in the energy history
199 float log_en_adjust; ///< speech mode dependent level adjustment
200 float log_pg_mean; ///< smoothed logarithm of the prediction gain
201 int hangover_count; ///< remaining DTX hangover frames
202 int dec_ana_elapsed; ///< frames since the last DTX hangover
203 int hangover_added; ///< a DTX hangover was assumed for this frame
204 int sid_frame; ///< the current frame carries CN parameters
205 int valid_data; ///< the CN parameters of this frame may be used
206 int data_updated; ///< CN parameters have been renewed at least once
207 int prev_bf; ///< the previous frame could not be used
208
209 /* Bad frame handling and error concealment state, 3GPP TS 26.091 */
210 int bfh_state; ///< bad frame handling state (0..6)
211 uint32_t no_data_seed; ///< pseudo-random source for lost frames
214} AMRContext;
215
219
220/** Double version of ff_weighted_vector_sumf() */
221static void weighted_vector_sumd(double *out, const double *in_a,
222 const double *in_b, double weight_coeff_a,
223 double weight_coeff_b, int length)
224{
225 int i;
226
227 for (i = 0; i < length; i++)
228 out[i] = weight_coeff_a * in_a[i]
229 + weight_coeff_b * in_b[i];
230}
231
232/**
233 * Reset the bad frame handling and gain concealment states, matching
234 * Decoder_amr_reset() and ec_gain_*_reset() in 3GPP TS 26.073.
235 */
236static void ec_reset(AMRContext *p)
237{
238 int i;
239
240 p->bfh_state = 0;
241 p->no_data_seed = 21845;
242 /* ec_gain_pitch_reset() stores 1640 in Q14. ec_gain_code_reset() stores 1
243 in Q1 (0.5). In this floating-point decoder p->excitation[] uses unit
244 pulse amplitude and is truncated with truncf(), so 1.0f is used as an
245 intentional unit-pulse floor rather than the 0.5 fixed-point scale. */
246 for (i = 0; i < 5; i++) {
247 p->ec_gain_pbuf[i] = 1640.0f / 16384.0f;
248 p->ec_gain_gbuf[i] = 1.0f;
249 }
250 p->ec_gain_past_pit = 0.0f;
251 p->ec_gain_prev_pit = 1.0f;
252 p->ec_gain_past_code = 0.0f;
253 p->ec_gain_prev_code = 1.0f;
254}
255
257{
259 int i, j;
260
261 if (avctx->ch_layout.nb_channels > 2) {
262 avpriv_report_missing_feature(avctx, ">2 channel AMR");
264 }
265
266 if (!avctx->ch_layout.nb_channels) {
269 }
270 if (!avctx->sample_rate)
271 avctx->sample_rate = 8000;
273
274 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
275 AMRContext *p = &s->ch[ch];
276 // p->excitation always points to the same position in p->excitation_buf
277 p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1];
278
279 for (i = 0; i < LP_FILTER_ORDER; i++) {
280 p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15);
281 p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15);
282 }
283
284 for (i = 0; i < 4; i++)
285 p->prediction_error[i] = MIN_ENERGY;
286
287 /* Comfort noise state, matches the DTX decoder reset */
288 p->dtx_state = DTX;
289 p->sid_period_inv = 0.25f;
290 p->cn_log_en = 3500.0f / 2048.0f;
291 p->cn_log_en_old = p->cn_log_en;
292 p->cn_seed = CN_PN_SEED_INIT;
293 p->hangover_count = DTX_HANG_CONST;
294 p->dec_ana_elapsed = 32767;
295 p->prev_bf = 0;
296
297 ec_reset(p);
298
299 for (i = 0; i < LP_FILTER_ORDER; i++)
300 p->cn_lsp[i] = p->cn_lsp_old[i] = cn_lsp_init[i];
301
302 for (i = 0; i < DTX_HIST_SIZE; i++) {
303 for (j = 0; j < LP_FILTER_ORDER; j++)
304 p->lsf_hist[i][j] = lsf_3_mean[j] * (1.0 / 8000.0);
305 memset(p->lsf_hist_mean[i], 0, sizeof(p->lsf_hist_mean[i]));
306 p->log_en_hist[i] = p->cn_log_en;
307 }
308
309 ff_acelp_filter_init(&p->acelpf_ctx);
310 ff_acelp_vectors_init(&p->acelpv_ctx);
311 ff_celp_filter_init(&p->celpf_ctx);
312 ff_celp_math_init(&p->celpm_ctx);
313 }
314
315 return 0;
316}
317
318
319/**
320 * Unpack an RFC4867 speech frame into the AMR frame mode and parameters.
321 *
322 * The order of speech bits is specified by 3GPP TS 26.101.
323 *
324 * @param p the context
325 * @param buf pointer to the input buffer
326 * @param buf_size size of the input buffer
327 *
328 * @return the frame mode
329 */
330static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf,
331 int buf_size)
332{
333 enum Mode mode;
334
335 // Decode the first octet.
336 mode = buf[0] >> 3 & 0x0F; // frame type
337 p->bad_frame_indicator = (buf[0] & 0x4) != 0x4; // quality bit
338
339 if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) {
340 return NO_DATA;
341 }
342
343 if (mode < MODE_DTX)
344 ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1,
346
347 return mode;
348}
349
350
351/// @name AMR pitch LPC coefficient decoding functions
352/// @{
353
354/**
355 * Interpolate the LSF vector (used for fixed gain smoothing).
356 * The interpolation is done over all four subframes even in MODE_12k2.
357 *
358 * @param[in] ctx The Context
359 * @param[in,out] lsf_q LSFs in [0,1] for each subframe
360 * @param[in] lsf_new New LSFs in [0,1] for subframe 4
361 */
362static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
363{
364 int i;
365
366 for (i = 0; i < 4; i++)
367 ctx->weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new,
368 0.25 * (3 - i), 0.25 * (i + 1),
370}
371
372/**
373 * Decode a set of 5 split-matrix quantized lsf indexes into an lsp vector.
374 *
375 * @param p the context
376 * @param lsp output LSP vector
377 * @param lsf_no_r LSF vector without the residual vector added
378 * @param lsf_quantizer pointers to LSF dictionary tables
379 * @param quantizer_offset offset in tables
380 * @param sign for the 3 dictionary table
381 * @param update store data for computing the next frame's LSFs
382 */
384 const float lsf_no_r[LP_FILTER_ORDER],
385 const int16_t *lsf_quantizer[5],
386 const int quantizer_offset,
387 const int sign, const int update)
388{
389 int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector
390 float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector
391 int i;
392
393 for (i = 0; i < LP_FILTER_ORDER >> 1; i++)
394 memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset],
395 2 * sizeof(*lsf_r));
396
397 if (sign) {
398 lsf_r[4] *= -1;
399 lsf_r[5] *= -1;
400 }
401
402 if (update)
403 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
404
405 for (i = 0; i < LP_FILTER_ORDER; i++)
406 lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0);
407
409
410 if (update)
411 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
412
414}
415
416/**
417 * Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors.
418 *
419 * @param p pointer to the AMRContext
420 * @param bfi the frame is unusable, conceal the LSF vector
421 */
422static void lsf2lsp_5(AMRContext *p, int bfi)
423{
424 const uint16_t *lsf_param = p->frame.lsf;
425 float lsf_no_r[LP_FILTER_ORDER]; // LSFs without the residual vector
426 const int16_t *lsf_quantizer[5];
427 float lsf_q[LP_FILTER_ORDER];
428 int i;
429
430 if (bfi) {
431 /* Use the past LSFs slightly shifted towards their mean, and update
432 the prediction residual, see D_plsf_5() in 3GPP TS 26.073
433 (d_plsf_5.c:88-115). The LSFs are kept in Hertz while the
434 residual prev_lsf_r is in the codebook domain (LSF_R_FAC). */
435 for (i = 0; i < LP_FILTER_ORDER; i++)
436 lsf_q[i] = (LSF_ALPHA_5 * p->lsf_q[3][i] * 8000.0f +
437 LSF_ONE_ALPHA_5 * lsf_5_mean[i]) * (1.0f / 8000.0f);
438
439 for (i = 0; i < LP_FILTER_ORDER; i++)
440 p->prev_lsf_r[i] = av_clip_int16(lrintf(
441 (lsf_q[i] * 8000.0f -
442 (lsf_5_mean[i] + PRED_FAC_MODE_12k2 * p->prev_lsf_r[i] *
443 LSF_R_FAC)) / LSF_R_FAC));
444
446 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
447 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
448
449 /* the first and second LSP vectors are concealed identically */
450 memcpy(p->lsp[1], p->lsp[3], sizeof(p->lsp[1]));
451 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5,
453 weighted_vector_sumd(p->lsp[2], p->lsp[1], p->lsp[3], 0.5, 0.5,
455 return;
456 }
457
458 lsf_quantizer[0] = lsf_5_1[lsf_param[0]];
459 lsf_quantizer[1] = lsf_5_2[lsf_param[1]];
460 lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1];
461 lsf_quantizer[3] = lsf_5_4[lsf_param[3]];
462 lsf_quantizer[4] = lsf_5_5[lsf_param[4]];
463
464 for (i = 0; i < LP_FILTER_ORDER; i++)
465 lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i];
466
467 lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0);
468 lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1);
469
470 // interpolate LSP vectors at subframes 1 and 3
471 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER);
472 weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);
473}
474
475/**
476 * Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector.
477 *
478 * @param p pointer to the AMRContext
479 * @param bfi the frame is unusable, conceal the LSF vector
480 */
481static void lsf2lsp_3(AMRContext *p, int bfi)
482{
483 const uint16_t *lsf_param = p->frame.lsf;
484 float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector
485 int i, j;
486
487 if (bfi) {
488 /* Use the past LSFs slightly shifted towards their mean, and update
489 the prediction residual, see D_plsf_3() in 3GPP TS 26.073
490 (d_plsf_3.c:84-115). The LSFs are kept in Hertz while the residual
491 prev_lsf_r is in the codebook domain (LSF_R_FAC). */
492 for (i = 0; i < LP_FILTER_ORDER; i++)
493 lsf_q[i] = (LSF_ALPHA * p->lsf_q[3][i] * 8000.0f +
494 LSF_ONE_ALPHA * lsf_3_mean[i]) * (1.0f / 8000.0f);
495
496 for (i = 0; i < LP_FILTER_ORDER; i++)
497 p->prev_lsf_r[i] = av_clip_int16(lrintf(
498 (lsf_q[i] * 8000.0f -
499 (lsf_3_mean[i] + pred_fac[i] * p->prev_lsf_r[i] *
500 LSF_R_FAC)) / LSF_R_FAC));
501
503 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
504 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
505
506 // interpolate LSP vectors at subframes 1, 2 and 3
507 for (i = 1; i <= 3; i++)
508 for (j = 0; j < LP_FILTER_ORDER; j++)
509 p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
510 (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
511 return;
512 }
513
514 int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector
515 const int16_t *lsf_quantizer;
516
517 lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]];
518 memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r));
519
520 lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)];
521 memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r));
522
523 lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]];
524 memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r));
525
526 // calculate mean-removed LSF vector and add mean
527 for (i = 0; i < LP_FILTER_ORDER; i++)
528 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);
529
531
532 // store data for computing the next frame's LSFs
533 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
534 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
535
536 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
537
538 // interpolate LSP vectors at subframes 1, 2 and 3
539 for (i = 1; i <= 3; i++)
540 for(j = 0; j < LP_FILTER_ORDER; j++)
541 p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
542 (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
543}
544
545/// @}
546
547
548/// @name AMR pitch vector decoding functions
549/// @{
550
551/**
552 * Like ff_decode_pitch_lag(), but with 1/6 resolution
553 */
554static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index,
555 const int prev_lag_int, const int subframe)
556{
557 if (subframe == 0 || subframe == 2) {
558 if (pitch_index < 463) {
559 *lag_int = (pitch_index + 107) * 10923 >> 16;
560 *lag_frac = pitch_index - *lag_int * 6 + 105;
561 } else {
562 *lag_int = pitch_index - 368;
563 *lag_frac = 0;
564 }
565 } else {
566 *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1;
567 *lag_frac = pitch_index - *lag_int * 6 - 3;
568 *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2,
569 PITCH_DELAY_MAX - 9);
570 }
571}
572
574 const AMRNBSubframe *amr_subframe,
575 const int subframe, const int bfi)
576{
577 int pitch_lag_int, pitch_lag_frac;
578 enum Mode mode = p->cur_frame_mode;
579
580 if (bfi) {
581 /* Graceful pitch degradation for a lost frame, see Decoder_amr() in
582 3GPP TS 26.073 (dec_amr.c:527-548 for the delta-coded modes and
583 dec_amr.c:559-568 for 12.2kbit/s). The integer lag keeps growing
584 until it reaches its maximum instead of being decoded. */
585 if (mode != MODE_12k2)
586 pitch_lag_int = FFMIN(p->pitch_lag_int + 1, PITCH_DELAY_MAX);
587 else
588 pitch_lag_int = p->pitch_lag_int;
589 pitch_lag_frac = 0;
590 } else if (p->cur_frame_mode == MODE_12k2) {
591 decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac,
592 amr_subframe->p_lag, p->pitch_lag_int,
593 subframe);
594 } else {
595 ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac,
596 amr_subframe->p_lag,
597 p->pitch_lag_int, subframe,
598 mode != MODE_4k75 && mode != MODE_5k15,
599 mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6));
600 pitch_lag_frac *= 2;
601 }
602
603 p->pitch_lag_int = pitch_lag_int; // store previous lag in a uint8_t
604
605 pitch_lag_int += pitch_lag_frac > 0;
606
607 /* Calculate the pitch vector by interpolating the past excitation at the
608 pitch lag using a b60 hamming windowed sinc function. */
609 p->acelpf_ctx.acelp_interpolatef(p->excitation,
610 p->excitation + 1 - pitch_lag_int,
611 ff_b60_sinc, 6,
612 pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0),
614
615 memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));
616}
617
618/// @}
619
620
621/// @name AMR algebraic code book (fixed) vector decoding functions
622/// @{
623
624/**
625 * Decode a 10-bit algebraic codebook index from a 10.2 kbit/s frame.
626 */
627static void decode_10bit_pulse(int code, int pulse_position[8],
628 int i1, int i2, int i3)
629{
630 // coded using 7+3 bits with the 3 LSBs being, individually, the LSB of 1 of
631 // the 3 pulses and the upper 7 bits being coded in base 5
632 const uint8_t *positions = base_five_table[code >> 3];
633 pulse_position[i1] = (positions[2] << 1) + ( code & 1);
634 pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1);
635 pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);
636}
637
638/**
639 * Decode the algebraic codebook index to pulse positions and signs and
640 * construct the algebraic codebook vector for MODE_10k2.
641 *
642 * @param fixed_index positions of the eight pulses
643 * @param fixed_sparse pointer to the algebraic codebook vector
644 */
645static void decode_8_pulses_31bits(const int16_t *fixed_index,
646 AMRFixed *fixed_sparse)
647{
648 int pulse_position[8];
649 int i, temp;
650
651 decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1);
652 decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5);
653
654 // coded using 5+2 bits with the 2 LSBs being, individually, the LSB of 1 of
655 // the 2 pulses and the upper 5 bits being coded in base 5
656 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5;
657 pulse_position[3] = temp % 5;
658 pulse_position[7] = temp / 5;
659 if (pulse_position[7] & 1)
660 pulse_position[3] = 4 - pulse_position[3];
661 pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1);
662 pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1);
663
664 fixed_sparse->n = 8;
665 for (i = 0; i < 4; i++) {
666 const int pos1 = (pulse_position[i] << 2) + i;
667 const int pos2 = (pulse_position[i + 4] << 2) + i;
668 const float sign = fixed_index[i] ? -1.0 : 1.0;
669 fixed_sparse->x[i ] = pos1;
670 fixed_sparse->x[i + 4] = pos2;
671 fixed_sparse->y[i ] = sign;
672 fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign;
673 }
674}
675
676/**
677 * Decode the algebraic codebook index to pulse positions and signs,
678 * then construct the algebraic codebook vector.
679 *
680 * nb of pulses | bits encoding pulses
681 * For MODE_4k75 or MODE_5k15, 2 | 1-3, 4-6, 7
682 * MODE_5k9, 2 | 1, 2-4, 5-6, 7-9
683 * MODE_6k7, 3 | 1-3, 4, 5-7, 8, 9-11
684 * MODE_7k4 or MODE_7k95, 4 | 1-3, 4-6, 7-9, 10, 11-13
685 *
686 * @param fixed_sparse pointer to the algebraic codebook vector
687 * @param pulses algebraic codebook indexes
688 * @param mode mode of the current frame
689 * @param subframe current subframe number
690 */
691static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses,
692 const enum Mode mode, const int subframe)
693{
694 av_assert1(MODE_4k75 <= (signed)mode && mode <= MODE_12k2);
695
696 if (mode == MODE_12k2) {
697 ff_decode_10_pulses_35bits(pulses, fixed_sparse, gray_decode, 5, 3);
698 } else if (mode == MODE_10k2) {
699 decode_8_pulses_31bits(pulses, fixed_sparse);
700 } else {
701 int *pulse_position = fixed_sparse->x;
702 int i, pulse_subset;
703 const int fixed_index = pulses[0];
704
705 if (mode <= MODE_5k15) {
706 pulse_subset = ((fixed_index >> 3) & 8) + (subframe << 1);
707 pulse_position[0] = ( fixed_index & 7) * 5 + track_position[pulse_subset];
708 pulse_position[1] = ((fixed_index >> 3) & 7) * 5 + track_position[pulse_subset + 1];
709 fixed_sparse->n = 2;
710 } else if (mode == MODE_5k9) {
711 pulse_subset = ((fixed_index & 1) << 1) + 1;
712 pulse_position[0] = ((fixed_index >> 1) & 7) * 5 + pulse_subset;
713 pulse_subset = (fixed_index >> 4) & 3;
714 pulse_position[1] = ((fixed_index >> 6) & 7) * 5 + pulse_subset + (pulse_subset == 3 ? 1 : 0);
715 fixed_sparse->n = pulse_position[0] == pulse_position[1] ? 1 : 2;
716 } else if (mode == MODE_6k7) {
717 pulse_position[0] = (fixed_index & 7) * 5;
718 pulse_subset = (fixed_index >> 2) & 2;
719 pulse_position[1] = ((fixed_index >> 4) & 7) * 5 + pulse_subset + 1;
720 pulse_subset = (fixed_index >> 6) & 2;
721 pulse_position[2] = ((fixed_index >> 8) & 7) * 5 + pulse_subset + 2;
722 fixed_sparse->n = 3;
723 } else { // mode <= MODE_7k95
724 pulse_position[0] = gray_decode[ fixed_index & 7];
725 pulse_position[1] = gray_decode[(fixed_index >> 3) & 7] + 1;
726 pulse_position[2] = gray_decode[(fixed_index >> 6) & 7] + 2;
727 pulse_subset = (fixed_index >> 9) & 1;
728 pulse_position[3] = gray_decode[(fixed_index >> 10) & 7] + pulse_subset + 3;
729 fixed_sparse->n = 4;
730 }
731 for (i = 0; i < fixed_sparse->n; i++)
732 fixed_sparse->y[i] = (pulses[1] >> i) & 1 ? 1.0 : -1.0;
733 }
734}
735
736/**
737 * Apply pitch lag to obtain the sharpened fixed vector (section 6.1.2)
738 *
739 * @param p the context
740 * @param subframe unpacked amr subframe
741 * @param mode mode of the current frame
742 * @param fixed_sparse sparse representation of the fixed vector
743 */
744static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode,
745 AMRFixed *fixed_sparse)
746{
747 // The spec suggests the current pitch gain is always used, but in other
748 // modes the pitch and codebook gains are jointly quantized (sec 5.8.2)
749 // so the codebook gain cannot depend on the quantized pitch gain.
750 if (mode == MODE_12k2)
751 p->beta = FFMIN(p->pitch_gain[4], 1.0);
752
753 fixed_sparse->pitch_lag = p->pitch_lag_int;
754 fixed_sparse->pitch_fac = p->beta;
755
756 // Save pitch sharpening factor for the next subframe
757 // MODE_4k75 only updates on the 2nd and 4th subframes - this follows from
758 // the fact that the gains for two subframes are jointly quantized.
759 if (mode != MODE_4k75 || subframe & 1)
760 p->beta = av_clipf(p->pitch_gain[4], 0.0, SHARP_MAX);
761}
762/// @}
763
764
765/// @name AMR gain decoding functions
766/// @{
767
768/**
769 * fixed gain smoothing
770 * Note that where the spec specifies the "spectrum in the q domain"
771 * in section 6.1.4, in fact frequencies should be used.
772 *
773 * @param p the context
774 * @param lsf LSFs for the current subframe, in the range [0,1]
775 * @param lsf_avg averaged LSFs
776 * @param mode mode of the current frame
777 *
778 * @return fixed gain smoothed
779 */
780static float fixed_gain_smooth(AMRContext *p , const float *lsf,
781 const float *lsf_avg, const enum Mode mode)
782{
783 float diff = 0.0;
784 int i;
785
786 for (i = 0; i < LP_FILTER_ORDER; i++)
787 diff += fabs(lsf_avg[i] - lsf[i]) / lsf_avg[i];
788
789 // If diff is large for ten subframes, disable smoothing for a 40-subframe
790 // hangover period.
791 p->diff_count++;
792 if (diff <= 0.65)
793 p->diff_count = 0;
794
795 if (p->diff_count > 10) {
796 p->hang_count = 0;
797 p->diff_count--; // don't let diff_count overflow
798 }
799
800 if (p->hang_count < 40) {
801 p->hang_count++;
802 } else if (mode < MODE_7k4 || mode == MODE_10k2) {
803 const float smoothing_factor = av_clipf(4.0 * diff - 1.6, 0.0, 1.0);
804 const float fixed_gain_mean = (p->fixed_gain[0] + p->fixed_gain[1] +
805 p->fixed_gain[2] + p->fixed_gain[3] +
806 p->fixed_gain[4]) * 0.2;
807 return smoothing_factor * p->fixed_gain[4] +
808 (1.0 - smoothing_factor) * fixed_gain_mean;
809 }
810 return p->fixed_gain[4];
811}
812
813/**
814 * Decode pitch gain and fixed gain factor (part of section 6.1.3).
815 *
816 * @param p the context
817 * @param amr_subframe unpacked amr subframe
818 * @param mode mode of the current frame
819 * @param subframe current subframe number
820 * @param fixed_gain_factor decoded gain correction factor
821 */
822static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe,
823 const enum Mode mode, const int subframe,
824 float *fixed_gain_factor)
825{
826 if (mode == MODE_12k2 || mode == MODE_7k95) {
827 p->pitch_gain[4] = qua_gain_pit [amr_subframe->p_gain ]
828 * (1.0 / 16384.0);
829 *fixed_gain_factor = qua_gain_code[amr_subframe->fixed_gain]
830 * (1.0 / 2048.0);
831 } else {
832 const uint16_t *gains;
833
834 if (mode >= MODE_6k7) {
835 gains = gains_high[amr_subframe->p_gain];
836 } else if (mode >= MODE_5k15) {
837 gains = gains_low [amr_subframe->p_gain];
838 } else {
839 // gain index is only coded in subframes 0,2 for MODE_4k75
840 gains = gains_MODE_4k75[(p->frame.subframe[subframe & 2].p_gain << 1) + (subframe & 1)];
841 }
842
843 p->pitch_gain[4] = gains[0] * (1.0 / 16384.0);
844 *fixed_gain_factor = gains[1] * (1.0 / 4096.0);
845 }
846}
847
848/// @}
849
850
851/// @name AMR error concealment functions
852/// @{
853
854/**
855 * Median of the last five gain values, equivalent to gmed_n() in
856 * 3GPP TS 26.073 (gmed_n.c:57-84).
857 */
858static float ec_gain_median(const float *buf)
859{
860 float tmp[5];
861 int i, j;
862
863 memcpy(tmp, buf, sizeof(tmp));
864
865 for (i = 1; i < 5; i++) {
866 float v = tmp[i];
867
868 for (j = i - 1; j >= 0 && tmp[j] > v; j--)
869 tmp[j + 1] = tmp[j];
870 tmp[j + 1] = v;
871 }
872
873 return tmp[2];
874}
875
876/**
877 * Conceal the pitch gain of a lost subframe, ec_gain_pitch() in
878 * 3GPP TS 26.073 (ec_gains.c:306-330).
879 *
880 * @param p the context
881 * @param state state of the bad frame handling state machine
882 */
883static void ec_gain_pitch(AMRContext *p, int state)
884{
885 /* pdown[] from 3GPP TS 26.073, kept as exact fractions of 32768 */
886 static const float pdown[7] = {
887 32767.0f / 32768.0f, 32112.0f / 32768.0f, 32112.0f / 32768.0f,
888 26214.0f / 32768.0f, 9830.0f / 32768.0f, 6553.0f / 32768.0f,
889 6553.0f / 32768.0f,
890 };
891 float gain = ec_gain_median(p->ec_gain_pbuf);
892
893 if (gain > p->ec_gain_past_pit)
894 gain = p->ec_gain_past_pit;
895 p->pitch_gain[4] = gain * pdown[state];
896}
897
898/**
899 * Update the pitch gain concealment state, ec_gain_pitch_update() in
900 * 3GPP TS 26.073 (ec_gains.c:343-379).
901 *
902 * @param p the context
903 * @param bfi the current frame is unusable
904 */
905static void ec_gain_pitch_update(AMRContext *p, int bfi)
906{
907 int i;
908
909 if (!bfi) {
910 /* Limit the gain of the first usable subframe after an unusable
911 frame to the last usable gain (3GPP TS 26.091 clause 7.2.2). */
912 if (p->prev_bf && p->pitch_gain[4] > p->ec_gain_prev_pit)
913 p->pitch_gain[4] = p->ec_gain_prev_pit;
914 p->ec_gain_prev_pit = p->pitch_gain[4];
915 }
916
917 p->ec_gain_past_pit = FFMIN(p->pitch_gain[4], 1.0f);
918
919 for (i = 1; i < 5; i++)
920 p->ec_gain_pbuf[i - 1] = p->ec_gain_pbuf[i];
921 p->ec_gain_pbuf[4] = p->ec_gain_past_pit;
922}
923
924/**
925 * Advance the gain predictor with the average of the four past energies,
926 * gc_pred_average_limited() followed by gc_pred_update(), as ec_gain_code()
927 * does in 3GPP TS 26.073 when the energies of the frame are not available.
928 *
929 * @param p the context
930 */
932{
933 float avg = 0.0f;
934 int i;
935
936 for (i = 0; i < 4; i++)
937 avg += p->prediction_error[i];
938 avg = FFMAX(avg * 0.25f, MIN_ENERGY);
939
940 memmove(&p->prediction_error[0], &p->prediction_error[1],
941 3 * sizeof(p->prediction_error[0]));
942 p->prediction_error[3] = avg;
943}
944
945/**
946 * Conceal the codebook gain of a lost subframe, ec_gain_code() in
947 * 3GPP TS 26.073 (ec_gains.c:137-171).
948 *
949 * @param p the context
950 * @param state state of the bad frame handling state machine
951 */
952static void ec_gain_code(AMRContext *p, int state)
953{
954 /* cdown[] from 3GPP TS 26.073, kept as exact fractions of 32768 */
955 static const float cdown[7] = {
956 32767.0f / 32768.0f, 32112.0f / 32768.0f, 32112.0f / 32768.0f,
957 32112.0f / 32768.0f, 32112.0f / 32768.0f, 32112.0f / 32768.0f,
958 22937.0f / 32768.0f,
959 };
960 float gain = ec_gain_median(p->ec_gain_gbuf);
961
962 if (gain > p->ec_gain_past_code)
963 gain = p->ec_gain_past_code;
964 p->fixed_gain[4] = gain * cdown[state];
965}
966
967/**
968 * Update the codebook gain concealment state, ec_gain_code_update() in
969 * 3GPP TS 26.073 (ec_gains.c:184-219).
970 *
971 * @param p the context
972 * @param bfi the current frame is unusable
973 */
974static void ec_gain_code_update(AMRContext *p, int bfi)
975{
976 int i;
977
978 if (!bfi) {
979 if (p->prev_bf && p->fixed_gain[4] > p->ec_gain_prev_code)
980 p->fixed_gain[4] = p->ec_gain_prev_code;
981 p->ec_gain_prev_code = p->fixed_gain[4];
982 }
983
984 p->ec_gain_past_code = p->fixed_gain[4];
985
986 for (i = 1; i < 5; i++)
987 p->ec_gain_gbuf[i - 1] = p->ec_gain_gbuf[i];
988 p->ec_gain_gbuf[4] = p->fixed_gain[4];
989}
990
991/// @}
992
993
994/// @name AMR preprocessing functions
995/// @{
996
997/**
998 * Circularly convolve a sparse fixed vector with a phase dispersion impulse
999 * response filter (D.6.2 of G.729 and 6.1.5 of AMR).
1000 *
1001 * @param out vector with filter applied
1002 * @param in source vector
1003 * @param filter phase filter coefficients
1004 *
1005 * out[n] = sum(i,0,len-1){ in[i] * filter[(len + n - i)%len] }
1006 */
1007static void apply_ir_filter(float *out, const AMRFixed *in,
1008 const float *filter)
1009{
1010 float filter1[AMR_SUBFRAME_SIZE], ///< filters at pitch lag*1 and *2
1011 filter2[AMR_SUBFRAME_SIZE];
1012 int lag = in->pitch_lag;
1013 float fac = in->pitch_fac;
1014 int i;
1015
1016 if (lag < AMR_SUBFRAME_SIZE) {
1019
1020 if (lag < AMR_SUBFRAME_SIZE >> 1)
1021 ff_celp_circ_addf(filter2, filter, filter1, lag, fac,
1023 }
1024
1025 memset(out, 0, sizeof(float) * AMR_SUBFRAME_SIZE);
1026 for (i = 0; i < in->n; i++) {
1027 int x = in->x[i];
1028 float y = in->y[i];
1029 const float *filterp;
1030
1031 if (x >= AMR_SUBFRAME_SIZE - lag) {
1032 filterp = filter;
1033 } else if (x >= AMR_SUBFRAME_SIZE - (lag << 1)) {
1034 filterp = filter1;
1035 } else
1036 filterp = filter2;
1037
1038 ff_celp_circ_addf(out, out, filterp, x, y, AMR_SUBFRAME_SIZE);
1039 }
1040}
1041
1042/**
1043 * Reduce fixed vector sparseness by smoothing with one of three IR filters.
1044 * Also know as "adaptive phase dispersion".
1045 *
1046 * This implements 3GPP TS 26.090 section 6.1(5).
1047 *
1048 * @param p the context
1049 * @param fixed_sparse algebraic codebook vector
1050 * @param fixed_vector unfiltered fixed vector
1051 * @param fixed_gain smoothed gain
1052 * @param out space for modified vector if necessary
1053 */
1054static const float *anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse,
1055 const float *fixed_vector,
1056 float fixed_gain, float *out)
1057{
1058 int ir_filter_nr;
1059
1060 if (p->pitch_gain[4] < 0.6) {
1061 ir_filter_nr = 0; // strong filtering
1062 } else if (p->pitch_gain[4] < 0.9) {
1063 ir_filter_nr = 1; // medium filtering
1064 } else
1065 ir_filter_nr = 2; // no filtering
1066
1067 // detect 'onset'
1068 if (fixed_gain > 2.0 * p->prev_sparse_fixed_gain) {
1069 p->ir_filter_onset = 2;
1070 } else if (p->ir_filter_onset)
1071 p->ir_filter_onset--;
1072
1073 if (!p->ir_filter_onset) {
1074 int i, count = 0;
1075
1076 for (i = 0; i < 5; i++)
1077 if (p->pitch_gain[i] < 0.6)
1078 count++;
1079 if (count > 2)
1080 ir_filter_nr = 0;
1081
1082 if (ir_filter_nr > p->prev_ir_filter_nr + 1)
1083 ir_filter_nr--;
1084 } else if (ir_filter_nr < 2)
1085 ir_filter_nr++;
1086
1087 // Disable filtering for very low level of fixed_gain.
1088 // Note this step is not specified in the technical description but is in
1089 // the reference source in the function Ph_disp.
1090 if (fixed_gain < 5.0)
1091 ir_filter_nr = 2;
1092
1093 if (p->cur_frame_mode != MODE_7k4 && p->cur_frame_mode < MODE_10k2
1094 && ir_filter_nr < 2) {
1095 apply_ir_filter(out, fixed_sparse,
1096 (p->cur_frame_mode == MODE_7k95 ?
1098 ir_filters_lookup)[ir_filter_nr]);
1099 fixed_vector = out;
1100 }
1101
1102 // update ir filter strength history
1103 p->prev_ir_filter_nr = ir_filter_nr;
1104 p->prev_sparse_fixed_gain = fixed_gain;
1105
1106 return fixed_vector;
1107}
1108
1109/// @}
1110
1111
1112/// @name AMR synthesis functions
1113/// @{
1114
1115/**
1116 * Conduct 10th order linear predictive coding synthesis.
1117 *
1118 * @param p pointer to the AMRContext
1119 * @param lpc pointer to the LPC coefficients
1120 * @param fixed_gain fixed codebook gain for synthesis
1121 * @param fixed_vector algebraic codebook vector
1122 * @param samples pointer to the output speech samples
1123 * @param overflow 16-bit overflow flag
1124 */
1125static int synthesis(AMRContext *p, float *lpc,
1126 float fixed_gain, const float *fixed_vector,
1127 float *samples, uint8_t overflow)
1128{
1129 int i;
1130 float excitation[AMR_SUBFRAME_SIZE];
1131
1132 // if an overflow has been detected, the pitch vector is scaled down by a
1133 // factor of 4
1134 if (overflow)
1135 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1136 p->pitch_vector[i] *= 0.25;
1137
1138 p->acelpv_ctx.weighted_vector_sumf(excitation, p->pitch_vector, fixed_vector,
1139 p->pitch_gain[4], fixed_gain, AMR_SUBFRAME_SIZE);
1140
1141 // emphasize pitch vector contribution
1142 if (p->pitch_gain[4] > 0.5 && !overflow) {
1143 float energy = p->celpm_ctx.dot_productf(excitation, excitation,
1145 float pitch_factor =
1146 p->pitch_gain[4] *
1147 (p->cur_frame_mode == MODE_12k2 ?
1148 0.25 * FFMIN(p->pitch_gain[4], 1.0) :
1149 0.5 * FFMIN(p->pitch_gain[4], SHARP_MAX));
1150
1151 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1152 excitation[i] += pitch_factor * p->pitch_vector[i];
1153
1154 ff_scale_vector_to_given_sum_of_squares(excitation, excitation, energy,
1156 }
1157
1158 p->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
1161
1162 // detect overflow
1163 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1164 if (fabsf(samples[i]) > AMR_SAMPLE_BOUND) {
1165 return 1;
1166 }
1167
1168 return 0;
1169}
1170
1171/// @}
1172
1173
1174/// @name AMR update functions
1175/// @{
1176
1177/**
1178 * Update buffers and history at the end of decoding a subframe.
1179 *
1180 * @param p pointer to the AMRContext
1181 */
1183{
1184 memcpy(p->prev_lsp_sub4, p->lsp[3], LP_FILTER_ORDER * sizeof(p->lsp[3][0]));
1185
1186 memmove(&p->excitation_buf[0], &p->excitation_buf[AMR_SUBFRAME_SIZE],
1187 (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) * sizeof(float));
1188
1189 memmove(&p->pitch_gain[0], &p->pitch_gain[1], 4 * sizeof(float));
1190 memmove(&p->fixed_gain[0], &p->fixed_gain[1], 4 * sizeof(float));
1191
1192 memmove(&p->samples_in[0], &p->samples_in[AMR_SUBFRAME_SIZE],
1193 LP_FILTER_ORDER * sizeof(float));
1194}
1195
1196/// @}
1197
1198
1199/// @name AMR Postprocessing functions
1200/// @{
1201
1202/**
1203 * Get the tilt factor of a formant filter from its transfer function
1204 *
1205 * @param p The Context
1206 * @param lpc_n LP_FILTER_ORDER coefficients of the numerator
1207 * @param lpc_d LP_FILTER_ORDER coefficients of the denominator
1208 */
1209static float tilt_factor(AMRContext *p, float *lpc_n, float *lpc_d)
1210{
1211 float rh0, rh1; // autocorrelation at lag 0 and 1
1212
1213 // LP_FILTER_ORDER prior zeros are needed for ff_celp_lp_synthesis_filterf
1214 float impulse_buffer[LP_FILTER_ORDER + AMR_TILT_RESPONSE] = { 0 };
1215 float *hf = impulse_buffer + LP_FILTER_ORDER; // start of impulse response
1216
1217 hf[0] = 1.0;
1218 memcpy(hf + 1, lpc_n, sizeof(float) * LP_FILTER_ORDER);
1219 p->celpf_ctx.celp_lp_synthesis_filterf(hf, lpc_d, hf,
1222
1223 rh0 = p->celpm_ctx.dot_productf(hf, hf, AMR_TILT_RESPONSE);
1224 rh1 = p->celpm_ctx.dot_productf(hf, hf + 1, AMR_TILT_RESPONSE - 1);
1225
1226 // The spec only specifies this check for 12.2 and 10.2 kbit/s
1227 // modes. But in the ref source the tilt is always non-negative.
1228 return rh1 >= 0.0 ? rh1 / rh0 * AMR_TILT_GAMMA_T : 0.0;
1229}
1230
1231/**
1232 * Perform adaptive post-filtering to enhance the quality of the speech.
1233 * See section 6.2.1.
1234 *
1235 * @param p pointer to the AMRContext
1236 * @param lpc interpolated LP coefficients for this subframe
1237 * @param buf_out output of the filter
1238 */
1239static void postfilter(AMRContext *p, float *lpc, float *buf_out)
1240{
1241 int i;
1242 float *samples = p->samples_in + LP_FILTER_ORDER; // Start of input
1243
1244 float speech_gain = p->celpm_ctx.dot_productf(samples, samples,
1246
1247 float pole_out[AMR_SUBFRAME_SIZE + LP_FILTER_ORDER]; // Output of pole filter
1248 const float *gamma_n, *gamma_d; // Formant filter factor table
1249 float lpc_n[LP_FILTER_ORDER], lpc_d[LP_FILTER_ORDER]; // Transfer function coefficients
1250
1251 if (p->cur_frame_mode == MODE_12k2 || p->cur_frame_mode == MODE_10k2) {
1252 gamma_n = ff_pow_0_7;
1253 gamma_d = ff_pow_0_75;
1254 } else {
1255 gamma_n = ff_pow_0_55;
1256 gamma_d = ff_pow_0_7;
1257 }
1258
1259 for (i = 0; i < LP_FILTER_ORDER; i++) {
1260 lpc_n[i] = lpc[i] * gamma_n[i];
1261 lpc_d[i] = lpc[i] * gamma_d[i];
1262 }
1263
1264 memcpy(pole_out, p->postfilter_mem, sizeof(float) * LP_FILTER_ORDER);
1265 p->celpf_ctx.celp_lp_synthesis_filterf(pole_out + LP_FILTER_ORDER, lpc_d, samples,
1267 memcpy(p->postfilter_mem, pole_out + AMR_SUBFRAME_SIZE,
1268 sizeof(float) * LP_FILTER_ORDER);
1269
1270 p->celpf_ctx.celp_lp_zero_synthesis_filterf(buf_out, lpc_n,
1271 pole_out + LP_FILTER_ORDER,
1273
1274 ff_tilt_compensation(&p->tilt_mem, tilt_factor(p, lpc_n, lpc_d), buf_out,
1276
1277 ff_adaptive_gain_control(buf_out, buf_out, speech_gain, AMR_SUBFRAME_SIZE,
1278 AMR_AGC_ALPHA, &p->postfilter_agc);
1279}
1280
1281/// @}
1282
1283/**
1284 * Comfort noise (DTX) generation, see 3GPP TS 26.092.
1285 *
1286 * As for AMR-WB the implementation follows the decoder of the reference
1287 * codec (3GPP TS 26.073) with the fixed point arithmetic replaced by
1288 * floating point: LSF vectors are kept in the same unit as in the speech
1289 * path (normalized frequency) and energies as log2 values.
1290 */
1291
1292
1293/**
1294 * Draw a value from the pseudo noise generator, see 3GPP TS 26.073.
1295 *
1296 * @param[in,out] shift_reg State of the shift register
1297 * @param[in] no_bits Number of bits to draw
1298 */
1299static int pseudonoise(uint32_t *shift_reg, int no_bits)
1300{
1301 int noise_bits = 0, i;
1302
1303 for (i = 0; i < no_bits; i++) {
1304 int Sn = (*shift_reg & 1) ^ ((*shift_reg >> 28) & 1);
1305
1306 noise_bits = (noise_bits << 1) | (*shift_reg & 1);
1307 *shift_reg >>= 1;
1308 if (Sn)
1309 *shift_reg |= 0x40000000u;
1310 }
1311
1312 return noise_bits;
1313}
1314
1315/**
1316 * Build the comfort noise excitation, a vector of sparse pulses.
1317 *
1318 * @param[in,out] seed State of the pseudo noise generator
1319 * @param[out] cod Excitation for one subframe
1320 */
1321static void build_cn_code(uint32_t *seed, float *cod)
1322{
1323 int i, k;
1324
1325 memset(cod, 0, AMR_SUBFRAME_SIZE * sizeof(*cod));
1326
1327 for (k = 0; k < CN_NB_PULSE; k++) {
1328 i = pseudonoise(seed, 2) * 10 + k;
1329 cod[i] = pseudonoise(seed, 1) > 0 ? 1.0f : -1.0f;
1330 }
1331}
1332
1333/**
1334 * Decode the comfort noise parameters of a SID frame.
1335 *
1336 * The payload carries 39 bits: five parameters (3, 8, 9, 9 and 6 bits),
1337 * a flag telling whether this is the first SID frame of a silent period
1338 * and the speech mode used before the silence.
1339 *
1340 * @param[out] ind The five SID parameters
1341 * @param[out] mode Speech mode used before the silence
1342 *
1343 * @return non zero if this is a SID update frame
1344 */
1345static int decode_sid_frame(const uint8_t *buf, int *ind, int *mode)
1346{
1347 GetBitContext gb;
1348 int sid_type;
1349
1350 init_get_bits8(&gb, buf, 5);
1351
1352 ind[0] = get_bits(&gb, 3);
1353 ind[1] = get_bits(&gb, 8);
1354 ind[2] = get_bits(&gb, 9);
1355 ind[3] = get_bits(&gb, 9);
1356 ind[4] = get_bits(&gb, 6);
1357 sid_type = get_bits1(&gb);
1358
1359 /* the three bits of the speech mode indication are interleaved */
1360 *mode = get_bits(&gb, 3);
1361 *mode = ((*mode & 4) >> 2) | (*mode & 2) | ((*mode & 1) << 2);
1362
1363 return sid_type;
1364}
1365
1366/**
1367 * Decode the LSF vector of a SID frame, the comfort noise quantizer does
1368 * not use the prediction of the speech modes.
1369 *
1370 * @param[in] ind The five SID parameters
1371 * @param[out] lsf_q Decoded LSF vector
1372 */
1373static void decode_lsf_noise(const int *ind, float *lsf_q)
1374{
1375 int i;
1376
1377 for (i = 0; i < 3; i++)
1378 lsf_q[i] = lsf_3_1[ind[1]][i] * (LSF_R_FAC / 8000.0);
1379
1380 for (i = 0; i < 3; i++)
1381 lsf_q[i + 3] = lsf_3_2[ind[2]][i] * (LSF_R_FAC / 8000.0);
1382
1383 for (i = 0; i < 4; i++)
1384 lsf_q[i + 6] = lsf_3_3[ind[3]][i] * (LSF_R_FAC / 8000.0);
1385
1386 /* the first parameter selects the initial prediction residual */
1387 for (i = 0; i < LP_FILTER_ORDER; i++) {
1388 lsf_q[i] += past_rq_init[ind[0]][i] * (LSF_R_FAC / 8000.0);
1389 lsf_q[i] += lsf_3_mean[i] * (1.0 / 8000.0);
1390 }
1391
1393}
1394
1395/**
1396 * Store the LSF vector and the energy of a speech frame so that the first
1397 * SID frame of a silent period can reuse them.
1398 *
1399 * @param[in,out] p The Context
1400 * @param[in] synth Synthesis of the decoded frame
1401 */
1402static void dtx_activity_update(AMRContext *p, const float *synth)
1403{
1404 float energy = 0.0f;
1405 int i;
1406
1407 p->lsf_hist_ptr = (p->lsf_hist_ptr + 1) % DTX_HIST_SIZE;
1408 memcpy(p->lsf_hist[p->lsf_hist_ptr], p->lsf_q[3], sizeof(p->lsf_q[3]));
1409
1410 p->log_en_hist_ptr = (p->log_en_hist_ptr + 1) % DTX_HIST_SIZE;
1411
1412 for (i = 0; i < AMR_BLOCK_SIZE; i++)
1413 energy += synth[i] * synth[i];
1414
1415 /* The reference accumulates the energy with L_mac(), which shifts every
1416 product left by one, so after dividing by 320 what is left is the
1417 logarithm of the mean square of the frame. The history keeps one more
1418 fractional bit than the energy carried by a SID frame. */
1419 p->log_en_hist[p->log_en_hist_ptr] =
1420 0.5f * log2f(FFMAX(energy, 1.0f) / AMR_BLOCK_SIZE);
1421}
1422
1423/**
1424 * Compute the reflection coefficients of an LP filter with the backward
1425 * Levinson recursion, they give the prediction gain of the filter.
1426 *
1427 * @param[in] lpc LP coefficients of the filter
1428 * @param[out] refl Reflection coefficients
1429 */
1430static void lpc2refl(const float *lpc, float *refl)
1431{
1432 float a[LP_FILTER_ORDER + 1], b[LP_FILTER_ORDER + 1];
1433 int i, m;
1434
1435 for (i = 1; i <= LP_FILTER_ORDER; i++)
1436 a[i] = lpc[i - 1];
1437
1438 for (m = LP_FILTER_ORDER; m >= 1; m--) {
1439 float k = a[m];
1440
1441 refl[m - 1] = k;
1442 for (i = 1; i < m; i++)
1443 b[i] = (a[i] - k * a[m - i]) / (1.0f - k * k);
1444 for (i = 1; i < m; i++)
1445 a[i] = b[i];
1446 }
1447}
1448
1449/**
1450 * Update the comfort noise state machine for a received frame.
1451 *
1452 * @param[in,out] p The Context
1453 * @param[in] frame_type Type of the received frame
1454 *
1455 * @return the DTX state to decode this frame with
1456 */
1458{
1459 int new_state, enc_state;
1460
1463 ((p->dtx_state == DTX || p->dtx_state == DTX_MUTE) &&
1465 frame_type == RX_ONSET))) {
1466 new_state = DTX;
1467
1468 /* stay muted for these input types */
1469 if (p->dtx_state == DTX_MUTE &&
1472 new_state = DTX_MUTE;
1473
1474 p->since_last_sid = FFMIN(p->since_last_sid + 1, 32767);
1475
1476 /* SID updates are handled separately to avoid muting a late one */
1477 if (frame_type != RX_SID_UPDATE &&
1478 p->since_last_sid > DTX_MAX_EMPTY_THRESH)
1479 new_state = DTX_MUTE;
1480 } else {
1481 new_state = DTX_SPEECH;
1482 p->since_last_sid = 0;
1483 }
1484
1485 if (!p->data_updated && frame_type == RX_SID_UPDATE)
1486 p->dec_ana_elapsed = 0;
1487
1488 p->dec_ana_elapsed = FFMIN(p->dec_ana_elapsed + 1, 32767);
1489 p->hangover_added = 0;
1490
1494 enc_state = DTX;
1495 else
1496 enc_state = DTX_SPEECH;
1497
1498 /* dtx_dec.c: a RX_NO_DATA frame may be a speech packet that was lost on
1499 the way, so the assumed encoder state is SPEECH in that case. */
1500 if (frame_type == RX_NO_DATA && new_state == DTX_SPEECH)
1501 enc_state = DTX_SPEECH;
1502
1503 if (enc_state == DTX_SPEECH) {
1504 p->hangover_count = DTX_HANG_CONST;
1505 } else if (p->dec_ana_elapsed > DTX_ELAPSED_FRAMES_THRESH) {
1506 p->hangover_added = 1;
1507 p->dec_ana_elapsed = 0;
1508 p->hangover_count = 0;
1509 } else if (!p->hangover_count) {
1510 p->dec_ana_elapsed = 0;
1511 } else {
1512 p->hangover_count--;
1513 }
1514
1515 if (new_state != DTX_SPEECH) {
1516 p->sid_frame = 0;
1517 p->valid_data = 0;
1518
1519 if (frame_type == RX_SID_FIRST) {
1520 p->sid_frame = 1;
1521 } else if (frame_type == RX_SID_UPDATE) {
1522 p->sid_frame = 1;
1523 p->valid_data = 1;
1524 } else if (frame_type == RX_SID_BAD) {
1525 p->sid_frame = 1;
1526 p->hangover_added = 0;
1527 }
1528 }
1529
1530 return new_state;
1531}
1532
1533/**
1534 * Compute the logarithm of the prediction gain of an LP filter, it is
1535 * used to normalize the level of the comfort noise.
1536 *
1537 * @param[in] lpc LP coefficients of the filter
1538 *
1539 * @return log2 of the amplitude gained by filtering, negated
1540 */
1541static float pred_gain_log(const float *lpc)
1542{
1543 float refl[LP_FILTER_ORDER], pred_err = 1.0f;
1544 int i;
1545
1546 lpc2refl(lpc, refl);
1547 for (i = 0; i < LP_FILTER_ORDER; i++)
1548 pred_err *= 1.0f - refl[i] * refl[i];
1549
1550 /* the prediction gain is applied to the amplitude of the excitation,
1551 so only half of it is subtracted from the logarithm of the level */
1552 return -0.5f * log2f(FFMAX(pred_err, 1e-6f));
1553}
1554
1555/**
1556 * Reuse the noise parameters of the speech frames preceding a silent
1557 * period, they are the average of the LSF and energy history.
1558 *
1559 * @param[in,out] p The Context
1560 * @param[in] mode Speech mode used before the silence
1561 */
1563{
1564 float lsf[LP_FILTER_ORDER];
1565 double lsp[LP_FILTER_ORDER];
1566 int i, j, ptr;
1567
1568 ptr = (p->lsf_hist_ptr + 1) % DTX_HIST_SIZE;
1569 memcpy(p->lsf_hist[ptr], p->lsf_hist[p->lsf_hist_ptr],
1570 sizeof(p->lsf_hist[0]));
1571 ptr = (p->log_en_hist_ptr + 1) % DTX_HIST_SIZE;
1572 p->log_en_hist[ptr] = p->log_en_hist[p->log_en_hist_ptr];
1573
1574 p->log_en_adjust = dtx_log_en_adjust[mode];
1575
1576 memset(lsf, 0, sizeof(lsf));
1577 p->cn_log_en = 0.0f;
1578 for (i = 0; i < DTX_HIST_SIZE; i++) {
1579 p->cn_log_en += p->log_en_hist[i];
1580 for (j = 0; j < LP_FILTER_ORDER; j++)
1581 lsf[j] += p->lsf_hist[i][j];
1582 }
1583 p->cn_log_en /= DTX_HIST_SIZE;
1584 for (j = 0; j < LP_FILTER_ORDER; j++)
1585 lsf[j] /= DTX_HIST_SIZE;
1586
1587 /* make the energy independent of the speech coder mode */
1588 p->cn_log_en -= p->log_en_adjust;
1589
1590 /* store the deviation from the mean of the LSF history, it is
1591 used to make the comfort noise spectrum vary like the one of
1592 the background noise */
1593 for (j = 0; j < LP_FILTER_ORDER; j++) {
1594 float mean = 0.0f;
1595
1596 for (i = 0; i < DTX_HIST_SIZE; i++)
1597 mean += p->lsf_hist[i][j];
1599
1600 for (i = 0; i < DTX_HIST_SIZE; i++) {
1601 float dev = (p->lsf_hist[i][j] - mean) *
1603 float sign = dev < 0.0f ? -1.0f : 1.0f;
1604
1605 dev = fabsf(dev);
1606 /* soft and hard limits on the deviation */
1607 if (dev > 655.0f / 32768.0f)
1608 dev = 655.0f / 32768.0f +
1609 (dev - 655.0f / 32768.0f) * 0.25f;
1610 dev = FFMIN(dev, 1310.0f / 32768.0f);
1611 p->lsf_hist_mean[i][j] = dev * sign;
1612 }
1613 }
1614
1617 for (i = 0; i < LP_FILTER_ORDER; i++)
1618 p->cn_lsp[i] = lsp[i];
1619}
1620
1621/**
1622 * Update the comfort noise parameters with the ones carried by a SID
1623 * frame and reset the state the speech decoder starts from.
1624 *
1625 * @param[in,out] p The Context
1626 * @param[in] ind SID parameters of this frame
1627 */
1628static void cn_update_from_sid(AMRContext *p, const int *ind)
1629{
1630 float lsf[LP_FILTER_ORDER];
1631 double lsp[LP_FILTER_ORDER];
1632 float pred_init;
1633 int i, period;
1634
1635 memcpy(p->cn_lsp_old, p->cn_lsp, sizeof(p->cn_lsp));
1636 p->cn_log_en_old = p->cn_log_en;
1637
1638 if (p->valid_data) {
1639 period = p->since_last_sid;
1640 p->since_last_sid = 0;
1641 period = FFMIN(period, 32);
1642 p->sid_period_inv = period >= 2 ? 1.0f / period : 0.5f;
1643
1644 decode_lsf_noise(ind, lsf);
1646 for (i = 0; i < LP_FILTER_ORDER; i++)
1647 p->cn_lsp[i] = lsp[i];
1648
1649 /* index 0 is reserved for silence */
1650 p->cn_log_en = ind[4] ? (ind[4] / 4.0f) - 2.5f : -16.0f;
1651
1652 if (!p->data_updated || p->dtx_state == DTX_SPEECH) {
1653 memcpy(p->cn_lsp_old, p->cn_lsp, sizeof(p->cn_lsp));
1654 p->cn_log_en_old = p->cn_log_en;
1655 }
1656 }
1657
1658 /* Initialize the gain predictor using this SID frame's energy.
1659
1660 Note: this decoder's log2-vs-dB gain-predictor modelling (pred_init)
1661 differs from the reference's fixed-point ma_pred_init (dtx_dec.c:446)
1662 on the first subframe after DTX; ma_pred_init corresponds to TS 26.092
1663 clause 6.2's "predictor memories are initialized". */
1664 pred_init = FFMIN(p->cn_log_en - 9000.0f / 1024.0f, 0.0f);
1665 pred_init = FFMAX(pred_init, MIN_ENERGY);
1666 for (i = 0; i < 4; i++)
1667 p->prediction_error[i] = pred_init;
1668}
1669
1670/**
1671 * Synthesize one frame of comfort noise into the output buffer.
1672 *
1673 * @param[in,out] p The Context
1674 * @param[in] lpc_variab LP coefficients of the dithered filter
1675 * @param[in] level Amplitude of the excitation
1676 * @param[in] new_state DTX state of this frame
1677 * @param[out] buf_out Synthesis of the frame, before filtering
1678 */
1679static void cn_synthesize(AMRContext *p, const float *lpc_variab, float level,
1680 int new_state, float *buf_out)
1681{
1682 float ex[AMR_SUBFRAME_SIZE];
1683 float synth_mem[LP_FILTER_ORDER + AMR_BLOCK_SIZE];
1684 int i, sub;
1685
1686 /* The synthesis filter needs the previous samples in front of the
1687 output. The reference decoder resets its state with the MRDTX mode,
1688 which keeps the synthesis memory, so the comfort noise of a silent
1689 period starts from the memory of the speech frames before it. */
1690 memcpy(synth_mem, p->samples_in, LP_FILTER_ORDER * sizeof(*synth_mem));
1691
1692 /* 3GPP TS 26.092 clause 6.2: while comfort noise is generated the
1693 adaptive codebook gain is zero and the memory of the adaptive codebook
1694 is set to zero, so that the speech decoder starts from a known state
1695 when the speech activity begins again. The predictor memory of the LSF
1696 quantizer is reset the same way. */
1697 memset(p->excitation_buf, 0, (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) *
1698 sizeof(*p->excitation_buf));
1699 memset(p->prev_lsf_r, 0, sizeof(p->prev_lsf_r));
1700
1701 /* 3GPP TS 26.073 Decoder_amr_reset() sets the pitch sharpening factor to
1702 SHARPMIN and the pitch lag to 40 whenever a silent period is entered
1703 (the reference resets these unconditionally, including for MRDTX). */
1704 p->beta = 0.0f;
1705 p->pitch_lag_int = 40;
1706
1707 /* The reference decoder resets the bad frame handling and error
1708 concealment states with every frame of a silent period
1709 (Decoder_amr_reset() and ec_gain_*_reset() in 3GPP TS 26.073), so the
1710 gains of the speech frame starting the next speech burst are limited
1711 accordingly (3GPP TS 26.091 clause 7.2.2). It also marks the frame as
1712 unusable when the comfort noise had been muted. */
1713 ec_reset(p);
1714 p->prev_bf = new_state == DTX_MUTE;
1715
1716 /* Decoder_amr_reset(MRDTX) clears the fixed-gain history and the
1717 phase-dispersion state. dtx_dec() then restarts smoothing with
1718 hangVar already past the 10-subframe threshold and hangCount at 0,
1719 so resumed speech does not blend in the gains from before the pause. */
1720 memset(p->fixed_gain, 0, sizeof(p->fixed_gain));
1721 memset(p->pitch_gain, 0, sizeof(p->pitch_gain));
1722 p->diff_count = 10;
1723 p->hang_count = 0;
1724 p->prev_ir_filter_nr = 0;
1725 p->ir_filter_onset = 0;
1726 p->prev_sparse_fixed_gain = 0.0f;
1727
1728 for (sub = 0; sub < 4; sub++) {
1729 build_cn_code(&p->cn_seed, ex);
1730 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
1731 ex[i] *= level;
1732
1733 p->celpf_ctx.celp_lp_synthesis_filterf(
1734 &synth_mem[LP_FILTER_ORDER + sub * AMR_SUBFRAME_SIZE], lpc_variab, ex,
1736 }
1737
1738 memcpy(buf_out, &synth_mem[LP_FILTER_ORDER], AMR_BLOCK_SIZE * sizeof(*buf_out));
1739
1740 /* keep the synthesis memory so that the next speech frame starts from it */
1741 memcpy(p->samples_in, &synth_mem[AMR_BLOCK_SIZE], LP_FILTER_ORDER * sizeof(*p->samples_in));
1742}
1743
1744/**
1745 * Decode one frame of comfort noise into the output buffer.
1746 *
1747 * @param[in,out] p The Context
1748 * @param[in] new_state DTX state of this frame
1749 * @param[in] ind SID parameters of this frame
1750 * @param[in] mode Speech mode used before the silence
1751 * @param[out] buf_out Synthesis of the frame, before filtering
1752 */
1753static void cn_frame(AMRContext *p, int new_state, const int *ind, int mode,
1754 float *buf_out)
1755{
1756 float lsf[LP_FILTER_ORDER], lsf_variab[LP_FILTER_ORDER];
1757 double lsp[LP_FILTER_ORDER], lsp_variab[LP_FILTER_ORDER];
1758 float lpc[LP_FILTER_ORDER], lpc_variab[LP_FILTER_ORDER];
1759 float variab_fac, log_en, int_fac, level, log_pg;
1760 int i, sub, period, variab_index;
1761
1762 /* A SID frame starting a silent period reuses the noise parameters
1763 of the speech frames preceding it */
1764 if (p->hangover_added && p->sid_frame)
1766
1767 /* A SID frame carries new comfort noise parameters */
1768 if (p->sid_frame)
1769 cn_update_from_sid(p, ind);
1770
1771 /* The level adjustment of the speech mode is low pass filtered so that
1772 it does not jump when the mode changes */
1773 p->log_en_adjust = 0.9f * p->log_en_adjust +
1774 0.1f * dtx_log_en_adjust[mode];
1775
1776 /* Interpolate the comfort noise parameters since the last update */
1777 int_fac = FFMIN((1 + p->since_last_sid) * p->sid_period_inv, 1.0f);
1778
1779 log_en = int_fac * p->cn_log_en + (1.0f - int_fac) * p->cn_log_en_old;
1780 for (i = 0; i < LP_FILTER_ORDER; i++)
1781 lsp[i] = int_fac * p->cn_lsp[i] + (1.0f - int_fac) * p->cn_lsp_old[i];
1782
1783 /* convert to LSF to apply the variability of the background noise */
1784 for (i = 0; i < LP_FILTER_ORDER; i++)
1785 lsf[i] = acosf(av_clipd(lsp[i], -1.0, 1.0)) / (2.0f * M_PI);
1786
1787 variab_fac = 1.0f - (p->log_pg_mean - 0.6f) * 0.3f;
1788 variab_fac = av_clipf(variab_fac, 0.0f, 1.0f);
1789 variab_index = pseudonoise(&p->cn_seed, 3);
1790 variab_index = FFMIN(variab_index, DTX_HIST_SIZE - 1);
1791
1792 memcpy(lsf_variab, lsf, sizeof(lsf_variab));
1793 for (i = 0; i < LP_FILTER_ORDER; i++)
1794 lsf_variab[i] += variab_fac * p->lsf_hist_mean[variab_index][i];
1795
1798
1800 ff_acelp_lsf2lspd(lsp_variab, lsf_variab, LP_FILTER_ORDER);
1801
1802 /* TS 26.073 publishes the comfort noise LSF to the speech decoder on
1803 every comfort noise frame (dtx_dec.c:548) and converts it there so
1804 that the first speech frame after the silent period interpolates from
1805 it (dec_amr.c:329-330). */
1806 memcpy(p->lsf_q[3], lsf, sizeof(p->lsf_q[3]));
1807 memcpy(p->prev_lsp_sub4, lsp, sizeof(p->prev_lsp_sub4));
1808 p->acelpv_ctx.weighted_vector_sumf(p->lsf_avg, p->lsf_avg, lsf,
1809 0.84, 0.16, LP_FILTER_ORDER);
1810
1811 ff_acelp_lspd2lpc(lsp, lpc, LP_FILTER_ORDER / 2);
1812 ff_acelp_lspd2lpc(lsp_variab, lpc_variab, LP_FILTER_ORDER / 2);
1813
1814 /* the filter without variability is used to normalize the level and
1815 by the postfilter */
1816 for (sub = 0; sub < 4; sub++)
1817 memcpy(p->lpc[sub], lpc, sizeof(*lpc) * LP_FILTER_ORDER);
1818
1819 /* level normalization with the prediction gain of the filter, the
1820 reference decoder normalizes with the gain of this very frame, its
1821 average is only used for the variability of the spectrum */
1822 log_pg = pred_gain_log(lpc);
1823 p->log_pg_mean = 0.9f * p->log_pg_mean + 0.1f * log_pg;
1824
1825 level = exp2f(log_en + 4.0f - log_pg + p->log_en_adjust) *
1827
1828 cn_synthesize(p, lpc_variab, level, new_state, buf_out);
1829
1830 if (new_state == DTX_MUTE) {
1831 /* mute the comfort noise, nothing has been updated for a while */
1832 period = p->since_last_sid;
1833 if (period > 32)
1834 period = 32;
1835 if (period <= 0)
1836 period = 8;
1837 p->sid_period_inv = 1.0f / period;
1838 p->since_last_sid = 0;
1839 /* Snapshot the stored SID target, not this frame's interpolated
1840 vector, so the next period does not fall back toward the
1841 previous SID (dtx_dec.c, DTX_MUTE). */
1842 memcpy(p->cn_lsp_old, p->cn_lsp, sizeof(p->cn_lsp));
1843 p->cn_log_en_old = p->cn_log_en;
1844 p->cn_log_en -= 0.125f;
1845 }
1846
1847 if (p->sid_frame &&
1848 (p->valid_data || (!p->valid_data && p->hangover_added))) {
1849 p->since_last_sid = 0;
1850 p->data_updated = 1;
1851 }
1852
1853 p->dtx_state = new_state;
1854}
1855
1856/**
1857 * Fill the frame parameters of a lost speech frame with pseudo-random
1858 * values, see build_CN_param() in 3GPP TS 26.073 (b_cn_cod.c:145-162).
1859 * Only the fields the concealment does not itself overwrite are actually
1860 * consumed (notably the fixed codebook pulses). The field order follows
1861 * the AMRNBFrame order table, not the reference's bitstream parameter
1862 * order, so this is deliberately not bit-exact with the reference -- the
1863 * decoder is float and not bit-exact either (see the file header).
1864 *
1865 * @param p the context
1866 * @param seed state of the pseudo-random generator
1867 * @param mode speech mode of the lost frame
1868 */
1869static void build_bad_frame_params(AMRContext *p, uint32_t *seed, enum Mode mode)
1870{
1871 const uint8_t *ord = amr_unpacking_bitmaps_per_mode[mode];
1872 const int16_t *win;
1873
1874 /* reference LCG: seed = (seed * 31821 + 13849) mod 2^16 */
1875 *seed = (*seed * 31821U + 13849U) & 0xFFFFU;
1876 win = &window_200_40[*seed & 0x7F];
1877
1878 while (*ord) {
1879 int field_size = *ord++;
1880 int field = *ord++; /* byte offset into AMRNBFrame */
1881
1882 ((uint16_t *)&p->frame)[field >> 1] =
1883 *win++ & ((1 << field_size) - 1);
1884
1885 ord += field_size; /* skip the bit-position list */
1886 }
1887}
1888
1890 int *got_frame_ptr, AVPacket *avpkt)
1891{
1892
1893 AMRChannelsContext *s = avctx->priv_data; // pointer to private data
1894 const uint8_t *buf = avpkt->data;
1895 int buf_size = avpkt->size;
1896 int ret;
1897
1898 /* get output buffer */
1899 frame->nb_samples = AMR_BLOCK_SIZE;
1900 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1901 return ret;
1902
1903 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
1904 AMRContext *p = &s->ch[ch];
1905 float fixed_gain_factor;
1906 AMRFixed fixed_sparse = {0}; // fixed vector up to anti-sparseness processing
1907 float spare_vector[AMR_SUBFRAME_SIZE]; // extra stack space to hold result from anti-sparseness processing
1908 float synth_fixed_gain; // the fixed gain that synthesis should use
1909 const float *synth_fixed_vector; // pointer to the fixed vector that synthesis should use
1910 float *buf_out = (float *)frame->extended_data[ch];
1911 /* Frames carrying no data only consist of the table of contents, so
1912 a packet may hold no more than one byte per channel. */
1913 if (buf_size < 1)
1914 return AVERROR_INVALIDDATA;
1915
1916 int mode = buf[0] >> 3 & 0x0F;
1917 int channel_size, rx_type, new_state, bfi;
1918 int prev_dtx_state = p->dtx_state; // DTX state before this frame
1919 int sid_ind[5] = { 0 };
1920 int sid_mode = 0;
1921 float synth[AMR_BLOCK_SIZE]; // synthesis before post-filtering
1922 int i, subframe;
1923
1924 /* Frames carrying no data only consist of the table of contents, and
1925 so do the frame types between the SID one and NO_DATA, which are
1926 reserved: the reference decoder treats them like NO_DATA. */
1927 if (mode == NO_DATA || (mode > MODE_DTX && mode < NO_DATA))
1928 channel_size = 1;
1929 else
1930 channel_size = frame_sizes_nb[mode] + 1; // +7 for rounding and +8 for TOC
1931
1932 if (channel_size > buf_size) {
1933 av_log(avctx, AV_LOG_ERROR, "Frame too small (%d bytes)\n",
1934 buf_size);
1935 return AVERROR_INVALIDDATA;
1936 }
1937
1938 p->bad_frame_indicator = (buf[0] & 0x4) != 0x4; // quality bit
1939
1940 /* Classify the received frame for the comfort noise state machine */
1941 if (mode == NO_DATA || (mode > MODE_DTX && mode < NO_DATA)) {
1942 rx_type = RX_NO_DATA;
1943 } else if (mode == MODE_DTX) {
1944 int sid_update = decode_sid_frame(buf + 1, sid_ind, &sid_mode);
1945
1946 /* the comfort noise is scaled with the speech mode of the SID
1947 frame, it is also needed by the frames carrying no data */
1948 p->cur_frame_mode = sid_mode;
1949
1950 if (p->bad_frame_indicator)
1951 rx_type = RX_SID_BAD;
1952 else if (sid_update)
1953 rx_type = RX_SID_UPDATE;
1954 else
1955 rx_type = RX_SID_FIRST;
1956 } else if (p->bad_frame_indicator) {
1957 rx_type = RX_SPEECH_BAD;
1958 } else {
1959 rx_type = RX_SPEECH_GOOD;
1960 }
1961
1962 if (rx_type == RX_SPEECH_GOOD || rx_type == RX_SPEECH_BAD) {
1963 p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
1964 if (p->cur_frame_mode == NO_DATA) {
1965 av_log(avctx, AV_LOG_ERROR, "Corrupt bitstream\n");
1966 return AVERROR_INVALIDDATA;
1967 }
1968 }
1969
1970 new_state = dtx_handler(p, rx_type);
1971
1972 if (new_state != DTX_SPEECH) {
1973 /* Comfort noise is generated for SID frames and, once a silent
1974 period has started, for the frames carrying no data at all.
1975 Speech_Decode_Frame() then post-filters that synthesis with
1976 the same filter as speech. samples_in keeps the unfiltered
1977 synthesis memory written by cn_synthesize(). */
1978 cn_frame(p, new_state, sid_ind, p->cur_frame_mode, synth);
1979 for (subframe = 0; subframe < 4; subframe++) {
1980 memcpy(p->samples_in + LP_FILTER_ORDER,
1981 synth + subframe * AMR_SUBFRAME_SIZE,
1982 AMR_SUBFRAME_SIZE * sizeof(float));
1983 postfilter(p, p->lpc[subframe],
1984 buf_out + subframe * AMR_SUBFRAME_SIZE);
1985 }
1986
1987 p->acelpf_ctx.acelp_apply_order_2_transfer_function(
1988 buf_out, buf_out, highpass_zeros, highpass_poles,
1989 highpass_gain * AMR_SAMPLE_SCALE, p->high_pass_mem,
1991
1992 buf += channel_size;
1993 buf_size -= channel_size;
1994 continue;
1995 }
1996
1997 /* A speech frame that is either flagged as bad or carries no data
1998 at all cannot be decoded: its parameters are concealed instead
1999 (3GPP TS 26.091 clause 7). */
2000 bfi = rx_type == RX_SPEECH_BAD || rx_type == RX_NO_DATA;
2001
2002 /* Bad frame handling state machine, 3GPP TS 26.073
2003 (dec_amr.c:357-398). The state saturates at 6 and, once it has
2004 reached 6, only decays to 5 on a good frame. */
2005 if (bfi)
2006 p->bfh_state = FFMIN(p->bfh_state + 1, 6);
2007 else
2008 p->bfh_state = p->bfh_state == 6 ? 5 : 0;
2009
2010 /* The first speech frame after a comfort noise period is seeded with
2011 the DTX state of the frame before it: prev_dtx_state was saved
2012 above, before this frame could overwrite dtx_state. */
2013 if (prev_dtx_state == DTX) {
2014 p->bfh_state = 5;
2015 p->prev_bf = 0;
2016 } else if (prev_dtx_state == DTX_MUTE) {
2017 p->bfh_state = 5;
2018 p->prev_bf = 1;
2019 }
2020
2021 /* A frame carrying no data has no parameters of its own, fill them
2022 with pseudo-random values (3GPP TS 26.073 build_CN_param()). */
2023 if (rx_type == RX_NO_DATA)
2024 build_bad_frame_params(p, &p->no_data_seed, p->cur_frame_mode);
2025
2026 p->dtx_state = new_state;
2027
2028 if (p->cur_frame_mode == MODE_12k2) {
2029 lsf2lsp_5(p, bfi);
2030 } else
2031 lsf2lsp_3(p, bfi);
2032
2033 for (i = 0; i < 4; i++)
2034 ff_acelp_lspd2lpc(p->lsp[i], p->lpc[i], 5);
2035
2036 for (subframe = 0; subframe < 4; subframe++) {
2037 const AMRNBSubframe *amr_subframe = &p->frame.subframe[subframe];
2038
2039 decode_pitch_vector(p, amr_subframe, subframe, bfi);
2040
2041 decode_fixed_sparse(&fixed_sparse, amr_subframe->pulses,
2042 p->cur_frame_mode, subframe);
2043
2044 // The fixed gain (section 6.1.3) depends on the fixed vector
2045 // (section 6.1.2), but the fixed vector calculation uses
2046 // pitch sharpening based on the on the pitch gain (section 6.1.3).
2047 // So the correct order is: pitch gain, pitch sharpening, fixed gain.
2048 if (bfi)
2049 ec_gain_pitch(p, p->bfh_state);
2050 else
2051 decode_gains(p, amr_subframe, p->cur_frame_mode, subframe,
2052 &fixed_gain_factor);
2053
2054 /* Keep the concealment history and limit the gain of the first
2055 usable subframe after an unusable one (3GPP TS 26.073
2056 ec_gain_pitch_update(), 3GPP TS 26.091 clause 7.2.2). */
2057 ec_gain_pitch_update(p, bfi);
2058
2059 pitch_sharpening(p, subframe, p->cur_frame_mode, &fixed_sparse);
2060
2061 if (fixed_sparse.pitch_lag == 0) {
2062 av_log(avctx, AV_LOG_ERROR, "The file is corrupted, pitch_lag = 0 is not allowed\n");
2063 return AVERROR_INVALIDDATA;
2064 }
2065 ff_set_fixed_vector(p->fixed_vector, &fixed_sparse, 1.0,
2067
2068 if (bfi) {
2069 ec_gain_code(p, p->bfh_state);
2071 } else {
2072 p->fixed_gain[4] =
2073 ff_amr_set_fixed_gain(fixed_gain_factor,
2074 p->celpm_ctx.dot_productf(p->fixed_vector,
2075 p->fixed_vector,
2078 p->prediction_error,
2079 energy_mean[p->cur_frame_mode], energy_pred_fac);
2080 }
2081 ec_gain_code_update(p, bfi);
2082
2083 // The excitation feedback is calculated without any processing such
2084 // as fixed gain smoothing. This isn't mentioned in the specification.
2085 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
2086 p->excitation[i] *= p->pitch_gain[4];
2087 ff_set_fixed_vector(p->excitation, &fixed_sparse, p->fixed_gain[4],
2089
2090 // In the ref decoder, excitation is stored with no fractional bits.
2091 // This step prevents buzz in silent periods. The ref encoder can
2092 // emit long sequences with pitch factor greater than one. This
2093 // creates unwanted feedback if the excitation vector is nonzero.
2094 // (e.g. test sequence T19_795.COD in 3GPP TS 26.074)
2095 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
2096 p->excitation[i] = truncf(p->excitation[i]);
2097
2098 // Smooth fixed gain.
2099 // The specification is ambiguous, but in the reference source, the
2100 // smoothed value is NOT fed back into later fixed gain smoothing.
2101 synth_fixed_gain = fixed_gain_smooth(p, p->lsf_q[subframe],
2102 p->lsf_avg, p->cur_frame_mode);
2103
2104 synth_fixed_vector = anti_sparseness(p, &fixed_sparse, p->fixed_vector,
2105 synth_fixed_gain, spare_vector);
2106
2107 if (synthesis(p, p->lpc[subframe], synth_fixed_gain,
2108 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 0))
2109 // overflow detected -> rerun synthesis scaling pitch vector down
2110 // by a factor of 4, skipping pitch vector contribution emphasis
2111 // and adaptive gain control
2112 synthesis(p, p->lpc[subframe], synth_fixed_gain,
2113 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 1);
2114
2115 memcpy(synth + subframe * AMR_SUBFRAME_SIZE,
2116 &p->samples_in[LP_FILTER_ORDER],
2117 AMR_SUBFRAME_SIZE * sizeof(*synth));
2118
2119 postfilter(p, p->lpc[subframe], buf_out + subframe * AMR_SUBFRAME_SIZE);
2120
2121 // update buffers and history
2122 ff_clear_fixed_vector(p->fixed_vector, &fixed_sparse, AMR_SUBFRAME_SIZE);
2123 update_state(p);
2124 }
2125
2126 p->acelpf_ctx.acelp_apply_order_2_transfer_function(buf_out,
2127 buf_out, highpass_zeros,
2130 p->high_pass_mem, AMR_BLOCK_SIZE);
2131
2132 p->prev_bf = bfi;
2133
2134 /* keep the noise parameters of this speech frame for the next SID */
2135 dtx_activity_update(p, synth);
2136
2137 /* Update averaged lsf vector (used for fixed gain smoothing).
2138 *
2139 * Note that lsf_avg should not incorporate the current frame's LSFs
2140 * for fixed_gain_smooth.
2141 * The specification has an incorrect formula: the reference decoder uses
2142 * qbar(n-1) rather than qbar(n) in section 6.1(4) equation 71. */
2143 p->acelpv_ctx.weighted_vector_sumf(p->lsf_avg, p->lsf_avg, p->lsf_q[3],
2144 0.84, 0.16, LP_FILTER_ORDER);
2145 buf += channel_size;
2146 buf_size -= channel_size;
2147 }
2148
2149 *got_frame_ptr = 1;
2150
2151 return buf - avpkt->data;
2152}
2153
2154
2156 .p.name = "amrnb",
2157 CODEC_LONG_NAME("AMR-NB (Adaptive Multi-Rate NarrowBand)"),
2158 .p.type = AVMEDIA_TYPE_AUDIO,
2159 .p.id = AV_CODEC_ID_AMR_NB,
2160 .priv_data_size = sizeof(AMRChannelsContext),
2163 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
2164};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t * hf
Definition dsp.h:272
void ff_tilt_compensation(float *mem, float tilt, float *samples, int size)
Apply tilt compensation filter, 1 - tilt * z-1.
av_cold void ff_acelp_filter_init(ACELPFContext *c)
Initialize ACELPFContext.
float ff_amr_set_fixed_gain(float fixed_gain_factor, float fixed_mean_energy, float *prediction_error, float energy_mean, const float *pred_table)
Calculate fixed gain (part of section 6.1.3 of AMR spec)
void ff_decode_pitch_lag(int *lag_int, int *lag_frac, int pitch_index, const int prev_lag_int, const int subframe, int third_as_first, int resolution)
Decode the adaptive codebook index to the integer and fractional parts of the pitch lag for one subfr...
#define PITCH_DELAY_MAX
const float ff_pow_0_7[10]
Table of pow(0.7,n)
av_cold void ff_acelp_vectors_init(ACELPVContext *c)
Initialize ACELPVContext.
void ff_decode_10_pulses_35bits(const int16_t *fixed_index, AMRFixed *fixed_sparse, const uint8_t *gray_decode, int half_pulse_count, int bits)
Decode the algebraic codebook index to pulse positions and signs and construct the algebraic codebook...
void ff_adaptive_gain_control(float *out, const float *in, float speech_energ, int size, float alpha, float *gain_mem)
Adaptive gain control (as used in AMR postfiltering)
void ff_scale_vector_to_given_sum_of_squares(float *out, const float *in, float sum_of_squares, const int n)
Set the sum of squares of a signal by scaling.
const float ff_b60_sinc[61]
b60 hamming windowed sinc function coefficients
const float ff_pow_0_55[10]
Table of pow(0.55,n)
const float ff_pow_0_75[10]
Table of pow(0.75,n)
void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size)
Add fixed vector to an array from a sparse representation.
void ff_clear_fixed_vector(float *out, const AMRFixed *in, int size)
Clear array values set by set_fixed_vector.
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_amrnb_decoder
Definition amrnbdec.c:2155
static void ff_amr_bit_reorder(uint16_t *out, int size, const uint8_t *data, const R_TABLE_TYPE *ord_table)
Fill the frame structure variables from bitstream by parsing the given reordering table that uses the...
Definition amr.h:51
AMR narrowband data and definitions.
static const float *const ir_filters_lookup[2]
Definition amrnbdata.h:1653
static const float dtx_log_en_adjust[9]
Speech mode dependent adjustment of the comfort noise level.
Definition amrnbdata.h:1700
#define DTX_MAX_EMPTY_THRESH
Definition amrnbdata.h:1669
static const int16_t window_200_40[240]
Definition amrnbdata.h:1709
static const float cn_lsp_init[LP_FILTER_ORDER]
Initial LSP vector of the comfort noise decoder.
Definition amrnbdata.h:1675
static const uint16_t gains_low[64][2]
gain table for 5.15 and 5.90 kbps modes
Definition amrnbdata.h:1605
static const uint16_t qua_gain_pit[16]
scalar quantized pitch gain table for 7.95 and 12.2 kbps modes
Definition amrnbdata.h:1439
static const uint8_t frame_sizes_nb[N_MODES]
number of bytes for each mode
Definition amrnbdata.h:357
static const float highpass_zeros[2]
Definition amrnbdata.h:1662
static const uint16_t qua_gain_code[32]
scalar quantized fixed gain table for 7.95 and 12.2 kbps modes
Definition amrnbdata.h:1445
#define DTX_HIST_SIZE
Definition amrnbdata.h:1668
static const float pred_fac[LP_FILTER_ORDER]
Prediction factor table for modes other than 12.2kbit/s.
Definition amrnbdata.h:1420
static const int16_t lsf_3_3_MODE_5k15[128][4]
Definition amrnbdata.h:413
static const int16_t lsp_avg_init[LP_FILTER_ORDER]
Mean lsp values.
Definition amrnbdata.h:404
#define CN_PN_SEED_INIT
Definition amrnbdata.h:1673
static const int16_t lsf_3_1_MODE_7k95[512][3]
Definition amrnbdata.h:459
Mode
Frame type (Table 1a in 3GPP TS 26.101)
Definition amrnbdata.h:39
@ MODE_5k15
5.15 kbit/s
Definition amrnbdata.h:41
@ MODE_12k2
12.2 kbit/s
Definition amrnbdata.h:47
@ MODE_DTX
silent frame
Definition amrnbdata.h:48
@ MODE_4k75
4.75 kbit/s
Definition amrnbdata.h:40
@ N_MODES
number of modes
Definition amrnbdata.h:49
@ MODE_10k2
10.2 kbit/s
Definition amrnbdata.h:46
@ MODE_7k4
7.40 kbit/s
Definition amrnbdata.h:44
@ MODE_7k95
7.95 kbit/s
Definition amrnbdata.h:45
@ MODE_6k7
6.70 kbit/s
Definition amrnbdata.h:43
@ NO_DATA
no transmission
Definition amrnbdata.h:50
@ MODE_5k9
5.90 kbit/s
Definition amrnbdata.h:42
static const uint8_t track_position[16]
track start positions for algebraic code book routines
Definition amrnbdata.h:1428
#define DTX_ELAPSED_FRAMES_THRESH
Definition amrnbdata.h:1671
static const float highpass_gain
Definition amrnbdata.h:1664
static const int16_t lsf_5_4[256][4]
Definition amrnbdata.h:1295
static const int8_t lsp_sub4_init[LP_FILTER_ORDER]
Values for the lsp vector from the 4th subframe of the previous subframe values.
Definition amrnbdata.h:395
static const int16_t lsf_5_5[64][4]
Definition amrnbdata.h:1384
static const uint8_t gray_decode[8]
3-bit Gray code to binary lookup table
Definition amrnbdata.h:1433
static const int16_t lsf_5_2[256][4]
Definition amrnbdata.h:1117
static const int16_t lsf_5_1[128][4]
Definition amrnbdata.h:1071
static const float *const ir_filters_lookup_MODE_7k95[2]
Definition amrnbdata.h:1656
static const float lsf_hist_mean_scale[LP_FILTER_ORDER]
Scale factors applied to the deviation from the mean LSF vector.
Definition amrnbdata.h:1695
static const int16_t lsf_3_1[256][3]
Definition amrnbdata.h:633
static const int16_t lsf_3_3[512][4]
Definition amrnbdata.h:897
static const float lsf_3_mean[LP_FILTER_ORDER]
Definition amrnbdata.h:1409
#define AMR_SUBFRAME_SIZE
samples per subframe
Definition amrnbdata.h:36
#define DTX_HANG_CONST
Definition amrnbdata.h:1670
static const uint16_t gains_MODE_4k75[512][2]
gain table for 4.75 kbps mode
Definition amrnbdata.h:1464
#define LP_FILTER_ORDER
linear predictive coding filter order
Definition amrnbdata.h:53
#define CN_NB_PULSE
Definition amrnbdata.h:1672
static const float energy_pred_fac[4]
4-tap moving average prediction coefficients in reverse order
Definition amrnbdata.h:1458
static const float lsf_5_mean[LP_FILTER_ORDER]
Definition amrnbdata.h:1414
static const int16_t lsf_5_3[256][4]
Definition amrnbdata.h:1206
static const float highpass_poles[2]
Definition amrnbdata.h:1663
static const int16_t past_rq_init[8][LP_FILTER_ORDER]
Initial prediction residual vectors of the comfort noise LSF decoder, indexed by the first SID parame...
Definition amrnbdata.h:1683
static const float energy_mean[8]
desired mean innovation energy, indexed by active mode
Definition amrnbdata.h:1453
static const uint16_t gains_high[128][2]
gain table for 6.70, 7.40 and 10.2 kbps modes
Definition amrnbdata.h:1573
static const uint8_t base_five_table[128][3]
Base-5 representation for values 0-124.
Definition amrnbdata.h:367
static const int16_t lsf_3_2[512][3]
Definition amrnbdata.h:723
static const uint8_t *const amr_unpacking_bitmaps_per_mode[N_MODES]
position of the bitmapping data for each packet type in the AMRNBFrame
Definition amrnbdata.h:345
#define CN_LEVEL_SCALE
Scaling of the comfort noise excitation level, the pulses of the fixed codebook of the reference have...
Definition amrnbdec.c:118
static int decode_sid_frame(const uint8_t *buf, int *ind, int *mode)
Decode the comfort noise parameters of a SID frame.
Definition amrnbdec.c:1345
static void ec_gain_pitch_update(AMRContext *p, int bfi)
Update the pitch gain concealment state, ec_gain_pitch_update() in 3GPP TS 26.073 (ec_gains....
Definition amrnbdec.c:905
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition amrnbdec.c:1239
#define LSF_ONE_ALPHA
Definition amrnbdec.c:93
static void decode_pitch_vector(AMRContext *p, const AMRNBSubframe *amr_subframe, const int subframe, const int bfi)
Definition amrnbdec.c:573
static void update_state(AMRContext *p)
Update buffers and history at the end of decoding a subframe.
Definition amrnbdec.c:1182
static const float * anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse, const float *fixed_vector, float fixed_gain, float *out)
Reduce fixed vector sparseness by smoothing with one of three IR filters.
Definition amrnbdec.c:1054
RXFrameType
Received frame types used by the comfort noise (DTX) state machine.
Definition amrnbdec.c:121
@ RX_SID_BAD
Definition amrnbdec.c:128
@ RX_SID_FIRST
Definition amrnbdec.c:126
@ RX_SPEECH_GOOD
Definition amrnbdec.c:122
@ RX_SID_UPDATE
Definition amrnbdec.c:127
@ RX_SPEECH_DEGRADED
Definition amrnbdec.c:123
@ RX_NO_DATA
Definition amrnbdec.c:129
@ RX_ONSET
Definition amrnbdec.c:124
@ RX_SPEECH_BAD
Definition amrnbdec.c:125
static void cn_synthesize(AMRContext *p, const float *lpc_variab, float level, int new_state, float *buf_out)
Synthesize one frame of comfort noise into the output buffer.
Definition amrnbdec.c:1679
static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
Interpolate the LSF vector (used for fixed gain smoothing).
Definition amrnbdec.c:362
static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER], const float lsf_no_r[LP_FILTER_ORDER], const int16_t *lsf_quantizer[5], const int quantizer_offset, const int sign, const int update)
Decode a set of 5 split-matrix quantized lsf indexes into an lsp vector.
Definition amrnbdec.c:383
#define LSF_ALPHA_5
Definition amrnbdec.c:94
#define AMR_AGC_ALPHA
Adaptive gain control factor used in post-filter.
Definition amrnbdec.c:112
static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index, const int prev_lag_int, const int subframe)
Like ff_decode_pitch_lag(), but with 1/6 resolution.
Definition amrnbdec.c:554
static void build_cn_code(uint32_t *seed, float *cod)
Build the comfort noise excitation, a vector of sparse pulses.
Definition amrnbdec.c:1321
static void decode_10bit_pulse(int code, int pulse_position[8], int i1, int i2, int i3)
Decode a 10-bit algebraic codebook index from a 10.2 kbit/s frame.
Definition amrnbdec.c:627
static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe, const enum Mode mode, const int subframe, float *fixed_gain_factor)
Decode pitch gain and fixed gain factor (part of section 6.1.3).
Definition amrnbdec.c:822
static void ec_gain_pitch(AMRContext *p, int state)
Conceal the pitch gain of a lost subframe, ec_gain_pitch() in 3GPP TS 26.073 (ec_gains....
Definition amrnbdec.c:883
static void ec_gain_code(AMRContext *p, int state)
Conceal the codebook gain of a lost subframe, ec_gain_code() in 3GPP TS 26.073 (ec_gains....
Definition amrnbdec.c:952
static void lpc2refl(const float *lpc, float *refl)
Compute the reflection coefficients of an LP filter with the backward Levinson recursion,...
Definition amrnbdec.c:1430
#define PITCH_LAG_MIN_MODE_12k2
Lower bound on decoded lag search in 12.2kbit/s mode.
Definition amrnbdec.c:82
static int dtx_handler(AMRContext *p, int frame_type)
Update the comfort noise state machine for a received frame.
Definition amrnbdec.c:1457
static void decode_lsf_noise(const int *ind, float *lsf_q)
Decode the LSF vector of a SID frame, the comfort noise quantizer does not use the prediction of the ...
Definition amrnbdec.c:1373
#define LSF_ONE_ALPHA_5
Definition amrnbdec.c:95
static void lsf2lsp_3(AMRContext *p, int bfi)
Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector.
Definition amrnbdec.c:481
static int synthesis(AMRContext *p, float *lpc, float fixed_gain, const float *fixed_vector, float *samples, uint8_t overflow)
Conduct 10th order linear predictive coding synthesis.
Definition amrnbdec.c:1125
static int amrnb_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition amrnbdec.c:1889
static float pred_gain_log(const float *lpc)
Compute the logarithm of the prediction gain of an LP filter, it is used to normalize the level of th...
Definition amrnbdec.c:1541
static void apply_ir_filter(float *out, const AMRFixed *in, const float *filter)
Circularly convolve a sparse fixed vector with a phase dispersion impulse response filter (D....
Definition amrnbdec.c:1007
static float tilt_factor(AMRContext *p, float *lpc_n, float *lpc_d)
Get the tilt factor of a formant filter from its transfer function.
Definition amrnbdec.c:1209
#define AMR_SAMPLE_BOUND
threshold for synthesis overflow
Definition amrnbdec.c:64
static void cn_reuse_speech_history(AMRContext *p, int mode)
Reuse the noise parameters of the speech frames preceding a silent period, they are the average of th...
Definition amrnbdec.c:1562
static void ec_gain_code_update(AMRContext *p, int bfi)
Update the codebook gain concealment state, ec_gain_code_update() in 3GPP TS 26.073 (ec_gains....
Definition amrnbdec.c:974
#define LSF_ALPHA
Weights of the past LSFs and of their mean when concealing a lost frame, ALPHA and ONE_ALPHA of 3GPP ...
Definition amrnbdec.c:92
#define PRED_FAC_MODE_12k2
Prediction factor for 12.2kbit/s mode.
Definition amrnbdec.c:78
static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses, const enum Mode mode, const int subframe)
Decode the algebraic codebook index to pulse positions and signs, then construct the algebraic codebo...
Definition amrnbdec.c:691
#define AMR_BLOCK_SIZE
samples per frame
Definition amrnbdec.c:63
#define AMR_TILT_GAMMA_T
Tilt factor = 1st reflection coefficient * gamma_t.
Definition amrnbdec.c:110
static void weighted_vector_sumd(double *out, const double *in_a, const double *in_b, double weight_coeff_a, double weight_coeff_b, int length)
Double version of ff_weighted_vector_sumf()
Definition amrnbdec.c:221
#define MIN_LSF_SPACING
Ensures stability of LPC filter.
Definition amrnbdec.c:81
static void cn_frame(AMRContext *p, int new_state, const int *ind, int mode, float *buf_out)
Decode one frame of comfort noise into the output buffer.
Definition amrnbdec.c:1753
static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf, int buf_size)
Unpack an RFC4867 speech frame into the AMR frame mode and parameters.
Definition amrnbdec.c:330
static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode, AMRFixed *fixed_sparse)
Apply pitch lag to obtain the sharpened fixed vector (section 6.1.2)
Definition amrnbdec.c:744
static float fixed_gain_smooth(AMRContext *p, const float *lsf, const float *lsf_avg, const enum Mode mode)
fixed gain smoothing Note that where the spec specifies the "spectrum in the q domain" in section 6....
Definition amrnbdec.c:780
static void build_bad_frame_params(AMRContext *p, uint32_t *seed, enum Mode mode)
Fill the frame parameters of a lost speech frame with pseudo-random values, see build_CN_param() in 3...
Definition amrnbdec.c:1869
static void cn_update_from_sid(AMRContext *p, const int *ind)
Update the comfort noise parameters with the ones carried by a SID frame and reset the state the spee...
Definition amrnbdec.c:1628
static float ec_gain_median(const float *buf)
Median of the last five gain values, equivalent to gmed_n() in 3GPP TS 26.073 (gmed_n....
Definition amrnbdec.c:858
static void ec_reset(AMRContext *p)
Reset the bad frame handling and gain concealment states, matching Decoder_amr_reset() and ec_gain_*_...
Definition amrnbdec.c:236
static void ec_gain_code_predictor_update(AMRContext *p)
Advance the gain predictor with the average of the four past energies, gc_pred_average_limited() foll...
Definition amrnbdec.c:931
#define AMR_SAMPLE_SCALE
Scale from constructed speech to [-1,1].
Definition amrnbdec.c:75
#define MIN_ENERGY
Initial energy in dB.
Definition amrnbdec.c:98
#define SHARP_MAX
Maximum sharpening factor.
Definition amrnbdec.c:105
DTXState
States of the comfort noise (DTX) state machine.
Definition amrnbdec.c:133
@ DTX_SPEECH
Definition amrnbdec.c:134
@ DTX
Definition amrnbdec.c:135
@ DTX_MUTE
Definition amrnbdec.c:136
static void decode_8_pulses_31bits(const int16_t *fixed_index, AMRFixed *fixed_sparse)
Decode the algebraic codebook index to pulse positions and signs and construct the algebraic codebook...
Definition amrnbdec.c:645
static int pseudonoise(uint32_t *shift_reg, int no_bits)
Comfort noise (DTX) generation, see 3GPP TS 26.092.
Definition amrnbdec.c:1299
static av_cold int amrnb_decode_init(AVCodecContext *avctx)
Definition amrnbdec.c:256
#define LSF_R_FAC
LSF residual tables to Hertz.
Definition amrnbdec.c:80
static void lsf2lsp_5(AMRContext *p, int bfi)
Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors.
Definition amrnbdec.c:422
static void dtx_activity_update(AMRContext *p, const float *synth)
Store the LSF vector and the energy of a speech frame so that the first SID frame of a silent period ...
Definition amrnbdec.c:1402
#define AMR_TILT_RESPONSE
Number of impulse response coefficients used for tilt factor.
Definition amrnbdec.c:108
#define log2f(x)
Definition math.h:27
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
void ff_celp_circ_addf(float *out, const float *in, const float *lagged, int lag, float fac, int n)
Add an array to a rotated array.
void av_cold ff_celp_filter_init(CELPFContext *c)
Initialize CELPFContext.
av_cold void ff_celp_math_init(CELPMContext *c)
Initialize CELPMContext.
Definition celp_math.c:114
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define avg(a, b, c, d)
common internal and external API header
#define av_clipd
Definition common.h:148
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
#define av_clipf
Definition common.h:145
static __device__ float truncf(float a)
static __device__ float fabsf(float a)
static __device__ float fabs(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1795
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ret
Definition ffplay.c:391
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Definition g723_1.h:260
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:439
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
int a
#define b
Definition input.c:43
frame_type
static void filter1(SUINT32 *dst, const int32_t *src, int32_t coeff, ptrdiff_t len)
Definition dcadsp.c:360
#define av_cold
Definition attributes.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define lrintf(x)
Definition libm_mips.h:74
void ff_acelp_lspd2lpc(const double *lsp, float *lpc, int lp_half_order)
Reconstruct LPC coefficients from the line spectral pair frequencies.
Definition lsp.c:226
void ff_set_min_dist_lsf(float *lsf, double min_spacing, int size)
Adjust the quantized LSFs so they are increasing and not too close.
Definition lsp.c:55
void ff_acelp_lsf2lspd(double *lsp, const float *lsf, int lp_order)
Floating point version of ff_acelp_lsf2lsp()
Definition lsp.c:97
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
float exp2f(float x)
Definition math.c:49
#define M_PI
Definition mathematics.h:67
const uint8_t * code
Definition spdifenc.c:433
AMRContext ch[2]
Definition amrnbdec.c:217
float ec_gain_past_code
Definition amrnbdec.c:213
CELPFContext celpf_ctx
context for filters for CELP-based codecs
Definition amrnbdec.c:182
uint32_t cn_seed
comfort noise generator shift register
Definition amrnbdec.c:193
double lsp[4][LP_FILTER_ORDER]
lsp vectors from current frame
Definition amrnbdec.c:145
float prev_sparse_fixed_gain
previous fixed gain; used by anti-sparseness processing to determine "onset"
Definition amrnbdec.c:169
uint8_t hang_count
the number of subframes since a hangover period started
Definition amrnbdec.c:167
float lsf_hist[DTX_HIST_SIZE][LP_FILTER_ORDER]
LSF history used when a SID frame starts DTX.
Definition amrnbdec.c:194
int hangover_added
a DTX hangover was assumed for this frame
Definition amrnbdec.c:203
uint8_t pitch_lag_int
integer part of pitch lag from current subframe
Definition amrnbdec.c:153
float pitch_vector[AMR_SUBFRAME_SIZE]
adaptive code book (pitch) vector
Definition amrnbdec.c:158
int valid_data
the CN parameters of this frame may be used
Definition amrnbdec.c:205
float ec_gain_prev_pit
Definition amrnbdec.c:212
ACELPFContext acelpf_ctx
context for filters for ACELP-based codecs
Definition amrnbdec.c:180
float excitation_buf[PITCH_DELAY_MAX+LP_FILTER_ORDER+1+AMR_SUBFRAME_SIZE]
current excitation and all necessary excitation history
Definition amrnbdec.c:155
float fixed_gain[5]
quantified fixed gains for the current and previous four subframes
Definition amrnbdec.c:163
float ec_gain_past_pit
Definition amrnbdec.c:212
float cn_lsp[LP_FILTER_ORDER]
comfort noise LSP vector of the last SID
Definition amrnbdec.c:191
enum Mode cur_frame_mode
Definition amrnbdec.c:142
float lsf_q[4][LP_FILTER_ORDER]
Interpolated LSF vector for fixed gain smoothing.
Definition amrnbdec.c:148
uint8_t bad_frame_indicator
bad frame ? 1 : 0
Definition amrnbdec.c:141
float tilt_mem
previous input to tilt compensation filter
Definition amrnbdec.c:174
int data_updated
CN parameters have been renewed at least once.
Definition amrnbdec.c:206
float sid_period_inv
inverse of the SID update period
Definition amrnbdec.c:188
float log_en_hist[DTX_HIST_SIZE]
energy history used when a SID frame starts DTX
Definition amrnbdec.c:196
uint32_t no_data_seed
pseudo-random source for lost frames
Definition amrnbdec.c:211
float cn_log_en
comfort noise log2 energy of the last SID
Definition amrnbdec.c:189
ACELPVContext acelpv_ctx
context for vector operations for ACELP-based codecs
Definition amrnbdec.c:181
int prev_bf
the previous frame could not be used
Definition amrnbdec.c:207
float lpc[4][LP_FILTER_ORDER]
lpc coefficient vectors for 4 subframes
Definition amrnbdec.c:151
float beta
previous pitch_gain, bounded by [0.0,SHARP_MAX]
Definition amrnbdec.c:165
int bfh_state
bad frame handling state (0..6)
Definition amrnbdec.c:210
float cn_lsp_old[LP_FILTER_ORDER]
comfort noise LSP vector before the last SID
Definition amrnbdec.c:192
float lsf_avg[LP_FILTER_ORDER]
vector of averaged lsf vector
Definition amrnbdec.c:149
float postfilter_mem[10]
previous intermediate values in the formant filter
Definition amrnbdec.c:173
int lsf_hist_ptr
next position to write in the LSF history
Definition amrnbdec.c:197
int16_t prev_lsf_r[LP_FILTER_ORDER]
residual LSF vector from previous subframe
Definition amrnbdec.c:144
uint8_t prev_ir_filter_nr
previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none
Definition amrnbdec.c:170
float lsf_hist_mean[DTX_HIST_SIZE][LP_FILTER_ORDER]
deviation from the mean of the LSF history
Definition amrnbdec.c:195
uint8_t ir_filter_onset
flag for impulse response filter strength
Definition amrnbdec.c:171
float high_pass_mem[2]
previous intermediate values in the high-pass filter
Definition amrnbdec.c:176
float prediction_error[4]
quantified prediction errors {20log10(^gamma_gc)} for previous four subframes
Definition amrnbdec.c:161
float cn_log_en_old
comfort noise log2 energy before the last SID
Definition amrnbdec.c:190
float * excitation
pointer to the current excitation vector in excitation_buf
Definition amrnbdec.c:156
float log_en_adjust
speech mode dependent level adjustment
Definition amrnbdec.c:199
float log_pg_mean
smoothed logarithm of the prediction gain
Definition amrnbdec.c:200
float pitch_gain[5]
quantified pitch gains for the current and previous four subframes
Definition amrnbdec.c:162
float samples_in[LP_FILTER_ORDER+AMR_SUBFRAME_SIZE]
floating point samples
Definition amrnbdec.c:178
float ec_gain_pbuf[5]
Definition amrnbdec.c:212
double prev_lsp_sub4[LP_FILTER_ORDER]
lsp vector for the 4th subframe of the previous frame
Definition amrnbdec.c:146
float postfilter_agc
previous factor used for adaptive gain control
Definition amrnbdec.c:175
int hangover_count
remaining DTX hangover frames
Definition amrnbdec.c:201
int dec_ana_elapsed
frames since the last DTX hangover
Definition amrnbdec.c:202
CELPMContext celpm_ctx
context for fixed point math operations
Definition amrnbdec.c:183
int since_last_sid
frames since the last SID update
Definition amrnbdec.c:187
int log_en_hist_ptr
next position to write in the energy history
Definition amrnbdec.c:198
float ec_gain_gbuf[5]
Definition amrnbdec.c:213
float ec_gain_prev_code
Definition amrnbdec.c:213
uint8_t diff_count
the number of subframes for which diff has been above 0.65
Definition amrnbdec.c:166
float fixed_vector[AMR_SUBFRAME_SIZE]
algebraic codebook (fixed) vector (must be kept zero between frames)
Definition amrnbdec.c:159
int sid_frame
the current frame carries CN parameters
Definition amrnbdec.c:204
int dtx_state
current DTX state (SPEECH, DTX or DTX_MUTE)
Definition amrnbdec.c:186
AMRNBFrame frame
decoded AMR parameters (lsf coefficients, codebook indexes, etc)
Definition amrnbdec.c:140
Sparse representation for the algebraic codebook (fixed) vector.
int x[10]
float pitch_fac
float y[10]
AMRNB unpacked data frame.
Definition amrnbdata.h:68
AMRNB unpacked data subframe.
Definition amrnbdata.h:58
uint16_t pulses[10]
pulses: 10 for MODE_12k2, 7 for MODE_10k2, and index and sign for others
Definition amrnbdata.h:62
uint16_t p_gain
index to decode the pitch gain
Definition amrnbdata.h:60
uint16_t fixed_gain
index to decode the fixed gain factor, for MODE_12k2 and MODE_7k95
Definition amrnbdata.h:61
uint16_t p_lag
index to decode the pitch lag
Definition amrnbdata.h:59
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1056
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1048
int sample_rate
samples per second
Definition avcodec.h:1041
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
Definition swscale.c:82
uint8_t level
Definition svq3.c:208
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static struct @232240247175332074307271266321204261331170211232 state
else temp
Definition vf_mcdeint.c:275
static float mean(const float *input, int size)
Definition vf_nnedi.c:861
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const uint16_t positions[][14][3]
static unsigned int seed
Definition videogen.c:78