FFmpeg
Loading...
Searching...
No Matches
amrwbdec.c
Go to the documentation of this file.
1/*
2 * AMR wideband decoder
3 * Copyright (c) 2010 Marcelo Galvao Povoa
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A particular PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * AMR wideband decoder
25 */
26
27#include "config.h"
28
29#include "libavutil/avassert.h"
31#include "libavutil/common.h"
32#include "libavutil/lfg.h"
33
34#include "avcodec.h"
35#include "get_bits.h"
36#include "lsp.h"
37#include "celp_filters.h"
38#include "celp_math.h"
39#include "acelp_filters.h"
40#include "acelp_vectors.h"
41#include "acelp_pitch_delay.h"
42#include "codec_internal.h"
43#include "decode.h"
44
45#define AMR_USE_16BIT_TABLES
46#include "amr.h"
47
48#include "amrwbdata.h"
49#if ARCH_MIPS
50#include "mips/amrwbdec_mips.h"
51#endif /* ARCH_MIPS */
52
53/**
54 * High band gain of the comfort noise relative to the low band one.
55 *
56 * This is a non-reference approximation for the high band excitation
57 * scaling, not a value ported from the reference decoder: the reference
58 * shapes much less noise into the high band but has no single constant
59 * counterpart to this figure.
60 */
61#define CN_HF_GAIN_FAC 0.25f
62
63/** Received frame types used by the comfort noise (DTX) state machine */
74
75/** States of the comfort noise (DTX) state machine */
81
82typedef struct AMRWBContext {
83 AMRWBFrame frame; ///< AMRWB parameters decoded from bitstream
84 enum Mode fr_cur_mode; ///< mode index of current frame
85 uint8_t fr_quality; ///< frame quality index (FQI)
86 float isf_cur[LP_ORDER]; ///< working ISF vector from current frame
87 float isf_q_past[LP_ORDER]; ///< quantized ISF vector of the previous frame
88 float isf_past_final[LP_ORDER]; ///< final processed ISF vector of the previous frame
89 double isp[4][LP_ORDER]; ///< ISP vectors from current frame
90 double isp_sub4_past[LP_ORDER]; ///< ISP vector for the 4th subframe of the previous frame
91
92 float lp_coef[4][LP_ORDER]; ///< Linear Prediction Coefficients from ISP vector
93
94 uint8_t base_pitch_lag; ///< integer part of pitch lag for the next relative subframe
95 uint8_t pitch_lag_int; ///< integer part of pitch lag of the previous subframe
96
97 float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE]; ///< current excitation and all necessary excitation history
98 float *excitation; ///< points to current excitation in excitation_buf[]
99
100 float pitch_vector[AMRWB_SFR_SIZE]; ///< adaptive codebook (pitch) vector for current subframe
101 float fixed_vector[AMRWB_SFR_SIZE]; ///< algebraic codebook (fixed) vector for current subframe
102
103 float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes
104 float pitch_gain[6]; ///< quantified pitch gains for the current and previous five subframes
105 float fixed_gain[2]; ///< quantified fixed gains for the current and previous subframes
106
107 float tilt_coef; ///< {beta_1} related to the voicing of the previous subframe
108
109 float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness to determine "onset"
110 uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none
111 float prev_tr_gain; ///< previous initial gain used by noise enhancer for threshold
112
113 float samples_az[LP_ORDER + AMRWB_SFR_SIZE]; ///< low-band samples and memory from synthesis at 12.8kHz
114 float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE]; ///< low-band samples and memory processed for upsampling
115 float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k]; ///< high-band samples and memory from synthesis at 16kHz
116
117 float hpf_31_mem[2], hpf_400_mem[2]; ///< previous values in the high pass filters
118 float demph_mem[1]; ///< previous value in the de-emphasis filter
119 float bpf_6_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band band pass filter
120 float lpf_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band low pass filter
121
122 AVLFG prng; ///< random number generator for white noise excitation
123 uint8_t first_frame; ///< flag active during decoding of the first frame
124 ACELPFContext acelpf_ctx; ///< context for filters for ACELP-based codecs
125 ACELPVContext acelpv_ctx; ///< context for vector operations for ACELP-based codecs
126 CELPFContext celpf_ctx; ///< context for filters for CELP-based codecs
127 CELPMContext celpm_ctx; ///< context for fixed point math operations
128
129 /* Comfort noise (DTX) state */
130 int dtx_state; ///< current DTX state (SPEECH, DTX or DTX_MUTE)
131 int since_last_sid; ///< frames since the last SID update
132 float sid_period_inv; ///< inverse of the SID update period
133 float cn_log_en; ///< comfort noise log2 energy of the last SID
134 float cn_log_en_old; ///< comfort noise log2 energy before the last SID
135 float cn_isf[LP_ORDER]; ///< comfort noise ISF vector of the last SID
136 float cn_isf_old[LP_ORDER]; ///< comfort noise ISF vector before the last SID
137 int16_t cn_seed; ///< comfort noise generator seed
138 int16_t cn_dither_seed; ///< comfort noise dithering seed
139 int cn_dith; ///< comfort noise dithering flag from the last SID
140 float isf_hist[DTX_HIST_SIZE][LP_ORDER]; ///< ISF history used when a SID frame starts DTX
141 float log_en_hist[DTX_HIST_SIZE]; ///< energy history used when a SID frame starts DTX
142 int hist_ptr; ///< next position to write in the DTX history
143 int hangover_count; ///< remaining DTX hangover frames
144 int dec_ana_elapsed; ///< frames since the last DTX hangover
145 int hangover_added; ///< a DTX hangover was assumed for this frame
146 int sid_frame; ///< the current frame carries CN parameters
147 int valid_data; ///< the CN parameters of this frame may be used
148 int data_updated; ///< CN parameters have been renewed at least once
149 int vad_hist; ///< consecutive frames without voice activity
150
152
156
158{
160 int i;
161
162 if (avctx->ch_layout.nb_channels > 2) {
163 avpriv_report_missing_feature(avctx, ">2 channel AMR");
165 }
166
167 if (!avctx->ch_layout.nb_channels) {
170 }
171 if (!avctx->sample_rate)
172 avctx->sample_rate = 16000;
174
175 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
176 AMRWBContext *ctx = &s->ch[ch];
177
178 av_lfg_init(&ctx->prng, 1);
179
180 ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1];
181 ctx->first_frame = 1;
182
183 for (i = 0; i < LP_ORDER; i++)
184 ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15));
185
186 for (i = 0; i < 4; i++)
187 ctx->prediction_error[i] = MIN_ENERGY;
188
189 /* Comfort noise state, matches the DTX decoder reset */
190 ctx->dtx_state = DTX_SPEECH;
191 ctx->sid_period_inv = 0.25f;
192 ctx->cn_log_en = CN_LOG_EN_INIT;
193 ctx->cn_log_en_old = CN_LOG_EN_INIT;
194 ctx->cn_seed = CN_NOISE_SEED_INIT;
195 ctx->cn_dither_seed = CN_NOISE_SEED_INIT;
196 ctx->hangover_count = DTX_HANG_CONST;
197 ctx->dec_ana_elapsed = 32767;
198
199 for (i = 0; i < LP_ORDER; i++)
200 ctx->cn_isf[i] = ctx->cn_isf_old[i] = isf_init[i] * (1.0f / (1 << 15));
201
202 for (i = 0; i < DTX_HIST_SIZE; i++) {
203 memcpy(ctx->isf_hist[i], ctx->cn_isf, LP_ORDER * sizeof(*ctx->cn_isf));
204 ctx->log_en_hist[i] = CN_LOG_EN_HIST_INIT;
205 }
206
207 ff_acelp_filter_init(&ctx->acelpf_ctx);
208 ff_acelp_vectors_init(&ctx->acelpv_ctx);
209 ff_celp_filter_init(&ctx->celpf_ctx);
210 ff_celp_math_init(&ctx->celpm_ctx);
211 }
212
213 return 0;
214}
215
216/**
217 * Decode the frame header in the "MIME/storage" format. This format
218 * is simpler and does not carry the auxiliary frame information.
219 *
220 * @param[in] ctx The Context
221 * @param[in] buf Pointer to the input buffer
222 *
223 * @return The decoded header length in bytes
224 */
225static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
226{
227 /* Decode frame header (1st octet) */
228 ctx->fr_cur_mode = buf[0] >> 3 & 0x0F;
229 ctx->fr_quality = (buf[0] & 0x4) == 0x4;
230
231 return 1;
232}
233
234/**
235 * Decode quantized ISF vectors using 36-bit indexes (6K60 mode only).
236 *
237 * @param[in] ind Array of 5 indexes
238 * @param[out] isf_q Buffer for isf_q[LP_ORDER]
239 */
240static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
241{
242 int i;
243
244 for (i = 0; i < 9; i++)
245 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
246
247 for (i = 0; i < 7; i++)
248 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
249
250 for (i = 0; i < 5; i++)
251 isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15));
252
253 for (i = 0; i < 4; i++)
254 isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15));
255
256 for (i = 0; i < 7; i++)
257 isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));
258}
259
260/**
261 * Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode).
262 *
263 * @param[in] ind Array of 7 indexes
264 * @param[out] isf_q Buffer for isf_q[LP_ORDER]
265 */
266static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
267{
268 int i;
269
270 for (i = 0; i < 9; i++)
271 isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
272
273 for (i = 0; i < 7; i++)
274 isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
275
276 for (i = 0; i < 3; i++)
277 isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
278
279 for (i = 0; i < 3; i++)
280 isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
281
282 for (i = 0; i < 3; i++)
283 isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
284
285 for (i = 0; i < 3; i++)
286 isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
287
288 for (i = 0; i < 4; i++)
289 isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
290}
291
292/**
293 * Apply mean and past ISF values using the prediction factor.
294 * Updates past ISF vector.
295 *
296 * @param[in,out] isf_q Current quantized ISF
297 * @param[in,out] isf_past Past quantized ISF
298 */
299static void isf_add_mean_and_past(float *isf_q, float *isf_past)
300{
301 int i;
302 float tmp;
303
304 for (i = 0; i < LP_ORDER; i++) {
305 tmp = isf_q[i];
306 isf_q[i] += isf_mean[i] * (1.0f / (1 << 15));
307 isf_q[i] += PRED_FACTOR * isf_past[i];
308 isf_past[i] = tmp;
309 }
310}
311
312/**
313 * Interpolate the fourth ISP vector from current and past frames
314 * to obtain an ISP vector for each subframe.
315 *
316 * @param[in,out] isp_q ISPs for each subframe
317 * @param[in] isp4_past Past ISP for subframe 4
318 */
319static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
320{
321 int i, k;
322
323 for (k = 0; k < 3; k++) {
324 float c = isfp_inter[k];
325 for (i = 0; i < LP_ORDER; i++)
326 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
327 }
328}
329
330/**
331 * Decode an adaptive codebook index into pitch lag (except 6k60, 8k85 modes).
332 * Calculate integer lag and fractional lag always using 1/4 resolution.
333 * In 1st and 3rd subframes the index is relative to last subframe integer lag.
334 *
335 * @param[out] lag_int Decoded integer pitch lag
336 * @param[out] lag_frac Decoded fractional pitch lag
337 * @param[in] pitch_index Adaptive codebook pitch index
338 * @param[in,out] base_lag_int Base integer lag used in relative subframes
339 * @param[in] subframe Current subframe index (0 to 3)
340 */
341static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index,
342 uint8_t *base_lag_int, int subframe)
343{
344 if (subframe == 0 || subframe == 2) {
345 if (pitch_index < 376) {
346 *lag_int = (pitch_index + 137) >> 2;
347 *lag_frac = pitch_index - (*lag_int << 2) + 136;
348 } else if (pitch_index < 440) {
349 *lag_int = (pitch_index + 257 - 376) >> 1;
350 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) * 2;
351 /* the actual resolution is 1/2 but expressed as 1/4 */
352 } else {
353 *lag_int = pitch_index - 280;
354 *lag_frac = 0;
355 }
356 /* minimum lag for next subframe */
357 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
359 // XXX: the spec states clearly that *base_lag_int should be
360 // the nearest integer to *lag_int (minus 8), but the ref code
361 // actually always uses its floor, I'm following the latter
362 } else {
363 *lag_int = (pitch_index + 1) >> 2;
364 *lag_frac = pitch_index - (*lag_int << 2);
365 *lag_int += *base_lag_int;
366 }
367}
368
369/**
370 * Decode an adaptive codebook index into pitch lag for 8k85 and 6k60 modes.
371 * The description is analogous to decode_pitch_lag_high, but in 6k60 the
372 * relative index is used for all subframes except the first.
373 */
374static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index,
375 uint8_t *base_lag_int, int subframe, enum Mode mode)
376{
377 if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) {
378 if (pitch_index < 116) {
379 *lag_int = (pitch_index + 69) >> 1;
380 *lag_frac = (pitch_index - (*lag_int << 1) + 68) * 2;
381 } else {
382 *lag_int = pitch_index - 24;
383 *lag_frac = 0;
384 }
385 // XXX: same problem as before
386 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
388 } else {
389 *lag_int = (pitch_index + 1) >> 1;
390 *lag_frac = (pitch_index - (*lag_int << 1)) * 2;
391 *lag_int += *base_lag_int;
392 }
393}
394
395/**
396 * Find the pitch vector by interpolating the past excitation at the
397 * pitch delay, which is obtained in this function.
398 *
399 * @param[in,out] ctx The context
400 * @param[in] amr_subframe Current subframe data
401 * @param[in] subframe Current subframe index (0 to 3)
402 */
404 const AMRWBSubFrame *amr_subframe,
405 const int subframe)
406{
407 int pitch_lag_int, pitch_lag_frac;
408 int i;
409 float *exc = ctx->excitation;
410 enum Mode mode = ctx->fr_cur_mode;
411
412 if (mode <= MODE_8k85) {
413 decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
414 &ctx->base_pitch_lag, subframe, mode);
415 } else
416 decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap,
417 &ctx->base_pitch_lag, subframe);
418
419 ctx->pitch_lag_int = pitch_lag_int;
420 pitch_lag_int += pitch_lag_frac > 0;
421
422 /* Calculate the pitch vector by interpolating the past excitation at the
423 pitch lag using a hamming windowed sinc function */
424 ctx->acelpf_ctx.acelp_interpolatef(exc,
425 exc + 1 - pitch_lag_int,
426 ac_inter, 4,
427 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
429
430 /* Check which pitch signal path should be used
431 * 6k60 and 8k85 modes have the ltp flag set to 0 */
432 if (amr_subframe->ltp) {
433 memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float));
434 } else {
435 for (i = 0; i < AMRWB_SFR_SIZE; i++)
436 ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
437 0.18 * exc[i + 1];
438 memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float));
439 }
440}
441
442/** Get x bits in the index interval [lsb,lsb+len-1] inclusive */
443#define BIT_STR(x,lsb,len) av_zero_extend((x) >> (lsb), (len))
444
445/** Get the bit at specified position */
446#define BIT_POS(x, p) (((x) >> (p)) & 1)
447
448/**
449 * The next six functions decode_[i]p_track decode exactly i pulses
450 * positions and amplitudes (-1 or 1) in a subframe track using
451 * an encoded pulse indexing (TS 26.190 section 5.8.2).
452 *
453 * The results are given in out[], in which a negative number means
454 * amplitude -1 and vice versa (i.e., ampl(x) = x / abs(x) ).
455 *
456 * @param[out] out Output buffer (writes i elements)
457 * @param[in] code Pulse index (no. of bits varies, see below)
458 * @param[in] m (log2) Number of potential positions
459 * @param[in] off Offset for decoded positions
460 */
461static inline void decode_1p_track(int *out, int code, int m, int off)
462{
463 int pos = BIT_STR(code, 0, m) + off; ///code: m+1 bits
464
465 out[0] = BIT_POS(code, m) ? -pos : pos;
466}
467
468static inline void decode_2p_track(int *out, int code, int m, int off) ///code: 2m+1 bits
469{
470 int pos0 = BIT_STR(code, m, m) + off;
471 int pos1 = BIT_STR(code, 0, m) + off;
472
473 out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0;
474 out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1;
475 out[1] = pos0 > pos1 ? -out[1] : out[1];
476}
477
478static void decode_3p_track(int *out, int code, int m, int off) ///code: 3m+1 bits
479{
480 int half_2p = BIT_POS(code, 2*m - 1) << (m - 1);
481
482 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
483 m - 1, off + half_2p);
484 decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);
485}
486
487static void decode_4p_track(int *out, int code, int m, int off) ///code: 4m bits
488{
489 int half_4p, subhalf_2p;
490 int b_offset = 1 << (m - 1);
491
492 switch (BIT_STR(code, 4*m - 2, 2)) { /* case ID (2 bits) */
493 case 0: /* 0 pulses in A, 4 pulses in B or vice versa */
494 half_4p = BIT_POS(code, 4*m - 3) << (m - 1); // which has 4 pulses
495 subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2);
496
497 decode_2p_track(out, BIT_STR(code, 0, 2*m - 3),
498 m - 2, off + half_4p + subhalf_2p);
499 decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1),
500 m - 1, off + half_4p);
501 break;
502 case 1: /* 1 pulse in A, 3 pulses in B */
503 decode_1p_track(out, BIT_STR(code, 3*m - 2, m),
504 m - 1, off);
505 decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2),
506 m - 1, off + b_offset);
507 break;
508 case 2: /* 2 pulses in each half */
509 decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1),
510 m - 1, off);
511 decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1),
512 m - 1, off + b_offset);
513 break;
514 case 3: /* 3 pulses in A, 1 pulse in B */
515 decode_3p_track(out, BIT_STR(code, m, 3*m - 2),
516 m - 1, off);
517 decode_1p_track(out + 3, BIT_STR(code, 0, m),
518 m - 1, off + b_offset);
519 break;
520 }
521}
522
523static void decode_5p_track(int *out, int code, int m, int off) ///code: 5m bits
524{
525 int half_3p = BIT_POS(code, 5*m - 1) << (m - 1);
526
527 decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2),
528 m - 1, off + half_3p);
529
530 decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);
531}
532
533static void decode_6p_track(int *out, int code, int m, int off) ///code: 6m-2 bits
534{
535 int b_offset = 1 << (m - 1);
536 /* which half has more pulses in cases 0 to 2 */
537 int half_more = BIT_POS(code, 6*m - 5) << (m - 1);
538 int half_other = b_offset - half_more;
539
540 switch (BIT_STR(code, 6*m - 4, 2)) { /* case ID (2 bits) */
541 case 0: /* 0 pulses in A, 6 pulses in B or vice versa */
543 m - 1, off + half_more);
544 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
545 m - 1, off + half_more);
546 break;
547 case 1: /* 1 pulse in A, 5 pulses in B or vice versa */
549 m - 1, off + half_other);
550 decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5),
551 m - 1, off + half_more);
552 break;
553 case 2: /* 2 pulses in A, 4 pulses in B or vice versa */
554 decode_2p_track(out, BIT_STR(code, 0, 2*m - 1),
555 m - 1, off + half_other);
556 decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4),
557 m - 1, off + half_more);
558 break;
559 case 3: /* 3 pulses in A, 3 pulses in B */
560 decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2),
561 m - 1, off);
562 decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2),
563 m - 1, off + b_offset);
564 break;
565 }
566}
567
568/**
569 * Decode the algebraic codebook index to pulse positions and signs,
570 * then construct the algebraic codebook vector.
571 *
572 * @param[out] fixed_vector Buffer for the fixed codebook excitation
573 * @param[in] pulse_hi MSBs part of the pulse index array (higher modes only)
574 * @param[in] pulse_lo LSBs part of the pulse index array
575 * @param[in] mode Mode of the current frame
576 */
577static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi,
578 const uint16_t *pulse_lo, const enum Mode mode)
579{
580 /* sig_pos stores for each track the decoded pulse position indexes
581 * (1-based) multiplied by its corresponding amplitude (+1 or -1) */
582 int sig_pos[4][6];
583 int spacing = (mode == MODE_6k60) ? 2 : 4;
584 int i, j;
585
586 switch (mode) {
587 case MODE_6k60:
588 for (i = 0; i < 2; i++)
589 decode_1p_track(sig_pos[i], pulse_lo[i], 5, 1);
590 break;
591 case MODE_8k85:
592 for (i = 0; i < 4; i++)
593 decode_1p_track(sig_pos[i], pulse_lo[i], 4, 1);
594 break;
595 case MODE_12k65:
596 for (i = 0; i < 4; i++)
597 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
598 break;
599 case MODE_14k25:
600 for (i = 0; i < 2; i++)
601 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
602 for (i = 2; i < 4; i++)
603 decode_2p_track(sig_pos[i], pulse_lo[i], 4, 1);
604 break;
605 case MODE_15k85:
606 for (i = 0; i < 4; i++)
607 decode_3p_track(sig_pos[i], pulse_lo[i], 4, 1);
608 break;
609 case MODE_18k25:
610 for (i = 0; i < 4; i++)
611 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
612 ((int) pulse_hi[i] << 14), 4, 1);
613 break;
614 case MODE_19k85:
615 for (i = 0; i < 2; i++)
616 decode_5p_track(sig_pos[i], (int) pulse_lo[i] +
617 ((int) pulse_hi[i] << 10), 4, 1);
618 for (i = 2; i < 4; i++)
619 decode_4p_track(sig_pos[i], (int) pulse_lo[i] +
620 ((int) pulse_hi[i] << 14), 4, 1);
621 break;
622 case MODE_23k05:
623 case MODE_23k85:
624 for (i = 0; i < 4; i++)
625 decode_6p_track(sig_pos[i], (int) pulse_lo[i] +
626 ((int) pulse_hi[i] << 11), 4, 1);
627 break;
628 default:
629 av_unreachable("SID and the unused frame types are handled before this "
630 "function, so only speech modes reach it");
631 }
632
633 memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE);
634
635 for (i = 0; i < 4; i++)
636 for (j = 0; j < pulses_nb_per_mode_tr[mode][i]; j++) {
637 int pos = (FFABS(sig_pos[i][j]) - 1) * spacing + i;
638
639 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
640 }
641}
642
643/**
644 * Decode pitch gain and fixed gain correction factor.
645 *
646 * @param[in] vq_gain Vector-quantized index for gains
647 * @param[in] mode Mode of the current frame
648 * @param[out] fixed_gain_factor Decoded fixed gain correction factor
649 * @param[out] pitch_gain Decoded pitch gain
650 */
651static void decode_gains(const uint8_t vq_gain, const enum Mode mode,
652 float *fixed_gain_factor, float *pitch_gain)
653{
654 const int16_t *gains = (mode <= MODE_8k85 ? qua_gain_6b[vq_gain] :
655 qua_gain_7b[vq_gain]);
656
657 *pitch_gain = gains[0] * (1.0f / (1 << 14));
658 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
659}
660
661/**
662 * Apply pitch sharpening filters to the fixed codebook vector.
663 *
664 * @param[in] ctx The context
665 * @param[in,out] fixed_vector Fixed codebook excitation
666 */
667// XXX: Spec states this procedure should be applied when the pitch
668// lag is less than 64, but this checking seems absent in reference and AMR-NB
669static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
670{
671 int i;
672
673 /* Tilt part */
674 for (i = AMRWB_SFR_SIZE - 1; i != 0; i--)
675 fixed_vector[i] -= fixed_vector[i - 1] * ctx->tilt_coef;
676
677 /* Periodicity enhancement part */
678 for (i = ctx->pitch_lag_int; i < AMRWB_SFR_SIZE; i++)
679 fixed_vector[i] += fixed_vector[i - ctx->pitch_lag_int] * 0.85;
680}
681
682/**
683 * Calculate the voicing factor (-1.0 = unvoiced to 1.0 = voiced).
684 *
685 * @param[in] p_vector, f_vector Pitch and fixed excitation vectors
686 * @param[in] p_gain, f_gain Pitch and fixed gains
687 * @param[in] ctx The context
688 */
689// XXX: There is something wrong with the precision here! The magnitudes
690// of the energies are not correct. Please check the reference code carefully
691static float voice_factor(float *p_vector, float p_gain,
692 float *f_vector, float f_gain,
694{
695 double p_ener = (double) ctx->dot_productf(p_vector, p_vector,
697 p_gain * p_gain;
698 double f_ener = (double) ctx->dot_productf(f_vector, f_vector,
700 f_gain * f_gain;
701
702 return (p_ener - f_ener) / (p_ener + f_ener + 0.01);
703}
704
705/**
706 * Reduce fixed vector sparseness by smoothing with one of three IR filters,
707 * also known as "adaptive phase dispersion".
708 *
709 * @param[in] ctx The context
710 * @param[in,out] fixed_vector Unfiltered fixed vector
711 * @param[out] buf Space for modified vector if necessary
712 *
713 * @return The potentially overwritten filtered fixed vector address
714 */
716 float *fixed_vector, float *buf)
717{
718 int ir_filter_nr;
719
720 if (ctx->fr_cur_mode > MODE_8k85) // no filtering in higher modes
721 return fixed_vector;
722
723 if (ctx->pitch_gain[0] < 0.6) {
724 ir_filter_nr = 0; // strong filtering
725 } else if (ctx->pitch_gain[0] < 0.9) {
726 ir_filter_nr = 1; // medium filtering
727 } else
728 ir_filter_nr = 2; // no filtering
729
730 /* detect 'onset' */
731 if (ctx->fixed_gain[0] > 3.0 * ctx->fixed_gain[1]) {
732 if (ir_filter_nr < 2)
733 ir_filter_nr++;
734 } else {
735 int i, count = 0;
736
737 for (i = 0; i < 6; i++)
738 if (ctx->pitch_gain[i] < 0.6)
739 count++;
740
741 if (count > 2)
742 ir_filter_nr = 0;
743
744 if (ir_filter_nr > ctx->prev_ir_filter_nr + 1)
745 ir_filter_nr--;
746 }
747
748 /* update ir filter strength history */
749 ctx->prev_ir_filter_nr = ir_filter_nr;
750
751 ir_filter_nr += (ctx->fr_cur_mode == MODE_8k85);
752
753 if (ir_filter_nr < 2) {
754 int i;
755 const float *coef = ir_filters_lookup[ir_filter_nr];
756
757 /* Circular convolution code in the reference
758 * decoder was modified to avoid using one
759 * extra array. The filtered vector is given by:
760 *
761 * c2(n) = sum(i,0,len-1){ c(i) * coef( (n - i + len) % len ) }
762 */
763
764 memset(buf, 0, sizeof(float) * AMRWB_SFR_SIZE);
765 for (i = 0; i < AMRWB_SFR_SIZE; i++)
766 if (fixed_vector[i])
767 ff_celp_circ_addf(buf, buf, coef, i, fixed_vector[i],
769 fixed_vector = buf;
770 }
771
772 return fixed_vector;
773}
774
775/**
776 * Calculate a stability factor {teta} based on distance between
777 * current and past isf. A value of 1 shows maximum signal stability.
778 */
779static float stability_factor(const float *isf, const float *isf_past)
780{
781 int i;
782 float acc = 0.0;
783
784 for (i = 0; i < LP_ORDER - 1; i++)
785 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
786
787 // XXX: This part is not so clear from the reference code
788 // the result is more accurate changing the "/ 256" to "* 512"
789 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
790}
791
792/**
793 * Apply a non-linear fixed gain smoothing in order to reduce
794 * fluctuation in the energy of excitation.
795 *
796 * @param[in] fixed_gain Unsmoothed fixed gain
797 * @param[in,out] prev_tr_gain Previous threshold gain (updated)
798 * @param[in] voice_fac Frame voicing factor
799 * @param[in] stab_fac Frame stability factor
800 *
801 * @return The smoothed gain
802 */
803static float noise_enhancer(float fixed_gain, float *prev_tr_gain,
804 float voice_fac, float stab_fac)
805{
806 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
807 float g0;
808
809 // XXX: the following fixed-point constants used to in(de)crement
810 // gain by 1.5dB were taken from the reference code, maybe it could
811 // be simpler
812 if (fixed_gain < *prev_tr_gain) {
813 g0 = FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
814 (6226 * (1.0f / (1 << 15)))); // +1.5 dB
815 } else
816 g0 = FFMAX(*prev_tr_gain, fixed_gain *
817 (27536 * (1.0f / (1 << 15)))); // -1.5 dB
818
819 *prev_tr_gain = g0; // update next frame threshold
820
821 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
822}
823
824/**
825 * Filter the fixed_vector to emphasize the higher frequencies.
826 *
827 * @param[in,out] fixed_vector Fixed codebook vector
828 * @param[in] voice_fac Frame voicing factor
829 */
830static void pitch_enhancer(float *fixed_vector, float voice_fac)
831{
832 int i;
833 float cpe = 0.125 * (1 + voice_fac);
834 float last = fixed_vector[0]; // holds c(i - 1)
835
836 fixed_vector[0] -= cpe * fixed_vector[1];
837
838 for (i = 1; i < AMRWB_SFR_SIZE - 1; i++) {
839 float cur = fixed_vector[i];
840
841 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
842 last = cur;
843 }
844
845 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
846}
847
848/**
849 * Conduct 16th order linear predictive coding synthesis from excitation.
850 *
851 * @param[in] ctx Pointer to the AMRWBContext
852 * @param[in] lpc Pointer to the LPC coefficients
853 * @param[out] excitation Buffer for synthesis final excitation
854 * @param[in] fixed_gain Fixed codebook gain for synthesis
855 * @param[in] fixed_vector Algebraic codebook vector
856 * @param[in,out] samples Pointer to the output samples and memory
857 */
858static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation,
859 float fixed_gain, const float *fixed_vector,
860 float *samples)
861{
862 ctx->acelpv_ctx.weighted_vector_sumf(excitation, ctx->pitch_vector, fixed_vector,
863 ctx->pitch_gain[0], fixed_gain, AMRWB_SFR_SIZE);
864
865 /* emphasize pitch vector contribution in low bitrate modes */
866 if (ctx->pitch_gain[0] > 0.5 && ctx->fr_cur_mode <= MODE_8k85) {
867 int i;
868 float energy = ctx->celpm_ctx.dot_productf(excitation, excitation,
870
871 // XXX: Weird part in both ref code and spec. A unknown parameter
872 // {beta} seems to be identical to the current pitch gain
873 float pitch_factor = 0.25 * ctx->pitch_gain[0] * ctx->pitch_gain[0];
874
875 for (i = 0; i < AMRWB_SFR_SIZE; i++)
876 excitation[i] += pitch_factor * ctx->pitch_vector[i];
877
878 ff_scale_vector_to_given_sum_of_squares(excitation, excitation,
879 energy, AMRWB_SFR_SIZE);
880 }
881
882 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
884}
885
886/**
887 * Apply to synthesis a de-emphasis filter of the form:
888 * H(z) = 1 / (1 - m * z^-1)
889 *
890 * @param[out] out Output buffer
891 * @param[in] in Input samples array with in[-1]
892 * @param[in] m Filter coefficient
893 * @param[in,out] mem State from last filtering
894 */
895static void de_emphasis(float *out, float *in, float m, float mem[1])
896{
897 int i;
898
899 out[0] = in[0] + m * mem[0];
900
901 for (i = 1; i < AMRWB_SFR_SIZE; i++)
902 out[i] = in[i] + out[i - 1] * m;
903
904 mem[0] = out[AMRWB_SFR_SIZE - 1];
905}
906
907/**
908 * Upsample a signal by 5/4 ratio (from 12.8kHz to 16kHz) using
909 * a FIR interpolation filter. Uses past data from before *in address.
910 *
911 * @param[out] out Buffer for interpolated signal
912 * @param[in] in Current signal data (length 0.8*o_size)
913 * @param[in] o_size Output signal length
914 * @param[in] ctx The context
915 */
916static void upsample_5_4(float *out, const float *in, int o_size, CELPMContext *ctx)
917{
918 const float *in0 = in - UPS_FIR_SIZE + 1;
919 int i, j, k;
920 int int_part = 0, frac_part;
921
922 i = 0;
923 for (j = 0; j < o_size / 5; j++) {
924 out[i] = in[int_part];
925 frac_part = 4;
926 i++;
927
928 for (k = 1; k < 5; k++) {
929 out[i] = ctx->dot_productf(in0 + int_part,
930 upsample_fir[4 - frac_part],
932 int_part++;
933 frac_part--;
934 i++;
935 }
936 }
937}
938
939/**
940 * Calculate the high-band gain based on encoded index (23k85 mode) or
941 * on the low-band speech signal and the Voice Activity Detection flag.
942 *
943 * @param[in] ctx The context
944 * @param[in] synth LB speech synthesis at 12.8k
945 * @param[in] hb_idx Gain index for mode 23k85 only
946 * @param[in] vad VAD flag for the frame
947 */
948static float find_hb_gain(AMRWBContext *ctx, const float *synth,
949 uint16_t hb_idx, uint8_t vad)
950{
951 int wsp = (vad > 0);
952 float tilt;
953 float tmp;
954
955 if (ctx->fr_cur_mode == MODE_23k85)
956 return qua_hb_gain[hb_idx] * (1.0f / (1 << 14));
957
958 tmp = ctx->celpm_ctx.dot_productf(synth, synth + 1, AMRWB_SFR_SIZE - 1);
959
960 if (tmp > 0) {
961 tilt = tmp / ctx->celpm_ctx.dot_productf(synth, synth, AMRWB_SFR_SIZE);
962 } else
963 tilt = 0;
964
965 /* return gain bounded by [0.1, 1.0] */
966 return av_clipf((1.0 - tilt) * (1.25 - 0.25 * wsp), 0.1, 1.0);
967}
968
969/**
970 * Generate the high-band excitation with the same energy from the lower
971 * one and scaled by the given gain.
972 *
973 * @param[in] ctx The context
974 * @param[out] hb_exc Buffer for the excitation
975 * @param[in] synth_exc Low-band excitation used for synthesis
976 * @param[in] hb_gain Wanted excitation gain
977 */
978static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc,
979 const float *synth_exc, float hb_gain)
980{
981 int i;
982 float energy = ctx->celpm_ctx.dot_productf(synth_exc, synth_exc,
984
985 /* Generate a white-noise excitation */
986 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
987 hb_exc[i] = 32768.0 - (uint16_t) av_lfg_get(&ctx->prng);
988
990 energy * hb_gain * hb_gain,
992}
993
994/**
995 * Calculate the auto-correlation for the ISF difference vector.
996 */
997static float auto_correlation(float *diff_isf, float mean, int lag)
998{
999 int i;
1000 float sum = 0.0;
1001
1002 for (i = 7; i < LP_ORDER - 2; i++) {
1003 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
1004 sum += prod * prod;
1005 }
1006 return sum;
1007}
1008
1009/**
1010 * Extrapolate a ISF vector to the 16kHz range (20th order LP)
1011 * used at mode 6k60 LP filter for the high frequency band.
1012 *
1013 * @param[out] isf Buffer for extrapolated isf; contains LP_ORDER
1014 * values on input
1015 */
1016static void extrapolate_isf(float isf[LP_ORDER_16k])
1017{
1018 float diff_isf[LP_ORDER - 2], diff_mean;
1019 float corr_lag[3];
1020 float est, scale;
1021 int i, j, i_max_corr;
1022
1023 isf[LP_ORDER_16k - 1] = isf[LP_ORDER - 1];
1024
1025 /* Calculate the difference vector */
1026 for (i = 0; i < LP_ORDER - 2; i++)
1027 diff_isf[i] = isf[i + 1] - isf[i];
1028
1029 diff_mean = 0.0;
1030 for (i = 2; i < LP_ORDER - 2; i++)
1031 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
1032
1033 /* Find which is the maximum autocorrelation */
1034 i_max_corr = 0;
1035 for (i = 0; i < 3; i++) {
1036 corr_lag[i] = auto_correlation(diff_isf, diff_mean, i + 2);
1037
1038 if (corr_lag[i] > corr_lag[i_max_corr])
1039 i_max_corr = i;
1040 }
1041 i_max_corr++;
1042
1043 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
1044 isf[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
1045 - isf[i - 2 - i_max_corr];
1046
1047 /* Calculate an estimate for ISF(18) and scale ISF based on the error */
1048 est = 7965 + (isf[2] - isf[3] - isf[4]) / 6.0;
1049 scale = 0.5 * (FFMIN(est, 7600) - isf[LP_ORDER - 2]) /
1050 (isf[LP_ORDER_16k - 2] - isf[LP_ORDER - 2]);
1051
1052 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
1053 diff_isf[j] = scale * (isf[i] - isf[i - 1]);
1054
1055 /* Stability insurance */
1056 for (i = 1; i < LP_ORDER_16k - LP_ORDER; i++)
1057 if (diff_isf[i] + diff_isf[i - 1] < 5.0) {
1058 if (diff_isf[i] > diff_isf[i - 1]) {
1059 diff_isf[i - 1] = 5.0 - diff_isf[i];
1060 } else
1061 diff_isf[i] = 5.0 - diff_isf[i - 1];
1062 }
1063
1064 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
1065 isf[i] = isf[i - 1] + diff_isf[j] * (1.0f / (1 << 15));
1066
1067 /* Scale the ISF vector for 16000 Hz */
1068 for (i = 0; i < LP_ORDER_16k - 1; i++)
1069 isf[i] *= 0.8;
1070}
1071
1072/**
1073 * Spectral expand the LP coefficients using the equation:
1074 * y[i] = x[i] * (gamma ** i)
1075 *
1076 * @param[out] out Output buffer (may use input array)
1077 * @param[in] lpc LP coefficients array
1078 * @param[in] gamma Weighting factor
1079 * @param[in] size LP array size
1080 */
1081static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
1082{
1083 int i;
1084 float fac = gamma;
1085
1086 for (i = 0; i < size; i++) {
1087 out[i] = lpc[i] * fac;
1088 fac *= gamma;
1089 }
1090}
1091
1092/**
1093 * Conduct 20th order linear predictive coding synthesis for the high
1094 * frequency band excitation at 16kHz.
1095 *
1096 * @param[in] ctx The context
1097 * @param[in] subframe Current subframe index (0 to 3)
1098 * @param[in,out] samples Pointer to the output speech samples
1099 * @param[in] exc Generated white-noise scaled excitation
1100 * @param[in] isf Current frame isf vector
1101 * @param[in] isf_past Past frame final isf vector
1102 */
1103static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples,
1104 const float *exc, const float *isf, const float *isf_past)
1105{
1106 float hb_lpc[LP_ORDER_16k];
1107 enum Mode mode = ctx->fr_cur_mode;
1108
1109 if (mode == MODE_6k60) {
1110 float e_isf[LP_ORDER_16k]; // ISF vector for extrapolation
1111 double e_isp[LP_ORDER_16k];
1112
1113 ctx->acelpv_ctx.weighted_vector_sumf(e_isf, isf_past, isf, isfp_inter[subframe],
1114 1.0 - isfp_inter[subframe], LP_ORDER);
1115
1116 extrapolate_isf(e_isf);
1117
1118 e_isf[LP_ORDER_16k - 1] *= 2.0;
1119 ff_acelp_lsf2lspd(e_isp, e_isf, LP_ORDER_16k);
1120 ff_amrwb_lsp2lpc(e_isp, hb_lpc, LP_ORDER_16k);
1121
1122 lpc_weighting(hb_lpc, hb_lpc, 0.9, LP_ORDER_16k);
1123 } else {
1124 lpc_weighting(hb_lpc, ctx->lp_coef[subframe], 0.6, LP_ORDER);
1125 }
1126
1127 ctx->celpf_ctx.celp_lp_synthesis_filterf(samples, hb_lpc, exc, AMRWB_SFR_SIZE_16k,
1129}
1130
1131/**
1132 * Apply a 15th order filter to high-band samples.
1133 * The filter characteristic depends on the given coefficients.
1134 *
1135 * @param[out] out Buffer for filtered output
1136 * @param[in] fir_coef Filter coefficients
1137 * @param[in,out] mem State from last filtering (updated)
1138 * @param[in] in Input speech data (high-band)
1139 *
1140 * @remark It is safe to pass the same array in in and out parameters
1141 */
1142
1143#ifndef hb_fir_filter
1144static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE + 1],
1145 float mem[HB_FIR_SIZE], const float *in)
1146{
1147 int i, j;
1148 float data[AMRWB_SFR_SIZE_16k + HB_FIR_SIZE]; // past and current samples
1149
1150 memcpy(data, mem, HB_FIR_SIZE * sizeof(float));
1151 memcpy(data + HB_FIR_SIZE, in, AMRWB_SFR_SIZE_16k * sizeof(float));
1152
1153 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++) {
1154 out[i] = 0.0;
1155 for (j = 0; j <= HB_FIR_SIZE; j++)
1156 out[i] += data[i + j] * fir_coef[j];
1157 }
1158
1159 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE * sizeof(float));
1160}
1161#endif /* hb_fir_filter */
1162
1163/**
1164 * Update context state before the next subframe.
1165 */
1167{
1168 memmove(&ctx->excitation_buf[0], &ctx->excitation_buf[AMRWB_SFR_SIZE],
1169 (AMRWB_P_DELAY_MAX + LP_ORDER + 1) * sizeof(float));
1170
1171 memmove(&ctx->pitch_gain[1], &ctx->pitch_gain[0], 5 * sizeof(float));
1172 memmove(&ctx->fixed_gain[1], &ctx->fixed_gain[0], 1 * sizeof(float));
1173
1174 memmove(&ctx->samples_az[0], &ctx->samples_az[AMRWB_SFR_SIZE],
1175 LP_ORDER * sizeof(float));
1176 memmove(&ctx->samples_up[0], &ctx->samples_up[AMRWB_SFR_SIZE],
1177 UPS_MEM_SIZE * sizeof(float));
1178 memmove(&ctx->samples_hb[0], &ctx->samples_hb[AMRWB_SFR_SIZE_16k],
1179 LP_ORDER_16k * sizeof(float));
1180}
1181
1182/**
1183 * Comfort noise (DTX) generation, see 3GPP TS 26.192.
1184 *
1185 * The implementation follows the reference decoder in 3GPP TS 26.173,
1186 * with the fixed-point arithmetic replaced by floating point. ISF vectors
1187 * and log2 energies stay in the same units as the speech path (0..0.5
1188 * normalized frequency and log2 of the excitation energy). The tables are
1189 * the noise ISF codebooks from that specification.
1190 */
1191
1192/**
1193 * Generate the next sample of the white noise used as comfort noise
1194 * excitation. The seed wraps around like a 16 bit integer.
1195 */
1196static int16_t noise_gen_amrwb(int16_t *seed)
1197{
1198 *seed = (int16_t)(uint16_t)((int32_t) *seed * 31821 + 13849);
1199
1200 return *seed;
1201}
1202
1203/**
1204 * Decode the comfort noise parameters of a SID frame.
1205 *
1206 * The payload is 40 bits, of which 36 are used: five ISF indices
1207 * (6, 6, 6, 5 and 5 bits), the log energy index (6 bits), the comfort noise
1208 * dithering flag and a flag telling whether this is the first SID frame of a
1209 * silent period.
1210 *
1211 * @param[in] buf Pointer to the frame payload (5 bytes)
1212 * @param[out] ind The five ISF indices
1213 * @param[out] en_idx Index of the logarithmic frame energy
1214 *
1215 * @return non zero if this is a SID update frame
1216 */
1217static int decode_sid_frame(AMRWBContext *ctx, const uint8_t *buf,
1218 int *ind, int *en_idx)
1219{
1220 GetBitContext gb;
1221 int dith, sid_update;
1222
1223 init_get_bits8(&gb, buf, 5);
1224
1225 ind[0] = get_bits(&gb, 6);
1226 ind[1] = get_bits(&gb, 6);
1227 ind[2] = get_bits(&gb, 6);
1228 ind[3] = get_bits(&gb, 5);
1229 ind[4] = get_bits(&gb, 5);
1230 *en_idx = get_bits(&gb, 6);
1231
1232 /* TS 26.173 reads the dither flag only on a valid SID_UPDATE. A
1233 SID_FIRST frame carries the bit but must not change the flag used
1234 for later frames that have no data. */
1235 dith = get_bits1(&gb);
1236 sid_update = get_bits1(&gb);
1237 if (sid_update)
1238 ctx->cn_dith = dith;
1239
1240 return sid_update;
1241}
1242
1243/**
1244 * Decode the quantized background noise ISF vector of a SID frame.
1245 *
1246 * @param[in] ind The five quantization indices
1247 * @param[out] isf_q Buffer for isf_q[LP_ORDER]
1248 */
1249static void decode_isf_noise(const int *ind, float *isf_q)
1250{
1251 int i;
1252
1253 isf_q[0] = dico1_isf_noise[ind[0]][0] * (1.0f / (1 << 15));
1254 isf_q[1] = dico1_isf_noise[ind[0]][1] * (1.0f / (1 << 15));
1255
1256 for (i = 0; i < 3; i++) {
1257 isf_q[i + 2] = dico2_isf_noise[ind[1]][i] * (1.0f / (1 << 15));
1258 isf_q[i + 5] = dico3_isf_noise[ind[2]][i] * (1.0f / (1 << 15));
1259 }
1260
1261 for (i = 0; i < 4; i++) {
1262 isf_q[i + 8] = dico4_isf_noise[ind[3]][i] * (1.0f / (1 << 15));
1263 isf_q[i + 12] = dico5_isf_noise[ind[4]][i] * (1.0f / (1 << 15));
1264 }
1265
1266 for (i = 0; i < LP_ORDER; i++)
1267 isf_q[i] += mean_isf_noise[i] * (1.0f / (1 << 15));
1268
1270}
1271
1272/**
1273 * Draw a dithering value in [-1, 1] from the comfort noise generator.
1274 *
1275 * @param[in,out] ctx The Context
1276 */
1278{
1279 return ((noise_gen_amrwb(&ctx->cn_dither_seed) >> 1) +
1280 (noise_gen_amrwb(&ctx->cn_dither_seed) >> 1)) * (1.0f / 32768.0f);
1281}
1282
1283/**
1284 * Insert comfort noise dithering, used when the background noise is not
1285 * stationary.
1286 *
1287 * @param[in,out] ctx The Context
1288 * @param[in,out] isf ISF vector of the comfort noise
1289 * @param[in,out] log_en log2 energy of the comfort noise
1290 */
1291static void cn_dithering(AMRWBContext *ctx, float *isf, float *log_en)
1292{
1293 float fac;
1294 int i;
1295
1296 /* Insert comfort noise dithering for energy parameter */
1297 *log_en = FFMAX(*log_en + cn_dither_noise(ctx) * CN_DITH_GAIN_FAC, 0.0f);
1298
1299 /* Insert comfort noise dithering for spectral parameters (ISF vector) */
1300 fac = ISF_DITH_FAC_LOW;
1301 isf[0] = FFMAX(isf[0] + cn_dither_noise(ctx) * fac, MIN_ISF_SPACING);
1302
1303 for (i = 1; i < LP_ORDER - 1; i++) {
1304 float cur;
1305 fac += ISF_DITH_FAC_STEP;
1306 cur = isf[i] + cn_dither_noise(ctx) * fac;
1307 /* Make sure that the ISF spacing remains at least ISF_DITH_GAP */
1308 isf[i] = FFMAX(cur, isf[i - 1] + ISF_DITH_GAP);
1309 }
1310
1311 isf[LP_ORDER - 2] = FFMIN(isf[LP_ORDER - 2], 0.5f);
1312}
1313
1314/**
1315 * Store the ISF vector and the excitation energy of a speech frame so
1316 * that the first SID frame of a silent period can reuse them.
1317 *
1318 * @param[in,out] ctx The Context
1319 * @param[in] isf ISF vector of the decoded frame
1320 * @param[in] exc_energy Excitation energy of the decoded frame
1321 */
1322static void dtx_activity_update(AMRWBContext *ctx, const float *isf,
1323 float exc_energy)
1324{
1325 ctx->hist_ptr = (ctx->hist_ptr + 1) & (DTX_HIST_SIZE - 1);
1326 memcpy(ctx->isf_hist[ctx->hist_ptr], isf, LP_ORDER * sizeof(*isf));
1327 ctx->log_en_hist[ctx->hist_ptr] = log2f(FFMAX(exc_energy, 1.0f) /
1328 (4 * AMRWB_SFR_SIZE));
1329}
1330
1331/**
1332 * Update the comfort noise state and generate the excitation of the
1333 * comfort noise for this frame.
1334 *
1335 * @param[in,out] ctx The Context
1336 * @param[out] cn_exc Excitation for the whole frame (256 samples)
1337 * @param[in] new_state DTX state of this frame
1338 * @param[in] ind ISF indices of the SID frame
1339 * @param[in] en_idx Energy index of the SID frame
1340 */
1341static void dtx_dec(AMRWBContext *ctx, float *cn_exc, int new_state,
1342 const int *ind, int en_idx)
1343{
1344 float *isf = ctx->isf_cur;
1345 float log_en, int_fac;
1346 int i, j, period;
1347
1348 /* SID frames that start a silent period reuse the noise parameters
1349 of the speech frames preceding them. */
1350 if (ctx->hangover_added && ctx->sid_frame) {
1351 int ptr = (ctx->hist_ptr + 1) & (DTX_HIST_SIZE - 1);
1352
1353 memcpy(ctx->isf_hist[ptr], ctx->isf_hist[ctx->hist_ptr],
1354 sizeof(ctx->isf_hist[0]));
1355 ctx->log_en_hist[ptr] = ctx->log_en_hist[ctx->hist_ptr];
1356
1357 ctx->cn_log_en = 0.0f;
1358 for (i = 0; i < DTX_HIST_SIZE; i++)
1359 ctx->cn_log_en += ctx->log_en_hist[i];
1360 /* the reference decoder keeps the average in Q9 and adds 2 so that
1361 the value stays positive, it is subtracted again when the level
1362 of the excitation is computed */
1363 ctx->cn_log_en = ctx->cn_log_en / DTX_HIST_SIZE + 2.0f;
1364 ctx->cn_log_en = FFMAX(ctx->cn_log_en, 0.0f);
1365
1366 for (j = 0; j < LP_ORDER; j++) {
1367 float acc = 0.0f;
1368 for (i = 0; i < DTX_HIST_SIZE; i++)
1369 acc += ctx->isf_hist[i][j];
1370 ctx->cn_isf[j] = acc / DTX_HIST_SIZE;
1371 }
1372 }
1373
1374 if (ctx->sid_frame) {
1375 memcpy(ctx->cn_isf_old, ctx->cn_isf, LP_ORDER * sizeof(*ctx->cn_isf));
1376 ctx->cn_log_en_old = ctx->cn_log_en;
1377
1378 if (ctx->valid_data) {
1379 period = FFMIN(ctx->since_last_sid, 32);
1380 ctx->sid_period_inv = period >= 2 ? 1.0f / period : 0.5f;
1381
1382 decode_isf_noise(ind, ctx->cn_isf);
1383 ctx->cn_log_en = en_idx * (1.0f / 2.625f);
1384
1385 /* no interpolation at startup or right after speech */
1386 if (!ctx->data_updated || ctx->dtx_state == DTX_SPEECH) {
1387 memcpy(ctx->cn_isf_old, ctx->cn_isf,
1388 LP_ORDER * sizeof(*ctx->cn_isf));
1389 ctx->cn_log_en_old = ctx->cn_log_en;
1390 }
1391 }
1392 }
1393
1394 /* A frame carrying new parameters ends the interpolation, so the
1395 parameters of this very frame are the ones used below. The counter is
1396 reset again at the end of this function. */
1397 if (ctx->sid_frame && ctx->valid_data)
1398 ctx->since_last_sid = 0;
1399
1400 /* Interpolate the comfort noise parameters since the last update */
1401 int_fac = FFMIN(ctx->since_last_sid * ctx->sid_period_inv, 1.0f);
1402
1403 log_en = int_fac * ctx->cn_log_en + (1.0f - int_fac) * ctx->cn_log_en_old;
1404 for (j = 0; j < LP_ORDER; j++)
1405 isf[j] = int_fac * ctx->cn_isf[j] + (1.0f - int_fac) * ctx->cn_isf_old[j];
1406
1407 if (ctx->cn_dith)
1408 cn_dithering(ctx, isf, &log_en);
1409
1410 /* Generate the excitation and scale it to the wanted energy */
1411 for (i = 0; i < 4 * AMRWB_SFR_SIZE; i++)
1412 cn_exc[i] = noise_gen_amrwb(&ctx->cn_seed) >> 4;
1413
1414 /* the excitation carries the energy of the decoded noise parameters */
1416 (4 * AMRWB_SFR_SIZE) *
1417 exp2f(log_en - 2.0f),
1418 4 * AMRWB_SFR_SIZE);
1419
1420 if (new_state == DTX_MUTE) {
1421 /* Mute the comfort noise, nothing has been updated for a while */
1422 period = ctx->since_last_sid;
1423 if (period > 32)
1424 period = 32;
1425 if (period <= 0)
1426 period = 8;
1427 ctx->sid_period_inv = 1.0f / period;
1428 ctx->since_last_sid = 0;
1429 ctx->cn_log_en_old = ctx->cn_log_en;
1430 ctx->cn_log_en -= CN_LOG_EN_MUTE_STEP;
1431 }
1432
1433 if (ctx->sid_frame &&
1434 (ctx->valid_data || (!ctx->valid_data && ctx->hangover_added))) {
1435 ctx->since_last_sid = 0;
1436 ctx->data_updated = 1;
1437 }
1438}
1439
1440/**
1441 * Update the comfort noise state machine for a received frame.
1442 *
1443 * @param[in,out] ctx The Context
1444 * @param[in] frame_type Type of the received frame
1445 *
1446 * @return the DTX state to decode this frame with
1447 */
1449{
1450 int new_state, enc_state;
1451
1454 ((ctx->dtx_state == DTX || ctx->dtx_state == DTX_MUTE) &&
1457 new_state = DTX;
1458
1459 /* stay muted for these input types */
1460 if (ctx->dtx_state == DTX_MUTE &&
1463 new_state = DTX_MUTE;
1464
1465 ctx->since_last_sid = FFMIN(ctx->since_last_sid + 1, 32767);
1466
1467 /* SID updates are handled separately to avoid muting a late one */
1468 if (frame_type != RX_SID_UPDATE &&
1469 ctx->since_last_sid > DTX_MAX_EMPTY_THRESH)
1470 new_state = DTX_MUTE;
1471 } else {
1472 new_state = DTX_SPEECH;
1473 ctx->since_last_sid = 0;
1474 }
1475
1476 if (!ctx->data_updated && frame_type == RX_SID_UPDATE)
1477 ctx->dec_ana_elapsed = 0;
1478
1479 ctx->dec_ana_elapsed = FFMIN(ctx->dec_ana_elapsed + 1, 32767);
1480 ctx->hangover_added = 0;
1481
1484 (frame_type == RX_NO_DATA && (ctx->dtx_state != DTX_SPEECH ||
1485 ctx->vad_hist >= DTX_HANG_CONST)))
1486 enc_state = DTX;
1487 else
1488 enc_state = DTX_SPEECH;
1489
1490 if (enc_state == DTX_SPEECH) {
1491 ctx->hangover_count = DTX_HANG_CONST;
1492 } else if (ctx->dec_ana_elapsed > DTX_ELAPSED_FRAMES_THRESH) {
1493 ctx->hangover_added = 1;
1494 ctx->dec_ana_elapsed = 0;
1495 ctx->hangover_count = 0;
1496 } else if (!ctx->hangover_count) {
1497 ctx->dec_ana_elapsed = 0;
1498 } else {
1499 ctx->hangover_count--;
1500 }
1501
1502 if (new_state != DTX_SPEECH) {
1503 ctx->sid_frame = 0;
1504 ctx->valid_data = 0;
1505
1506 if (frame_type == RX_SID_FIRST) {
1507 ctx->sid_frame = 1;
1508 } else if (frame_type == RX_SID_UPDATE) {
1509 ctx->sid_frame = 1;
1510 ctx->valid_data = 1;
1511 } else if (frame_type == RX_SID_BAD) {
1512 ctx->sid_frame = 1;
1513 ctx->hangover_added = 0;
1514 }
1515 }
1516
1517 return new_state;
1518}
1519
1520/**
1521 * Reset the speech decoder state, it is not used during a silent period.
1522 */
1524{
1525 memset(ctx->excitation_buf, 0, sizeof(ctx->excitation_buf));
1526 memset(ctx->isf_q_past, 0, sizeof(ctx->isf_q_past));
1527
1528 ctx->first_frame = 1;
1529 ctx->prev_tr_gain = 0.0f;
1530 ctx->prev_ir_filter_nr = 0;
1531 ctx->tilt_coef = 0.0f;
1532
1533 /* Reset_decoder(st, 0) clears the phase-dispersion memory, including
1534 the previous fixed gain and the pitch-gain history. */
1535 memset(ctx->pitch_gain, 0, sizeof(ctx->pitch_gain));
1536 memset(ctx->fixed_gain, 0, sizeof(ctx->fixed_gain));
1537 ctx->prev_sparse_fixed_gain = 0.0f;
1538}
1539
1540/**
1541 * Post-process one synthesized subframe, generate the high frequency band
1542 * and store the result at 16 kHz.
1543 *
1544 * @param[in,out] ctx The Context
1545 * @param[out] sub_buf Output samples of the subframe at 16 kHz
1546 * @param[in] synth_exc Excitation the subframe was built from
1547 * @param[in] sub Subframe index
1548 * @param[in] hb_idx High-band gain index (23k85 mode only)
1549 * @param[in] vad Voice activity flag of the frame
1550 * @param[in] hf_gain Scaling of the high band noise
1551 */
1552static void subframe_output(AMRWBContext *ctx, float *sub_buf,
1553 const float *synth_exc, int sub, uint16_t hb_idx,
1554 uint8_t vad, float hf_gain)
1555{
1556 float hb_exc[AMRWB_SFR_SIZE_16k];
1557 float hb_samples[AMRWB_SFR_SIZE_16k];
1558 float hb_gain;
1559 int i;
1560
1561 /* Synthesis speech post-processing */
1562 de_emphasis(&ctx->samples_up[UPS_MEM_SIZE], &ctx->samples_az[LP_ORDER],
1563 PREEMPH_FAC, ctx->demph_mem);
1564
1565 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(&ctx->samples_up[UPS_MEM_SIZE],
1566 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_31_poles,
1567 hpf_31_gain, ctx->hpf_31_mem, AMRWB_SFR_SIZE);
1568
1569 upsample_5_4(sub_buf, &ctx->samples_up[UPS_FIR_SIZE],
1570 AMRWB_SFR_SIZE_16k, &ctx->celpm_ctx);
1571
1572 /* High frequency band (6.4 - 7.0 kHz) generation part */
1573 ctx->acelpf_ctx.acelp_apply_order_2_transfer_function(hb_samples,
1574 &ctx->samples_up[UPS_MEM_SIZE], hpf_zeros, hpf_400_poles,
1575 hpf_400_gain, ctx->hpf_400_mem, AMRWB_SFR_SIZE);
1576
1577 hb_gain = find_hb_gain(ctx, hb_samples, hb_idx, vad);
1578
1579 scaled_hb_excitation(ctx, hb_exc, synth_exc, hb_gain * hf_gain);
1580
1581 hb_synthesis(ctx, sub, &ctx->samples_hb[LP_ORDER_16k],
1582 hb_exc, ctx->isf_cur, ctx->isf_past_final);
1583
1584 /* High-band post-processing filters */
1585 hb_fir_filter(hb_samples, bpf_6_7_coef, ctx->bpf_6_7_mem,
1586 &ctx->samples_hb[LP_ORDER_16k]);
1587
1588 if (ctx->fr_cur_mode == MODE_23k85)
1589 hb_fir_filter(hb_samples, lpf_7_coef, ctx->lpf_7_mem,
1590 hb_samples);
1591
1592 /* Add the low and high frequency bands */
1593 for (i = 0; i < AMRWB_SFR_SIZE_16k; i++)
1594 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
1595
1596 /* Update buffers and history */
1598}
1599
1600/**
1601 * Decode one frame of comfort noise into the output buffer.
1602 *
1603 * @param[in,out] ctx The Context
1604 * @param[in] new_state DTX state of this frame
1605 * @param[in] ind ISF indices of the SID frame
1606 * @param[in] en_idx Energy index of the SID frame
1607 * @param[out] buf_out Output buffer for one frame at 16 kHz
1608 */
1609static void cn_frame(AMRWBContext *ctx, int new_state, const int *ind,
1610 int en_idx, float *buf_out)
1611{
1612 float cn_exc[4 * AMRWB_SFR_SIZE];
1613 float synth_exc[AMRWB_SFR_SIZE];
1614 int sub;
1615
1616 dtx_dec(ctx, cn_exc, new_state, ind, en_idx);
1617
1619
1620 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
1621 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
1622 ff_amrwb_lsp2lpc(ctx->isp[3], ctx->lp_coef[0], LP_ORDER);
1623 for (sub = 1; sub < 4; sub++)
1624 memcpy(ctx->lp_coef[sub], ctx->lp_coef[0], sizeof(ctx->lp_coef[0]));
1625
1626 /* the reference decoder uses one set of CN parameters per frame and
1627 no adaptive codebook contribution */
1628 ctx->pitch_gain[0] = 0.0f;
1629
1630 for (sub = 0; sub < 4; sub++) {
1631 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
1632
1633 memcpy(ctx->fixed_vector, &cn_exc[sub * AMRWB_SFR_SIZE],
1634 AMRWB_SFR_SIZE * sizeof(*ctx->fixed_vector));
1635
1636 synthesis(ctx, ctx->lp_coef[sub], synth_exc, 1.0f,
1637 ctx->fixed_vector, &ctx->samples_az[LP_ORDER]);
1638
1639 subframe_output(ctx, sub_buf, synth_exc, sub, 0, !ctx->vad_hist,
1641 }
1642
1643 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
1644 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(*ctx->isf_cur));
1645
1647 ctx->dtx_state = new_state;
1648}
1649
1651 int *got_frame_ptr, AVPacket *avpkt)
1652{
1654 const uint8_t *buf = avpkt->data;
1655 int buf_size = avpkt->size;
1656 int sub, i, ret;
1657
1658 /* get output buffer */
1659 frame->nb_samples = 4 * AMRWB_SFR_SIZE_16k;
1660 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1661 return ret;
1662
1663 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
1664 AMRWBContext *ctx = &s->ch[ch];
1665 AMRWBFrame *cf = &ctx->frame;
1666 int expected_fr_size, header_size, rx_type, new_state;
1667 float spare_vector[AMRWB_SFR_SIZE]; // extra stack space to hold result from anti-sparseness processing
1668 float fixed_gain_factor; // fixed gain correction factor (gamma)
1669 float *synth_fixed_vector; // pointer to the fixed vector that synthesis should use
1670 float synth_fixed_gain; // the fixed gain that synthesis should use
1671 float voice_fac, stab_fac; // parameters used for gain smoothing
1672 float synth_exc[AMRWB_SFR_SIZE]; // post-processed excitation for synthesis
1673 int sid_ind[5] = { 0 }; // ISF indices of a SID frame
1674 int sid_en_idx = 0; // energy index of a SID frame
1675 float frame_exc_energy = 0.0f; // excitation energy of this frame
1676 float *buf_out = (float *)frame->extended_data[ch];
1677
1678 header_size = decode_mime_header(ctx, buf);
1679 expected_fr_size = ((cf_sizes_wb[ctx->fr_cur_mode] + 7) >> 3) + 1;
1680
1681 if (!ctx->fr_quality)
1682 av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
1683
1684 if (buf_size < expected_fr_size) {
1685 av_log(avctx, AV_LOG_ERROR,
1686 "Frame too small (%d bytes). Truncated file?\n", buf_size);
1687 *got_frame_ptr = 0;
1688 return AVERROR_INVALIDDATA;
1689 }
1690 /* Classify the received frame for the comfort noise state machine */
1691 if (ctx->fr_cur_mode == MODE_SID) {
1692 if (ctx->fr_quality) {
1693 if (decode_sid_frame(ctx, buf + 1, sid_ind, &sid_en_idx))
1694 rx_type = RX_SID_UPDATE;
1695 else
1696 rx_type = RX_SID_FIRST;
1697 } else {
1698 rx_type = RX_SID_BAD;
1699 }
1700 } else if (ctx->fr_cur_mode == NO_DATA ||
1701 (ctx->fr_cur_mode > MODE_SID && ctx->fr_cur_mode < SP_LOST)) {
1702 /* the reference decoder replaces the unused frame types by
1703 frames carrying no data */
1704 rx_type = RX_NO_DATA;
1705 } else if (ctx->fr_cur_mode == SP_LOST) {
1706 rx_type = RX_SPEECH_LOST;
1707 } else if (!ctx->fr_quality) {
1708 rx_type = RX_SPEECH_BAD;
1709 } else {
1710 rx_type = RX_SPEECH_GOOD;
1711 }
1712 new_state = dtx_handler(ctx, rx_type);
1713 if (new_state != DTX_SPEECH) {
1714 /* Comfort noise is generated for SID frames and, once a silent
1715 period has started, for the frames carrying no data at all */
1716 cn_frame(ctx, new_state, sid_ind, sid_en_idx,
1717 (float *)frame->extended_data[ch]);
1718 buf += expected_fr_size;
1719 buf_size -= expected_fr_size;
1720 continue;
1721 }
1722 if (ctx->fr_cur_mode > MODE_SID || !ctx->fr_quality) {
1723 /* The specification suggests a "random signal" and
1724 "a muting technique" to "gradually decrease the output level".
1725 Outside of a silent period no comfort noise parameters are
1726 available, so the frames carrying no payload at all are output
1727 as silence. They still produce a full frame of samples, keeping
1728 the frame count and the timing of the decoded stream correct. */
1729 av_samples_set_silence(&frame->extended_data[ch], 0, frame->nb_samples, 1, AV_SAMPLE_FMT_FLT);
1730 buf += expected_fr_size;
1731 buf_size -= expected_fr_size;
1732 continue;
1733 }
1734 ff_amr_bit_reorder((uint16_t *) &ctx->frame, sizeof(AMRWBFrame),
1735 buf + header_size, amr_bit_orderings_by_mode[ctx->fr_cur_mode]);
1736
1737 /* Decode the quantized ISF vector */
1738 if (ctx->fr_cur_mode == MODE_6k60) {
1739 decode_isf_indices_36b(cf->isp_id, ctx->isf_cur);
1740 } else {
1741 decode_isf_indices_46b(cf->isp_id, ctx->isf_cur);
1742 }
1743
1744 isf_add_mean_and_past(ctx->isf_cur, ctx->isf_q_past);
1746
1747 stab_fac = stability_factor(ctx->isf_cur, ctx->isf_past_final);
1748
1749 ctx->isf_cur[LP_ORDER - 1] *= 2.0;
1750 ff_acelp_lsf2lspd(ctx->isp[3], ctx->isf_cur, LP_ORDER);
1751
1752 /* Generate a ISP vector for each subframe */
1753 if (ctx->first_frame) {
1754 ctx->first_frame = 0;
1755 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(double));
1756 }
1757 interpolate_isp(ctx->isp, ctx->isp_sub4_past);
1758
1759 for (sub = 0; sub < 4; sub++)
1760 ff_amrwb_lsp2lpc(ctx->isp[sub], ctx->lp_coef[sub], LP_ORDER);
1761
1762 for (sub = 0; sub < 4; sub++) {
1763 const AMRWBSubFrame *cur_subframe = &cf->subframe[sub];
1764 float *sub_buf = buf_out + sub * AMRWB_SFR_SIZE_16k;
1765
1766 /* Decode adaptive codebook (pitch vector) */
1767 decode_pitch_vector(ctx, cur_subframe, sub);
1768 /* Decode innovative codebook (fixed vector) */
1769 decode_fixed_vector(ctx->fixed_vector, cur_subframe->pul_ih,
1770 cur_subframe->pul_il, ctx->fr_cur_mode);
1771
1772 pitch_sharpening(ctx, ctx->fixed_vector);
1773
1774 decode_gains(cur_subframe->vq_gain, ctx->fr_cur_mode,
1775 &fixed_gain_factor, &ctx->pitch_gain[0]);
1776
1777 ctx->fixed_gain[0] =
1778 ff_amr_set_fixed_gain(fixed_gain_factor,
1779 ctx->celpm_ctx.dot_productf(ctx->fixed_vector,
1780 ctx->fixed_vector,
1783 ctx->prediction_error,
1785
1786 /* Calculate voice factor and store tilt for next subframe */
1787 voice_fac = voice_factor(ctx->pitch_vector, ctx->pitch_gain[0],
1788 ctx->fixed_vector, ctx->fixed_gain[0],
1789 &ctx->celpm_ctx);
1790 ctx->tilt_coef = voice_fac * 0.25 + 0.25;
1791
1792 /* Construct current excitation */
1793 for (i = 0; i < AMRWB_SFR_SIZE; i++) {
1794 ctx->excitation[i] *= ctx->pitch_gain[0];
1795 ctx->excitation[i] += ctx->fixed_gain[0] * ctx->fixed_vector[i];
1796 ctx->excitation[i] = truncf(ctx->excitation[i]);
1797 }
1798
1799 /* the excitation energy is the comfort noise reference for the
1800 silent periods following this frame */
1801 frame_exc_energy += ctx->celpm_ctx.dot_productf(ctx->excitation,
1802 ctx->excitation,
1804 /* Post-processing of excitation elements */
1805 synth_fixed_gain = noise_enhancer(ctx->fixed_gain[0], &ctx->prev_tr_gain,
1806 voice_fac, stab_fac);
1807
1808 synth_fixed_vector = anti_sparseness(ctx, ctx->fixed_vector,
1809 spare_vector);
1810
1811 pitch_enhancer(synth_fixed_vector, voice_fac);
1812
1813 synthesis(ctx, ctx->lp_coef[sub], synth_exc, synth_fixed_gain,
1814 synth_fixed_vector, &ctx->samples_az[LP_ORDER]);
1815
1816 subframe_output(ctx, sub_buf, synth_exc, sub,
1817 cur_subframe->hb_gain, cf->vad, 1.0f);
1818 }
1819
1820 /* update state for next frame */
1821 memcpy(ctx->isp_sub4_past, ctx->isp[3], LP_ORDER * sizeof(ctx->isp[3][0]));
1822 memcpy(ctx->isf_past_final, ctx->isf_cur, LP_ORDER * sizeof(float));
1823
1824 /* keep the noise parameters of this speech frame for the next SID */
1825 ctx->isf_cur[LP_ORDER - 1] *= 0.5f;
1826 dtx_activity_update(ctx, ctx->isf_cur, frame_exc_energy);
1827 ctx->isf_cur[LP_ORDER - 1] *= 2.0f;
1828 if (ctx->fr_quality) {
1829 ctx->vad_hist = cf->vad ? 0 : FFMIN(ctx->vad_hist + 1, 32767);
1830 } else if (ctx->vad_hist > 0)
1831 ctx->vad_hist = FFMIN(ctx->vad_hist + 1, 32767);
1832 ctx->dtx_state = new_state;
1833 buf += expected_fr_size;
1834 buf_size -= expected_fr_size;
1835 }
1836
1837 *got_frame_ptr = 1;
1838
1839 return buf - avpkt->data;
1840}
1841
1843 .p.name = "amrwb",
1844 CODEC_LONG_NAME("AMR-WB (Adaptive Multi-Rate WideBand)"),
1845 .p.type = AVMEDIA_TYPE_AUDIO,
1846 .p.id = AV_CODEC_ID_AMR_WB,
1847 .priv_data_size = sizeof(AMRWBChannelsContext),
1850 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1851};
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)
av_cold void ff_acelp_vectors_init(ACELPVContext *c)
Initialize ACELPVContext.
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 FFCodec ff_amrwb_decoder
Definition amrwbdec.c:1842
static const float *const ir_filters_lookup[2]
Definition amrnbdata.h:1653
#define DTX_MAX_EMPTY_THRESH
Definition amrnbdata.h:1669
#define DTX_HIST_SIZE
Definition amrnbdata.h:1668
Mode
Frame type (Table 1a in 3GPP TS 26.101)
Definition amrnbdata.h:39
@ NO_DATA
no transmission
Definition amrnbdata.h:50
#define DTX_ELAPSED_FRAMES_THRESH
Definition amrnbdata.h:1671
#define DTX_HANG_CONST
Definition amrnbdata.h:1670
static const float energy_pred_fac[4]
4-tap moving average prediction coefficients in reverse order
Definition amrnbdata.h:1458
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_NO_DATA
Definition amrnbdec.c:129
@ RX_SPEECH_BAD
Definition amrnbdec.c:125
#define MIN_ENERGY
Initial energy in dB.
Definition amrnbdec.c:98
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
AMR wideband data and definitions.
static const int16_t dico24_isf[32][3]
Definition amrwbdata.h:1393
static const int16_t mean_isf_noise[16]
Means of ISF vectors for background noise in Q15.
Definition amrwbdata.h:1649
static const int16_t qua_gain_7b[128][2]
Definition amrwbdata.h:1998
static const int16_t dico5_isf_noise[32][4]
Definition amrwbdata.h:1895
#define UPS_MEM_SIZE
Definition amrwbdata.h:37
#define CN_LOG_EN_HIST_INIT
initial value of the energy history
Definition amrwbdata.h:55
static const uint16_t qua_hb_gain[16]
High band quantized gains for 23k85 in Q14.
Definition amrwbdata.h:2150
#define MIN_ISF_SPACING
minimum isf gap
Definition amrwbdata.h:39
static const float hpf_zeros[2]
High-pass filters coefficients for 31 Hz and 400 Hz cutoff.
Definition amrwbdata.h:2113
static const float lpf_7_coef[31]
Definition amrwbdata.h:2170
static const int16_t dico1_isf[256][9]
Indexed tables for retrieval of quantized ISF vectors in Q15.
Definition amrwbdata.h:706
#define HB_FIR_SIZE
amount of past data needed by HB filters
Definition amrwbdata.h:35
static const uint16_t *const amr_bit_orderings_by_mode[]
Reordering array addresses for each mode.
Definition amrwbdata.h:690
static const int16_t isf_mean[LP_ORDER]
Means of ISF vectors in Q15.
Definition amrwbdata.h:1633
static const float hpf_31_poles[2]
Definition amrwbdata.h:2114
@ MODE_23k05
23.05 kbit/s
Definition amrwbdata.h:73
@ MODE_12k65
12.65 kbit/s
Definition amrwbdata.h:68
@ SP_LOST
speech lost
Definition amrwbdata.h:77
@ MODE_8k85
8.85 kbit/s
Definition amrwbdata.h:67
@ MODE_18k25
18.25 kbit/s
Definition amrwbdata.h:71
@ MODE_15k85
15.85 kbit/s
Definition amrwbdata.h:70
@ MODE_19k85
19.85 kbit/s
Definition amrwbdata.h:72
@ MODE_SID
comfort noise frame
Definition amrwbdata.h:75
@ MODE_23k85
23.85 kbit/s
Definition amrwbdata.h:74
@ MODE_14k25
14.25 kbit/s
Definition amrwbdata.h:69
@ MODE_6k60
6.60 kbit/s
Definition amrwbdata.h:66
static const int16_t dico25_isf[32][4]
Definition amrwbdata.h:1412
#define AMRWB_P_DELAY_MIN
Definition amrwbdata.h:62
static const int16_t qua_gain_6b[64][2]
Tables for decoding quantized gains { pitch (Q14), fixed factor (Q11) }.
Definition amrwbdata.h:1963
static const int16_t dico4_isf_noise[32][4]
Definition amrwbdata.h:1860
#define AMRWB_P_DELAY_MAX
maximum pitch delay value
Definition amrwbdata.h:61
static const float hpf_400_gain
Definition amrwbdata.h:2118
#define CN_DITH_GAIN_FAC
energy dithering factor
Definition amrwbdata.h:53
static const int16_t dico21_isf[64][3]
Definition amrwbdata.h:1224
static const int16_t isf_init[LP_ORDER]
Initialization tables for the processed ISF vector in Q15.
Definition amrwbdata.h:1639
#define UPS_FIR_SIZE
upsampling filter size
Definition amrwbdata.h:36
static const float hpf_31_gain
Definition amrwbdata.h:2115
static const int16_t dico23_isf_36b[64][7]
Definition amrwbdata.h:1565
static const int16_t dico2_isf[256][7]
Definition amrwbdata.h:965
static const uint16_t cf_sizes_wb[]
Core frame sizes in each mode.
Definition amrwbdata.h:2185
#define ISF_DITH_FAC_STEP
ISF dithering factor increment.
Definition amrwbdata.h:52
static const float ac_inter[65]
Coefficients for FIR interpolation of excitation vector at pitch lag resulting the adaptive codebook ...
Definition amrwbdata.h:1935
static const float hpf_400_poles[2]
Definition amrwbdata.h:2117
static const float bpf_6_7_coef[31]
High-band post-processing FIR filters coefficients from Q15.
Definition amrwbdata.h:2156
#define LP_ORDER
linear predictive coding filter order
Definition amrwbdata.h:33
#define CN_LOG_EN_MUTE_STEP
log2 energy decrease per muted frame (-3/8 dB)
Definition amrwbdata.h:56
#define CN_NOISE_SEED_INIT
initial seed of the comfort noise generator
Definition amrwbdata.h:57
#define PREEMPH_FAC
factor used to de-emphasize synthesis
Definition amrwbdata.h:43
static const float upsample_fir[4][24]
Interpolation coefficients for 5/4 signal upsampling Table from the reference source was reordered fo...
Definition amrwbdata.h:2122
#define CN_LOG_EN_INIT
initial comfort noise log2 energy
Definition amrwbdata.h:54
#define ENERGY_MEAN
mean innovation energy (dB) in all modes
Definition amrwbdata.h:42
#define ISF_DITH_GAP
minimum ISF gap used by comfort noise dithering
Definition amrwbdata.h:50
static const int16_t dico21_isf_36b[128][5]
Definition amrwbdata.h:1431
static const int16_t dico1_isf_noise[64][2]
Indexed tables for retrieval of quantized background noise ISF vectors in Q15 (split-by-5 VQ: 2+3+3+4...
Definition amrwbdata.h:1659
static const int16_t dico2_isf_noise[64][3]
Definition amrwbdata.h:1726
#define AMRWB_SFR_SIZE_16k
samples per subframe at 16 kHz
Definition amrwbdata.h:60
#define PRED_FACTOR
Definition amrwbdata.h:40
static const int16_t dico3_isf_noise[64][3]
Definition amrwbdata.h:1793
static const int16_t dico22_isf[128][3]
Definition amrwbdata.h:1259
static const int16_t dico23_isf[128][3]
Definition amrwbdata.h:1326
static const float isfp_inter[4]
ISF/ISP interpolation coefficients for each subframe.
Definition amrwbdata.h:1931
#define ISF_DITH_FAC_LOW
lowest ISF dithering factor
Definition amrwbdata.h:51
#define LP_ORDER_16k
lpc filter order at 16kHz
Definition amrwbdata.h:34
static const int16_t dico22_isf_36b[128][4]
Definition amrwbdata.h:1498
static const uint8_t pulses_nb_per_mode_tr[][4]
[i][j] is the number of pulses present in track j at mode i
Definition amrwbdata.h:1956
#define AMRWB_SFR_SIZE
samples per subframe at 12.8 kHz
Definition amrwbdata.h:59
static int decode_sid_frame(AMRWBContext *ctx, const uint8_t *buf, int *ind, int *en_idx)
Decode the comfort noise parameters of a SID frame.
Definition amrwbdec.c:1217
static void decode_gains(const uint8_t vq_gain, const enum Mode mode, float *fixed_gain_factor, float *pitch_gain)
Decode pitch gain and fixed gain correction factor.
Definition amrwbdec.c:651
static void dtx_dec(AMRWBContext *ctx, float *cn_exc, int new_state, const int *ind, int en_idx)
Update the comfort noise state and generate the excitation of the comfort noise for this frame.
Definition amrwbdec.c:1341
static void extrapolate_isf(float isf[LP_ORDER_16k])
Extrapolate a ISF vector to the 16kHz range (20th order LP) used at mode 6k60 LP filter for the high ...
Definition amrwbdec.c:1016
static void update_sub_state(AMRWBContext *ctx)
Update context state before the next subframe.
Definition amrwbdec.c:1166
#define CN_HF_GAIN_FAC
High band gain of the comfort noise relative to the low band one.
Definition amrwbdec.c:61
static void decode_6p_track(int *out, int code, int m, int off)
code: 6m-2 bits
Definition amrwbdec.c:533
@ RX_SPEECH_PROBABLY_DEGRADED
Definition amrwbdec.c:66
@ RX_SPEECH_LOST
Definition amrwbdec.c:67
static void decode_pitch_vector(AMRWBContext *ctx, const AMRWBSubFrame *amr_subframe, const int subframe)
Find the pitch vector by interpolating the past excitation at the pitch delay, which is obtained in t...
Definition amrwbdec.c:403
static void decode_1p_track(int *out, int code, int m, int off)
The next six functions decode_[i]p_track decode exactly i pulses positions and amplitudes (-1 or 1) i...
Definition amrwbdec.c:461
static void isf_add_mean_and_past(float *isf_q, float *isf_past)
Apply mean and past ISF values using the prediction factor.
Definition amrwbdec.c:299
static void upsample_5_4(float *out, const float *in, int o_size, CELPMContext *ctx)
Upsample a signal by 5/4 ratio (from 12.8kHz to 16kHz) using a FIR interpolation filter.
Definition amrwbdec.c:916
static void scaled_hb_excitation(AMRWBContext *ctx, float *hb_exc, const float *synth_exc, float hb_gain)
Generate the high-band excitation with the same energy from the lower one and scaled by the given gai...
Definition amrwbdec.c:978
static void cn_frame(AMRWBContext *ctx, int new_state, const int *ind, int en_idx, float *buf_out)
Decode one frame of comfort noise into the output buffer.
Definition amrwbdec.c:1609
static void cn_reset_speech_state(AMRWBContext *ctx)
Reset the speech decoder state, it is not used during a silent period.
Definition amrwbdec.c:1523
static float stability_factor(const float *isf, const float *isf_past)
Calculate a stability factor {teta} based on distance between current and past isf.
Definition amrwbdec.c:779
static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe, enum Mode mode)
Decode an adaptive codebook index into pitch lag for 8k85 and 6k60 modes.
Definition amrwbdec.c:374
static void cn_dithering(AMRWBContext *ctx, float *isf, float *log_en)
Insert comfort noise dithering, used when the background noise is not stationary.
Definition amrwbdec.c:1291
static float voice_factor(float *p_vector, float p_gain, float *f_vector, float f_gain, CELPMContext *ctx)
Calculate the voicing factor (-1.0 = unvoiced to 1.0 = voiced).
Definition amrwbdec.c:691
static void decode_isf_indices_36b(uint16_t *ind, float *isf_q)
Decode quantized ISF vectors using 36-bit indexes (6K60 mode only).
Definition amrwbdec.c:240
static void pitch_enhancer(float *fixed_vector, float voice_fac)
Filter the fixed_vector to emphasize the higher frequencies.
Definition amrwbdec.c:830
static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi, const uint16_t *pulse_lo, const enum Mode mode)
Decode the algebraic codebook index to pulse positions and signs, then construct the algebraic codebo...
Definition amrwbdec.c:577
static int16_t noise_gen_amrwb(int16_t *seed)
Comfort noise (DTX) generation, see 3GPP TS 26.192.
Definition amrwbdec.c:1196
static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past)
Interpolate the fourth ISP vector from current and past frames to obtain an ISP vector for each subfr...
Definition amrwbdec.c:319
static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe)
Decode an adaptive codebook index into pitch lag (except 6k60, 8k85 modes).
Definition amrwbdec.c:341
static float cn_dither_noise(AMRWBContext *ctx)
Draw a dithering value in [-1, 1] from the comfort noise generator.
Definition amrwbdec.c:1277
static int dtx_handler(AMRWBContext *ctx, int frame_type)
Update the comfort noise state machine for a received frame.
Definition amrwbdec.c:1448
static void hb_synthesis(AMRWBContext *ctx, int subframe, float *samples, const float *exc, const float *isf, const float *isf_past)
Conduct 20th order linear predictive coding synthesis for the high frequency band excitation at 16kHz...
Definition amrwbdec.c:1103
static av_cold int amrwb_decode_init(AVCodecContext *avctx)
Definition amrwbdec.c:157
static float noise_enhancer(float fixed_gain, float *prev_tr_gain, float voice_fac, float stab_fac)
Apply a non-linear fixed gain smoothing in order to reduce fluctuation in the energy of excitation.
Definition amrwbdec.c:803
#define BIT_POS(x, p)
Get the bit at specified position.
Definition amrwbdec.c:446
#define BIT_STR(x, lsb, len)
Get x bits in the index interval [lsb,lsb+len-1] inclusive.
Definition amrwbdec.c:443
static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf)
Decode the frame header in the "MIME/storage" format.
Definition amrwbdec.c:225
static void subframe_output(AMRWBContext *ctx, float *sub_buf, const float *synth_exc, int sub, uint16_t hb_idx, uint8_t vad, float hf_gain)
Post-process one synthesized subframe, generate the high frequency band and store the result at 16 kH...
Definition amrwbdec.c:1552
static void decode_isf_noise(const int *ind, float *isf_q)
Decode the quantized background noise ISF vector of a SID frame.
Definition amrwbdec.c:1249
static void decode_4p_track(int *out, int code, int m, int off)
code: 4m bits
Definition amrwbdec.c:487
static int amrwb_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition amrwbdec.c:1650
static void hb_fir_filter(float *out, const float fir_coef[HB_FIR_SIZE+1], float mem[HB_FIR_SIZE], const float *in)
Apply a 15th order filter to high-band samples.
Definition amrwbdec.c:1144
static float auto_correlation(float *diff_isf, float mean, int lag)
Calculate the auto-correlation for the ISF difference vector.
Definition amrwbdec.c:997
static void lpc_weighting(float *out, const float *lpc, float gamma, int size)
Spectral expand the LP coefficients using the equation: y[i] = x[i] * (gamma ** i)
Definition amrwbdec.c:1081
static void decode_isf_indices_46b(uint16_t *ind, float *isf_q)
Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode).
Definition amrwbdec.c:266
static float * anti_sparseness(AMRWBContext *ctx, float *fixed_vector, float *buf)
Reduce fixed vector sparseness by smoothing with one of three IR filters, also known as "adaptive pha...
Definition amrwbdec.c:715
static void de_emphasis(float *out, float *in, float m, float mem[1])
Apply to synthesis a de-emphasis filter of the form: H(z) = 1 / (1 - m * z^-1)
Definition amrwbdec.c:895
static void synthesis(AMRWBContext *ctx, float *lpc, float *excitation, float fixed_gain, const float *fixed_vector, float *samples)
Conduct 16th order linear predictive coding synthesis from excitation.
Definition amrwbdec.c:858
static void decode_5p_track(int *out, int code, int m, int off)
code: 5m bits
Definition amrwbdec.c:523
static float find_hb_gain(AMRWBContext *ctx, const float *synth, uint16_t hb_idx, uint8_t vad)
Calculate the high-band gain based on encoded index (23k85 mode) or on the low-band speech signal and...
Definition amrwbdec.c:948
static void pitch_sharpening(AMRWBContext *ctx, float *fixed_vector)
Apply pitch sharpening filters to the fixed codebook vector.
Definition amrwbdec.c:669
static void decode_3p_track(int *out, int code, int m, int off)
code: 3m+1 bits
Definition amrwbdec.c:478
static void decode_2p_track(int *out, int code, int m, int off)
code: 2m+1 bits
Definition amrwbdec.c:468
static void dtx_activity_update(AMRWBContext *ctx, const float *isf, float exc_energy)
Store the ISF vector and the excitation energy of a speech frame so that the first SID frame of a sil...
Definition amrwbdec.c:1322
Reference: libavcodec/amrwbdec.c.
#define log2f(x)
Definition math.h:27
static FILE * out
static AVFormatContext * ctx
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
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)
common internal and external API header
#define av_clip
Definition common.h:100
#define av_clipf
Definition common.h:145
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
static __device__ float truncf(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
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_WB
Definition codec_id.h:441
#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
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:247
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
frame_type
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
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
#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.
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
void ff_amrwb_lsp2lpc(const double *lsp, float *lp, int lp_order)
LSP to LP conversion (5.2.4 of AMR-WB)
Definition lsp.c:181
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
float exp2f(float x)
Definition math.c:49
const char data[16]
Definition mxf.c:155
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
AMRWBContext ch[2]
Definition amrwbdec.c:154
CELPMContext celpm_ctx
context for fixed point math operations
Definition amrwbdec.c:127
int since_last_sid
frames since the last SID update
Definition amrwbdec.c:131
float cn_isf[LP_ORDER]
comfort noise ISF vector of the last SID
Definition amrwbdec.c:135
uint8_t base_pitch_lag
integer part of pitch lag for the next relative subframe
Definition amrwbdec.c:94
int cn_dith
comfort noise dithering flag from the last SID
Definition amrwbdec.c:139
int valid_data
the CN parameters of this frame may be used
Definition amrwbdec.c:147
int vad_hist
consecutive frames without voice activity
Definition amrwbdec.c:149
uint8_t first_frame
flag active during decoding of the first frame
Definition amrwbdec.c:123
int hist_ptr
next position to write in the DTX history
Definition amrwbdec.c:142
float prev_sparse_fixed_gain
previous fixed gain; used by anti-sparseness to determine "onset"
Definition amrwbdec.c:109
int16_t cn_dither_seed
comfort noise dithering seed
Definition amrwbdec.c:138
CELPFContext celpf_ctx
context for filters for CELP-based codecs
Definition amrwbdec.c:126
float isf_past_final[LP_ORDER]
final processed ISF vector of the previous frame
Definition amrwbdec.c:88
int dtx_state
current DTX state (SPEECH, DTX or DTX_MUTE)
Definition amrwbdec.c:130
float cn_isf_old[LP_ORDER]
comfort noise ISF vector before the last SID
Definition amrwbdec.c:136
ACELPFContext acelpf_ctx
context for filters for ACELP-based codecs
Definition amrwbdec.c:124
AMRWBFrame frame
AMRWB parameters decoded from bitstream.
Definition amrwbdec.c:83
float log_en_hist[DTX_HIST_SIZE]
energy history used when a SID frame starts DTX
Definition amrwbdec.c:141
float samples_az[LP_ORDER+AMRWB_SFR_SIZE]
low-band samples and memory from synthesis at 12.8kHz
Definition amrwbdec.c:113
float pitch_gain[6]
quantified pitch gains for the current and previous five subframes
Definition amrwbdec.c:104
int hangover_count
remaining DTX hangover frames
Definition amrwbdec.c:143
float cn_log_en_old
comfort noise log2 energy before the last SID
Definition amrwbdec.c:134
float isf_q_past[LP_ORDER]
quantized ISF vector of the previous frame
Definition amrwbdec.c:87
ACELPVContext acelpv_ctx
context for vector operations for ACELP-based codecs
Definition amrwbdec.c:125
float excitation_buf[AMRWB_P_DELAY_MAX+LP_ORDER+2+AMRWB_SFR_SIZE]
current excitation and all necessary excitation history
Definition amrwbdec.c:97
float sid_period_inv
inverse of the SID update period
Definition amrwbdec.c:132
float fixed_vector[AMRWB_SFR_SIZE]
algebraic codebook (fixed) vector for current subframe
Definition amrwbdec.c:101
float hpf_400_mem[2]
previous values in the high pass filters
Definition amrwbdec.c:117
int dec_ana_elapsed
frames since the last DTX hangover
Definition amrwbdec.c:144
int hangover_added
a DTX hangover was assumed for this frame
Definition amrwbdec.c:145
float cn_log_en
comfort noise log2 energy of the last SID
Definition amrwbdec.c:133
float bpf_6_7_mem[HB_FIR_SIZE]
previous values in the high-band band pass filter
Definition amrwbdec.c:119
AVLFG prng
random number generator for white noise excitation
Definition amrwbdec.c:122
int sid_frame
the current frame carries CN parameters
Definition amrwbdec.c:146
float lpf_7_mem[HB_FIR_SIZE]
previous values in the high-band low pass filter
Definition amrwbdec.c:120
float * excitation
points to current excitation in excitation_buf[]
Definition amrwbdec.c:98
float prev_tr_gain
previous initial gain used by noise enhancer for threshold
Definition amrwbdec.c:111
uint8_t fr_quality
frame quality index (FQI)
Definition amrwbdec.c:85
float samples_hb[LP_ORDER_16k+AMRWB_SFR_SIZE_16k]
high-band samples and memory from synthesis at 16kHz
Definition amrwbdec.c:115
double isp[4][LP_ORDER]
ISP vectors from current frame.
Definition amrwbdec.c:89
uint8_t pitch_lag_int
integer part of pitch lag of the previous subframe
Definition amrwbdec.c:95
uint8_t prev_ir_filter_nr
previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none
Definition amrwbdec.c:110
enum Mode fr_cur_mode
mode index of current frame
Definition amrwbdec.c:84
float isf_hist[DTX_HIST_SIZE][LP_ORDER]
ISF history used when a SID frame starts DTX.
Definition amrwbdec.c:140
float prediction_error[4]
quantified prediction errors {20log10(^gamma_gc)} for previous four subframes
Definition amrwbdec.c:103
float tilt_coef
{beta_1} related to the voicing of the previous subframe
Definition amrwbdec.c:107
int16_t cn_seed
comfort noise generator seed
Definition amrwbdec.c:137
double isp_sub4_past[LP_ORDER]
ISP vector for the 4th subframe of the previous frame.
Definition amrwbdec.c:90
float hpf_31_mem[2]
Definition amrwbdec.c:117
float pitch_vector[AMRWB_SFR_SIZE]
adaptive codebook (pitch) vector for current subframe
Definition amrwbdec.c:100
float lp_coef[4][LP_ORDER]
Linear Prediction Coefficients from ISP vector.
Definition amrwbdec.c:92
float demph_mem[1]
previous value in the de-emphasis filter
Definition amrwbdec.c:118
int data_updated
CN parameters have been renewed at least once.
Definition amrwbdec.c:148
float fixed_gain[2]
quantified fixed gains for the current and previous subframes
Definition amrwbdec.c:105
float isf_cur[LP_ORDER]
working ISF vector from current frame
Definition amrwbdec.c:86
float samples_up[UPS_MEM_SIZE+AMRWB_SFR_SIZE]
low-band samples and memory processed for upsampling
Definition amrwbdec.c:114
uint16_t isp_id[7]
index of ISP subvectors
Definition amrwbdata.h:94
uint16_t vad
voice activity detection flag
Definition amrwbdata.h:93
AMRWBSubFrame subframe[4]
data for subframes
Definition amrwbdata.h:95
uint16_t adap
adaptive codebook index
Definition amrwbdata.h:84
uint16_t vq_gain
VQ adaptive and innovative gains.
Definition amrwbdata.h:86
uint16_t hb_gain
high-band energy index (mode 23k85 only)
Definition amrwbdata.h:87
uint16_t ltp
ltp-filtering flag
Definition amrwbdata.h:85
uint16_t pul_il[4]
LSBs part of codebook index.
Definition amrwbdata.h:89
uint16_t pul_ih[4]
MSBs part of codebook index (high modes only)
Definition amrwbdata.h:88
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:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int sample_rate
samples per second
Definition avcodec.h:1044
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
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:85
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int size
static float mean(const float *input, int size)
Definition vf_nnedi.c:792
static unsigned int seed
Definition videogen.c:78
static double c[64]