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 "encode.h"
#include "g723_1.h"
#include "internal.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 AVCodecDefault defaults []
 
const AVCodec 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) << 1; \
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 342 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 130 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 146 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 188 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 232 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 248 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 368 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 416 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 439 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 471 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 544 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 617 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 627 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 646 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 688 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 806 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 826 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 981 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 1016 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 1048 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 1104 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 AVCodecDefault defaults[]
static
Initial value:
= {
{ "b", "6300" },
{ NULL },
}

Definition at line 1241 of file g723_1enc.c.

◆ ff_g723_1_encoder

const AVCodec ff_g723_1_encoder
Initial value:
= {
.name = "g723_1",
.long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
.capabilities = AV_CODEC_CAP_DR1,
.priv_data_size = sizeof(G723_1_Context),
.encode2 = g723_1_encode_frame,
.sample_fmts = (const enum AVSampleFormat[]) {
},
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
}

Definition at line 1246 of file g723_1enc.c.

error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
g723_1_encode_init
static av_cold int g723_1_encode_init(AVCodecContext *avctx)
Definition: g723_1enc.c:92
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
G723_1_Context
Definition: g723_1.h:159
max
#define max(a, b)
Definition: cuda_runtime.h:33
init
static int init
Definition: av_tx.c:47
LSP_CB_SIZE
#define LSP_CB_SIZE
Definition: g723_1.h:42
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:475
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:1561
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
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:271
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
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:1104
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
temp
else temp
Definition: vf_mcdeint.c:248
defaults
static const AVCodecDefault defaults[]
Definition: g723_1enc.c:1241