32 #define BITSTREAM_READER_LE
41 #define CNG_RANDOM_SEED 12345
49 60, 0, 0, 2489, 60, 0, 0, 5217,
50 1, 6171, 0, 3953, 0, 10364, 1, 9357,
51 -1, 8843, 1, 9396, 0, 5794, -1, 10816,
52 2, 11606, -2, 12072, 0, 8616, 1, 12170,
53 0, 14440, 0, 7787, -1, 13721, 0, 18205,
54 0, 14471, 0, 15807, 1, 15275, 0, 13480,
55 -1, 18375, -1, 0, 1, 11194, -1, 13010,
56 1, 18836, -2, 20354, 1, 16233, -1, 0,
57 60, 0, 0, 12130, 0, 13385, 1, 17834,
58 1, 20875, 0, 21996, 1, 0, 1, 18277,
59 -1, 21321, 1, 13738, -1, 19094, -1, 20387,
60 -1, 0, 0, 21008, 60, 0, -2, 22807,
61 0, 15900, 1, 0, 0, 17989, -1, 22259,
62 1, 24395, 1, 23138, 0, 23948, 1, 22997,
63 2, 22604, -1, 25942, 0, 26246, 1, 25321,
64 0, 26423, 0, 24061, 0, 27247, 60, 0,
65 -1, 25572, 1, 23918, 1, 25930, 2, 26408,
66 -1, 19049, 1, 27357, -1, 24538, 60, 0,
67 -1, 25093, 0, 28549, 1, 0, 0, 22793,
68 -1, 25659, 0, 29377, 0, 30276, 0, 26198,
69 1, 22521, -1, 28919, 0, 27384, 1, 30162,
70 -1, 0, 0, 24237, -1, 30062, 0, 21763,
71 1, 30917, 60, 0, 0, 31284, 0, 29433,
72 1, 26821, 1, 28655, 0, 31327, 2, 30799,
73 1, 31389, 0, 32322, 1, 31760, -2, 31830,
74 0, 26936, -1, 31180, 1, 30875, 0, 27873,
75 -1, 30429, 1, 31050, 0, 0, 0, 31912,
76 1, 31611, 0, 31565, 0, 25557, 0, 31357,
77 60, 0, 1, 29536, 1, 28985, -1, 26984,
78 -1, 31587, 2, 30836, -2, 31133, 0, 30243,
79 -1, 30742, -1, 32090, 60, 0, 2, 30902,
80 60, 0, 0, 30027, 0, 29042, 60, 0,
81 0, 31756, 0, 24553, 0, 25636, -2, 30501,
82 60, 0, -1, 29617, 0, 30649, 60, 0,
83 0, 29274, 2, 30415, 0, 27480, 0, 31213,
84 -1, 28147, 0, 30600, 1, 31652, 2, 29068,
85 60, 0, 1, 28571, 1, 28730, 1, 31422,
86 0, 28257, 0, 24797, 60, 0, 0, 0,
87 60, 0, 0, 22105, 0, 27852, 60, 0,
88 60, 0, -1, 24214, 0, 24642, 0, 23305,
89 60, 0, 60, 0, 1, 22883, 0, 21601,
90 60, 0, 2, 25650, 60, 0, -2, 31253,
104 {21299, 13844, 8999, 5849, 3802, 2471, 1606, 1044, 679, 441},
106 {24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845}
111 static const int cng_filt[4] = { 273, 998, 499, 333 };
113 static const int cng_bseg[3] = { 2048, 18432, 231233 };
125 for (
int ch = 0; ch < avctx->
channels; ch++) {
152 int temp, info_bits,
i;
162 if (info_bits == 3) {
172 if (info_bits == 2) {
282 enum Rate cur_rate,
int pitch_lag,
int index)
319 for (
i = 0;
i < 8;
i += 2) {
320 offset = ((cb_pos & 7) << 3) + cb_shift +
i;
321 vector[
offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
333 vector[
i] += beta * vector[
i - lag] >> 15;
349 int pitch_lag,
int length,
int dir)
351 int limit, ccr, lag = 0;
358 limit = pitch_lag + 3;
360 for (
i = pitch_lag - 3;
i <= limit;
i++) {
363 if (ccr > *ccr_max) {
382 int tgt_eng,
int ccr,
int res_eng)
389 temp1 = tgt_eng * res_eng >> 1;
390 temp2 = ccr * ccr << 1;
393 if (ccr >= res_eng) {
396 ppf->
opt_gain = (ccr << 15) / res_eng *
400 temp1 = (tgt_eng << 15) + (ccr * ppf->
opt_gain << 1);
402 pf_residual =
av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
404 if (tgt_eng >= pf_residual << 1) {
407 temp1 = (tgt_eng << 14) / pf_residual;
444 int energy[5] = {0, 0, 0, 0, 0};
456 if (!back_lag && !fwd_lag)
474 for (
i = 0;
i < 5;
i++)
475 temp1 =
FFMAX(energy[
i], temp1);
478 for (
i = 0;
i < 5;
i++)
479 energy[
i] = (energy[
i] << scale) >> 16;
481 if (fwd_lag && !back_lag) {
484 }
else if (!fwd_lag) {
493 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
494 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
495 if (temp1 >= temp2) {
516 int *exc_eng,
int *scale)
543 temp = best_eng * *exc_eng >> 3;
545 if (
temp < ccr * ccr) {
561 int gain,
int *rseed)
567 for (
i = 0;
i < lag;
i++)
568 out[
i] = vector_ptr[
i - lag] * 3 >> 2;
573 *rseed = (int16_t)(*rseed * 521 + 259);
574 out[
i] = gain * *rseed >> 15;
589 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
592 int res_shift = 16 & ~-(width);\
593 int in_shift = 16 - res_shift;\
595 for (m = 0; m < SUBFRAME_LEN; m++) {\
597 for (n = 1; n <= LPC_ORDER; n++) {\
598 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
599 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
602 (dest)[m] = av_clipl_int32(((src)[m] * 65536) + (filter * 8) +\
603 (1 << 15)) >> res_shift;\
622 int temp = buf[
i] >> 2;
630 num = num << bits1 >> 1;
636 gain = (num >> 1) / (denom >> 16);
658 int16_t *buf, int16_t *dst)
674 iir_filter(filter_coef[0], filter_coef[1], buf +
i, filter_signal +
i, 1);
696 temp = auto_corr[1] >> 16;
706 (signal_ptr[j - 1] >> 16) *
temp) >> 16;
710 temp = 2 * scale + 4;
714 energy = auto_corr[1] >>
temp;
728 else if (gain < 0x20)
729 return gain - 8 << 7;
731 return gain - 20 << 8;
742 int i,
shift, seg, seg2, t,
val, val_add, x, y;
753 }
else if(
shift < -31) {
769 seg2 =
FFMIN(seg, 3);
774 t = seg * 32 + (
val << seg2);
783 t = seg * 32 + (
val << seg2);
786 t = seg * 32 + (
val + 1 << seg2);
788 val = (seg2 - 1) * 16 +
val;
792 t = seg * 32 + (
val - 1 << seg2);
794 val = (seg2 - 1) * 16 +
val;
825 for (j = 0; j < 11; j++) {
826 signs[
i * 11 + j] = ((t & 1) * 2 - 1) * (1 << 14);
836 for (j = 0; j <
pulses[
i]; j++, idx++) {
839 pos[idx] =
tmp[idx2] * 2 + off[
i];
858 t |=
FFABS(vector_ptr[j]);
859 t =
FFMIN(t, 0x7FFF);
870 t = vector_ptr[j] * (1 << -
shift);
876 t = vector_ptr[j] >>
shift;
883 for (j = 0; j < 11; j++)
884 b0 +=
tmp[
pos[(
i / 2) * 11 + j]] * signs[(
i / 2) * 11 + j];
885 b0 =
b0 * 2 * 2979LL + (1 << 29) >> 30;
888 if (
shift * 2 + 3 >= 0)
911 for (j = 0; j < 11; j++) {
912 idx = (
i / 2) * 11 + j;
914 (x * signs[idx] >> 15));
918 memcpy(vector_ptr +
PITCH_MAX, vector_ptr,
928 int *got_frame_ptr,
AVPacket *avpkt)
933 int buf_size = avpkt->
size;
934 int dec_mode = buf[0] & 3;
941 int bad_frame = 0,
i, j, ret;
946 "Expected %d bytes, got %d - skipping packet\n",
956 for (
int ch = 0; ch < avctx->
channels; ch++) {
958 int16_t *audio = p->
audio;
1013 if (
s->postfilter) {
1022 vector_ptr +
i + ppf[j].
index,
1083 if (
s->postfilter) {
1096 #define OFFSET(x) offsetof(G723_1_Context, x)
1097 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1101 { .i64 = 1 }, 0, 1,
AD },
static const uint8_t bits2[81]
static const uint8_t bits1[81]
void ff_acelp_weighted_vector_sum(int16_t *out, const int16_t *in_a, const int16_t *in_b, int16_t weight_coeff_a, int16_t weight_coeff_b, int16_t rounder, int shift, int length)
weighted sum of two vectors with rounding.
static double val(void *priv, double ch)
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs, const int16_t *in, int buffer_length, int filter_length, int stop_on_overflow, int shift, int rounder)
LP synthesis filter.
audio channel layout utility functions
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
G.723.1 types, functions and data tables.
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
@ ACTIVE_FRAME
Active speech.
@ SID_FRAME
Silence Insertion Descriptor frame.
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
static void formant_postfilter(G723_1_ChannelContext *p, int16_t *lpc, int16_t *buf, int16_t *dst)
Perform formant filtering.
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
static const AVClass g723_1dec_class
static const int16_t postfilter_tbl[2][LPC_ORDER]
0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
static int cng_rand(int *state, int base)
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.
static void gain_scale(G723_1_ChannelContext *p, int16_t *buf, int energy)
Adjust gain of postfiltered signal.
static const int cng_adaptive_cb_lag[4]
static int16_t square_root(unsigned val)
Bitexact implementation of sqrt(val/2).
static const AVOption options[]
static void generate_noise(G723_1_ChannelContext *p)
static int sid_gain_to_lsp_index(int gain)
AVCodec ff_g723_1_decoder
static void residual_interp(int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
Perform residual interpolation based on frame classification.
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.
static int unpack_bitstream(G723_1_ChannelContext *p, const uint8_t *buf, int buf_size)
Unpack the frame into parameters.
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.
static int estimate_sid_gain(G723_1_ChannelContext *p)
static const int16_t pitch_contrib[340]
static const int cng_bseg[3]
static int comp_interp_index(G723_1_ChannelContext *p, int pitch_lag, int *exc_eng, int *scale)
Classify frames as voiced/unvoiced.
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
static void comp_ppf_coeff(G723_1_ChannelContext *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
Calculate pitch postfilter parameters.
static const int16_t ppf_gain_weight[2]
Postfilter gain weighting factors scaled by 2^15.
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static const int cng_filt[4]
#define iir_filter(fir_coef, iir_coef, src, dest, width)
Perform IIR filtering.
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
#define LIBAVUTIL_VERSION_INT
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_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]
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Memory handling functions.
static int shift(int a, int b)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
enum AVSampleFormat sample_fmt
audio sample format
int channels
number of audio channels
uint64_t channel_layout
Audio channel layout.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
uint8_t ** extended_data
pointers to the data planes/channels.
This structure stores compressed data.
int pf_gain
formant postfilter gain scaling unit memory
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
enum FrameType past_frame_type
int16_t synth_mem[LPC_ORDER]
int16_t prev_lsp[LPC_ORDER]
uint8_t lsp_index[LSP_BANDS]
int16_t sid_lsp[LPC_ORDER]
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
enum FrameType cur_frame_type
int16_t prev_excitation[PITCH_MAX]
int16_t fir_mem[LPC_ORDER]
G723_1_Subframe subframe[4]
G723.1 unpacked data subframe.
int ad_cb_lag
adaptive codebook lag
Pitch postfilter parameters.
int16_t opt_gain
optimal gain
int16_t sc_gain
scaling gain
int index
postfilter backward/forward lag
static const uint8_t offset[127][2]
static double b0(void *priv, double x, double y)