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