FFmpeg
Loading...
Searching...
No Matches
celt.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdint.h>
23
24#include "celt.h"
25#include "pvq.h"
26#include "tab.h"
27
29{
30 float lowband_scratch[8 * 22];
31 float norm1[2 * 8 * 100];
32 float *norm2 = norm1 + 8 * 100;
33
34 int totalbits = (f->framebits << 3) - f->anticollapse_needed;
35
36 int update_lowband = 1;
37 int lowband_offset = 0;
38
39 int i, j;
40
41 for (i = f->start_band; i < f->end_band; i++) {
42 uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
43 int band_offset = ff_celt_freq_bands[i] << f->size;
44 int band_size = ff_celt_freq_range[i] << f->size;
45 float *X = f->block[0].coeffs + band_offset;
46 float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
47 float *norm_loc1, *norm_loc2;
48
49 int consumed = opus_rc_tell_frac(rc);
50 int effective_lowband = -1;
51 int b = 0;
52
53 /* Compute how many bits we want to allocate to this band */
54 if (i != f->start_band)
55 f->remaining -= consumed;
56 f->remaining2 = totalbits - consumed - 1;
57 if (i <= f->coded_bands - 1) {
58 int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
59 b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
60 }
61
63 i == f->start_band + 1) && (update_lowband || lowband_offset == 0))
64 lowband_offset = i;
65
66 if (i == f->start_band + 1) {
67 /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
68 the second to ensure the second band never has to use the LCG. */
69 int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
70
71 memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
72
73 if (f->channels == 2)
74 memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
75 }
76
77 /* Get a conservative estimate of the collapse_mask's for the bands we're
78 going to be folding from. */
79 if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
80 f->blocks > 1 || f->tf_change[i] < 0)) {
81 int foldstart, foldend;
82
83 /* This ensures we never repeat spectral content within one band */
84 effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
85 ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
86 foldstart = lowband_offset;
87 while (ff_celt_freq_bands[--foldstart] > effective_lowband);
88 foldend = lowband_offset - 1;
89 while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]);
90
91 cm[0] = cm[1] = 0;
92 for (j = foldstart; j < foldend; j++) {
93 cm[0] |= f->block[0].collapse_masks[j];
94 cm[1] |= f->block[f->channels - 1].collapse_masks[j];
95 }
96 }
97
98 if (f->dual_stereo && i == f->intensity_stereo) {
99 /* Switch off dual stereo to do intensity */
100 f->dual_stereo = 0;
101 for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
102 norm1[j] = (norm1[j] + norm2[j]) / 2;
103 }
104
105 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
106 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
107
108 if (f->dual_stereo) {
109 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
110 f->blocks, norm_loc1, f->size,
111 norm1 + band_offset, 0, 1.0f,
112 lowband_scratch, cm[0]);
113
114 cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
115 f->blocks, norm_loc2, f->size,
116 norm2 + band_offset, 0, 1.0f,
117 lowband_scratch, cm[1]);
118 } else {
119 cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0,
120 f->blocks, norm_loc1, f->size,
121 norm1 + band_offset, 0, 1.0f,
122 lowband_scratch, cm[0] | cm[1]);
123 cm[1] = cm[0];
124 }
125
126 f->block[0].collapse_masks[i] = (uint8_t)cm[0];
127 f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
128 f->remaining += f->pulses[i] + consumed;
129
130 /* Update the folding position only as long as we have 1 bit/sample depth */
131 update_lowband = (b > band_size << 3);
132 }
133}
134
135#define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
136
138{
139 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
140 int skip_startband = f->start_band;
141 int skip_bit = 0;
142 int intensitystereo_bit = 0;
143 int dualstereo_bit = 0;
144 int dynalloc = 6;
145 int extrabits = 0;
146
147 int boost[CELT_MAX_BANDS] = { 0 };
148 int trim_offset[CELT_MAX_BANDS];
149 int threshold[CELT_MAX_BANDS];
152
153 /* Spread */
154 if (opus_rc_tell(rc) + 4 <= f->framebits) {
155 if (encode)
157 else
159 } else {
160 f->spread = CELT_SPREAD_NORMAL;
161 }
162
163 /* Initialize static allocation caps */
164 for (i = 0; i < CELT_MAX_BANDS; i++)
165 f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
166
167 /* Band boosts */
168 tbits_8ths = f->framebits << 3;
169 for (i = f->start_band; i < f->end_band; i++) {
170 int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
171 int b_dynalloc = dynalloc;
172 int boost_amount = f->alloc_boost[i];
173 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
174
175 while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) {
176 int is_boost;
177 if (encode) {
178 is_boost = boost_amount--;
179 ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
180 } else {
181 is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
182 }
183
184 if (!is_boost)
185 break;
186
187 boost[i] += quanta;
188 tbits_8ths -= quanta;
189
190 b_dynalloc = 1;
191 }
192
193 if (boost[i])
194 dynalloc = FFMAX(dynalloc - 1, 2);
195 }
196
197 /* Allocation trim */
198 if (!encode)
199 f->alloc_trim = 5;
200 if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
201 if (encode)
203 else
205
206 /* Anti-collapse bit reservation */
207 tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
208 f->anticollapse_needed = 0;
209 if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
210 f->anticollapse_needed = 1 << 3;
211 tbits_8ths -= f->anticollapse_needed;
212
213 /* Band skip bit reservation */
214 if (tbits_8ths >= 1 << 3)
215 skip_bit = 1 << 3;
216 tbits_8ths -= skip_bit;
217
218 /* Intensity/dual stereo bit reservation */
219 if (f->channels == 2) {
220 intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
221 if (intensitystereo_bit <= tbits_8ths) {
222 tbits_8ths -= intensitystereo_bit;
223 if (tbits_8ths >= 1 << 3) {
224 dualstereo_bit = 1 << 3;
225 tbits_8ths -= 1 << 3;
226 }
227 } else {
228 intensitystereo_bit = 0;
229 }
230 }
231
232 /* Trim offsets */
233 for (i = f->start_band; i < f->end_band; i++) {
234 int trim = f->alloc_trim - 5 - f->size;
235 int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
236 int duration = f->size + 3;
237 int scale = duration + f->channels - 1;
238
239 /* PVQ minimum allocation threshold, below this value the band is
240 * skipped */
241 threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
242 f->channels << 3);
243
244 trim_offset[i] = trim * (band << scale) >> 6;
245
246 if (ff_celt_freq_range[i] << f->size == 1)
247 trim_offset[i] -= f->channels << 3;
248 }
249
250 /* Bisection */
251 low = 1;
252 high = CELT_VECTORS - 1;
253 while (low <= high) {
254 int center = (low + high) >> 1;
255 done = total = 0;
256
257 for (i = f->end_band - 1; i >= f->start_band; i--) {
258 bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
259
260 if (bandbits)
261 bandbits = FFMAX(bandbits + trim_offset[i], 0);
262 bandbits += boost[i];
263
264 if (bandbits >= threshold[i] || done) {
265 done = 1;
266 total += FFMIN(bandbits, f->caps[i]);
267 } else if (bandbits >= f->channels << 3) {
268 total += f->channels << 3;
269 }
270 }
271
272 if (total > tbits_8ths)
273 high = center - 1;
274 else
275 low = center + 1;
276 }
277 high = low--;
278
279 /* Bisection */
280 for (i = f->start_band; i < f->end_band; i++) {
281 bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]);
282 bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
283 NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]);
284
285 if (bits1[i])
286 bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
287 if (bits2[i])
288 bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
289
290 if (low)
291 bits1[i] += boost[i];
292 bits2[i] += boost[i];
293
294 if (boost[i])
295 skip_startband = i;
296 bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
297 }
298
299 /* Bisection */
300 low = 0;
301 high = 1 << CELT_ALLOC_STEPS;
302 for (i = 0; i < CELT_ALLOC_STEPS; i++) {
303 int center = (low + high) >> 1;
304 done = total = 0;
305
306 for (j = f->end_band - 1; j >= f->start_band; j--) {
307 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
308
309 if (bandbits >= threshold[j] || done) {
310 done = 1;
311 total += FFMIN(bandbits, f->caps[j]);
312 } else if (bandbits >= f->channels << 3)
313 total += f->channels << 3;
314 }
315 if (total > tbits_8ths)
316 high = center;
317 else
318 low = center;
319 }
320
321 /* Bisection */
322 done = total = 0;
323 for (i = f->end_band - 1; i >= f->start_band; i--) {
324 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
325
326 if (bandbits >= threshold[i] || done)
327 done = 1;
328 else
329 bandbits = (bandbits >= f->channels << 3) ?
330 f->channels << 3 : 0;
331
332 bandbits = FFMIN(bandbits, f->caps[i]);
333 f->pulses[i] = bandbits;
334 total += bandbits;
335 }
336
337 /* Band skipping */
338 for (f->coded_bands = f->end_band; ; f->coded_bands--) {
339 int allocation;
340 j = f->coded_bands - 1;
341
342 if (j == skip_startband) {
343 /* all remaining bands are not skipped */
344 tbits_8ths += skip_bit;
345 break;
346 }
347
348 /* determine the number of bits available for coding "do not skip" markers */
349 remaining = tbits_8ths - total;
350 bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
351 remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
352 allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j];
353 allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
354
355 /* a "do not skip" marker is only coded if the allocation is
356 * above the chosen threshold */
357 if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
358 int do_not_skip;
359 if (encode) {
360 do_not_skip = f->coded_bands <= f->skip_band_floor;
361 ff_opus_rc_enc_log(rc, do_not_skip, 1);
362 } else {
363 do_not_skip = ff_opus_rc_dec_log(rc, 1);
364 }
365
366 if (do_not_skip)
367 break;
368
369 total += 1 << 3;
370 allocation -= 1 << 3;
371 }
372
373 /* the band is skipped, so reclaim its bits */
374 total -= f->pulses[j];
375 if (intensitystereo_bit) {
376 total -= intensitystereo_bit;
377 intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
378 total += intensitystereo_bit;
379 }
380
381 total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0;
382 }
383
384 /* IS start band */
385 if (encode) {
386 if (intensitystereo_bit) {
387 f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
388 ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
389 }
390 } else {
391 f->intensity_stereo = f->dual_stereo = 0;
392 if (intensitystereo_bit)
393 f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
394 }
395
396 /* DS flag */
397 if (f->intensity_stereo <= f->start_band)
398 tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
399 else if (dualstereo_bit)
400 if (encode)
401 ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
402 else
403 f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
404
405 /* Supply the remaining bits in this frame to lower bands */
406 remaining = tbits_8ths - total;
407 bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
408 remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
409 for (i = f->start_band; i < f->coded_bands; i++) {
410 const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
411 f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
412 remaining -= bits;
413 }
414
415 /* Finally determine the allocation */
416 for (i = f->start_band; i < f->coded_bands; i++) {
417 int N = ff_celt_freq_range[i] << f->size;
418 int prev_extra = extrabits;
419 f->pulses[i] += extrabits;
420
421 if (N > 1) {
422 int dof; /* degrees of freedom */
423 int temp; /* dof * channels * log(dof) */
424 int fine_bits;
425 int max_bits;
426 int offset; /* fine energy quantization offset, i.e.
427 * extra bits assigned over the standard
428 * totalbits/dof */
429
430 extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
431 f->pulses[i] -= extrabits;
432
433 /* intensity stereo makes use of an extra degree of freedom */
434 dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
435 temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
436 offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
437 if (N == 2) /* dof=2 is the only case that doesn't fit the model */
438 offset += dof << 1;
439
440 /* grant an additional bias for the first and second pulses */
441 if (f->pulses[i] + offset < 2 * (dof << 3))
442 offset += temp >> 2;
443 else if (f->pulses[i] + offset < 3 * (dof << 3))
444 offset += temp >> 3;
445
446 fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
447 max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
448 max_bits = FFMAX(max_bits, 0);
449 f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
450
451 /* If fine_bits was rounded down or capped,
452 * give priority for the final fine energy pass */
453 f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
454
455 /* the remaining bits are assigned to PVQ */
456 f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
457 } else {
458 /* all bits go to fine energy except for the sign bit */
459 extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
460 f->pulses[i] -= extrabits;
461 f->fine_bits[i] = 0;
462 f->fine_priority[i] = 1;
463 }
464
465 /* hand back a limited number of extra fine energy bits to this band */
466 if (extrabits > 0) {
467 int fineextra = FFMIN(extrabits >> (f->channels + 2),
468 CELT_MAX_FINE_BITS - f->fine_bits[i]);
469 f->fine_bits[i] += fineextra;
470
471 fineextra <<= f->channels + 2;
472 f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
473 extrabits -= fineextra;
474 }
475 }
476 f->remaining = extrabits;
477
478 /* skipped bands dedicate all of their bits for fine energy */
479 for (; i < f->end_band; i++) {
480 f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
481 f->pulses[i] = 0;
482 f->fine_priority[i] = f->fine_bits[i] < 1;
483 }
484}
static const uint8_t bits2[81]
Definition aactab.c:248
static const uint8_t bits1[81]
Definition aactab.c:225
#define N
Definition af_mcompand.c:54
#define Y
Definition boxblur.h:37
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define NORMC(bits)
Definition celt.c:135
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition celt.c:28
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition celt.c:137
#define CELT_VECTORS
Definition celt.h:45
@ CELT_SPREAD_NORMAL
Definition celt.h:58
@ CELT_SPREAD_AGGRESSIVE
Definition celt.h:59
#define CELT_ALLOC_STEPS
Definition celt.h:46
#define CELT_MAX_BANDS
Definition celt.h:43
#define CELT_MAX_FINE_BITS
Definition celt.h:48
#define CELT_FINE_OFFSET
Definition celt.h:47
#define av_clip
Definition common.h:100
#define av_clip_uintp2
Definition common.h:124
#define NULL
Definition coverity.c:32
int high
Definition dovi_rpuenc.c:39
#define cm
Definition dvbsubdec.c:40
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
#define X
Definition f_ebur128.c:157
static const uint8_t bits[8]
Definition fastaudio.c:100
static int64_t duration
Definition ffplay.c:330
for(k=2;k<=8;++k)
if(svq3)
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
unsigned offset
Definition libaomenc.c:763
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition rc.c:182
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
Definition rc.c:204
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition rc.c:114
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
Definition rc.c:90
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
Definition rc.c:109
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
Definition rc.c:131
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame,...
Definition rc.h:62
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition rc.h:67
const uint8_t ff_celt_freq_range[]
Definition tab.c:836
const uint8_t ff_celt_static_alloc[11][21]
Definition tab.c:916
const uint8_t ff_celt_log_freq_range[]
Definition tab.c:840
const uint16_t ff_celt_model_spread[]
Definition tab.c:826
const uint8_t ff_celt_freq_bands[]
Definition tab.c:832
const uint8_t ff_celt_static_caps[4][2][21]
Definition tab.c:930
const uint16_t ff_celt_model_alloc_trim[]
Definition tab.c:828
const uint8_t ff_celt_log2_frac[]
Definition tab.c:994
else temp
Definition vf_mcdeint.c:275