FFmpeg
Loading...
Searching...
No Matches
amrnbdec.c File Reference

AMR narrowband decoder. More...

#include <string.h>
#include <math.h>
#include "libavutil/channel_layout.h"
#include "avcodec.h"
#include "libavutil/common.h"
#include "libavutil/avassert.h"
#include "celp_math.h"
#include "celp_filters.h"
#include "acelp_filters.h"
#include "acelp_vectors.h"
#include "acelp_pitch_delay.h"
#include "get_bits.h"
#include "lsp.h"
#include "amr.h"
#include "codec_internal.h"
#include "decode.h"
#include "amrnbdata.h"

Go to the source code of this file.

Data Structures

struct  AMRContext
 
struct  AMRChannelsContext
 

Macros

#define AMR_BLOCK_SIZE   160
 samples per frame
 
#define AMR_SAMPLE_BOUND   32768.0
 threshold for synthesis overflow
 
#define AMR_SAMPLE_SCALE   (2.0 / 32768.0)
 Scale from constructed speech to [-1,1].
 
#define PRED_FAC_MODE_12k2   0.65
 Prediction factor for 12.2kbit/s mode.
 
#define LSF_R_FAC   (8000.0 / 32768.0)
 LSF residual tables to Hertz.
 
#define MIN_LSF_SPACING   (50.0488 / 8000.0)
 Ensures stability of LPC filter.
 
#define PITCH_LAG_MIN_MODE_12k2   18
 Lower bound on decoded lag search in 12.2kbit/s mode.
 
#define LSF_ALPHA   (29491.0f / 32768.0f)
 Weights of the past LSFs and of their mean when concealing a lost frame, ALPHA and ONE_ALPHA of 3GPP TS 26.073.
 
#define LSF_ONE_ALPHA   ( 3277.0f / 32768.0f)
 
#define LSF_ALPHA_5   (31128.0f / 32768.0f)
 
#define LSF_ONE_ALPHA_5   ( 1639.0f / 32768.0f)
 
#define MIN_ENERGY   -14.0
 Initial energy in dB.
 
#define SHARP_MAX   0.79449462890625
 Maximum sharpening factor.
 
#define AMR_TILT_RESPONSE   22
 Number of impulse response coefficients used for tilt factor.
 
#define AMR_TILT_GAMMA_T   0.8
 Tilt factor = 1st reflection coefficient * gamma_t.
 
#define AMR_AGC_ALPHA   0.9
 Adaptive gain control factor used in post-filter.
 
#define CN_LEVEL_SCALE   (1.0f / 8.0f)
 Scaling of the comfort noise excitation level, the pulses of the fixed codebook of the reference have an amplitude of 4096 in a Q15 domain.
 

Enumerations

enum  RXFrameType {
  RX_SPEECH_GOOD = 0 , RX_SPEECH_DEGRADED , RX_ONSET , RX_SPEECH_BAD ,
  RX_SID_FIRST , RX_SID_UPDATE , RX_SID_BAD , RX_NO_DATA
}
 Received frame types used by the comfort noise (DTX) state machine. More...
 
enum  DTXState { DTX_SPEECH = 0 , DTX , DTX_MUTE }
 States of the comfort noise (DTX) state machine. More...
 

Functions

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()
 
static void ec_reset (AMRContext *p)
 Reset the bad frame handling and gain concealment states, matching Decoder_amr_reset() and ec_gain_*_reset() in 3GPP TS 26.073.
 
static av_cold int amrnb_decode_init (AVCodecContext *avctx)
 
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.
 
static int pseudonoise (uint32_t *shift_reg, int no_bits)
 Comfort noise (DTX) generation, see 3GPP TS 26.092.
 
static void build_cn_code (uint32_t *seed, float *cod)
 Build the comfort noise excitation, a vector of sparse pulses.
 
static int decode_sid_frame (const uint8_t *buf, int *ind, int *mode)
 Decode the comfort noise parameters of a SID frame.
 
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 speech modes.
 
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 can reuse them.
 
static void lpc2refl (const float *lpc, float *refl)
 Compute the reflection coefficients of an LP filter with the backward Levinson recursion, they give the prediction gain of the filter.
 
static int dtx_handler (AMRContext *p, int frame_type)
 Update the comfort noise state machine for a received frame.
 
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 the comfort noise.
 
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 the LSF and energy history.
 
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 speech decoder starts from.
 
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.
 
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.
 
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 3GPP TS 26.073 (b_cn_cod.c:145-162).
 
static int amrnb_decode_frame (AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
 
AMR pitch LPC coefficient decoding functions
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).
 
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.
 
static void lsf2lsp_5 (AMRContext *p, int bfi)
 Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors.
 
static void lsf2lsp_3 (AMRContext *p, int bfi)
 Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector.
 
AMR pitch vector decoding functions
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.
 
static void decode_pitch_vector (AMRContext *p, const AMRNBSubframe *amr_subframe, const int subframe, const int bfi)
 
AMR algebraic code book (fixed) vector decoding functions
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.
 
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 vector for MODE_10k2.
 
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 codebook vector.
 
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)
 
AMR gain decoding functions
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.1.4, in fact frequencies should be used.
 
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).
 
AMR error concealment functions
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.c:57-84).
 
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.c:306-330).
 
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.c:343-379).
 
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() followed by gc_pred_update(), as ec_gain_code() does in 3GPP TS 26.073 when the energies of the frame are not available.
 
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.c:137-171).
 
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.c:184-219).
 
AMR preprocessing functions
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.6.2 of G.729 and 6.1.5 of AMR).
 
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.
 
AMR synthesis functions
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.
 
AMR update functions
static void update_state (AMRContext *p)
 Update buffers and history at the end of decoding a subframe.
 
AMR Postprocessing functions
static float tilt_factor (AMRContext *p, float *lpc_n, float *lpc_d)
 Get the tilt factor of a formant filter from its transfer function.
 
static void postfilter (AMRContext *p, float *lpc, float *buf_out)
 Perform adaptive post-filtering to enhance the quality of the speech.
 

Variables

const FFCodec ff_amrnb_decoder
 

Detailed Description

AMR narrowband decoder.

This decoder uses floats for simplicity and so is not bit-exact. One difference is that differences in phase can accumulate. The test sequences in 3GPP TS 26.074 can still be useful.

  • Comparing this file's output to the output of the ref decoder gives a PSNR of 30 to 80. Plotting the output samples shows a difference in phase in some areas.
  • Comparing both decoders against their input, this decoder gives a similar PSNR. If the test sequence homing frames are removed (this decoder does not detect them), the PSNR is at least as good as the reference on 140 out of 169 tests.

Definition in file amrnbdec.c.

Macro Definition Documentation

◆ AMR_BLOCK_SIZE

#define AMR_BLOCK_SIZE   160

samples per frame

Definition at line 63 of file amrnbdec.c.

Referenced by amrnb_decode_frame(), amrnb_decode_init(), cn_synthesize(), and dtx_activity_update().

◆ AMR_SAMPLE_BOUND

#define AMR_SAMPLE_BOUND   32768.0

threshold for synthesis overflow

Definition at line 64 of file amrnbdec.c.

Referenced by synthesis().

◆ AMR_SAMPLE_SCALE

#define AMR_SAMPLE_SCALE   (2.0 / 32768.0)

Scale from constructed speech to [-1,1].

AMR is designed to produce 16-bit PCM samples (3GPP TS 26.090 4.2) but upscales by two (section 6.2.2).

Fundamentally, this scale is determined by energy_mean through the fixed vector contribution to the excitation vector.

Definition at line 75 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ PRED_FAC_MODE_12k2

#define PRED_FAC_MODE_12k2   0.65

Prediction factor for 12.2kbit/s mode.

Definition at line 78 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ LSF_R_FAC

#define LSF_R_FAC   (8000.0 / 32768.0)

LSF residual tables to Hertz.

Definition at line 80 of file amrnbdec.c.

Referenced by decode_lsf_noise(), lsf2lsp_3(), lsf2lsp_5(), and lsf2lsp_for_mode12k2().

◆ MIN_LSF_SPACING

#define MIN_LSF_SPACING   (50.0488 / 8000.0)

Ensures stability of LPC filter.

Definition at line 81 of file amrnbdec.c.

Referenced by cn_frame(), cn_reuse_speech_history(), decode_lsf_noise(), lsf2lsp_3(), lsf2lsp_5(), and lsf2lsp_for_mode12k2().

◆ PITCH_LAG_MIN_MODE_12k2

#define PITCH_LAG_MIN_MODE_12k2   18

Lower bound on decoded lag search in 12.2kbit/s mode.

Definition at line 82 of file amrnbdec.c.

Referenced by decode_pitch_lag_1_6().

◆ LSF_ALPHA

#define LSF_ALPHA   (29491.0f / 32768.0f)

Weights of the past LSFs and of their mean when concealing a lost frame, ALPHA and ONE_ALPHA of 3GPP TS 26.073.

The 12.2 kbit/s mode uses a different pair than all the other modes do:

D_plsf_3() d_plsf_3.c:51-52 29491 / 3277 (0.90 / 0.10) D_plsf_5() d_plsf_5.c:49-50 31128 / 1639 (0.95 / 0.05)

Definition at line 92 of file amrnbdec.c.

Referenced by lsf2lsp_3().

◆ LSF_ONE_ALPHA

#define LSF_ONE_ALPHA   ( 3277.0f / 32768.0f)

Definition at line 93 of file amrnbdec.c.

Referenced by lsf2lsp_3().

◆ LSF_ALPHA_5

#define LSF_ALPHA_5   (31128.0f / 32768.0f)

Definition at line 94 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ LSF_ONE_ALPHA_5

#define LSF_ONE_ALPHA_5   ( 1639.0f / 32768.0f)

Definition at line 95 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ MIN_ENERGY

#define MIN_ENERGY   -14.0

Initial energy in dB.

Also used for bad frames (unimplemented).

Definition at line 98 of file amrnbdec.c.

Referenced by amrnb_decode_init(), amrwb_decode_init(), cn_update_from_sid(), and ec_gain_code_predictor_update().

◆ SHARP_MAX

#define SHARP_MAX   0.79449462890625

Maximum sharpening factor.

The specification says 0.8, which should be 13107, but the reference C code uses 13017 instead. (Amusingly the same applies to SHARP_MAX in g729dec.c.)

Definition at line 105 of file amrnbdec.c.

Referenced by decode_frame(), pitch_sharpening(), and synthesis().

◆ AMR_TILT_RESPONSE

#define AMR_TILT_RESPONSE   22

Number of impulse response coefficients used for tilt factor.

Definition at line 108 of file amrnbdec.c.

Referenced by tilt_factor().

◆ AMR_TILT_GAMMA_T

#define AMR_TILT_GAMMA_T   0.8

Tilt factor = 1st reflection coefficient * gamma_t.

Definition at line 110 of file amrnbdec.c.

Referenced by tilt_factor().

◆ AMR_AGC_ALPHA

#define AMR_AGC_ALPHA   0.9

Adaptive gain control factor used in post-filter.

Definition at line 112 of file amrnbdec.c.

Referenced by postfilter().

◆ CN_LEVEL_SCALE

#define CN_LEVEL_SCALE   (1.0f / 8.0f)

Scaling of the comfort noise excitation level, the pulses of the fixed codebook of the reference have an amplitude of 4096 in a Q15 domain.

Definition at line 118 of file amrnbdec.c.

Referenced by cn_frame().

Enumeration Type Documentation

◆ RXFrameType

Received frame types used by the comfort noise (DTX) state machine.

Enumerator
RX_SPEECH_GOOD 
RX_SPEECH_DEGRADED 
RX_ONSET 
RX_SPEECH_BAD 
RX_SID_FIRST 
RX_SID_UPDATE 
RX_SID_BAD 
RX_NO_DATA 

Definition at line 121 of file amrnbdec.c.

◆ DTXState

enum DTXState

States of the comfort noise (DTX) state machine.

Enumerator
DTX_SPEECH 
DTX 
DTX_MUTE 

Definition at line 133 of file amrnbdec.c.

Function Documentation

◆ weighted_vector_sumd()

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 )
static

Double version of ff_weighted_vector_sumf()

Definition at line 221 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ ec_reset()

static void ec_reset ( AMRContext * p)
static

Reset the bad frame handling and gain concealment states, matching Decoder_amr_reset() and ec_gain_*_reset() in 3GPP TS 26.073.

Definition at line 236 of file amrnbdec.c.

Referenced by amrnb_decode_init(), and cn_synthesize().

◆ amrnb_decode_init()

static av_cold int amrnb_decode_init ( AVCodecContext * avctx)
static

Definition at line 256 of file amrnbdec.c.

◆ unpack_bitstream()

static enum Mode unpack_bitstream ( AMRContext * p,
const uint8_t * buf,
int buf_size )
static

Unpack an RFC4867 speech frame into the AMR frame mode and parameters.

The order of speech bits is specified by 3GPP TS 26.101.

Parameters
pthe context
bufpointer to the input buffer
buf_sizesize of the input buffer
Returns
the frame mode

Definition at line 331 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ interpolate_lsf()

static void interpolate_lsf ( ACELPVContext * ctx,
float lsf_q[4][LP_FILTER_ORDER],
float * lsf_new )
static

Interpolate the LSF vector (used for fixed gain smoothing).

The interpolation is done over all four subframes even in MODE_12k2.

Parameters
[in]ctxThe Context
[in,out]lsf_qLSFs in [0,1] for each subframe
[in]lsf_newNew LSFs in [0,1] for subframe 4

Definition at line 363 of file amrnbdec.c.

Referenced by lsf2lsp_3(), lsf2lsp_5(), and lsf2lsp_for_mode12k2().

◆ lsf2lsp_for_mode12k2()

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 )
static

Decode a set of 5 split-matrix quantized lsf indexes into an lsp vector.

Parameters
pthe context
lspoutput LSP vector
lsf_no_rLSF vector without the residual vector added
lsf_quantizerpointers to LSF dictionary tables
quantizer_offsetoffset in tables
signfor the 3 dictionary table
updatestore data for computing the next frame's LSFs

Definition at line 384 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ lsf2lsp_5()

static void lsf2lsp_5 ( AMRContext * p,
int bfi )
static

Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors.

Parameters
ppointer to the AMRContext
bfithe frame is unusable, conceal the LSF vector

Definition at line 423 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ lsf2lsp_3()

static void lsf2lsp_3 ( AMRContext * p,
int bfi )
static

Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector.

Parameters
ppointer to the AMRContext
bfithe frame is unusable, conceal the LSF vector

Definition at line 482 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ decode_pitch_lag_1_6()

static void decode_pitch_lag_1_6 ( int * lag_int,
int * lag_frac,
int pitch_index,
const int prev_lag_int,
const int subframe )
static

Like ff_decode_pitch_lag(), but with 1/6 resolution.

Definition at line 555 of file amrnbdec.c.

Referenced by decode_pitch_vector().

◆ decode_pitch_vector()

static void decode_pitch_vector ( AMRContext * p,
const AMRNBSubframe * amr_subframe,
const int subframe,
const int bfi )
static

Definition at line 574 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ decode_10bit_pulse()

static void decode_10bit_pulse ( int code,
int pulse_position[8],
int i1,
int i2,
int i3 )
static

Decode a 10-bit algebraic codebook index from a 10.2 kbit/s frame.

Definition at line 628 of file amrnbdec.c.

Referenced by decode_8_pulses_31bits().

◆ decode_8_pulses_31bits()

static void decode_8_pulses_31bits ( const int16_t * fixed_index,
AMRFixed * fixed_sparse )
static

Decode the algebraic codebook index to pulse positions and signs and construct the algebraic codebook vector for MODE_10k2.

Parameters
fixed_indexpositions of the eight pulses
fixed_sparsepointer to the algebraic codebook vector

Definition at line 646 of file amrnbdec.c.

Referenced by decode_fixed_sparse().

◆ decode_fixed_sparse()

static void decode_fixed_sparse ( AMRFixed * fixed_sparse,
const uint16_t * pulses,
const enum Mode mode,
const int subframe )
static

Decode the algebraic codebook index to pulse positions and signs, then construct the algebraic codebook vector.

                         nb of pulses | bits encoding pulses

For MODE_4k75 or MODE_5k15, 2 | 1-3, 4-6, 7 MODE_5k9, 2 | 1, 2-4, 5-6, 7-9 MODE_6k7, 3 | 1-3, 4, 5-7, 8, 9-11 MODE_7k4 or MODE_7k95, 4 | 1-3, 4-6, 7-9, 10, 11-13

Parameters
fixed_sparsepointer to the algebraic codebook vector
pulsesalgebraic codebook indexes
modemode of the current frame
subframecurrent subframe number

Definition at line 692 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ pitch_sharpening()

static void pitch_sharpening ( AMRContext * p,
int subframe,
enum Mode mode,
AMRFixed * fixed_sparse )
static

Apply pitch lag to obtain the sharpened fixed vector (section 6.1.2)

Parameters
pthe context
subframeunpacked amr subframe
modemode of the current frame
fixed_sparsesparse representation of the fixed vector

Definition at line 745 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ fixed_gain_smooth()

static float fixed_gain_smooth ( AMRContext * p,
const float * lsf,
const float * lsf_avg,
const enum Mode mode )
static

fixed gain smoothing Note that where the spec specifies the "spectrum in the q domain" in section 6.1.4, in fact frequencies should be used.

Parameters
pthe context
lsfLSFs for the current subframe, in the range [0,1]
lsf_avgaveraged LSFs
modemode of the current frame
Returns
fixed gain smoothed

Definition at line 781 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ decode_gains()

static void decode_gains ( AMRContext * p,
const AMRNBSubframe * amr_subframe,
const enum Mode mode,
const int subframe,
float * fixed_gain_factor )
static

Decode pitch gain and fixed gain factor (part of section 6.1.3).

Parameters
pthe context
amr_subframeunpacked amr subframe
modemode of the current frame
subframecurrent subframe number
fixed_gain_factordecoded gain correction factor

Definition at line 823 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ ec_gain_median()

static float ec_gain_median ( const float * buf)
static

Median of the last five gain values, equivalent to gmed_n() in 3GPP TS 26.073 (gmed_n.c:57-84).

Definition at line 859 of file amrnbdec.c.

Referenced by ec_gain_code(), and ec_gain_pitch().

◆ ec_gain_pitch()

static void ec_gain_pitch ( AMRContext * p,
int state )
static

Conceal the pitch gain of a lost subframe, ec_gain_pitch() in 3GPP TS 26.073 (ec_gains.c:306-330).

Parameters
pthe context
statestate of the bad frame handling state machine

Definition at line 884 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ ec_gain_pitch_update()

static void ec_gain_pitch_update ( AMRContext * p,
int bfi )
static

Update the pitch gain concealment state, ec_gain_pitch_update() in 3GPP TS 26.073 (ec_gains.c:343-379).

Parameters
pthe context
bfithe current frame is unusable

Definition at line 906 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ ec_gain_code_predictor_update()

static void ec_gain_code_predictor_update ( AMRContext * p)
static

Advance the gain predictor with the average of the four past energies, gc_pred_average_limited() followed by gc_pred_update(), as ec_gain_code() does in 3GPP TS 26.073 when the energies of the frame are not available.

Parameters
pthe context

Definition at line 932 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ ec_gain_code()

static void ec_gain_code ( AMRContext * p,
int state )
static

Conceal the codebook gain of a lost subframe, ec_gain_code() in 3GPP TS 26.073 (ec_gains.c:137-171).

Parameters
pthe context
statestate of the bad frame handling state machine

Definition at line 953 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ ec_gain_code_update()

static void ec_gain_code_update ( AMRContext * p,
int bfi )
static

Update the codebook gain concealment state, ec_gain_code_update() in 3GPP TS 26.073 (ec_gains.c:184-219).

Parameters
pthe context
bfithe current frame is unusable

Definition at line 975 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ apply_ir_filter()

static void apply_ir_filter ( float * out,
const AMRFixed * in,
const float * filter )
static

Circularly convolve a sparse fixed vector with a phase dispersion impulse response filter (D.6.2 of G.729 and 6.1.5 of AMR).

Parameters
outvector with filter applied
insource vector
filterphase filter coefficients

out[n] = sum(i,0,len-1){ in[i] * filter[(len + n - i)len] }

< filters at pitch lag*1 and *2

Definition at line 1008 of file amrnbdec.c.

Referenced by anti_sparseness().

◆ anti_sparseness()

static const float * anti_sparseness ( AMRContext * p,
AMRFixed * fixed_sparse,
const float * fixed_vector,
float fixed_gain,
float * out )
static

Reduce fixed vector sparseness by smoothing with one of three IR filters.

Also know as "adaptive phase dispersion".

This implements 3GPP TS 26.090 section 6.1(5).

Parameters
pthe context
fixed_sparsealgebraic codebook vector
fixed_vectorunfiltered fixed vector
fixed_gainsmoothed gain
outspace for modified vector if necessary

Definition at line 1055 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ synthesis()

static int synthesis ( AMRContext * p,
float * lpc,
float fixed_gain,
const float * fixed_vector,
float * samples,
uint8_t overflow )
static

Conduct 10th order linear predictive coding synthesis.

Parameters
ppointer to the AMRContext
lpcpointer to the LPC coefficients
fixed_gainfixed codebook gain for synthesis
fixed_vectoralgebraic codebook vector
samplespointer to the output speech samples
overflow16-bit overflow flag

Definition at line 1126 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ update_state()

static void update_state ( AMRContext * p)
static

Update buffers and history at the end of decoding a subframe.

Parameters
ppointer to the AMRContext

Definition at line 1183 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ tilt_factor()

static float tilt_factor ( AMRContext * p,
float * lpc_n,
float * lpc_d )
static

Get the tilt factor of a formant filter from its transfer function.

Parameters
pThe Context
lpc_nLP_FILTER_ORDER coefficients of the numerator
lpc_dLP_FILTER_ORDER coefficients of the denominator

Definition at line 1210 of file amrnbdec.c.

Referenced by postfilter().

◆ postfilter()

static void postfilter ( AMRContext * p,
float * lpc,
float * buf_out )
static

Perform adaptive post-filtering to enhance the quality of the speech.

See section 6.2.1.

Parameters
ppointer to the AMRContext
lpcinterpolated LP coefficients for this subframe
buf_outoutput of the filter

Definition at line 1240 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ pseudonoise()

static int pseudonoise ( uint32_t * shift_reg,
int no_bits )
static

Comfort noise (DTX) generation, see 3GPP TS 26.092.

As for AMR-WB the implementation follows the decoder of the reference codec (3GPP TS 26.073) with the fixed point arithmetic replaced by floating point: LSF vectors are kept in the same unit as in the speech path (normalized frequency) and energies as log2 values. Draw a value from the pseudo noise generator, see 3GPP TS 26.073.

Parameters
[in,out]shift_regState of the shift register
[in]no_bitsNumber of bits to draw

Definition at line 1300 of file amrnbdec.c.

Referenced by build_cn_code(), and cn_frame().

◆ build_cn_code()

static void build_cn_code ( uint32_t * seed,
float * cod )
static

Build the comfort noise excitation, a vector of sparse pulses.

Parameters
[in,out]seedState of the pseudo noise generator
[out]codExcitation for one subframe

Definition at line 1322 of file amrnbdec.c.

Referenced by cn_synthesize().

◆ decode_sid_frame()

static int decode_sid_frame ( const uint8_t * buf,
int * ind,
int * mode )
static

Decode the comfort noise parameters of a SID frame.

The payload carries 39 bits: five parameters (3, 8, 9, 9 and 6 bits), a flag telling whether this is the first SID frame of a silent period and the speech mode used before the silence.

Parameters
[out]indThe five SID parameters
[out]modeSpeech mode used before the silence
Returns
non zero if this is a SID update frame

Definition at line 1346 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ decode_lsf_noise()

static void decode_lsf_noise ( const int * ind,
float * lsf_q )
static

Decode the LSF vector of a SID frame, the comfort noise quantizer does not use the prediction of the speech modes.

Parameters
[in]indThe five SID parameters
[out]lsf_qDecoded LSF vector

Definition at line 1374 of file amrnbdec.c.

Referenced by cn_update_from_sid().

◆ dtx_activity_update()

static void dtx_activity_update ( AMRContext * p,
const float * synth )
static

Store the LSF vector and the energy of a speech frame so that the first SID frame of a silent period can reuse them.

Parameters
[in,out]pThe Context
[in]synthSynthesis of the decoded frame

Definition at line 1403 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ lpc2refl()

static void lpc2refl ( const float * lpc,
float * refl )
static

Compute the reflection coefficients of an LP filter with the backward Levinson recursion, they give the prediction gain of the filter.

Parameters
[in]lpcLP coefficients of the filter
[out]reflReflection coefficients

Definition at line 1431 of file amrnbdec.c.

Referenced by pred_gain_log().

◆ dtx_handler()

static int dtx_handler ( AMRContext * p,
int frame_type )
static

Update the comfort noise state machine for a received frame.

Parameters
[in,out]pThe Context
[in]frame_typeType of the received frame
Returns
the DTX state to decode this frame with

Definition at line 1458 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ pred_gain_log()

static float pred_gain_log ( const float * lpc)
static

Compute the logarithm of the prediction gain of an LP filter, it is used to normalize the level of the comfort noise.

Parameters
[in]lpcLP coefficients of the filter
Returns
log2 of the amplitude gained by filtering, negated

Definition at line 1542 of file amrnbdec.c.

Referenced by cn_frame().

◆ cn_reuse_speech_history()

static void cn_reuse_speech_history ( AMRContext * p,
int mode )
static

Reuse the noise parameters of the speech frames preceding a silent period, they are the average of the LSF and energy history.

Parameters
[in,out]pThe Context
[in]modeSpeech mode used before the silence

Definition at line 1563 of file amrnbdec.c.

Referenced by cn_frame().

◆ cn_update_from_sid()

static void cn_update_from_sid ( AMRContext * p,
const int * ind )
static

Update the comfort noise parameters with the ones carried by a SID frame and reset the state the speech decoder starts from.

Parameters
[in,out]pThe Context
[in]indSID parameters of this frame

Definition at line 1629 of file amrnbdec.c.

Referenced by cn_frame().

◆ cn_synthesize()

static void cn_synthesize ( AMRContext * p,
const float * lpc_variab,
float level,
int new_state,
float * buf_out )
static

Synthesize one frame of comfort noise into the output buffer.

Parameters
[in,out]pThe Context
[in]lpc_variabLP coefficients of the dithered filter
[in]levelAmplitude of the excitation
[in]new_stateDTX state of this frame
[out]buf_outSynthesis of the frame, before filtering

Definition at line 1680 of file amrnbdec.c.

Referenced by cn_frame().

◆ cn_frame()

static void cn_frame ( AMRContext * p,
int new_state,
const int * ind,
int mode,
float * buf_out )
static

Decode one frame of comfort noise into the output buffer.

Parameters
[in,out]pThe Context
[in]new_stateDTX state of this frame
[in]indSID parameters of this frame
[in]modeSpeech mode used before the silence
[out]buf_outSynthesis of the frame, before filtering

Definition at line 1754 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ build_bad_frame_params()

static void build_bad_frame_params ( AMRContext * p,
uint32_t * seed,
enum Mode mode )
static

Fill the frame parameters of a lost speech frame with pseudo-random values, see build_CN_param() in 3GPP TS 26.073 (b_cn_cod.c:145-162).

Only the fields the concealment does not itself overwrite are actually consumed (notably the fixed codebook pulses). The field order follows the AMRNBFrame order table, not the reference's bitstream parameter order, so this is deliberately not bit-exact with the reference – the decoder is float and not bit-exact either (see the file header).

Parameters
pthe context
seedstate of the pseudo-random generator
modespeech mode of the lost frame

Definition at line 1870 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ amrnb_decode_frame()

static int amrnb_decode_frame ( AVCodecContext * avctx,
AVFrame * frame,
int * got_frame_ptr,
AVPacket * avpkt )
static

Definition at line 1890 of file amrnbdec.c.

Variable Documentation

◆ ff_amrnb_decoder

const FFCodec ff_amrnb_decoder
Initial value:
= {
.p.name = "amrnb",
CODEC_LONG_NAME("AMR-NB (Adaptive Multi-Rate NarrowBand)"),
.p.type = AVMEDIA_TYPE_AUDIO,
.priv_data_size = sizeof(AMRChannelsContext),
}
static int amrnb_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition amrnbdec.c:1890
static av_cold int amrnb_decode_init(AVCodecContext *avctx)
Definition amrnbdec.c:256
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
#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:440
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201

Definition at line 2156 of file amrnbdec.c.