FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 431 519 83.0%
Branches: 264 349 75.6%

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 decoder/parser shared code
25 */
26
27 #include <stdint.h>
28
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/error.h"
31 #include "libavutil/ffmath.h"
32
33 #include "opus_celt.h"
34 #include "opustab.h"
35 #include "internal.h"
36 #include "vorbis.h"
37
38 static const uint16_t opus_frame_duration[32] = {
39 480, 960, 1920, 2880,
40 480, 960, 1920, 2880,
41 480, 960, 1920, 2880,
42 480, 960,
43 480, 960,
44 120, 240, 480, 960,
45 120, 240, 480, 960,
46 120, 240, 480, 960,
47 120, 240, 480, 960,
48 };
49
50 /**
51 * Read a 1- or 2-byte frame length
52 */
53 21816 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
54 {
55 int val;
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21816 times.
21816 if (*ptr >= end)
58 return AVERROR_INVALIDDATA;
59 21816 val = *(*ptr)++;
60
2/2
✓ Branch 0 taken 898 times.
✓ Branch 1 taken 20918 times.
21816 if (val >= 252) {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
898 if (*ptr >= end)
62 return AVERROR_INVALIDDATA;
63 898 val += 4 * *(*ptr)++;
64 }
65 21816 return val;
66 }
67
68 /**
69 * Read a multi-byte length (used for code 3 packet padding size)
70 */
71 1320 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
72 {
73 1320 int val = 0;
74 int next;
75
76 while (1) {
77
2/4
✓ Branch 0 taken 1776 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1776 times.
1776 if (*ptr >= end || val > INT_MAX - 254)
78 return AVERROR_INVALIDDATA;
79 1776 next = *(*ptr)++;
80 1776 val += next;
81
2/2
✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 456 times.
1776 if (next < 255)
82 1320 break;
83 else
84 456 val--;
85 }
86 1320 return val;
87 }
88
89 /**
90 * Parse Opus packet info from raw packet data
91 */
92 48871 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
93 int self_delimiting)
94 {
95 48871 const uint8_t *ptr = buf;
96 48871 const uint8_t *end = buf + buf_size;
97 48871 int padding = 0;
98 int frame_bytes, i;
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48871 times.
48871 if (buf_size < 1)
101 goto fail;
102
103 /* TOC byte */
104 48871 i = *ptr++;
105 48871 pkt->code = (i ) & 0x3;
106 48871 pkt->stereo = (i >> 2) & 0x1;
107 48871 pkt->config = (i >> 3) & 0x1F;
108
109 /* code 2 and code 3 packets have at least 1 byte after the TOC */
110
3/4
✓ Branch 0 taken 8200 times.
✓ Branch 1 taken 40671 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8200 times.
48871 if (pkt->code >= 2 && buf_size < 2)
111 goto fail;
112
113
4/5
✓ Branch 0 taken 40031 times.
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 1998 times.
✓ Branch 3 taken 6202 times.
✗ Branch 4 not taken.
48871 switch (pkt->code) {
114 40031 case 0:
115 /* 1 frame */
116 40031 pkt->frame_count = 1;
117 40031 pkt->vbr = 0;
118
119
2/2
✓ Branch 0 taken 6892 times.
✓ Branch 1 taken 33139 times.
40031 if (self_delimiting) {
120 6892 int len = xiph_lacing_16bit(&ptr, end);
121
2/4
✓ Branch 0 taken 6892 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6892 times.
6892 if (len < 0 || len > end - ptr)
122 goto fail;
123 6892 end = ptr + len;
124 6892 buf_size = end - buf;
125 }
126
127 40031 frame_bytes = end - ptr;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40031 times.
40031 if (frame_bytes > MAX_FRAME_SIZE)
129 goto fail;
130 40031 pkt->frame_offset[0] = ptr - buf;
131 40031 pkt->frame_size[0] = frame_bytes;
132 40031 break;
133 640 case 1:
134 /* 2 frames, equal size */
135 640 pkt->frame_count = 2;
136 640 pkt->vbr = 0;
137
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 640 times.
640 if (self_delimiting) {
139 int len = xiph_lacing_16bit(&ptr, end);
140 if (len < 0 || 2 * len > end - ptr)
141 goto fail;
142 end = ptr + 2 * len;
143 buf_size = end - buf;
144 }
145
146 640 frame_bytes = end - ptr;
147
2/4
✓ Branch 0 taken 640 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 640 times.
640 if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
148 goto fail;
149 640 pkt->frame_offset[0] = ptr - buf;
150 640 pkt->frame_size[0] = frame_bytes >> 1;
151 640 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
152 640 pkt->frame_size[1] = frame_bytes >> 1;
153 640 break;
154 1998 case 2:
155 /* 2 frames, different sizes */
156 1998 pkt->frame_count = 2;
157 1998 pkt->vbr = 1;
158
159 /* read 1st frame size */
160 1998 frame_bytes = xiph_lacing_16bit(&ptr, end);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
1998 if (frame_bytes < 0)
162 goto fail;
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
1998 if (self_delimiting) {
165 int len = xiph_lacing_16bit(&ptr, end);
166 if (len < 0 || len + frame_bytes > end - ptr)
167 goto fail;
168 end = ptr + frame_bytes + len;
169 buf_size = end - buf;
170 }
171
172 1998 pkt->frame_offset[0] = ptr - buf;
173 1998 pkt->frame_size[0] = frame_bytes;
174
175 /* calculate 2nd frame size */
176 1998 frame_bytes = end - ptr - pkt->frame_size[0];
177
2/4
✓ Branch 0 taken 1998 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1998 times.
1998 if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
178 goto fail;
179 1998 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
180 1998 pkt->frame_size[1] = frame_bytes;
181 1998 break;
182 6202 case 3:
183 /* 1 to 48 frames, can be different sizes */
184 6202 i = *ptr++;
185 6202 pkt->frame_count = (i ) & 0x3F;
186 6202 padding = (i >> 6) & 0x01;
187 6202 pkt->vbr = (i >> 7) & 0x01;
188
189
2/4
✓ Branch 0 taken 6202 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6202 times.
6202 if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
190 goto fail;
191
192 /* read padding size */
193
2/2
✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 4882 times.
6202 if (padding) {
194 1320 padding = xiph_lacing_full(&ptr, end);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 if (padding < 0)
196 goto fail;
197 }
198
199 /* read frame sizes */
200
2/2
✓ Branch 0 taken 4352 times.
✓ Branch 1 taken 1850 times.
6202 if (pkt->vbr) {
201 /* for VBR, all frames except the final one have their size coded
202 in the bitstream. the last frame size is implicit. */
203 4352 int total_bytes = 0;
204
2/2
✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
17278 for (i = 0; i < pkt->frame_count - 1; i++) {
205 12926 frame_bytes = xiph_lacing_16bit(&ptr, end);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
12926 if (frame_bytes < 0)
207 goto fail;
208 12926 pkt->frame_size[i] = frame_bytes;
209 12926 total_bytes += frame_bytes;
210 }
211
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
4352 if (self_delimiting) {
213 int len = xiph_lacing_16bit(&ptr, end);
214 if (len < 0 || len + total_bytes + padding > end - ptr)
215 goto fail;
216 end = ptr + total_bytes + len + padding;
217 buf_size = end - buf;
218 }
219
220 4352 frame_bytes = end - ptr - padding;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
4352 if (total_bytes > frame_bytes)
222 goto fail;
223 4352 pkt->frame_offset[0] = ptr - buf;
224
2/2
✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
17278 for (i = 1; i < pkt->frame_count; i++)
225 12926 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
226 4352 pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
227 } else {
228 /* for CBR, the remaining packet bytes are divided evenly between
229 the frames */
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
1850 if (self_delimiting) {
231 frame_bytes = xiph_lacing_16bit(&ptr, end);
232 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
233 goto fail;
234 end = ptr + pkt->frame_count * frame_bytes + padding;
235 buf_size = end - buf;
236 } else {
237 1850 frame_bytes = end - ptr - padding;
238
1/2
✓ Branch 0 taken 1850 times.
✗ Branch 1 not taken.
1850 if (frame_bytes % pkt->frame_count ||
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
1850 frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
240 goto fail;
241 1850 frame_bytes /= pkt->frame_count;
242 }
243
244 1850 pkt->frame_offset[0] = ptr - buf;
245 1850 pkt->frame_size[0] = frame_bytes;
246
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 1850 times.
2630 for (i = 1; i < pkt->frame_count; i++) {
247 780 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
248 780 pkt->frame_size[i] = frame_bytes;
249 }
250 }
251 }
252
253 48871 pkt->packet_size = buf_size;
254 48871 pkt->data_size = pkt->packet_size - padding;
255
256 /* total packet duration cannot be larger than 120ms */
257 48871 pkt->frame_duration = opus_frame_duration[pkt->config];
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48871 times.
48871 if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
259 goto fail;
260
261 /* set mode and bandwidth */
262
2/2
✓ Branch 0 taken 9059 times.
✓ Branch 1 taken 39812 times.
48871 if (pkt->config < 12) {
263 9059 pkt->mode = OPUS_MODE_SILK;
264 9059 pkt->bandwidth = pkt->config >> 2;
265
2/2
✓ Branch 0 taken 9750 times.
✓ Branch 1 taken 30062 times.
39812 } else if (pkt->config < 16) {
266 9750 pkt->mode = OPUS_MODE_HYBRID;
267
2/2
✓ Branch 0 taken 5147 times.
✓ Branch 1 taken 4603 times.
9750 pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
268 } else {
269 30062 pkt->mode = OPUS_MODE_CELT;
270 30062 pkt->bandwidth = (pkt->config - 16) >> 2;
271 /* skip medium band */
272
2/2
✓ Branch 0 taken 26218 times.
✓ Branch 1 taken 3844 times.
30062 if (pkt->bandwidth)
273 26218 pkt->bandwidth++;
274 }
275
276 48871 return 0;
277
278 fail:
279 memset(pkt, 0, sizeof(*pkt));
280 return AVERROR_INVALIDDATA;
281 }
282
283 207 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
284 {
285 207 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
286 }
287
288 146 static int channel_reorder_unknown(int nb_channels, int channel_idx)
289 {
290 146 return channel_idx;
291 }
292
293 75 av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
294 OpusContext *s)
295 {
296 static const uint8_t default_channel_map[2] = { 0, 1 };
297
298 75 int (*channel_reorder)(int, int) = channel_reorder_unknown;
299
300 const uint8_t *extradata, *channel_map;
301 int extradata_size;
302 int version, channels, map_type, streams, stereo_streams, i, j;
303 uint64_t layout;
304
305
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 74 times.
75 if (!avctx->extradata) {
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->channels > 2) {
307 av_log(avctx, AV_LOG_ERROR,
308 "Multichannel configuration without extradata.\n");
309 return AVERROR(EINVAL);
310 }
311 1 extradata = opus_default_extradata;
312 1 extradata_size = sizeof(opus_default_extradata);
313 } else {
314 74 extradata = avctx->extradata;
315 74 extradata_size = avctx->extradata_size;
316 }
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (extradata_size < 19) {
319 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
320 extradata_size);
321 return AVERROR_INVALIDDATA;
322 }
323
324 75 version = extradata[8];
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (version > 15) {
326 avpriv_request_sample(avctx, "Extradata version %d", version);
327 return AVERROR_PATCHWELCOME;
328 }
329
330 75 avctx->delay = AV_RL16(extradata + 10);
331
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 28 times.
75 if (avctx->internal)
332 47 avctx->internal->skip_samples = avctx->delay;
333
334
3/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
75 channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (!channels) {
336 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
337 return AVERROR_INVALIDDATA;
338 }
339
340 75 s->gain_i = AV_RL16(extradata + 16);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (s->gain_i)
342 s->gain = ff_exp10(s->gain_i / (20.0 * 256));
343
344 75 map_type = extradata[18];
345
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 7 times.
75 if (!map_type) {
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (channels > 2) {
347 av_log(avctx, AV_LOG_ERROR,
348 "Channel mapping 0 is only specified for up to 2 channels\n");
349 return AVERROR_INVALIDDATA;
350 }
351
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 39 times.
68 layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
352 68 streams = 1;
353 68 stereo_streams = channels - 1;
354 68 channel_map = default_channel_map;
355
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7 } else if (map_type == 1 || map_type == 2 || map_type == 255) {
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (extradata_size < 21 + channels) {
357 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
358 extradata_size);
359 return AVERROR_INVALIDDATA;
360 }
361
362 7 streams = extradata[19];
363 7 stereo_streams = extradata[20];
364
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if (!streams || stereo_streams > streams ||
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 streams + stereo_streams > 255) {
366 av_log(avctx, AV_LOG_ERROR,
367 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
368 return AVERROR_INVALIDDATA;
369 }
370
371
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (map_type == 1) {
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (channels > 8) {
373 av_log(avctx, AV_LOG_ERROR,
374 "Channel mapping 1 is only specified for up to 8 channels\n");
375 return AVERROR_INVALIDDATA;
376 }
377 7 layout = ff_vorbis_channel_layouts[channels - 1];
378 7 channel_reorder = channel_reorder_vorbis;
379 } else if (map_type == 2) {
380 int ambisonic_order = ff_sqrt(channels) - 1;
381 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
382 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
383 av_log(avctx, AV_LOG_ERROR,
384 "Channel mapping 2 is only specified for channel counts"
385 " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
386 " for nonnegative integer n\n");
387 return AVERROR_INVALIDDATA;
388 }
389 if (channels > 227) {
390 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
391 return AVERROR_INVALIDDATA;
392 }
393 layout = 0;
394 } else
395 layout = 0;
396
397 7 channel_map = extradata + 21;
398 } else {
399 avpriv_request_sample(avctx, "Mapping type %d", map_type);
400 return AVERROR_PATCHWELCOME;
401 }
402
403 75 s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (!s->channel_maps)
405 return AVERROR(ENOMEM);
406
407
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 75 times.
232 for (i = 0; i < channels; i++) {
408 157 ChannelMap *map = &s->channel_maps[i];
409 157 uint8_t idx = channel_map[channel_reorder(channels, i)];
410
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (idx == 255) {
412 map->silence = 1;
413 continue;
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 } else if (idx >= streams + stereo_streams) {
415 av_log(avctx, AV_LOG_ERROR,
416 "Invalid channel map for output channel %d: %d\n", i, idx);
417 av_freep(&s->channel_maps);
418 return AVERROR_INVALIDDATA;
419 }
420
421 /* check that we did not see this index yet */
422 157 map->copy = 0;
423
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 157 times.
353 for (j = 0; j < i; j++)
424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 196 times.
196 if (channel_map[channel_reorder(channels, j)] == idx) {
425 map->copy = 1;
426 map->copy_idx = j;
427 break;
428 }
429
430
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 43 times.
157 if (idx < 2 * stereo_streams) {
431 114 map->stream_idx = idx / 2;
432 114 map->channel_idx = idx & 1;
433 } else {
434 43 map->stream_idx = idx - stereo_streams;
435 43 map->channel_idx = 0;
436 }
437 }
438
439 75 avctx->channels = channels;
440 75 avctx->channel_layout = layout;
441 75 s->nb_streams = streams;
442 75 s->nb_stereo_streams = stereo_streams;
443
444 75 return 0;
445 }
446
447 29890 void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
448 {
449 float lowband_scratch[8 * 22];
450 float norm1[2 * 8 * 100];
451 29890 float *norm2 = norm1 + 8 * 100;
452
453 29890 int totalbits = (f->framebits << 3) - f->anticollapse_needed;
454
455 29890 int update_lowband = 1;
456 29890 int lowband_offset = 0;
457
458 int i, j;
459
460
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
461 508406 uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
462 508406 int band_offset = ff_celt_freq_bands[i] << f->size;
463 508406 int band_size = ff_celt_freq_range[i] << f->size;
464 508406 float *X = f->block[0].coeffs + band_offset;
465
2/2
✓ Branch 0 taken 341914 times.
✓ Branch 1 taken 166492 times.
508406 float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
466 float *norm_loc1, *norm_loc2;
467
468 508406 int consumed = opus_rc_tell_frac(rc);
469 508406 int effective_lowband = -1;
470 508406 int b = 0;
471
472 /* Compute how many bits we want to allocate to this band */
473
2/2
✓ Branch 0 taken 478516 times.
✓ Branch 1 taken 29890 times.
508406 if (i != f->start_band)
474 478516 f->remaining -= consumed;
475 508406 f->remaining2 = totalbits - consumed - 1;
476
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 50726 times.
508406 if (i <= f->coded_bands - 1) {
477 457680 int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
478
2/2
✓ Branch 0 taken 457672 times.
✓ Branch 1 taken 8 times.
457680 b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
479 }
480
481
2/2
✓ Branch 0 taken 34828 times.
✓ Branch 1 taken 473578 times.
508406 if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] ||
482
6/6
✓ Branch 0 taken 4938 times.
✓ Branch 1 taken 29890 times.
✓ Branch 2 taken 97595 times.
✓ Branch 3 taken 380921 times.
✓ Branch 4 taken 6514 times.
✓ Branch 5 taken 91081 times.
508406 i == f->start_band + 1) && (update_lowband || lowband_offset == 0))
483 387435 lowband_offset = i;
484
485
2/2
✓ Branch 0 taken 29890 times.
✓ Branch 1 taken 478516 times.
508406 if (i == f->start_band + 1) {
486 /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
487 the second to ensure the second band never has to use the LCG. */
488 29890 int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
489
490 29890 memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
491
492
2/2
✓ Branch 0 taken 18744 times.
✓ Branch 1 taken 11146 times.
29890 if (f->channels == 2)
493 18744 memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
494 }
495
496 /* Get a conservative estimate of the collapse_mask's for the bands we're
497 going to be folding from. */
498
4/4
✓ Branch 0 taken 478516 times.
✓ Branch 1 taken 29890 times.
✓ Branch 2 taken 71108 times.
✓ Branch 3 taken 407408 times.
508406 if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
499
3/4
✓ Branch 0 taken 71108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11835 times.
✓ Branch 3 taken 59273 times.
71108 f->blocks > 1 || f->tf_change[i] < 0)) {
500 int foldstart, foldend;
501
502 /* This ensures we never repeat spectral content within one band */
503 419243 effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
504 ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
505 419243 foldstart = lowband_offset;
506
2/2
✓ Branch 0 taken 197958 times.
✓ Branch 1 taken 419243 times.
617201 while (ff_celt_freq_bands[--foldstart] > effective_lowband);
507 419243 foldend = lowband_offset - 1;
508
4/4
✓ Branch 0 taken 103600 times.
✓ Branch 1 taken 344609 times.
✓ Branch 2 taken 28966 times.
✓ Branch 3 taken 74634 times.
448209 while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]);
509
510 419243 cm[0] = cm[1] = 0;
511
2/2
✓ Branch 0 taken 646167 times.
✓ Branch 1 taken 419243 times.
1065410 for (j = foldstart; j < foldend; j++) {
512 646167 cm[0] |= f->block[0].collapse_masks[j];
513 646167 cm[1] |= f->block[f->channels - 1].collapse_masks[j];
514 }
515 }
516
517
4/4
✓ Branch 0 taken 36826 times.
✓ Branch 1 taken 471580 times.
✓ Branch 2 taken 1674 times.
✓ Branch 3 taken 35152 times.
508406 if (f->dual_stereo && i == f->intensity_stereo) {
518 /* Switch off dual stereo to do intensity */
519 1674 f->dual_stereo = 0;
520
2/2
✓ Branch 0 taken 570798 times.
✓ Branch 1 taken 1674 times.
572472 for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
521 570798 norm1[j] = (norm1[j] + norm2[j]) / 2;
522 }
523
524
2/2
✓ Branch 0 taken 419243 times.
✓ Branch 1 taken 89163 times.
508406 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
525
2/2
✓ Branch 0 taken 419243 times.
✓ Branch 1 taken 89163 times.
508406 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
526
527
2/2
✓ Branch 0 taken 35152 times.
✓ Branch 1 taken 473254 times.
508406 if (f->dual_stereo) {
528 70304 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
529 35152 f->blocks, norm_loc1, f->size,
530 35152 norm1 + band_offset, 0, 1.0f,
531 35152 lowband_scratch, cm[0]);
532
533 35152 cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
534 35152 f->blocks, norm_loc2, f->size,
535 35152 norm2 + band_offset, 0, 1.0f,
536 35152 lowband_scratch, cm[1]);
537 } else {
538 946508 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0,
539 473254 f->blocks, norm_loc1, f->size,
540 473254 norm1 + band_offset, 0, 1.0f,
541 473254 lowband_scratch, cm[0] | cm[1]);
542 473254 cm[1] = cm[0];
543 }
544
545 508406 f->block[0].collapse_masks[i] = (uint8_t)cm[0];
546 508406 f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
547 508406 f->remaining += f->pulses[i] + consumed;
548
549 /* Update the folding position only as long as we have 1 bit/sample depth */
550 508406 update_lowband = (b > band_size << 3);
551 }
552 29890 }
553
554 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
555
556 29890 void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
557 {
558 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
559 29890 int skip_startband = f->start_band;
560 29890 int skip_bit = 0;
561 29890 int intensitystereo_bit = 0;
562 29890 int dualstereo_bit = 0;
563 29890 int dynalloc = 6;
564 29890 int extrabits = 0;
565
566 29890 int boost[CELT_MAX_BANDS] = { 0 };
567 int trim_offset[CELT_MAX_BANDS];
568 int threshold[CELT_MAX_BANDS];
569 int bits1[CELT_MAX_BANDS];
570 int bits2[CELT_MAX_BANDS];
571
572 /* Spread */
573
2/2
✓ Branch 1 taken 29755 times.
✓ Branch 2 taken 135 times.
29890 if (opus_rc_tell(rc) + 4 <= f->framebits) {
574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29755 times.
29755 if (encode)
575 ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread);
576 else
577 29755 f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
578 } else {
579 135 f->spread = CELT_SPREAD_NORMAL;
580 }
581
582 /* Initialize static allocation caps */
583
2/2
✓ Branch 0 taken 627690 times.
✓ Branch 1 taken 29890 times.
657580 for (i = 0; i < CELT_MAX_BANDS; i++)
584 627690 f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
585
586 /* Band boosts */
587 29890 tbits_8ths = f->framebits << 3;
588
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
589 508406 int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
590 508406 int b_dynalloc = dynalloc;
591 508406 int boost_amount = f->alloc_boost[i];
592 508406 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
593
594
3/4
✓ Branch 1 taken 522361 times.
✓ Branch 2 taken 2511 times.
✓ Branch 3 taken 522361 times.
✗ Branch 4 not taken.
524872 while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) {
595 int is_boost;
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 522361 times.
522361 if (encode) {
597 is_boost = boost_amount--;
598 ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
599 } else {
600 522361 is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
601 }
602
603
2/2
✓ Branch 0 taken 505895 times.
✓ Branch 1 taken 16466 times.
522361 if (!is_boost)
604 505895 break;
605
606 16466 boost[i] += quanta;
607 16466 tbits_8ths -= quanta;
608
609 16466 b_dynalloc = 1;
610 }
611
612
2/2
✓ Branch 0 taken 12551 times.
✓ Branch 1 taken 495855 times.
508406 if (boost[i])
613 12551 dynalloc = FFMAX(dynalloc - 1, 2);
614 }
615
616 /* Allocation trim */
617
1/2
✓ Branch 0 taken 29890 times.
✗ Branch 1 not taken.
29890 if (!encode)
618 29890 f->alloc_trim = 5;
619
2/2
✓ Branch 1 taken 29751 times.
✓ Branch 2 taken 139 times.
29890 if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29751 times.
29751 if (encode)
621 ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim);
622 else
623 29751 f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
624
625 /* Anti-collapse bit reservation */
626 29890 tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
627 29890 f->anticollapse_needed = 0;
628
6/6
✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 25874 times.
✓ Branch 2 taken 3133 times.
✓ Branch 3 taken 883 times.
✓ Branch 4 taken 3130 times.
✓ Branch 5 taken 3 times.
29890 if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
629 3130 f->anticollapse_needed = 1 << 3;
630 29890 tbits_8ths -= f->anticollapse_needed;
631
632 /* Band skip bit reservation */
633
2/2
✓ Branch 0 taken 29757 times.
✓ Branch 1 taken 133 times.
29890 if (tbits_8ths >= 1 << 3)
634 29757 skip_bit = 1 << 3;
635 29890 tbits_8ths -= skip_bit;
636
637 /* Intensity/dual stereo bit reservation */
638
2/2
✓ Branch 0 taken 18744 times.
✓ Branch 1 taken 11146 times.
29890 if (f->channels == 2) {
639 18744 intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
640
2/2
✓ Branch 0 taken 18672 times.
✓ Branch 1 taken 72 times.
18744 if (intensitystereo_bit <= tbits_8ths) {
641 18672 tbits_8ths -= intensitystereo_bit;
642
2/2
✓ Branch 0 taken 18668 times.
✓ Branch 1 taken 4 times.
18672 if (tbits_8ths >= 1 << 3) {
643 18668 dualstereo_bit = 1 << 3;
644 18668 tbits_8ths -= 1 << 3;
645 }
646 } else {
647 72 intensitystereo_bit = 0;
648 }
649 }
650
651 /* Trim offsets */
652
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
653 508406 int trim = f->alloc_trim - 5 - f->size;
654 508406 int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
655 508406 int duration = f->size + 3;
656 508406 int scale = duration + f->channels - 1;
657
658 /* PVQ minimum allocation threshold, below this value the band is
659 * skipped */
660 508406 threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
661 f->channels << 3);
662
663 508406 trim_offset[i] = trim * (band << scale) >> 6;
664
665
2/2
✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 398758 times.
508406 if (ff_celt_freq_range[i] << f->size == 1)
666 109648 trim_offset[i] -= f->channels << 3;
667 }
668
669 /* Bisection */
670 29890 low = 1;
671 29890 high = CELT_VECTORS - 1;
672
2/2
✓ Branch 0 taken 111627 times.
✓ Branch 1 taken 29890 times.
141517 while (low <= high) {
673 111627 int center = (low + high) >> 1;
674 111627 done = total = 0;
675
676
2/2
✓ Branch 0 taken 1902856 times.
✓ Branch 1 taken 111627 times.
2014483 for (i = f->end_band - 1; i >= f->start_band; i--) {
677 1902856 bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
678
679
2/2
✓ Branch 0 taken 1813016 times.
✓ Branch 1 taken 89840 times.
1902856 if (bandbits)
680 1813016 bandbits = FFMAX(bandbits + trim_offset[i], 0);
681 1902856 bandbits += boost[i];
682
683
4/4
✓ Branch 0 taken 171355 times.
✓ Branch 1 taken 1731501 times.
✓ Branch 2 taken 21091 times.
✓ Branch 3 taken 150264 times.
1902856 if (bandbits >= threshold[i] || done) {
684 1752592 done = 1;
685 1752592 total += FFMIN(bandbits, f->caps[i]);
686
2/2
✓ Branch 0 taken 31509 times.
✓ Branch 1 taken 118755 times.
150264 } else if (bandbits >= f->channels << 3) {
687 31509 total += f->channels << 3;
688 }
689 }
690
691
2/2
✓ Branch 0 taken 39893 times.
✓ Branch 1 taken 71734 times.
111627 if (total > tbits_8ths)
692 39893 high = center - 1;
693 else
694 71734 low = center + 1;
695 }
696 29890 high = low--;
697
698 /* Bisection */
699
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
700 508406 bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]);
701
2/2
✓ Branch 0 taken 37784 times.
✓ Branch 1 taken 470622 times.
508406 bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
702 470622 NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]);
703
704
2/2
✓ Branch 0 taken 465322 times.
✓ Branch 1 taken 43084 times.
508406 if (bits1[i])
705 465322 bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
706
2/2
✓ Branch 0 taken 496514 times.
✓ Branch 1 taken 11892 times.
508406 if (bits2[i])
707 496514 bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
708
709
2/2
✓ Branch 0 taken 486068 times.
✓ Branch 1 taken 22338 times.
508406 if (low)
710 486068 bits1[i] += boost[i];
711 508406 bits2[i] += boost[i];
712
713
2/2
✓ Branch 0 taken 12551 times.
✓ Branch 1 taken 495855 times.
508406 if (boost[i])
714 12551 skip_startband = i;
715 508406 bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
716 }
717
718 /* Bisection */
719 29890 low = 0;
720 29890 high = 1 << CELT_ALLOC_STEPS;
721
2/2
✓ Branch 0 taken 179340 times.
✓ Branch 1 taken 29890 times.
209230 for (i = 0; i < CELT_ALLOC_STEPS; i++) {
722 179340 int center = (low + high) >> 1;
723 179340 done = total = 0;
724
725
2/2
✓ Branch 0 taken 3050436 times.
✓ Branch 1 taken 179340 times.
3229776 for (j = f->end_band - 1; j >= f->start_band; j--) {
726 3050436 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
727
728
4/4
✓ Branch 0 taken 262149 times.
✓ Branch 1 taken 2788287 times.
✓ Branch 2 taken 51935 times.
✓ Branch 3 taken 210214 times.
3050436 if (bandbits >= threshold[j] || done) {
729 2840222 done = 1;
730 2840222 total += FFMIN(bandbits, f->caps[j]);
731
2/2
✓ Branch 0 taken 52455 times.
✓ Branch 1 taken 157759 times.
210214 } else if (bandbits >= f->channels << 3)
732 52455 total += f->channels << 3;
733 }
734
2/2
✓ Branch 0 taken 85635 times.
✓ Branch 1 taken 93705 times.
179340 if (total > tbits_8ths)
735 85635 high = center;
736 else
737 93705 low = center;
738 }
739
740 /* Bisection */
741 29890 done = total = 0;
742
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->end_band - 1; i >= f->start_band; i--) {
743 508406 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
744
745
4/4
✓ Branch 0 taken 45195 times.
✓ Branch 1 taken 463211 times.
✓ Branch 2 taken 9156 times.
✓ Branch 3 taken 36039 times.
508406 if (bandbits >= threshold[i] || done)
746 472367 done = 1;
747 else
748 36039 bandbits = (bandbits >= f->channels << 3) ?
749
2/2
✓ Branch 0 taken 8853 times.
✓ Branch 1 taken 27186 times.
36039 f->channels << 3 : 0;
750
751 508406 bandbits = FFMIN(bandbits, f->caps[i]);
752 508406 f->pulses[i] = bandbits;
753 508406 total += bandbits;
754 }
755
756 /* Band skipping */
757 29890 for (f->coded_bands = f->end_band; ; f->coded_bands--) {
758 int allocation;
759 80616 j = f->coded_bands - 1;
760
761
2/2
✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 78967 times.
80616 if (j == skip_startband) {
762 /* all remaining bands are not skipped */
763 1649 tbits_8ths += skip_bit;
764 1649 break;
765 }
766
767 /* determine the number of bits available for coding "do not skip" markers */
768 78967 remaining = tbits_8ths - total;
769 78967 bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
770 78967 remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
771 78967 allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j];
772 78967 allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
773
774 /* a "do not skip" marker is only coded if the allocation is
775 * above the chosen threshold */
776
2/2
✓ Branch 0 taken 38133 times.
✓ Branch 1 taken 40834 times.
78967 if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
777 int do_not_skip;
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38133 times.
38133 if (encode) {
779 do_not_skip = f->coded_bands <= f->skip_band_floor;
780 ff_opus_rc_enc_log(rc, do_not_skip, 1);
781 } else {
782 38133 do_not_skip = ff_opus_rc_dec_log(rc, 1);
783 }
784
785
2/2
✓ Branch 0 taken 28241 times.
✓ Branch 1 taken 9892 times.
38133 if (do_not_skip)
786 28241 break;
787
788 9892 total += 1 << 3;
789 9892 allocation -= 1 << 3;
790 }
791
792 /* the band is skipped, so reclaim its bits */
793 50726 total -= f->pulses[j];
794
2/2
✓ Branch 0 taken 34531 times.
✓ Branch 1 taken 16195 times.
50726 if (intensitystereo_bit) {
795 34531 total -= intensitystereo_bit;
796 34531 intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
797 34531 total += intensitystereo_bit;
798 }
799
800
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;
801 }
802
803 /* IS start band */
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
29890 if (encode) {
805 if (intensitystereo_bit) {
806 f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
807 ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
808 }
809 } else {
810 29890 f->intensity_stereo = f->dual_stereo = 0;
811
2/2
✓ Branch 0 taken 18672 times.
✓ Branch 1 taken 11218 times.
29890 if (intensitystereo_bit)
812 18672 f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
813 }
814
815 /* DS flag */
816
2/2
✓ Branch 0 taken 13163 times.
✓ Branch 1 taken 16727 times.
29890 if (f->intensity_stereo <= f->start_band)
817 13163 tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
818
1/2
✓ Branch 0 taken 16727 times.
✗ Branch 1 not taken.
16727 else if (dualstereo_bit)
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16727 times.
16727 if (encode)
820 ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
821 else
822 16727 f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
823
824 /* Supply the remaining bits in this frame to lower bands */
825 29890 remaining = tbits_8ths - total;
826 29890 bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
827 29890 remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
828
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 29890 times.
487570 for (i = f->start_band; i < f->coded_bands; i++) {
829 457680 const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
830 457680 f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
831 457680 remaining -= bits;
832 }
833
834 /* Finally determine the allocation */
835
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 29890 times.
487570 for (i = f->start_band; i < f->coded_bands; i++) {
836 457680 int N = ff_celt_freq_range[i] << f->size;
837 457680 int prev_extra = extrabits;
838 457680 f->pulses[i] += extrabits;
839
840
2/2
✓ Branch 0 taken 351129 times.
✓ Branch 1 taken 106551 times.
457680 if (N > 1) {
841 int dof; /* degrees of freedom */
842 int temp; /* dof * channels * log(dof) */
843 int fine_bits;
844 int max_bits;
845 int offset; /* fine energy quantization offset, i.e.
846 * extra bits assigned over the standard
847 * totalbits/dof */
848
849 351129 extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
850 351129 f->pulses[i] -= extrabits;
851
852 /* intensity stereo makes use of an extra degree of freedom */
853
8/8
✓ Branch 0 taken 242587 times.
✓ Branch 1 taken 108542 times.
✓ Branch 2 taken 190250 times.
✓ Branch 3 taken 52337 times.
✓ Branch 4 taken 155966 times.
✓ Branch 5 taken 34284 times.
✓ Branch 6 taken 140074 times.
✓ Branch 7 taken 15892 times.
351129 dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
854 351129 temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
855 351129 offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
856
2/2
✓ Branch 0 taken 83666 times.
✓ Branch 1 taken 267463 times.
351129 if (N == 2) /* dof=2 is the only case that doesn't fit the model */
857 83666 offset += dof << 1;
858
859 /* grant an additional bias for the first and second pulses */
860
2/2
✓ Branch 0 taken 247256 times.
✓ Branch 1 taken 103873 times.
351129 if (f->pulses[i] + offset < 2 * (dof << 3))
861 247256 offset += temp >> 2;
862
2/2
✓ Branch 0 taken 50020 times.
✓ Branch 1 taken 53853 times.
103873 else if (f->pulses[i] + offset < 3 * (dof << 3))
863 50020 offset += temp >> 3;
864
865 351129 fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
866 351129 max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
867 351129 max_bits = FFMAX(max_bits, 0);
868 351129 f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
869
870 /* If fine_bits was rounded down or capped,
871 * give priority for the final fine energy pass */
872 351129 f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
873
874 /* the remaining bits are assigned to PVQ */
875 351129 f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
876 } else {
877 /* all bits go to fine energy except for the sign bit */
878 106551 extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
879 106551 f->pulses[i] -= extrabits;
880 106551 f->fine_bits[i] = 0;
881 106551 f->fine_priority[i] = 1;
882 }
883
884 /* hand back a limited number of extra fine energy bits to this band */
885
2/2
✓ Branch 0 taken 123205 times.
✓ Branch 1 taken 334475 times.
457680 if (extrabits > 0) {
886 123205 int fineextra = FFMIN(extrabits >> (f->channels + 2),
887 CELT_MAX_FINE_BITS - f->fine_bits[i]);
888 123205 f->fine_bits[i] += fineextra;
889
890 123205 fineextra <<= f->channels + 2;
891 123205 f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
892 123205 extrabits -= fineextra;
893 }
894 }
895 29890 f->remaining = extrabits;
896
897 /* skipped bands dedicate all of their bits for fine energy */
898
2/2
✓ Branch 0 taken 50726 times.
✓ Branch 1 taken 29890 times.
80616 for (; i < f->end_band; i++) {
899 50726 f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
900 50726 f->pulses[i] = 0;
901 50726 f->fine_priority[i] = f->fine_bits[i] < 1;
902 }
903 29890 }
904