1 /*
2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
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 * Opus SILK decoder
25 */
26
27 #include <stdint.h>
28
30 #include "mathops.h"
35
36 #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
37
43
47
50
54
60
63
67
69 };
70
72 {
74 for (pass = 0; pass < 20; pass++) {
75 int k, min_diff = 0;
76 for (
i = 0;
i < order+1;
i++) {
77 int low =
i != 0 ? nlsf[
i-1] : 0;
78 int high =
i != order ? nlsf[
i] : 32768;
80
81 if (
diff < min_diff) {
84
85 if (pass == 20)
86 break;
87 }
88 }
89 if (min_diff == 0) /* no issues; stabilized */
90 return;
91
92 /* wiggle one or two LSFs */
93 if (k == 0) {
94 /* repel away from lower bound */
95 nlsf[0] = min_delta[0];
96 } else if (k == order) {
97 /* repel away from higher bound */
98 nlsf[order-1] = 32768 - min_delta[order];
99 } else {
100 /* repel away from current position */
101 int min_center = 0, max_center = 32768, center_val;
102
103 /* lower extent */
104 for (
i = 0;
i < k;
i++)
105 min_center += min_delta[
i];
106 min_center += min_delta[k] >> 1;
107
108 /* upper extent */
109 for (
i = order;
i > k;
i--)
110 max_center -= min_delta[
i];
111 max_center -= min_delta[k] >> 1;
112
113 /* move apart */
114 center_val = nlsf[k - 1] + nlsf[k];
115 center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2
116 center_val =
FFMIN(max_center,
FFMAX(min_center, center_val));
117
118 nlsf[k - 1] = center_val - (min_delta[k] >> 1);
119 nlsf[k] = nlsf[k - 1] + min_delta[k];
120 }
121 }
122
123 /* resort to the fall-back method, the standard method for LSF stabilization */
124
125 /* sort; as the LSFs should be nearly sorted, use insertion sort */
126 for (
i = 1;
i < order;
i++) {
128 for (j =
i - 1; j >= 0 && nlsf[j] >
value; j--)
129 nlsf[j + 1] = nlsf[j];
131 }
132
133 /* push forwards to increase distance */
134 if (nlsf[0] < min_delta[0])
135 nlsf[0] = min_delta[0];
136 for (
i = 1;
i < order;
i++)
137 nlsf[
i] =
FFMAX(nlsf[
i],
FFMIN(nlsf[
i - 1] + min_delta[
i], 32767));
138
139 /* push backwards to increase distance */
140 if (nlsf[order-1] > 32768 - min_delta[order])
141 nlsf[order-1] = 32768 - min_delta[order];
142 for (
i = order-2;
i >= 0;
i--)
143 if (nlsf[
i] > nlsf[
i + 1] - min_delta[
i+1])
144 nlsf[
i] = nlsf[
i + 1] - min_delta[
i+1];
145
146 return;
147 }
148
150 {
151 int k, j, DC_resp = 0;
153 int totalinvgain = 1 << 30; // 1.0 in Q30
154 int32_t *row = lpc32[0], *prevrow;
155
156 /* initialize the first row for the Levinson recursion */
157 for (k = 0; k < order; k++) {
158 DC_resp += lpc[k];
159 row[k] = lpc[k] * 4096;
160 }
161
162 if (DC_resp >= 4096)
163 return 0;
164
165 /* check if prediction gain pushes any coefficients too far */
166 for (k = order - 1; 1; k--) {
167 int rc; // Q31; reflection coefficient
168 int gaindiv; // Q30; inverse of the gain (the divisor)
169 int gain; // gain for this reflection coefficient
170 int fbits; // fractional bits used for the gain
171 int error;
// Q29; estimate of the error of our partial estimate of 1/gaindiv
172
173 if (
FFABS(row[k]) > 16773022)
174 return 0;
175
176 rc = -(row[k] * 128);
177 gaindiv = (1 << 30) -
MULH(rc, rc);
178
179 totalinvgain =
MULH(totalinvgain, gaindiv) << 2;
180 if (k == 0)
181 return (totalinvgain >= 107374);
182
183 /* approximate 1.0/gaindiv */
185 gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16>
186 error = (1 << 29) -
MULL(gaindiv << (15 + 16 - fbits), gain, 16);
187 gain = ((gain << 16) + (
error * gain >> 13));
188
189 /* switch to the next row of the LPC coefficients */
190 prevrow = row;
191 row = lpc32[k & 1];
192
193 for (j = 0; j < k; j++) {
196
197 /* per RFC 8251 section 6, if this calculation overflows, the filter
198 is considered unstable. */
199 if (tmp < INT32_MIN || tmp > INT32_MAX)
200 return 0;
201
203 }
204 }
205 }
206
208 int32_t pol[
/* half_order + 1 */],
int half_order)
209 {
211
212 pol[0] = 65536; // 1.0 in Q16
213 pol[1] = -lsp[0];
214
215 for (
i = 1;
i < half_order;
i++) {
217 for (j =
i; j > 1; j--)
218 pol[j] += pol[j - 2] -
ROUND_MULL(lsp[2 *
i], pol[j - 1], 16);
219
220 pol[1] -= lsp[2 *
i];
221 }
222 }
223
224 static void silk_lsf2lpc(
const int16_t nlsf[16],
float lpcf[16],
int order)
225 {
227 int32_t lsp[16];
// Q17; 2*cos(LSF)
230 int16_t lpc[16]; // Q12
231
232 /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */
233 for (k = 0; k < order; k++) {
234 int index = nlsf[k] >> 8;
235 int offset = nlsf[k] & 255;
237
238 /* interpolate and round */
241 lsp[k2] = (lsp[k2] + 4) >> 3;
242 }
243
246
247 /* reconstruct A(z) */
248 for (k = 0; k < order>>1; k++) {
250 int32_t q_tmp = q[k + 1] - q[k];
251 lpc32[k] = -q_tmp - p_tmp;
252 lpc32[order-k-1] = q_tmp - p_tmp;
253 }
254
255 /* limit the range of the LPC coefficients to each fit within an int16_t */
256 for (
i = 0;
i < 10;
i++) {
257 int j;
258 unsigned int maxabs = 0;
259 for (j = 0, k = 0; j < order; j++) {
260 unsigned int x =
FFABS(lpc32[k]);
261 if (x > maxabs) {
262 maxabs = x; // Q17
263 k = j;
264 }
265 }
266
267 maxabs = (maxabs + 16) >> 5; // convert to Q12
268
269 if (maxabs > 32767) {
270 /* perform bandwidth expansion */
271 unsigned int chirp, chirp_base; // Q16
272 maxabs =
FFMIN(maxabs, 163838);
// anything above this overflows chirp's numerator
273 chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);
274
275 for (k = 0; k < order; k++) {
277 chirp = (chirp_base * chirp + 32768) >> 16;
278 }
279 } else break;
280 }
281
283 /* time's up: just clamp */
284 for (k = 0; k < order; k++) {
285 int x = (lpc32[k] + 16) >> 5;
287 lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits
288 }
289 } else {
290 for (k = 0; k < order; k++)
291 lpc[k] = (lpc32[k] + 16) >> 5;
292 }
293
294 /* if the prediction gain causes the LPC filter to become unstable,
295 apply further bandwidth expansion on the Q17 coefficients */
297 unsigned int chirp, chirp_base;
298 chirp_base = chirp = 65536 - (1 <<
i);
299
300 for (k = 0; k < order; k++) {
302 lpc[k] = (lpc32[k] + 16) >> 5;
303 chirp = (chirp_base * chirp + 32768) >> 16;
304 }
305 }
306
307 for (
i = 0;
i < order;
i++)
308 lpcf[
i] = lpc[
i] / 4096.0
f;
309 }
310
313 float lpc_leadin[16], float lpc[16],
314 int *lpc_order, int *has_lpc_leadin, int voiced)
315 {
317 int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB
318 int8_t lsf_i1, lsf_i2[16]; // stage-1 and stage-2 codebook indices
319 int16_t lsf_res[16]; // residual as a Q10 value
320 int16_t nlsf[16]; // Q15
321
322 *lpc_order = order =
s->wb ? 16 : 10;
323
324 /* obtain LSF stage-1 and stage-2 indices */
326 for (
i = 0;
i < order;
i++) {
332 else if (lsf_i2[
i] == 4)
334 }
335
336 /* reverse the backwards-prediction step */
337 for (
i = order - 1;
i >= 0;
i--) {
338 int qstep =
s->wb ? 9830 : 11796;
339
340 lsf_res[
i] = lsf_i2[
i] * 1024;
341 if (lsf_i2[
i] < 0) lsf_res[
i] += 102;
342 else if (lsf_i2[
i] > 0) lsf_res[
i] -= 102;
343 lsf_res[
i] = (lsf_res[
i] * qstep) >> 16;
344
348 lsf_res[
i] += (lsf_res[
i+1] *
weight) >> 8;
349 }
350 }
351
352 /* reconstruct the NLSF coefficients from the supplied indices */
353 for (
i = 0;
i < order;
i++) {
356 int cur, prev, next, weight_sq,
weight, ipart, fpart, y,
value;
357
358 /* find the weight of the residual */
359 /* TODO: precompute */
363 weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16;
364
365 /* approximate square-root with mandated fixed-point arithmetic */
367 fpart = (weight_sq >> (ipart-8)) & 127;
368 y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1);
369 weight = y + ((213 * fpart * y) >> 16);
370
373 }
374
375 /* stabilize the NLSF coefficients */
378
379 /* produce an interpolation for the first 2 subframes, */
380 /* and then convert both sets of NLSFs to LPC coefficients */
381 *has_lpc_leadin = 0;
382 if (
s->subframes == 4) {
385 *has_lpc_leadin = 1;
387 int16_t nlsf_leadin[16];
388 for (
i = 0;
i < order;
i++)
389 nlsf_leadin[
i] =
frame->nlsf[
i] +
392 } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */
393 memcpy(lpc_leadin,
frame->lpc, 16 *
sizeof(
float));
394 } else
396 s->nlsf_interp_factor =
offset;
397
399 } else {
400 s->nlsf_interp_factor = 4;
402 }
403
404 memcpy(
frame->nlsf, nlsf, order *
sizeof(nlsf[0]));
405 memcpy(
frame->lpc, lpc, order *
sizeof(lpc[0]));
406 }
407
410 {
411 if (total != 0) {
414 child[1] = total - child[0];
415 } else {
416 child[0] = 0;
417 child[1] = 0;
418 }
419 }
420
422 float* excitationf,
423 int qoffset_high, int active, int voiced)
424 {
427 int shellblocks;
428 int ratelevel;
429 uint8_t pulsecount[20]; // total pulses in each shell block
430 uint8_t lsbcount[20] = {0}; // raw lsbits defined for each pulse in each shell block
431 int32_t excitation[320];
// Q23
432
433 /* excitation parameters */
437
438 for (
i = 0;
i < shellblocks;
i++) {
440 if (pulsecount[
i] == 17) {
441 while (pulsecount[
i] == 17 && ++lsbcount[
i] != 10)
443 if (lsbcount[
i] == 10)
445 }
446 }
447
448 /* decode pulse locations using PVQ */
449 for (
i = 0;
i < shellblocks;
i++) {
450 if (pulsecount[
i] != 0) {
452 int32_t * location = excitation + 16*
i;
454 branch[0][0] = pulsecount[
i];
455
456 /* unrolled tail recursion */
457 for (
a = 0;
a < 1;
a++) {
459 for (
b = 0;
b < 2;
b++) {
461 for (
c = 0;
c < 2;
c++) {
463 for (d = 0; d < 2; d++) {
465 location += 2;
466 }
467 }
468 }
469 }
470 } else
471 memset(excitation + 16*
i, 0, 16*
sizeof(
int32_t));
472 }
473
474 /* decode least significant bits */
475 for (
i = 0;
i < shellblocks << 4;
i++) {
477 for (
bit = 0; bit < lsbcount[i >> 4];
bit++)
478 excitation[
i] = (excitation[
i] << 1) |
480 }
481
482 /* decode signs */
483 for (
i = 0;
i < shellblocks << 4;
i++) {
484 if (excitation[
i] != 0) {
486 voiced][qoffset_high][
FFMIN(pulsecount[
i >> 4], 6)]);
487 if (sign == 0)
489 }
490 }
491
492 /* assemble the excitation */
493 for (
i = 0;
i < shellblocks << 4;
i++) {
494 int value = excitation[
i];
496 if (
value < 0) excitation[
i] += 20;
497 else if (
value > 0) excitation[
i] -= 20;
498
499 /* invert samples pseudorandomly */
500 seed = 196314165 *
seed + 907633515;
501 if (
seed & 0x80000000)
504
505 excitationf[
i] = excitation[
i] / 8388608.0f;
506 }
507 }
508
509 /** Maximum residual history according to 4.2.7.6.1 */
510 #define SILK_MAX_LAG (288 + LTP_ORDER / 2)
511
512 /** Order of the LTP filter */
514
516 int frame_num,
int channel,
int coded_channels,
517 int active, int active1, int redundant)
518 {
519 /* per frame */
520 int voiced; // combines with active to indicate inactive, active, or active+voiced
521 int qoffset_high;
522 int order; // order of the LPC coefficients
524 int has_lpc_leadin;
525 float ltpscale;
526
527 /* per subframe */
528 struct {
529 float gain;
530 int pitchlag;
531 float ltptaps[5];
532 } sf[4];
533
535
537
538 /* obtain stereo weights */
539 if (coded_channels == 2 &&
channel == 0) {
540 int n, wi[2], ws[2],
w[2];
546
547 for (
i = 0;
i < 2;
i++)
551
552 s->stereo_weights[0] = (
w[0] -
w[1]) / 8192.0;
553 s->stereo_weights[1] =
w[1] / 8192.0;
554
555 /* and read the mid-only flag */
557 }
558
559 /* obtain frame type */
560 if (!active) {
562 voiced = 0;
563 } else {
565 qoffset_high =
type & 1;
567 }
568
569 /* obtain subframe quantization gains */
570 for (
i = 0;
i <
s->subframes;
i++) {
571 int log_gain; //Q7
572 int ipart, fpart, lingain;
573
574 if (
i == 0 && (frame_num == 0 || !
frame->coded)) {
575 /* gain is coded absolute */
578
580 log_gain =
FFMAX(log_gain,
frame->log_gain - 16);
581 } else {
582 /* gain is coded relative */
585 frame->log_gain + delta_gain - 4), 6);
586 }
587
588 frame->log_gain = log_gain;
589
590 /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */
591 log_gain = (log_gain * 0x1D1C71 >> 16) + 2090;
592 ipart = log_gain >> 7;
593 fpart = log_gain & 127;
594 lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7);
595 sf[
i].gain = lingain / 65536.0f;
596 }
597
598 /* obtain LPC filter coefficients */
600
601 /* obtain pitch lags, if this is a voiced frame */
602 if (voiced) {
603 int lag_absolute = (!frame_num || !
frame->prev_voiced);
604 int primarylag; // primary pitch lag for the entire SILK frame
605 int ltpfilter;
607
608 if (!lag_absolute) {
612 else
613 lag_absolute = 1;
614 }
615
616 if (lag_absolute) {
617 /* primary lag is coded absolute */
618 int highbits, lowbits;
619 static const uint16_t * const model[] = {
622 };
625
628 }
629 frame->primarylag = primarylag;
630
631 if (
s->subframes == 2)
637 else
643
644 for (
i = 0;
i <
s->subframes;
i++)
648
649 /* obtain LTP filter coefficients */
651 for (
i = 0;
i <
s->subframes;
i++) {
653 static const uint16_t * const filter_sel[] = {
656 };
657 static const int8_t (* const filter_taps[])[5] = {
659 };
661 for (j = 0; j < 5; j++)
662 sf[
i].ltptaps[j] = filter_taps[ltpfilter][
index][j] / 128.0
f;
663 }
664 }
665
666 /* obtain LTP scale factor */
667 if (voiced && frame_num == 0)
670 else ltpscale = 15565.0f/16384.0f;
671
672 /* generate the excitation signal for the entire frame */
674 active, voiced);
675
676 /* skip synthesising the output if we do not need it */
677 // TODO: implement error recovery
678 if (
s->output_channels ==
channel || redundant)
679 return;
680
681 /* generate the output signal */
682 for (
i = 0;
i <
s->subframes;
i++) {
683 const float * lpc_coeff = (
i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body;
687 float sum;
688 int j, k;
689
690 if (voiced) {
691 int out_end;
693
694 if (i < 2 || s->nlsf_interp_factor == 4) {
695 out_end = -
i *
s->sflength;
697 } else {
698 out_end = -(
i - 2) *
s->sflength;
700 }
701
702 /* when the LPC coefficients change, a re-whitening filter is used */
703 /* to produce a residual that accounts for the change */
704 for (j = - sf[
i].pitchlag -
LTP_ORDER/2; j < out_end; j++) {
706 for (k = 0; k < order; k++)
707 sum -= lpc_coeff[k] *
dst[j - k - 1];
709 }
710
711 if (out_end) {
712 float rescale = sf[
i-1].gain / sf[
i].gain;
713 for (j = out_end; j < 0; j++)
715 }
716
717 /* LTP synthesis */
718 for (j = 0; j <
s->sflength; j++) {
719 sum = resptr[j];
721 sum += sf[
i].ltptaps[k] * resptr[j - sf[
i].pitchlag +
LTP_ORDER/2 - k];
722 resptr[j] = sum;
723 }
724 }
725
726 /* LPC synthesis */
727 for (j = 0; j <
s->sflength; j++) {
728 sum = resptr[j] * sf[
i].gain;
729 for (k = 1; k <= order; k++)
730 sum += lpc_coeff[k - 1] * lpc[j - k];
731
732 lpc[j] = sum;
734 }
735 }
736
737 frame->prev_voiced = voiced;
740
742 }
743
745 {
748 float w0_prev =
s->prev_stereo_weights[0];
749 float w1_prev =
s->prev_stereo_weights[1];
750 float w0 =
s->stereo_weights[0];
751 float w1 =
s->stereo_weights[1];
754
755 for (
i = 0;
i < n1;
i++) {
756 float interp0 = w0_prev +
i * (w0 - w0_prev) / n1;
757 float interp1 = w1_prev +
i * (w1 - w1_prev) / n1;
758 float p0 = 0.25 * (mid[
i - 2] + 2 * mid[
i - 1] + mid[
i]);
759
760 l[
i] =
av_clipf((1 + interp1) * mid[
i - 1] + side[
i - 1] + interp0 * p0, -1.0, 1.0);
761 r[
i] =
av_clipf((1 - interp1) * mid[
i - 1] - side[
i - 1] - interp0 * p0, -1.0, 1.0);
762 }
763
764 for (;
i <
s->flength;
i++) {
765 float p0 = 0.25 * (mid[
i - 2] + 2 * mid[
i - 1] + mid[
i]);
766
767 l[
i] =
av_clipf((1 + w1) * mid[
i - 1] + side[
i - 1] + w0 * p0, -1.0, 1.0);
768 r[
i] =
av_clipf((1 - w1) * mid[
i - 1] - side[
i - 1] - w0 * p0, -1.0, 1.0);
769 }
770
771 memcpy(
s->prev_stereo_weights,
s->stereo_weights,
sizeof(
s->stereo_weights));
772 }
773
775 {
777 return;
778
779 memset(
frame->output, 0,
sizeof(
frame->output));
780 memset(
frame->lpc_history, 0,
sizeof(
frame->lpc_history));
781
784
786
787 frame->primarylag = 0;
788 frame->prev_voiced = 0;
790 }
791
795 int coded_channels,
796 int duration_ms)
797 {
798 int active[2][6], redundancy[2];
800
802 coded_channels > 2 || duration_ms > 60) {
804 "to the SILK decoder.\n");
806 }
807
808 nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40);
809 s->subframes = duration_ms / nb_frames / 5;
// 5ms subframes
810 s->sflength = 20 * (bandwidth + 2);
811 s->flength =
s->sflength *
s->subframes;
812 s->bandwidth = bandwidth;
814
815 /* make sure to flush the side channel when switching from mono to stereo */
816 if (coded_channels >
s->prev_coded_channels)
818 s->prev_coded_channels = coded_channels;
819
820 /* read the LP-layer header bits */
821 for (
i = 0;
i < coded_channels;
i++) {
822 for (j = 0; j < nb_frames; j++)
824
826 }
827
828 /* read the per-frame LBRR flags */
829 for (
i = 0;
i < coded_channels;
i++)
830 if (redundancy[
i] && duration_ms > 20) {
833 }
834
835 /* decode the LBRR frames */
836 for (
i = 0;
i < nb_frames;
i++) {
837 for (j = 0; j < coded_channels; j++)
838 if (redundancy[j] & (1 <<
i)) {
839 int active1 = (j == 0 && !(redundancy[1] & (1 <<
i))) ? 0 : 1;
841 }
842
844 }
845
846 for (
i = 0;
i < nb_frames;
i++) {
847 for (j = 0; j < coded_channels && !
s->midonly; j++) {
848 int active1 = coded_channels > 1 ? active[1][
i] : 0;
850 }
851
852 /* reset the side channel if it is not coded */
853 if (
s->midonly &&
s->frame[1].coded)
855
856 if (coded_channels == 1 ||
s->output_channels == 1) {
857 for (j = 0; j <
s->output_channels; j++) {
860 s->flength *
sizeof(
float));
861 }
862 } else {
864 }
865
867 }
868
869 return nb_frames *
s->flength;
870 }
871
873 {
875 }
876
878 {
881
882 memset(
s->prev_stereo_weights, 0,
sizeof(
s->prev_stereo_weights));
883 }
884
886 {
888
889 if (output_channels != 1 && output_channels != 2) {
891 output_channels);
893 }
894
898
900 s->output_channels = output_channels;
901
903
905
906 return 0;
907 }