41#define BITSTREAM_WRITER_LE
48 2621, 2631, 2659, 2705, 2770, 2853, 2955, 3074, 3212, 3367,
49 3541, 3731, 3939, 4164, 4405, 4663, 4937, 5226, 5531, 5851,
50 6186, 6534, 6897, 7273, 7661, 8062, 8475, 8899, 9334, 9780,
51 10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
52 15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
53 20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
54 25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
55 29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
56 31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
57 32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
58 31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
59 29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
60 24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
61 19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
62 14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
63 9780, 9334, 8899, 8475, 8062, 7661, 7273, 6897, 6534, 6186,
64 5851, 5531, 5226, 4937, 4663, 4405, 4164, 3939, 3731, 3541,
65 3367, 3212, 3074, 2955, 2853, 2770, 2705, 2659, 2631, 2621
72 32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
79 32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
87 {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
89 {16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32}
104 }
else if (avctx->
bit_rate == 5300) {
129 *iir = (buf[
i] - *fir) * (1 << 15) +
MULL2(*iir, 0x7f00);
165 memset(autocorr + 1, 0,
LPC_ORDER *
sizeof(int16_t));
186 int16_t partial_corr;
189 memset(lpc, 0,
LPC_ORDER *
sizeof(int16_t));
194 for (j = 0; j <
i; j++)
195 temp -= lpc[j] * autocorr[
i - j - 1];
196 temp = (autocorr[
i] * (1 << 13) +
temp) * (1 << 3);
203 lpc[
i] = (partial_corr + (1 << 1)) >> 2;
210 memcpy(vector, lpc,
i *
sizeof(int16_t));
211 for (j = 0; j <
i; j++) {
212 temp = partial_corr * vector[
i - j - 1] * 2;
229 int16_t *autocorr_ptr = autocorr;
230 int16_t *lpc_ptr = lpc;
242static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
248 int max,
shift, cur_val, prev_val, count, p;
257 f[0] =
f[1] = 1 << 25;
262 f[2 *
i + 2] = -
f[2 *
i] - (lsp[
i] + lsp[
LPC_ORDER - 1 -
i]) * (1 << 12);
264 f[2 *
i + 3] =
f[2 *
i + 1] - (lsp[
i] - lsp[
LPC_ORDER - 1 -
i]) * (1 << 12);
299 if ((cur_val ^ prev_val) < 0) {
300 int abs_cur =
FFABS(cur_val);
301 int abs_prev =
FFABS(prev_val);
302 int sum = abs_cur + abs_prev;
306 abs_prev = abs_prev << shift >> 8;
307 lsp[count++] = ((
i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
326 memcpy(lsp, prev_lsp,
LPC_ORDER *
sizeof(int16_t));
336#define get_index(num, offset, size) \
338 int error, max = -1; \
342 for (i = 0; i < LSP_CB_SIZE; i++) { \
343 for (j = 0; j < size; j++){ \
344 temp[j] = (weight[j + (offset)] * ff_g723_1_lsp_band##num[i][j] + \
347 error = ff_g723_1_dot_product(lsp + (offset), temp, size) * 2; \
348 error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \
351 lsp_index[num] = i; \
362static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
369 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
394 (((prev_lsp[
i] -
dc_lsp[
i]) * 12288 + (1 << 14)) >> 15);
411 int16_t *
src, int16_t *dest)
419 iir_coef[n - 1] * dest[m - n];
434 int16_t *unq_lpc, int16_t *buf)
439 memcpy(buf, p->iir_mem,
sizeof(int16_t) *
LPC_ORDER);
440 memcpy(vector, p->fir_mem,
sizeof(int16_t) *
LPC_ORDER);
447 flt_coef[k + 2 * l +
LPC_ORDER] = (unq_lpc[k + l] *
452 vector +
i, buf +
i);
468 int max_ccr = 0x4000;
469 int max_eng = 0x7fff;
473 int ccr, eng, orig_eng, ccr_eng,
exp;
496 ccr = ccr << temp >> 16;
510 if (
exp + 1 < max_exp)
514 if (
exp + 1 == max_exp)
518 ccr_eng = ccr * max_eng;
540 int ccr, eng, max_ccr, max_eng;
545 for (
i = 0, j = pitch_lag - 3; j <= pitch_lag + 3;
i++, j++) {
557 for (
i = 0;
i < 15;
i++)
561 for (
i = 0;
i < 15;
i++) {
571 for (
i = 0;
i <= 6;
i++) {
572 eng = energy[
i << 1];
573 ccr = energy[(
i << 1) + 1];
578 ccr = (ccr * ccr + (1 << 14)) >> 15;
579 diff = ccr * max_eng - eng * max_ccr;
587 if (
hf->index == -1) {
588 hf->index = pitch_lag;
592 eng = energy[14] * max_eng;
593 eng = (eng >> 2) + (eng >> 3);
594 ccr = energy[(
hf->index << 1) + 1] * energy[(
hf->index << 1) + 1];
596 eng = energy[(
hf->index << 1) + 1];
601 hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
603 hf->index += pitch_lag - 3;
641 int16_t *perf_fir, int16_t *perf_iir,
642 const int16_t *
src, int16_t *dest,
int scale)
650 memcpy(buf_16, perf_fir,
sizeof(int16_t) *
LPC_ORDER);
656 temp -= qnt_lpc[j - 1] * bptr_16[
i - j];
658 buf[
i] =
src[
i] * (1 << 15) +
temp * (1 << 3);
665 fir -= perf_lpc[j - 1] * bptr_16[
i - j];
666 iir += perf_lpc[j +
LPC_ORDER - 1] * dest[
i - j];
683 int16_t *impulse_resp,
const int16_t *buf,
692 int pitch_lag = p->pitch_lag[
index >> 1];
695 int odd_frame =
index & 1;
696 int iter = 3 + odd_frame;
710 for (
i = 0;
i < iter;
i++) {
715 for (k = 0; k <= j; k++)
721 flt_buf[j][0] = (residual[j] + (1 << 1)) >> 2;
723 temp = flt_buf[j + 1][k - 1] * (1 << 15) +
724 residual[j] * impulse_resp[k];
742 for (k = 0; k < j; k++) {
751 for (
i = 0;
i < 20 * iter;
i++)
756 for (
i = 0;
i < 20 * iter;
i++)
761 for (
i = 0;
i < iter;
i++) {
769 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
771 for (l = 0; l < 20; l++)
772 temp += ccr_buf[20 *
i + l] * cb_tbl[k + l];
784 pitch_lag += acb_lag - 1;
788 p->pitch_lag[
index >> 1] = pitch_lag;
789 p->subframe[
index].ad_cb_lag = acb_lag;
790 p->subframe[
index].ad_cb_gain = acb_gain;
806 for (j = 0; j <=
i; j++)
807 temp -= residual[j] * impulse_resp[
i - j];
820 int16_t *buf,
int pulse_cnt,
int pitch_lag)
829 int amp, err,
max, max_amp_index,
min,
scale,
i, j, k, l;
834 memcpy(impulse_r, impulse_resp,
sizeof(int16_t) *
SUBFRAME_LEN);
842 temp_corr[
i] = impulse_r[
i] >> 1;
882 for (j = max_amp_index; j >= 2; j--) {
884 impulse_corr[0] << 1);
894 for (j = 1; j < 5; j++) {
905 for (k = 1; k < pulse_cnt; k++) {
929 for (k = 0; k < pulse_cnt; k++)
934 for (l = 0; l <= k; l++) {
936 impulse_r[k - l] * 2);
939 temp_corr[k] =
temp >> 14;
953 if (err < optim->min_err) {
959 for (k = 0; k < pulse_cnt; k++) {
975 int16_t *buf,
int pulse_cnt)
1010 int16_t *buf,
int index)
1021 p->pitch_lag[
index >> 1]);
1026 for (
i = 0;
i < pulse_cnt;
i++)
1055 put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1057 put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1062 p->subframe[
i].amp_index;
1064 temp += p->subframe[
i].dirac_train << 11;
1068 put_bits(&pb, 1, p->subframe[0].grid_index);
1069 put_bits(&pb, 1, p->subframe[1].grid_index);
1070 put_bits(&pb, 1, p->subframe[2].grid_index);
1071 put_bits(&pb, 1, p->subframe[3].grid_index);
1077 temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1078 (p->subframe[1].pulse_pos >> 14) * 90 +
1079 (p->subframe[2].pulse_pos >> 16) * 9 +
1080 (p->subframe[3].pulse_pos >> 14);
1083 put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1084 put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1085 put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1086 put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1088 put_bits(&pb, 6, p->subframe[0].pulse_sign);
1089 put_bits(&pb, 5, p->subframe[1].pulse_sign);
1090 put_bits(&pb, 6, p->subframe[2].pulse_sign);
1091 put_bits(&pb, 5, p->subframe[3].pulse_sign);
1107 int offset, ret,
i, j, info_bits = 0;
1108 int16_t *in, *start;
1137 memcpy(vector, p->prev_weight_sig,
sizeof(int16_t) *
PITCH_MAX);
1148 memcpy(vector, p->prev_weight_sig,
sizeof(int16_t) *
PITCH_MAX);
1158 memcpy(p->prev_lsp, cur_lsp,
sizeof(int16_t) *
LPC_ORDER);
1172 memset(vector, 0,
sizeof(int16_t) *
PITCH_MAX);
1175 flt_in[0] = 1 << 13;
1182 memcpy(fir, p->perf_fir_mem,
sizeof(int16_t) *
LPC_ORDER);
1183 memcpy(iir, p->perf_iir_mem,
sizeof(int16_t) *
LPC_ORDER);
1186 fir, iir, flt_in, vector +
PITCH_MAX, 0);
1187 memcpy(vector, p->harmonic_mem,
sizeof(int16_t) *
PITCH_MAX);
1192 p->pitch_lag[
i >> 1], &p->subframe[
i],
1200 p->pitch_lag[
i >> 1], &p->subframe[
i],
1203 memmove(p->prev_excitation, p->prev_excitation +
SUBFRAME_LEN,
1212 p->perf_fir_mem, p->perf_iir_mem,
1214 memmove(p->harmonic_mem, p->harmonic_mem +
SUBFRAME_LEN,
1229 *got_packet_ptr = 1;
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t * hf
static double val(void *priv, double ch)
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
const FFCodec ff_g723_1_encoder
static const FFCodecDefault defaults[]
Libavcodec external API header.
#define i(width, name, range_min, range_max)
int64_t ff_dot_product(const int16_t *a, const int16_t *b, int length)
Calculate the dot product of 2 int16_t vectors.
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
common internal and external API header
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int(* init)(AVBSFContext *ctx)
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
G.723.1 types, functions and data tables.
#define G723_1_COS_TAB_FIRST_ELEMENT
#define MULL2(a, b)
Bitexact implementation of 2ab scaled by 1/2^16.
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
static const uint8_t frame_size[4]
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
#define iir_filter(fir_coef, iir_coef, src, dest, width)
Perform IIR filtering.
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.
static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
Pack the frame parameters into output bitstream.
static av_cold int g723_1_encode_init(AVCodecContext *avctx)
static int estimate_pitch(int16_t *buf, int start)
Estimate the open loop pitch period.
static const int16_t binomial_window[LPC_ORDER]
Binomial window coefficients scaled by 2^15.
static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp, int16_t *buf, int index)
Compute the fixed codebook excitation.
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_autocorr(int16_t *buf, int16_t *autocorr)
Estimate autocorrelation of the input vector.
static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
Remove DC component from the input signal.
static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
Vector quantize the LSP frequencies.
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.
static const int16_t percept_flt_tbl[2][LPC_ORDER]
0.5^i scaled by 2^15
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 const int16_t hamming_window[LPC_FRAME]
Hamming window coefficients scaled by 2^15.
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.
static const int16_t bandwidth_expand[LPC_ORDER]
0.994^i scaled by 2^15
#define get_index(num, offset, size)
Quantize the current LSP subvector.
static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
Calculate LPC coefficients for the current frame.
static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
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.
static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
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, const int16_t *src, int16_t *dest)
Apply the harmonic noise shaping filter.
static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
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.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
@ AV_SAMPLE_FMT_S16
signed 16 bits
static int zero(InterplayACMContext *s, unsigned ind, unsigned col)
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
const int16_t ff_g723_1_cos_tab[COS_TBL_SIZE+1]
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
void ff_g723_1_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.
int ff_g723_1_normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
Get delayed contribution from the previous excitation vector.
void ff_g723_1_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.
void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, uint8_t *lsp_index, int bad_frame)
Perform inverse quantization of LSP frequencies.
int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
Scale vector contents based on the largest of their absolutes.
const int32_t ff_g723_1_combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.
const int16_t ff_g723_1_fixed_cb_gain[GAIN_LEVELS]
const int16_t ff_g723_1_adaptive_cb_gain85[85 *20]
const int16_t ff_g723_1_adaptive_cb_gain170[170 *20]
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
Memory handling functions.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
const h264_weight_func weight
main external API structure.
int64_t bit_rate
the average bitrate
int sample_rate
samples per second
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
Optimized fixed codebook excitation parameters.
int pulse_sign[PULSE_MAX]
G723.1 unpacked data subframe.
Harmonic filter parameters.
static void error(const char *err)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)