Libav
Data Structures | Macros | Enumerations | Functions | Variables
g723_1.c File Reference

G.723.1 compatible decoder. More...

#include "libavutil/channel_layout.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "get_bits.h"
#include "acelp_vectors.h"
#include "celp_filters.h"
#include "g723_1_data.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

struct  G723_1_Subframe
 G723.1 unpacked data subframe. More...
 
struct  PPFParam
 Pitch postfilter parameters. More...
 
struct  G723_1_Context
 

Macros

#define BITSTREAM_READER_LE
 
#define CNG_RANDOM_SEED   12345
 
#define MULL2(a, b)   ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
 Bitexact implementation of 2ab scaled by 1/2^16. More...
 
#define OFFSET(x)   offsetof(G723_1_Context, x)
 
#define AD   AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 

Enumerations

enum  FrameType {
  ACTIVE_FRAME, SID_FRAME, UNTRANSMITTED_FRAME, INTRA_FRAME = 0,
  INTER_FRAME, SKIP_FRAME
}
 G723.1 frame types. More...
 
enum  Rate { RATE_6300, RATE_5300 }
 

Functions

static av_cold int g723_1_decode_init (AVCodecContext *avctx)
 
static int unpack_bitstream (G723_1_Context *p, const uint8_t *buf, int buf_size)
 Unpack the frame into parameters. More...
 
static int16_t square_root (int val)
 Bitexact implementation of sqrt(val/2). More...
 
static int normalize_bits (int num, int width)
 Calculate the number of left-shifts required for normalizing the input. More...
 
static int scale_vector (int16_t *dst, const int16_t *vector, int length)
 Scale vector contents based on the largest of their absolutes. More...
 
static void inverse_quant (int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
 Perform inverse quantization of LSP frequencies. More...
 
static void lsp2lpc (int16_t *lpc)
 Convert LSP frequencies to LPC coefficients. More...
 
static void lsp_interpolate (int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
 Quantize LSP frequencies by interpolation and convert them to the corresponding LPC coefficients. More...
 
static void gen_dirac_train (int16_t *buf, int pitch_lag)
 Generate a train of dirac functions with period as pitch lag. More...
 
static void gen_fcb_excitation (int16_t *vector, G723_1_Subframe *subfrm, enum Rate cur_rate, int pitch_lag, int index)
 Generate fixed codebook excitation vector. More...
 
static void get_residual (int16_t *residual, int16_t *prev_excitation, int lag)
 Get delayed contribution from the previous excitation vector. More...
 
static int dot_product (const int16_t *a, const int16_t *b, int length)
 
static void gen_acb_excitation (int16_t *vector, int16_t *prev_excitation, int pitch_lag, G723_1_Subframe *subfrm, enum Rate cur_rate)
 Generate adaptive codebook excitation. More...
 
static int autocorr_max (const int16_t *buf, int offset, int *ccr_max, int pitch_lag, int length, int dir)
 Estimate maximum auto-correlation around pitch lag. More...
 
static void comp_ppf_gains (int lag, PPFParam *ppf, enum Rate cur_rate, int tgt_eng, int ccr, int res_eng)
 Calculate pitch postfilter optimal and scaling gains. More...
 
static void comp_ppf_coeff (G723_1_Context *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
 Calculate pitch postfilter parameters. More...
 
static int comp_interp_index (G723_1_Context *p, int pitch_lag, int *exc_eng, int *scale)
 Classify frames as voiced/unvoiced. More...
 
static void residual_interp (int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
 Peform residual interpolation based on frame classification. More...
 
static void iir_filter (int16_t *fir_coef, int16_t *iir_coef, int16_t *src, int *dest)
 Perform IIR filtering. More...
 
static void gain_scale (G723_1_Context *p, int16_t *buf, int energy)
 Adjust gain of postfiltered signal. More...
 
static void formant_postfilter (G723_1_Context *p, int16_t *lpc, int16_t *buf, int16_t *dst)
 Perform formant filtering. More...
 
static int sid_gain_to_lsp_index (int gain)
 
static int cng_rand (int *state, int base)
 
static int estimate_sid_gain (G723_1_Context *p)
 
static void generate_noise (G723_1_Context *p)
 
static int g723_1_decode_frame (AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
 

Variables

static const AVOption options []
 
static const AVClass g723_1dec_class
 
AVCodec ff_g723_1_decoder
 

Detailed Description

G.723.1 compatible decoder.

Definition in file g723_1.c.

Macro Definition Documentation

#define BITSTREAM_READER_LE

Definition at line 28 of file g723_1.c.

#define CNG_RANDOM_SEED   12345

Definition at line 39 of file g723_1.c.

Referenced by g723_1_decode_frame(), and g723_1_decode_init().

#define MULL2 (   a,
  b 
)    ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))

Bitexact implementation of 2ab scaled by 1/2^16.

Parameters
a32 bit multiplicand
b16 bit multiplier

Definition at line 373 of file g723_1.c.

Referenced by lsp2lpc().

#define OFFSET (   x)    offsetof(G723_1_Context, x)

Definition at line 1351 of file g723_1.c.

Definition at line 1352 of file g723_1.c.

Enumeration Type Documentation

enum FrameType

G723.1 frame types.

Enumerator
ACTIVE_FRAME 

Active speech.

SID_FRAME 

Silence Insertion Descriptor frame.

UNTRANSMITTED_FRAME 
INTRA_FRAME 
INTER_FRAME 
SKIP_FRAME 

Definition at line 44 of file g723_1.c.

enum Rate
Enumerator
RATE_6300 
RATE_5300 

Definition at line 50 of file g723_1.c.

Function Documentation

static av_cold int g723_1_decode_init ( AVCodecContext avctx)
static

Definition at line 109 of file g723_1.c.

static int unpack_bitstream ( G723_1_Context p,
const uint8_t buf,
int  buf_size 
)
static

Unpack the frame into parameters.

Parameters
pthe context
bufpointer to the input buffer
buf_sizesize of the input buffer

Definition at line 135 of file g723_1.c.

Referenced by g723_1_decode_frame().

static int16_t square_root ( int  val)
static

Bitexact implementation of sqrt(val/2).

Definition at line 250 of file g723_1.c.

Referenced by comp_ppf_gains(), gain_scale(), and generate_noise().

static int normalize_bits ( int  num,
int  width 
)
static

Calculate the number of left-shifts required for normalizing the input.

Parameters
numinput number
widthwidth of the input, 16 bits(0) / 32 bits(1)

Definition at line 271 of file g723_1.c.

Referenced by comp_ppf_coeff(), gain_scale(), and scale_vector().

static int scale_vector ( int16_t *  dst,
const int16_t *  vector,
int  length 
)
static

Scale vector contents based on the largest of their absolutes.

Definition at line 279 of file g723_1.c.

Referenced by comp_interp_index(), and formant_postfilter().

static void inverse_quant ( int16_t *  cur_lsp,
int16_t *  prev_lsp,
uint8_t lsp_index,
int  bad_frame 
)
static

Perform inverse quantization of LSP frequencies.

Parameters
cur_lspthe current LSP vector
prev_lspthe previous LSP vector
lsp_indexVQ indices
bad_framebad frame flag

Definition at line 305 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void lsp2lpc ( int16_t *  lpc)
static

Convert LSP frequencies to LPC coefficients.

Parameters
lpcbuffer for LPC coefficients

Definition at line 381 of file g723_1.c.

Referenced by lsp_interpolate().

static void lsp_interpolate ( int16_t *  lpc,
int16_t *  cur_lsp,
int16_t *  prev_lsp 
)
static

Quantize LSP frequencies by interpolation and convert them to the corresponding LPC coefficients.

Parameters
lpcbuffer for LPC coefficients
cur_lspthe current LSP vector
prev_lspthe previous LSP vector

Definition at line 451 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void gen_dirac_train ( int16_t *  buf,
int  pitch_lag 
)
static

Generate a train of dirac functions with period as pitch lag.

Definition at line 474 of file g723_1.c.

Referenced by gen_fcb_excitation().

static void gen_fcb_excitation ( int16_t *  vector,
G723_1_Subframe subfrm,
enum Rate  cur_rate,
int  pitch_lag,
int  index 
)
static

Generate fixed codebook excitation vector.

Parameters
vectordecoded excitation vector
subfrmcurrent subframe
cur_ratecurrent bitrate
pitch_lagclosed loop pitch lag
indexcurrent subframe index

Definition at line 495 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void get_residual ( int16_t *  residual,
int16_t *  prev_excitation,
int  lag 
)
static

Get delayed contribution from the previous excitation vector.

Definition at line 555 of file g723_1.c.

Referenced by gen_acb_excitation().

static int dot_product ( const int16_t *  a,
const int16_t *  b,
int  length 
)
static
static void gen_acb_excitation ( int16_t *  vector,
int16_t *  prev_excitation,
int  pitch_lag,
G723_1_Subframe subfrm,
enum Rate  cur_rate 
)
static

Generate adaptive codebook excitation.

Definition at line 582 of file g723_1.c.

Referenced by g723_1_decode_frame(), and generate_noise().

static int autocorr_max ( const int16_t *  buf,
int  offset,
int *  ccr_max,
int  pitch_lag,
int  length,
int  dir 
)
static

Estimate maximum auto-correlation around pitch lag.

Parameters
bufbuffer with offset applied
offsetoffset of the excitation vector
ccr_maxpointer to the maximum auto-correlation
pitch_lagdecoded pitch lag
lengthlength of autocorrelation
dirforward lag(1) / backward lag(-1)

Definition at line 619 of file g723_1.c.

Referenced by comp_interp_index(), and comp_ppf_coeff().

static void comp_ppf_gains ( int  lag,
PPFParam ppf,
enum Rate  cur_rate,
int  tgt_eng,
int  ccr,
int  res_eng 
)
static

Calculate pitch postfilter optimal and scaling gains.

Parameters
lagpitch postfilter forward/backward lag
ppfpitch postfilter parameters
cur_ratecurrent bitrate
tgt_engtarget energy
ccrcross-correlation
res_engresidual energy

Definition at line 652 of file g723_1.c.

Referenced by comp_ppf_coeff().

static void comp_ppf_coeff ( G723_1_Context p,
int  offset,
int  pitch_lag,
PPFParam ppf,
enum Rate  cur_rate 
)
static

Calculate pitch postfilter parameters.

Parameters
pthe context
offsetoffset of the excitation vector
pitch_lagdecoded pitch lag
ppfpitch postfilter parameters
cur_ratecurrent bitrate

Definition at line 700 of file g723_1.c.

Referenced by g723_1_decode_frame().

static int comp_interp_index ( G723_1_Context p,
int  pitch_lag,
int *  exc_eng,
int *  scale 
)
static

Classify frames as voiced/unvoiced.

Parameters
pthe context
pitch_lagdecoded pitch_lag
exc_engexcitation energy estimation
scalescaling factor of exc_eng
Returns
residual interpolation index if voiced, 0 otherwise

Definition at line 784 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void residual_interp ( int16_t *  buf,
int16_t *  out,
int  lag,
int  gain,
int *  rseed 
)
static

Peform residual interpolation based on frame classification.

Parameters
bufdecoded excitation vector
outoutput vector
lagdecoded pitch lag
gaininterpolated gain
rseedseed for random number generator

Definition at line 828 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void iir_filter ( int16_t *  fir_coef,
int16_t *  iir_coef,
int16_t *  src,
int *  dest 
)
inlinestatic

Perform IIR filtering.

Parameters
fir_coefFIR coefficients
iir_coefIIR coefficients
srcsource vector
destdestination vector

Definition at line 856 of file g723_1.c.

Referenced by formant_postfilter().

static void gain_scale ( G723_1_Context p,
int16_t *  buf,
int  energy 
)
static

Adjust gain of postfiltered signal.

Parameters
pthe context
bufpostfiltered output vector
energyinput energy coefficient

Definition at line 879 of file g723_1.c.

Referenced by formant_postfilter().

static void formant_postfilter ( G723_1_Context p,
int16_t *  lpc,
int16_t *  buf,
int16_t *  dst 
)
static

Perform formant filtering.

Parameters
pthe context
lpcquantized lpc coefficients
bufinput buffer
dstoutput buffer

Definition at line 922 of file g723_1.c.

Referenced by g723_1_decode_frame().

static int sid_gain_to_lsp_index ( int  gain)
static

Definition at line 991 of file g723_1.c.

Referenced by g723_1_decode_frame().

static int cng_rand ( int *  state,
int  base 
)
inlinestatic

Definition at line 1001 of file g723_1.c.

Referenced by generate_noise().

static int estimate_sid_gain ( G723_1_Context p)
static

Definition at line 1007 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void generate_noise ( G723_1_Context p)
static

Definition at line 1061 of file g723_1.c.

Referenced by g723_1_decode_frame().

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

Definition at line 1186 of file g723_1.c.

Variable Documentation

const AVOption options[]
static
Initial value:
= {
{ "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
{ .i64 = 1 }, 0, 1, AD },
{ NULL }
}
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition: amrnbdec.c:886
#define OFFSET(x)
Definition: g723_1.c:1351
NULL
Definition: eval.c:55
#define AD
Definition: g723_1.c:1352

Definition at line 1354 of file g723_1.c.

const AVClass g723_1dec_class
static
Initial value:
= {
.class_name = "G.723.1 decoder",
.item_name = av_default_item_name,
.option = options,
}
static const AVOption options[]
Definition: g723_1.c:1354
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:52

Definition at line 1361 of file g723_1.c.

AVCodec ff_g723_1_decoder
Initial value:
= {
.name = "g723_1",
.long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
.priv_data_size = sizeof(G723_1_Context),
.priv_class = &g723_1dec_class,
}
static const AVClass g723_1dec_class
Definition: g723_1.c:1361
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: g723_1.c:1186
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
Definition: g723_1.c:109
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:736
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499

Definition at line 1368 of file g723_1.c.