FFmpeg
Data Structures | Macros | Functions | Variables
amrnbdec.c File Reference
#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 "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 More...
 
#define AMR_SAMPLE_BOUND   32768.0
 threshold for synthesis overflow More...
 
#define AMR_SAMPLE_SCALE   (2.0 / 32768.0)
 Scale from constructed speech to [-1,1]. More...
 
#define PRED_FAC_MODE_12k2   0.65
 Prediction factor for 12.2kbit/s mode. More...
 
#define LSF_R_FAC   (8000.0 / 32768.0)
 LSF residual tables to Hertz. More...
 
#define MIN_LSF_SPACING   (50.0488 / 8000.0)
 Ensures stability of LPC filter. More...
 
#define PITCH_LAG_MIN_MODE_12k2   18
 Lower bound on decoded lag search in 12.2kbit/s mode. More...
 
#define MIN_ENERGY   -14.0
 Initial energy in dB. More...
 
#define SHARP_MAX   0.79449462890625
 Maximum sharpening factor. More...
 
#define AMR_TILT_RESPONSE   22
 Number of impulse response coefficients used for tilt factor. More...
 
#define AMR_TILT_GAMMA_T   0.8
 Tilt factor = 1st reflection coefficient * gamma_t. More...
 
#define AMR_AGC_ALPHA   0.9
 Adaptive gain control factor used in post-filter. 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() More...
 
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. More...
 
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). More...
 
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. More...
 
static void lsf2lsp_5 (AMRContext *p)
 Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors. More...
 
static void lsf2lsp_3 (AMRContext *p)
 Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector. More...
 
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. More...
 
static void decode_pitch_vector (AMRContext *p, const AMRNBSubframe *amr_subframe, const int subframe)
 
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. More...
 
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. More...
 
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. More...
 
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) More...
 
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. More...
 
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). More...
 
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). More...
 
static const floatanti_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. More...
 
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. More...
 
AMR update functions
static void update_state (AMRContext *p)
 Update buffers and history at the end of decoding a subframe. More...
 
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. More...
 
static void postfilter (AMRContext *p, float *lpc, float *buf_out)
 Perform adaptive post-filtering to enhance the quality of the speech. More...
 

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.

Definition in file amrnbdec.c.

Macro Definition Documentation

◆ AMR_BLOCK_SIZE

#define AMR_BLOCK_SIZE   160

samples per frame

Definition at line 62 of file amrnbdec.c.

◆ AMR_SAMPLE_BOUND

#define AMR_SAMPLE_BOUND   32768.0

threshold for synthesis overflow

Definition at line 63 of file amrnbdec.c.

◆ 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 74 of file amrnbdec.c.

◆ PRED_FAC_MODE_12k2

#define PRED_FAC_MODE_12k2   0.65

Prediction factor for 12.2kbit/s mode.

Definition at line 77 of file amrnbdec.c.

◆ LSF_R_FAC

#define LSF_R_FAC   (8000.0 / 32768.0)

LSF residual tables to Hertz.

Definition at line 79 of file amrnbdec.c.

◆ MIN_LSF_SPACING

#define MIN_LSF_SPACING   (50.0488 / 8000.0)

Ensures stability of LPC filter.

Definition at line 80 of file amrnbdec.c.

◆ 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 81 of file amrnbdec.c.

◆ MIN_ENERGY

#define MIN_ENERGY   -14.0

Initial energy in dB.

Also used for bad frames (unimplemented).

Definition at line 84 of file amrnbdec.c.

◆ 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 91 of file amrnbdec.c.

◆ AMR_TILT_RESPONSE

#define AMR_TILT_RESPONSE   22

Number of impulse response coefficients used for tilt factor.

Definition at line 94 of file amrnbdec.c.

◆ AMR_TILT_GAMMA_T

#define AMR_TILT_GAMMA_T   0.8

Tilt factor = 1st reflection coefficient * gamma_t.

Definition at line 96 of file amrnbdec.c.

◆ AMR_AGC_ALPHA

#define AMR_AGC_ALPHA   0.9

Adaptive gain control factor used in post-filter.

Definition at line 98 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 153 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ amrnb_decode_init()

static av_cold int amrnb_decode_init ( AVCodecContext avctx)
static

Definition at line 164 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 216 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 248 of file amrnbdec.c.

Referenced by lsf2lsp_3(), 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 269 of file amrnbdec.c.

Referenced by lsf2lsp_5().

◆ lsf2lsp_5()

static void lsf2lsp_5 ( AMRContext p)
static

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

Parameters
ppointer to the AMRContext

Definition at line 307 of file amrnbdec.c.

Referenced by amrnb_decode_frame().

◆ lsf2lsp_3()

static void lsf2lsp_3 ( AMRContext p)
static

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

Parameters
ppointer to the AMRContext

Definition at line 336 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 381 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 
)
static

Definition at line 400 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 444 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 462 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 508 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 561 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 597 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 639 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 681 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 728 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 799 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 856 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 883 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 913 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 957 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),
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
}

Definition at line 1098 of file amrnbdec.c.

AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:423
AMRChannelsContext
Definition: amrnbdec.c:148
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
amrnb_decode_frame
static int amrnb_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: amrnbdec.c:957
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
amrnb_decode_init
static av_cold int amrnb_decode_init(AVCodecContext *avctx)
Definition: amrnbdec.c:164