FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus_celt.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 259 269 96.3%
Branches: 174 186 93.5%

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 #include <stdint.h>
23
24 #include "opus_celt.h"
25 #include "opus_pvq.h"
26 #include "opustab.h"
27
28 29947 void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
29 {
30 float lowband_scratch[8 * 22];
31 float norm1[2 * 8 * 100];
32 29947 float *norm2 = norm1 + 8 * 100;
33
34 29947 int totalbits = (f->framebits << 3) - f->anticollapse_needed;
35
36 29947 int update_lowband = 1;
37 29947 int lowband_offset = 0;
38
39 int i, j;
40
41
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
42 508634 uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
43 508634 int band_offset = ff_celt_freq_bands[i] << f->size;
44 508634 int band_size = ff_celt_freq_range[i] << f->size;
45 508634 float *X = f->block[0].coeffs + band_offset;
46
2/2
✓ Branch 0 taken 342079 times.
✓ Branch 1 taken 166555 times.
508634 float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
47 float *norm_loc1, *norm_loc2;
48
49 508634 int consumed = opus_rc_tell_frac(rc);
50 508634 int effective_lowband = -1;
51 508634 int b = 0;
52
53 /* Compute how many bits we want to allocate to this band */
54
2/2
✓ Branch 0 taken 478687 times.
✓ Branch 1 taken 29947 times.
508634 if (i != f->start_band)
55 478687 f->remaining -= consumed;
56 508634 f->remaining2 = totalbits - consumed - 1;
57
2/2
✓ Branch 0 taken 457908 times.
✓ Branch 1 taken 50726 times.
508634 if (i <= f->coded_bands - 1) {
58 457908 int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
59
2/2
✓ Branch 0 taken 457900 times.
✓ Branch 1 taken 8 times.
457908 b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
60 }
61
62
2/2
✓ Branch 0 taken 34936 times.
✓ Branch 1 taken 473698 times.
508634 if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] ||
63
6/6
✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 29947 times.
✓ Branch 2 taken 97595 times.
✓ Branch 3 taken 381092 times.
✓ Branch 4 taken 6514 times.
✓ Branch 5 taken 91081 times.
508634 i == f->start_band + 1) && (update_lowband || lowband_offset == 0))
64 387606 lowband_offset = i;
65
66
2/2
✓ Branch 0 taken 29947 times.
✓ Branch 1 taken 478687 times.
508634 if (i == f->start_band + 1) {
67 /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
68 the second to ensure the second band never has to use the LCG. */
69 29947 int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
70
71 29947 memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
72
73
2/2
✓ Branch 0 taken 18798 times.
✓ Branch 1 taken 11149 times.
29947 if (f->channels == 2)
74 18798 memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
75 }
76
77 /* Get a conservative estimate of the collapse_mask's for the bands we're
78 going to be folding from. */
79
4/4
✓ Branch 0 taken 478687 times.
✓ Branch 1 taken 29947 times.
✓ Branch 2 taken 71137 times.
✓ Branch 3 taken 407550 times.
508634 if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
80
3/4
✓ Branch 0 taken 71137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11835 times.
✓ Branch 3 taken 59302 times.
71137 f->blocks > 1 || f->tf_change[i] < 0)) {
81 int foldstart, foldend;
82
83 /* This ensures we never repeat spectral content within one band */
84 419385 effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
85 ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
86 419385 foldstart = lowband_offset;
87
2/2
✓ Branch 0 taken 198000 times.
✓ Branch 1 taken 419385 times.
617385 while (ff_celt_freq_bands[--foldstart] > effective_lowband);
88 419385 foldend = lowband_offset - 1;
89
4/4
✓ Branch 0 taken 103600 times.
✓ Branch 1 taken 344751 times.
✓ Branch 2 taken 28966 times.
✓ Branch 3 taken 74634 times.
448351 while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]);
90
91 419385 cm[0] = cm[1] = 0;
92
2/2
✓ Branch 0 taken 646351 times.
✓ Branch 1 taken 419385 times.
1065736 for (j = foldstart; j < foldend; j++) {
93 646351 cm[0] |= f->block[0].collapse_masks[j];
94 646351 cm[1] |= f->block[f->channels - 1].collapse_masks[j];
95 }
96 }
97
98
4/4
✓ Branch 0 taken 36886 times.
✓ Branch 1 taken 471748 times.
✓ Branch 2 taken 1677 times.
✓ Branch 3 taken 35209 times.
508634 if (f->dual_stereo && i == f->intensity_stereo) {
99 /* Switch off dual stereo to do intensity */
100 1677 f->dual_stereo = 0;
101
2/2
✓ Branch 0 taken 572238 times.
✓ Branch 1 taken 1677 times.
573915 for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
102 572238 norm1[j] = (norm1[j] + norm2[j]) / 2;
103 }
104
105
2/2
✓ Branch 0 taken 419385 times.
✓ Branch 1 taken 89249 times.
508634 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
106
2/2
✓ Branch 0 taken 419385 times.
✓ Branch 1 taken 89249 times.
508634 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
107
108
2/2
✓ Branch 0 taken 35209 times.
✓ Branch 1 taken 473425 times.
508634 if (f->dual_stereo) {
109 70418 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
110 35209 f->blocks, norm_loc1, f->size,
111 35209 norm1 + band_offset, 0, 1.0f,
112 35209 lowband_scratch, cm[0]);
113
114 35209 cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
115 35209 f->blocks, norm_loc2, f->size,
116 35209 norm2 + band_offset, 0, 1.0f,
117 35209 lowband_scratch, cm[1]);
118 } else {
119 946850 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0,
120 473425 f->blocks, norm_loc1, f->size,
121 473425 norm1 + band_offset, 0, 1.0f,
122 473425 lowband_scratch, cm[0] | cm[1]);
123 473425 cm[1] = cm[0];
124 }
125
126 508634 f->block[0].collapse_masks[i] = (uint8_t)cm[0];
127 508634 f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
128 508634 f->remaining += f->pulses[i] + consumed;
129
130 /* Update the folding position only as long as we have 1 bit/sample depth */
131 508634 update_lowband = (b > band_size << 3);
132 }
133 29947 }
134
135 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
136
137 29947 void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
138 {
139 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
140 29947 int skip_startband = f->start_band;
141 29947 int skip_bit = 0;
142 29947 int intensitystereo_bit = 0;
143 29947 int dualstereo_bit = 0;
144 29947 int dynalloc = 6;
145 29947 int extrabits = 0;
146
147 29947 int boost[CELT_MAX_BANDS] = { 0 };
148 int trim_offset[CELT_MAX_BANDS];
149 int threshold[CELT_MAX_BANDS];
150 int bits1[CELT_MAX_BANDS];
151 int bits2[CELT_MAX_BANDS];
152
153 /* Spread */
154
2/2
✓ Branch 1 taken 29812 times.
✓ Branch 2 taken 135 times.
29947 if (opus_rc_tell(rc) + 4 <= f->framebits) {
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29812 times.
29812 if (encode)
156 ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread);
157 else
158 29812 f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
159 } else {
160 135 f->spread = CELT_SPREAD_NORMAL;
161 }
162
163 /* Initialize static allocation caps */
164
2/2
✓ Branch 0 taken 628887 times.
✓ Branch 1 taken 29947 times.
658834 for (i = 0; i < CELT_MAX_BANDS; i++)
165 628887 f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
166
167 /* Band boosts */
168 29947 tbits_8ths = f->framebits << 3;
169
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
170 508634 int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
171 508634 int b_dynalloc = dynalloc;
172 508634 int boost_amount = f->alloc_boost[i];
173 508634 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
174
175
3/4
✓ Branch 1 taken 522661 times.
✓ Branch 2 taken 2511 times.
✓ Branch 3 taken 522661 times.
✗ Branch 4 not taken.
525172 while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) {
176 int is_boost;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 522661 times.
522661 if (encode) {
178 is_boost = boost_amount--;
179 ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
180 } else {
181 522661 is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
182 }
183
184
2/2
✓ Branch 0 taken 506123 times.
✓ Branch 1 taken 16538 times.
522661 if (!is_boost)
185 506123 break;
186
187 16538 boost[i] += quanta;
188 16538 tbits_8ths -= quanta;
189
190 16538 b_dynalloc = 1;
191 }
192
193
2/2
✓ Branch 0 taken 12578 times.
✓ Branch 1 taken 496056 times.
508634 if (boost[i])
194 12578 dynalloc = FFMAX(dynalloc - 1, 2);
195 }
196
197 /* Allocation trim */
198
1/2
✓ Branch 0 taken 29947 times.
✗ Branch 1 not taken.
29947 if (!encode)
199 29947 f->alloc_trim = 5;
200
2/2
✓ Branch 1 taken 29808 times.
✓ Branch 2 taken 139 times.
29947 if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29808 times.
29808 if (encode)
202 ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim);
203 else
204 29808 f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
205
206 /* Anti-collapse bit reservation */
207 29947 tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
208 29947 f->anticollapse_needed = 0;
209
6/6
✓ Branch 0 taken 4044 times.
✓ Branch 1 taken 25903 times.
✓ Branch 2 taken 3161 times.
✓ Branch 3 taken 883 times.
✓ Branch 4 taken 3158 times.
✓ Branch 5 taken 3 times.
29947 if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
210 3158 f->anticollapse_needed = 1 << 3;
211 29947 tbits_8ths -= f->anticollapse_needed;
212
213 /* Band skip bit reservation */
214
2/2
✓ Branch 0 taken 29814 times.
✓ Branch 1 taken 133 times.
29947 if (tbits_8ths >= 1 << 3)
215 29814 skip_bit = 1 << 3;
216 29947 tbits_8ths -= skip_bit;
217
218 /* Intensity/dual stereo bit reservation */
219
2/2
✓ Branch 0 taken 18798 times.
✓ Branch 1 taken 11149 times.
29947 if (f->channels == 2) {
220 18798 intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
221
2/2
✓ Branch 0 taken 18726 times.
✓ Branch 1 taken 72 times.
18798 if (intensitystereo_bit <= tbits_8ths) {
222 18726 tbits_8ths -= intensitystereo_bit;
223
2/2
✓ Branch 0 taken 18722 times.
✓ Branch 1 taken 4 times.
18726 if (tbits_8ths >= 1 << 3) {
224 18722 dualstereo_bit = 1 << 3;
225 18722 tbits_8ths -= 1 << 3;
226 }
227 } else {
228 72 intensitystereo_bit = 0;
229 }
230 }
231
232 /* Trim offsets */
233
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
234 508634 int trim = f->alloc_trim - 5 - f->size;
235 508634 int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
236 508634 int duration = f->size + 3;
237 508634 int scale = duration + f->channels - 1;
238
239 /* PVQ minimum allocation threshold, below this value the band is
240 * skipped */
241 508634 threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
242 f->channels << 3);
243
244 508634 trim_offset[i] = trim * (band << scale) >> 6;
245
246
2/2
✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 398986 times.
508634 if (ff_celt_freq_range[i] << f->size == 1)
247 109648 trim_offset[i] -= f->channels << 3;
248 }
249
250 /* Bisection */
251 29947 low = 1;
252 29947 high = CELT_VECTORS - 1;
253
2/2
✓ Branch 0 taken 111843 times.
✓ Branch 1 taken 29947 times.
141790 while (low <= high) {
254 111843 int center = (low + high) >> 1;
255 111843 done = total = 0;
256
257
2/2
✓ Branch 0 taken 1903744 times.
✓ Branch 1 taken 111843 times.
2015587 for (i = f->end_band - 1; i >= f->start_band; i--) {
258 1903744 bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
259
260
2/2
✓ Branch 0 taken 1813748 times.
✓ Branch 1 taken 89996 times.
1903744 if (bandbits)
261 1813748 bandbits = FFMAX(bandbits + trim_offset[i], 0);
262 1903744 bandbits += boost[i];
263
264
4/4
✓ Branch 0 taken 171562 times.
✓ Branch 1 taken 1732182 times.
✓ Branch 2 taken 21091 times.
✓ Branch 3 taken 150471 times.
1903744 if (bandbits >= threshold[i] || done) {
265 1753273 done = 1;
266 1753273 total += FFMIN(bandbits, f->caps[i]);
267
2/2
✓ Branch 0 taken 31560 times.
✓ Branch 1 taken 118911 times.
150471 } else if (bandbits >= f->channels << 3) {
268 31560 total += f->channels << 3;
269 }
270 }
271
272
2/2
✓ Branch 0 taken 39962 times.
✓ Branch 1 taken 71881 times.
111843 if (total > tbits_8ths)
273 39962 high = center - 1;
274 else
275 71881 low = center + 1;
276 }
277 29947 high = low--;
278
279 /* Bisection */
280
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->start_band; i < f->end_band; i++) {
281 508634 bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]);
282
2/2
✓ Branch 0 taken 37784 times.
✓ Branch 1 taken 470850 times.
508634 bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
283 470850 NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]);
284
285
2/2
✓ Branch 0 taken 465550 times.
✓ Branch 1 taken 43084 times.
508634 if (bits1[i])
286 465550 bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
287
2/2
✓ Branch 0 taken 496742 times.
✓ Branch 1 taken 11892 times.
508634 if (bits2[i])
288 496742 bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
289
290
2/2
✓ Branch 0 taken 486296 times.
✓ Branch 1 taken 22338 times.
508634 if (low)
291 486296 bits1[i] += boost[i];
292 508634 bits2[i] += boost[i];
293
294
2/2
✓ Branch 0 taken 12578 times.
✓ Branch 1 taken 496056 times.
508634 if (boost[i])
295 12578 skip_startband = i;
296 508634 bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
297 }
298
299 /* Bisection */
300 29947 low = 0;
301 29947 high = 1 << CELT_ALLOC_STEPS;
302
2/2
✓ Branch 0 taken 179682 times.
✓ Branch 1 taken 29947 times.
209629 for (i = 0; i < CELT_ALLOC_STEPS; i++) {
303 179682 int center = (low + high) >> 1;
304 179682 done = total = 0;
305
306
2/2
✓ Branch 0 taken 3051804 times.
✓ Branch 1 taken 179682 times.
3231486 for (j = f->end_band - 1; j >= f->start_band; j--) {
307 3051804 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
308
309
4/4
✓ Branch 0 taken 262149 times.
✓ Branch 1 taken 2789655 times.
✓ Branch 2 taken 51935 times.
✓ Branch 3 taken 210214 times.
3051804 if (bandbits >= threshold[j] || done) {
310 2841590 done = 1;
311 2841590 total += FFMIN(bandbits, f->caps[j]);
312
2/2
✓ Branch 0 taken 52455 times.
✓ Branch 1 taken 157759 times.
210214 } else if (bandbits >= f->channels << 3)
313 52455 total += f->channels << 3;
314 }
315
2/2
✓ Branch 0 taken 85798 times.
✓ Branch 1 taken 93884 times.
179682 if (total > tbits_8ths)
316 85798 high = center;
317 else
318 93884 low = center;
319 }
320
321 /* Bisection */
322 29947 done = total = 0;
323
2/2
✓ Branch 0 taken 508634 times.
✓ Branch 1 taken 29947 times.
538581 for (i = f->end_band - 1; i >= f->start_band; i--) {
324 508634 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
325
326
4/4
✓ Branch 0 taken 45195 times.
✓ Branch 1 taken 463439 times.
✓ Branch 2 taken 9156 times.
✓ Branch 3 taken 36039 times.
508634 if (bandbits >= threshold[i] || done)
327 472595 done = 1;
328 else
329 36039 bandbits = (bandbits >= f->channels << 3) ?
330
2/2
✓ Branch 0 taken 8853 times.
✓ Branch 1 taken 27186 times.
36039 f->channels << 3 : 0;
331
332 508634 bandbits = FFMIN(bandbits, f->caps[i]);
333 508634 f->pulses[i] = bandbits;
334 508634 total += bandbits;
335 }
336
337 /* Band skipping */
338 29947 for (f->coded_bands = f->end_band; ; f->coded_bands--) {
339 int allocation;
340 80673 j = f->coded_bands - 1;
341
342
2/2
✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 79024 times.
80673 if (j == skip_startband) {
343 /* all remaining bands are not skipped */
344 1649 tbits_8ths += skip_bit;
345 1649 break;
346 }
347
348 /* determine the number of bits available for coding "do not skip" markers */
349 79024 remaining = tbits_8ths - total;
350 79024 bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
351 79024 remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
352 79024 allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j];
353 79024 allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
354
355 /* a "do not skip" marker is only coded if the allocation is
356 * above the chosen threshold */
357
2/2
✓ Branch 0 taken 38190 times.
✓ Branch 1 taken 40834 times.
79024 if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
358 int do_not_skip;
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38190 times.
38190 if (encode) {
360 do_not_skip = f->coded_bands <= f->skip_band_floor;
361 ff_opus_rc_enc_log(rc, do_not_skip, 1);
362 } else {
363 38190 do_not_skip = ff_opus_rc_dec_log(rc, 1);
364 }
365
366
2/2
✓ Branch 0 taken 28298 times.
✓ Branch 1 taken 9892 times.
38190 if (do_not_skip)
367 28298 break;
368
369 9892 total += 1 << 3;
370 9892 allocation -= 1 << 3;
371 }
372
373 /* the band is skipped, so reclaim its bits */
374 50726 total -= f->pulses[j];
375
2/2
✓ Branch 0 taken 34531 times.
✓ Branch 1 taken 16195 times.
50726 if (intensitystereo_bit) {
376 34531 total -= intensitystereo_bit;
377 34531 intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
378 34531 total += intensitystereo_bit;
379 }
380
381
2/2
✓ Branch 0 taken 21880 times.
✓ Branch 1 taken 28846 times.
50726 total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0;
382 }
383
384 /* IS start band */
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29947 times.
29947 if (encode) {
386 if (intensitystereo_bit) {
387 f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
388 ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
389 }
390 } else {
391 29947 f->intensity_stereo = f->dual_stereo = 0;
392
2/2
✓ Branch 0 taken 18726 times.
✓ Branch 1 taken 11221 times.
29947 if (intensitystereo_bit)
393 18726 f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
394 }
395
396 /* DS flag */
397
2/2
✓ Branch 0 taken 13217 times.
✓ Branch 1 taken 16730 times.
29947 if (f->intensity_stereo <= f->start_band)
398 13217 tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
399
1/2
✓ Branch 0 taken 16730 times.
✗ Branch 1 not taken.
16730 else if (dualstereo_bit)
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16730 times.
16730 if (encode)
401 ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
402 else
403 16730 f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
404
405 /* Supply the remaining bits in this frame to lower bands */
406 29947 remaining = tbits_8ths - total;
407 29947 bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
408 29947 remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
409
2/2
✓ Branch 0 taken 457908 times.
✓ Branch 1 taken 29947 times.
487855 for (i = f->start_band; i < f->coded_bands; i++) {
410 457908 const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
411 457908 f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
412 457908 remaining -= bits;
413 }
414
415 /* Finally determine the allocation */
416
2/2
✓ Branch 0 taken 457908 times.
✓ Branch 1 taken 29947 times.
487855 for (i = f->start_band; i < f->coded_bands; i++) {
417 457908 int N = ff_celt_freq_range[i] << f->size;
418 457908 int prev_extra = extrabits;
419 457908 f->pulses[i] += extrabits;
420
421
2/2
✓ Branch 0 taken 351357 times.
✓ Branch 1 taken 106551 times.
457908 if (N > 1) {
422 int dof; /* degrees of freedom */
423 int temp; /* dof * channels * log(dof) */
424 int fine_bits;
425 int max_bits;
426 int offset; /* fine energy quantization offset, i.e.
427 * extra bits assigned over the standard
428 * totalbits/dof */
429
430 351357 extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
431 351357 f->pulses[i] -= extrabits;
432
433 /* intensity stereo makes use of an extra degree of freedom */
434
8/8
✓ Branch 0 taken 242752 times.
✓ Branch 1 taken 108605 times.
✓ Branch 2 taken 190415 times.
✓ Branch 3 taken 52337 times.
✓ Branch 4 taken 156068 times.
✓ Branch 5 taken 34347 times.
✓ Branch 6 taken 140074 times.
✓ Branch 7 taken 15994 times.
351357 dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
435 351357 temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
436 351357 offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
437
2/2
✓ Branch 0 taken 83666 times.
✓ Branch 1 taken 267691 times.
351357 if (N == 2) /* dof=2 is the only case that doesn't fit the model */
438 83666 offset += dof << 1;
439
440 /* grant an additional bias for the first and second pulses */
441
2/2
✓ Branch 0 taken 247361 times.
✓ Branch 1 taken 103996 times.
351357 if (f->pulses[i] + offset < 2 * (dof << 3))
442 247361 offset += temp >> 2;
443
2/2
✓ Branch 0 taken 50077 times.
✓ Branch 1 taken 53919 times.
103996 else if (f->pulses[i] + offset < 3 * (dof << 3))
444 50077 offset += temp >> 3;
445
446 351357 fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
447 351357 max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
448 351357 max_bits = FFMAX(max_bits, 0);
449 351357 f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
450
451 /* If fine_bits was rounded down or capped,
452 * give priority for the final fine energy pass */
453 351357 f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
454
455 /* the remaining bits are assigned to PVQ */
456 351357 f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
457 } else {
458 /* all bits go to fine energy except for the sign bit */
459 106551 extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
460 106551 f->pulses[i] -= extrabits;
461 106551 f->fine_bits[i] = 0;
462 106551 f->fine_priority[i] = 1;
463 }
464
465 /* hand back a limited number of extra fine energy bits to this band */
466
2/2
✓ Branch 0 taken 123205 times.
✓ Branch 1 taken 334703 times.
457908 if (extrabits > 0) {
467 123205 int fineextra = FFMIN(extrabits >> (f->channels + 2),
468 CELT_MAX_FINE_BITS - f->fine_bits[i]);
469 123205 f->fine_bits[i] += fineextra;
470
471 123205 fineextra <<= f->channels + 2;
472 123205 f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
473 123205 extrabits -= fineextra;
474 }
475 }
476 29947 f->remaining = extrabits;
477
478 /* skipped bands dedicate all of their bits for fine energy */
479
2/2
✓ Branch 0 taken 50726 times.
✓ Branch 1 taken 29947 times.
80673 for (; i < f->end_band; i++) {
480 50726 f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
481 50726 f->pulses[i] = 0;
482 50726 f->fine_priority[i] = f->fine_bits[i] < 1;
483 }
484 29947 }
485