77 if (*ptr >= end ||
val > INT_MAX - 254)
95 const uint8_t *ptr = buf;
96 const uint8_t *end = buf + buf_size;
105 pkt->code = (
i ) & 0x3;
106 pkt->stereo = (
i >> 2) & 0x1;
107 pkt->config = (
i >> 3) & 0x1F;
110 if (
pkt->code >= 2 && buf_size < 2)
116 pkt->frame_count = 1;
119 if (self_delimiting) {
121 if (len < 0 || len > end - ptr)
124 buf_size = end - buf;
127 frame_bytes = end - ptr;
130 pkt->frame_offset[0] = ptr - buf;
131 pkt->frame_size[0] = frame_bytes;
135 pkt->frame_count = 2;
138 if (self_delimiting) {
140 if (len < 0 || 2 * len > end - ptr)
143 buf_size = end - buf;
146 frame_bytes = end - ptr;
149 pkt->frame_offset[0] = ptr - buf;
150 pkt->frame_size[0] = frame_bytes >> 1;
151 pkt->frame_offset[1] =
pkt->frame_offset[0] +
pkt->frame_size[0];
152 pkt->frame_size[1] = frame_bytes >> 1;
156 pkt->frame_count = 2;
164 if (self_delimiting) {
166 if (len < 0 || len + frame_bytes > end - ptr)
168 end = ptr + frame_bytes +
len;
169 buf_size = end - buf;
172 pkt->frame_offset[0] = ptr - buf;
173 pkt->frame_size[0] = frame_bytes;
176 frame_bytes = end - ptr -
pkt->frame_size[0];
179 pkt->frame_offset[1] =
pkt->frame_offset[0] +
pkt->frame_size[0];
180 pkt->frame_size[1] = frame_bytes;
185 pkt->frame_count = (
i ) & 0x3F;
186 padding = (
i >> 6) & 0x01;
187 pkt->vbr = (
i >> 7) & 0x01;
204 for (
i = 0;
i <
pkt->frame_count - 1;
i++) {
208 pkt->frame_size[
i] = frame_bytes;
209 total_bytes += frame_bytes;
212 if (self_delimiting) {
214 if (len < 0 || len + total_bytes + padding > end - ptr)
216 end = ptr + total_bytes +
len + padding;
217 buf_size = end - buf;
220 frame_bytes = end - ptr - padding;
221 if (total_bytes > frame_bytes)
223 pkt->frame_offset[0] = ptr - buf;
224 for (
i = 1;
i <
pkt->frame_count;
i++)
225 pkt->frame_offset[
i] =
pkt->frame_offset[
i-1] +
pkt->frame_size[
i-1];
226 pkt->frame_size[
pkt->frame_count-1] = frame_bytes - total_bytes;
230 if (self_delimiting) {
232 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
234 end = ptr +
pkt->frame_count * frame_bytes + padding;
235 buf_size = end - buf;
237 frame_bytes = end - ptr - padding;
238 if (frame_bytes %
pkt->frame_count ||
241 frame_bytes /=
pkt->frame_count;
244 pkt->frame_offset[0] = ptr - buf;
245 pkt->frame_size[0] = frame_bytes;
246 for (
i = 1;
i <
pkt->frame_count;
i++) {
247 pkt->frame_offset[
i] =
pkt->frame_offset[
i-1] +
pkt->frame_size[
i-1];
248 pkt->frame_size[
i] = frame_bytes;
253 pkt->packet_size = buf_size;
254 pkt->data_size =
pkt->packet_size - padding;
262 if (
pkt->config < 12) {
264 pkt->bandwidth =
pkt->config >> 2;
265 }
else if (
pkt->config < 16) {
270 pkt->bandwidth = (
pkt->config - 16) >> 2;
279 memset(
pkt, 0,
sizeof(*
pkt));
296 static const uint8_t default_channel_map[2] = { 0, 1 };
308 "Multichannel configuration without extradata.\n");
318 if (extradata_size < 19) {
340 s->gain_i =
AV_RL16(extradata + 16);
344 map_type = extradata[18];
348 "Channel mapping 0 is only specified for up to 2 channels\n");
355 }
else if (map_type == 1 || map_type == 2 || map_type == 255) {
356 if (extradata_size < 21 +
channels) {
362 streams = extradata[19];
363 stereo_streams = extradata[20];
364 if (!streams || stereo_streams > streams ||
365 streams + stereo_streams > 255) {
367 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
374 "Channel mapping 1 is only specified for up to 8 channels\n");
379 }
else if (map_type == 2) {
381 if (
channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
382 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
384 "Channel mapping 2 is only specified for channel counts"
385 " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
386 " for nonnegative integer n\n");
404 if (!
s->channel_maps)
414 }
else if (idx >= streams + stereo_streams) {
416 "Invalid channel map for output channel %d: %d\n",
i, idx);
423 for (j = 0; j <
i; j++)
430 if (idx < 2 * stereo_streams) {
431 map->stream_idx = idx / 2;
432 map->channel_idx = idx & 1;
434 map->stream_idx = idx - stereo_streams;
435 map->channel_idx = 0;
441 s->nb_streams = streams;
442 s->nb_stereo_streams = stereo_streams;
449 float lowband_scratch[8 * 22];
450 float norm1[2 * 8 * 100];
451 float *norm2 = norm1 + 8 * 100;
453 int totalbits = (
f->framebits << 3) -
f->anticollapse_needed;
455 int update_lowband = 1;
456 int lowband_offset = 0;
460 for (
i =
f->start_band; i < f->end_band;
i++) {
461 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
464 float *
X =
f->block[0].coeffs + band_offset;
465 float *
Y = (
f->channels == 2) ?
f->block[1].coeffs + band_offset :
NULL;
466 float *norm_loc1, *norm_loc2;
469 int effective_lowband = -1;
473 if (
i !=
f->start_band)
474 f->remaining -= consumed;
475 f->remaining2 = totalbits - consumed - 1;
476 if (i <= f->coded_bands - 1) {
477 int curr_balance =
f->remaining /
FFMIN(3,
f->coded_bands-
i);
482 i ==
f->start_band + 1) && (update_lowband || lowband_offset == 0))
485 if (
i ==
f->start_band + 1) {
490 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
492 if (
f->channels == 2)
493 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
499 f->blocks > 1 ||
f->tf_change[
i] < 0)) {
500 int foldstart, foldend;
505 foldstart = lowband_offset;
507 foldend = lowband_offset - 1;
511 for (j = foldstart; j < foldend; j++) {
512 cm[0] |=
f->block[0].collapse_masks[j];
513 cm[1] |=
f->block[
f->channels - 1].collapse_masks[j];
517 if (
f->dual_stereo &&
i ==
f->intensity_stereo) {
521 norm1[j] = (norm1[j] + norm2[j]) / 2;
524 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband <<
f->size) :
NULL;
525 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband <<
f->size) :
NULL;
527 if (
f->dual_stereo) {
528 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
NULL, band_size,
b >> 1,
529 f->blocks, norm_loc1,
f->size,
530 norm1 + band_offset, 0, 1.0f,
531 lowband_scratch,
cm[0]);
533 cm[1] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
Y,
NULL, band_size,
b >> 1,
534 f->blocks, norm_loc2,
f->size,
535 norm2 + band_offset, 0, 1.0f,
536 lowband_scratch,
cm[1]);
538 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
Y, band_size,
b >> 0,
539 f->blocks, norm_loc1,
f->size,
540 norm1 + band_offset, 0, 1.0f,
541 lowband_scratch,
cm[0] |
cm[1]);
545 f->block[0].collapse_masks[
i] = (uint8_t)
cm[0];
546 f->block[
f->channels - 1].collapse_masks[
i] = (uint8_t)
cm[1];
547 f->remaining +=
f->pulses[
i] + consumed;
550 update_lowband = (
b > band_size << 3);
554 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
558 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
559 int skip_startband =
f->start_band;
561 int intensitystereo_bit = 0;
562 int dualstereo_bit = 0;
587 tbits_8ths =
f->framebits << 3;
588 for (
i =
f->start_band; i < f->end_band;
i++) {
590 int b_dynalloc = dynalloc;
591 int boost_amount =
f->alloc_boost[
i];
592 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
597 is_boost = boost_amount--;
607 tbits_8ths -= quanta;
613 dynalloc =
FFMAX(dynalloc - 1, 2);
627 f->anticollapse_needed = 0;
628 if (
f->transient &&
f->size >= 2 && tbits_8ths >= ((
f->size + 2) << 3))
629 f->anticollapse_needed = 1 << 3;
630 tbits_8ths -=
f->anticollapse_needed;
633 if (tbits_8ths >= 1 << 3)
635 tbits_8ths -= skip_bit;
638 if (
f->channels == 2) {
640 if (intensitystereo_bit <= tbits_8ths) {
641 tbits_8ths -= intensitystereo_bit;
642 if (tbits_8ths >= 1 << 3) {
643 dualstereo_bit = 1 << 3;
644 tbits_8ths -= 1 << 3;
647 intensitystereo_bit = 0;
652 for (
i =
f->start_band; i < f->end_band;
i++) {
653 int trim =
f->alloc_trim - 5 -
f->size;
663 trim_offset[
i] = trim * (band << scale) >> 6;
666 trim_offset[
i] -=
f->channels << 3;
672 while (low <= high) {
673 int center = (low + high) >> 1;
676 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
680 bandbits =
FFMAX(bandbits + trim_offset[
i], 0);
681 bandbits += boost[
i];
683 if (bandbits >= threshold[
i] || done) {
685 total +=
FFMIN(bandbits,
f->caps[
i]);
686 }
else if (bandbits >=
f->channels << 3) {
687 total +=
f->channels << 3;
691 if (total > tbits_8ths)
699 for (
i =
f->start_band; i < f->end_band;
i++) {
722 int center = (low + high) >> 1;
725 for (j =
f->end_band - 1; j >=
f->start_band; j--) {
728 if (bandbits >= threshold[j] || done) {
730 total +=
FFMIN(bandbits,
f->caps[j]);
731 }
else if (bandbits >=
f->channels << 3)
732 total +=
f->channels << 3;
734 if (total > tbits_8ths)
742 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
745 if (bandbits >= threshold[
i] || done)
748 bandbits = (bandbits >=
f->channels << 3) ?
749 f->channels << 3 : 0;
751 bandbits =
FFMIN(bandbits,
f->caps[
i]);
752 f->pulses[
i] = bandbits;
757 for (
f->coded_bands =
f->end_band; ;
f->coded_bands--) {
759 j =
f->coded_bands - 1;
761 if (j == skip_startband) {
763 tbits_8ths += skip_bit;
768 remaining = tbits_8ths - total;
776 if (allocation >=
FFMAX(threshold[j], (
f->channels + 1) << 3)) {
779 do_not_skip =
f->coded_bands <=
f->skip_band_floor;
789 allocation -= 1 << 3;
793 total -=
f->pulses[j];
794 if (intensitystereo_bit) {
795 total -= intensitystereo_bit;
797 total += intensitystereo_bit;
800 total +=
f->pulses[j] = (allocation >=
f->channels << 3) ?
f->channels << 3 : 0;
805 if (intensitystereo_bit) {
806 f->intensity_stereo =
FFMIN(
f->intensity_stereo,
f->coded_bands);
810 f->intensity_stereo =
f->dual_stereo = 0;
811 if (intensitystereo_bit)
816 if (
f->intensity_stereo <=
f->start_band)
817 tbits_8ths += dualstereo_bit;
818 else if (dualstereo_bit)
825 remaining = tbits_8ths - total;
828 for (
i =
f->start_band; i < f->coded_bands;
i++) {
835 for (
i =
f->start_band; i < f->coded_bands;
i++) {
837 int prev_extra = extrabits;
838 f->pulses[
i] += extrabits;
849 extrabits =
FFMAX(
f->pulses[
i] -
f->caps[
i], 0);
850 f->pulses[
i] -= extrabits;
853 dof =
N *
f->channels + (
f->channels == 2 &&
N > 2 && !
f->dual_stereo &&
i <
f->intensity_stereo);
860 if (
f->pulses[
i] +
offset < 2 * (dof << 3))
862 else if (
f->pulses[
i] +
offset < 3 * (dof << 3))
865 fine_bits = (
f->pulses[
i] +
offset + (dof << 2)) / (dof << 3);
867 max_bits =
FFMAX(max_bits, 0);
868 f->fine_bits[
i] =
av_clip(fine_bits, 0, max_bits);
872 f->fine_priority[
i] = (
f->fine_bits[
i] * (dof << 3) >=
f->pulses[
i] +
offset);
875 f->pulses[
i] -=
f->fine_bits[
i] << (
f->channels - 1) << 3;
878 extrabits =
FFMAX(
f->pulses[
i] - (
f->channels << 3), 0);
879 f->pulses[
i] -= extrabits;
881 f->fine_priority[
i] = 1;
886 int fineextra =
FFMIN(extrabits >> (
f->channels + 2),
888 f->fine_bits[
i] += fineextra;
890 fineextra <<=
f->channels + 2;
891 f->fine_priority[
i] = (fineextra >= extrabits - prev_extra);
892 extrabits -= fineextra;
895 f->remaining = extrabits;
898 for (;
i <
f->end_band;
i++) {
899 f->fine_bits[
i] =
f->pulses[
i] >> (
f->channels - 1) >> 3;
901 f->fine_priority[
i] =
f->fine_bits[
i] < 1;