#include "get_bits.h"
#include "acelp_vectors.h"
#include "celp_filters.h"
-#include "celp_math.h"
-#include "lsp.h"
#include "g723_1_data.h"
/**
/**
* Scale vector contents based on the largest of their absolutes.
*/
-static int scale_vector(int16_t *vector, int length)
+static int scale_vector(int16_t *dst, const int16_t *vector, int length)
{
int bits, max = 0;
- int64_t scale;
int i;
for (i = 0; i < length; i++)
- max = FFMAX(max, FFABS(vector[i]));
+ max |= FFABS(vector[i]);
+ max = FFMIN(max, 0x7FFF);
bits = normalize_bits(max, 15);
- scale = (bits == 15) ? 0x7FFF : (1 << bits);
- for (i = 0; i < length; i++)
- vector[i] = av_clipl_int32(vector[i] * scale << 1) >> 4;
+ if (bits == 15)
+ for (i = 0; i < length; i++)
+ dst[i] = vector[i] * 0x7fff >> 3;
+ else
+ for (i = 0; i < length; i++)
+ dst[i] = vector[i] << bits >> 3;
return bits - 3;
}
for (j = 0; j < LPC_ORDER; j++) {
int index = lpc[j] >> 7;
int offset = lpc[j] & 0x7f;
- int64_t temp1 = cos_tab[index] << 16;
+ int temp1 = cos_tab[index] << 16;
int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
((offset << 8) + 0x80) << 1;
- lpc[j] = -(av_clipl_int32(((temp1 + temp2) << 1) + (1 << 15)) >> 16);
+ lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
}
/*
residual[i] = prev_excitation[offset + (i - 2) % lag];
}
-static int dot_product(const int16_t *a, const int16_t *b, int length,
- int shift)
+static int dot_product(const int16_t *a, const int16_t *b, int length)
{
int i, sum = 0;
for (i = 0; i < length; i++) {
- int64_t prod = av_clipl_int32(MUL64(a[i], b[i]) << shift);
- sum = av_clipl_int32(sum + prod);
+ int prod = a[i] * b[i];
+ sum = av_sat_dadd32(sum, prod);
}
return sum;
}
int lag = pitch_lag + subfrm.ad_cb_lag - 1;
int i;
- int64_t sum;
+ int sum;
get_residual(residual, prev_excitation, lag);
/* Calculate adaptive vector */
cb_ptr += subfrm.ad_cb_gain * 20;
for (i = 0; i < SUBFRAME_LEN; i++) {
- sum = dot_product(residual + i, cb_ptr, PITCH_ORDER, 1);
- vector[i] = av_clipl_int32((sum << 1) + (1 << 15)) >> 16;
+ sum = dot_product(residual + i, cb_ptr, PITCH_ORDER);
+ vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
}
}
/**
* Estimate maximum auto-correlation around pitch lag.
*
- * @param p the context
+ * @param buf buffer with offset applied
* @param offset offset of the excitation vector
* @param ccr_max pointer to the maximum auto-correlation
* @param pitch_lag decoded pitch lag
* @param length length of autocorrelation
* @param dir forward lag(1) / backward lag(-1)
*/
-static int autocorr_max(G723_1_Context *p, int offset, int *ccr_max,
+static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
int pitch_lag, int length, int dir)
{
int limit, ccr, lag = 0;
- int16_t *buf = p->excitation + offset;
int i;
pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
limit = pitch_lag + 3;
for (i = pitch_lag - 3; i <= limit; i++) {
- ccr = dot_product(buf, buf + dir * i, length, 1);
+ ccr = dot_product(buf, buf + dir * i, length);
if (ccr > *ccr_max) {
*ccr_max = ccr;
int tgt_eng, int ccr, int res_eng)
{
int pf_residual; /* square of postfiltered residual */
- int64_t temp1, temp2;
+ int temp1, temp2;
ppf->index = lag;
/* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
- pf_residual = av_clipl_int32(temp1 + temp2 + (1 << 15)) >> 16;
+ pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
if (tgt_eng >= pf_residual << 1) {
temp1 = 0x7fff;
int16_t scale;
int i;
- int64_t temp1, temp2;
+ int temp1, temp2;
/*
* 0 - target energy
*/
int energy[5] = {0, 0, 0, 0, 0};
int16_t *buf = p->excitation + offset;
- int fwd_lag = autocorr_max(p, offset, &energy[1], pitch_lag,
+ int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
SUBFRAME_LEN, 1);
- int back_lag = autocorr_max(p, offset, &energy[3], pitch_lag,
+ int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
SUBFRAME_LEN, -1);
ppf->index = 0;
return;
/* Compute target energy */
- energy[0] = dot_product(buf, buf, SUBFRAME_LEN, 1);
+ energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
/* Compute forward residual energy */
if (fwd_lag)
- energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag,
- SUBFRAME_LEN, 1);
+ energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
/* Compute backward residual energy */
if (back_lag)
- energy[4] = dot_product(buf - back_lag, buf - back_lag,
- SUBFRAME_LEN, 1);
+ energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
/* Normalize and shorten */
temp1 = 0;
int *exc_eng, int *scale)
{
int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
- int16_t *buf = p->excitation + offset;
+ const int16_t *buf = p->excitation + offset;
int index, ccr, tgt_eng, best_eng, temp;
- *scale = scale_vector(p->excitation, FRAME_LEN + PITCH_MAX);
+ *scale = scale_vector(p->excitation, p->excitation, FRAME_LEN + PITCH_MAX);
/* Compute maximum backward cross-correlation */
ccr = 0;
- index = autocorr_max(p, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
- ccr = av_clipl_int32((int64_t)ccr + (1 << 15)) >> 16;
+ index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
+ ccr = av_sat_add32(ccr, 1 << 15) >> 16;
/* Compute target energy */
- tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2, 1);
- *exc_eng = av_clipl_int32((int64_t)tgt_eng + (1 << 15)) >> 16;
+ tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
+ *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
if (ccr <= 0)
return 0;
/* Compute best energy */
- best_eng = dot_product(buf - index, buf - index,
- SUBFRAME_LEN * 2, 1);
- best_eng = av_clipl_int32((int64_t)best_eng + (1 << 15)) >> 16;
+ best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
+ best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
temp = best_eng * *exc_eng >> 3;
num = energy;
denom = 0;
for (i = 0; i < SUBFRAME_LEN; i++) {
- int64_t temp = buf[i] >> 2;
- temp = av_clipl_int32(MUL64(temp, temp) << 1);
- denom = av_clipl_int32(denom + temp);
+ int temp = buf[i] >> 2;
+ temp *= temp;
+ denom = av_sat_dadd32(denom, temp);
}
if (num && denom) {
}
for (i = 0; i < SUBFRAME_LEN; i++) {
- p->pf_gain = ((p->pf_gain << 4) - p->pf_gain + gain + (1 << 3)) >> 4;
+ p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
(1 << 10)) >> 11);
}
*
* @param p the context
* @param lpc quantized lpc coefficients
- * @param buf output buffer
+ * @param buf input buffer
+ * @param dst output buffer
*/
-static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf)
+static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
+ int16_t *buf, int16_t *dst)
{
- int16_t filter_coef[2][LPC_ORDER], *buf_ptr;
+ int16_t filter_coef[2][LPC_ORDER];
int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
int i, j, k;
}
iir_filter(filter_coef[0], filter_coef[1], buf + i,
filter_signal + i);
+ lpc += LPC_ORDER;
}
memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(*p->fir_mem));
memcpy(p->iir_mem, filter_signal + FRAME_LEN,
LPC_ORDER * sizeof(*p->iir_mem));
- buf_ptr = buf + LPC_ORDER;
+ buf += LPC_ORDER;
signal_ptr = filter_signal + LPC_ORDER;
for (i = 0; i < SUBFRAMES; i++) {
- int16_t temp_vector[SUBFRAME_LEN];
- int16_t temp;
+ int temp;
int auto_corr[2];
int scale, energy;
/* Normalize */
- memcpy(temp_vector, buf_ptr, SUBFRAME_LEN * sizeof(*temp_vector));
- scale = scale_vector(temp_vector, SUBFRAME_LEN);
+ scale = scale_vector(dst, buf, SUBFRAME_LEN);
/* Compute auto correlation coefficients */
- auto_corr[0] = dot_product(temp_vector, temp_vector + 1,
- SUBFRAME_LEN - 1, 1);
- auto_corr[1] = dot_product(temp_vector, temp_vector, SUBFRAME_LEN, 1);
+ auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
+ auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
/* Compute reflection coefficient */
temp = auto_corr[1] >> 16;
if (temp) {
temp = (auto_corr[0] >> 2) / temp;
}
- p->reflection_coef = ((p->reflection_coef << 2) - p->reflection_coef +
- temp + 2) >> 2;
- temp = (p->reflection_coef * 0xffffc >> 3) & 0xfffc;
+ p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
+ temp = -p->reflection_coef >> 1 & ~3;
/* Compensation filter */
for (j = 0; j < SUBFRAME_LEN; j++) {
- buf_ptr[j] = av_clipl_int32(signal_ptr[j] +
- ((signal_ptr[j - 1] >> 16) *
- temp << 1)) >> 16;
+ dst[j] = av_sat_dadd32(signal_ptr[j],
+ (signal_ptr[j - 1] >> 16) * temp) >> 16;
}
/* Compute normalized signal energy */
} else
energy = auto_corr[1] >> temp;
- gain_scale(p, buf_ptr, energy);
+ gain_scale(p, dst, energy);
- buf_ptr += SUBFRAME_LEN;
+ buf += SUBFRAME_LEN;
signal_ptr += SUBFRAME_LEN;
+ dst += SUBFRAME_LEN;
}
}
int16_t lpc[SUBFRAMES * LPC_ORDER];
int16_t acb_vector[SUBFRAME_LEN];
int16_t *vector_ptr;
+ int16_t *out;
int bad_frame = 0, i, j, ret;
if (buf_size < frame_size[dec_mode]) {
return ret;
}
+ out = (int16_t *)p->frame.data[0];
+
if (p->cur_frame_type == ACTIVE_FRAME) {
if (!bad_frame)
p->erased_frames = 0;
memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
PITCH_MAX * sizeof(*p->excitation));
} else {
- memset(p->frame.data[0], 0, FRAME_LEN * 2);
+ memset(out, 0, FRAME_LEN * 2);
av_log(avctx, AV_LOG_WARNING,
"G.723.1: Comfort noise generation not supported yet\n");
0, 1, 1 << 12);
memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
- if (p->postfilter)
- formant_postfilter(p, lpc, p->audio);
-
- memcpy(p->frame.data[0], p->audio + LPC_ORDER, FRAME_LEN * 2);
+ if (p->postfilter) {
+ formant_postfilter(p, lpc, p->audio, out);
+ } else { // if output is not postfiltered it should be scaled by 2
+ for (i = 0; i < FRAME_LEN; i++)
+ out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
+ }
*got_frame_ptr = 1;
*(AVFrame *)data = p->frame;
AVCodec ff_g723_1_decoder = {
.name = "g723_1",
.type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_G723_1,
+ .id = AV_CODEC_ID_G723_1,
.priv_data_size = sizeof(G723_1_Context),
.init = g723_1_decode_init,
.decode = g723_1_decode_frame,