FFmpeg
Macros | Functions | Variables
g723_1dec.c File Reference
#include "libavutil/channel_layout.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "acelp_vectors.h"
#include "avcodec.h"
#include "celp_filters.h"
#include "codec_internal.h"
#include "decode.h"
#include "get_bits.h"
#include "g723_1.h"

Go to the source code of this file.

Macros

#define BITSTREAM_READER_LE
 
#define CNG_RANDOM_SEED   12345
 
#define iir_filter(fir_coef, iir_coef, src, dest, width)
 Perform IIR filtering. More...
 
#define OFFSET(x)   offsetof(G723_1_Context, x)
 
#define AD   AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 

Functions

static av_cold int g723_1_decode_init (AVCodecContext *avctx)
 
static int unpack_bitstream (G723_1_ChannelContext *p, const uint8_t *buf, int buf_size)
 Unpack the frame into parameters. More...
 
static int16_t square_root (unsigned val)
 Bitexact implementation of sqrt(val/2). 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 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_ChannelContext *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
 Calculate pitch postfilter parameters. More...
 
static int comp_interp_index (G723_1_ChannelContext *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)
 Perform residual interpolation based on frame classification. More...
 
static void gain_scale (G723_1_ChannelContext *p, int16_t *buf, int energy)
 Adjust gain of postfiltered signal. More...
 
static void formant_postfilter (G723_1_ChannelContext *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_ChannelContext *p)
 
static void generate_noise (G723_1_ChannelContext *p)
 
static int g723_1_decode_frame (AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
 

Variables

static const int16_t ppf_gain_weight [2] = {0x1800, 0x2000}
 Postfilter gain weighting factors scaled by 2^15. More...
 
static const int16_t pitch_contrib [340]
 
static const int32_t max_pos [4] = {593775, 142506, 593775, 142506}
 Size of the MP-MLQ fixed excitation codebooks. More...
 
static const int16_t postfilter_tbl [2][LPC_ORDER]
 0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15 More...
 
static const int cng_adaptive_cb_lag [4] = { 1, 0, 1, 3 }
 
static const int cng_filt [4] = { 273, 998, 499, 333 }
 
static const int cng_bseg [3] = { 2048, 18432, 231233 }
 
static const AVOption options []
 
static const AVClass g723_1dec_class
 
const FFCodec ff_g723_1_decoder
 

Detailed Description

G.723.1 compatible decoder

Definition in file g723_1dec.c.

Macro Definition Documentation

◆ BITSTREAM_READER_LE

#define BITSTREAM_READER_LE

Definition at line 32 of file g723_1dec.c.

◆ CNG_RANDOM_SEED

#define CNG_RANDOM_SEED   12345

Definition at line 41 of file g723_1dec.c.

◆ iir_filter

#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] * 65536) + (filter * 8) +\
(1 << 15)) >> res_shift;\
}\
}

Perform IIR filtering.

Parameters
fir_coefFIR coefficients
iir_coefIIR coefficients
srcsource vector
destdestination vector
widthwidth of the output, 16 bits(0) / 32 bits(1)

Definition at line 589 of file g723_1dec.c.

◆ OFFSET

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

Definition at line 1096 of file g723_1dec.c.

◆ AD

Definition at line 1097 of file g723_1dec.c.

Function Documentation

◆ g723_1_decode_init()

static av_cold int g723_1_decode_init ( AVCodecContext avctx)
static

Definition at line 115 of file g723_1dec.c.

◆ unpack_bitstream()

static int unpack_bitstream ( G723_1_ChannelContext 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 147 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ square_root()

static int16_t square_root ( unsigned  val)
static

Bitexact implementation of sqrt(val/2).

Definition at line 265 of file g723_1dec.c.

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

◆ 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 281 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ autocorr_max()

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 348 of file g723_1dec.c.

Referenced by comp_interp_index(), and comp_ppf_coeff().

◆ comp_ppf_gains()

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 381 of file g723_1dec.c.

Referenced by comp_ppf_coeff().

◆ comp_ppf_coeff()

static void comp_ppf_coeff ( G723_1_ChannelContext 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 429 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ comp_interp_index()

static int comp_interp_index ( G723_1_ChannelContext 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 515 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ residual_interp()

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

Perform 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 560 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ gain_scale()

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

Adjust gain of postfiltered signal.

Parameters
pthe context
bufpostfiltered output vector
energyinput energy coefficient

Definition at line 614 of file g723_1dec.c.

Referenced by formant_postfilter().

◆ formant_postfilter()

static void formant_postfilter ( G723_1_ChannelContext 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 657 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ sid_gain_to_lsp_index()

static int sid_gain_to_lsp_index ( int  gain)
static

Definition at line 724 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ cng_rand()

static int cng_rand ( int state,
int  base 
)
inlinestatic

Definition at line 734 of file g723_1dec.c.

Referenced by generate_noise().

◆ estimate_sid_gain()

static int estimate_sid_gain ( G723_1_ChannelContext p)
static

Definition at line 740 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ generate_noise()

static void generate_noise ( G723_1_ChannelContext p)
static

Definition at line 802 of file g723_1dec.c.

Referenced by g723_1_decode_frame().

◆ g723_1_decode_frame()

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

Definition at line 927 of file g723_1dec.c.

Variable Documentation

◆ ppf_gain_weight

const int16_t ppf_gain_weight[2] = {0x1800, 0x2000}
static

Postfilter gain weighting factors scaled by 2^15.

Definition at line 46 of file g723_1dec.c.

Referenced by comp_ppf_gains().

◆ pitch_contrib

const int16_t pitch_contrib[340]
static

Definition at line 48 of file g723_1dec.c.

Referenced by gen_fcb_excitation().

◆ max_pos

const int32_t max_pos[4] = {593775, 142506, 593775, 142506}
static

Size of the MP-MLQ fixed excitation codebooks.

Definition at line 97 of file g723_1dec.c.

Referenced by amf_parse_object(), gen_fcb_excitation(), and parse_keyframes_index().

◆ postfilter_tbl

const int16_t postfilter_tbl[2][LPC_ORDER]
static
Initial value:
= {
{21299, 13844, 8999, 5849, 3802, 2471, 1606, 1044, 679, 441},
{24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845}
}

0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15

Definition at line 102 of file g723_1dec.c.

Referenced by formant_postfilter().

◆ cng_adaptive_cb_lag

const int cng_adaptive_cb_lag[4] = { 1, 0, 1, 3 }
static

Definition at line 109 of file g723_1dec.c.

Referenced by generate_noise().

◆ cng_filt

const int cng_filt[4] = { 273, 998, 499, 333 }
static

Definition at line 111 of file g723_1dec.c.

Referenced by estimate_sid_gain().

◆ cng_bseg

const int cng_bseg[3] = { 2048, 18432, 231233 }
static

Definition at line 113 of file g723_1dec.c.

Referenced by estimate_sid_gain().

◆ options

const AVOption options[]
static
Initial value:
= {
{ "postfilter", "enable postfilter", OFFSET(postfilter), AV_OPT_TYPE_BOOL,
{ .i64 = 1 }, 0, 1, AD },
{ NULL }
}

Definition at line 1099 of file g723_1dec.c.

◆ g723_1dec_class

const AVClass g723_1dec_class
static
Initial value:
= {
.class_name = "G.723.1 decoder",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 1106 of file g723_1dec.c.

◆ ff_g723_1_decoder

const FFCodec ff_g723_1_decoder
Initial value:
= {
.p.name = "g723_1",
CODEC_LONG_NAME("G.723.1"),
.p.type = AVMEDIA_TYPE_AUDIO,
.priv_data_size = sizeof(G723_1_Context),
.p.capabilities =
AV_CODEC_CAP_SUBFRAMES |
.p.priv_class = &g723_1dec_class,
}

Definition at line 1113 of file g723_1dec.c.

g723_1dec_class
static const AVClass g723_1dec_class
Definition: g723_1dec.c:1106
AD
#define AD
Definition: g723_1dec.c:1097
G723_1_Context
Definition: g723_1.h:159
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
options
static const AVOption options[]
Definition: g723_1dec.c:1099
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
postfilter
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition: amrnbdec.c:913
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
LPC_ORDER
#define LPC_ORDER
Definition: g723_1.h:40
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:492
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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_clipl_int32
#define av_clipl_int32
Definition: common.h:116
OFFSET
#define OFFSET(x)
Definition: g723_1dec.c:1096
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
g723_1_decode_frame
static int g723_1_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: g723_1dec.c:927
SUBFRAME_LEN
#define SUBFRAME_LEN
Definition: g723_1.h:36
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
g723_1_decode_init
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
Definition: g723_1dec.c:115