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