FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 433 537 80.6%
Branches: 265 353 75.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Opus 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 48872 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
93 int self_delimiting)
94 {
95 48872 const uint8_t *ptr = buf;
96 48872 const uint8_t *end = buf + buf_size;
97 48872 int padding = 0;
98 int frame_bytes, i;
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48872 times.
48872 if (buf_size < 1)
101 goto fail;
102
103 /* TOC byte */
104 48872 i = *ptr++;
105 48872 pkt->code = (i ) & 0x3;
106 48872 pkt->stereo = (i >> 2) & 0x1;
107 48872 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 40672 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8200 times.
48872 if (pkt->code >= 2 && buf_size < 2)
111 goto fail;
112
113
4/5
✓ Branch 0 taken 40032 times.
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 1998 times.
✓ Branch 3 taken 6202 times.
✗ Branch 4 not taken.
48872 switch (pkt->code) {
114 40032 case 0:
115 /* 1 frame */
116 40032 pkt->frame_count = 1;
117 40032 pkt->vbr = 0;
118
119
2/2
✓ Branch 0 taken 6892 times.
✓ Branch 1 taken 33140 times.
40032 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 40032 frame_bytes = end - ptr;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40032 times.
40032 if (frame_bytes > MAX_FRAME_SIZE)
129 goto fail;
130 40032 pkt->frame_offset[0] = ptr - buf;
131 40032 pkt->frame_size[0] = frame_bytes;
132 40032 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 48872 pkt->packet_size = buf_size;
254 48872 pkt->data_size = pkt->packet_size - padding;
255
256 /* total packet duration cannot be larger than 120ms */
257 48872 pkt->frame_duration = opus_frame_duration[pkt->config];
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48872 times.
48872 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 9060 times.
✓ Branch 1 taken 39812 times.
48872 if (pkt->config < 12) {
263 9060 pkt->mode = OPUS_MODE_SILK;
264 9060 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 48872 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 147 static int channel_reorder_unknown(int nb_channels, int channel_idx)
289 {
290 147 return channel_idx;
291 }
292
293 76 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 76 int (*channel_reorder)(int, int) = channel_reorder_unknown;
299 76 int channels = avctx->ch_layout.nb_channels;
300
301 const uint8_t *extradata, *channel_map;
302 int extradata_size;
303 int version, map_type, streams, stereo_streams, i, j, ret;
304 76 AVChannelLayout layout = { 0 };
305
306
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 75 times.
76 if (!avctx->extradata) {
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (channels > 2) {
308 av_log(avctx, AV_LOG_ERROR,
309 "Multichannel configuration without extradata.\n");
310 return AVERROR(EINVAL);
311 }
312 1 extradata = opus_default_extradata;
313 1 extradata_size = sizeof(opus_default_extradata);
314 } else {
315 75 extradata = avctx->extradata;
316 75 extradata_size = avctx->extradata_size;
317 }
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (extradata_size < 19) {
320 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
321 extradata_size);
322 return AVERROR_INVALIDDATA;
323 }
324
325 76 version = extradata[8];
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (version > 15) {
327 avpriv_request_sample(avctx, "Extradata version %d", version);
328 return AVERROR_PATCHWELCOME;
329 }
330
331 76 avctx->delay = AV_RL16(extradata + 10);
332
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 28 times.
76 if (avctx->internal)
333 48 avctx->internal->skip_samples = avctx->delay;
334
335
3/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
76 channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!channels) {
337 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
338 return AVERROR_INVALIDDATA;
339 }
340
341 76 s->gain_i = AV_RL16(extradata + 16);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (s->gain_i)
343 s->gain = ff_exp10(s->gain_i / (20.0 * 256));
344
345 76 map_type = extradata[18];
346
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7 times.
76 if (!map_type) {
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (channels > 2) {
348 av_log(avctx, AV_LOG_ERROR,
349 "Channel mapping 0 is only specified for up to 2 channels\n");
350 ret = AVERROR_INVALIDDATA;
351 goto fail;
352 }
353
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 39 times.
69 layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
354 (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
355 69 streams = 1;
356 69 stereo_streams = channels - 1;
357 69 channel_map = default_channel_map;
358
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) {
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (extradata_size < 21 + channels) {
360 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
361 extradata_size);
362 ret = AVERROR_INVALIDDATA;
363 goto fail;
364 }
365
366 7 streams = extradata[19];
367 7 stereo_streams = extradata[20];
368
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 ||
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 streams + stereo_streams > 255) {
370 av_log(avctx, AV_LOG_ERROR,
371 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
372 ret = AVERROR_INVALIDDATA;
373 goto fail;
374 }
375
376
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (map_type == 1) {
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (channels > 8) {
378 av_log(avctx, AV_LOG_ERROR,
379 "Channel mapping 1 is only specified for up to 8 channels\n");
380 ret = AVERROR_INVALIDDATA;
381 goto fail;
382 }
383 7 av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]);
384 7 channel_reorder = channel_reorder_vorbis;
385 } else if (map_type == 2) {
386 int ambisonic_order = ff_sqrt(channels) - 1;
387 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
388 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
389 av_log(avctx, AV_LOG_ERROR,
390 "Channel mapping 2 is only specified for channel counts"
391 " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
392 " for nonnegative integer n\n");
393 ret = AVERROR_INVALIDDATA;
394 goto fail;
395 }
396 if (channels > 227) {
397 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
398 ret = AVERROR_INVALIDDATA;
399 goto fail;
400 }
401
402 layout.order = AV_CHANNEL_ORDER_AMBISONIC;
403 layout.nb_channels = channels;
404 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
405 layout.u.mask = AV_CH_LAYOUT_STEREO;
406 } else {
407 layout.order = AV_CHANNEL_ORDER_UNSPEC;
408 layout.nb_channels = channels;
409 }
410
411 7 channel_map = extradata + 21;
412 } else {
413 avpriv_request_sample(avctx, "Mapping type %d", map_type);
414 return AVERROR_PATCHWELCOME;
415 }
416
417 76 s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!s->channel_maps) {
419 ret = AVERROR(ENOMEM);
420 goto fail;
421 }
422
423
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 76 times.
234 for (i = 0; i < channels; i++) {
424 158 ChannelMap *map = &s->channel_maps[i];
425 158 uint8_t idx = channel_map[channel_reorder(channels, i)];
426
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (idx == 255) {
428 map->silence = 1;
429 continue;
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 } else if (idx >= streams + stereo_streams) {
431 av_log(avctx, AV_LOG_ERROR,
432 "Invalid channel map for output channel %d: %d\n", i, idx);
433 av_freep(&s->channel_maps);
434 ret = AVERROR_INVALIDDATA;
435 goto fail;
436 }
437
438 /* check that we did not see this index yet */
439 158 map->copy = 0;
440
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 158 times.
354 for (j = 0; j < i; j++)
441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 196 times.
196 if (channel_map[channel_reorder(channels, j)] == idx) {
442 map->copy = 1;
443 map->copy_idx = j;
444 break;
445 }
446
447
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 44 times.
158 if (idx < 2 * stereo_streams) {
448 114 map->stream_idx = idx / 2;
449 114 map->channel_idx = idx & 1;
450 } else {
451 44 map->stream_idx = idx - stereo_streams;
452 44 map->channel_idx = 0;
453 }
454 }
455
456 76 ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (ret < 0)
458 goto fail;
459
460 76 s->nb_streams = streams;
461 76 s->nb_stereo_streams = stereo_streams;
462
463 76 return 0;
464 fail:
465 av_channel_layout_uninit(&layout);
466 return ret;
467 }
468
469 29890 void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
470 {
471 float lowband_scratch[8 * 22];
472 float norm1[2 * 8 * 100];
473 29890 float *norm2 = norm1 + 8 * 100;
474
475 29890 int totalbits = (f->framebits << 3) - f->anticollapse_needed;
476
477 29890 int update_lowband = 1;
478 29890 int lowband_offset = 0;
479
480 int i, j;
481
482
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
483 508406 uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
484 508406 int band_offset = ff_celt_freq_bands[i] << f->size;
485 508406 int band_size = ff_celt_freq_range[i] << f->size;
486 508406 float *X = f->block[0].coeffs + band_offset;
487
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;
488 float *norm_loc1, *norm_loc2;
489
490 508406 int consumed = opus_rc_tell_frac(rc);
491 508406 int effective_lowband = -1;
492 508406 int b = 0;
493
494 /* Compute how many bits we want to allocate to this band */
495
2/2
✓ Branch 0 taken 478516 times.
✓ Branch 1 taken 29890 times.
508406 if (i != f->start_band)
496 478516 f->remaining -= consumed;
497 508406 f->remaining2 = totalbits - consumed - 1;
498
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 50726 times.
508406 if (i <= f->coded_bands - 1) {
499 457680 int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
500
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);
501 }
502
503
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] ||
504
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))
505 387435 lowband_offset = i;
506
507
2/2
✓ Branch 0 taken 29890 times.
✓ Branch 1 taken 478516 times.
508406 if (i == f->start_band + 1) {
508 /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
509 the second to ensure the second band never has to use the LCG. */
510 29890 int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
511
512 29890 memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
513
514
2/2
✓ Branch 0 taken 18744 times.
✓ Branch 1 taken 11146 times.
29890 if (f->channels == 2)
515 18744 memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
516 }
517
518 /* Get a conservative estimate of the collapse_mask's for the bands we're
519 going to be folding from. */
520
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 ||
521
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)) {
522 int foldstart, foldend;
523
524 /* This ensures we never repeat spectral content within one band */
525 419243 effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
526 ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
527 419243 foldstart = lowband_offset;
528
2/2
✓ Branch 0 taken 197958 times.
✓ Branch 1 taken 419243 times.
617201 while (ff_celt_freq_bands[--foldstart] > effective_lowband);
529 419243 foldend = lowband_offset - 1;
530
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]);
531
532 419243 cm[0] = cm[1] = 0;
533
2/2
✓ Branch 0 taken 646167 times.
✓ Branch 1 taken 419243 times.
1065410 for (j = foldstart; j < foldend; j++) {
534 646167 cm[0] |= f->block[0].collapse_masks[j];
535 646167 cm[1] |= f->block[f->channels - 1].collapse_masks[j];
536 }
537 }
538
539
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) {
540 /* Switch off dual stereo to do intensity */
541 1674 f->dual_stereo = 0;
542
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++)
543 570798 norm1[j] = (norm1[j] + norm2[j]) / 2;
544 }
545
546
2/2
✓ Branch 0 taken 419243 times.
✓ Branch 1 taken 89163 times.
508406 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
547
2/2
✓ Branch 0 taken 419243 times.
✓ Branch 1 taken 89163 times.
508406 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
548
549
2/2
✓ Branch 0 taken 35152 times.
✓ Branch 1 taken 473254 times.
508406 if (f->dual_stereo) {
550 70304 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
551 35152 f->blocks, norm_loc1, f->size,
552 35152 norm1 + band_offset, 0, 1.0f,
553 35152 lowband_scratch, cm[0]);
554
555 35152 cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
556 35152 f->blocks, norm_loc2, f->size,
557 35152 norm2 + band_offset, 0, 1.0f,
558 35152 lowband_scratch, cm[1]);
559 } else {
560 946508 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0,
561 473254 f->blocks, norm_loc1, f->size,
562 473254 norm1 + band_offset, 0, 1.0f,
563 473254 lowband_scratch, cm[0] | cm[1]);
564 473254 cm[1] = cm[0];
565 }
566
567 508406 f->block[0].collapse_masks[i] = (uint8_t)cm[0];
568 508406 f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
569 508406 f->remaining += f->pulses[i] + consumed;
570
571 /* Update the folding position only as long as we have 1 bit/sample depth */
572 508406 update_lowband = (b > band_size << 3);
573 }
574 29890 }
575
576 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
577
578 29890 void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
579 {
580 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
581 29890 int skip_startband = f->start_band;
582 29890 int skip_bit = 0;
583 29890 int intensitystereo_bit = 0;
584 29890 int dualstereo_bit = 0;
585 29890 int dynalloc = 6;
586 29890 int extrabits = 0;
587
588 29890 int boost[CELT_MAX_BANDS] = { 0 };
589 int trim_offset[CELT_MAX_BANDS];
590 int threshold[CELT_MAX_BANDS];
591 int bits1[CELT_MAX_BANDS];
592 int bits2[CELT_MAX_BANDS];
593
594 /* Spread */
595
2/2
✓ Branch 1 taken 29755 times.
✓ Branch 2 taken 135 times.
29890 if (opus_rc_tell(rc) + 4 <= f->framebits) {
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29755 times.
29755 if (encode)
597 ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread);
598 else
599 29755 f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
600 } else {
601 135 f->spread = CELT_SPREAD_NORMAL;
602 }
603
604 /* Initialize static allocation caps */
605
2/2
✓ Branch 0 taken 627690 times.
✓ Branch 1 taken 29890 times.
657580 for (i = 0; i < CELT_MAX_BANDS; i++)
606 627690 f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
607
608 /* Band boosts */
609 29890 tbits_8ths = f->framebits << 3;
610
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
611 508406 int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
612 508406 int b_dynalloc = dynalloc;
613 508406 int boost_amount = f->alloc_boost[i];
614 508406 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
615
616
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]) {
617 int is_boost;
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 522361 times.
522361 if (encode) {
619 is_boost = boost_amount--;
620 ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
621 } else {
622 522361 is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
623 }
624
625
2/2
✓ Branch 0 taken 505895 times.
✓ Branch 1 taken 16466 times.
522361 if (!is_boost)
626 505895 break;
627
628 16466 boost[i] += quanta;
629 16466 tbits_8ths -= quanta;
630
631 16466 b_dynalloc = 1;
632 }
633
634
2/2
✓ Branch 0 taken 12551 times.
✓ Branch 1 taken 495855 times.
508406 if (boost[i])
635 12551 dynalloc = FFMAX(dynalloc - 1, 2);
636 }
637
638 /* Allocation trim */
639
1/2
✓ Branch 0 taken 29890 times.
✗ Branch 1 not taken.
29890 if (!encode)
640 29890 f->alloc_trim = 5;
641
2/2
✓ Branch 1 taken 29751 times.
✓ Branch 2 taken 139 times.
29890 if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29751 times.
29751 if (encode)
643 ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim);
644 else
645 29751 f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
646
647 /* Anti-collapse bit reservation */
648 29890 tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
649 29890 f->anticollapse_needed = 0;
650
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))
651 3130 f->anticollapse_needed = 1 << 3;
652 29890 tbits_8ths -= f->anticollapse_needed;
653
654 /* Band skip bit reservation */
655
2/2
✓ Branch 0 taken 29757 times.
✓ Branch 1 taken 133 times.
29890 if (tbits_8ths >= 1 << 3)
656 29757 skip_bit = 1 << 3;
657 29890 tbits_8ths -= skip_bit;
658
659 /* Intensity/dual stereo bit reservation */
660
2/2
✓ Branch 0 taken 18744 times.
✓ Branch 1 taken 11146 times.
29890 if (f->channels == 2) {
661 18744 intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
662
2/2
✓ Branch 0 taken 18672 times.
✓ Branch 1 taken 72 times.
18744 if (intensitystereo_bit <= tbits_8ths) {
663 18672 tbits_8ths -= intensitystereo_bit;
664
2/2
✓ Branch 0 taken 18668 times.
✓ Branch 1 taken 4 times.
18672 if (tbits_8ths >= 1 << 3) {
665 18668 dualstereo_bit = 1 << 3;
666 18668 tbits_8ths -= 1 << 3;
667 }
668 } else {
669 72 intensitystereo_bit = 0;
670 }
671 }
672
673 /* Trim offsets */
674
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
675 508406 int trim = f->alloc_trim - 5 - f->size;
676 508406 int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
677 508406 int duration = f->size + 3;
678 508406 int scale = duration + f->channels - 1;
679
680 /* PVQ minimum allocation threshold, below this value the band is
681 * skipped */
682 508406 threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
683 f->channels << 3);
684
685 508406 trim_offset[i] = trim * (band << scale) >> 6;
686
687
2/2
✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 398758 times.
508406 if (ff_celt_freq_range[i] << f->size == 1)
688 109648 trim_offset[i] -= f->channels << 3;
689 }
690
691 /* Bisection */
692 29890 low = 1;
693 29890 high = CELT_VECTORS - 1;
694
2/2
✓ Branch 0 taken 111627 times.
✓ Branch 1 taken 29890 times.
141517 while (low <= high) {
695 111627 int center = (low + high) >> 1;
696 111627 done = total = 0;
697
698
2/2
✓ Branch 0 taken 1902856 times.
✓ Branch 1 taken 111627 times.
2014483 for (i = f->end_band - 1; i >= f->start_band; i--) {
699 1902856 bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
700
701
2/2
✓ Branch 0 taken 1813016 times.
✓ Branch 1 taken 89840 times.
1902856 if (bandbits)
702 1813016 bandbits = FFMAX(bandbits + trim_offset[i], 0);
703 1902856 bandbits += boost[i];
704
705
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) {
706 1752592 done = 1;
707 1752592 total += FFMIN(bandbits, f->caps[i]);
708
2/2
✓ Branch 0 taken 31509 times.
✓ Branch 1 taken 118755 times.
150264 } else if (bandbits >= f->channels << 3) {
709 31509 total += f->channels << 3;
710 }
711 }
712
713
2/2
✓ Branch 0 taken 39893 times.
✓ Branch 1 taken 71734 times.
111627 if (total > tbits_8ths)
714 39893 high = center - 1;
715 else
716 71734 low = center + 1;
717 }
718 29890 high = low--;
719
720 /* Bisection */
721
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->start_band; i < f->end_band; i++) {
722 508406 bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]);
723
2/2
✓ Branch 0 taken 37784 times.
✓ Branch 1 taken 470622 times.
508406 bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
724 470622 NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]);
725
726
2/2
✓ Branch 0 taken 465322 times.
✓ Branch 1 taken 43084 times.
508406 if (bits1[i])
727 465322 bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
728
2/2
✓ Branch 0 taken 496514 times.
✓ Branch 1 taken 11892 times.
508406 if (bits2[i])
729 496514 bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
730
731
2/2
✓ Branch 0 taken 486068 times.
✓ Branch 1 taken 22338 times.
508406 if (low)
732 486068 bits1[i] += boost[i];
733 508406 bits2[i] += boost[i];
734
735
2/2
✓ Branch 0 taken 12551 times.
✓ Branch 1 taken 495855 times.
508406 if (boost[i])
736 12551 skip_startband = i;
737 508406 bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
738 }
739
740 /* Bisection */
741 29890 low = 0;
742 29890 high = 1 << CELT_ALLOC_STEPS;
743
2/2
✓ Branch 0 taken 179340 times.
✓ Branch 1 taken 29890 times.
209230 for (i = 0; i < CELT_ALLOC_STEPS; i++) {
744 179340 int center = (low + high) >> 1;
745 179340 done = total = 0;
746
747
2/2
✓ Branch 0 taken 3050436 times.
✓ Branch 1 taken 179340 times.
3229776 for (j = f->end_band - 1; j >= f->start_band; j--) {
748 3050436 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
749
750
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) {
751 2840222 done = 1;
752 2840222 total += FFMIN(bandbits, f->caps[j]);
753
2/2
✓ Branch 0 taken 52455 times.
✓ Branch 1 taken 157759 times.
210214 } else if (bandbits >= f->channels << 3)
754 52455 total += f->channels << 3;
755 }
756
2/2
✓ Branch 0 taken 85635 times.
✓ Branch 1 taken 93705 times.
179340 if (total > tbits_8ths)
757 85635 high = center;
758 else
759 93705 low = center;
760 }
761
762 /* Bisection */
763 29890 done = total = 0;
764
2/2
✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
538296 for (i = f->end_band - 1; i >= f->start_band; i--) {
765 508406 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
766
767
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)
768 472367 done = 1;
769 else
770 36039 bandbits = (bandbits >= f->channels << 3) ?
771
2/2
✓ Branch 0 taken 8853 times.
✓ Branch 1 taken 27186 times.
36039 f->channels << 3 : 0;
772
773 508406 bandbits = FFMIN(bandbits, f->caps[i]);
774 508406 f->pulses[i] = bandbits;
775 508406 total += bandbits;
776 }
777
778 /* Band skipping */
779 29890 for (f->coded_bands = f->end_band; ; f->coded_bands--) {
780 int allocation;
781 80616 j = f->coded_bands - 1;
782
783
2/2
✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 78967 times.
80616 if (j == skip_startband) {
784 /* all remaining bands are not skipped */
785 1649 tbits_8ths += skip_bit;
786 1649 break;
787 }
788
789 /* determine the number of bits available for coding "do not skip" markers */
790 78967 remaining = tbits_8ths - total;
791 78967 bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
792 78967 remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
793 78967 allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j];
794 78967 allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
795
796 /* a "do not skip" marker is only coded if the allocation is
797 * above the chosen threshold */
798
2/2
✓ Branch 0 taken 38133 times.
✓ Branch 1 taken 40834 times.
78967 if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
799 int do_not_skip;
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38133 times.
38133 if (encode) {
801 do_not_skip = f->coded_bands <= f->skip_band_floor;
802 ff_opus_rc_enc_log(rc, do_not_skip, 1);
803 } else {
804 38133 do_not_skip = ff_opus_rc_dec_log(rc, 1);
805 }
806
807
2/2
✓ Branch 0 taken 28241 times.
✓ Branch 1 taken 9892 times.
38133 if (do_not_skip)
808 28241 break;
809
810 9892 total += 1 << 3;
811 9892 allocation -= 1 << 3;
812 }
813
814 /* the band is skipped, so reclaim its bits */
815 50726 total -= f->pulses[j];
816
2/2
✓ Branch 0 taken 34531 times.
✓ Branch 1 taken 16195 times.
50726 if (intensitystereo_bit) {
817 34531 total -= intensitystereo_bit;
818 34531 intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
819 34531 total += intensitystereo_bit;
820 }
821
822
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;
823 }
824
825 /* IS start band */
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
29890 if (encode) {
827 if (intensitystereo_bit) {
828 f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
829 ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
830 }
831 } else {
832 29890 f->intensity_stereo = f->dual_stereo = 0;
833
2/2
✓ Branch 0 taken 18672 times.
✓ Branch 1 taken 11218 times.
29890 if (intensitystereo_bit)
834 18672 f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
835 }
836
837 /* DS flag */
838
2/2
✓ Branch 0 taken 13163 times.
✓ Branch 1 taken 16727 times.
29890 if (f->intensity_stereo <= f->start_band)
839 13163 tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
840
1/2
✓ Branch 0 taken 16727 times.
✗ Branch 1 not taken.
16727 else if (dualstereo_bit)
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16727 times.
16727 if (encode)
842 ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
843 else
844 16727 f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
845
846 /* Supply the remaining bits in this frame to lower bands */
847 29890 remaining = tbits_8ths - total;
848 29890 bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
849 29890 remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
850
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 29890 times.
487570 for (i = f->start_band; i < f->coded_bands; i++) {
851 457680 const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
852 457680 f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
853 457680 remaining -= bits;
854 }
855
856 /* Finally determine the allocation */
857
2/2
✓ Branch 0 taken 457680 times.
✓ Branch 1 taken 29890 times.
487570 for (i = f->start_band; i < f->coded_bands; i++) {
858 457680 int N = ff_celt_freq_range[i] << f->size;
859 457680 int prev_extra = extrabits;
860 457680 f->pulses[i] += extrabits;
861
862
2/2
✓ Branch 0 taken 351129 times.
✓ Branch 1 taken 106551 times.
457680 if (N > 1) {
863 int dof; /* degrees of freedom */
864 int temp; /* dof * channels * log(dof) */
865 int fine_bits;
866 int max_bits;
867 int offset; /* fine energy quantization offset, i.e.
868 * extra bits assigned over the standard
869 * totalbits/dof */
870
871 351129 extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
872 351129 f->pulses[i] -= extrabits;
873
874 /* intensity stereo makes use of an extra degree of freedom */
875
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);
876 351129 temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
877 351129 offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
878
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 */
879 83666 offset += dof << 1;
880
881 /* grant an additional bias for the first and second pulses */
882
2/2
✓ Branch 0 taken 247256 times.
✓ Branch 1 taken 103873 times.
351129 if (f->pulses[i] + offset < 2 * (dof << 3))
883 247256 offset += temp >> 2;
884
2/2
✓ Branch 0 taken 50020 times.
✓ Branch 1 taken 53853 times.
103873 else if (f->pulses[i] + offset < 3 * (dof << 3))
885 50020 offset += temp >> 3;
886
887 351129 fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
888 351129 max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
889 351129 max_bits = FFMAX(max_bits, 0);
890 351129 f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
891
892 /* If fine_bits was rounded down or capped,
893 * give priority for the final fine energy pass */
894 351129 f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
895
896 /* the remaining bits are assigned to PVQ */
897 351129 f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
898 } else {
899 /* all bits go to fine energy except for the sign bit */
900 106551 extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
901 106551 f->pulses[i] -= extrabits;
902 106551 f->fine_bits[i] = 0;
903 106551 f->fine_priority[i] = 1;
904 }
905
906 /* hand back a limited number of extra fine energy bits to this band */
907
2/2
✓ Branch 0 taken 123205 times.
✓ Branch 1 taken 334475 times.
457680 if (extrabits > 0) {
908 123205 int fineextra = FFMIN(extrabits >> (f->channels + 2),
909 CELT_MAX_FINE_BITS - f->fine_bits[i]);
910 123205 f->fine_bits[i] += fineextra;
911
912 123205 fineextra <<= f->channels + 2;
913 123205 f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
914 123205 extrabits -= fineextra;
915 }
916 }
917 29890 f->remaining = extrabits;
918
919 /* skipped bands dedicate all of their bits for fine energy */
920
2/2
✓ Branch 0 taken 50726 times.
✓ Branch 1 taken 29890 times.
80616 for (; i < f->end_band; i++) {
921 50726 f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
922 50726 f->pulses[i] = 0;
923 50726 f->fine_priority[i] = f->fine_bits[i] < 1;
924 }
925 29890 }
926