FFmpeg
Macros | Functions | Variables
g723_1enc.c File Reference
#include <stdint.h>
#include <string.h>
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "celp_math.h"
#include "codec_internal.h"
#include "encode.h"
#include "g723_1.h"
#include "put_bits.h"

Go to the source code of this file.

Macros

#define BITSTREAM_WRITER_LE
 
#define get_index(num, offset, size)
 Quantize the current LSP subvector. More...
 

Functions

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. More...
 
static void comp_autocorr (int16_t *buf, int16_t *autocorr)
 Estimate autocorrelation of the input vector. More...
 
static void levinson_durbin (int16_t *lpc, int16_t *autocorr, int16_t error)
 Use Levinson-Durbin recursion to compute LPC coefficients from autocorrelation values. More...
 
static void comp_lpc_coeff (int16_t *buf, int16_t *lpc)
 Calculate LPC coefficients for the current frame. More...
 
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. More...
 
static void iir_filter (int16_t *fir_coef, int16_t *iir_coef, int16_t *src, int16_t *dest)
 Perform IIR filtering. More...
 
static void perceptual_filter (G723_1_ChannelContext *p, int16_t *flt_coef, int16_t *unq_lpc, int16_t *buf)
 Apply the formant perceptual weighting filter. More...
 
static int estimate_pitch (int16_t *buf, int start)
 Estimate the open loop pitch period. More...
 
static void comp_harmonic_coeff (int16_t *buf, int16_t pitch_lag, HFParam *hf)
 Compute harmonic noise filter parameters. More...
 
static void harmonic_filter (HFParam *hf, const int16_t *src, int16_t *dest)
 Apply the harmonic noise shaping filter. More...
 
static void harmonic_noise_sub (HFParam *hf, const 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, const int16_t *src, int16_t *dest, int scale)
 Combined synthesis and formant perceptual weighting filer. More...
 
static void acb_search (G723_1_ChannelContext *p, int16_t *residual, int16_t *impulse_resp, const int16_t *buf, int index)
 Compute the adaptive codebook contribution. More...
 
static void sub_acb_contrib (const int16_t *residual, const int16_t *impulse_resp, int16_t *buf)
 Subtract the adaptive codebook contribution from the input to obtain the residual. More...
 
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). More...
 
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. More...
 
static void fcb_search (G723_1_ChannelContext *p, int16_t *impulse_resp, int16_t *buf, int index)
 Compute the fixed codebook excitation. More...
 
static void pack_bitstream (G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
 Pack the frame parameters into output bitstream. More...
 
static int g723_1_encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
 

Variables

static const int16_t hamming_window [LPC_FRAME]
 Hamming window coefficients scaled by 2^15. More...
 
static const int16_t binomial_window [LPC_ORDER]
 Binomial window coefficients scaled by 2^15. More...
 
static const int16_t bandwidth_expand [LPC_ORDER]
 0.994^i scaled by 2^15 More...
 
static const int16_t percept_flt_tbl [2][LPC_ORDER]
 0.5^i scaled by 2^15 More...
 
static const FFCodecDefault defaults []
 
const FFCodec ff_g723_1_encoder
 

Detailed Description

G.723.1 compatible encoder

Definition in file g723_1enc.c.

Macro Definition Documentation

◆ BITSTREAM_WRITER_LE

#define BITSTREAM_WRITER_LE

Definition at line 41 of file g723_1enc.c.

◆ get_index

#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)] * ff_g723_1_lsp_band##num[i][j] + \
(1 << 14)) >> 15; \
} \
error = ff_g723_1_dot_product(lsp + (offset), temp, size) * 2; \
error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \
if (error > max) { \
max = error; \
lsp_index[num] = i; \
} \
} \
}

Quantize the current LSP subvector.

Parameters
numband number
offsetoffset of the current subvector in an LPC_ORDER vector
sizesize of the current subvector

Definition at line 336 of file g723_1enc.c.

Function Documentation

◆ g723_1_encode_init()

static av_cold int g723_1_encode_init ( AVCodecContext avctx)
static

Definition at line 92 of file g723_1enc.c.

◆ highpass_filter()

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

Remove DC component from the input signal.

Parameters
bufinput signal
firzero memory
iirpole memory

Definition at line 125 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ comp_autocorr()

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

Estimate autocorrelation of the input vector.

Parameters
bufinput buffer
autocorrautocorrelation coefficients vector

Definition at line 141 of file g723_1enc.c.

Referenced by comp_lpc_coeff().

◆ levinson_durbin()

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
lpcLPC coefficients vector
autocorrautocorrelation coefficients vector
errorprediction error

Definition at line 183 of file g723_1enc.c.

Referenced by comp_lpc_coeff().

◆ comp_lpc_coeff()

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

Calculate LPC coefficients for the current frame.

Parameters
bufcurrent frame
prev_data2 trailing subframes of the previous frame
lpcLPC coefficients vector

Definition at line 226 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ lpc2lsp()

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 242 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ lsp_quantize()

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

Vector quantize the LSP frequencies.

Parameters
lspthe current lsp vector
prev_lspthe previous lsp vector

Definition at line 362 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ iir_filter()

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

Perform IIR filtering.

Parameters
fir_coefFIR coefficients
iir_coefIIR coefficients
srcsource vector
destdestination vector

Definition at line 410 of file g723_1enc.c.

Referenced by perceptual_filter().

◆ perceptual_filter()

static void perceptual_filter ( G723_1_ChannelContext p,
int16_t *  flt_coef,
int16_t *  unq_lpc,
int16_t *  buf 
)
static

Apply the formant perceptual weighting filter.

Parameters
flt_coeffilter coefficients
unq_lpcunquantized lpc vector

Definition at line 433 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ estimate_pitch()

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

Estimate the open loop pitch period.

Parameters
bufperceptually weighted speech
startestimation is carried out from this position

Definition at line 465 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ comp_harmonic_coeff()

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

Compute harmonic noise filter parameters.

Parameters
bufperceptually weighted speech
pitch_lagopen loop pitch period
hfharmonic filter parameters

Definition at line 538 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ harmonic_filter()

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

Apply the harmonic noise shaping filter.

Parameters
hffilter parameters

Definition at line 611 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ harmonic_noise_sub()

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

Definition at line 621 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ synth_percept_filter()

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

Combined synthesis and formant perceptual weighting filer.

Parameters
qnt_lpcquantized lpc coefficients
perf_lpcperceptual filter coefficients
perf_firperceptual filter fir memory
perf_iirperceptual filter iir memory
scalethe filter output will be scaled by 2^scale

Definition at line 640 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ acb_search()

static void acb_search ( G723_1_ChannelContext p,
int16_t *  residual,
int16_t *  impulse_resp,
const int16_t *  buf,
int  index 
)
static

Compute the adaptive codebook contribution.

Parameters
bufinput signal
indexthe current subframe index

Definition at line 682 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ sub_acb_contrib()

static void sub_acb_contrib ( const int16_t *  residual,
const int16_t *  impulse_resp,
int16_t *  buf 
)
static

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

Parameters
buftarget vector

Definition at line 799 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ get_fcb_param()

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
optimoptimized fixed codebook parameters
bufexcitation vector

Definition at line 819 of file g723_1enc.c.

Referenced by fcb_search().

◆ pack_fcb_param()

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
optimoptimized fixed CB parameters
bufexcitation vector

Definition at line 974 of file g723_1enc.c.

Referenced by fcb_search().

◆ fcb_search()

static void fcb_search ( G723_1_ChannelContext p,
int16_t *  impulse_resp,
int16_t *  buf,
int  index 
)
static

Compute the fixed codebook excitation.

Parameters
buftarget vector
impulse_respimpulse response of the combined filter

Definition at line 1009 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ pack_bitstream()

static void pack_bitstream ( G723_1_ChannelContext p,
AVPacket avpkt,
int  info_bits 
)
static

Pack the frame parameters into output bitstream.

Parameters
frameoutput buffer
sizesize of the buffer

Definition at line 1041 of file g723_1enc.c.

Referenced by g723_1_encode_frame().

◆ g723_1_encode_frame()

static int g723_1_encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int got_packet_ptr 
)
static

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

Definition at line 1097 of file g723_1enc.c.

Variable Documentation

◆ hamming_window

const int16_t hamming_window[LPC_FRAME]
static
Initial value:
= {
2621, 2631, 2659, 2705, 2770, 2853, 2955, 3074, 3212, 3367,
3541, 3731, 3939, 4164, 4405, 4663, 4937, 5226, 5531, 5851,
6186, 6534, 6897, 7273, 7661, 8062, 8475, 8899, 9334, 9780,
10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
9780, 9334, 8899, 8475, 8062, 7661, 7273, 6897, 6534, 6186,
5851, 5531, 5226, 4937, 4663, 4405, 4164, 3939, 3731, 3541,
3367, 3212, 3074, 2955, 2853, 2770, 2705, 2659, 2631, 2621
}

Hamming window coefficients scaled by 2^15.

Definition at line 47 of file g723_1enc.c.

Referenced by comp_autocorr().

◆ binomial_window

const int16_t binomial_window[LPC_ORDER]
static
Initial value:
= {
32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
}

Binomial window coefficients scaled by 2^15.

Definition at line 71 of file g723_1enc.c.

Referenced by comp_autocorr().

◆ bandwidth_expand

const int16_t bandwidth_expand[LPC_ORDER]
static
Initial value:
= {
32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
}

0.994^i scaled by 2^15

Definition at line 78 of file g723_1enc.c.

Referenced by lpc2lsp().

◆ percept_flt_tbl

const int16_t percept_flt_tbl[2][LPC_ORDER]
static
Initial value:
= {
{29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
{16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32}
}

0.5^i scaled by 2^15

Definition at line 85 of file g723_1enc.c.

Referenced by perceptual_filter().

◆ defaults

const FFCodecDefault defaults[]
static
Initial value:
= {
{ "b", "6300" },
{ NULL },
}

Definition at line 1234 of file g723_1enc.c.

◆ ff_g723_1_encoder

const FFCodec ff_g723_1_encoder
Initial value:
= {
.p.name = "g723_1",
CODEC_LONG_NAME("G.723.1"),
.p.type = AVMEDIA_TYPE_AUDIO,
.priv_data_size = sizeof(G723_1_Context),
.defaults = defaults,
.p.sample_fmts = (const enum AVSampleFormat[]) {
},
.p.ch_layouts = (const AVChannelLayout[]){
},
}

Definition at line 1239 of file g723_1enc.c.

error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
g723_1_encode_init
static av_cold int g723_1_encode_init(AVCodecContext *avctx)
Definition: g723_1enc.c:92
defaults
static const FFCodecDefault defaults[]
Definition: g723_1enc.c:1234
G723_1_Context
Definition: g723_1.h:159
max
#define max(a, b)
Definition: cuda_runtime.h:33
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
LSP_CB_SIZE
#define LSP_CB_SIZE
Definition: g723_1.h:42
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:492
ff_g723_1_dot_product
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
Definition: g723_1.c:1126
weight
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1563
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
size
int size
Definition: twinvq_data.h:10344
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
g723_1_encode_frame
static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: g723_1enc.c:1097
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
temp
else temp
Definition: vf_mcdeint.c:263
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378