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
37
38 #define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
41 #define NB_SUBFRAMES 3
42 #define SUBFRAME_SIZE 54
43 #define FILTER_ORDER 10
45
54
55 /**
56 * EVRC-A unpacked data frame
57 */
59 uint8_t
lpc_flag;
///< spectral change indicator
60 uint16_t
lsp[4];
///< index into LSP codebook
62 uint8_t
delay_diff;
///< delay difference for entire frame
63 uint8_t
acb_gain[3];
///< adaptive codebook gain
65 uint8_t
fcb_gain[3];
///< fixed codebook gain index
67 uint8_t
tty;
///< tty baud rate bit
69
72
74
79
96
101
102 /**
103 * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
104 *
105 * @param e the context
106 *
107 * TIA/IS-127 Table 4.21-1
108 */
110 {
113
142 break;
157 break;
162 break;
163 }
164 }
165
167 {
168 switch (buf_size) {
174 }
175
177 }
178
179 /**
180 * Determine the bitrate from the frame size and/or the first byte of the frame.
181 *
182 * @param avctx the AV codec context
183 * @param buf_size length of the buffer
184 * @param buf the bufffer
185 *
186 * @return the bitrate on success,
187 * RATE_ERRS if the bitrate cannot be satisfactorily determined
188 */
190 int *buf_size,
191 const uint8_t **buf)
192 {
194
200 "Claimed bitrate and buffer size mismatch.\n");
202 }
206 "Buffer is too small for the claimed bitrate.\n");
208 }
209 (*buf)++;
210 *buf_size -= 1;
213 "Bitrate byte is missing, guessing the bitrate from packet size.\n");
214 } else
216
218 }
219
222 {
225 }
226
227 /**
228 * Initialize the speech codec according to the specification.
229 *
230 * TIA/IS-127 5.2
231 */
233 {
236 float denom = 2.0 / (2.0 * 8.0 + 1.0);
237
241
245 }
246
249
255
257 float tt = ((float)
i - 8.0 / 2.0) / 8.0;
258
259 for (n = -8; n <= 8; n++, idx++) {
260 float arg1 =
M_PI * 0.9 * (tt - n);
261 float arg2 =
M_PI * (tt - n);
262
264 if (arg1)
266 sin(arg1) / arg1;
267 }
268 }
269
270 return 0;
271 }
272
273 /**
274 * Decode the 10 vector quantized line spectral pair frequencies from the LSP
275 * transmission codes of any bitrate and check for badly received packets.
276 *
277 * @param e the context
278 *
279 * @return 0 on success, -1 if the packet is badly received
280 *
281 * TIA/IS-127 5.2.1, 5.7.1
282 */
284 {
287
291
292 for (j = 0; j < row_size; j++)
294 }
295
296 // check for monotonic LSPs
299 return -1;
300
301 // check for minimum separation of LSPs at the splits
305 return -1;
306 }
307
308 return 0;
309 }
310
311 /*
312 * Interpolation of LSP parameters.
313 *
314 * TIA/IS-127 5.2.3.1, 5.7.3.2
315 */
317 const float *prev,
int index)
318 {
319 static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
321 1.0 - lsp_interpolation_factors[
index],
323 }
324
325 /*
326 * Reconstruction of the delay contour.
327 *
328 * TIA/IS-127 5.2.2.3.2
329 */
331 {
332 static const float d_interpolation_factors[] = { 0, 0.3313, 0.6625, 1, 1 };
333 dst[0] = (1.0 - d_interpolation_factors[
index ]) * prev
334 + d_interpolation_factors[
index ] * current;
335 dst[1] = (1.0 - d_interpolation_factors[
index + 1]) * prev
336 + d_interpolation_factors[
index + 1] * current;
337 dst[2] = (1.0 - d_interpolation_factors[
index + 2]) * prev
338 + d_interpolation_factors[
index + 2] * current;
339 }
340
341 /*
342 * Convert the quantized, interpolated line spectral frequencies,
343 * to prediction coefficients.
344 *
345 * TIA/IS-127 5.2.3.2, 4.7.2.2
346 */
348 {
356
358
360 a[0] = k < 2 ? 0.25 : 0;
361 b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
362
365 b[
i + 1] =
b[
i] - 2 * lsp[
i * 2 + 1] *
b1[
i] +
b2[
i];
370 }
371
372 if (k)
374 }
375 }
376
378 {
381 int16_t t;
382
384
385 t = (
offset - delay + 0.5) * 8.0 + 0.5;
386 if (t == 8) {
387 t = 0;
389 }
390
392
393 coef_idx = t * (2 * 8 + 1);
394
395 ex[0] = 0.0;
396 for (
i = 0;
i < 2 * 8 + 1;
i++)
398 }
399
400 /*
401 * Adaptive codebook excitation.
402 *
403 * TIA/IS-127 5.2.2.3.3, 4.12.5.2
404 */
406 const float delay[3], int length)
407 {
408 float denom, locdelay, dpr, invl;
410
411 invl = 1.0 / ((float) length);
412 dpr = length;
413
414 /* first at-most extra samples */
415 denom = (delay[1] - delay[0]) * invl;
416 for (
i = 0;
i < dpr;
i++) {
417 locdelay = delay[0] +
i * denom;
419 }
420
421 denom = (delay[2] - delay[1]) * invl;
422 /* interpolation */
423 for (
i = dpr;
i < dpr + 10;
i++) {
424 locdelay = delay[1] + (
i - dpr) * denom;
426 }
427
428 for (
i = 0;
i < length;
i++)
429 excitation[
i] *= gain;
430 }
431
433 {
435
436 offset = (fixed_index[3] >> 9) & 3;
437
438 for (
i = 0;
i < 3;
i++) {
439 pos1 = ((fixed_index[
i] & 0x7f) / 11) * 5 + ((
i +
offset) % 5);
440 pos2 = ((fixed_index[
i] & 0x7f) % 11) * 5 + ((
i +
offset) % 5);
441
442 cod[pos1] = (fixed_index[
i] & 0x80) ? -1.0 : 1.0;
443
444 if (pos2 < pos1)
445 cod[pos2] = -cod[pos1];
446 else
447 cod[pos2] += cod[pos1];
448 }
449
450 pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 +
offset) % 5);
451 pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 +
offset) % 5);
452
453 cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
454 cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
455 }
456
458 {
459 float sign;
461
462 sign = (fixed_index & 0x200) ? -1.0 : 1.0;
463
464 pos = ((fixed_index & 0x7) * 7) + 4;
466 pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
468 pos = (((fixed_index >> 6) & 0x7) * 7);
470 }
471
472 /*
473 * Reconstruction of ACELP fixed codebook excitation for full and half rate.
474 *
475 * TIA/IS-127 5.2.3.7
476 */
478 float *excitation, float pitch_gain,
479 int pitch_lag, int subframe_size)
480 {
482
485 else
487
488 pitch_gain =
av_clipf(pitch_gain, 0.2, 0.9);
489
490 for (
i = pitch_lag;
i < subframe_size;
i++)
491 excitation[
i] += pitch_gain * excitation[
i - pitch_lag];
492 }
493
494 /**
495 * Synthesis of the decoder output signal.
496 *
497 * param[in] in input signal
498 * param[in] filter_coeffs LPC coefficients
499 * param[in/out] memory synthesis filter memory
500 * param buffer_length amount of data to process
501 * param[out] samples output samples
502 *
503 * TIA/IS-127 5.2.3.15, 5.7.3.4
504 */
506 float *memory,
int buffer_length,
float *
samples)
507 {
509
510 for (
i = 0;
i < buffer_length;
i++) {
513 samples[
i] -= filter_coeffs[j] * memory[j];
514 memory[j] = memory[j - 1];
515 }
516 samples[
i] -= filter_coeffs[0] * memory[0];
518 }
519 }
520
522 {
523 double fac = gamma;
525
528 fac *= gamma;
529 }
530 }
531
533 const float *coef, float *memory, int length)
534 {
535 float sum;
537
538 for (
i = 0;
i < length;
i++) {
540
542 sum += coef[j] * memory[j];
543 memory[j] = memory[j - 1];
544 }
545 sum += coef[0] * memory[0];
548 }
549 }
550
551 /*
552 * TIA/IS-127 Table 5.9.1-1.
553 */
560 { 0.0 , 0.0 , 0.0 , 0.0 },
561 { 0.0 , 0.0 , 0.57, 0.57 },
562 { 0.0 , 0.0 , 0.0 , 0.0 },
563 { 0.35, 0.50, 0.50, 0.75 },
564 { 0.20, 0.50, 0.57, 0.75 },
565 };
566
567 /*
568 * Adaptive postfilter.
569 *
570 * TIA/IS-127 5.9
571 */
573 float *
out,
int idx,
const struct PfCoeff *pfc,
574 int length)
575 {
579 float sum1 = 0.0, sum2 = 0.0, gamma, gain;
580 float tilt = pfc->
tilt;
582
585
586 /* Tilt compensation filter, TIA/IS-127 5.9.1 */
587 for (
i = 0;
i < length - 1;
i++)
588 sum2 += in[
i] * in[
i + 1];
589 if (sum2 < 0.0)
590 tilt = 0.0;
591
592 for (
i = 0;
i < length;
i++) {
593 scratch[
i] = in[
i] - tilt * e->
last;
595 }
596
597 /* Short term residual filter, TIA/IS-127 5.9.2 */
599
600 /* Long term postfilter */
601 best = idx;
606 sum1 = sum2;
608 }
609 }
610
615
618 } else {
619 gamma = sum2 / sum1;
620 if (gamma < 0.5)
622 else {
623 gamma =
FFMIN(gamma, 1.0);
624
625 for (
i = 0;
i < length;
i++) {
628 }
629 }
630 }
631
632 memcpy(scratch,
temp, length *
sizeof(
float));
635
636 /* Gain computation, TIA/IS-127 5.9.4-2 */
637 for (
i = 0, sum1 = 0, sum2 = 0;
i < length;
i++) {
638 sum1 += in[
i] * in[
i];
639 sum2 += scratch[
i] * scratch[
i];
640 }
641 gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
642
643 for (
i = 0;
i < length;
i++)
645
646 /* Short term postfilter */
648
651 }
652
654 {
658
662 else
664 }
665
672 else
674
677 } else {
678 float sum = 0;
679
680 idelay[0] = idelay[1] = idelay[2] =
MIN_DELAY;
681
685 sum = pow(10, sum);
688 }
689
692
695 int pitch_lag;
696
698
704 } else {
706 }
707 }
708
709 pitch_lag =
lrintf((idelay[1] + idelay[0]) / 2.0);
711
715 for (j = 0; j < subframe_size; j++)
718 } else {
719 for (j = 0; j < subframe_size; j++)
721 }
722
724
727 for (j = 0; j < subframe_size; j++)
730 for (j = 0; j < subframe_size; j++)
732 }
733
738
740 }
741 }
742
744 int *got_frame_ptr,
AVPacket *avpkt)
745 {
746 const uint8_t *buf = avpkt->
data;
749 int buf_size = avpkt->
size;
752 int i, j,
ret, error_flag = 0;
753
754 frame->nb_samples = 160;
758
761 goto erasure;
762 }
764 goto erasure;
767 goto erasure;
768
772
774
776 uint8_t *p = (uint8_t *) &e->
frame;
779 break;
780 }
782 goto erasure;
786 goto erasure;
787 }
788
790 goto erasure;
791
793 /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
795 goto erasure;
796
798
799 /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
803 goto erasure;
804 }
805
806 /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
809 float delay;
810
812
815
818
821
825 }
826 }
827
828 /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
831
833 } else {
834 idelay[0] = idelay[1] = idelay[2] =
MIN_DELAY;
835
836 /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
840 }
841
845 int pitch_lag;
846
848
851
852 pitch_lag =
lrintf((idelay[1] + idelay[0]) / 2.0);
854
855 /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
858
861
867
869 acb_sum, idelay, subframe_size);
871 acb_sum, pitch_lag, subframe_size);
872
873 /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
874 for (j = 0; j < subframe_size; j++)
877 } else {
878 for (j = 0; j < subframe_size; j++)
880 }
881
883
890
892 }
893
894 if (error_flag) {
895 erasure:
896 error_flag = 1;
899 }
900
904
907
911
912 *got_frame_ptr = 1;
913
915 }
916
917 #define OFFSET(x) offsetof(EVRCContext, x)
918 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
919
923 };
924
930 };
931
943 };