76 if (*ptr >= end ||
val > INT_MAX - 254)
95 const uint8_t *end = buf + buf_size;
104 pkt->code = (
i ) & 0x3;
105 pkt->stereo = (
i >> 2) & 0x1;
106 pkt->config = (
i >> 3) & 0x1F;
109 if (
pkt->code >= 2 && buf_size < 2)
115 pkt->frame_count = 1;
118 if (self_delimiting) {
120 if (len < 0 || len > end - ptr)
123 buf_size = end - buf;
126 frame_bytes = end - ptr;
129 pkt->frame_offset[0] = ptr - buf;
130 pkt->frame_size[0] = frame_bytes;
134 pkt->frame_count = 2;
137 if (self_delimiting) {
139 if (len < 0 || 2 * len > end - ptr)
142 buf_size = end - buf;
145 frame_bytes = end - ptr;
148 pkt->frame_offset[0] = ptr - buf;
149 pkt->frame_size[0] = frame_bytes >> 1;
150 pkt->frame_offset[1] =
pkt->frame_offset[0] +
pkt->frame_size[0];
151 pkt->frame_size[1] = frame_bytes >> 1;
155 pkt->frame_count = 2;
163 if (self_delimiting) {
165 if (len < 0 || len + frame_bytes > end - ptr)
167 end = ptr + frame_bytes +
len;
168 buf_size = end - buf;
171 pkt->frame_offset[0] = ptr - buf;
172 pkt->frame_size[0] = frame_bytes;
175 frame_bytes = end - ptr -
pkt->frame_size[0];
178 pkt->frame_offset[1] =
pkt->frame_offset[0] +
pkt->frame_size[0];
179 pkt->frame_size[1] = frame_bytes;
184 pkt->frame_count = (
i ) & 0x3F;
185 padding = (
i >> 6) & 0x01;
186 pkt->vbr = (
i >> 7) & 0x01;
203 for (
i = 0;
i <
pkt->frame_count - 1;
i++) {
207 pkt->frame_size[
i] = frame_bytes;
208 total_bytes += frame_bytes;
211 if (self_delimiting) {
213 if (len < 0 || len + total_bytes + padding > end - ptr)
215 end = ptr + total_bytes +
len + padding;
216 buf_size = end - buf;
219 frame_bytes = end - ptr - padding;
220 if (total_bytes > frame_bytes)
222 pkt->frame_offset[0] = ptr - buf;
223 for (
i = 1;
i <
pkt->frame_count;
i++)
224 pkt->frame_offset[
i] =
pkt->frame_offset[
i-1] +
pkt->frame_size[
i-1];
225 pkt->frame_size[
pkt->frame_count-1] = frame_bytes - total_bytes;
229 if (self_delimiting) {
231 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
233 end = ptr +
pkt->frame_count * frame_bytes + padding;
234 buf_size = end - buf;
236 frame_bytes = end - ptr - padding;
237 if (frame_bytes %
pkt->frame_count ||
240 frame_bytes /=
pkt->frame_count;
243 pkt->frame_offset[0] = ptr - buf;
244 pkt->frame_size[0] = frame_bytes;
245 for (
i = 1;
i <
pkt->frame_count;
i++) {
246 pkt->frame_offset[
i] =
pkt->frame_offset[
i-1] +
pkt->frame_size[
i-1];
247 pkt->frame_size[
i] = frame_bytes;
252 pkt->packet_size = buf_size;
253 pkt->data_size =
pkt->packet_size - padding;
261 if (
pkt->config < 12) {
263 pkt->bandwidth =
pkt->config >> 2;
264 }
else if (
pkt->config < 16) {
269 pkt->bandwidth = (
pkt->config - 16) >> 2;
278 memset(
pkt, 0,
sizeof(*
pkt));
295 static const uint8_t default_channel_map[2] = { 0, 1 };
307 "Multichannel configuration without extradata.\n");
317 if (extradata_size < 19) {
339 s->gain_i =
AV_RL16(extradata + 16);
343 map_type = extradata[18];
347 "Channel mapping 0 is only specified for up to 2 channels\n");
354 }
else if (map_type == 1 || map_type == 2 || map_type == 255) {
355 if (extradata_size < 21 +
channels) {
361 streams = extradata[19];
362 stereo_streams = extradata[20];
363 if (!streams || stereo_streams > streams ||
364 streams + stereo_streams > 255) {
366 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
373 "Channel mapping 1 is only specified for up to 8 channels\n");
378 }
else if (map_type == 2) {
380 if (
channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
381 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
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");
403 if (!
s->channel_maps)
413 }
else if (idx >= streams + stereo_streams) {
415 "Invalid channel map for output channel %d: %d\n",
i, idx);
422 for (j = 0; j <
i; j++)
429 if (idx < 2 * stereo_streams) {
430 map->stream_idx = idx / 2;
431 map->channel_idx = idx & 1;
433 map->stream_idx = idx - stereo_streams;
434 map->channel_idx = 0;
440 s->nb_streams = streams;
441 s->nb_stereo_streams = stereo_streams;
448 float lowband_scratch[8 * 22];
449 float norm1[2 * 8 * 100];
450 float *norm2 = norm1 + 8 * 100;
452 int totalbits = (
f->framebits << 3) -
f->anticollapse_needed;
454 int update_lowband = 1;
455 int lowband_offset = 0;
459 for (
i =
f->start_band; i < f->end_band;
i++) {
460 uint32_t
cm[2] = { (1 <<
f->blocks) - 1, (1 <<
f->blocks) - 1 };
463 float *
X =
f->block[0].coeffs + band_offset;
464 float *
Y = (
f->channels == 2) ?
f->block[1].coeffs + band_offset :
NULL;
465 float *norm_loc1, *norm_loc2;
468 int effective_lowband = -1;
472 if (
i !=
f->start_band)
473 f->remaining -= consumed;
474 f->remaining2 = totalbits - consumed - 1;
475 if (i <= f->coded_bands - 1) {
476 int curr_balance =
f->remaining /
FFMIN(3,
f->coded_bands-
i);
481 i ==
f->start_band + 1) && (update_lowband || lowband_offset == 0))
484 if (
i ==
f->start_band + 1) {
489 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
491 if (
f->channels == 2)
492 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
498 f->blocks > 1 ||
f->tf_change[
i] < 0)) {
499 int foldstart, foldend;
504 foldstart = lowband_offset;
506 foldend = lowband_offset - 1;
510 for (j = foldstart; j < foldend; j++) {
511 cm[0] |=
f->block[0].collapse_masks[j];
512 cm[1] |=
f->block[
f->channels - 1].collapse_masks[j];
516 if (
f->dual_stereo &&
i ==
f->intensity_stereo) {
520 norm1[j] = (norm1[j] + norm2[j]) / 2;
523 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband <<
f->size) :
NULL;
524 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband <<
f->size) :
NULL;
526 if (
f->dual_stereo) {
527 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
NULL, band_size,
b >> 1,
528 f->blocks, norm_loc1,
f->size,
529 norm1 + band_offset, 0, 1.0f,
530 lowband_scratch,
cm[0]);
532 cm[1] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
Y,
NULL, band_size,
b >> 1,
533 f->blocks, norm_loc2,
f->size,
534 norm2 + band_offset, 0, 1.0f,
535 lowband_scratch,
cm[1]);
537 cm[0] =
f->pvq->quant_band(
f->pvq,
f, rc,
i,
X,
Y, band_size,
b >> 0,
538 f->blocks, norm_loc1,
f->size,
539 norm1 + band_offset, 0, 1.0f,
540 lowband_scratch,
cm[0] |
cm[1]);
545 f->block[
f->channels - 1].collapse_masks[
i] = (
uint8_t)
cm[1];
546 f->remaining +=
f->pulses[
i] + consumed;
549 update_lowband = (
b > band_size << 3);
553 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
557 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
558 int skip_startband =
f->start_band;
560 int intensitystereo_bit = 0;
561 int dualstereo_bit = 0;
586 tbits_8ths =
f->framebits << 3;
587 for (
i =
f->start_band; i < f->end_band;
i++) {
589 int b_dynalloc = dynalloc;
590 int boost_amount =
f->alloc_boost[
i];
591 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
596 is_boost = boost_amount--;
606 tbits_8ths -= quanta;
612 dynalloc =
FFMAX(dynalloc - 1, 2);
626 f->anticollapse_needed = 0;
627 if (
f->transient &&
f->size >= 2 && tbits_8ths >= ((
f->size + 2) << 3))
628 f->anticollapse_needed = 1 << 3;
629 tbits_8ths -=
f->anticollapse_needed;
632 if (tbits_8ths >= 1 << 3)
634 tbits_8ths -= skip_bit;
637 if (
f->channels == 2) {
639 if (intensitystereo_bit <= tbits_8ths) {
640 tbits_8ths -= intensitystereo_bit;
641 if (tbits_8ths >= 1 << 3) {
642 dualstereo_bit = 1 << 3;
643 tbits_8ths -= 1 << 3;
646 intensitystereo_bit = 0;
651 for (
i =
f->start_band; i < f->end_band;
i++) {
652 int trim =
f->alloc_trim - 5 -
f->size;
662 trim_offset[
i] = trim * (band << scale) >> 6;
665 trim_offset[
i] -=
f->channels << 3;
671 while (low <= high) {
672 int center = (low + high) >> 1;
675 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
679 bandbits =
FFMAX(bandbits + trim_offset[
i], 0);
680 bandbits += boost[
i];
682 if (bandbits >= threshold[
i] || done) {
684 total +=
FFMIN(bandbits,
f->caps[
i]);
685 }
else if (bandbits >=
f->channels << 3) {
686 total +=
f->channels << 3;
690 if (total > tbits_8ths)
698 for (
i =
f->start_band; i < f->end_band;
i++) {
721 int center = (low + high) >> 1;
724 for (j =
f->end_band - 1; j >=
f->start_band; j--) {
727 if (bandbits >= threshold[j] || done) {
729 total +=
FFMIN(bandbits,
f->caps[j]);
730 }
else if (bandbits >=
f->channels << 3)
731 total +=
f->channels << 3;
733 if (total > tbits_8ths)
741 for (
i =
f->end_band - 1;
i >=
f->start_band;
i--) {
744 if (bandbits >= threshold[
i] || done)
747 bandbits = (bandbits >=
f->channels << 3) ?
748 f->channels << 3 : 0;
750 bandbits =
FFMIN(bandbits,
f->caps[
i]);
751 f->pulses[
i] = bandbits;
756 for (
f->coded_bands =
f->end_band; ;
f->coded_bands--) {
758 j =
f->coded_bands - 1;
760 if (j == skip_startband) {
762 tbits_8ths += skip_bit;
767 remaining = tbits_8ths - total;
775 if (allocation >=
FFMAX(threshold[j], (
f->channels + 1) << 3)) {
778 do_not_skip =
f->coded_bands <=
f->skip_band_floor;
788 allocation -= 1 << 3;
792 total -=
f->pulses[j];
793 if (intensitystereo_bit) {
794 total -= intensitystereo_bit;
796 total += intensitystereo_bit;
799 total +=
f->pulses[j] = (allocation >=
f->channels << 3) ?
f->channels << 3 : 0;
804 if (intensitystereo_bit) {
805 f->intensity_stereo =
FFMIN(
f->intensity_stereo,
f->coded_bands);
809 f->intensity_stereo =
f->dual_stereo = 0;
810 if (intensitystereo_bit)
815 if (
f->intensity_stereo <=
f->start_band)
816 tbits_8ths += dualstereo_bit;
817 else if (dualstereo_bit)
824 remaining = tbits_8ths - total;
827 for (
i =
f->start_band; i < f->coded_bands;
i++) {
834 for (
i =
f->start_band; i < f->coded_bands;
i++) {
836 int prev_extra = extrabits;
837 f->pulses[
i] += extrabits;
848 extrabits =
FFMAX(
f->pulses[
i] -
f->caps[
i], 0);
849 f->pulses[
i] -= extrabits;
852 dof =
N *
f->channels + (
f->channels == 2 &&
N > 2 && !
f->dual_stereo &&
i <
f->intensity_stereo);
859 if (
f->pulses[
i] +
offset < 2 * (dof << 3))
861 else if (
f->pulses[
i] +
offset < 3 * (dof << 3))
864 fine_bits = (
f->pulses[
i] +
offset + (dof << 2)) / (dof << 3);
866 max_bits =
FFMAX(max_bits, 0);
867 f->fine_bits[
i] =
av_clip(fine_bits, 0, max_bits);
871 f->fine_priority[
i] = (
f->fine_bits[
i] * (dof << 3) >=
f->pulses[
i] +
offset);
874 f->pulses[
i] -=
f->fine_bits[
i] << (
f->channels - 1) << 3;
877 extrabits =
FFMAX(
f->pulses[
i] - (
f->channels << 3), 0);
878 f->pulses[
i] -= extrabits;
880 f->fine_priority[
i] = 1;
885 int fineextra =
FFMIN(extrabits >> (
f->channels + 2),
887 f->fine_bits[
i] += fineextra;
889 fineextra <<=
f->channels + 2;
890 f->fine_priority[
i] = (fineextra >= extrabits - prev_extra);
891 extrabits -= fineextra;
894 f->remaining = extrabits;
897 for (;
i <
f->end_band;
i++) {
898 f->fine_bits[
i] =
f->pulses[
i] >> (
f->channels - 1) >> 3;
900 f->fine_priority[
i] =
f->fine_bits[
i] < 1;