FFmpeg
Loading...
Searching...
No Matches
silk.c
Go to the documentation of this file.
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
29#include "libavutil/mem.h"
30
31#include "libavcodec/mathops.h"
32
33#include "opus.h"
34#include "rc.h"
35#include "silk.h"
36#include "tab.h"
37
38#define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
39
40typedef struct SilkFrame {
41 int coded;
43 int16_t nlsf[16];
44 float lpc[16];
45
46 float output [2 * SILK_HISTORY];
49
51} SilkFrame;
52
72
73static inline void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17])
74{
75 int pass, i;
76 for (pass = 0; pass < 20; pass++) {
77 int k, min_diff = 0;
78 for (i = 0; i < order+1; i++) {
79 int low = i != 0 ? nlsf[i-1] : 0;
80 int high = i != order ? nlsf[i] : 32768;
81 int diff = (high - low) - (min_delta[i]);
82
83 if (diff < min_diff) {
84 min_diff = diff;
85 k = i;
86
87 if (pass == 20)
88 break;
89 }
90 }
91 if (min_diff == 0) /* no issues; stabilized */
92 return;
93
94 /* wiggle one or two LSFs */
95 if (k == 0) {
96 /* repel away from lower bound */
97 nlsf[0] = min_delta[0];
98 } else if (k == order) {
99 /* repel away from higher bound */
100 nlsf[order-1] = 32768 - min_delta[order];
101 } else {
102 /* repel away from current position */
103 int min_center = 0, max_center = 32768, center_val;
104
105 /* lower extent */
106 for (i = 0; i < k; i++)
107 min_center += min_delta[i];
108 min_center += min_delta[k] >> 1;
109
110 /* upper extent */
111 for (i = order; i > k; i--)
112 max_center -= min_delta[i];
113 max_center -= min_delta[k] >> 1;
114
115 /* move apart */
116 center_val = nlsf[k - 1] + nlsf[k];
117 center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2
118 center_val = FFMIN(max_center, FFMAX(min_center, center_val));
119
120 nlsf[k - 1] = center_val - (min_delta[k] >> 1);
121 nlsf[k] = nlsf[k - 1] + min_delta[k];
122 }
123 }
124
125 /* resort to the fall-back method, the standard method for LSF stabilization */
126
127 /* sort; as the LSFs should be nearly sorted, use insertion sort */
128 for (i = 1; i < order; i++) {
129 int j, value = nlsf[i];
130 for (j = i - 1; j >= 0 && nlsf[j] > value; j--)
131 nlsf[j + 1] = nlsf[j];
132 nlsf[j + 1] = value;
133 }
134
135 /* push forwards to increase distance */
136 if (nlsf[0] < min_delta[0])
137 nlsf[0] = min_delta[0];
138 for (i = 1; i < order; i++)
139 nlsf[i] = FFMAX(nlsf[i], FFMIN(nlsf[i - 1] + min_delta[i], 32767));
140
141 /* push backwards to increase distance */
142 if (nlsf[order-1] > 32768 - min_delta[order])
143 nlsf[order-1] = 32768 - min_delta[order];
144 for (i = order-2; i >= 0; i--)
145 if (nlsf[i] > nlsf[i + 1] - min_delta[i+1])
146 nlsf[i] = nlsf[i + 1] - min_delta[i+1];
147
148 return;
149}
150
151static inline int silk_is_lpc_stable(const int16_t lpc[16], int order)
152{
153 int k, j, DC_resp = 0;
154 int32_t lpc32[2][16]; // Q24
155 int totalinvgain = 1 << 30; // 1.0 in Q30
156 int32_t *row = lpc32[0], *prevrow;
157
158 /* initialize the first row for the Levinson recursion */
159 for (k = 0; k < order; k++) {
160 DC_resp += lpc[k];
161 row[k] = lpc[k] * 4096;
162 }
163
164 if (DC_resp >= 4096)
165 return 0;
166
167 /* check if prediction gain pushes any coefficients too far */
168 for (k = order - 1; 1; k--) {
169 int rc; // Q31; reflection coefficient
170 int gaindiv; // Q30; inverse of the gain (the divisor)
171 int gain; // gain for this reflection coefficient
172 int fbits; // fractional bits used for the gain
173 int error; // Q29; estimate of the error of our partial estimate of 1/gaindiv
174
175 if (FFABS(row[k]) > 16773022)
176 return 0;
177
178 rc = -(row[k] * 128);
179 gaindiv = (1 << 30) - MULH(rc, rc);
180
181 totalinvgain = MULH(totalinvgain, gaindiv) << 2;
182 if (k == 0)
183 return (totalinvgain >= 107374);
184
185 /* approximate 1.0/gaindiv */
186 fbits = opus_ilog(gaindiv);
187 gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16>
188 error = (1 << 29) - MULL(gaindiv << (15 + 16 - fbits), gain, 16);
189 gain = ((gain << 16) + (error * gain >> 13));
190
191 /* switch to the next row of the LPC coefficients */
192 prevrow = row;
193 row = lpc32[k & 1];
194
195 for (j = 0; j < k; j++) {
196 int x = av_sat_sub32(prevrow[j], ROUND_MULL(prevrow[k - j - 1], rc, 31));
197 int64_t tmp = ROUND_MULL(x, gain, fbits);
198
199 /* per RFC 8251 section 6, if this calculation overflows, the filter
200 is considered unstable. */
201 if (tmp < INT32_MIN || tmp > INT32_MAX)
202 return 0;
203
204 row[j] = (int32_t)tmp;
205 }
206 }
207}
208
209static void silk_lsp2poly(const int32_t lsp[/* 2 * half_order - 1 */],
210 int32_t pol[/* half_order + 1 */], int half_order)
211{
212 int i, j;
213
214 pol[0] = 65536; // 1.0 in Q16
215 pol[1] = -lsp[0];
216
217 for (i = 1; i < half_order; i++) {
218 pol[i + 1] = pol[i - 1] * 2 - ROUND_MULL(lsp[2 * i], pol[i], 16);
219 for (j = i; j > 1; j--)
220 pol[j] += pol[j - 2] - ROUND_MULL(lsp[2 * i], pol[j - 1], 16);
221
222 pol[1] -= lsp[2 * i];
223 }
224}
225
226static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
227{
228 int i, k;
229 int32_t lsp[16]; // Q17; 2*cos(LSF)
230 int32_t p[9], q[9]; // Q16
231 int32_t lpc32[16]; // Q17
232 int16_t lpc[16]; // Q12
233
234 /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */
235 for (k = 0; k < order; k++) {
236 int index = nlsf[k] >> 8;
237 int offset = nlsf[k] & 255;
238 int k2 = (order == 10) ? ff_silk_lsf_ordering_nbmb[k] : ff_silk_lsf_ordering_wb[k];
239
240 /* interpolate and round */
241 lsp[k2] = ff_silk_cosine[index] * 256;
242 lsp[k2] += (ff_silk_cosine[index + 1] - ff_silk_cosine[index]) * offset;
243 lsp[k2] = (lsp[k2] + 4) >> 3;
244 }
245
246 silk_lsp2poly(lsp , p, order >> 1);
247 silk_lsp2poly(lsp + 1, q, order >> 1);
248
249 /* reconstruct A(z) */
250 for (k = 0; k < order>>1; k++) {
251 int32_t p_tmp = p[k + 1] + p[k];
252 int32_t q_tmp = q[k + 1] - q[k];
253 lpc32[k] = -q_tmp - p_tmp;
254 lpc32[order-k-1] = q_tmp - p_tmp;
255 }
256
257 /* limit the range of the LPC coefficients to each fit within an int16_t */
258 for (i = 0; i < 10; i++) {
259 int j;
260 unsigned int maxabs = 0;
261 for (j = 0, k = 0; j < order; j++) {
262 unsigned int x = FFABS(lpc32[k]);
263 if (x > maxabs) {
264 maxabs = x; // Q17
265 k = j;
266 }
267 }
268
269 maxabs = (maxabs + 16) >> 5; // convert to Q12
270
271 if (maxabs > 32767) {
272 /* perform bandwidth expansion */
273 unsigned int chirp, chirp_base; // Q16
274 maxabs = FFMIN(maxabs, 163838); // anything above this overflows chirp's numerator
275 chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);
276
277 for (k = 0; k < order; k++) {
278 lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);
279 chirp = (chirp_base * chirp + 32768) >> 16;
280 }
281 } else break;
282 }
283
284 if (i == 10) {
285 /* time's up: just clamp */
286 for (k = 0; k < order; k++) {
287 int x = (lpc32[k] + 16) >> 5;
288 lpc[k] = av_clip_int16(x);
289 lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits
290 }
291 } else {
292 for (k = 0; k < order; k++)
293 lpc[k] = (lpc32[k] + 16) >> 5;
294 }
295
296 /* if the prediction gain causes the LPC filter to become unstable,
297 apply further bandwidth expansion on the Q17 coefficients */
298 for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) {
299 unsigned int chirp, chirp_base;
300 chirp_base = chirp = 65536 - (1 << i);
301
302 for (k = 0; k < order; k++) {
303 lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);
304 lpc[k] = (lpc32[k] + 16) >> 5;
305 chirp = (chirp_base * chirp + 32768) >> 16;
306 }
307 }
308
309 for (i = 0; i < order; i++)
310 lpcf[i] = lpc[i] / 4096.0f;
311}
312
314 OpusRangeCoder *rc,
315 float lpc_leadin[16], float lpc[16],
316 int *lpc_order, int *has_lpc_leadin, int voiced)
317{
318 int i;
319 int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB
320 int8_t lsf_i1, lsf_i2[16]; // stage-1 and stage-2 codebook indices
321 int16_t lsf_res[16]; // residual as a Q10 value
322 int16_t nlsf[16]; // Q15
323
324 *lpc_order = order = s->wb ? 16 : 10;
325
326 /* obtain LSF stage-1 and stage-2 indices */
327 lsf_i1 = ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_s1[s->wb][voiced]);
328 for (i = 0; i < order; i++) {
329 int index = s->wb ? ff_silk_lsf_s2_model_sel_wb [lsf_i1][i] :
332 if (lsf_i2[i] == -4)
334 else if (lsf_i2[i] == 4)
336 }
337
338 /* reverse the backwards-prediction step */
339 for (i = order - 1; i >= 0; i--) {
340 int qstep = s->wb ? 9830 : 11796;
341
342 lsf_res[i] = lsf_i2[i] * 1024;
343 if (lsf_i2[i] < 0) lsf_res[i] += 102;
344 else if (lsf_i2[i] > 0) lsf_res[i] -= 102;
345 lsf_res[i] = (lsf_res[i] * qstep) >> 16;
346
347 if (i + 1 < order) {
350 lsf_res[i] += (lsf_res[i+1] * weight) >> 8;
351 }
352 }
353
354 /* reconstruct the NLSF coefficients from the supplied indices */
355 for (i = 0; i < order; i++) {
356 const uint8_t * codebook = s->wb ? ff_silk_lsf_codebook_wb [lsf_i1] :
358 int cur, weight, value;
359
360 /* find the weight of the residual */
361 cur = codebook[i];
362 weight = s->wb ? ff_silk_model_lsf_weight_wb[lsf_i1][i] :
364
365 value = cur * 128 + (lsf_res[i] * 16384) / weight;
366 nlsf[i] = av_clip_uintp2(value, 15);
367 }
368
369 /* stabilize the NLSF coefficients */
372
373 /* produce an interpolation for the first 2 subframes, */
374 /* and then convert both sets of NLSFs to LPC coefficients */
375 *has_lpc_leadin = 0;
376 if (s->subframes == 4) {
378 if (offset != 4 && frame->coded) {
379 *has_lpc_leadin = 1;
380 if (offset != 0) {
381 int16_t nlsf_leadin[16];
382 for (i = 0; i < order; i++)
383 nlsf_leadin[i] = frame->nlsf[i] +
384 ((nlsf[i] - frame->nlsf[i]) * offset >> 2);
385 silk_lsf2lpc(nlsf_leadin, lpc_leadin, order);
386 } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */
387 memcpy(lpc_leadin, frame->lpc, 16 * sizeof(float));
388 } else
389 offset = 4;
390 s->nlsf_interp_factor = offset;
391
392 silk_lsf2lpc(nlsf, lpc, order);
393 } else {
394 s->nlsf_interp_factor = 4;
395 silk_lsf2lpc(nlsf, lpc, order);
396 }
397
398 memcpy(frame->nlsf, nlsf, order * sizeof(nlsf[0]));
399 memcpy(frame->lpc, lpc, order * sizeof(lpc[0]));
400}
401
402static inline void silk_count_children(OpusRangeCoder *rc, int model, int32_t total,
403 int32_t child[2])
404{
405 if (total != 0) {
406 child[0] = ff_opus_rc_dec_cdf(rc,
407 ff_silk_model_pulse_location[model] + (((total - 1 + 5) * (total - 1)) >> 1));
408 child[1] = total - child[0];
409 } else {
410 child[0] = 0;
411 child[1] = 0;
412 }
413}
414
416 float* excitationf,
417 int qoffset_high, int active, int voiced)
418{
419 int i;
420 uint32_t seed;
421 int shellblocks;
422 int ratelevel;
423 uint8_t pulsecount[20]; // total pulses in each shell block
424 uint8_t lsbcount[20] = {0}; // raw lsbits defined for each pulse in each shell block
425 int32_t excitation[320]; // Q23
426
427 /* excitation parameters */
429 shellblocks = ff_silk_shell_blocks[s->bandwidth][s->subframes >> 2];
430 ratelevel = ff_opus_rc_dec_cdf(rc, ff_silk_model_exc_rate[voiced]);
431
432 for (i = 0; i < shellblocks; i++) {
433 pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[ratelevel]);
434 if (pulsecount[i] == 17) {
435 while (pulsecount[i] == 17 && ++lsbcount[i] != 10)
436 pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[9]);
437 if (lsbcount[i] == 10)
438 pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[10]);
439 }
440 }
441
442 /* decode pulse locations using PVQ */
443 for (i = 0; i < shellblocks; i++) {
444 if (pulsecount[i] != 0) {
445 int a, b, c, d;
446 int32_t * location = excitation + 16*i;
447 int32_t branch[4][2];
448 branch[0][0] = pulsecount[i];
449
450 /* unrolled tail recursion */
451 for (a = 0; a < 1; a++) {
452 silk_count_children(rc, 0, branch[0][a], branch[1]);
453 for (b = 0; b < 2; b++) {
454 silk_count_children(rc, 1, branch[1][b], branch[2]);
455 for (c = 0; c < 2; c++) {
456 silk_count_children(rc, 2, branch[2][c], branch[3]);
457 for (d = 0; d < 2; d++) {
458 silk_count_children(rc, 3, branch[3][d], location);
459 location += 2;
460 }
461 }
462 }
463 }
464 } else
465 memset(excitation + 16*i, 0, 16*sizeof(int32_t));
466 }
467
468 /* decode least significant bits */
469 for (i = 0; i < shellblocks << 4; i++) {
470 int bit;
471 for (bit = 0; bit < lsbcount[i >> 4]; bit++)
472 excitation[i] = (excitation[i] << 1) |
474 }
475
476 /* decode signs */
477 for (i = 0; i < shellblocks << 4; i++) {
478 if (excitation[i] != 0) {
480 voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)]);
481 if (sign == 0)
482 excitation[i] *= -1;
483 }
484 }
485
486 /* assemble the excitation */
487 for (i = 0; i < shellblocks << 4; i++) {
488 int value = excitation[i];
489 excitation[i] = value * 256 | ff_silk_quant_offset[voiced][qoffset_high];
490 if (value < 0) excitation[i] += 20;
491 else if (value > 0) excitation[i] -= 20;
492
493 /* invert samples pseudorandomly */
494 seed = 196314165 * seed + 907633515;
495 if (seed & 0x80000000)
496 excitation[i] *= -1;
497 seed += value;
498
499 excitationf[i] = excitation[i] / 8388608.0f;
500 }
501}
502
503/** Maximum residual history according to 4.2.7.6.1 */
504#define SILK_MAX_LAG (288 + LTP_ORDER / 2)
505
506/** Order of the LTP filter */
507#define LTP_ORDER 5
508
510 int frame_num, int channel, int coded_channels,
511 int active, int active1, int redundant)
512{
513 /* per frame */
514 int voiced; // combines with active to indicate inactive, active, or active+voiced
515 int qoffset_high;
516 int order; // order of the LPC coefficients
517 float lpc_leadin[16], lpc_body[16], residual[SILK_MAX_LAG + SILK_HISTORY];
518 int has_lpc_leadin;
519 float ltpscale;
520
521 /* per subframe */
522 struct {
523 float gain;
524 int pitchlag;
525 float ltptaps[5];
526 } sf[4];
527
528 SilkFrame * const frame = s->frame + channel;
529
530 int i;
531
532 /* obtain stereo weights */
533 if (coded_channels == 2 && channel == 0) {
534 int n, wi[2], ws[2], w[2];
536 wi[0] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s2) + 3 * (n / 5);
538 wi[1] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s2) + 3 * (n % 5);
540
541 for (i = 0; i < 2; i++)
542 w[i] = ff_silk_stereo_weights[wi[i]] +
543 (((ff_silk_stereo_weights[wi[i] + 1] - ff_silk_stereo_weights[wi[i]]) * 6554) >> 16)
544 * (ws[i]*2 + 1);
545
546 s->stereo_weights[0] = (w[0] - w[1]) / 8192.0;
547 s->stereo_weights[1] = w[1] / 8192.0;
548
549 /* and read the mid-only flag */
550 s->midonly = active1 ? 0 : ff_opus_rc_dec_cdf(rc, ff_silk_model_mid_only);
551 }
552
553 /* obtain frame type */
554 if (!active) {
556 voiced = 0;
557 } else {
559 qoffset_high = type & 1;
560 voiced = type >> 1;
561 }
562
563 /* obtain subframe quantization gains */
564 for (i = 0; i < s->subframes; i++) {
565 int log_gain; //Q7
566 int ipart, fpart, lingain;
567
568 if (i == 0 && (frame_num == 0 || !frame->coded)) {
569 /* gain is coded absolute */
570 int x = ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_highbits[active + voiced]);
571 log_gain = (x<<3) | ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_lowbits);
572
573 if (frame->coded)
574 log_gain = FFMAX(log_gain, frame->log_gain - 16);
575 } else {
576 /* gain is coded relative */
577 int delta_gain = ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_delta);
578 log_gain = av_clip_uintp2(FFMAX((delta_gain<<1) - 16,
579 frame->log_gain + delta_gain - 4), 6);
580 }
581
582 frame->log_gain = log_gain;
583
584 /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */
585 log_gain = (log_gain * 0x1D1C71 >> 16) + 2090;
586 ipart = log_gain >> 7;
587 fpart = log_gain & 127;
588 lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7);
589 sf[i].gain = lingain / 65536.0f;
590 }
591
592 /* obtain LPC filter coefficients */
593 silk_decode_lpc(s, frame, rc, lpc_leadin, lpc_body, &order, &has_lpc_leadin, voiced);
594
595 /* obtain pitch lags, if this is a voiced frame */
596 if (voiced) {
597 int lag_absolute = (!frame_num || !frame->prev_voiced);
598 int primarylag; // primary pitch lag for the entire SILK frame
599 int ltpfilter;
600 const int8_t * offsets;
601
602 if (!lag_absolute) {
604 if (delta)
605 primarylag = frame->primarylag + delta - 9;
606 else
607 lag_absolute = 1;
608 }
609
610 if (lag_absolute) {
611 /* primary lag is coded absolute */
612 int highbits, lowbits;
613 static const uint16_t * const model[] = {
616 };
618 lowbits = ff_opus_rc_dec_cdf(rc, model[s->bandwidth]);
619
620 primarylag = ff_silk_pitch_min_lag[s->bandwidth] +
621 highbits*ff_silk_pitch_scale[s->bandwidth] + lowbits;
622 }
623 frame->primarylag = primarylag;
624
625 if (s->subframes == 2)
626 offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
631 else
632 offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
637
638 for (i = 0; i < s->subframes; i++)
639 sf[i].pitchlag = av_clip(primarylag + offsets[i],
640 ff_silk_pitch_min_lag[s->bandwidth],
641 ff_silk_pitch_max_lag[s->bandwidth]);
642
643 /* obtain LTP filter coefficients */
645 for (i = 0; i < s->subframes; i++) {
646 int index, j;
647 static const uint16_t * const filter_sel[] = {
650 };
651 static const int8_t (* const filter_taps[])[5] = {
653 };
654 index = ff_opus_rc_dec_cdf(rc, filter_sel[ltpfilter]);
655 for (j = 0; j < 5; j++)
656 sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f;
657 }
658 }
659
660 /* obtain LTP scale factor */
661 if (voiced && frame_num == 0)
664 else ltpscale = 15565.0f/16384.0f;
665
666 /* generate the excitation signal for the entire frame */
667 silk_decode_excitation(s, rc, residual + SILK_MAX_LAG, qoffset_high,
668 active, voiced);
669
670 /* skip synthesising the output if we do not need it */
671 // TODO: implement error recovery
672 if (s->output_channels == channel || redundant)
673 return;
674
675 /* generate the output signal */
676 for (i = 0; i < s->subframes; i++) {
677 const float * lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body;
678 float *dst = frame->output + SILK_HISTORY + i * s->sflength;
679 float *resptr = residual + SILK_MAX_LAG + i * s->sflength;
680 float *lpc = frame->lpc_history + SILK_HISTORY + i * s->sflength;
681 float sum;
682 int j, k;
683
684 if (voiced) {
685 int out_end;
686 float scale;
687
688 if (i < 2 || s->nlsf_interp_factor == 4) {
689 out_end = -i * s->sflength;
690 scale = ltpscale;
691 } else {
692 out_end = -(i - 2) * s->sflength;
693 scale = 1.0f;
694 }
695
696 /* when the LPC coefficients change, a re-whitening filter is used */
697 /* to produce a residual that accounts for the change */
698 for (j = - sf[i].pitchlag - LTP_ORDER/2; j < out_end; j++) {
699 sum = dst[j];
700 for (k = 0; k < order; k++)
701 sum -= lpc_coeff[k] * dst[j - k - 1];
702 resptr[j] = av_clipf(sum, -1.0f, 1.0f) * scale / sf[i].gain;
703 }
704
705 if (out_end) {
706 float rescale = sf[i-1].gain / sf[i].gain;
707 for (j = out_end; j < 0; j++)
708 resptr[j] *= rescale;
709 }
710
711 /* LTP synthesis */
712 for (j = 0; j < s->sflength; j++) {
713 sum = resptr[j];
714 for (k = 0; k < LTP_ORDER; k++)
715 sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k];
716 resptr[j] = sum;
717 }
718 }
719
720 /* LPC synthesis */
721 for (j = 0; j < s->sflength; j++) {
722 sum = resptr[j] * sf[i].gain;
723 for (k = 1; k <= order; k++)
724 sum += lpc_coeff[k - 1] * lpc[j - k];
725
726 lpc[j] = sum;
727 dst[j] = av_clipf(sum, -1.0f, 1.0f);
728 }
729 }
730
731 frame->prev_voiced = voiced;
732 memmove(frame->lpc_history, frame->lpc_history + s->flength, SILK_HISTORY * sizeof(float));
733 memmove(frame->output, frame->output + s->flength, SILK_HISTORY * sizeof(float));
734
735 frame->coded = 1;
736}
737
738static void silk_unmix_ms(SilkContext *s, float *l, float *r)
739{
740 float *mid = s->frame[0].output + SILK_HISTORY - s->flength;
741 float *side = s->frame[1].output + SILK_HISTORY - s->flength;
742 float w0_prev = s->prev_stereo_weights[0];
743 float w1_prev = s->prev_stereo_weights[1];
744 float w0 = s->stereo_weights[0];
745 float w1 = s->stereo_weights[1];
746 int n1 = ff_silk_stereo_interp_len[s->bandwidth];
747 int i;
748
749 for (i = 0; i < n1; i++) {
750 float interp0 = w0_prev + i * (w0 - w0_prev) / n1;
751 float interp1 = w1_prev + i * (w1 - w1_prev) / n1;
752 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
753
754 l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0);
755 r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0);
756 }
757
758 for (; i < s->flength; i++) {
759 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
760
761 l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0);
762 r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0);
763 }
764
765 memcpy(s->prev_stereo_weights, s->stereo_weights, sizeof(s->stereo_weights));
766}
767
769{
770 if (!frame->coded)
771 return;
772
773 memset(frame->output, 0, sizeof(frame->output));
774 memset(frame->lpc_history, 0, sizeof(frame->lpc_history));
775
776 memset(frame->lpc, 0, sizeof(frame->lpc));
777 memset(frame->nlsf, 0, sizeof(frame->nlsf));
778
779 frame->log_gain = 0;
780
781 frame->primarylag = 0;
782 frame->prev_voiced = 0;
783 frame->coded = 0;
784}
785
787 float *output[2],
788 enum OpusBandwidth bandwidth,
789 int coded_channels,
790 int duration_ms)
791{
792 int active[2][6], redundancy[2];
793 int nb_frames, i, j;
794
795 if (bandwidth > OPUS_BANDWIDTH_WIDEBAND ||
796 coded_channels > 2 || duration_ms > 60) {
797 av_log(s->logctx, AV_LOG_ERROR, "Invalid parameters passed "
798 "to the SILK decoder.\n");
799 return AVERROR(EINVAL);
800 }
801
802 nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40);
803 s->subframes = duration_ms / nb_frames / 5; // 5ms subframes
804 s->sflength = 20 * (bandwidth + 2);
805 s->flength = s->sflength * s->subframes;
806 s->bandwidth = bandwidth;
807 s->wb = bandwidth == OPUS_BANDWIDTH_WIDEBAND;
808
809 /* make sure to flush the side channel when switching from mono to stereo */
810 if (coded_channels > s->prev_coded_channels)
811 silk_flush_frame(&s->frame[1]);
812 s->prev_coded_channels = coded_channels;
813
814 /* read the LP-layer header bits */
815 for (i = 0; i < coded_channels; i++) {
816 for (j = 0; j < nb_frames; j++)
817 active[i][j] = ff_opus_rc_dec_log(rc, 1);
818
819 redundancy[i] = ff_opus_rc_dec_log(rc, 1);
820 }
821
822 /* read the per-frame LBRR flags */
823 for (i = 0; i < coded_channels; i++)
824 if (redundancy[i] && duration_ms > 20) {
825 redundancy[i] = ff_opus_rc_dec_cdf(rc, duration_ms == 40 ?
827 }
828
829 /* decode the LBRR frames */
830 for (i = 0; i < nb_frames; i++) {
831 for (j = 0; j < coded_channels; j++)
832 if (redundancy[j] & (1 << i)) {
833 int active1 = (j == 0 && !(redundancy[1] & (1 << i))) ? 0 : 1;
834 silk_decode_frame(s, rc, i, j, coded_channels, 1, active1, 1);
835 }
836
837 s->midonly = 0;
838 }
839
840 for (i = 0; i < nb_frames; i++) {
841 for (j = 0; j < coded_channels && !s->midonly; j++) {
842 int active1 = coded_channels > 1 ? active[1][i] : 0;
843 silk_decode_frame(s, rc, i, j, coded_channels, active[j][i], active1, 0);
844 }
845
846 /* reset the side channel if it is not coded */
847 if (s->midonly && s->frame[1].coded)
848 silk_flush_frame(&s->frame[1]);
849
850 if (coded_channels == 1 || s->output_channels == 1) {
851 for (j = 0; j < s->output_channels; j++) {
852 memcpy(output[j] + i * s->flength,
853 s->frame[0].output + SILK_HISTORY - s->flength - 2,
854 s->flength * sizeof(float));
855 }
856 } else {
857 silk_unmix_ms(s, output[0] + i * s->flength, output[1] + i * s->flength);
858 }
859
860 s->midonly = 0;
861 }
862
863 return nb_frames * s->flength;
864}
865
867{
868 av_freep(ps);
869}
870
872{
873 silk_flush_frame(&s->frame[0]);
874 silk_flush_frame(&s->frame[1]);
875
876 memset(s->prev_stereo_weights, 0, sizeof(s->prev_stereo_weights));
877}
878
879int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
880{
881 SilkContext *s;
882
883 if (output_channels != 1 && output_channels != 2) {
884 av_log(logctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
885 output_channels);
886 return AVERROR(EINVAL);
887 }
888
889 s = av_mallocz(sizeof(*s));
890 if (!s)
891 return AVERROR(ENOMEM);
892
893 s->logctx = logctx;
894 s->output_channels = output_channels;
895
897
898 *ps = s;
899
900 return 0;
901}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
int32_t
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
static const unsigned codebook[256][2]
Definition cfhdenc.c:41
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
#define av_clip_uintp2
Definition common.h:124
#define av_clipf
Definition common.h:145
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define av_sat_sub32
Definition common.h:133
long long int64_t
Definition coverity.c:34
static AVFrame * frame
int high
Definition dovi_rpuenc.c:39
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
double value
Definition eval.c:102
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int index
Definition gxfenc.c:90
int a
static const int offsets[]
Definition hevc_pel.c:34
cl_device_type type
#define r
Definition input.c:42
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
unsigned offset
Definition libaomenc.c:763
static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
Definition gdv.c:133
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define MULL(a, b, s)
Definition mathops.h:60
Memory handling functions.
OpusBandwidth
Definition opus.h:49
@ OPUS_BANDWIDTH_NARROWBAND
Definition opus.h:50
@ OPUS_BANDWIDTH_WIDEBAND
Definition opus.h:52
#define MULH
Definition mathops.h:42
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition rc.c:114
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
Definition rc.c:90
#define opus_ilog(i)
Definition rc.h:32
const h264_weight_func weight
static void silk_decode_lpc(SilkContext *s, SilkFrame *frame, OpusRangeCoder *rc, float lpc_leadin[16], float lpc[16], int *lpc_order, int *has_lpc_leadin, int voiced)
Definition silk.c:313
#define ROUND_MULL(a, b, s)
Definition silk.c:38
#define LTP_ORDER
Order of the LTP filter.
Definition silk.c:507
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition silk.c:786
static void silk_flush_frame(SilkFrame *frame)
Definition silk.c:768
#define SILK_MAX_LAG
Maximum residual history according to 4.2.7.6.1.
Definition silk.c:504
static void silk_unmix_ms(SilkContext *s, float *l, float *r)
Definition silk.c:738
static void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17])
Definition silk.c:73
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
Definition silk.c:879
void ff_silk_flush(SilkContext *s)
Definition silk.c:871
static void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, float *excitationf, int qoffset_high, int active, int voiced)
Definition silk.c:415
void ff_silk_free(SilkContext **ps)
Definition silk.c:866
static void silk_lsp2poly(const int32_t lsp[], int32_t pol[], int half_order)
Definition silk.c:209
static void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, int32_t child[2])
Definition silk.c:402
static int silk_is_lpc_stable(const int16_t lpc[16], int order)
Definition silk.c:151
static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, int frame_num, int channel, int coded_channels, int active, int active1, int redundant)
Definition silk.c:509
static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
Definition silk.c:226
#define SILK_HISTORY
Definition silk.h:29
int output_channels
Definition silk.c:55
int midonly
Definition silk.c:57
float prev_stereo_weights[2]
Definition silk.c:67
SilkFrame frame[2]
Definition silk.c:66
int subframes
Definition silk.c:58
int nlsf_interp_factor
Definition silk.c:61
enum OpusBandwidth bandwidth
Definition silk.c:63
int wb
Definition silk.c:64
int flength
Definition silk.c:60
void * logctx
Definition silk.c:54
int sflength
Definition silk.c:59
float stereo_weights[2]
Definition silk.c:68
int prev_coded_channels
Definition silk.c:70
int16_t nlsf[16]
Definition silk.c:43
int primarylag
Definition silk.c:48
int coded
Definition silk.c:41
float lpc_history[2 *SILK_HISTORY]
Definition silk.c:47
float lpc[16]
Definition silk.c:44
int prev_voiced
Definition silk.c:50
int log_gain
Definition silk.c:42
float output[2 *SILK_HISTORY]
Definition silk.c:46
const uint8_t ff_silk_lsf_weight_sel_nbmb[32][9]
Definition tab.c:402
const uint16_t ff_silk_model_stereo_s3[]
Definition tab.c:41
const uint16_t ff_silk_model_excitation_sign[3][2][7][3]
Definition tab.c:259
const uint8_t ff_silk_lsf_s2_model_sel_wb[32][16]
Definition tab.c:357
const uint16_t ff_silk_model_ltp_filter[]
Definition tab.c:136
const uint16_t ff_silk_lsf_min_spacing_wb[]
Definition tab.c:546
const uint16_t ff_silk_pitch_scale[]
Definition tab.c:594
const uint16_t ff_silk_model_pitch_contour_nb10ms[]
Definition tab.c:120
const uint16_t ff_silk_ltp_scale_factor[]
Definition tab.c:807
const uint16_t ff_silk_model_stereo_s1[]
Definition tab.c:34
const int16_t ff_silk_cosine[]
Definition tab.c:558
const uint8_t ff_silk_lsf_codebook_nbmb[32][10]
Definition tab.c:472
const uint16_t ff_silk_model_lsf_interpolation_offset[]
Definition tab.c:106
const uint16_t ff_silk_model_pitch_lowbits_mb[]
Definition tab.c:113
const uint16_t ff_silk_model_lbrr_flags_60[]
Definition tab.c:32
const uint8_t ff_silk_lsf_pred_weights_wb[2][15]
Definition tab.c:397
const uint8_t ff_silk_lsf_codebook_wb[32][16]
Definition tab.c:507
const uint16_t ff_silk_model_mid_only[]
Definition tab.c:43
const uint16_t ff_silk_model_ltp_filter2_sel[]
Definition tab.c:146
const uint16_t ff_silk_pitch_max_lag[]
Definition tab.c:598
const uint8_t ff_silk_lsf_weight_sel_wb[32][15]
Definition tab.c:437
const uint16_t ff_silk_model_stereo_s2[]
Definition tab.c:39
const uint8_t ff_silk_lsf_ordering_nbmb[]
Definition tab.c:550
const int8_t ff_silk_pitch_offset_mbwb10ms[12][2]
Definition tab.c:620
const uint16_t ff_silk_lsf_min_spacing_nbmb[]
Definition tab.c:542
const uint16_t ff_silk_model_lsf_weight_nbmb[32][10]
Definition tab.c:737
const uint16_t ff_silk_model_pulse_location[4][168]
Definition tab.c:185
const uint8_t ff_silk_lsf_pred_weights_nbmb[2][9]
Definition tab.c:392
const uint8_t ff_silk_lsf_ordering_wb[]
Definition tab.c:554
const uint16_t ff_silk_model_lbrr_flags_40[]
Definition tab.c:31
const uint16_t ff_silk_model_gain_delta[]
Definition tab.c:57
const uint16_t ff_silk_model_gain_lowbits[]
Definition tab.c:55
const uint16_t ff_silk_model_ltp_filter1_sel[]
Definition tab.c:142
const uint16_t ff_silk_model_pitch_contour_mbwb10ms[]
Definition tab.c:126
const uint16_t ff_silk_model_lsf_weight_wb[32][16]
Definition tab.c:772
const uint16_t ff_silk_model_pitch_contour_mbwb20ms[]
Definition tab.c:130
const int8_t ff_silk_pitch_offset_nb20ms[11][4]
Definition tab.c:606
const uint16_t ff_silk_model_frame_type_inactive[]
Definition tab.c:45
const int ff_silk_stereo_interp_len[3]
Definition tab.c:820
const uint16_t ff_silk_model_excitation_lsb[]
Definition tab.c:257
const int8_t ff_silk_ltp_filter1_taps[16][5]
Definition tab.c:683
const int8_t ff_silk_ltp_filter2_taps[32][5]
Definition tab.c:702
const uint16_t ff_silk_model_frame_type_active[]
Definition tab.c:47
const uint8_t ff_silk_shell_blocks[3][2]
Definition tab.c:809
const uint8_t ff_silk_lsf_s2_model_sel_nbmb[32][10]
Definition tab.c:322
const uint8_t ff_silk_quant_offset[2][2]
Definition tab.c:815
const uint16_t ff_silk_model_pulse_count[11][19]
Definition tab.c:160
const uint16_t ff_silk_pitch_min_lag[]
Definition tab.c:596
const uint16_t ff_silk_model_gain_highbits[3][9]
Definition tab.c:49
const uint16_t ff_silk_model_pitch_delta[]
Definition tab.c:115
const uint16_t ff_silk_model_lsf_s2[32][10]
Definition tab.c:82
const uint16_t ff_silk_model_ltp_filter0_sel[]
Definition tab.c:138
const int8_t ff_silk_pitch_offset_nb10ms[3][2]
Definition tab.c:600
const uint16_t ff_silk_model_lsf_s2_ext[]
Definition tab.c:104
const uint16_t ff_silk_model_lcg_seed[]
Definition tab.c:153
const int16_t ff_silk_stereo_weights[]
Definition tab.c:317
const int8_t ff_silk_pitch_offset_mbwb20ms[34][4]
Definition tab.c:635
const uint16_t ff_silk_model_pitch_highbits[]
Definition tab.c:108
const uint16_t ff_silk_model_exc_rate[2][10]
Definition tab.c:155
const uint16_t ff_silk_model_pitch_contour_nb20ms[]
Definition tab.c:122
const uint16_t ff_silk_model_lsf_s1[2][2][33]
Definition tab.c:62
const int8_t ff_silk_ltp_filter0_taps[8][5]
Definition tab.c:672
const uint16_t ff_silk_model_ltp_scale_index[]
Definition tab.c:151
#define ff_silk_model_pitch_lowbits_nb
Definition tab.h:58
#define ff_silk_model_pitch_lowbits_wb
Definition tab.h:60
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static unsigned int seed
Definition videogen.c:78
float delta
static double c[64]