+typedef enum {
+ I_F_Q = -1, /**< insufficient frame quality */
+ SILENCE,
+ RATE_OCTAVE,
+ RATE_QUARTER,
+ RATE_HALF,
+ RATE_FULL
+} qcelp_packet_rate;
+
+typedef struct {
+ GetBitContext gb;
+ qcelp_packet_rate bitrate;
+ QCELPFrame frame; /**< unpacked data frame */
+
+ uint8_t erasure_count;
+ uint8_t octave_count; /**< count the consecutive RATE_OCTAVE frames */
+ float prev_lspf[10];
+ float predictor_lspf[10];/**< LSP predictor for RATE_OCTAVE and I_F_Q */
+ float pitch_synthesis_filter_mem[303];
+ float pitch_pre_filter_mem[303];
+ float rnd_fir_filter_mem[180];
+ float formant_mem[170];
+ float last_codebook_gain;
+ int prev_g1[2];
+ int prev_bitrate;
+ float pitch_gain[4];
+ uint8_t pitch_lag[4];
+ uint16_t first16bits;
+ uint8_t warned_buf_mismatch_bitrate;
+
+ /* postfilter */
+ float postfilter_synth_mem[10];
+ float postfilter_agc_mem;
+ float postfilter_tilt_mem;
+} QCELPContext;
+
+/**
+ * Initialize the speech codec according to the specification.
+ *
+ * TIA/EIA/IS-733 2.4.9
+ */
+static av_cold int qcelp_decode_init(AVCodecContext *avctx)
+{
+ QCELPContext *q = avctx->priv_data;
+ int i;
+
+ avctx->channels = 1;
+ avctx->channel_layout = AV_CH_LAYOUT_MONO;
+ avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
+
+ for (i = 0; i < 10; i++)
+ q->prev_lspf[i] = (i + 1) / 11.0;
+
+ return 0;
+}
+
+/**
+ * Decode the 10 quantized LSP frequencies from the LSPV/LSP
+ * transmission codes of any bitrate and check for badly received packets.
+ *
+ * @param q the context
+ * @param lspf line spectral pair frequencies
+ *
+ * @return 0 on success, -1 if the packet is badly received
+ *
+ * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
+ */
+static int decode_lspf(QCELPContext *q, float *lspf)
+{
+ int i;
+ float tmp_lspf, smooth, erasure_coeff;
+ const float *predictors;
+
+ if (q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) {
+ predictors = q->prev_bitrate != RATE_OCTAVE &&
+ q->prev_bitrate != I_F_Q ? q->prev_lspf
+ : q->predictor_lspf;
+
+ if (q->bitrate == RATE_OCTAVE) {
+ q->octave_count++;
+
+ for (i = 0; i < 10; i++) {
+ q->predictor_lspf[i] =
+ lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR
+ : -QCELP_LSP_SPREAD_FACTOR) +
+ predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR +
+ (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR) / 11);
+ }
+ smooth = q->octave_count < 10 ? .875 : 0.1;
+ } else {
+ erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
+
+ assert(q->bitrate == I_F_Q);
+
+ if (q->erasure_count > 1)
+ erasure_coeff *= q->erasure_count < 4 ? 0.9 : 0.7;
+
+ for (i = 0; i < 10; i++) {
+ q->predictor_lspf[i] =
+ lspf[i] = (i + 1) * (1 - erasure_coeff) / 11 +
+ erasure_coeff * predictors[i];
+ }
+ smooth = 0.125;
+ }
+
+ // Check the stability of the LSP frequencies.
+ lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
+ for (i = 1; i < 10; i++)
+ lspf[i] = FFMAX(lspf[i], lspf[i - 1] + QCELP_LSP_SPREAD_FACTOR);
+
+ lspf[9] = FFMIN(lspf[9], 1.0 - QCELP_LSP_SPREAD_FACTOR);
+ for (i = 9; i > 0; i--)
+ lspf[i - 1] = FFMIN(lspf[i - 1], lspf[i] - QCELP_LSP_SPREAD_FACTOR);
+
+ // Low-pass filter the LSP frequencies.
+ ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
+ } else {
+ q->octave_count = 0;
+
+ tmp_lspf = 0.0;
+ for (i = 0; i < 5; i++) {
+ lspf[2 * i + 0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
+ lspf[2 * i + 1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
+ }
+
+ // Check for badly received packets.
+ if (q->bitrate == RATE_QUARTER) {
+ if (lspf[9] <= .70 || lspf[9] >= .97)
+ return -1;
+ for (i = 3; i < 10; i++)
+ if (fabs(lspf[i] - lspf[i - 2]) < .08)
+ return -1;
+ } else {
+ if (lspf[9] <= .66 || lspf[9] >= .985)
+ return -1;
+ for (i = 4; i < 10; i++)
+ if (fabs(lspf[i] - lspf[i - 4]) < .0931)
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/**
+ * Convert codebook transmission codes to GAIN and INDEX.
+ *
+ * @param q the context
+ * @param gain array holding the decoded gain
+ *
+ * TIA/EIA/IS-733 2.4.6.2
+ */
+static void decode_gain_and_index(QCELPContext *q, float *gain)
+{
+ int i, subframes_count, g1[16];
+ float slope;
+
+ if (q->bitrate >= RATE_QUARTER) {
+ switch (q->bitrate) {
+ case RATE_FULL: subframes_count = 16; break;
+ case RATE_HALF: subframes_count = 4; break;
+ default: subframes_count = 5;
+ }
+ for (i = 0; i < subframes_count; i++) {
+ g1[i] = 4 * q->frame.cbgain[i];
+ if (q->bitrate == RATE_FULL && !((i + 1) & 3)) {
+ g1[i] += av_clip((g1[i - 1] + g1[i - 2] + g1[i - 3]) / 3 - 6, 0, 32);
+ }
+
+ gain[i] = qcelp_g12ga[g1[i]];
+
+ if (q->frame.cbsign[i]) {
+ gain[i] = -gain[i];
+ q->frame.cindex[i] = (q->frame.cindex[i] - 89) & 127;
+ }
+ }
+
+ q->prev_g1[0] = g1[i - 2];
+ q->prev_g1[1] = g1[i - 1];
+ q->last_codebook_gain = qcelp_g12ga[g1[i - 1]];
+
+ if (q->bitrate == RATE_QUARTER) {
+ // Provide smoothing of the unvoiced excitation energy.
+ gain[7] = gain[4];
+ gain[6] = 0.4 * gain[3] + 0.6 * gain[4];
+ gain[5] = gain[3];
+ gain[4] = 0.8 * gain[2] + 0.2 * gain[3];
+ gain[3] = 0.2 * gain[1] + 0.8 * gain[2];
+ gain[2] = gain[1];
+ gain[1] = 0.6 * gain[0] + 0.4 * gain[1];
+ }
+ } else if (q->bitrate != SILENCE) {
+ if (q->bitrate == RATE_OCTAVE) {
+ g1[0] = 2 * q->frame.cbgain[0] +
+ av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
+ subframes_count = 8;
+ } else {
+ assert(q->bitrate == I_F_Q);
+
+ g1[0] = q->prev_g1[1];
+ switch (q->erasure_count) {
+ case 1 : break;
+ case 2 : g1[0] -= 1; break;
+ case 3 : g1[0] -= 2; break;
+ default: g1[0] -= 6;
+ }
+ if (g1[0] < 0)
+ g1[0] = 0;
+ subframes_count = 4;
+ }
+ // This interpolation is done to produce smoother background noise.
+ slope = 0.5 * (qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
+ for (i = 1; i <= subframes_count; i++)
+ gain[i - 1] = q->last_codebook_gain + slope * i;
+
+ q->last_codebook_gain = gain[i - 2];
+ q->prev_g1[0] = q->prev_g1[1];
+ q->prev_g1[1] = g1[0];
+ }
+}
+
+/**
+ * If the received packet is Rate 1/4 a further sanity check is made of the
+ * codebook gain.
+ *
+ * @param cbgain the unpacked cbgain array
+ * @return -1 if the sanity check fails, 0 otherwise
+ *
+ * TIA/EIA/IS-733 2.4.8.7.3
+ */
+static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
+{
+ int i, diff, prev_diff = 0;
+
+ for (i = 1; i < 5; i++) {
+ diff = cbgain[i] - cbgain[i-1];
+ if (FFABS(diff) > 10)
+ return -1;
+ else if (FFABS(diff - prev_diff) > 12)
+ return -1;
+ prev_diff = diff;
+ }
+ return 0;
+}
+
+/**
+ * Compute the scaled codebook vector Cdn From INDEX and GAIN
+ * for all rates.
+ *
+ * The specification lacks some information here.
+ *
+ * TIA/EIA/IS-733 has an omission on the codebook index determination
+ * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
+ * you have to subtract the decoded index parameter from the given scaled
+ * codebook vector index 'n' to get the desired circular codebook index, but
+ * it does not mention that you have to clamp 'n' to [0-9] in order to get
+ * RI-compliant results.
+ *
+ * The reason for this mistake seems to be the fact they forgot to mention you
+ * have to do these calculations per codebook subframe and adjust given
+ * equation values accordingly.
+ *
+ * @param q the context
+ * @param gain array holding the 4 pitch subframe gain values
+ * @param cdn_vector array for the generated scaled codebook vector
+ */
+static void compute_svector(QCELPContext *q, const float *gain,
+ float *cdn_vector)
+{
+ int i, j, k;
+ uint16_t cbseed, cindex;
+ float *rnd, tmp_gain, fir_filter_value;
+
+ switch (q->bitrate) {
+ case RATE_FULL:
+ for (i = 0; i < 16; i++) {
+ tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
+ cindex = -q->frame.cindex[i];
+ for (j = 0; j < 10; j++)
+ *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
+ }
+ break;
+ case RATE_HALF:
+ for (i = 0; i < 4; i++) {
+ tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
+ cindex = -q->frame.cindex[i];
+ for (j = 0; j < 40; j++)
+ *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
+ }
+ break;
+ case RATE_QUARTER:
+ cbseed = (0x0003 & q->frame.lspv[4]) << 14 |
+ (0x003F & q->frame.lspv[3]) << 8 |
+ (0x0060 & q->frame.lspv[2]) << 1 |
+ (0x0007 & q->frame.lspv[1]) << 3 |
+ (0x0038 & q->frame.lspv[0]) >> 3;
+ rnd = q->rnd_fir_filter_mem + 20;
+ for (i = 0; i < 8; i++) {
+ tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
+ for (k = 0; k < 20; k++) {
+ cbseed = 521 * cbseed + 259;
+ *rnd = (int16_t) cbseed;
+
+ // FIR filter
+ fir_filter_value = 0.0;
+ for (j = 0; j < 10; j++)
+ fir_filter_value += qcelp_rnd_fir_coefs[j] *
+ (rnd[-j] + rnd[-20+j]);
+
+ fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
+ *cdn_vector++ = tmp_gain * fir_filter_value;
+ rnd++;
+ }
+ }
+ memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160,
+ 20 * sizeof(float));
+ break;
+ case RATE_OCTAVE:
+ cbseed = q->first16bits;
+ for (i = 0; i < 8; i++) {
+ tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
+ for (j = 0; j < 20; j++) {
+ cbseed = 521 * cbseed + 259;
+ *cdn_vector++ = tmp_gain * (int16_t) cbseed;
+ }
+ }
+ break;
+ case I_F_Q:
+ cbseed = -44; // random codebook index
+ for (i = 0; i < 4; i++) {
+ tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
+ for (j = 0; j < 40; j++)
+ *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
+ }
+ break;
+ case SILENCE:
+ memset(cdn_vector, 0, 160 * sizeof(float));
+ break;
+ }
+}
+
+/**
+ * Apply generic gain control.
+ *
+ * @param v_out output vector
+ * @param v_in gain-controlled vector
+ * @param v_ref vector to control gain of
+ *
+ * TIA/EIA/IS-733 2.4.8.3, 2.4.8.6
+ */
+static void apply_gain_ctrl(float *v_out, const float *v_ref, const float *v_in)
+{
+ int i;
+
+ for (i = 0; i < 160; i += 40) {
+ float res = avpriv_scalarproduct_float_c(v_ref + i, v_ref + i, 40);
+ ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i, res, 40);
+ }
+}
+