libavcodec/g723_1.c File Reference

G.723.1 compatible decoder. More...

#include "avcodec.h"
#include "get_bits.h"
#include "acelp_vectors.h"
#include "celp_filters.h"
#include "celp_math.h"
#include "lsp.h"
#include "libavutil/lzo.h"
#include "g723_1_data.h"
#include "put_bits.h"

Go to the source code of this file.

Data Structures

struct  g723_1_context

Defines

#define BITSTREAM_READER_LE
#define normalize_bits_int16(num)   normalize_bits(num, 0)
#define normalize_bits_int32(num)   normalize_bits(num, 1)
#define dot_product(a, b, c, d)   (ff_dot_product(a,b,c)<<(d))
#define MULL2(a, b)   MULL(a,b,15)
 Bitexact implementation of 2ab scaled by 1/2^16.
#define iir_filter(fir_coef, iir_coef, src, dest, width)
 Perform IIR filtering.
#define BITSTREAM_WRITER_LE
#define get_index(num, offset, size)
 Quantize the current LSP subvector.

Typedefs

typedef struct g723_1_context G723_1_Context

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.
static int16_t square_root (int val)
 Bitexact implementation of sqrt(val/2).
static int normalize_bits (int num, int width)
 Calculate the number of left-shifts required for normalizing the input.
static int scale_vector (int16_t *vector, int length)
 Scale vector contents based on the largest of their absolutes.
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.
static void lsp2lpc (int16_t *lpc)
 Convert LSP frequencies to LPC coefficients.
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.
static void gen_dirac_train (int16_t *buf, int pitch_lag)
 Generate a train of dirac functions with period as pitch lag.
static void gen_fcb_excitation (int16_t *vector, G723_1_Subframe subfrm, Rate cur_rate, int pitch_lag, int index)
 Generate fixed codebook excitation vector.
static void get_residual (int16_t *residual, int16_t *prev_excitation, int lag)
 Get delayed contribution from the previous excitation vector.
static void gen_acb_excitation (int16_t *vector, int16_t *prev_excitation, int pitch_lag, G723_1_Subframe subfrm, Rate cur_rate)
 Generate adaptive codebook excitation.
static int autocorr_max (G723_1_Context *p, int offset, int *ccr_max, int pitch_lag, int length, int dir)
 Estimate maximum auto-correlation around pitch lag.
static void comp_ppf_gains (int lag, PPFParam *ppf, Rate cur_rate, int tgt_eng, int ccr, int res_eng)
 Calculate pitch postfilter optimal and scaling gains.
static void comp_ppf_coeff (G723_1_Context *p, int offset, int pitch_lag, PPFParam *ppf, Rate cur_rate)
 Calculate pitch postfilter parameters.
static int comp_interp_index (G723_1_Context *p, int pitch_lag, int *exc_eng, int *scale)
 Classify frames as voiced/unvoiced.
static void residual_interp (int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
 Peform residual interpolation based on frame classification.
static void gain_scale (G723_1_Context *p, int16_t *buf, int energy)
 Adjust gain of postfiltered signal.
static void formant_postfilter (G723_1_Context *p, int16_t *lpc, int16_t *buf)
 Perform formant filtering.
static int g723_1_decode_frame (AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static av_cold int g723_1_encode_init (AVCodecContext *avctx)
static void highpass_filter (int16_t *buf, int16_t *fir, int *iir)
 Remove DC component from the input signal.
static void comp_autocorr (int16_t *buf, int16_t *autocorr)
 Estimate autocorrelation of the input vector.
static void levinson_durbin (int16_t *lpc, int16_t *autocorr, int16_t error)
 Use Levinson-Durbin recursion to compute LPC coefficients from autocorrelation values.
static void comp_lpc_coeff (int16_t *buf, int16_t *lpc)
 Calculate LPC coefficients for the current frame.
static void lpc2lsp (int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
static void lsp_quantize (uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
 Vector quantize the LSP frequencies.
static void perceptual_filter (G723_1_Context *p, int16_t *flt_coef, int16_t *unq_lpc, int16_t *buf)
 Apply the formant perceptual weighting filter.
static int estimate_pitch (int16_t *buf, int start)
 Estimate the open loop pitch period.
static void comp_harmonic_coeff (int16_t *buf, int16_t pitch_lag, HFParam *hf)
 Compute harmonic noise filter parameters.
static void harmonic_filter (HFParam *hf, int16_t *src, int16_t *dest)
 Apply the harmonic noise shaping filter.
static void harmonic_noise_sub (HFParam *hf, int16_t *src, int16_t *dest)
static void synth_percept_filter (int16_t *qnt_lpc, int16_t *perf_lpc, int16_t *perf_fir, int16_t *perf_iir, int16_t *src, int16_t *dest, int scale)
 Combined synthesis and formant perceptual weighting filer.
static void acb_search (G723_1_Context *p, int16_t *residual, int16_t *impulse_resp, int16_t *buf, int index)
 Compute the adaptive codebook contribution.
static void sub_acb_contrib (int16_t *residual, int16_t *impulse_resp, int16_t *buf)
 Subtract the adaptive codebook contribution from the input to obtain the residual.
static void get_fcb_param (FCBParam *optim, int16_t *impulse_resp, int16_t *buf, int pulse_cnt, int pitch_lag)
 Quantize the residual signal using the fixed codebook (MP-MLQ).
static void pack_fcb_param (G723_1_Subframe *subfrm, FCBParam *optim, int16_t *buf, int pulse_cnt)
 Encode the pulse position and gain of the current subframe.
static void fcb_search (G723_1_Context *p, int16_t *impulse_resp, int16_t *buf, int index)
 Compute the fixed codebook excitation.
static int pack_bitstream (G723_1_Context *p, unsigned char *frame, int size)
 Pack the frame parameters into output bitstream.
static int g723_1_encode_frame (AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)

Variables

AVCodec ff_g723_1_decoder
AVCodec ff_g723_1_encoder

Detailed Description

G.723.1 compatible decoder.

Definition in file g723_1.c.


Define Documentation

#define BITSTREAM_READER_LE

Definition at line 29 of file g723_1.c.

#define BITSTREAM_WRITER_LE

Definition at line 1102 of file g723_1.c.

#define dot_product (   a,
  b,
  c,
  d 
)    (ff_dot_product(a,b,c)<<(d))
#define get_index (   num,
  offset,
  size 
)
Value:
{\
    int error, max = -1;\
    int16_t temp[4];\
    int i, j;\
    for (i = 0; i < LSP_CB_SIZE; i++) {\
        for (j = 0; j < size; j++){\
            temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
                      (1 << 14)) >> 15;\
        }\
        error =  dot_product(lsp + (offset), temp, size, 1) << 1;\
        error -= dot_product(lsp_band##num[i], temp, size, 1);\
        if (error > max) {\
            max = error;\
            lsp_index[num] = i;\
        }\
    }\
}

Quantize the current LSP subvector.

Parameters:
num band number
offset offset of the current subvector in an LPC_ORDER vector
size size of the current subvector

Definition at line 1356 of file g723_1.c.

Referenced by lsp_quantize().

#define iir_filter (   fir_coef,
  iir_coef,
  src,
  dest,
  width 
)
Value:
{\
    int m, n;\
    int res_shift = 16 & ~-(width);\
    int in_shift  = 16 - res_shift;\
\
    for (m = 0; m < SUBFRAME_LEN; m++) {\
        int64_t filter = 0;\
        for (n = 1; n <= LPC_ORDER; n++) {\
            filter -= (fir_coef)[n - 1] * (src)[m - n] -\
                      (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
        }\
\
        (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
                                   (1 << 15)) >> res_shift;\
    }\
}

Perform IIR filtering.

Parameters:
fir_coef FIR coefficients
iir_coef IIR coefficients
src source vector
dest destination vector
width width of the output, 16 bits(0) / 32 bits(1)

Definition at line 818 of file g723_1.c.

Referenced by formant_postfilter(), and perceptual_filter().

#define MULL2 (   a,
  b 
)    MULL(a,b,15)

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

Parameters:
a 32 bit multiplicand
b 16 bit multiplier

Definition at line 344 of file g723_1.c.

Referenced by comp_autocorr(), highpass_filter(), levinson_durbin(), and lsp2lpc().

#define normalize_bits_int16 (   num  )     normalize_bits(num, 0)

Definition at line 239 of file g723_1.c.

Referenced by lsp_quantize().

#define normalize_bits_int32 (   num  )     normalize_bits(num, 1)

Typedef Documentation


Function Documentation

static void acb_search ( G723_1_Context p,
int16_t *  residual,
int16_t *  impulse_resp,
int16_t *  buf,
int  index 
) [static]

Compute the adaptive codebook contribution.

Parameters:
buf input signal
index the current subframe index

Definition at line 1677 of file g723_1.c.

Referenced by g723_1_encode_frame().

static int autocorr_max ( G723_1_Context p,
int  offset,
int *  ccr_max,
int  pitch_lag,
int  length,
int  dir 
) [static]

Estimate maximum auto-correlation around pitch lag.

Parameters:
p the context
offset offset of the excitation vector
ccr_max pointer to the maximum auto-correlation
pitch_lag decoded pitch lag
length length of autocorrelation
dir forward lag(1) / backward lag(-1)

Definition at line 579 of file g723_1.c.

Referenced by comp_interp_index(), and comp_ppf_coeff().

static void comp_autocorr ( int16_t *  buf,
int16_t *  autocorr 
) [static]

Estimate autocorrelation of the input vector.

Parameters:
buf input buffer
autocorr autocorrelation coefficients vector

Definition at line 1158 of file g723_1.c.

Referenced by comp_lpc_coeff().

static void comp_harmonic_coeff ( int16_t *  buf,
int16_t  pitch_lag,
HFParam hf 
) [static]

Compute harmonic noise filter parameters.

Parameters:
buf perceptually weighted speech
pitch_lag open loop pitch period
hf harmonic filter parameters

Definition at line 1532 of file g723_1.c.

Referenced by g723_1_encode_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:
p the context
pitch_lag decoded pitch_lag
exc_eng excitation energy estimation
scale scaling factor of exc_eng
Returns:
residual interpolation index if voiced, 0 otherwise

Definition at line 744 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void comp_lpc_coeff ( int16_t *  buf,
int16_t *  lpc 
) [static]

Calculate LPC coefficients for the current frame.

Parameters:
buf current frame
prev_data 2 trailing subframes of the previous frame
lpc LPC coefficients vector

Definition at line 1245 of file g723_1.c.

Referenced by g723_1_encode_frame().

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

Calculate pitch postfilter parameters.

Parameters:
p the context
offset offset of the excitation vector
pitch_lag decoded pitch lag
ppf pitch postfilter parameters
cur_rate current bitrate

Definition at line 658 of file g723_1.c.

Referenced by g723_1_decode_frame().

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

Calculate pitch postfilter optimal and scaling gains.

Parameters:
lag pitch postfilter forward/backward lag
ppf pitch postfilter parameters
cur_rate current bitrate
tgt_eng target energy
ccr cross-correlation
res_eng residual energy

Definition at line 610 of file g723_1.c.

Referenced by comp_ppf_coeff().

static int estimate_pitch ( int16_t *  buf,
int  start 
) [static]

Estimate the open loop pitch period.

Parameters:
buf perceptually weighted speech
start estimation is carried out from this position

Definition at line 1459 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void fcb_search ( G723_1_Context p,
int16_t *  impulse_resp,
int16_t *  buf,
int  index 
) [static]

Compute the fixed codebook excitation.

Parameters:
buf target vector
impulse_resp impulse response of the combined filter

Definition at line 2004 of file g723_1.c.

Referenced by g723_1_encode_frame().

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

Perform formant filtering.

Parameters:
p the context
lpc quantized lpc coefficients
buf output buffer

Definition at line 885 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 956 of file g723_1.c.

static av_cold int g723_1_decode_init ( AVCodecContext avctx  )  [static]

Definition at line 76 of file g723_1.c.

static int g723_1_encode_frame ( AVCodecContext avctx,
unsigned char *  buf,
int  buf_size,
void *  data 
) [static]

Compute the combined impulse response of the synthesis filter, formant perceptual weighting filter and harmonic noise shaping filter

Definition at line 2096 of file g723_1.c.

static av_cold int g723_1_encode_init ( AVCodecContext avctx  )  [static]

Definition at line 1105 of file g723_1.c.

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

Adjust gain of postfiltered signal.

Parameters:
p the context
buf postfiltered output vector
energy input energy coefficient

Definition at line 843 of file g723_1.c.

Referenced by formant_postfilter().

static void gen_acb_excitation ( int16_t *  vector,
int16_t *  prev_excitation,
int  pitch_lag,
G723_1_Subframe  subfrm,
Rate  cur_rate 
) [static]

Generate adaptive codebook excitation.

Definition at line 542 of file g723_1.c.

Referenced by g723_1_decode_frame(), and g723_1_encode_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 445 of file g723_1.c.

Referenced by fcb_search(), gen_fcb_excitation(), and get_fcb_param().

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

Generate fixed codebook excitation vector.

Parameters:
vector decoded excitation vector
subfrm current subframe
cur_rate current bitrate
pitch_lag closed loop pitch lag
index current subframe index

Definition at line 466 of file g723_1.c.

Referenced by g723_1_decode_frame().

static void get_fcb_param ( FCBParam optim,
int16_t *  impulse_resp,
int16_t *  buf,
int  pulse_cnt,
int  pitch_lag 
) [static]

Quantize the residual signal using the fixed codebook (MP-MLQ).

Parameters:
optim optimized fixed codebook parameters
buf excitation vector

Definition at line 1817 of file g723_1.c.

Referenced by fcb_search().

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 526 of file g723_1.c.

Referenced by acb_search(), and gen_acb_excitation().

static void harmonic_filter ( HFParam hf,
int16_t *  src,
int16_t *  dest 
) [static]

Apply the harmonic noise shaping filter.

Parameters:
hf filter parameters

Definition at line 1605 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void harmonic_noise_sub ( HFParam hf,
int16_t *  src,
int16_t *  dest 
) [static]

Definition at line 1615 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void highpass_filter ( int16_t *  buf,
int16_t *  fir,
int *  iir 
) [static]

Remove DC component from the input signal.

Parameters:
buf input signal
fir zero memory
iir pole memory

Definition at line 1142 of file g723_1.c.

Referenced by g723_1_encode_frame().

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_lsp the current LSP vector
prev_lsp the previous LSP vector
lsp_index VQ indices
bad_frame bad frame flag

Definition at line 276 of file g723_1.c.

Referenced by g723_1_decode_frame(), and g723_1_encode_frame().

static void levinson_durbin ( int16_t *  lpc,
int16_t *  autocorr,
int16_t  error 
) [static]

Use Levinson-Durbin recursion to compute LPC coefficients from autocorrelation values.

Parameters:
lpc LPC coefficients vector
autocorr autocorrelation coefficients vector
error prediction error

Definition at line 1201 of file g723_1.c.

Referenced by comp_lpc_coeff().

static void lpc2lsp ( int16_t *  lpc,
int16_t *  prev_lsp,
int16_t *  lsp 
) [static]

< coefficients of the sum and difference polynomials (F1, F2) ordered as f1[0], f2[0], ...., f1[5], f2[5]

Evaluate F1 and F2 at uniform intervals of pi/256 along the unit circle and check for zero crossings.

Definition at line 1261 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void lsp2lpc ( int16_t *  lpc  )  [static]

Convert LSP frequencies to LPC coefficients.

Parameters:
lpc buffer for LPC coefficients

Definition at line 352 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:
lpc buffer for LPC coefficients
cur_lsp the current LSP vector
prev_lsp the previous LSP vector

Definition at line 422 of file g723_1.c.

Referenced by g723_1_decode_frame(), and g723_1_encode_frame().

static void lsp_quantize ( uint8_t *  lsp_index,
int16_t *  lsp,
int16_t *  prev_lsp 
) [static]

Vector quantize the LSP frequencies.

Parameters:
lsp the current lsp vector
prev_lsp the previous lsp vector

Definition at line 1381 of file g723_1.c.

Referenced by g723_1_encode_frame().

static int normalize_bits ( int  num,
int  width 
) [static]

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

Parameters:
num input number
width width of the input, 16 bits(0) / 32 bits(1)

Definition at line 223 of file g723_1.c.

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

static int pack_bitstream ( G723_1_Context p,
unsigned char *  frame,
int  size 
) [static]

Pack the frame parameters into output bitstream.

Parameters:
frame output buffer
size size of the buffer

Definition at line 2036 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void pack_fcb_param ( G723_1_Subframe subfrm,
FCBParam optim,
int16_t *  buf,
int  pulse_cnt 
) [static]

Encode the pulse position and gain of the current subframe.

Parameters:
optim optimized fixed CB parameters
buf excitation vector

Definition at line 1971 of file g723_1.c.

Referenced by fcb_search().

static void perceptual_filter ( G723_1_Context p,
int16_t *  flt_coef,
int16_t *  unq_lpc,
int16_t *  buf 
) [static]

Apply the formant perceptual weighting filter.

Parameters:
flt_coef filter coefficients
unq_lpc unquantized lpc vector

Definition at line 1427 of file g723_1.c.

Referenced by g723_1_encode_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:
buf decoded excitation vector
out output vector
lag decoded pitch lag
gain interpolated gain
rseed seed for random number generator

Definition at line 788 of file g723_1.c.

Referenced by g723_1_decode_frame().

static int scale_vector ( int16_t *  vector,
int  length 
) [static]

Scale vector contents based on the largest of their absolutes.

Definition at line 246 of file g723_1.c.

Referenced by comp_autocorr(), comp_interp_index(), formant_postfilter(), and g723_1_encode_frame().

static int16_t square_root ( int  val  )  [static]

Bitexact implementation of sqrt(val/2).

Definition at line 212 of file g723_1.c.

Referenced by comp_ppf_gains(), and gain_scale().

static void sub_acb_contrib ( int16_t *  residual,
int16_t *  impulse_resp,
int16_t *  buf 
) [static]

Subtract the adaptive codebook contribution from the input to obtain the residual.

Parameters:
buf target vector

Definition at line 1797 of file g723_1.c.

Referenced by g723_1_encode_frame().

static void synth_percept_filter ( int16_t *  qnt_lpc,
int16_t *  perf_lpc,
int16_t *  perf_fir,
int16_t *  perf_iir,
int16_t *  src,
int16_t *  dest,
int  scale 
) [static]

Combined synthesis and formant perceptual weighting filer.

Parameters:
qnt_lpc quantized lpc coefficients
perf_lpc perceptual filter coefficients
perf_fir perceptual filter fir memory
perf_iir perceptual filter iir memory
scale the filter output will be scaled by 2^scale

Definition at line 1635 of file g723_1.c.

Referenced by g723_1_encode_frame().

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

Unpack the frame into parameters.

Parameters:
p the context
buf pointer to the input buffer
buf_size size of the input buffer

Definition at line 97 of file g723_1.c.

Referenced by g723_1_decode_frame().


Variable Documentation

Initial value:
 {
    .name           = "g723_1",
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = CODEC_ID_G723_1,
    .priv_data_size = sizeof(G723_1_Context),
    .init           = g723_1_decode_init,
    .decode         = g723_1_decode_frame,
    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
    .capabilities   = CODEC_CAP_SUBFRAMES,
}

Definition at line 1090 of file g723_1.c.

Initial value:
 {
    .name           = "g723_1",
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = CODEC_ID_G723_1,
    .priv_data_size = sizeof(G723_1_Context),
    .init           = g723_1_encode_init,
    .encode         = g723_1_encode_frame,
    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
    .sample_fmts    = (const enum SampleFormat[]){AV_SAMPLE_FMT_S16,
                                                  AV_SAMPLE_FMT_NONE},
}

Definition at line 2219 of file g723_1.c.