32 #define BITSTREAM_READER_LE
41 #define CNG_RANDOM_SEED 12345
73 int temp, info_bits, i;
187 return (
ff_sqrt(val << 1) >> 1) & (~1);
200 enum Rate cur_rate,
int pitch_lag,
int index)
237 for (i = 0; i < 8; i += 2) {
238 offset = ((cb_pos & 7) << 3) + cb_shift + i;
239 vector[
offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
251 vector[i] += beta * vector[i - lag] >> 15;
267 int pitch_lag,
int length,
int dir)
269 int limit, ccr, lag = 0;
276 limit = pitch_lag + 3;
278 for (i = pitch_lag - 3; i <= limit; i++) {
281 if (ccr > *ccr_max) {
300 int tgt_eng,
int ccr,
int res_eng)
307 temp1 = tgt_eng * res_eng >> 1;
308 temp2 = ccr * ccr << 1;
311 if (ccr >= res_eng) {
314 ppf->
opt_gain = (ccr << 15) / res_eng *
318 temp1 = (tgt_eng << 15) + (ccr * ppf->
opt_gain << 1);
320 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
322 if (tgt_eng >= pf_residual << 1) {
325 temp1 = (tgt_eng << 14) / pf_residual;
362 int energy[5] = {0, 0, 0, 0, 0};
364 int fwd_lag =
autocorr_max(buf, offset, &energy[1], pitch_lag,
366 int back_lag =
autocorr_max(buf, offset, &energy[3], pitch_lag,
374 if (!back_lag && !fwd_lag)
392 for (i = 0; i < 5; i++)
393 temp1 =
FFMAX(energy[i], temp1);
396 for (i = 0; i < 5; i++)
397 energy[i] = (energy[i] << scale) >> 16;
399 if (fwd_lag && !back_lag) {
402 }
else if (!fwd_lag) {
411 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
412 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
413 if (temp1 >= temp2) {
434 int *exc_eng,
int *scale)
446 index =
autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
447 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
451 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
459 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
461 temp = best_eng * *exc_eng >> 3;
463 if (temp < ccr * ccr) {
479 int gain,
int *rseed)
485 for (i = 0; i < lag; i++)
486 out[i] = vector_ptr[i - lag] * 3 >> 2;
491 *rseed = *rseed * 521 + 259;
492 out[i] = gain * *rseed >> 15;
494 memset(buf, 0, (FRAME_LEN +
PITCH_MAX) *
sizeof(*buf));
507 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
510 int res_shift = 16 & ~-(width);\
511 int in_shift = 16 - res_shift;\
513 for (m = 0; m < SUBFRAME_LEN; m++) {\
515 for (n = 1; n <= LPC_ORDER; n++) {\
516 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
517 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
520 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
521 (1 << 15)) >> res_shift;\
540 int temp = buf[i] >> 2;
542 denom = av_sat_dadd32(denom, temp);
548 num = num << bits1 >> 1;
551 bits2 = 5 + bits1 -
bits2;
552 bits2 =
FFMAX(0, bits2);
554 gain = (num >> 1) / (denom >> 16);
562 buf[i] = av_clip_int16((buf[i] * (p->
pf_gain + (p->
pf_gain >> 4)) +
576 int16_t *
buf, int16_t *dst)
592 iir_filter(filter_coef[0], filter_coef[1], buf + i, filter_signal + i, 1);
614 temp = auto_corr[1] >> 16;
616 temp = (auto_corr[0] >> 2) / temp;
623 dst[j] = av_sat_dadd32(signal_ptr[j],
624 (signal_ptr[j - 1] >> 16) * temp) >> 16;
628 temp = 2 * scale + 4;
630 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
632 energy = auto_corr[1] >>
temp;
646 else if (gain < 0x20)
647 return gain - 8 << 7;
649 return gain - 20 << 8;
654 *state = (*state * 521 + 259) & 0xFFFF;
655 return (*state & 0x7FFF) * base >> 15;
660 int i,
shift, seg, seg2, t,
val, val_add, x, y;
679 seg2 =
FFMIN(seg, 3);
683 for (i = 0; i <
shift; i++) {
684 t = seg * 32 + (val << seg2);
693 t = seg * 32 + (val << seg2);
696 t = seg * 32 + (val + 1 << seg2);
698 val = (seg2 - 1 << 4) + val;
702 t = seg * 32 + (val - 1 << seg2);
704 val = (seg2 - 1 << 4) + val;
730 for (i = 0; i < SUBFRAMES / 2; i++) {
735 for (j = 0; j < 11; j++) {
736 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
746 for (j = 0; j <
pulses[i]; j++, idx++) {
749 pos[idx] = tmp[idx2] * 2 + off[i];
750 tmp[idx2] = tmp[--t];
768 t |=
FFABS(vector_ptr[j]);
769 t =
FFMIN(t, 0x7FFF);
779 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
780 t = vector_ptr[j] << -
shift;
785 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
786 t = vector_ptr[j] >>
shift;
793 for (j = 0; j < 11; j++)
794 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
795 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30;
798 if (shift * 2 + 3 >= 0)
801 c <<= -(shift * 2 + 3);
802 c = (av_clipl_int32(sum << 1) -
c) * 2979LL >> 15;
804 delta = b0 * b0 * 2 -
c;
819 x = av_clip(x, -10000, 10000);
821 for (j = 0; j < 11; j++) {
822 idx = (i / 2) * 11 + j;
823 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
824 (x * signs[idx] >> 15));
828 memcpy(vector_ptr +
PITCH_MAX, vector_ptr,
829 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
830 vector_ptr += SUBFRAME_LEN * 2;
838 int *got_frame_ptr,
AVPacket *avpkt)
843 int buf_size = avpkt->
size;
844 int dec_mode = buf[0] & 3;
851 int bad_frame = 0, i, j, ret;
852 int16_t *audio = p->
audio;
857 "Expected %d bytes, got %d - skipping packet\n",
875 out = (int16_t *)frame->
data[0];
907 int v = av_clip_int16(vector_ptr[j] << 1);
908 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
928 vector_ptr + i + ppf[j].
index,
947 memset(frame->
data[0], 0,
993 out[i] = av_clip_int16(p->
audio[LPC_ORDER + i] << 1);
1001 #define OFFSET(x) offsetof(G723_1_Context, x)
1002 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1006 { .i64 = 1 }, 0, 1,
AD },
1027 .priv_class = &g723_1dec_class,
int16_t audio[FRAME_LEN+LPC_ORDER+PITCH_MAX+4]
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.
const char const char void * val
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
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 unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
#define LIBAVUTIL_VERSION_INT
Memory handling functions.
static av_cold int init(AVCodecContext *avctx)
G723.1 unpacked data subframe.
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.
int16_t fir_mem[LPC_ORDER]
int16_t excitation[PITCH_MAX+FRAME_LEN+4]
static const AVClass g723_1dec_class
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
static void residual_interp(int16_t *buf, int16_t *out, int lag, int gain, int *rseed)
Perform residual interpolation based on frame classification.
static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, int buf_size)
Unpack the frame into parameters.
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
enum FrameType past_frame_type
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
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.
static const int cng_filt[4]
enum AVSampleFormat sample_fmt
audio sample format
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static int comp_interp_index(G723_1_Context *p, int pitch_lag, int *exc_eng, int *scale)
Classify frames as voiced/unvoiced.
static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag, PPFParam *ppf, enum Rate cur_rate)
Calculate pitch postfilter parameters.
static const uint8_t bits2[81]
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
bitstream reader API header.
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...
static const int32_t combinatorial_table[PULSE_MAX][SUBFRAME_LEN/GRID_SIZE]
Used for the coding/decoding of the pulses positions for the MP-MLQ codebook.
int16_t sid_lsp[LPC_ORDER]
int ff_g723_1_normalize_bits(int num, int width)
Calculate the number of left-shifts required for normalizing the input.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
Generate a train of dirac functions with period as pitch lag.
static void gain_scale(G723_1_Context *p, int16_t *buf, int energy)
Adjust gain of postfiltered signal.
const char * name
Name of the codec implementation.
int16_t prev_excitation[PITCH_MAX]
static const uint8_t offset[127][2]
uint64_t channel_layout
Audio channel layout.
static int estimate_sid_gain(G723_1_Context *p)
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.
G723_1_Subframe subframe[4]
static const int16_t fixed_cb_gain[GAIN_LEVELS]
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 const int16_t postfilter_tbl[2][LPC_ORDER]
0.65^i (Zero part) and 0.75^i (Pole part) scaled by 2^15
audio channel layout utility functions
int16_t synth_mem[LPC_ORDER]
AVCodec ff_g723_1_decoder
static const int cng_adaptive_cb_lag[4]
int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int index
postfilter backward/forward lag
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.
#define iir_filter(fir_coef, iir_coef, src, dest, width)
Perform IIR filtering.
int16_t opt_gain
optimal gain
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.
Libavcodec external API header.
static const int16_t dc_lsp[LPC_ORDER]
LSP DC component.
static const int16_t pitch_contrib[340]
main external API structure.
static const int16_t ppf_gain_weight[2]
Postfilter gain weighting factors scaled by 2^15.
static int sid_gain_to_lsp_index(int gain)
Silence Insertion Descriptor frame.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
G.723.1 types, functions and data tables.
static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf, int16_t *dst)
Perform formant filtering.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
Describe the class of an AVClass context structure.
int16_t sc_gain
scaling gain
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
enum FrameType cur_frame_type
int16_t prev_lsp[LPC_ORDER]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int pf_gain
formant postfilter gain scaling unit memory
common internal api header.
Pitch postfilter parameters.
static const int cng_bseg[3]
int channels
number of audio channels
static int16_t square_root(unsigned val)
Bitexact implementation of sqrt(val/2).
static const AVOption options[]
uint8_t lsp_index[LSP_BANDS]
static av_cold int g723_1_decode_init(AVCodecContext *avctx)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
static int cng_rand(int *state, int base)
static void generate_noise(G723_1_Context *p)
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int g723_1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static const uint8_t bits1[81]
int ad_cb_lag
adaptive codebook lag