FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions | Variables
amrwbdec.c File Reference

AMR wideband decoder. More...

#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/float_dsp.h"
#include "libavutil/lfg.h"
#include "avcodec.h"
#include "lsp.h"
#include "celp_filters.h"
#include "celp_math.h"
#include "acelp_filters.h"
#include "acelp_vectors.h"
#include "acelp_pitch_delay.h"
#include "internal.h"
#include "amr.h"
#include "amrwbdata.h"
#include "mips/amrwbdec_mips.h"

Go to the source code of this file.

Data Structures

struct  AMRWBContext
 

Macros

#define AMR_USE_16BIT_TABLES
 
#define BIT_STR(x, lsb, len)   (((x) >> (lsb)) & ((1 << (len)) - 1))
 Get x bits in the index interval [lsb,lsb+len-1] inclusive.
 
#define BIT_POS(x, p)   (((x) >> (p)) & 1)
 Get the bit at specified position.
 

Functions

static av_cold int amrwb_decode_init (AVCodecContext *avctx)
 
static int decode_mime_header (AMRWBContext *ctx, const uint8_t *buf)
 Decode the frame header in the "MIME/storage" format.
 
static void decode_isf_indices_36b (uint16_t *ind, float *isf_q)
 Decode quantized ISF vectors using 36-bit indexes (6K60 mode only).
 
static void decode_isf_indices_46b (uint16_t *ind, float *isf_q)
 Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode).
 
static void isf_add_mean_and_past (float *isf_q, float *isf_past)
 Apply mean and past ISF values using the prediction factor.
 
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 subframe.
 
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).
 
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.
 
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 this function.
 
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) in a subframe track using an encoded pulse indexing (TS 26.190 section 5.8.2).
 
static void decode_2p_track (int *out, int code, int m, int off)
 code: 2m+1 bits
 
static void decode_3p_track (int *out, int code, int m, int off)
 code: 3m+1 bits
 
static void decode_4p_track (int *out, int code, int m, int off)
 code: 4m bits
 
static void decode_5p_track (int *out, int code, int m, int off)
 code: 5m bits
 
static void decode_6p_track (int *out, int code, int m, int off)
 code: 6m-2 bits
 
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 codebook vector.
 
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.
 
static void pitch_sharpening (AMRWBContext *ctx, float *fixed_vector)
 Apply pitch sharpening filters to the fixed codebook vector.
 
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).
 
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 phase dispersion".
 
static float stability_factor (const float *isf, const float *isf_past)
 Calculate a stability factor {teta} based on distance between current and past isf.
 
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.
 
static void pitch_enhancer (float *fixed_vector, float voice_fac)
 Filter the fixed_vector to emphasize the higher frequencies.
 
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.
 
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)
 
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.
 
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 the Voice Activity Detection flag.
 
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 gain.
 
static float auto_correlation (float *diff_isf, float mean, int lag)
 Calculate the auto-correlation for the ISF difference vector.
 
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 frequency band.
 
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)
 
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.
 
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.
 
static void update_sub_state (AMRWBContext *ctx)
 Update context state before the next subframe.
 
static int amrwb_decode_frame (AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
 

Variables

AVCodec ff_amrwb_decoder
 

Detailed Description

AMR wideband decoder.

Definition in file amrwbdec.c.

Macro Definition Documentation

#define AMR_USE_16BIT_TABLES

Definition at line 41 of file amrwbdec.c.

#define BIT_STR (   x,
  lsb,
  len 
)    (((x) >> (lsb)) & ((1 << (len)) - 1))

Get x bits in the index interval [lsb,lsb+len-1] inclusive.

Definition at line 361 of file amrwbdec.c.

Referenced by decode_1p_track(), decode_2p_track(), decode_3p_track(), decode_4p_track(), decode_5p_track(), and decode_6p_track().

#define BIT_POS (   x,
 
)    (((x) >> (p)) & 1)

Get the bit at specified position.

Definition at line 364 of file amrwbdec.c.

Referenced by decode_1p_track(), decode_2p_track(), decode_3p_track(), decode_4p_track(), decode_5p_track(), and decode_6p_track().

Function Documentation

static av_cold int amrwb_decode_init ( AVCodecContext avctx)
static

Definition at line 96 of file amrwbdec.c.

static int decode_mime_header ( AMRWBContext ctx,
const uint8_t buf 
)
static

Decode the frame header in the "MIME/storage" format.

This format is simpler and does not carry the auxiliary frame information.

Parameters
[in]ctxThe Context
[in]bufPointer to the input buffer
Returns
The decoded header length in bytes

Definition at line 140 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void decode_isf_indices_36b ( uint16_t *  ind,
float *  isf_q 
)
static

Decode quantized ISF vectors using 36-bit indexes (6K60 mode only).

Parameters
[in]indArray of 5 indexes
[out]isf_qBuffer for isf_q[LP_ORDER]

Definition at line 156 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void decode_isf_indices_46b ( uint16_t *  ind,
float *  isf_q 
)
static

Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode).

Parameters
[in]indArray of 7 indexes
[out]isf_qBuffer for isf_q[LP_ORDER]

Definition at line 183 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void isf_add_mean_and_past ( float *  isf_q,
float *  isf_past 
)
static

Apply mean and past ISF values using the prediction factor.

Updates past ISF vector.

Parameters
[in,out]isf_qCurrent quantized ISF
[in,out]isf_pastPast quantized ISF

Definition at line 217 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void interpolate_isp ( double  isp_q[4][LP_ORDER],
const double *  isp4_past 
)
static

Interpolate the fourth ISP vector from current and past frames to obtain an ISP vector for each subframe.

Parameters
[in,out]isp_qISPs for each subframe
[in]isp4_pastPast ISP for subframe 4

Definition at line 237 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void decode_pitch_lag_high ( int *  lag_int,
int *  lag_frac,
int  pitch_index,
uint8_t base_lag_int,
int  subframe 
)
static

Decode an adaptive codebook index into pitch lag (except 6k60, 8k85 modes).

Calculate integer lag and fractional lag always using 1/4 resolution. In 1st and 3rd subframes the index is relative to last subframe integer lag.

Parameters
[out]lag_intDecoded integer pitch lag
[out]lag_fracDecoded fractional pitch lag
[in]pitch_indexAdaptive codebook pitch index
[in,out]base_lag_intBase integer lag used in relative subframes
[in]subframeCurrent subframe index (0 to 3)

Definition at line 259 of file amrwbdec.c.

Referenced by decode_pitch_vector().

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

Decode an adaptive codebook index into pitch lag for 8k85 and 6k60 modes.

The description is analogous to decode_pitch_lag_high, but in 6k60 the relative index is used for all subframes except the first.

Definition at line 292 of file amrwbdec.c.

Referenced by decode_pitch_vector().

static void decode_pitch_vector ( AMRWBContext ctx,
const AMRWBSubFrame amr_subframe,
const int  subframe 
)
static

Find the pitch vector by interpolating the past excitation at the pitch delay, which is obtained in this function.

Parameters
[in,out]ctxThe context
[in]amr_subframeCurrent subframe data
[in]subframeCurrent subframe index (0 to 3)

Definition at line 321 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void decode_1p_track ( int *  out,
int  code,
int  m,
int  off 
)
inlinestatic

The next six functions decode_[i]p_track decode exactly i pulses positions and amplitudes (-1 or 1) in a subframe track using an encoded pulse indexing (TS 26.190 section 5.8.2).

The results are given in out[], in which a negative number means amplitude -1 and vice versa (i.e., ampl(x) = x / abs(x) ).

Parameters
[out]outOutput buffer (writes i elements)
[in]codePulse index (no. of bits varies, see below)
[in]m(log2) Number of potential positions
[in]offOffset for decoded positions

code: m+1 bits

Definition at line 379 of file amrwbdec.c.

Referenced by decode_3p_track(), decode_4p_track(), decode_6p_track(), and decode_fixed_vector().

static void decode_2p_track ( int *  out,
int  code,
int  m,
int  off 
)
inlinestatic

code: 2m+1 bits

Definition at line 386 of file amrwbdec.c.

Referenced by decode_3p_track(), decode_4p_track(), decode_5p_track(), decode_6p_track(), and decode_fixed_vector().

static void decode_3p_track ( int *  out,
int  code,
int  m,
int  off 
)
static

code: 3m+1 bits

Definition at line 396 of file amrwbdec.c.

Referenced by decode_4p_track(), decode_5p_track(), decode_6p_track(), and decode_fixed_vector().

static void decode_4p_track ( int *  out,
int  code,
int  m,
int  off 
)
static

code: 4m bits

Definition at line 405 of file amrwbdec.c.

Referenced by decode_6p_track(), and decode_fixed_vector().

static void decode_5p_track ( int *  out,
int  code,
int  m,
int  off 
)
static

code: 5m bits

Definition at line 441 of file amrwbdec.c.

Referenced by decode_6p_track(), and decode_fixed_vector().

static void decode_6p_track ( int *  out,
int  code,
int  m,
int  off 
)
static

code: 6m-2 bits

Definition at line 451 of file amrwbdec.c.

Referenced by decode_fixed_vector().

static void decode_fixed_vector ( float *  fixed_vector,
const uint16_t *  pulse_hi,
const uint16_t *  pulse_lo,
const enum Mode  mode 
)
static

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

Parameters
[out]fixed_vectorBuffer for the fixed codebook excitation
[in]pulse_hiMSBs part of the pulse index array (higher modes only)
[in]pulse_loLSBs part of the pulse index array
[in]modeMode of the current frame

Definition at line 495 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void decode_gains ( const uint8_t  vq_gain,
const enum Mode  mode,
float *  fixed_gain_factor,
float *  pitch_gain 
)
static

Decode pitch gain and fixed gain correction factor.

Parameters
[in]vq_gainVector-quantized index for gains
[in]modeMode of the current frame
[out]fixed_gain_factorDecoded fixed gain correction factor
[out]pitch_gainDecoded pitch gain

Definition at line 566 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void pitch_sharpening ( AMRWBContext ctx,
float *  fixed_vector 
)
static

Apply pitch sharpening filters to the fixed codebook vector.

Parameters
[in]ctxThe context
[in,out]fixed_vectorFixed codebook excitation

Definition at line 584 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float voice_factor ( float *  p_vector,
float  p_gain,
float *  f_vector,
float  f_gain,
CELPMContext ctx 
)
static

Calculate the voicing factor (-1.0 = unvoiced to 1.0 = voiced).

Parameters
[in]p_vector,f_vectorPitch and fixed excitation vectors
[in]p_gain,f_gainPitch and fixed gains
[in]ctxThe context

Definition at line 606 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float* anti_sparseness ( AMRWBContext ctx,
float *  fixed_vector,
float *  buf 
)
static

Reduce fixed vector sparseness by smoothing with one of three IR filters, also known as "adaptive phase dispersion".

Parameters
[in]ctxThe context
[in,out]fixed_vectorUnfiltered fixed vector
[out]bufSpace for modified vector if necessary
Returns
The potentially overwritten filtered fixed vector address

Definition at line 630 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float stability_factor ( const float *  isf,
const float *  isf_past 
)
static

Calculate a stability factor {teta} based on distance between current and past isf.

A value of 1 shows maximum signal stability.

Definition at line 694 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float noise_enhancer ( float  fixed_gain,
float *  prev_tr_gain,
float  voice_fac,
float  stab_fac 
)
static

Apply a non-linear fixed gain smoothing in order to reduce fluctuation in the energy of excitation.

Parameters
[in]fixed_gainUnsmoothed fixed gain
[in,out]prev_tr_gainPrevious threshold gain (updated)
[in]voice_facFrame voicing factor
[in]stab_facFrame stability factor
Returns
The smoothed gain

Definition at line 718 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void pitch_enhancer ( float *  fixed_vector,
float  voice_fac 
)
static

Filter the fixed_vector to emphasize the higher frequencies.

Parameters
[in,out]fixed_vectorFixed codebook vector
[in]voice_facFrame voicing factor

Definition at line 745 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void synthesis ( AMRWBContext ctx,
float *  lpc,
float *  excitation,
float  fixed_gain,
const float *  fixed_vector,
float *  samples 
)
static

Conduct 16th order linear predictive coding synthesis from excitation.

Parameters
[in]ctxPointer to the AMRWBContext
[in]lpcPointer to the LPC coefficients
[out]excitationBuffer for synthesis final excitation
[in]fixed_gainFixed codebook gain for synthesis
[in]fixed_vectorAlgebraic codebook vector
[in,out]samplesPointer to the output samples and memory

Definition at line 773 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void de_emphasis ( float *  out,
float *  in,
float  m,
float  mem[1] 
)
static

Apply to synthesis a de-emphasis filter of the form: H(z) = 1 / (1 - m * z^-1)

Parameters
[out]outOutput buffer
[in]inInput samples array with in[-1]
[in]mFilter coefficient
[in,out]memState from last filtering

Definition at line 810 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void upsample_5_4 ( float *  out,
const float *  in,
int  o_size,
CELPMContext ctx 
)
static

Upsample a signal by 5/4 ratio (from 12.8kHz to 16kHz) using a FIR interpolation filter.

Uses past data from before *in address.

Parameters
[out]outBuffer for interpolated signal
[in]inCurrent signal data (length 0.8*o_size)
[in]o_sizeOutput signal length
[in]ctxThe context

Definition at line 831 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float find_hb_gain ( AMRWBContext ctx,
const float *  synth,
uint16_t  hb_idx,
uint8_t  vad 
)
static

Calculate the high-band gain based on encoded index (23k85 mode) or on the low-band speech signal and the Voice Activity Detection flag.

Parameters
[in]ctxThe context
[in]synthLB speech synthesis at 12.8k
[in]hb_idxGain index for mode 23k85 only
[in]vadVAD flag for the frame

Definition at line 863 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void scaled_hb_excitation ( AMRWBContext ctx,
float *  hb_exc,
const float *  synth_exc,
float  hb_gain 
)
static

Generate the high-band excitation with the same energy from the lower one and scaled by the given gain.

Parameters
[in]ctxThe context
[out]hb_excBuffer for the excitation
[in]synth_excLow-band excitation used for synthesis
[in]hb_gainWanted excitation gain

Definition at line 888 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static float auto_correlation ( float *  diff_isf,
float  mean,
int  lag 
)
static

Calculate the auto-correlation for the ISF difference vector.

Definition at line 907 of file amrwbdec.c.

Referenced by extrapolate_isf().

static void extrapolate_isf ( float  isf[LP_ORDER_16k])
static

Extrapolate a ISF vector to the 16kHz range (20th order LP) used at mode 6k60 LP filter for the high frequency band.

Parameters
[out]isfBuffer for extrapolated isf; contains LP_ORDER values on input

Definition at line 926 of file amrwbdec.c.

Referenced by hb_synthesis().

static void lpc_weighting ( float *  out,
const float *  lpc,
float  gamma,
int  size 
)
static

Spectral expand the LP coefficients using the equation: y[i] = x[i] * (gamma ** i)

Parameters
[out]outOutput buffer (may use input array)
[in]lpcLP coefficients array
[in]gammaWeighting factor
[in]sizeLP array size

Definition at line 991 of file amrwbdec.c.

Referenced by hb_synthesis().

static void hb_synthesis ( AMRWBContext ctx,
int  subframe,
float *  samples,
const float *  exc,
const float *  isf,
const float *  isf_past 
)
static

Conduct 20th order linear predictive coding synthesis for the high frequency band excitation at 16kHz.

Parameters
[in]ctxThe context
[in]subframeCurrent subframe index (0 to 3)
[in,out]samplesPointer to the output speech samples
[in]excGenerated white-noise scaled excitation
[in]isfCurrent frame isf vector
[in]isf_pastPast frame final isf vector

Definition at line 1013 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void hb_fir_filter ( float *  out,
const float  fir_coef[HB_FIR_SIZE+1],
float  mem[HB_FIR_SIZE],
const float *  in 
)
static

Apply a 15th order filter to high-band samples.

The filter characteristic depends on the given coefficients.

Parameters
[out]outBuffer for filtered output
[in]fir_coefFilter coefficients
[in,out]memState from last filtering (updated)
[in]inInput speech data (high-band)
Remarks
It is safe to pass the same array in in and out parameters

Definition at line 1054 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static void update_sub_state ( AMRWBContext ctx)
static

Update context state before the next subframe.

Definition at line 1076 of file amrwbdec.c.

Referenced by amrwb_decode_frame().

static int amrwb_decode_frame ( AVCodecContext avctx,
void data,
int *  got_frame_ptr,
AVPacket avpkt 
)
static

Definition at line 1092 of file amrwbdec.c.

Variable Documentation

AVCodec ff_amrwb_decoder
Initial value:
= {
.name = "amrwb",
.priv_data_size = sizeof(AMRWBContext),
.capabilities = CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("AMR-WB (Adaptive Multi-Rate WideBand)"),
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
}

Definition at line 1268 of file amrwbdec.c.