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
35
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
43
52
53 /**
54 * EVRC-A unpacked data frame
55 */
58 uint16_t
lsp[4];
///< index into LSP codebook
67
73
90
95
96 /**
97 * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
98 *
99 * @param e the context
100 *
101 * TIA/IS-127 Table 4.21-1
102 */
104 {
107
136 break;
151 break;
156 break;
157 }
158 }
159
161 {
162 switch (buf_size) {
168 }
169
171 }
172
173 /**
174 * Determine the bitrate from the frame size and/or the first byte of the frame.
175 *
176 * @param avctx the AV codec context
177 * @param buf_size length of the buffer
178 * @param buf the bufffer
179 *
180 * @return the bitrate on success,
181 * RATE_ERRS if the bitrate cannot be satisfactorily determined
182 */
184 int *buf_size,
186 {
188
190 if (bitrate > **buf) {
194 "Claimed bitrate and buffer size mismatch.\n");
196 }
198 } else if (bitrate < **buf) {
200 "Buffer is too small for the claimed bitrate.\n");
202 }
203 (*buf)++;
204 *buf_size -= 1;
207 "Bitrate byte is missing, guessing the bitrate from packet size.\n");
208 } else
210
211 return bitrate;
212 }
213
215 const char *message)
216 {
219 }
220
221 /**
222 * Initialize the speech codec according to the specification.
223 *
224 * TIA/IS-127 5.2
225 */
227 {
230 float denom = 2.0 / (2.0 * 8.0 + 1.0);
231
235
239 }
240
243
249
250 for (i = 0; i < 8; i++) {
251 float tt = ((float)i - 8.0 / 2.0) / 8.0;
252
253 for (n = -8; n <= 8; n++, idx++) {
254 float arg1 =
M_PI * 0.9 * (tt -
n);
255 float arg2 =
M_PI * (tt -
n);
256
258 if (arg1)
260 sin(arg1) / arg1;
261 }
262 }
263
264 return 0;
265 }
266
267 /**
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.
270 *
271 * @param e the context
272 *
273 * @return 0 on success, -1 if the packet is badly received
274 *
275 * TIA/IS-127 5.2.1, 5.7.1
276 */
278 {
280 int i, j, k = 0;
281
284 const float *codebook = codebooks[i];
285
287 e->
lspf[k++] = codebook[e->
frame.
lsp[i] * row_size + j];
288 }
289
290 // check for monotonic LSPs
293 return -1;
294
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++) {
299 return -1;
300 }
301
302 return 0;
303 }
304
305 /*
306 * Interpolation of LSP parameters.
307 *
308 * TIA/IS-127 5.2.3.1, 5.7.3.2
309 */
311 const float *prev,
int index)
312 {
313 static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
315 1.0 - lsp_interpolation_factors[index],
317 }
318
319 /*
320 * Reconstruction of the delay contour.
321 *
322 * TIA/IS-127 5.2.2.3.2
323 */
325 {
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;
333 }
334
335 /*
336 * Convert the quantized, interpolated line spectral frequencies,
337 * to prediction coefficients.
338 *
339 * TIA/IS-127 5.2.3.2, 4.7.2.2
340 */
342 {
349 int i, k;
350
352
354 a[0] = k < 2 ? 0.25 : 0;
355 b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
356
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];
360 a2[i] = a1[i];
361 a1[i] = a[i];
362 b2[i] = b1[i];
363 b1[i] = b[i];
364 }
365
366 if (k)
368 }
369 }
370
372 {
373 float *f;
376
378
379 t = (offset - delay + 0.5) * 8.0 + 0.5;
380 if (t == 8) {
381 t = 0;
382 offset--;
383 }
384
385 f = ex - offset - 8;
386
387 coef_idx = t * (2 * 8 + 1);
388
389 ex[0] = 0.0;
390 for (i = 0; i < 2 * 8 + 1; i++)
392 }
393
394 /*
395 * Adaptive codebook excitation.
396 *
397 * TIA/IS-127 5.2.2.3.3, 4.12.5.2
398 */
400 const float delay[3],
int length)
401 {
402 float denom, locdelay, dpr, invl;
403 int i;
404
405 invl = 1.0 / ((float) length);
407
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);
413 }
414
415 denom = (delay[2] - delay[1]) * invl;
416 /* interpolation */
417 for (i = dpr; i < dpr + 10; i++) {
418 locdelay = delay[1] + (i - dpr) * denom;
419 bl_intrp(e, excitation + i, locdelay);
420 }
421
422 for (i = 0; i <
length; i++)
423 excitation[i] *= gain;
424 }
425
427 {
428 int i, pos1, pos2,
offset;
429
430 offset = (fixed_index[3] >> 9) & 3;
431
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);
435
436 cod[pos1] = (fixed_index[i] & 0x80) ? -1.0 : 1.0;
437
438 if (pos2 < pos1)
439 cod[pos2] = -cod[pos1];
440 else
441 cod[pos2] += cod[pos1];
442 }
443
444 pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 +
offset) % 5);
445 pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 +
offset) % 5);
446
447 cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
448 cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
449 }
450
452 {
453 float sign;
454 int pos;
455
456 sign = (fixed_index & 0x200) ? -1.0 : 1.0;
457
458 pos = ((fixed_index & 0x7) * 7) + 4;
459 cod[pos] += sign;
460 pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
461 cod[pos] -= sign;
462 pos = (((fixed_index >> 6) & 0x7) * 7);
463 cod[pos] += sign;
464 }
465
466 /*
467 * Reconstruction of ACELP fixed codebook excitation for full and half rate.
468 *
469 * TIA/IS-127 5.2.3.7
470 */
472 float *excitation, float pitch_gain,
473 int pitch_lag, int subframe_size)
474 {
475 int i;
476
479 else
481
482 pitch_gain = av_clipf(pitch_gain, 0.2, 0.9);
483
484 for (i = pitch_lag; i < subframe_size; i++)
485 excitation[i] += pitch_gain * excitation[i - pitch_lag];
486 }
487
488 /**
489 * Synthesis of the decoder output signal.
490 *
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
496 *
497 * TIA/IS-127 5.2.3.15, 5.7.3.4
498 */
500 float *memory, int buffer_length, float *samples)
501 {
502 int i, j;
503
504 for (i = 0; i < buffer_length; i++) {
505 samples[i] = in[i];
507 samples[i] -= filter_coeffs[j] * memory[j];
508 memory[j] = memory[j - 1];
509 }
510 samples[i] -= filter_coeffs[0] * memory[0];
511 memory[0] = samples[i];
512 }
513 }
514
516 {
517 double fac = gamma;
518 int i;
519
521 coeff[i] = inbuf[i] * fac;
522 fac *= gamma;
523 }
524 }
525
527 const float *coef,
float *memory,
int length)
528 {
529 float sum;
530 int i, j;
531
532 for (i = 0; i <
length; i++) {
533 sum = input[i];
534
536 sum += coef[j] * memory[j];
537 memory[j] = memory[j - 1];
538 }
539 sum += coef[0] * memory[0];
540 memory[0] = input[i];
541 output[i] = sum;
542 }
543 }
544
545 /*
546 * TIA/IS-127 Table 5.9.1-1.
547 */
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 },
559 };
560
561 /*
562 * Adaptive postfilter.
563 *
564 * TIA/IS-127 5.9
565 */
567 float *
out,
int idx,
const struct PfCoeff *pfc,
569 {
573 float sum1 = 0.0, sum2 = 0.0, gamma, gain;
574 float tilt = pfc->
tilt;
576
579
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];
583 if (sum2 < 0.0)
584 tilt = 0.0;
585
586 for (i = 0; i <
length; i++) {
587 scratch[i] = in[i] - tilt * e->
last;
589 }
590
591 /* Short term residual filter, TIA/IS-127 5.9.2 */
593
594 /* Long term postfilter */
595 best = idx;
600 sum1 = sum2;
601 best = i;
602 }
603 }
604
609
612 } else {
613 gamma = sum2 / sum1;
614 if (gamma < 0.5)
616 else {
617 gamma =
FFMIN(gamma, 1.0);
618
619 for (i = 0; i <
length; i++) {
622 }
623 }
624 }
625
626 memcpy(scratch, temp, length * sizeof(float));
629
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];
634 }
635 gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
636
637 for (i = 0; i <
length; i++)
638 temp[i] *= gain;
639
640 /* Short term postfilter */
642
645 }
646
648 {
651 int i, j;
652
655 e->
lspf[i] = e->
prev_lspf[i] * 0.875 + 0.125 * (i + 1) * 0.048;
656 else
658 }
659
666 else
668
671 } else {
672 float sum = 0;
673
674 idelay[0] = idelay[1] = idelay[2] =
MIN_DELAY;
675
679 sum = pow(10, sum);
682 }
683
686
689 int pitch_lag;
690
692
698 } else {
700 }
701 }
702
703 pitch_lag =
lrintf((idelay[1] + idelay[0]) / 2.0);
705
709 for (j = 0; j < subframe_size; j++)
712 } else {
713 for (j = 0; j < subframe_size; j++)
715 }
716
718
721 for (j = 0; j < subframe_size; j++)
724 for (j = 0; j < subframe_size; j++)
726 }
727
732
733 samples += subframe_size;
734 }
735 }
736
738 int *got_frame_ptr,
AVPacket *avpkt)
739 {
743 int buf_size = avpkt->
size;
745 float *samples;
746 int i, j,
ret, error_flag = 0;
747
751 samples = (
float *)frame->
data[0];
752
755 goto erasure;
756 }
758 goto erasure;
761 goto erasure;
762
765
767
771 if (p[i])
772 break;
773 }
775 goto erasure;
776 }
else if (e->
frame.
lsp[0] == 0xf &&
779 goto erasure;
780 }
781
783 goto erasure;
784
786 /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
788 goto erasure;
789
791
792 /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
796 goto erasure;
797 }
798
799 /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
802 float delay;
803
805
808
811
814
818 }
819 }
820
821 /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
824
826 } else {
827 idelay[0] = idelay[1] = idelay[2] =
MIN_DELAY;
828
829 /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
833 }
834
838 int pitch_lag;
839
841
844
845 pitch_lag =
lrintf((idelay[1] + idelay[0]) / 2.0);
847
848 /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
851
853 float acb_sum, f;
854
860
862 acb_sum, idelay, subframe_size);
864 acb_sum, pitch_lag, subframe_size);
865
866 /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
867 for (j = 0; j < subframe_size; j++)
870 } else {
871 for (j = 0; j < subframe_size; j++)
873 }
874
876
881
882 samples += subframe_size;
883 }
884
885 if (error_flag) {
886 erasure:
887 error_flag = 1;
890 }
891
895
898
899 samples = (
float *)frame->
data[0];
900 for (i = 0; i < 160; i++)
901 samples[i] /= 32768;
902
903 *got_frame_ptr = 1;
904
906 }
907
917 };