FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opusdec_celt.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 295 317 93.1%
Functions: 13 13 100.0%
Branches: 192 210 91.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
4 * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Opus CELT decoder
26 */
27
28 #include <float.h>
29
30 #include "opus_celt.h"
31 #include "opustab.h"
32 #include "opus_pvq.h"
33
34 /* Use the 2D z-transform to apply prediction in both the time domain (alpha)
35 * and the frequency domain (beta) */
36 29947 static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc)
37 {
38 int i, j;
39 29947 float prev[2] = { 0 };
40 29947 float alpha = ff_celt_alpha_coef[f->size];
41 29947 float beta = ff_celt_beta_coef[f->size];
42 29947 const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0];
43
44 /* intra frame */
45
4/4
✓ Branch 1 taken 29821 times.
✓ Branch 2 taken 126 times.
✓ Branch 4 taken 5882 times.
✓ Branch 5 taken 23939 times.
29947 if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) {
46 5882 alpha = 0.0f;
47 5882 beta = 1.0f - (4915.0f/32768.0f);
48 5882 model = ff_celt_coarse_energy_dist[f->size][1];
49 }
50
51
2/2
✓ Branch 0 taken 628887 times.
✓ Branch 1 taken 29947 times.
658834 for (i = 0; i < CELT_MAX_BANDS; i++) {
52
2/2
✓ Branch 0 taken 1023645 times.
✓ Branch 1 taken 628887 times.
1652532 for (j = 0; j < f->channels; j++) {
53 1023645 CeltBlock *block = &f->block[j];
54 float value;
55 int available;
56
57
4/4
✓ Branch 0 taken 898049 times.
✓ Branch 1 taken 125596 times.
✓ Branch 2 taken 47336 times.
✓ Branch 3 taken 850713 times.
1023645 if (i < f->start_band || i >= f->end_band) {
58 172932 block->energy[i] = 0.0;
59 172932 continue;
60 }
61
62 850713 available = f->framebits - opus_rc_tell(rc);
63
2/2
✓ Branch 0 taken 846877 times.
✓ Branch 1 taken 3836 times.
850713 if (available >= 15) {
64 /* decode using a Laplace distribution */
65 846877 int k = FFMIN(i, 20) << 1;
66 846877 value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
67
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 3723 times.
3836 } else if (available >= 2) {
68 113 int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small);
69 113 value = (x>>1) ^ -(x&1);
70
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3720 times.
3723 } else if (available >= 1) {
71 3 value = -(float)ff_opus_rc_dec_log(rc, 1);
72 3720 } else value = -1;
73
74
2/2
✓ Branch 0 taken 119050 times.
✓ Branch 1 taken 731663 times.
850713 block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value;
75 850713 prev[j] += beta * value;
76 }
77 }
78 29947 }
79
80 29947 static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc)
81 {
82 int i;
83
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
84 int j;
85
2/2
✓ Branch 0 taken 95389 times.
✓ Branch 1 taken 413245 times.
508634 if (!f->fine_bits[i])
86 95389 continue;
87
88
2/2
✓ Branch 0 taken 682217 times.
✓ Branch 1 taken 413245 times.
1095462 for (j = 0; j < f->channels; j++) {
89 682217 CeltBlock *block = &f->block[j];
90 int q2;
91 float offset;
92 682217 q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]);
93 682217 offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f;
94 682217 block->energy[i] += offset;
95 }
96 }
97 29947 }
98
99 29947 static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc)
100 {
101 int priority, i, j;
102 29947 int bits_left = f->framebits - opus_rc_tell(rc);
103
104
2/2
✓ Branch 0 taken 59894 times.
✓ Branch 1 taken 29947 times.
89841 for (priority = 0; priority < 2; priority++) {
105
4/4
✓ Branch 0 taken 156577 times.
✓ Branch 1 taken 6226 times.
✓ Branch 2 taken 102909 times.
✓ Branch 3 taken 53668 times.
162803 for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) {
106
4/4
✓ Branch 0 taken 55115 times.
✓ Branch 1 taken 47794 times.
✓ Branch 2 taken 17685 times.
✓ Branch 3 taken 37430 times.
102909 if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
107 65479 continue;
108
109
2/2
✓ Branch 0 taken 58976 times.
✓ Branch 1 taken 37430 times.
96406 for (j = 0; j < f->channels; j++) {
110 int q2;
111 float offset;
112 58976 q2 = ff_opus_rc_get_raw(rc, 1);
113 58976 offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
114 58976 f->block[j].energy[i] += offset;
115 58976 bits_left--;
116 }
117 }
118 }
119 29947 }
120
121 29947 static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc)
122 {
123 29947 int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
124
2/2
✓ Branch 0 taken 4044 times.
✓ Branch 1 taken 25903 times.
29947 int consumed, bits = f->transient ? 2 : 4;
125
126 29947 consumed = opus_rc_tell(rc);
127
4/4
✓ Branch 0 taken 16241 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16144 times.
✓ Branch 3 taken 97 times.
29947 tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits);
128
129
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
130
2/2
✓ Branch 0 taken 506137 times.
✓ Branch 1 taken 2497 times.
508634 if (consumed+bits+tf_select_bit <= f->framebits) {
131 506137 diff ^= ff_opus_rc_dec_log(rc, bits);
132 506137 consumed = opus_rc_tell(rc);
133 506137 tf_changed |= diff;
134 }
135 508634 f->tf_change[i] = diff;
136
2/2
✓ Branch 0 taken 60533 times.
✓ Branch 1 taken 448101 times.
508634 bits = f->transient ? 4 : 5;
137 }
138
139
2/2
✓ Branch 0 taken 16144 times.
✓ Branch 1 taken 13803 times.
29947 if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
140
2/2
✓ Branch 0 taken 8124 times.
✓ Branch 1 taken 8020 times.
16144 ff_celt_tf_select[f->size][f->transient][1][tf_changed])
141 8124 tf_select = ff_opus_rc_dec_log(rc, 1);
142
143
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
144 508634 f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
145 }
146 29947 }
147
148 48745 static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
149 {
150 int i, j;
151
152
2/2
✓ Branch 0 taken 850713 times.
✓ Branch 1 taken 48745 times.
899458 for (i = f->start_band; i < f->end_band; i++) {
153 850713 float *dst = data + (ff_celt_freq_bands[i] << f->size);
154 850713 float log_norm = block->energy[i] + ff_celt_mean_energy[i];
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 850713 times.
850713 float norm = exp2f(FFMIN(log_norm, 32.0f));
156
157
2/2
✓ Branch 0 taken 12349060 times.
✓ Branch 1 taken 850713 times.
13199773 for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
158 12349060 dst[j] *= norm;
159 }
160 48745 }
161
162 89302 static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
163 {
164 89302 const int T0 = block->pf_period_old;
165 89302 const int T1 = block->pf_period;
166
167 float g00, g01, g02;
168 float g10, g11, g12;
169
170 float x0, x1, x2, x3, x4;
171
172 int i;
173
174
2/2
✓ Branch 0 taken 41858 times.
✓ Branch 1 taken 47444 times.
89302 if (block->pf_gains[0] == 0.0 &&
175
2/2
✓ Branch 0 taken 37678 times.
✓ Branch 1 taken 4180 times.
41858 block->pf_gains_old[0] == 0.0)
176 37678 return;
177
178 51624 g00 = block->pf_gains_old[0];
179 51624 g01 = block->pf_gains_old[1];
180 51624 g02 = block->pf_gains_old[2];
181 51624 g10 = block->pf_gains[0];
182 51624 g11 = block->pf_gains[1];
183 51624 g12 = block->pf_gains[2];
184
185 51624 x1 = data[-T1 + 1];
186 51624 x2 = data[-T1];
187 51624 x3 = data[-T1 - 1];
188 51624 x4 = data[-T1 - 2];
189
190
2/2
✓ Branch 0 taken 6194880 times.
✓ Branch 1 taken 51624 times.
6246504 for (i = 0; i < CELT_OVERLAP; i++) {
191 6194880 float w = ff_celt_window2[i];
192 6194880 x0 = data[i - T1 + 2];
193
194 6194880 data[i] += (1.0 - w) * g00 * data[i - T0] +
195 6194880 (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
196 6194880 (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
197 6194880 w * g10 * x2 +
198 6194880 w * g11 * (x1 + x3) +
199 6194880 w * g12 * (x0 + x4);
200 6194880 x4 = x3;
201 6194880 x3 = x2;
202 6194880 x2 = x1;
203 6194880 x1 = x0;
204 }
205 }
206
207 56826 static void celt_postfilter(CeltFrame *f, CeltBlock *block)
208 {
209 56826 int len = f->blocksize * f->blocks;
210 56826 const int filter_len = len - 2 * CELT_OVERLAP;
211
212 56826 celt_postfilter_apply_transition(block, block->buf + 1024);
213
214 56826 block->pf_period_old = block->pf_period;
215 56826 memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
216
217 56826 block->pf_period = block->pf_period_new;
218 56826 memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
219
220
2/2
✓ Branch 0 taken 32476 times.
✓ Branch 1 taken 24350 times.
56826 if (len > CELT_OVERLAP) {
221 32476 celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
222
223
4/4
✓ Branch 0 taken 16740 times.
✓ Branch 1 taken 15736 times.
✓ Branch 2 taken 10126 times.
✓ Branch 3 taken 6614 times.
32476 if (block->pf_gains[0] > FLT_EPSILON && filter_len > 0)
224 10126 f->opusdsp.postfilter(block->buf + 1024 + 2 * CELT_OVERLAP,
225 10126 block->pf_period, block->pf_gains,
226 filter_len);
227
228 32476 block->pf_period_old = block->pf_period;
229 32476 memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
230 }
231
232 56826 memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
233 56826 }
234
235 29947 static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
236 {
237 int i;
238
239 29947 memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
240 29947 memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
241
242
4/4
✓ Branch 0 taken 24958 times.
✓ Branch 1 taken 4989 times.
✓ Branch 2 taken 24832 times.
✓ Branch 3 taken 126 times.
29947 if (f->start_band == 0 && consumed + 16 <= f->framebits) {
243 24832 int has_postfilter = ff_opus_rc_dec_log(rc, 1);
244
2/2
✓ Branch 0 taken 15874 times.
✓ Branch 1 taken 8958 times.
24832 if (has_postfilter) {
245 float gain;
246 int tapset, octave, period;
247
248 15874 octave = ff_opus_rc_dec_uint(rc, 6);
249 15874 period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
250 15874 gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
251 15874 tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
252
1/2
✓ Branch 0 taken 15874 times.
✗ Branch 1 not taken.
15874 ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
253
254
2/2
✓ Branch 0 taken 31748 times.
✓ Branch 1 taken 15874 times.
47622 for (i = 0; i < 2; i++) {
255 31748 CeltBlock *block = &f->block[i];
256
257 31748 block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
258 31748 block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
259 31748 block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
260 31748 block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
261 }
262 }
263
264 24832 consumed = opus_rc_tell(rc);
265 }
266
267 29947 return consumed;
268 }
269
270 2793 static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
271 {
272 int i, j, k;
273
274
2/2
✓ Branch 0 taken 35536 times.
✓ Branch 1 taken 2793 times.
38329 for (i = f->start_band; i < f->end_band; i++) {
275 35536 int renormalize = 0;
276 float *xptr;
277 float prev[2];
278 float Ediff, r;
279 float thresh, sqrt_1;
280 int depth;
281
282 /* depth in 1/8 bits */
283 35536 depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
284 35536 thresh = exp2f(-1.0 - 0.125f * depth);
285 35536 sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
286
287 35536 xptr = X + (ff_celt_freq_bands[i] << f->size);
288
289 35536 prev[0] = block->prev_energy[0][i];
290 35536 prev[1] = block->prev_energy[1][i];
291
2/2
✓ Branch 0 taken 10086 times.
✓ Branch 1 taken 25450 times.
35536 if (f->channels == 1) {
292 10086 CeltBlock *block1 = &f->block[1];
293
294
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10072 times.
10086 prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
295
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 9949 times.
10086 prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
296 }
297
2/2
✓ Branch 0 taken 16490 times.
✓ Branch 1 taken 19046 times.
35536 Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
298
2/2
✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 32784 times.
35536 Ediff = FFMAX(0, Ediff);
299
300 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
301 short blocks don't have the same energy as long */
302 35536 r = exp2f(1 - Ediff);
303
2/2
✓ Branch 0 taken 23975 times.
✓ Branch 1 taken 11561 times.
35536 if (f->size == 3)
304 23975 r *= M_SQRT2;
305
2/2
✓ Branch 0 taken 5841 times.
✓ Branch 1 taken 29695 times.
35536 r = FFMIN(thresh, r) * sqrt_1;
306
2/2
✓ Branch 0 taken 238044 times.
✓ Branch 1 taken 35536 times.
273580 for (k = 0; k < 1 << f->size; k++) {
307 /* Detect collapse */
308
2/2
✓ Branch 0 taken 20731 times.
✓ Branch 1 taken 217313 times.
238044 if (!(block->collapse_masks[i] & 1 << k)) {
309 /* Fill with noise */
310
2/2
✓ Branch 0 taken 100329 times.
✓ Branch 1 taken 20731 times.
121060 for (j = 0; j < ff_celt_freq_range[i]; j++)
311
2/2
✓ Branch 1 taken 50064 times.
✓ Branch 2 taken 50265 times.
100329 xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
312 20731 renormalize = 1;
313 }
314 }
315
316 /* We just added some energy, so we need to renormalize */
317
2/2
✓ Branch 0 taken 8841 times.
✓ Branch 1 taken 26695 times.
35536 if (renormalize)
318 8841 celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
319 }
320 2793 }
321
322 29947 int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc,
323 float **output, int channels, int frame_size,
324 int start_band, int end_band)
325 {
326 29947 int i, j, downmix = 0;
327 int consumed; // bits of entropy consumed thus far for this frame
328 AVTXContext *imdct;
329 av_tx_fn imdct_fn;
330
331
3/4
✓ Branch 0 taken 18798 times.
✓ Branch 1 taken 11149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18798 times.
29947 if (channels != 1 && channels != 2) {
332 av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
333 channels);
334 return AVERROR_INVALIDDATA;
335 }
336
3/6
✓ Branch 0 taken 29947 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29947 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29947 times.
29947 if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
337 av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
338 start_band, end_band);
339 return AVERROR_INVALIDDATA;
340 }
341
342 29947 f->silence = 0;
343 29947 f->transient = 0;
344 29947 f->anticollapse = 0;
345 29947 f->flushed = 0;
346 29947 f->channels = channels;
347 29947 f->start_band = start_band;
348 29947 f->end_band = end_band;
349 29947 f->framebits = rc->rb.bytes * 8;
350
351 29947 f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
352
1/2
✓ Branch 0 taken 29947 times.
✗ Branch 1 not taken.
29947 if (f->size > CELT_MAX_LOG_BLOCKS ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29947 times.
29947 frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
354 av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
355 frame_size);
356 return AVERROR_INVALIDDATA;
357 }
358
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29947 times.
29947 if (!f->output_channels)
360 f->output_channels = channels;
361
362
2/2
✓ Branch 0 taken 48745 times.
✓ Branch 1 taken 29947 times.
78692 for (i = 0; i < f->channels; i++) {
363 48745 memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs));
364 48745 memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
365 }
366
367 29947 consumed = opus_rc_tell(rc);
368
369 /* obtain silence flag */
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29947 times.
29947 if (consumed >= f->framebits)
371 f->silence = 1;
372
2/2
✓ Branch 0 taken 24958 times.
✓ Branch 1 taken 4989 times.
29947 else if (consumed == 1)
373 24958 f->silence = ff_opus_rc_dec_log(rc, 15);
374
375
376
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 29821 times.
29947 if (f->silence) {
377 126 consumed = f->framebits;
378 126 rc->total_bits += f->framebits - opus_rc_tell(rc);
379 }
380
381 /* obtain post-filter options */
382 29947 consumed = parse_postfilter(f, rc, consumed);
383
384 /* obtain transient flag */
385
4/4
✓ Branch 0 taken 16241 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16152 times.
✓ Branch 3 taken 89 times.
29947 if (f->size != 0 && consumed+3 <= f->framebits)
386 16152 f->transient = ff_opus_rc_dec_log(rc, 3);
387
388
2/2
✓ Branch 0 taken 4044 times.
✓ Branch 1 taken 25903 times.
29947 f->blocks = f->transient ? 1 << f->size : 1;
389 29947 f->blocksize = frame_size / f->blocks;
390
391
2/2
✓ Branch 0 taken 25903 times.
✓ Branch 1 taken 4044 times.
29947 imdct = f->tx[f->transient ? 0 : f->size];
392
2/2
✓ Branch 0 taken 25903 times.
✓ Branch 1 taken 4044 times.
29947 imdct_fn = f->tx_fn[f->transient ? 0 : f->size];
393
394
2/2
✓ Branch 0 taken 11149 times.
✓ Branch 1 taken 18798 times.
29947 if (channels == 1) {
395
2/2
✓ Branch 0 taken 234129 times.
✓ Branch 1 taken 11149 times.
245278 for (i = 0; i < CELT_MAX_BANDS; i++)
396
2/2
✓ Branch 0 taken 393 times.
✓ Branch 1 taken 233736 times.
234129 f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
397 }
398
399 29947 celt_decode_coarse_energy(f, rc);
400 29947 celt_decode_tf_changes (f, rc);
401 29947 ff_celt_bitalloc (f, rc, 0);
402 29947 celt_decode_fine_energy (f, rc);
403 29947 ff_celt_quant_bands (f, rc);
404
405
2/2
✓ Branch 0 taken 3158 times.
✓ Branch 1 taken 26789 times.
29947 if (f->anticollapse_needed)
406 3158 f->anticollapse = ff_opus_rc_get_raw(rc, 1);
407
408 29947 celt_decode_final_energy(f, rc);
409
410 /* apply anti-collapse processing and denormalization to
411 * each coded channel */
412
2/2
✓ Branch 0 taken 48745 times.
✓ Branch 1 taken 29947 times.
78692 for (i = 0; i < f->channels; i++) {
413 48745 CeltBlock *block = &f->block[i];
414
415
2/2
✓ Branch 0 taken 2793 times.
✓ Branch 1 taken 45952 times.
48745 if (f->anticollapse)
416 2793 process_anticollapse(f, block, f->block[i].coeffs);
417
418 48745 celt_denormalize(f, block, f->block[i].coeffs);
419 }
420
421 /* stereo -> mono downmix */
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29947 times.
29947 if (f->output_channels < f->channels) {
423 f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
424 downmix = 1;
425
2/2
✓ Branch 0 taken 8081 times.
✓ Branch 1 taken 21866 times.
29947 } else if (f->output_channels > f->channels)
426 8081 memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
427
428
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 29821 times.
29947 if (f->silence) {
429
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 126 times.
378 for (i = 0; i < 2; i++) {
430 252 CeltBlock *block = &f->block[i];
431
432
2/2
✓ Branch 0 taken 5292 times.
✓ Branch 1 taken 252 times.
5544 for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
433 5292 block->energy[j] = CELT_ENERGY_SILENCE;
434 }
435 126 memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
436 126 memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
437 }
438
439 /* transform and output for each output channel */
440
2/2
✓ Branch 0 taken 56826 times.
✓ Branch 1 taken 29947 times.
86773 for (i = 0; i < f->output_channels; i++) {
441 56826 CeltBlock *block = &f->block[i];
442
443 /* iMDCT and overlap-add */
444
2/2
✓ Branch 0 taken 93066 times.
✓ Branch 1 taken 56826 times.
149892 for (j = 0; j < f->blocks; j++) {
445 93066 float *dst = block->buf + 1024 + j * f->blocksize;
446
447 93066 imdct_fn(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
448 93066 sizeof(float)*f->blocks);
449 93066 f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
450 ff_celt_window, CELT_OVERLAP / 2);
451 }
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56826 times.
56826 if (downmix)
454 f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
455
456 /* postfilter */
457 56826 celt_postfilter(f, block);
458
459 /* deemphasis */
460 56826 block->emph_coeff = f->opusdsp.deemphasis(output[i],
461 56826 &block->buf[1024 - frame_size],
462 block->emph_coeff, frame_size);
463 }
464
465
2/2
✓ Branch 0 taken 11149 times.
✓ Branch 1 taken 18798 times.
29947 if (channels == 1)
466 11149 memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
467
468
2/2
✓ Branch 0 taken 59894 times.
✓ Branch 1 taken 29947 times.
89841 for (i = 0; i < 2; i++ ) {
469 59894 CeltBlock *block = &f->block[i];
470
471
2/2
✓ Branch 0 taken 51806 times.
✓ Branch 1 taken 8088 times.
59894 if (!f->transient) {
472 51806 memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
473 51806 memcpy(block->prev_energy[0], block->energy, sizeof(block->prev_energy[0]));
474 } else {
475
2/2
✓ Branch 0 taken 169848 times.
✓ Branch 1 taken 8088 times.
177936 for (j = 0; j < CELT_MAX_BANDS; j++)
476
2/2
✓ Branch 0 taken 19438 times.
✓ Branch 1 taken 150410 times.
169848 block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
477 }
478
479
2/2
✓ Branch 0 taken 169626 times.
✓ Branch 1 taken 59894 times.
229520 for (j = 0; j < f->start_band; j++) {
480 169626 block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
481 169626 block->energy[j] = 0.0;
482 }
483
2/2
✓ Branch 0 taken 70880 times.
✓ Branch 1 taken 59894 times.
130774 for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
484 70880 block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
485 70880 block->energy[j] = 0.0;
486 }
487 }
488
489 29947 f->seed = rc->range;
490
491 29947 return 0;
492 }
493
494 4622 void ff_celt_flush(CeltFrame *f)
495 {
496 int i, j;
497
498
2/2
✓ Branch 0 taken 4541 times.
✓ Branch 1 taken 81 times.
4622 if (f->flushed)
499 4541 return;
500
501
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 81 times.
243 for (i = 0; i < 2; i++) {
502 162 CeltBlock *block = &f->block[i];
503
504
2/2
✓ Branch 0 taken 3402 times.
✓ Branch 1 taken 162 times.
3564 for (j = 0; j < CELT_MAX_BANDS; j++)
505 3402 block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
506
507 162 memset(block->energy, 0, sizeof(block->energy));
508 162 memset(block->buf, 0, sizeof(block->buf));
509
510 162 memset(block->pf_gains, 0, sizeof(block->pf_gains));
511 162 memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
512 162 memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
513
514 /* libopus uses CELT_EMPH_COEFF on init, but 0 is better since there's
515 * a lesser discontinuity when seeking.
516 * The deemphasis functions differ from libopus in that they require
517 * an initial state divided by the coefficient. */
518 162 block->emph_coeff = 0.0f / CELT_EMPH_COEFF;
519 }
520 81 f->seed = 0;
521
522 81 f->flushed = 1;
523 }
524
525 58 void ff_celt_free(CeltFrame **f)
526 {
527 58 CeltFrame *frm = *f;
528 int i;
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!frm)
531 return;
532
533
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 58 times.
290 for (i = 0; i < FF_ARRAY_ELEMS(frm->tx); i++)
534 232 av_tx_uninit(&frm->tx[i]);
535
536 58 ff_celt_pvq_uninit(&frm->pvq);
537
538 58 av_freep(&frm->dsp);
539 58 av_freep(f);
540 }
541
542 58 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
543 int apply_phase_inv)
544 {
545 CeltFrame *frm;
546 int i, ret;
547
548
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) {
549 av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
550 output_channels);
551 return AVERROR(EINVAL);
552 }
553
554 58 frm = av_mallocz(sizeof(*frm));
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!frm)
556 return AVERROR(ENOMEM);
557
558 58 frm->avctx = avctx;
559 58 frm->output_channels = output_channels;
560 58 frm->apply_phase_inv = apply_phase_inv;
561
562
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 58 times.
290 for (i = 0; i < FF_ARRAY_ELEMS(frm->tx); i++) {
563 232 const float scale = -1.0f/32768;
564
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
232 if ((ret = av_tx_init(&frm->tx[i], &frm->tx_fn[i], AV_TX_FLOAT_MDCT, 1, 15 << (i + 3), &scale, 0)) < 0)
565 goto fail;
566 }
567
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
58 if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
569 goto fail;
570
571 58 frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!frm->dsp) {
573 ret = AVERROR(ENOMEM);
574 goto fail;
575 }
576
577 58 ff_opus_dsp_init(&frm->opusdsp);
578 58 ff_celt_flush(frm);
579
580 58 *f = frm;
581
582 58 return 0;
583 fail:
584 ff_celt_free(&frm);
585 return ret;
586 }
587