FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus_silk.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 401 436 92.0%
Functions: 14 14 100.0%
Branches: 269 316 85.1%

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