FFmpeg
Loading...
Searching...
No Matches
evrcdec.c
Go to the documentation of this file.
1/*
2 * Enhanced Variable Rate Codec, Service Option 3 decoder
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * Enhanced Variable Rate Codec, Service Option 3 decoder
25 * @author Paul B Mahol
26 */
27
30#include "libavutil/opt.h"
31#include "avcodec.h"
32#include "codec_internal.h"
33#include "decode.h"
34#include "get_bits.h"
35#include "evrcdata.h"
36#include "acelp_vectors.h"
37#include "lsp.h"
38
39#define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
40#define MIN_DELAY 20
41#define MAX_DELAY 120
42#define NB_SUBFRAMES 3
43#define SUBFRAME_SIZE 54
44#define FILTER_ORDER 10
45#define ACB_SIZE 128
46
55
56/**
57 * EVRC-A unpacked data frame
58 */
59typedef struct EVRCAFrame {
60 uint8_t lpc_flag; ///< spectral change indicator
61 uint16_t lsp[4]; ///< index into LSP codebook
62 uint8_t pitch_delay; ///< pitch delay for entire frame
63 uint8_t delay_diff; ///< delay difference for entire frame
64 uint8_t acb_gain[3]; ///< adaptive codebook gain
65 uint16_t fcb_shape[3][4]; ///< fixed codebook shape
66 uint8_t fcb_gain[3]; ///< fixed codebook gain index
67 uint8_t energy_gain; ///< frame energy gain index
68 uint8_t tty; ///< tty baud rate bit
70
102
103/**
104 * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
105 *
106 * @param e the context
107 *
108 * TIA/IS-127 Table 4.21-1
109 */
111{
112 EVRCAFrame *frame = &e->frame;
113 GetBitContext *gb = &e->gb;
114
115 switch (e->bitrate) {
116 case RATE_FULL:
117 frame->lpc_flag = get_bits1(gb);
118 frame->lsp[0] = get_bits(gb, 6);
119 frame->lsp[1] = get_bits(gb, 6);
120 frame->lsp[2] = get_bits(gb, 9);
121 frame->lsp[3] = get_bits(gb, 7);
122 frame->pitch_delay = get_bits(gb, 7);
123 frame->delay_diff = get_bits(gb, 5);
124 frame->acb_gain[0] = get_bits(gb, 3);
125 frame->fcb_shape[0][0] = get_bits(gb, 8);
126 frame->fcb_shape[0][1] = get_bits(gb, 8);
127 frame->fcb_shape[0][2] = get_bits(gb, 8);
128 frame->fcb_shape[0][3] = get_bits(gb, 11);
129 frame->fcb_gain[0] = get_bits(gb, 5);
130 frame->acb_gain[1] = get_bits(gb, 3);
131 frame->fcb_shape[1][0] = get_bits(gb, 8);
132 frame->fcb_shape[1][1] = get_bits(gb, 8);
133 frame->fcb_shape[1][2] = get_bits(gb, 8);
134 frame->fcb_shape[1][3] = get_bits(gb, 11);
135 frame->fcb_gain [1] = get_bits(gb, 5);
136 frame->acb_gain [2] = get_bits(gb, 3);
137 frame->fcb_shape[2][0] = get_bits(gb, 8);
138 frame->fcb_shape[2][1] = get_bits(gb, 8);
139 frame->fcb_shape[2][2] = get_bits(gb, 8);
140 frame->fcb_shape[2][3] = get_bits(gb, 11);
141 frame->fcb_gain [2] = get_bits(gb, 5);
142 frame->tty = get_bits1(gb);
143 break;
144 case RATE_HALF:
145 frame->lsp [0] = get_bits(gb, 7);
146 frame->lsp [1] = get_bits(gb, 7);
147 frame->lsp [2] = get_bits(gb, 8);
148 frame->pitch_delay = get_bits(gb, 7);
149 frame->acb_gain [0] = get_bits(gb, 3);
150 frame->fcb_shape[0][0] = get_bits(gb, 10);
151 frame->fcb_gain [0] = get_bits(gb, 4);
152 frame->acb_gain [1] = get_bits(gb, 3);
153 frame->fcb_shape[1][0] = get_bits(gb, 10);
154 frame->fcb_gain [1] = get_bits(gb, 4);
155 frame->acb_gain [2] = get_bits(gb, 3);
156 frame->fcb_shape[2][0] = get_bits(gb, 10);
157 frame->fcb_gain [2] = get_bits(gb, 4);
158 break;
159 case RATE_QUANT:
160 frame->lsp [0] = get_bits(gb, 4);
161 frame->lsp [1] = get_bits(gb, 4);
162 frame->energy_gain = get_bits(gb, 8);
163 break;
164 }
165}
166
167static evrc_packet_rate buf_size2bitrate(const int buf_size)
168{
169 switch (buf_size) {
170 case 23: return RATE_FULL;
171 case 11: return RATE_HALF;
172 case 6: return RATE_QUARTER;
173 case 3: return RATE_QUANT;
174 case 1: return SILENCE;
175 }
176
177 return RATE_ERRS;
178}
179
180/**
181 * Determine the bitrate from the frame size and/or the first byte of the frame.
182 *
183 * @param avctx the AV codec context
184 * @param buf_size length of the buffer
185 * @param buf the buffer
186 *
187 * @return the bitrate on success,
188 * RATE_ERRS if the bitrate cannot be satisfactorily determined
189 */
191 int *buf_size,
192 const uint8_t **buf)
193{
195
196 if ((bitrate = buf_size2bitrate(*buf_size)) >= 0) {
197 if (bitrate > **buf) {
198 EVRCContext *e = avctx->priv_data;
200 av_log(avctx, AV_LOG_WARNING,
201 "Claimed bitrate and buffer size mismatch.\n");
203 }
204 bitrate = **buf;
205 } else if (bitrate < **buf) {
206 av_log(avctx, AV_LOG_ERROR,
207 "Buffer is too small for the claimed bitrate.\n");
208 return RATE_ERRS;
209 }
210 (*buf)++;
211 *buf_size -= 1;
212 } else if ((bitrate = buf_size2bitrate(*buf_size + 1)) >= 0) {
213 av_log(avctx, AV_LOG_DEBUG,
214 "Bitrate byte is missing, guessing the bitrate from packet size.\n");
215 } else
216 return RATE_ERRS;
217
218 return bitrate;
219}
220
222 const char *message)
223{
224 av_log(avctx, AV_LOG_WARNING, "Frame #%"PRId64", %s\n",
225 avctx->frame_num, message);
226}
227
228/**
229 * Initialize the speech codec according to the specification.
230 *
231 * TIA/IS-127 5.2
232 */
234{
235 EVRCContext *e = avctx->priv_data;
236 int i, n, idx = 0;
237 float denom = 2.0 / (2.0 * 8.0 + 1.0);
238
242 if (!avctx->sample_rate)
243 avctx->sample_rate = 8000;
244
245 for (i = 0; i < FILTER_ORDER; i++) {
246 e->prev_lspf[i] = (i + 1) * 0.048;
247 e->synthesis[i] = 0.0;
248 }
249
250 for (i = 0; i < ACB_SIZE; i++)
251 e->pitch[i] = e->pitch_back[i] = 0.0;
252
254 e->prev_pitch_delay = 40.0;
255 e->fade_scale = 1.0;
256 e->prev_error_flag = 0;
257 e->avg_acb_gain = e->avg_fcb_gain = 0.0;
258
259 for (i = 0; i < 8; i++) {
260 float tt = ((float)i - 8.0 / 2.0) / 8.0;
261
262 for (n = -8; n <= 8; n++, idx++) {
263 float arg1 = M_PI * 0.9 * (tt - n);
264 float arg2 = M_PI * (tt - n);
265
266 e->interpolation_coeffs[idx] = 0.9;
267 if (arg1)
268 e->interpolation_coeffs[idx] *= (0.54 + 0.46 * cos(arg2 * denom)) *
269 sin(arg1) / arg1;
270 }
271 }
272
273 return 0;
274}
275
276/**
277 * Decode the 10 vector quantized line spectral pair frequencies from the LSP
278 * transmission codes of any bitrate and check for badly received packets.
279 *
280 * @param e the context
281 *
282 * @return 0 on success, -1 if the packet is badly received
283 *
284 * TIA/IS-127 5.2.1, 5.7.1
285 */
287{
288 const float * const *codebooks = evrc_lspq_codebooks[e->bitrate];
289 int i, j, k = 0;
290
291 for (i = 0; i < evrc_lspq_nb_codebooks[e->bitrate]; i++) {
292 int row_size = evrc_lspq_codebooks_row_sizes[e->bitrate][i];
293 const float *codebook = codebooks[i];
294
295 for (j = 0; j < row_size; j++)
296 e->lspf[k++] = codebook[e->frame.lsp[i] * row_size + j];
297 }
298
299 // check for monotonic LSPs
300 for (i = 1; i < FILTER_ORDER; i++)
301 if (e->lspf[i] <= e->lspf[i - 1])
302 return -1;
303
304 // check for minimum separation of LSPs at the splits
305 for (i = 0, k = 0; i < evrc_lspq_nb_codebooks[e->bitrate] - 1; i++) {
307 if (e->lspf[k] - e->lspf[k - 1] <= MIN_LSP_SEP)
308 return -1;
309 }
310
311 return 0;
312}
313
314/*
315 * Interpolation of LSP parameters.
316 *
317 * TIA/IS-127 5.2.3.1, 5.7.3.2
318 */
319static void interpolate_lsp(float *ilsp, const float *lsp,
320 const float *prev, int index)
321{
322 static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
323 ff_weighted_vector_sumf(ilsp, prev, lsp,
324 1.0 - lsp_interpolation_factors[index],
325 lsp_interpolation_factors[index], FILTER_ORDER);
326}
327
328/*
329 * Reconstruction of the delay contour.
330 *
331 * TIA/IS-127 5.2.2.3.2
332 */
333static void interpolate_delay(float *dst, float current, float prev, int index)
334{
335 static const float d_interpolation_factors[] = { 0, 0.3313, 0.6625, 1, 1 };
336 dst[0] = (1.0 - d_interpolation_factors[index ]) * prev
337 + d_interpolation_factors[index ] * current;
338 dst[1] = (1.0 - d_interpolation_factors[index + 1]) * prev
339 + d_interpolation_factors[index + 1] * current;
340 dst[2] = (1.0 - d_interpolation_factors[index + 2]) * prev
341 + d_interpolation_factors[index + 2] * current;
342}
343
344/*
345 * Convert the quantized, interpolated line spectral frequencies,
346 * to prediction coefficients.
347 *
348 * TIA/IS-127 5.2.3.2, 4.7.2.2
349 */
350static void decode_predictor_coeffs(const float *ilspf, float *ilpc)
351{
352 double lsp[FILTER_ORDER];
353 float a[FILTER_ORDER / 2 + 1], b[FILTER_ORDER / 2 + 1];
354 float a1[FILTER_ORDER / 2] = { 0 };
355 float a2[FILTER_ORDER / 2] = { 0 };
356 float b1[FILTER_ORDER / 2] = { 0 };
357 float b2[FILTER_ORDER / 2] = { 0 };
358 int i, k;
359
360 ff_acelp_lsf2lspd(lsp, ilspf, FILTER_ORDER);
361
362 for (k = 0; k <= FILTER_ORDER; k++) {
363 a[0] = k < 2 ? 0.25 : 0;
364 b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
365
366 for (i = 0; i < FILTER_ORDER / 2; i++) {
367 a[i + 1] = a[i] - 2 * lsp[i * 2 ] * a1[i] + a2[i];
368 b[i + 1] = b[i] - 2 * lsp[i * 2 + 1] * b1[i] + b2[i];
369 a2[i] = a1[i];
370 a1[i] = a[i];
371 b2[i] = b1[i];
372 b1[i] = b[i];
373 }
374
375 if (k)
376 ilpc[k - 1] = 2.0 * (a[FILTER_ORDER / 2] + b[FILTER_ORDER / 2]);
377 }
378}
379
380static void bl_intrp(EVRCContext *e, float *ex, float delay)
381{
382 float *f;
383 int offset, i, coef_idx;
384 int16_t t;
385
386 offset = lrintf(delay);
387
388 t = (offset - delay + 0.5) * 8.0 + 0.5;
389 if (t == 8) {
390 t = 0;
391 offset--;
392 }
393
394 f = ex - offset - 8;
395
396 coef_idx = t * (2 * 8 + 1);
397
398 ex[0] = 0.0;
399 for (i = 0; i < 2 * 8 + 1; i++)
400 ex[0] += e->interpolation_coeffs[coef_idx + i] * f[i];
401}
402
403/*
404 * Adaptive codebook excitation.
405 *
406 * TIA/IS-127 5.2.2.3.3, 4.12.5.2
407 */
408static void acb_excitation(EVRCContext *e, float *excitation, float gain,
409 const float delay[3], int length)
410{
411 float denom, locdelay, dpr, invl;
412 int i;
413
414 invl = 1.0 / ((float) length);
415 dpr = length;
416
417 /* first at-most extra samples */
418 denom = (delay[1] - delay[0]) * invl;
419 for (i = 0; i < dpr; i++) {
420 locdelay = delay[0] + i * denom;
421 bl_intrp(e, excitation + i, locdelay);
422 }
423
424 denom = (delay[2] - delay[1]) * invl;
425 /* interpolation */
426 for (i = dpr; i < dpr + 10; i++) {
427 locdelay = delay[1] + (i - dpr) * denom;
428 bl_intrp(e, excitation + i, locdelay);
429 }
430
431 for (i = 0; i < length; i++)
432 excitation[i] *= gain;
433}
434
435static void decode_8_pulses_35bits(const uint16_t *fixed_index, float *cod)
436{
437 int i, pos1, pos2, offset;
438
439 offset = (fixed_index[3] >> 9) & 3;
440
441 for (i = 0; i < 3; i++) {
442 pos1 = ((fixed_index[i] & 0x7f) / 11) * 5 + ((i + offset) % 5);
443 pos2 = ((fixed_index[i] & 0x7f) % 11) * 5 + ((i + offset) % 5);
444
445 cod[pos1] = (fixed_index[i] & 0x80) ? -1.0 : 1.0;
446
447 if (pos2 < pos1)
448 cod[pos2] = -cod[pos1];
449 else
450 cod[pos2] += cod[pos1];
451 }
452
453 pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 + offset) % 5);
454 pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 + offset) % 5);
455
456 cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
457 cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
458}
459
460static void decode_3_pulses_10bits(uint16_t fixed_index, float *cod)
461{
462 float sign;
463 int pos;
464
465 sign = (fixed_index & 0x200) ? -1.0 : 1.0;
466
467 pos = ((fixed_index & 0x7) * 7) + 4;
468 cod[pos] += sign;
469 pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
470 cod[pos] -= sign;
471 pos = (((fixed_index >> 6) & 0x7) * 7);
472 cod[pos] += sign;
473}
474
475/*
476 * Reconstruction of ACELP fixed codebook excitation for full and half rate.
477 *
478 * TIA/IS-127 5.2.3.7
479 */
480static void fcb_excitation(EVRCContext *e, const uint16_t *codebook,
481 float *excitation, float pitch_gain,
482 int pitch_lag, int subframe_size)
483{
484 int i;
485
486 if (e->bitrate == RATE_FULL)
487 decode_8_pulses_35bits(codebook, excitation);
488 else
489 decode_3_pulses_10bits(*codebook, excitation);
490
491 pitch_gain = av_clipf(pitch_gain, 0.2, 0.9);
492
493 for (i = pitch_lag; i < subframe_size; i++)
494 excitation[i] += pitch_gain * excitation[i - pitch_lag];
495}
496
497/**
498 * Synthesis of the decoder output signal.
499 *
500 * @param[in] in input signal
501 * @param[in] filter_coeffs LPC coefficients
502 * @param[in/out] memory synthesis filter memory
503 * @param buffer_length amount of data to process
504 * @param[out] samples output samples
505 *
506 * TIA/IS-127 5.2.3.15, 5.7.3.4
507 */
508static void synthesis_filter(const float *in, const float *filter_coeffs,
509 float *memory, int buffer_length, float *samples)
510{
511 int i, j;
512
513 for (i = 0; i < buffer_length; i++) {
514 samples[i] = in[i];
515 for (j = FILTER_ORDER - 1; j > 0; j--) {
516 samples[i] -= filter_coeffs[j] * memory[j];
517 memory[j] = memory[j - 1];
518 }
519 samples[i] -= filter_coeffs[0] * memory[0];
520 memory[0] = samples[i];
521 }
522}
523
524static void bandwidth_expansion(float *coeff, const float *inbuf, float gamma)
525{
526 double fac = gamma;
527 int i;
528
529 for (i = 0; i < FILTER_ORDER; i++) {
530 coeff[i] = inbuf[i] * fac;
531 fac *= gamma;
532 }
533}
534
535static void residual_filter(float *output, const float *input,
536 const float *coef, float *memory, int length)
537{
538 float sum;
539 int i, j;
540
541 for (i = 0; i < length; i++) {
542 sum = input[i];
543
544 for (j = FILTER_ORDER - 1; j > 0; j--) {
545 sum += coef[j] * memory[j];
546 memory[j] = memory[j - 1];
547 }
548 sum += coef[0] * memory[0];
549 memory[0] = input[i];
550 output[i] = sum;
551 }
552}
553
554/*
555 * TIA/IS-127 Table 5.9.1-1.
556 */
557static const struct PfCoeff {
558 float tilt;
559 float ltgain;
560 float p1;
561 float p2;
562} postfilter_coeffs[5] = {
563 { 0.0 , 0.0 , 0.0 , 0.0 },
564 { 0.0 , 0.0 , 0.57, 0.57 },
565 { 0.0 , 0.0 , 0.0 , 0.0 },
566 { 0.35, 0.50, 0.50, 0.75 },
567 { 0.20, 0.50, 0.57, 0.75 },
569
570/*
571 * Adaptive postfilter.
572 *
573 * TIA/IS-127 5.9
574 */
575static void postfilter(EVRCContext *e, float *in, const float *coeff,
576 float *out, int idx, const struct PfCoeff *pfc,
577 int length)
578{
579 float wcoef1[FILTER_ORDER], wcoef2[FILTER_ORDER],
581 mem[SUBFRAME_SIZE];
582 float sum1 = 0.0, sum2 = 0.0, gamma, gain;
583 float tilt = pfc->tilt;
584 int i, n, best;
585
586 bandwidth_expansion(wcoef1, coeff, pfc->p1);
587 bandwidth_expansion(wcoef2, coeff, pfc->p2);
588
589 /* Tilt compensation filter, TIA/IS-127 5.9.1 */
590 for (i = 0; i < length - 1; i++)
591 sum2 += in[i] * in[i + 1];
592 if (sum2 < 0.0)
593 tilt = 0.0;
594
595 for (i = 0; i < length; i++) {
596 scratch[i] = in[i] - tilt * e->last;
597 e->last = in[i];
598 }
599
600 /* Short term residual filter, TIA/IS-127 5.9.2 */
601 residual_filter(&e->postfilter_residual[ACB_SIZE], scratch, wcoef1, e->postfilter_fir, length);
602
603 /* Long term postfilter */
604 best = idx;
605 for (i = FFMIN(MIN_DELAY, idx - 3); i <= FFMAX(MAX_DELAY, idx + 3); i++) {
606 for (n = ACB_SIZE, sum2 = 0; n < ACB_SIZE + length; n++)
607 sum2 += e->postfilter_residual[n] * e->postfilter_residual[n - i];
608 if (sum2 > sum1) {
609 sum1 = sum2;
610 best = i;
611 }
612 }
613
614 for (i = ACB_SIZE, sum1 = 0; i < ACB_SIZE + length; i++)
615 sum1 += e->postfilter_residual[i - best] * e->postfilter_residual[i - best];
616 for (i = ACB_SIZE, sum2 = 0; i < ACB_SIZE + length; i++)
617 sum2 += e->postfilter_residual[i] * e->postfilter_residual[i - best];
618
619 if (sum2 * sum1 == 0 || e->bitrate == RATE_QUANT) {
620 memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
621 } else {
622 gamma = sum2 / sum1;
623 if (gamma < 0.5)
624 memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
625 else {
626 gamma = FFMIN(gamma, 1.0);
627
628 for (i = 0; i < length; i++) {
629 temp[i] = e->postfilter_residual[ACB_SIZE + i] + gamma *
630 pfc->ltgain * e->postfilter_residual[ACB_SIZE + i - best];
631 }
632 }
633 }
634
635 memcpy(scratch, temp, length * sizeof(float));
636 memcpy(mem, e->postfilter_iir, FILTER_ORDER * sizeof(float));
637 synthesis_filter(scratch, wcoef2, mem, length, scratch);
638
639 /* Gain computation, TIA/IS-127 5.9.4-2 */
640 for (i = 0, sum1 = 0, sum2 = 0; i < length; i++) {
641 sum1 += in[i] * in[i];
642 sum2 += scratch[i] * scratch[i];
643 }
644 gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
645
646 for (i = 0; i < length; i++)
647 temp[i] *= gain;
648
649 /* Short term postfilter */
650 synthesis_filter(temp, wcoef2, e->postfilter_iir, length, out);
651
652 memmove(e->postfilter_residual,
653 e->postfilter_residual + length, ACB_SIZE * sizeof(float));
654}
655
656static void frame_erasure(EVRCContext *e, float *samples)
657{
658 float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES],
659 tmp[SUBFRAME_SIZE + 6], f;
660 int i, j;
661
662 for (i = 0; i < FILTER_ORDER; i++) {
663 if (e->bitrate != RATE_QUANT)
664 e->lspf[i] = e->prev_lspf[i] * 0.875 + 0.125 * (i + 1) * 0.048;
665 else
666 e->lspf[i] = e->prev_lspf[i];
667 }
668
669 if (e->prev_error_flag)
670 e->avg_acb_gain *= 0.75;
671 if (e->bitrate == RATE_FULL)
672 memcpy(e->pitch_back, e->pitch, ACB_SIZE * sizeof(float));
674 e->bitrate = RATE_QUANT;
675 else
676 e->bitrate = RATE_FULL;
677
678 if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
680 } else {
681 float sum = 0;
682
683 idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
684
685 for (i = 0; i < NB_SUBFRAMES; i++)
687 sum /= (float) NB_SUBFRAMES;
688 sum = pow(10, sum);
689 for (i = 0; i < NB_SUBFRAMES; i++)
690 e->energy_vector[i] = sum;
691 }
692
693 if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
695
696 for (i = 0; i < NB_SUBFRAMES; i++) {
697 int subframe_size = subframe_sizes[i];
698 int pitch_lag;
699
700 interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
701
702 if (e->bitrate != RATE_QUANT) {
703 if (e->avg_acb_gain < 0.3) {
704 idelay[0] = estimation_delay[i];
705 idelay[1] = estimation_delay[i + 1];
706 idelay[2] = estimation_delay[i + 2];
707 } else {
709 }
710 }
711
712 pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
713 decode_predictor_coeffs(ilspf, ilpc);
714
715 if (e->bitrate != RATE_QUANT) {
717 e->avg_acb_gain, idelay, subframe_size);
718 for (j = 0; j < subframe_size; j++)
719 e->pitch[ACB_SIZE + j] *= e->fade_scale;
720 e->fade_scale = FFMAX(e->fade_scale - 0.05, 0.0);
721 } else {
722 for (j = 0; j < subframe_size; j++)
723 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
724 }
725
726 memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
727
728 if (e->bitrate != RATE_QUANT && e->avg_acb_gain < 0.4) {
729 f = 0.1 * e->avg_fcb_gain;
730 for (j = 0; j < subframe_size; j++)
731 e->pitch[ACB_SIZE + j] += f;
732 } else if (e->bitrate == RATE_QUANT) {
733 for (j = 0; j < subframe_size; j++)
734 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
735 }
736
738 e->synthesis, subframe_size, tmp);
739 postfilter(e, tmp, ilpc, samples, pitch_lag,
740 &postfilter_coeffs[e->bitrate], subframe_size);
741
742 samples += subframe_size;
743 }
744}
745
747 int *got_frame_ptr, AVPacket *avpkt)
748{
749 const uint8_t *buf = avpkt->data;
750 EVRCContext *e = avctx->priv_data;
751 int buf_size = avpkt->size;
752 float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES];
753 float *samples;
754 int i, j, ret, error_flag = 0;
755
756 frame->nb_samples = 160;
757 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
758 return ret;
759 samples = (float *)frame->data[0];
760
761 if ((e->bitrate = determine_bitrate(avctx, &buf_size, &buf)) == RATE_ERRS) {
762 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
763 goto erasure;
764 }
765 if (e->bitrate <= SILENCE || e->bitrate == RATE_QUARTER)
766 goto erasure;
768 && !e->prev_error_flag)
769 goto erasure;
770
771 if ((ret = init_get_bits8(&e->gb, buf, buf_size)) < 0)
772 return ret;
773 memset(&e->frame, 0, sizeof(EVRCAFrame));
774
775 unpack_frame(e);
776
777 if (e->bitrate != RATE_QUANT) {
778 uint8_t *p = (uint8_t *) &e->frame;
779 for (i = 0; i < sizeof(EVRCAFrame); i++) {
780 if (p[i])
781 break;
782 }
783 if (i == sizeof(EVRCAFrame))
784 goto erasure;
785 } else if (e->frame.lsp[0] == 0xf &&
786 e->frame.lsp[1] == 0xf &&
787 e->frame.energy_gain == 0xff) {
788 goto erasure;
789 }
790
791 if (decode_lspf(e) < 0)
792 goto erasure;
793
794 if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
795 /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
797 goto erasure;
798
800
801 /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
802 if (e->frame.delay_diff) {
803 int p = e->pitch_delay - e->frame.delay_diff + 16;
804 if (p < MIN_DELAY || p > MAX_DELAY)
805 goto erasure;
806 }
807
808 /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
809 if (e->frame.delay_diff &&
810 e->bitrate == RATE_FULL && e->prev_error_flag) {
811 float delay;
812
813 memcpy(e->pitch, e->pitch_back, ACB_SIZE * sizeof(float));
814
815 delay = e->prev_pitch_delay;
816 e->prev_pitch_delay = delay - e->frame.delay_diff + 16.0;
817
818 if (fabs(e->pitch_delay - delay) > 15)
819 delay = e->pitch_delay;
820
821 for (i = 0; i < NB_SUBFRAMES; i++) {
822 int subframe_size = subframe_sizes[i];
823
824 interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
825 acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
826 memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
827 }
828 }
829
830 /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
831 if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
833
834 e->avg_acb_gain = e->avg_fcb_gain = 0.0;
835 } else {
836 idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
837
838 /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
839 for (i = 0; i < NB_SUBFRAMES; i++)
842 }
843
844 for (i = 0; i < NB_SUBFRAMES; i++) {
845 float tmp[SUBFRAME_SIZE + 6] = { 0 };
846 int subframe_size = subframe_sizes[i];
847 int pitch_lag;
848
849 interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
850
851 if (e->bitrate != RATE_QUANT)
853
854 pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
855 decode_predictor_coeffs(ilspf, ilpc);
856
857 /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
858 if (e->frame.lpc_flag && e->prev_error_flag)
859 bandwidth_expansion(ilpc, ilpc, 0.75);
860
861 if (e->bitrate != RATE_QUANT) {
862 float acb_sum, f;
863
864 f = exp((e->bitrate == RATE_HALF ? 0.5 : 0.25)
865 * (e->frame.fcb_gain[i] + 1));
866 acb_sum = pitch_gain_vq[e->frame.acb_gain[i]];
867 e->avg_acb_gain += acb_sum / NB_SUBFRAMES;
869
871 acb_sum, idelay, subframe_size);
873 acb_sum, pitch_lag, subframe_size);
874
875 /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
876 for (j = 0; j < subframe_size; j++)
877 e->pitch[ACB_SIZE + j] += f * tmp[j];
878 e->fade_scale = FFMIN(e->fade_scale + 0.2, 1.0);
879 } else {
880 for (j = 0; j < subframe_size; j++)
881 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
882 }
883
884 memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
885
887 e->synthesis, subframe_size,
888 e->postfilter ? tmp : samples);
889 if (e->postfilter)
890 postfilter(e, tmp, ilpc, samples, pitch_lag,
891 &postfilter_coeffs[e->bitrate], subframe_size);
892
893 samples += subframe_size;
894 }
895
896 if (error_flag) {
897erasure:
898 error_flag = 1;
899 av_log(avctx, AV_LOG_WARNING, "frame erasure\n");
900 frame_erasure(e, samples);
901 }
902
903 memcpy(e->prev_lspf, e->lspf, sizeof(e->prev_lspf));
904 e->prev_error_flag = error_flag;
906
907 if (e->bitrate != RATE_QUANT)
909
910 samples = (float *)frame->data[0];
911 for (i = 0; i < 160; i++)
912 samples[i] /= 32768;
913
914 *got_frame_ptr = 1;
915
916 return avpkt->size;
917}
918
919#define OFFSET(x) offsetof(EVRCContext, x)
920#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
921
922static const AVOption options[] = {
923 { "postfilter", "enable postfilter", OFFSET(postfilter), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AD },
924 { NULL }
925};
926
927static const AVClass evrcdec_class = {
928 .class_name = "evrc",
929 .item_name = av_default_item_name,
930 .option = options,
931 .version = LIBAVUTIL_VERSION_INT,
932};
933
935 .p.name = "evrc",
936 CODEC_LONG_NAME("EVRC (Enhanced Variable Rate Codec)"),
937 .p.type = AVMEDIA_TYPE_AUDIO,
938 .p.id = AV_CODEC_ID_EVRC,
939 .init = evrc_decode_init,
942 .priv_data_size = sizeof(EVRCContext),
943 .p.priv_class = &evrcdec_class,
944};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b, float weight_coeff_a, float weight_coeff_b, int length)
float implementation of weighted sum of two vectors.
const FFCodec ff_evrc_decoder
Definition evrcdec.c:934
static void postfilter(AMRContext *p, float *lpc, float *buf_out)
Perform adaptive post-filtering to enhance the quality of the speech.
Definition amrnbdec.c:913
static FILE * out
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
static const unsigned codebook[256][2]
Definition cfhdenc.c:41
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int8_t exp
Definition eval.c:76
Data tables for the EVRC decoder.
static const float pitch_gain_vq[]
Definition evrcdata.h:1496
static const float *const *const evrc_lspq_codebooks[]
Definition evrcdata.h:1454
static const uint8_t *const evrc_lspq_codebooks_row_sizes[]
Definition evrcdata.h:1488
static const uint8_t evrc_lspq_nb_codebooks[]
Definition evrcdata.h:1462
static const float evrc_energy_quant[][3]
Rate 1/8 frame energy quantization.
Definition evrcdata.h:38
static const uint8_t subframe_sizes[]
Definition evrcdata.h:1498
static const float estimation_delay[]
Definition evrcdata.h:1497
static evrc_packet_rate determine_bitrate(AVCodecContext *avctx, int *buf_size, const uint8_t **buf)
Determine the bitrate from the frame size and/or the first byte of the frame.
Definition evrcdec.c:190
static void synthesis_filter(const float *in, const float *filter_coeffs, float *memory, int buffer_length, float *samples)
Synthesis of the decoder output signal.
Definition evrcdec.c:508
#define MIN_LSP_SEP
Definition evrcdec.c:39
#define FILTER_ORDER
Definition evrcdec.c:44
static void frame_erasure(EVRCContext *e, float *samples)
Definition evrcdec.c:656
static evrc_packet_rate buf_size2bitrate(const int buf_size)
Definition evrcdec.c:167
static void acb_excitation(EVRCContext *e, float *excitation, float gain, const float delay[3], int length)
Definition evrcdec.c:408
static void warn_insufficient_frame_quality(AVCodecContext *avctx, const char *message)
Definition evrcdec.c:221
evrc_packet_rate
Definition evrcdec.c:47
@ RATE_HALF
Definition evrcdec.c:52
@ RATE_FULL
Definition evrcdec.c:53
@ SILENCE
Definition evrcdec.c:49
@ RATE_QUARTER
Definition evrcdec.c:51
@ RATE_QUANT
Definition evrcdec.c:50
@ RATE_ERRS
Definition evrcdec.c:48
static void decode_predictor_coeffs(const float *ilspf, float *ilpc)
Definition evrcdec.c:350
static void unpack_frame(EVRCContext *e)
Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT.
Definition evrcdec.c:110
static void decode_8_pulses_35bits(const uint16_t *fixed_index, float *cod)
Definition evrcdec.c:435
#define NB_SUBFRAMES
Definition evrcdec.c:42
static int decode_lspf(EVRCContext *e)
Decode the 10 vector quantized line spectral pair frequencies from the LSP transmission codes of any ...
Definition evrcdec.c:286
static void bandwidth_expansion(float *coeff, const float *inbuf, float gamma)
Definition evrcdec.c:524
static void residual_filter(float *output, const float *input, const float *coef, float *memory, int length)
Definition evrcdec.c:535
#define AD
Definition evrcdec.c:920
static void interpolate_lsp(float *ilsp, const float *lsp, const float *prev, int index)
Definition evrcdec.c:319
static int evrc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition evrcdec.c:746
#define ACB_SIZE
Definition evrcdec.c:45
static void interpolate_delay(float *dst, float current, float prev, int index)
Definition evrcdec.c:333
static const struct PfCoeff postfilter_coeffs[5]
#define OFFSET(x)
Definition evrcdec.c:919
#define MIN_DELAY
Definition evrcdec.c:40
static void decode_3_pulses_10bits(uint16_t fixed_index, float *cod)
Definition evrcdec.c:460
static void fcb_excitation(EVRCContext *e, const uint16_t *codebook, float *excitation, float pitch_gain, int pitch_lag, int subframe_size)
Definition evrcdec.c:480
static const AVClass evrcdec_class
Definition evrcdec.c:927
#define SUBFRAME_SIZE
Definition evrcdec.c:43
static void bl_intrp(EVRCContext *e, float *ex, float delay)
Definition evrcdec.c:380
static av_cold int evrc_decode_init(AVCodecContext *avctx)
Initialize the speech codec according to the specification.
Definition evrcdec.c:233
static void postfilter(EVRCContext *e, float *in, const float *coeff, float *out, int idx, const struct PfCoeff *pfc, int length)
Definition evrcdec.c:575
static struct @111144215057303131116103221376075045141373005341 current
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_EVRC
Definition codec_id.h:524
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
int a
#define b
Definition input.c:43
unsigned offset
Definition libaomenc.c:763
#define MAX_DELAY
Definition diracdec.c:53
#define av_cold
Definition attributes.h:117
#define lrintf(x)
Definition libm_mips.h:72
void ff_acelp_lsf2lspd(double *lsp, const float *lsf, int lp_order)
Floating point version of ff_acelp_lsf2lsp()
Definition lsp.c:97
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
AVOptions.
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
EVRC-A unpacked data frame.
Definition evrcdec.c:59
uint8_t lpc_flag
spectral change indicator
Definition evrcdec.c:60
uint8_t pitch_delay
pitch delay for entire frame
Definition evrcdec.c:62
uint16_t lsp[4]
index into LSP codebook
Definition evrcdec.c:61
uint8_t tty
tty baud rate bit
Definition evrcdec.c:68
uint8_t acb_gain[3]
adaptive codebook gain
Definition evrcdec.c:64
uint8_t fcb_gain[3]
fixed codebook gain index
Definition evrcdec.c:66
uint8_t energy_gain
frame energy gain index
Definition evrcdec.c:67
uint16_t fcb_shape[3][4]
fixed codebook shape
Definition evrcdec.c:65
uint8_t delay_diff
delay difference for entire frame
Definition evrcdec.c:63
float postfilter_fir[FILTER_ORDER]
Definition evrcdec.c:84
float synthesis[FILTER_ORDER]
Definition evrcdec.c:83
GetBitContext gb
Definition evrcdec.c:76
evrc_packet_rate last_valid_bitrate
Definition evrcdec.c:78
uint8_t prev_energy_gain
Definition evrcdec.c:98
float pitch_delay
Definition evrcdec.c:87
float pitch[ACB_SIZE+FILTER_ORDER+SUBFRAME_SIZE]
Definition evrcdec.c:91
float prev_lspf[FILTER_ORDER]
Definition evrcdec.c:82
float interpolation_coeffs[136]
Definition evrcdec.c:93
float postfilter_iir[FILTER_ORDER]
Definition evrcdec.c:85
float fade_scale
Definition evrcdec.c:95
uint8_t warned_buf_mismatch_bitrate
Definition evrcdec.c:100
float energy_vector[NB_SUBFRAMES]
Definition evrcdec.c:94
float lspf[FILTER_ORDER]
Definition evrcdec.c:81
evrc_packet_rate bitrate
Definition evrcdec.c:77
EVRCAFrame frame
Definition evrcdec.c:79
float postfilter_residual[ACB_SIZE+SUBFRAME_SIZE]
Definition evrcdec.c:86
float pitch_back[ACB_SIZE]
Definition evrcdec.c:92
float avg_fcb_gain
average fixed codebook gain
Definition evrcdec.c:90
int postfilter
Definition evrcdec.c:74
float avg_acb_gain
average adaptive codebook gain
Definition evrcdec.c:89
uint8_t prev_error_flag
Definition evrcdec.c:99
float prev_pitch_delay
Definition evrcdec.c:88
float last
Definition evrcdec.c:96
float tilt
Definition evrcdec.c:558
float p2
Definition evrcdec.c:561
float p1
Definition evrcdec.c:560
float ltgain
Definition evrcdec.c:559
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int64_t bitrate
Definition av1_levels.c:47
else temp
Definition vf_mcdeint.c:275
static const double coeff[2][5]
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029
static const uint8_t codebooks[]