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 };
303 int version, map_type, streams, stereo_streams,
i, j,
ret;
309 "Multichannel configuration without extradata.\n");
319 if (extradata_size < 19) {
341 s->gain_i =
AV_RL16(extradata + 16);
345 map_type = extradata[18];
349 "Channel mapping 0 is only specified for up to 2 channels\n");
358 }
else if (map_type == 1 || map_type == 2 || map_type == 255) {
359 if (extradata_size < 21 +
channels) {
366 streams = extradata[19];
367 stereo_streams = extradata[20];
368 if (!streams || stereo_streams > streams ||
369 streams + stereo_streams > 255) {
371 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
379 "Channel mapping 1 is only specified for up to 8 channels\n");
385 }
else if (map_type == 2) {
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");
418 if (!
s->channel_maps) {
430 }
else if (idx >= streams + stereo_streams) {
432 "Invalid channel map for output channel %d: %d\n",
i, idx);
440 for (j = 0; j <
i; j++)
447 if (idx < 2 * stereo_streams) {
448 map->stream_idx = idx / 2;
449 map->channel_idx = idx & 1;
451 map->stream_idx = idx - stereo_streams;
452 map->channel_idx = 0;
460 s->nb_streams = streams;
461 s->nb_stereo_streams = stereo_streams;
471 float lowband_scratch[8 * 22];
472 float norm1[2 * 8 * 100];
473 float *norm2 = norm1 + 8 * 100;
475 int totalbits = (
f->framebits << 3) -
f->anticollapse_needed;
477 int update_lowband = 1;
478 int lowband_offset = 0;
482 for (
i =
f->start_band; i < f->end_band;
i++) {
483 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
486 float *
X =
f->block[0].coeffs + band_offset;
487 float *
Y = (
f->channels == 2) ?
f->block[1].coeffs + band_offset :
NULL;
488 float *norm_loc1, *norm_loc2;
491 int effective_lowband = -1;
495 if (
i !=
f->start_band)
496 f->remaining -= consumed;
497 f->remaining2 = totalbits - consumed - 1;
498 if (i <= f->coded_bands - 1) {
499 int curr_balance =
f->remaining /
FFMIN(3,
f->coded_bands-
i);
504 i ==
f->start_band + 1) && (update_lowband || lowband_offset == 0))
507 if (
i ==
f->start_band + 1) {
512 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
514 if (
f->channels == 2)
515 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
521 f->blocks > 1 ||
f->tf_change[
i] < 0)) {
522 int foldstart, foldend;
527 foldstart = lowband_offset;
529 foldend = lowband_offset - 1;
533 for (j = foldstart; j < foldend; j++) {
534 cm[0] |=
f->block[0].collapse_masks[j];
535 cm[1] |=
f->block[
f->channels - 1].collapse_masks[j];
539 if (
f->dual_stereo &&
i ==
f->intensity_stereo) {
543 norm1[j] = (norm1[j] + norm2[j]) / 2;
546 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband <<
f->size) :
NULL;
547 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband <<
f->size) :
NULL;
549 if (
f->dual_stereo) {
550 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
NULL, band_size,
b >> 1,
551 f->blocks, norm_loc1,
f->size,
552 norm1 + band_offset, 0, 1.0f,
553 lowband_scratch,
cm[0]);
555 cm[1] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
Y,
NULL, band_size,
b >> 1,
556 f->blocks, norm_loc2,
f->size,
557 norm2 + band_offset, 0, 1.0f,
558 lowband_scratch,
cm[1]);
560 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
Y, band_size,
b >> 0,
561 f->blocks, norm_loc1,
f->size,
562 norm1 + band_offset, 0, 1.0f,
563 lowband_scratch,
cm[0] |
cm[1]);
567 f->block[0].collapse_masks[
i] = (uint8_t)
cm[0];
568 f->block[
f->channels - 1].collapse_masks[
i] = (uint8_t)
cm[1];
569 f->remaining +=
f->pulses[
i] + consumed;
572 update_lowband = (
b > band_size << 3);
576 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
580 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
581 int skip_startband =
f->start_band;
583 int intensitystereo_bit = 0;
584 int dualstereo_bit = 0;
609 tbits_8ths =
f->framebits << 3;
610 for (
i =
f->start_band; i < f->end_band;
i++) {
612 int b_dynalloc = dynalloc;
613 int boost_amount =
f->alloc_boost[
i];
614 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
619 is_boost = boost_amount--;
629 tbits_8ths -= quanta;
635 dynalloc =
FFMAX(dynalloc - 1, 2);
649 f->anticollapse_needed = 0;
650 if (
f->transient &&
f->size >= 2 && tbits_8ths >= ((
f->size + 2) << 3))
651 f->anticollapse_needed = 1 << 3;
652 tbits_8ths -=
f->anticollapse_needed;
655 if (tbits_8ths >= 1 << 3)
657 tbits_8ths -= skip_bit;
660 if (
f->channels == 2) {
662 if (intensitystereo_bit <= tbits_8ths) {
663 tbits_8ths -= intensitystereo_bit;
664 if (tbits_8ths >= 1 << 3) {
665 dualstereo_bit = 1 << 3;
666 tbits_8ths -= 1 << 3;
669 intensitystereo_bit = 0;
674 for (
i =
f->start_band; i < f->end_band;
i++) {
675 int trim =
f->alloc_trim - 5 -
f->size;
685 trim_offset[
i] = trim * (band << scale) >> 6;
688 trim_offset[
i] -=
f->channels << 3;
694 while (low <= high) {
695 int center = (low + high) >> 1;
698 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
702 bandbits =
FFMAX(bandbits + trim_offset[
i], 0);
703 bandbits += boost[
i];
705 if (bandbits >= threshold[
i] || done) {
707 total +=
FFMIN(bandbits,
f->caps[
i]);
708 }
else if (bandbits >=
f->channels << 3) {
709 total +=
f->channels << 3;
713 if (total > tbits_8ths)
721 for (
i =
f->start_band; i < f->end_band;
i++) {
744 int center = (low + high) >> 1;
747 for (j =
f->end_band - 1; j >=
f->start_band; j--) {
750 if (bandbits >= threshold[j] || done) {
752 total +=
FFMIN(bandbits,
f->caps[j]);
753 }
else if (bandbits >=
f->channels << 3)
754 total +=
f->channels << 3;
756 if (total > tbits_8ths)
764 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
767 if (bandbits >= threshold[
i] || done)
770 bandbits = (bandbits >=
f->channels << 3) ?
771 f->channels << 3 : 0;
773 bandbits =
FFMIN(bandbits,
f->caps[
i]);
774 f->pulses[
i] = bandbits;
779 for (
f->coded_bands =
f->end_band; ;
f->coded_bands--) {
781 j =
f->coded_bands - 1;
783 if (j == skip_startband) {
785 tbits_8ths += skip_bit;
790 remaining = tbits_8ths - total;
798 if (allocation >=
FFMAX(threshold[j], (
f->channels + 1) << 3)) {
801 do_not_skip =
f->coded_bands <=
f->skip_band_floor;
811 allocation -= 1 << 3;
815 total -=
f->pulses[j];
816 if (intensitystereo_bit) {
817 total -= intensitystereo_bit;
819 total += intensitystereo_bit;
822 total +=
f->pulses[j] = (allocation >=
f->channels << 3) ?
f->channels << 3 : 0;
827 if (intensitystereo_bit) {
828 f->intensity_stereo =
FFMIN(
f->intensity_stereo,
f->coded_bands);
832 f->intensity_stereo =
f->dual_stereo = 0;
833 if (intensitystereo_bit)
838 if (
f->intensity_stereo <=
f->start_band)
839 tbits_8ths += dualstereo_bit;
840 else if (dualstereo_bit)
847 remaining = tbits_8ths - total;
850 for (
i =
f->start_band; i < f->coded_bands;
i++) {
857 for (
i =
f->start_band; i < f->coded_bands;
i++) {
859 int prev_extra = extrabits;
860 f->pulses[
i] += extrabits;
871 extrabits =
FFMAX(
f->pulses[
i] -
f->caps[
i], 0);
872 f->pulses[
i] -= extrabits;
875 dof =
N *
f->channels + (
f->channels == 2 &&
N > 2 && !
f->dual_stereo &&
i <
f->intensity_stereo);
882 if (
f->pulses[
i] +
offset < 2 * (dof << 3))
884 else if (
f->pulses[
i] +
offset < 3 * (dof << 3))
887 fine_bits = (
f->pulses[
i] +
offset + (dof << 2)) / (dof << 3);
889 max_bits =
FFMAX(max_bits, 0);
890 f->fine_bits[
i] =
av_clip(fine_bits, 0, max_bits);
894 f->fine_priority[
i] = (
f->fine_bits[
i] * (dof << 3) >=
f->pulses[
i] +
offset);
897 f->pulses[
i] -=
f->fine_bits[
i] << (
f->channels - 1) << 3;
900 extrabits =
FFMAX(
f->pulses[
i] - (
f->channels << 3), 0);
901 f->pulses[
i] -= extrabits;
903 f->fine_priority[
i] = 1;
908 int fineextra =
FFMIN(extrabits >> (
f->channels + 2),
910 f->fine_bits[
i] += fineextra;
912 fineextra <<=
f->channels + 2;
913 f->fine_priority[
i] = (fineextra >= extrabits - prev_extra);
914 extrabits -= fineextra;
917 f->remaining = extrabits;
920 for (;
i <
f->end_band;
i++) {
921 f->fine_bits[
i] =
f->pulses[
i] >> (
f->channels - 1) >> 3;
923 f->fine_priority[
i] =
f->fine_bits[
i] < 1;