30 float lowband_scratch[8 * 22];
31 float norm1[2 * 8 * 100];
32 float *norm2 = norm1 + 8 * 100;
34 int totalbits = (
f->framebits << 3) -
f->anticollapse_needed;
36 int update_lowband = 1;
37 int lowband_offset = 0;
41 for (
i =
f->start_band; i < f->end_band;
i++) {
42 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
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;
50 int effective_lowband = -1;
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);
63 i ==
f->start_band + 1) && (update_lowband || lowband_offset == 0))
66 if (
i ==
f->start_band + 1) {
71 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
74 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
80 f->blocks > 1 ||
f->tf_change[
i] < 0)) {
81 int foldstart, foldend;
86 foldstart = lowband_offset;
88 foldend = lowband_offset - 1;
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];
98 if (
f->dual_stereo &&
i ==
f->intensity_stereo) {
102 norm1[j] = (norm1[j] + norm2[j]) / 2;
105 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband <<
f->size) :
NULL;
106 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband <<
f->size) :
NULL;
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]);
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]);
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]);
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;
131 update_lowband = (
b > band_size << 3);
135 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
139 int i, j, low,
high, total, done, bandbits, remaining, tbits_8ths;
140 int skip_startband =
f->start_band;
142 int intensitystereo_bit = 0;
143 int dualstereo_bit = 0;
168 tbits_8ths =
f->framebits << 3;
169 for (
i =
f->start_band; i < f->end_band;
i++) {
171 int b_dynalloc = dynalloc;
172 int boost_amount =
f->alloc_boost[
i];
173 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
178 is_boost = boost_amount--;
188 tbits_8ths -= quanta;
194 dynalloc =
FFMAX(dynalloc - 1, 2);
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;
214 if (tbits_8ths >= 1 << 3)
216 tbits_8ths -= skip_bit;
219 if (
f->channels == 2) {
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;
228 intensitystereo_bit = 0;
233 for (
i =
f->start_band; i < f->end_band;
i++) {
234 int trim =
f->alloc_trim - 5 -
f->size;
244 trim_offset[
i] = trim * (band << scale) >> 6;
247 trim_offset[
i] -=
f->channels << 3;
253 while (low <=
high) {
254 int center = (low +
high) >> 1;
257 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
261 bandbits =
FFMAX(bandbits + trim_offset[
i], 0);
262 bandbits += boost[
i];
264 if (bandbits >= threshold[
i] || done) {
266 total +=
FFMIN(bandbits,
f->caps[
i]);
267 }
else if (bandbits >=
f->channels << 3) {
268 total +=
f->channels << 3;
272 if (total > tbits_8ths)
280 for (
i =
f->start_band; i < f->end_band;
i++) {
303 int center = (low +
high) >> 1;
306 for (j =
f->end_band - 1; j >=
f->start_band; j--) {
309 if (bandbits >= threshold[j] || done) {
311 total +=
FFMIN(bandbits,
f->caps[j]);
312 }
else if (bandbits >=
f->channels << 3)
313 total +=
f->channels << 3;
315 if (total > tbits_8ths)
323 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
326 if (bandbits >= threshold[
i] || done)
329 bandbits = (bandbits >=
f->channels << 3) ?
330 f->channels << 3 : 0;
332 bandbits =
FFMIN(bandbits,
f->caps[
i]);
333 f->pulses[
i] = bandbits;
338 for (
f->coded_bands =
f->end_band; ;
f->coded_bands--) {
340 j =
f->coded_bands - 1;
342 if (j == skip_startband) {
344 tbits_8ths += skip_bit;
349 remaining = tbits_8ths - total;
357 if (allocation >=
FFMAX(threshold[j], (
f->channels + 1) << 3)) {
360 do_not_skip =
f->coded_bands <=
f->skip_band_floor;
370 allocation -= 1 << 3;
374 total -=
f->pulses[j];
375 if (intensitystereo_bit) {
376 total -= intensitystereo_bit;
378 total += intensitystereo_bit;
381 total +=
f->pulses[j] = (allocation >=
f->channels << 3) ?
f->channels << 3 : 0;
386 if (intensitystereo_bit) {
387 f->intensity_stereo =
FFMIN(
f->intensity_stereo,
f->coded_bands);
391 f->intensity_stereo =
f->dual_stereo = 0;
392 if (intensitystereo_bit)
397 if (
f->intensity_stereo <=
f->start_band)
398 tbits_8ths += dualstereo_bit;
399 else if (dualstereo_bit)
406 remaining = tbits_8ths - total;
409 for (
i =
f->start_band; i < f->coded_bands;
i++) {
416 for (
i =
f->start_band; i < f->coded_bands;
i++) {
418 int prev_extra = extrabits;
419 f->pulses[
i] += extrabits;
430 extrabits =
FFMAX(
f->pulses[
i] -
f->caps[
i], 0);
431 f->pulses[
i] -= extrabits;
434 dof =
N *
f->channels + (
f->channels == 2 &&
N > 2 && !
f->dual_stereo &&
i <
f->intensity_stereo);
441 if (
f->pulses[
i] +
offset < 2 * (dof << 3))
443 else if (
f->pulses[
i] +
offset < 3 * (dof << 3))
446 fine_bits = (
f->pulses[
i] +
offset + (dof << 2)) / (dof << 3);
448 max_bits =
FFMAX(max_bits, 0);
449 f->fine_bits[
i] =
av_clip(fine_bits, 0, max_bits);
453 f->fine_priority[
i] = (
f->fine_bits[
i] * (dof << 3) >=
f->pulses[
i] +
offset);
456 f->pulses[
i] -=
f->fine_bits[
i] << (
f->channels - 1) << 3;
459 extrabits =
FFMAX(
f->pulses[
i] - (
f->channels << 3), 0);
460 f->pulses[
i] -= extrabits;
462 f->fine_priority[
i] = 1;
467 int fineextra =
FFMIN(extrabits >> (
f->channels + 2),
469 f->fine_bits[
i] += fineextra;
471 fineextra <<=
f->channels + 2;
472 f->fine_priority[
i] = (fineextra >= extrabits - prev_extra);
473 extrabits -= fineextra;
476 f->remaining = extrabits;
479 for (;
i <
f->end_band;
i++) {
480 f->fine_bits[
i] =
f->pulses[
i] >> (
f->channels - 1) >> 3;
482 f->fine_priority[
i] =
f->fine_bits[
i] < 1;