2 * Enhanced Variable Rate Codec, Service Option 3 decoder
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Enhanced Variable Rate Codec, Service Option 3 decoder
25 * @author Paul B Mahol
28 #include "libavutil/mathematics.h"
33 #include "acelp_vectors.h"
36 #define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
39 #define NB_SUBFRAMES 3
40 #define SUBFRAME_SIZE 54
41 #define FILTER_ORDER 10
54 * EVRC-A unpacked data frame
56 typedef struct EVRCAFrame {
57 uint8_t lpc_flag; ///< spectral change indicator
58 uint16_t lsp[4]; ///< index into LSP codebook
59 uint8_t pitch_delay; ///< pitch delay for entire frame
60 uint8_t delay_diff; ///< delay difference for entire frame
61 uint8_t acb_gain[3]; ///< adaptive codebook gain
62 uint16_t fcb_shape[3][4]; ///< fixed codebook shape
63 uint8_t fcb_gain[3]; ///< fixed codebook gain index
64 uint8_t energy_gain; ///< frame energy gain index
65 uint8_t tty; ///< tty baud rate bit
68 typedef struct EVRCContext {
70 evrc_packet_rate bitrate;
71 evrc_packet_rate last_valid_bitrate;
74 float lspf[FILTER_ORDER];
75 float prev_lspf[FILTER_ORDER];
76 float synthesis[FILTER_ORDER];
77 float postfilter_fir[FILTER_ORDER];
78 float postfilter_iir[FILTER_ORDER];
79 float postfilter_residual[ACB_SIZE + SUBFRAME_SIZE];
81 float prev_pitch_delay;
82 float avg_acb_gain; ///< average adaptive codebook gain
83 float avg_fcb_gain; ///< average fixed codebook gain
84 float pitch[ACB_SIZE + FILTER_ORDER + SUBFRAME_SIZE];
85 float pitch_back[ACB_SIZE];
86 float interpolation_coeffs[136];
87 float energy_vector[NB_SUBFRAMES];
91 uint8_t prev_energy_gain;
92 uint8_t prev_error_flag;
93 uint8_t warned_buf_mismatch_bitrate;
97 * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
99 * @param e the context
101 * TIA/IS-127 Table 4.21-1
103 static void unpack_frame(EVRCContext *e)
105 EVRCAFrame *frame = &e->frame;
106 GetBitContext *gb = &e->gb;
108 switch (e->bitrate) {
110 frame->lpc_flag = get_bits1(gb);
111 frame->lsp[0] = get_bits(gb, 6);
112 frame->lsp[1] = get_bits(gb, 6);
113 frame->lsp[2] = get_bits(gb, 9);
114 frame->lsp[3] = get_bits(gb, 7);
115 frame->pitch_delay = get_bits(gb, 7);
116 frame->delay_diff = get_bits(gb, 5);
117 frame->acb_gain[0] = get_bits(gb, 3);
118 frame->fcb_shape[0][0] = get_bits(gb, 8);
119 frame->fcb_shape[0][1] = get_bits(gb, 8);
120 frame->fcb_shape[0][2] = get_bits(gb, 8);
121 frame->fcb_shape[0][3] = get_bits(gb, 11);
122 frame->fcb_gain[0] = get_bits(gb, 5);
123 frame->acb_gain[1] = get_bits(gb, 3);
124 frame->fcb_shape[1][0] = get_bits(gb, 8);
125 frame->fcb_shape[1][1] = get_bits(gb, 8);
126 frame->fcb_shape[1][2] = get_bits(gb, 8);
127 frame->fcb_shape[1][3] = get_bits(gb, 11);
128 frame->fcb_gain [1] = get_bits(gb, 5);
129 frame->acb_gain [2] = get_bits(gb, 3);
130 frame->fcb_shape[2][0] = get_bits(gb, 8);
131 frame->fcb_shape[2][1] = get_bits(gb, 8);
132 frame->fcb_shape[2][2] = get_bits(gb, 8);
133 frame->fcb_shape[2][3] = get_bits(gb, 11);
134 frame->fcb_gain [2] = get_bits(gb, 5);
135 frame->tty = get_bits1(gb);
138 frame->lsp [0] = get_bits(gb, 7);
139 frame->lsp [1] = get_bits(gb, 7);
140 frame->lsp [2] = get_bits(gb, 8);
141 frame->pitch_delay = get_bits(gb, 7);
142 frame->acb_gain [0] = get_bits(gb, 3);
143 frame->fcb_shape[0][0] = get_bits(gb, 10);
144 frame->fcb_gain [0] = get_bits(gb, 4);
145 frame->acb_gain [1] = get_bits(gb, 3);
146 frame->fcb_shape[1][0] = get_bits(gb, 10);
147 frame->fcb_gain [1] = get_bits(gb, 4);
148 frame->acb_gain [2] = get_bits(gb, 3);
149 frame->fcb_shape[2][0] = get_bits(gb, 10);
150 frame->fcb_gain [2] = get_bits(gb, 4);
153 frame->lsp [0] = get_bits(gb, 4);
154 frame->lsp [1] = get_bits(gb, 4);
155 frame->energy_gain = get_bits(gb, 8);
160 static evrc_packet_rate buf_size2bitrate(const int buf_size)
163 case 23: return RATE_FULL;
164 case 11: return RATE_HALF;
165 case 6: return RATE_QUARTER;
166 case 3: return RATE_QUANT;
167 case 1: return SILENCE;
174 * Determine the bitrate from the frame size and/or the first byte of the frame.
176 * @param avctx the AV codec context
177 * @param buf_size length of the buffer
178 * @param buf the bufffer
180 * @return the bitrate on success,
181 * RATE_ERRS if the bitrate cannot be satisfactorily determined
183 static evrc_packet_rate determine_bitrate(AVCodecContext *avctx,
187 evrc_packet_rate bitrate;
189 if ((bitrate = buf_size2bitrate(*buf_size)) >= 0) {
190 if (bitrate > **buf) {
191 EVRCContext *e = avctx->priv_data;
192 if (!e->warned_buf_mismatch_bitrate) {
193 av_log(avctx, AV_LOG_WARNING,
194 "Claimed bitrate and buffer size mismatch.\n");
195 e->warned_buf_mismatch_bitrate = 1;
198 } else if (bitrate < **buf) {
199 av_log(avctx, AV_LOG_ERROR,
200 "Buffer is too small for the claimed bitrate.\n");
205 } else if ((bitrate = buf_size2bitrate(*buf_size + 1)) >= 0) {
206 av_log(avctx, AV_LOG_DEBUG,
207 "Bitrate byte is missing, guessing the bitrate from packet size.\n");
214 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
217 av_log(avctx, AV_LOG_WARNING, "Frame #%d, %s\n",
218 avctx->frame_number, message);
222 * Initialize the speech codec according to the specification.
226 static av_cold int evrc_decode_init(AVCodecContext *avctx)
228 EVRCContext *e = avctx->priv_data;
230 float denom = 2.0 / (2.0 * 8.0 + 1.0);
233 avctx->channel_layout = AV_CH_LAYOUT_MONO;
234 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
236 for (i = 0; i < FILTER_ORDER; i++) {
237 e->prev_lspf[i] = (i + 1) * 0.048;
238 e->synthesis[i] = 0.0;
241 for (i = 0; i < ACB_SIZE; i++)
242 e->pitch[i] = e->pitch_back[i] = 0.0;
244 e->last_valid_bitrate = RATE_QUANT;
245 e->prev_pitch_delay = 40.0;
247 e->prev_error_flag = 0;
248 e->avg_acb_gain = e->avg_fcb_gain = 0.0;
250 for (i = 0; i < 8; i++) {
251 float tt = ((float)i - 8.0 / 2.0) / 8.0;
253 for (n = -8; n <= 8; n++, idx++) {
254 float arg1 = M_PI * 0.9 * (tt - n);
255 float arg2 = M_PI * (tt - n);
257 e->interpolation_coeffs[idx] = 0.9;
259 e->interpolation_coeffs[idx] *= (0.54 + 0.46 * cos(arg2 * denom)) *
268 * Decode the 10 vector quantized line spectral pair frequencies from the LSP
269 * transmission codes of any bitrate and check for badly received packets.
271 * @param e the context
273 * @return 0 on success, -1 if the packet is badly received
275 * TIA/IS-127 5.2.1, 5.7.1
277 static int decode_lspf(EVRCContext *e)
279 const float **codebooks = evrc_lspq_codebooks[e->bitrate];
282 for (i = 0; i < evrc_lspq_nb_codebooks[e->bitrate]; i++) {
283 int row_size = evrc_lspq_codebooks_row_sizes[e->bitrate][i];
284 const float *codebook = codebooks[i];
286 for (j = 0; j < row_size; j++)
287 e->lspf[k++] = codebook[e->frame.lsp[i] * row_size + j];
290 // check for monotonic LSPs
291 for (i = 1; i < FILTER_ORDER; i++)
292 if (e->lspf[i] <= e->lspf[i - 1])
295 // check for minimum separation of LSPs at the splits
296 for (i = 0, k = 0; i < evrc_lspq_nb_codebooks[e->bitrate] - 1; i++) {
297 k += evrc_lspq_codebooks_row_sizes[e->bitrate][i];
298 if (e->lspf[k] - e->lspf[k - 1] <= MIN_LSP_SEP)
306 * Interpolation of LSP parameters.
308 * TIA/IS-127 5.2.3.1, 5.7.3.2
310 static void interpolate_lsp(float *ilsp, const float *lsp,
311 const float *prev, int index)
313 static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
314 ff_weighted_vector_sumf(ilsp, prev, lsp,
315 1.0 - lsp_interpolation_factors[index],
316 lsp_interpolation_factors[index], FILTER_ORDER);
320 * Reconstruction of the delay contour.
322 * TIA/IS-127 5.2.2.3.2
324 static void interpolate_delay(float *dst, float current, float prev, int index)
326 static const float d_interpolation_factors[] = { 0, 0.3313, 0.6625, 1, 1 };
327 dst[0] = (1.0 - d_interpolation_factors[index ]) * prev
328 + d_interpolation_factors[index ] * current;
329 dst[1] = (1.0 - d_interpolation_factors[index + 1]) * prev
330 + d_interpolation_factors[index + 1] * current;
331 dst[2] = (1.0 - d_interpolation_factors[index + 2]) * prev
332 + d_interpolation_factors[index + 2] * current;
336 * Convert the quantized, interpolated line spectral frequencies,
337 * to prediction coefficients.
339 * TIA/IS-127 5.2.3.2, 4.7.2.2
341 static void decode_predictor_coeffs(const float *ilspf, float *ilpc)
343 double lsp[FILTER_ORDER];
344 float a[FILTER_ORDER / 2 + 1], b[FILTER_ORDER / 2 + 1];
345 float a1[FILTER_ORDER / 2] = { 0 };
346 float a2[FILTER_ORDER / 2] = { 0 };
347 float b1[FILTER_ORDER / 2] = { 0 };
348 float b2[FILTER_ORDER / 2] = { 0 };
351 ff_acelp_lsf2lspd(lsp, ilspf, FILTER_ORDER);
353 for (k = 0; k <= FILTER_ORDER; k++) {
354 a[0] = k < 2 ? 0.25 : 0;
355 b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
357 for (i = 0; i < FILTER_ORDER / 2; i++) {
358 a[i + 1] = a[i] - 2 * lsp[i * 2 ] * a1[i] + a2[i];
359 b[i + 1] = b[i] - 2 * lsp[i * 2 + 1] * b1[i] + b2[i];
367 ilpc[k - 1] = 2.0 * (a[FILTER_ORDER / 2] + b[FILTER_ORDER / 2]);
371 static void bl_intrp(EVRCContext *e, float *ex, float delay)
374 int offset, i, coef_idx;
377 offset = lrintf(fabs(delay));
379 t = (offset - delay + 0.5) * 8.0 + 0.5;
387 coef_idx = t * (2 * 8 + 1);
390 for (i = 0; i < 2 * 8 + 1; i++)
391 ex[0] += e->interpolation_coeffs[coef_idx + i] * f[i];
395 * Adaptive codebook excitation.
397 * TIA/IS-127 5.2.2.3.3, 4.12.5.2
399 static void acb_excitation(EVRCContext *e, float *excitation, float gain,
400 const float delay[3], int length)
402 float denom, locdelay, dpr, invl;
405 invl = 1.0 / ((float) length);
408 /* first at-most extra samples */
409 denom = (delay[1] - delay[0]) * invl;
410 for (i = 0; i < dpr; i++) {
411 locdelay = delay[0] + i * denom;
412 bl_intrp(e, excitation + i, locdelay);
415 denom = (delay[2] - delay[1]) * invl;
417 for (i = dpr; i < dpr + 10; i++) {
418 locdelay = delay[1] + (i - dpr) * denom;
419 bl_intrp(e, excitation + i, locdelay);
422 for (i = 0; i < length; i++)
423 excitation[i] *= gain;
426 static void decode_8_pulses_35bits(const uint16_t *fixed_index, float *cod)
428 int i, pos1, pos2, offset;
430 offset = (fixed_index[3] >> 9) & 3;
432 for (i = 0; i < 3; i++) {
433 pos1 = ((fixed_index[i] & 0x7f) / 11) * 5 + ((i + offset) % 5);
434 pos2 = ((fixed_index[i] & 0x7f) % 11) * 5 + ((i + offset) % 5);
436 cod[pos1] = (fixed_index[i] & 0x80) ? -1.0 : 1.0;
439 cod[pos2] = -cod[pos1];
441 cod[pos2] += cod[pos1];
444 pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 + offset) % 5);
445 pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 + offset) % 5);
447 cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
448 cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
451 static void decode_3_pulses_10bits(uint16_t fixed_index, float *cod)
456 sign = (fixed_index & 0x200) ? -1.0 : 1.0;
458 pos = ((fixed_index & 0x7) * 7) + 4;
460 pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
462 pos = (((fixed_index >> 6) & 0x7) * 7);
467 * Reconstruction of ACELP fixed codebook excitation for full and half rate.
471 static void fcb_excitation(EVRCContext *e, const uint16_t *codebook,
472 float *excitation, float pitch_gain,
473 int pitch_lag, int subframe_size)
477 if (e->bitrate == RATE_FULL)
478 decode_8_pulses_35bits(codebook, excitation);
480 decode_3_pulses_10bits(*codebook, excitation);
482 pitch_gain = av_clipf(pitch_gain, 0.2, 0.9);
484 for (i = pitch_lag; i < subframe_size; i++)
485 excitation[i] += pitch_gain * excitation[i - pitch_lag];
489 * Synthesis of the decoder output signal.
491 * param[in] in input signal
492 * param[in] filter_coeffs LPC coefficients
493 * param[in/out] memory synthesis filter memory
494 * param buffer_length amount of data to process
495 * param[out] samples output samples
497 * TIA/IS-127 5.2.3.15, 5.7.3.4
499 static void synthesis_filter(const float *in, const float *filter_coeffs,
500 float *memory, int buffer_length, float *samples)
504 for (i = 0; i < buffer_length; i++) {
506 for (j = FILTER_ORDER - 1; j > 0; j--) {
507 samples[i] -= filter_coeffs[j] * memory[j];
508 memory[j] = memory[j - 1];
510 samples[i] -= filter_coeffs[0] * memory[0];
511 memory[0] = samples[i];
515 static void bandwidth_expansion(float *coeff, const float *inbuf, float gamma)
520 for (i = 0; i < FILTER_ORDER; i++) {
521 coeff[i] = inbuf[i] * fac;
526 static void residual_filter(float *output, const float *input,
527 const float *coef, float *memory, int length)
532 for (i = 0; i < length; i++) {
535 for (j = FILTER_ORDER - 1; j > 0; j--) {
536 sum += coef[j] * memory[j];
537 memory[j] = memory[j - 1];
539 sum += coef[0] * memory[0];
540 memory[0] = input[i];
546 * TIA/IS-127 Table 5.9.1-1.
548 static const struct PfCoeff {
553 } postfilter_coeffs[5] = {
554 { 0.0 , 0.0 , 0.0 , 0.0 },
555 { 0.0 , 0.0 , 0.57, 0.57 },
556 { 0.0 , 0.0 , 0.0 , 0.0 },
557 { 0.35, 0.50, 0.50, 0.75 },
558 { 0.20, 0.50, 0.57, 0.75 },
562 * Adaptive postfilter.
566 static void postfilter(EVRCContext *e, float *in, const float *coeff,
567 float *out, int idx, const struct PfCoeff *pfc,
570 float wcoef1[FILTER_ORDER], wcoef2[FILTER_ORDER],
571 scratch[SUBFRAME_SIZE], temp[SUBFRAME_SIZE],
573 float sum1 = 0.0, sum2 = 0.0, gamma, gain;
574 float tilt = pfc->tilt;
577 bandwidth_expansion(wcoef1, coeff, pfc->p1);
578 bandwidth_expansion(wcoef2, coeff, pfc->p2);
580 /* Tilt compensation filter, TIA/IS-127 5.9.1 */
581 for (i = 0; i < length - 1; i++)
582 sum2 += in[i] * in[i + 1];
586 for (i = 0; i < length; i++) {
587 scratch[i] = in[i] - tilt * e->last;
591 /* Short term residual filter, TIA/IS-127 5.9.2 */
592 residual_filter(&e->postfilter_residual[ACB_SIZE], scratch, wcoef1, e->postfilter_fir, length);
594 /* Long term postfilter */
596 for (i = FFMIN(MIN_DELAY, idx - 3); i <= FFMAX(MAX_DELAY, idx + 3); i++) {
597 for (n = ACB_SIZE, sum2 = 0; n < ACB_SIZE + length; n++)
598 sum2 += e->postfilter_residual[n] * e->postfilter_residual[n - i];
605 for (i = ACB_SIZE, sum1 = 0; i < ACB_SIZE + length; i++)
606 sum1 += e->postfilter_residual[i - best] * e->postfilter_residual[i - best];
607 for (i = ACB_SIZE, sum2 = 0; i < ACB_SIZE + length; i++)
608 sum2 += e->postfilter_residual[i] * e->postfilter_residual[i - best];
610 if (sum2 * sum1 == 0 || e->bitrate == RATE_QUANT) {
611 memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
615 memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
617 gamma = FFMIN(gamma, 1.0);
619 for (i = 0; i < length; i++) {
620 temp[i] = e->postfilter_residual[ACB_SIZE + i] + gamma *
621 pfc->ltgain * e->postfilter_residual[ACB_SIZE + i - best];
626 memcpy(scratch, temp, length * sizeof(float));
627 memcpy(mem, e->postfilter_iir, FILTER_ORDER * sizeof(float));
628 synthesis_filter(scratch, wcoef2, mem, length, scratch);
630 /* Gain computation, TIA/IS-127 5.9.4-2 */
631 for (i = 0, sum1 = 0, sum2 = 0; i < length; i++) {
632 sum1 += in[i] * in[i];
633 sum2 += scratch[i] * scratch[i];
635 gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
637 for (i = 0; i < length; i++)
640 /* Short term postfilter */
641 synthesis_filter(temp, wcoef2, e->postfilter_iir, length, out);
643 memcpy(e->postfilter_residual,
644 e->postfilter_residual + length, ACB_SIZE * sizeof(float));
647 static void frame_erasure(EVRCContext *e, float *samples)
649 float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES],
650 tmp[SUBFRAME_SIZE + 6], f;
653 for (i = 0; i < FILTER_ORDER; i++) {
654 if (e->bitrate != RATE_QUANT)
655 e->lspf[i] = e->prev_lspf[i] * 0.875 + 0.125 * (i + 1) * 0.048;
657 e->lspf[i] = e->prev_lspf[i];
660 if (e->prev_error_flag)
661 e->avg_acb_gain *= 0.75;
662 if (e->bitrate == RATE_FULL)
663 memcpy(e->pitch_back, e->pitch, ACB_SIZE * sizeof(float));
664 if (e->last_valid_bitrate == RATE_QUANT)
665 e->bitrate = RATE_QUANT;
667 e->bitrate = RATE_FULL;
669 if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
670 e->pitch_delay = e->prev_pitch_delay;
674 idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
676 for (i = 0; i < NB_SUBFRAMES; i++)
677 sum += evrc_energy_quant[e->prev_energy_gain][i];
678 sum /= (float) NB_SUBFRAMES;
680 for (i = 0; i < NB_SUBFRAMES; i++)
681 e->energy_vector[i] = sum;
684 if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
685 e->prev_pitch_delay = e->pitch_delay;
687 for (i = 0; i < NB_SUBFRAMES; i++) {
688 int subframe_size = subframe_sizes[i];
691 interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
693 if (e->bitrate != RATE_QUANT) {
694 if (e->avg_acb_gain < 0.3) {
695 idelay[0] = estimation_delay[i];
696 idelay[1] = estimation_delay[i + 1];
697 idelay[2] = estimation_delay[i + 2];
699 interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
703 pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
704 decode_predictor_coeffs(ilspf, ilpc);
706 if (e->bitrate != RATE_QUANT) {
707 acb_excitation(e, e->pitch + ACB_SIZE,
708 e->avg_acb_gain, idelay, subframe_size);
709 for (j = 0; j < subframe_size; j++)
710 e->pitch[ACB_SIZE + j] *= e->fade_scale;
711 e->fade_scale = FFMAX(e->fade_scale - 0.05, 0.0);
713 for (j = 0; j < subframe_size; j++)
714 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
717 memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
719 if (e->bitrate != RATE_QUANT && e->avg_acb_gain < 0.4) {
720 f = 0.1 * e->avg_fcb_gain;
721 for (j = 0; j < subframe_size; j++)
722 e->pitch[ACB_SIZE + j] += f;
723 } else if (e->bitrate == RATE_QUANT) {
724 for (j = 0; j < subframe_size; j++)
725 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
728 synthesis_filter(e->pitch + ACB_SIZE, ilpc,
729 e->synthesis, subframe_size, tmp);
730 postfilter(e, tmp, ilpc, samples, pitch_lag,
731 &postfilter_coeffs[e->bitrate], subframe_size);
733 samples += subframe_size;
737 static int evrc_decode_frame(AVCodecContext *avctx, void *data,
738 int *got_frame_ptr, AVPacket *avpkt)
740 const uint8_t *buf = avpkt->data;
741 AVFrame *frame = data;
742 EVRCContext *e = avctx->priv_data;
743 int buf_size = avpkt->size;
744 float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES];
746 int i, j, ret, error_flag = 0;
748 frame->nb_samples = 160;
749 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
751 samples = (float *)frame->data[0];
753 if ((e->bitrate = determine_bitrate(avctx, &buf_size, &buf)) == RATE_ERRS) {
754 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
757 if (e->bitrate <= SILENCE || e->bitrate == RATE_QUARTER)
759 if (e->bitrate == RATE_QUANT && e->last_valid_bitrate == RATE_FULL
760 && !e->prev_error_flag)
763 init_get_bits(&e->gb, buf, 8 * buf_size);
764 memset(&e->frame, 0, sizeof(EVRCAFrame));
768 if (e->bitrate != RATE_QUANT) {
769 uint8_t *p = (uint8_t *) &e->frame;
770 for (i = 0; i < sizeof(EVRCAFrame); i++) {
774 if (i == sizeof(EVRCAFrame))
776 } else if (e->frame.lsp[0] == 0xf &&
777 e->frame.lsp[1] == 0xf &&
778 e->frame.energy_gain == 0xff) {
782 if (decode_lspf(e) < 0)
785 if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
786 /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
787 if (e->frame.pitch_delay > MAX_DELAY - MIN_DELAY)
790 e->pitch_delay = e->frame.pitch_delay + MIN_DELAY;
792 /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
793 if (e->frame.delay_diff) {
794 int p = e->pitch_delay - e->frame.delay_diff + 16;
795 if (p < MIN_DELAY || p > MAX_DELAY)
799 /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
800 if (e->frame.delay_diff &&
801 e->bitrate == RATE_FULL && e->prev_error_flag) {
804 memcpy(e->pitch, e->pitch_back, ACB_SIZE * sizeof(float));
806 delay = e->prev_pitch_delay;
807 e->prev_pitch_delay = delay - e->frame.delay_diff + 16.0;
809 if (fabs(e->pitch_delay - delay) > 15)
810 delay = e->pitch_delay;
812 for (i = 0; i < NB_SUBFRAMES; i++) {
813 int subframe_size = subframe_sizes[i];
815 interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
816 acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
817 memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
821 /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
822 if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
823 e->prev_pitch_delay = e->pitch_delay;
825 e->avg_acb_gain = e->avg_fcb_gain = 0.0;
827 idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
829 /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
830 for (i = 0; i < NB_SUBFRAMES; i++)
831 e->energy_vector[i] = pow(10, evrc_energy_quant[e->frame.energy_gain][i]);
832 e->prev_energy_gain = e->frame.energy_gain;
835 for (i = 0; i < NB_SUBFRAMES; i++) {
836 float tmp[SUBFRAME_SIZE + 6] = { 0 };
837 int subframe_size = subframe_sizes[i];
840 interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
842 if (e->bitrate != RATE_QUANT)
843 interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
845 pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
846 decode_predictor_coeffs(ilspf, ilpc);
848 /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
849 if (e->frame.lpc_flag && e->prev_error_flag)
850 bandwidth_expansion(ilpc, ilpc, 0.75);
852 if (e->bitrate != RATE_QUANT) {
855 f = exp((e->bitrate == RATE_HALF ? 0.5 : 0.25)
856 * (e->frame.fcb_gain[i] + 1));
857 acb_sum = pitch_gain_vq[e->frame.acb_gain[i]];
858 e->avg_acb_gain += acb_sum / NB_SUBFRAMES;
859 e->avg_fcb_gain += f / NB_SUBFRAMES;
861 acb_excitation(e, e->pitch + ACB_SIZE,
862 acb_sum, idelay, subframe_size);
863 fcb_excitation(e, e->frame.fcb_shape[i], tmp,
864 acb_sum, pitch_lag, subframe_size);
866 /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
867 for (j = 0; j < subframe_size; j++)
868 e->pitch[ACB_SIZE + j] += f * tmp[j];
869 e->fade_scale = FFMIN(e->fade_scale + 0.2, 1.0);
871 for (j = 0; j < subframe_size; j++)
872 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
875 memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
877 synthesis_filter(e->pitch + ACB_SIZE, ilpc,
878 e->synthesis, subframe_size, tmp);
879 postfilter(e, tmp, ilpc, samples, pitch_lag,
880 &postfilter_coeffs[e->bitrate], subframe_size);
882 samples += subframe_size;
888 av_log(avctx, AV_LOG_WARNING, "frame erasure\n");
889 frame_erasure(e, samples);
892 memcpy(e->prev_lspf, e->lspf, sizeof(e->prev_lspf));
893 e->prev_error_flag = error_flag;
894 e->last_valid_bitrate = e->bitrate;
896 if (e->bitrate != RATE_QUANT)
897 e->prev_pitch_delay = e->pitch_delay;
899 samples = (float *)frame->data[0];
900 for (i = 0; i < 160; i++)
908 AVCodec ff_evrc_decoder = {
910 .type = AVMEDIA_TYPE_AUDIO,
911 .id = AV_CODEC_ID_EVRC,
912 .init = evrc_decode_init,
913 .decode = evrc_decode_frame,
914 .capabilities = CODEC_CAP_DR1,
915 .priv_data_size = sizeof(EVRCContext),
916 .long_name = NULL_IF_CONFIG_SMALL("EVRC (Enhanced Variable Rate Codec)"),