FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/dec_celt.c
Date: 2026-09-11 02:21:26
Exec Total Coverage
Lines: 297 319 93.1%
Functions: 13 13 100.0%
Branches: 194 212 91.5%

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 "libavutil/mem.h"
31 #include "celt.h"
32 #include "tab.h"
33 #include "pvq.h"
34
35 /* Use the 2D z-transform to apply prediction in both the time domain (alpha)
36 * and the frequency domain (beta) */
37 30192 static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc)
38 {
39 int i, j;
40 30192 float prev[2] = { 0 };
41 30192 float alpha = ff_celt_alpha_coef[f->size];
42 30192 float beta = ff_celt_beta_coef[f->size];
43 30192 const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0];
44
45 /* intra frame */
46
4/4
✓ Branch 1 taken 30054 times.
✓ Branch 2 taken 138 times.
✓ Branch 4 taken 5912 times.
✓ Branch 5 taken 24142 times.
30192 if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) {
47 5912 alpha = 0.0f;
48 5912 beta = 1.0f - (4915.0f/32768.0f);
49 5912 model = ff_celt_coarse_energy_dist[f->size][1];
50 }
51
52
2/2
✓ Branch 0 taken 634032 times.
✓ Branch 1 taken 30192 times.
664224 for (i = 0; i < CELT_MAX_BANDS; i++) {
53
2/2
✓ Branch 0 taken 1033242 times.
✓ Branch 1 taken 634032 times.
1667274 for (j = 0; j < f->channels; j++) {
54 1033242 CeltBlock *block = &f->block[j];
55 float value;
56 int available;
57
58
4/4
✓ Branch 0 taken 907646 times.
✓ Branch 1 taken 125596 times.
✓ Branch 2 taken 47336 times.
✓ Branch 3 taken 860310 times.
1033242 if (i < f->start_band || i >= f->end_band) {
59 172932 block->energy[i] = 0.0;
60 172932 continue;
61 }
62
63 860310 available = f->framebits - opus_rc_tell(rc);
64
2/2
✓ Branch 0 taken 856096 times.
✓ Branch 1 taken 4214 times.
860310 if (available >= 15) {
65 /* decode using a Laplace distribution */
66 856096 int k = FFMIN(i, 20) << 1;
67 856096 value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
68
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 4101 times.
4214 } else if (available >= 2) {
69 113 int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small);
70 113 value = (x>>1) ^ -(x&1);
71
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4098 times.
4101 } else if (available >= 1) {
72 3 value = -(float)ff_opus_rc_dec_log(rc, 1);
73 4098 } else value = -1;
74
75
2/2
✓ Branch 0 taken 119458 times.
✓ Branch 1 taken 740852 times.
860310 block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value;
76 860310 prev[j] += beta * value;
77 }
78 }
79 30192 }
80
81 30192 static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc)
82 {
83 int i;
84
2/2
✓ Branch 0 taken 513779 times.
✓ Branch 1 taken 30192 times.
543971 for (i = f->start_band; i < f->end_band; i++) {
85 int j;
86
2/2
✓ Branch 0 taken 95641 times.
✓ Branch 1 taken 418138 times.
513779 if (!f->fine_bits[i])
87 95641 continue;
88
89
2/2
✓ Branch 0 taken 691436 times.
✓ Branch 1 taken 418138 times.
1109574 for (j = 0; j < f->channels; j++) {
90 691436 CeltBlock *block = &f->block[j];
91 int q2;
92 float offset;
93 691436 q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]);
94 691436 offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f;
95 691436 block->energy[i] += offset;
96 }
97 }
98 30192 }
99
100 30192 static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc)
101 {
102 int priority, i, j;
103 30192 int bits_left = f->framebits - opus_rc_tell(rc);
104
105
2/2
✓ Branch 0 taken 60384 times.
✓ Branch 1 taken 30192 times.
90576 for (priority = 0; priority < 2; priority++) {
106
4/4
✓ Branch 0 taken 158250 times.
✓ Branch 1 taken 6252 times.
✓ Branch 2 taken 104118 times.
✓ Branch 3 taken 54132 times.
164502 for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) {
107
4/4
✓ Branch 0 taken 55722 times.
✓ Branch 1 taken 48396 times.
✓ Branch 2 taken 17685 times.
✓ Branch 3 taken 38037 times.
104118 if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
108 66081 continue;
109
110
2/2
✓ Branch 0 taken 59991 times.
✓ Branch 1 taken 38037 times.
98028 for (j = 0; j < f->channels; j++) {
111 int q2;
112 float offset;
113 59991 q2 = ff_opus_rc_get_raw(rc, 1);
114 59991 offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
115 59991 f->block[j].energy[i] += offset;
116 59991 bits_left--;
117 }
118 }
119 }
120 30192 }
121
122 30192 static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc)
123 {
124 30192 int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
125
2/2
✓ Branch 0 taken 4083 times.
✓ Branch 1 taken 26109 times.
30192 int consumed, bits = f->transient ? 2 : 4;
126
127 30192 consumed = opus_rc_tell(rc);
128
4/4
✓ Branch 0 taken 16486 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16377 times.
✓ Branch 3 taken 109 times.
30192 tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits);
129
130
2/2
✓ Branch 0 taken 513779 times.
✓ Branch 1 taken 30192 times.
543971 for (i = f->start_band; i < f->end_band; i++) {
131
2/2
✓ Branch 0 taken 511030 times.
✓ Branch 1 taken 2749 times.
513779 if (consumed+bits+tf_select_bit <= f->framebits) {
132 511030 diff ^= ff_opus_rc_dec_log(rc, bits);
133 511030 consumed = opus_rc_tell(rc);
134 511030 tf_changed |= diff;
135 }
136 513779 f->tf_change[i] = diff;
137
2/2
✓ Branch 0 taken 61352 times.
✓ Branch 1 taken 452427 times.
513779 bits = f->transient ? 4 : 5;
138 }
139
140
2/2
✓ Branch 0 taken 16377 times.
✓ Branch 1 taken 13815 times.
30192 if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
141
2/2
✓ Branch 0 taken 8301 times.
✓ Branch 1 taken 8076 times.
16377 ff_celt_tf_select[f->size][f->transient][1][tf_changed])
142 8301 tf_select = ff_opus_rc_dec_log(rc, 1);
143
144
2/2
✓ Branch 0 taken 513779 times.
✓ Branch 1 taken 30192 times.
543971 for (i = f->start_band; i < f->end_band; i++) {
145 513779 f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
146 }
147 30192 }
148
149 49202 static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
150 {
151 int i, j;
152
153
2/2
✓ Branch 0 taken 860310 times.
✓ Branch 1 taken 49202 times.
909512 for (i = f->start_band; i < f->end_band; i++) {
154 860310 float *dst = data + (ff_celt_freq_bands[i] << f->size);
155 860310 float log_norm = block->energy[i] + ff_celt_mean_energy[i];
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 860310 times.
860310 float norm = exp2f(FFMIN(log_norm, 32.0f));
157
158
2/2
✓ Branch 0 taken 12714660 times.
✓ Branch 1 taken 860310 times.
13574970 for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
159 12714660 dst[j] *= norm;
160 }
161 49202 }
162
163 90216 static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
164 {
165 90216 const int T0 = block->pf_period_old;
166 90216 const int T1 = block->pf_period;
167
168 float g00, g01, g02;
169 float g10, g11, g12;
170
171 float x0, x1, x2, x3, x4;
172
173 int i;
174
175
2/2
✓ Branch 0 taken 42203 times.
✓ Branch 1 taken 48013 times.
90216 if (block->pf_gains[0] == 0.0 &&
176
2/2
✓ Branch 0 taken 37999 times.
✓ Branch 1 taken 4204 times.
42203 block->pf_gains_old[0] == 0.0)
177 37999 return;
178
179 52217 g00 = block->pf_gains_old[0];
180 52217 g01 = block->pf_gains_old[1];
181 52217 g02 = block->pf_gains_old[2];
182 52217 g10 = block->pf_gains[0];
183 52217 g11 = block->pf_gains[1];
184 52217 g12 = block->pf_gains[2];
185
186 52217 x1 = data[-T1 + 1];
187 52217 x2 = data[-T1];
188 52217 x3 = data[-T1 - 1];
189 52217 x4 = data[-T1 - 2];
190
191
2/2
✓ Branch 0 taken 6266040 times.
✓ Branch 1 taken 52217 times.
6318257 for (i = 0; i < CELT_OVERLAP; i++) {
192 6266040 float w = ff_celt_window2[i];
193 6266040 x0 = data[i - T1 + 2];
194
195 6266040 data[i] += (1.0 - w) * g00 * data[i - T0] +
196 6266040 (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
197 6266040 (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
198 6266040 w * g10 * x2 +
199 6266040 w * g11 * (x1 + x3) +
200 6266040 w * g12 * (x0 + x4);
201 6266040 x4 = x3;
202 6266040 x3 = x2;
203 6266040 x2 = x1;
204 6266040 x1 = x0;
205 }
206 }
207
208 57283 static void celt_postfilter(CeltFrame *f, CeltBlock *block)
209 {
210 57283 int len = f->blocksize * f->blocks;
211 57283 const int filter_len = len - 2 * CELT_OVERLAP;
212
213 57283 celt_postfilter_apply_transition(block, block->buf + 1024);
214
215 57283 block->pf_period_old = block->pf_period;
216 57283 memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
217
218 57283 block->pf_period = block->pf_period_new;
219 57283 memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
220
221
2/2
✓ Branch 0 taken 32933 times.
✓ Branch 1 taken 24350 times.
57283 if (len > CELT_OVERLAP) {
222 32933 celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
223
224
4/4
✓ Branch 0 taken 17031 times.
✓ Branch 1 taken 15902 times.
✓ Branch 2 taken 10417 times.
✓ Branch 3 taken 6614 times.
32933 if (block->pf_gains[0] > FLT_EPSILON && filter_len > 0)
225 10417 f->opusdsp.postfilter(block->buf + 1024 + 2 * CELT_OVERLAP,
226 10417 block->pf_period, block->pf_gains,
227 filter_len);
228
229 32933 block->pf_period_old = block->pf_period;
230 32933 memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
231 }
232
233 57283 memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
234 57283 }
235
236 30192 static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
237 {
238 int i;
239
240 30192 memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
241 30192 memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
242
243
4/4
✓ Branch 0 taken 25203 times.
✓ Branch 1 taken 4989 times.
✓ Branch 2 taken 25065 times.
✓ Branch 3 taken 138 times.
30192 if (f->start_band == 0 && consumed + 16 <= f->framebits) {
244 25065 int has_postfilter = ff_opus_rc_dec_log(rc, 1);
245
2/2
✓ Branch 0 taken 16033 times.
✓ Branch 1 taken 9032 times.
25065 if (has_postfilter) {
246 float gain;
247 int tapset, octave, period;
248
249 16033 octave = ff_opus_rc_dec_uint(rc, 6);
250 16033 period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
251 16033 gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
252 16033 tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
253
1/2
✓ Branch 0 taken 16033 times.
✗ Branch 1 not taken.
16033 ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
254
255
2/2
✓ Branch 0 taken 32066 times.
✓ Branch 1 taken 16033 times.
48099 for (i = 0; i < 2; i++) {
256 32066 CeltBlock *block = &f->block[i];
257
258 32066 block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
259 32066 block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
260 32066 block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
261 32066 block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
262 }
263 }
264
265 25065 consumed = opus_rc_tell(rc);
266 }
267
268 30192 return consumed;
269 }
270
271 2860 static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
272 {
273 int i, j, k;
274
275
2/2
✓ Branch 0 taken 36943 times.
✓ Branch 1 taken 2860 times.
39803 for (i = f->start_band; i < f->end_band; i++) {
276 36943 int renormalize = 0;
277 float *xptr;
278 float prev[2];
279 float Ediff, r;
280 float thresh, sqrt_1;
281 int depth;
282
283 /* depth in 1/8 bits */
284 36943 depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
285 36943 thresh = exp2f(-1.0 - 0.125f * depth);
286 36943 sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
287
288 36943 xptr = X + (ff_celt_freq_bands[i] << f->size);
289
290 36943 prev[0] = block->prev_energy[0][i];
291 36943 prev[1] = block->prev_energy[1][i];
292
2/2
✓ Branch 0 taken 10317 times.
✓ Branch 1 taken 26626 times.
36943 if (f->channels == 1) {
293 10317 CeltBlock *block1 = &f->block[1];
294
295
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10303 times.
10317 prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
296
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 10180 times.
10317 prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
297 }
298
2/2
✓ Branch 0 taken 16948 times.
✓ Branch 1 taken 19995 times.
36943 Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
299
2/2
✓ Branch 0 taken 2776 times.
✓ Branch 1 taken 34167 times.
36943 Ediff = FFMAX(0, Ediff);
300
301 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
302 short blocks don't have the same energy as long */
303 36943 r = exp2f(1 - Ediff);
304
2/2
✓ Branch 0 taken 25382 times.
✓ Branch 1 taken 11561 times.
36943 if (f->size == 3)
305 25382 r *= M_SQRT2;
306
2/2
✓ Branch 0 taken 6446 times.
✓ Branch 1 taken 30497 times.
36943 r = FFMIN(thresh, r) * sqrt_1;
307
2/2
✓ Branch 0 taken 249300 times.
✓ Branch 1 taken 36943 times.
286243 for (k = 0; k < 1 << f->size; k++) {
308 /* Detect collapse */
309
2/2
✓ Branch 0 taken 23238 times.
✓ Branch 1 taken 226062 times.
249300 if (!(block->collapse_masks[i] & 1 << k)) {
310 /* Fill with noise */
311
2/2
✓ Branch 0 taken 109416 times.
✓ Branch 1 taken 23238 times.
132654 for (j = 0; j < ff_celt_freq_range[i]; j++)
312
2/2
✓ Branch 1 taken 54641 times.
✓ Branch 2 taken 54775 times.
109416 xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
313 23238 renormalize = 1;
314 }
315 }
316
317 /* We just added some energy, so we need to renormalize */
318
2/2
✓ Branch 0 taken 9668 times.
✓ Branch 1 taken 27275 times.
36943 if (renormalize)
319 9668 celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
320 }
321 2860 }
322
323 30192 int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc,
324 float **output, int channels, int frame_size,
325 int start_band, int end_band)
326 {
327 30192 int i, j, downmix = 0;
328 int consumed; // bits of entropy consumed thus far for this frame
329 AVTXContext *imdct;
330 av_tx_fn imdct_fn;
331
332
3/4
✓ Branch 0 taken 19010 times.
✓ Branch 1 taken 11182 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19010 times.
30192 if (channels != 1 && channels != 2) {
333 av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
334 channels);
335 return AVERROR_INVALIDDATA;
336 }
337
3/6
✓ Branch 0 taken 30192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30192 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30192 times.
30192 if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
338 av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
339 start_band, end_band);
340 return AVERROR_INVALIDDATA;
341 }
342
343 30192 f->silence = 0;
344 30192 f->transient = 0;
345 30192 f->anticollapse = 0;
346 30192 f->flushed = 0;
347 30192 f->channels = channels;
348 30192 f->start_band = start_band;
349 30192 f->end_band = end_band;
350 30192 f->framebits = rc->rb.bytes * 8;
351
352 30192 f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
353
1/2
✓ Branch 0 taken 30192 times.
✗ Branch 1 not taken.
30192 if (f->size > CELT_MAX_LOG_BLOCKS ||
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30192 times.
30192 frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
355 av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
356 frame_size);
357 return AVERROR_INVALIDDATA;
358 }
359
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30192 times.
30192 if (!f->output_channels)
361 f->output_channels = channels;
362
363
2/2
✓ Branch 0 taken 49202 times.
✓ Branch 1 taken 30192 times.
79394 for (i = 0; i < f->channels; i++) {
364 49202 memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs));
365 49202 memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
366 }
367
368 30192 consumed = opus_rc_tell(rc);
369
370 /* obtain silence flag */
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30192 times.
30192 if (consumed >= f->framebits)
372 f->silence = 1;
373
2/2
✓ Branch 0 taken 25203 times.
✓ Branch 1 taken 4989 times.
30192 else if (consumed == 1)
374 25203 f->silence = ff_opus_rc_dec_log(rc, 15);
375
376
377
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 30054 times.
30192 if (f->silence) {
378 138 consumed = f->framebits;
379 138 rc->total_bits += f->framebits - opus_rc_tell(rc);
380 }
381
382 /* obtain post-filter options */
383 30192 consumed = parse_postfilter(f, rc, consumed);
384
385 /* obtain transient flag */
386
4/4
✓ Branch 0 taken 16486 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16385 times.
✓ Branch 3 taken 101 times.
30192 if (f->size != 0 && consumed+3 <= f->framebits)
387 16385 f->transient = ff_opus_rc_dec_log(rc, 3);
388
389
2/2
✓ Branch 0 taken 4083 times.
✓ Branch 1 taken 26109 times.
30192 f->blocks = f->transient ? 1 << f->size : 1;
390 30192 f->blocksize = frame_size / f->blocks;
391
392
2/2
✓ Branch 0 taken 26109 times.
✓ Branch 1 taken 4083 times.
30192 imdct = f->tx[f->transient ? 0 : f->size];
393
2/2
✓ Branch 0 taken 26109 times.
✓ Branch 1 taken 4083 times.
30192 imdct_fn = f->tx_fn[f->transient ? 0 : f->size];
394
395
2/2
✓ Branch 0 taken 11182 times.
✓ Branch 1 taken 19010 times.
30192 if (channels == 1) {
396
2/2
✓ Branch 0 taken 234822 times.
✓ Branch 1 taken 11182 times.
246004 for (i = 0; i < CELT_MAX_BANDS; i++)
397
2/2
✓ Branch 0 taken 393 times.
✓ Branch 1 taken 234429 times.
234822 f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
398 }
399
400 30192 celt_decode_coarse_energy(f, rc);
401 30192 celt_decode_tf_changes (f, rc);
402 30192 ff_celt_bitalloc (f, rc, 0);
403 30192 celt_decode_fine_energy (f, rc);
404 30192 ff_celt_quant_bands (f, rc);
405
406
2/2
✓ Branch 0 taken 3197 times.
✓ Branch 1 taken 26995 times.
30192 if (f->anticollapse_needed)
407 3197 f->anticollapse = ff_opus_rc_get_raw(rc, 1);
408
409 30192 celt_decode_final_energy(f, rc);
410
411 /* apply anti-collapse processing and denormalization to
412 * each coded channel */
413
2/2
✓ Branch 0 taken 49202 times.
✓ Branch 1 taken 30192 times.
79394 for (i = 0; i < f->channels; i++) {
414 49202 CeltBlock *block = &f->block[i];
415
416
2/2
✓ Branch 0 taken 2860 times.
✓ Branch 1 taken 46342 times.
49202 if (f->anticollapse)
417 2860 process_anticollapse(f, block, f->block[i].coeffs);
418
419 49202 celt_denormalize(f, block, f->block[i].coeffs);
420 }
421
422 /* stereo -> mono downmix */
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30192 times.
30192 if (f->output_channels < f->channels) {
424 f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
425 downmix = 1;
426
2/2
✓ Branch 0 taken 8081 times.
✓ Branch 1 taken 22111 times.
30192 } else if (f->output_channels > f->channels)
427 8081 memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
428
429
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 30054 times.
30192 if (f->silence) {
430
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 138 times.
414 for (i = 0; i < 2; i++) {
431 276 CeltBlock *block = &f->block[i];
432
433
2/2
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 276 times.
6072 for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
434 5796 block->energy[j] = CELT_ENERGY_SILENCE;
435 }
436 138 memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
437 138 memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
438 }
439
440 /* transform and output for each output channel */
441
2/2
✓ Branch 0 taken 57283 times.
✓ Branch 1 taken 30192 times.
87475 for (i = 0; i < f->output_channels; i++) {
442 57283 CeltBlock *block = &f->block[i];
443
444 /* iMDCT and overlap-add */
445
2/2
✓ Branch 0 taken 93992 times.
✓ Branch 1 taken 57283 times.
151275 for (j = 0; j < f->blocks; j++) {
446 93992 float *dst = block->buf + 1024 + j * f->blocksize;
447
448 93992 imdct_fn(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
449 93992 sizeof(float)*f->blocks);
450 93992 f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
451 ff_celt_window, CELT_OVERLAP / 2);
452 }
453
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57283 times.
57283 if (downmix)
455 f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
456
457 /* postfilter */
458 57283 celt_postfilter(f, block);
459
460 /* deemphasis */
461 114566 block->emph_coeff = f->opusdsp.deemphasis(output[i],
462 57283 &block->buf[1024 - frame_size],
463 block->emph_coeff,
464 ff_opus_deemph_weights,
465 frame_size);
466
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 57091 times.
57283 if (!isnormal(block->emph_coeff))
467 192 block->emph_coeff = 0.0;
468 }
469
470
2/2
✓ Branch 0 taken 11182 times.
✓ Branch 1 taken 19010 times.
30192 if (channels == 1)
471 11182 memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
472
473
2/2
✓ Branch 0 taken 60384 times.
✓ Branch 1 taken 30192 times.
90576 for (i = 0; i < 2; i++ ) {
474 60384 CeltBlock *block = &f->block[i];
475
476
2/2
✓ Branch 0 taken 52218 times.
✓ Branch 1 taken 8166 times.
60384 if (!f->transient) {
477 52218 memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
478 52218 memcpy(block->prev_energy[0], block->energy, sizeof(block->prev_energy[0]));
479 } else {
480
2/2
✓ Branch 0 taken 171486 times.
✓ Branch 1 taken 8166 times.
179652 for (j = 0; j < CELT_MAX_BANDS; j++)
481
2/2
✓ Branch 0 taken 19462 times.
✓ Branch 1 taken 152024 times.
171486 block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
482 }
483
484
2/2
✓ Branch 0 taken 169626 times.
✓ Branch 1 taken 60384 times.
230010 for (j = 0; j < f->start_band; j++) {
485 169626 block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
486 169626 block->energy[j] = 0.0;
487 }
488
2/2
✓ Branch 0 taken 70880 times.
✓ Branch 1 taken 60384 times.
131264 for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
489 70880 block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
490 70880 block->energy[j] = 0.0;
491 }
492 }
493
494 30192 f->seed = rc->range;
495
496 30192 return 0;
497 }
498
499 4705 void ff_celt_flush(CeltFrame *f)
500 {
501 int i, j;
502
503
2/2
✓ Branch 0 taken 4541 times.
✓ Branch 1 taken 164 times.
4705 if (f->flushed)
504 4541 return;
505
506
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 164 times.
492 for (i = 0; i < 2; i++) {
507 328 CeltBlock *block = &f->block[i];
508
509
2/2
✓ Branch 0 taken 6888 times.
✓ Branch 1 taken 328 times.
7216 for (j = 0; j < CELT_MAX_BANDS; j++)
510 6888 block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
511
512 328 memset(block->energy, 0, sizeof(block->energy));
513 328 memset(block->buf, 0, sizeof(block->buf));
514
515 328 memset(block->pf_gains, 0, sizeof(block->pf_gains));
516 328 memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
517 328 memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
518
519 /* libopus uses CELT_EMPH_COEFF on init, but 0 is better since there's
520 * a lesser discontinuity when seeking.
521 * The deemphasis functions differ from libopus in that they require
522 * an initial state divided by the coefficient. */
523 328 block->emph_coeff = 0.0f / ff_opus_deemph_weights[0];
524 }
525 164 f->seed = 0;
526
527 164 f->flushed = 1;
528 }
529
530 141 void ff_celt_free(CeltFrame **f)
531 {
532 141 CeltFrame *frm = *f;
533 int i;
534
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (!frm)
536 return;
537
538
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 141 times.
705 for (i = 0; i < FF_ARRAY_ELEMS(frm->tx); i++)
539 564 av_tx_uninit(&frm->tx[i]);
540
541 141 ff_celt_pvq_uninit(&frm->pvq);
542
543 141 av_freep(&frm->dsp);
544 141 av_freep(f);
545 }
546
547 141 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
548 int apply_phase_inv)
549 {
550 CeltFrame *frm;
551 int i, ret;
552
553
3/4
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
141 if (output_channels != 1 && output_channels != 2) {
554 av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
555 output_channels);
556 return AVERROR(EINVAL);
557 }
558
559 141 frm = av_mallocz(sizeof(*frm));
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (!frm)
561 return AVERROR(ENOMEM);
562
563 141 frm->avctx = avctx;
564 141 frm->output_channels = output_channels;
565 141 frm->apply_phase_inv = apply_phase_inv;
566
567
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 141 times.
705 for (i = 0; i < FF_ARRAY_ELEMS(frm->tx); i++) {
568 564 const float scale = -1.0f/32768;
569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 564 times.
564 if ((ret = av_tx_init(&frm->tx[i], &frm->tx_fn[i], AV_TX_FLOAT_MDCT, 1, 15 << (i + 3), &scale, 0)) < 0)
570 goto fail;
571 }
572
573
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 141 times.
141 if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
574 goto fail;
575
576 141 frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (!frm->dsp) {
578 ret = AVERROR(ENOMEM);
579 goto fail;
580 }
581
582 141 ff_opus_dsp_init(&frm->opusdsp);
583 141 ff_celt_flush(frm);
584
585 141 *f = frm;
586
587 141 return 0;
588 fail:
589 ff_celt_free(&frm);
590 return ret;
591 }
592