+static void postfilter(QCELPContext *q, float *samples, float *lpc)
+{
+ static const float pow_0_775[10] = {
+ 0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
+ 0.216676, 0.167924, 0.130141, 0.100859, 0.078166
+ }, pow_0_625[10] = {
+ 0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
+ 0.059605, 0.037253, 0.023283, 0.014552, 0.009095
+ };
+ float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
+ int n;
+
+ for (n = 0; n < 10; n++) {
+ lpc_s[n] = lpc[n] * pow_0_625[n];
+ lpc_p[n] = lpc[n] * pow_0_775[n];
+ }
+
+ ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
+ q->formant_mem + 10, 160, 10);
+ memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
+ ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
+ memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
+
+ ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
+
+ ff_adaptive_gain_control(samples, pole_out + 10,
+ ff_dot_productf(q->formant_mem + 10, q->formant_mem + 10, 160),
+ 160, 0.9375, &q->postfilter_agc_mem);
+}
+
+static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
+ AVPacket *avpkt)
+{
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
+ QCELPContext *q = avctx->priv_data;
+ float *outbuffer = data;
+ int i;
+ float quantized_lspf[10], lpc[10];
+ float gain[16];
+ float *formant_mem;
+
+ if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q)
+ {
+ warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
+ goto erasure;
+ }
+
+ if(q->bitrate == RATE_OCTAVE &&
+ (q->first16bits = AV_RB16(buf)) == 0xFFFF)
+ {
+ warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
+ goto erasure;
+ }
+
+ if(q->bitrate > SILENCE)
+ {
+ const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
+ const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
+ + qcelp_unpacking_bitmaps_lengths[q->bitrate];
+ uint8_t *unpacked_data = (uint8_t *)&q->frame;
+
+ init_get_bits(&q->gb, buf, 8*buf_size);
+
+ memset(&q->frame, 0, sizeof(QCELPFrame));
+
+ for(; bitmaps < bitmaps_end; bitmaps++)
+ unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
+
+ // Check for erasures/blanks on rates 1, 1/4 and 1/8.
+ if(q->frame.reserved)
+ {
+ warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
+ goto erasure;
+ }
+ if(q->bitrate == RATE_QUARTER &&
+ codebook_sanity_check_for_rate_quarter(q->frame.cbgain))
+ {
+ warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
+ goto erasure;
+ }
+
+ if(q->bitrate >= RATE_HALF)
+ {
+ for(i=0; i<4; i++)
+ {
+ if(q->frame.pfrac[i] && q->frame.plag[i] >= 124)
+ {
+ warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
+ goto erasure;
+ }
+ }
+ }
+ }
+
+ decode_gain_and_index(q, gain);
+ compute_svector(q, gain, outbuffer);
+
+ if(decode_lspf(q, quantized_lspf) < 0)
+ {
+ warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
+ goto erasure;
+ }
+
+
+ apply_pitch_filters(q, outbuffer);
+
+ if(q->bitrate == I_F_Q)
+ {
+erasure:
+ q->bitrate = I_F_Q;
+ q->erasure_count++;
+ decode_gain_and_index(q, gain);
+ compute_svector(q, gain, outbuffer);
+ decode_lspf(q, quantized_lspf);
+ apply_pitch_filters(q, outbuffer);
+ }else
+ q->erasure_count = 0;
+
+ formant_mem = q->formant_mem + 10;
+ for(i=0; i<4; i++)
+ {
+ interpolate_lpc(q, quantized_lspf, lpc, i);
+ ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40,
+ 10);
+ formant_mem += 40;
+ }
+
+ // postfilter, as per TIA/EIA/IS-733 2.4.8.6
+ postfilter(q, outbuffer, lpc);
+
+ memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
+
+ memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
+ q->prev_bitrate = q->bitrate;
+
+ *data_size = 160 * sizeof(*outbuffer);
+
+ return *data_size;
+}
+
+AVCodec ff_qcelp_decoder =