43 #include "config_components.h"
55 #define SAMPLETYPE_SIZE(ctx) (sizeof(float) == sizeof(int32_t) ? sizeof(float) : \
56 (ctx)->fixed_point ? sizeof(int32_t) : sizeof(float))
63 #define CMIXLEV_NUM_OPTIONS 3
68 #define SURMIXLEV_NUM_OPTIONS 3
73 #define EXTMIXLEV_NUM_OPTIONS 8
82 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
83 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
202 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
203 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
214 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
215 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
216 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
218 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
219 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
220 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
222 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
223 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
224 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
226 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
227 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
228 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
230 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
231 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
232 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
248 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
249 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
250 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
253 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
254 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
255 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
258 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
259 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
260 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
263 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
264 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
265 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
268 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
269 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
270 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
273 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
274 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
275 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
279 #define FLT_OPTION_THRESHOLD 0.01
285 for (
i = 0;
i < v_list_size;
i++) {
290 if (
i == v_list_size)
298 float *opt_param,
const float *
list,
299 int list_size,
int default_value,
int min_value,
303 if (mixlev < min_value) {
304 mixlev = default_value;
305 if (*opt_param >= 0.0) {
307 "default value: %0.3f\n", opt_name,
list[mixlev]);
310 *opt_param =
list[mixlev];
342 if (
s->has_surround &&
383 &
s->center_mix_level);
385 if (
s->has_surround) {
388 &
s->surround_mix_level);
397 if (!
s->eac3 ||
s->has_center) {
402 &
s->ltrt_center_mix_level);
407 &
s->loro_center_mix_level);
409 if (!
s->eac3 ||
s->has_surround) {
414 &
s->ltrt_surround_mix_level);
419 &
s->loro_surround_mix_level);
431 "specified number of channels\n");
468 "room_type is set\n");
496 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
497 s->bits_written -=
s->bit_rate;
498 s->samples_written -=
s->sample_rate;
500 s->frame_size =
s->frame_size_min +
501 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
502 s->bits_written +=
s->frame_size * 8;
521 for (ch = 1; ch <=
s->fbw_channels; ch++)
522 block->channel_in_cpl[ch] =
s->cpl_on;
531 block->num_cpl_channels = 0;
532 for (ch = 1; ch <=
s->fbw_channels; ch++)
533 block->num_cpl_channels +=
block->channel_in_cpl[ch];
534 block->cpl_in_use =
block->num_cpl_channels > 1;
535 num_cpl_blocks +=
block->cpl_in_use;
537 block->num_cpl_channels = 0;
538 for (ch = 1; ch <=
s->fbw_channels; ch++)
539 block->channel_in_cpl[ch] = 0;
544 for (ch = 1; ch <=
s->fbw_channels; ch++) {
545 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
546 block->new_cpl_strategy = 1;
551 block->new_cpl_leak =
block->new_cpl_strategy;
553 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
554 block->new_snr_offsets = 1;
555 if (
block->cpl_in_use)
558 block->new_snr_offsets = 0;
567 for (ch = 1; ch <=
s->fbw_channels; ch++) {
568 if (
block->channel_in_cpl[ch])
571 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
589 if (!
s->rematrixing_enabled)
594 if (
block->new_rematrixing_strategy)
597 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
601 for (
i = start;
i < end;
i++) {
604 block->fixed_coef[1][
i] = (lt + rt) >> 1;
605 block->fixed_coef[2][
i] = (lt - rt) >> 1;
618 int expstr,
i, grpsize;
621 grpsize = 3 << expstr;
622 for (
i = 12;
i < 256;
i++) {
638 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
641 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
649 #define EXP_DIFF_THRESHOLD 500
669 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
670 uint8_t *exp_strategy =
s->exp_strategy[ch];
671 uint8_t *
exp =
s->blocks[0].exp[ch];
680 if (!
s->blocks[
blk-1].cpl_in_use) {
683 }
else if (!
s->blocks[
blk].cpl_in_use) {
687 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
702 while (blk < s->num_blocks) {
704 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
718 if (CONFIG_EAC3_ENCODER &&
s->eac3)
739 switch(exp_strategy) {
741 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
742 uint8_t exp_min =
exp[k];
743 if (
exp[k+1] < exp_min)
745 exp[
i-cpl] = exp_min;
750 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
751 uint8_t exp_min =
exp[k];
752 if (
exp[k+1] < exp_min)
754 if (
exp[k+2] < exp_min)
756 if (
exp[k+3] < exp_min)
758 exp[
i-cpl] = exp_min;
765 if (!cpl &&
exp[0] > 15)
770 for (
i = 1;
i <= nb_groups;
i++)
780 switch (exp_strategy) {
782 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
783 uint8_t exp1 =
exp[
i-cpl];
789 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
806 int blk, blk1, ch, cpl;
807 uint8_t *
exp, *exp_strategy;
810 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
811 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
812 exp_strategy =
s->exp_strategy[ch];
816 while (blk < s->num_blocks) {
818 if (cpl && !
block->cpl_in_use) {
828 s->exp_ref_block[ch][
blk] =
blk;
829 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
830 s->exp_ref_block[ch][blk1] =
blk;
833 num_reuse_blocks = blk1 -
blk - 1;
836 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
857 int nb_groups, bit_count;
863 int exp_strategy =
s->exp_strategy[ch][
blk];
871 bit_count += 4 + (nb_groups * 7);
889 int group_size, nb_groups;
891 int delta0, delta1, delta2;
897 int exp_strategy =
s->exp_strategy[ch][
blk];
901 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
903 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
907 block->grouped_exp[ch][0] = exp1;
910 for (
i = 1;
i <= nb_groups;
i++) {
915 delta0 = exp1 - exp0 + 2;
921 delta1 = exp1 - exp0 + 2;
927 delta2 = exp1 - exp0 + 2;
930 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
962 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
981 if (
s->num_blocks != 0x6)
985 if (
s->num_blocks == 6)
989 if (
s->use_frame_exp_strategy)
990 frame_bits += 5 *
s->fbw_channels;
992 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
994 frame_bits +=
s->num_blocks;
996 if (
s->num_blks_code != 0x3)
999 frame_bits +=
s->fbw_channels * 5;
1003 if (
s->num_blocks != 1)
1007 frame_bits += frame_bits_inc[
s->channel_mode];
1014 frame_bits +=
s->fbw_channels;
1017 frame_bits +=
s->fbw_channels;
1033 frame_bits += 2 *
s->fbw_channels;
1040 frame_bits += 2 + 2 + 2 + 2 + 3;
1059 frame_bits += 1 + 16;
1061 s->frame_bits_fixed = frame_bits;
1074 s->slow_decay_code = 2;
1075 s->fast_decay_code = 1;
1076 s->slow_gain_code = 1;
1077 s->db_per_bit_code =
s->eac3 ? 2 : 3;
1079 for (ch = 0; ch <=
s->channels; ch++)
1080 s->fast_gain_code[ch] = 4;
1083 s->coarse_snr_offset = 40;
1093 s->bit_alloc.cpl_fast_leak = 0;
1094 s->bit_alloc.cpl_slow_leak = 0;
1118 if (
s->has_surround)
1120 frame_bits +=
s->lfe_on;
1121 frame_bits += 1 + 1 + 2;
1127 frame_bits += 3 + 1 + 1;
1129 frame_bits += 2 + 2;
1134 frame_bits += 5 + 2 + 1;
1143 if (
block->new_cpl_strategy)
1149 if (
s->use_frame_exp_strategy) {
1153 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
1159 if (
s->bitstream_id == 6) {
1172 if (
block->new_cpl_strategy) {
1175 if (
block->cpl_in_use) {
1179 frame_bits +=
s->fbw_channels;
1182 frame_bits += 4 + 4;
1186 frame_bits +=
s->num_cpl_subbands - 1;
1191 if (
block->cpl_in_use) {
1192 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1193 if (
block->channel_in_cpl[ch]) {
1194 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1196 if (
block->new_cpl_coords[ch]) {
1198 frame_bits += (4 + 4) *
s->num_cpl_bands;
1206 if (!
s->eac3 ||
blk > 0)
1208 if (
s->blocks[
blk].new_rematrixing_strategy)
1209 frame_bits +=
block->num_rematrixing_bands;
1213 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1215 if (!
block->channel_in_cpl[ch])
1222 if (!
s->eac3 &&
block->cpl_in_use)
1227 if (
block->new_snr_offsets)
1228 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1232 if (
block->cpl_in_use) {
1233 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1235 if (
block->new_cpl_leak)
1236 frame_bits += 3 + 3;
1240 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1261 block->band_psd[ch]);
1263 s->start_freq[ch],
block->end_freq[ch],
1265 ch ==
s->lfe_channel,
1283 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1286 ref_bap =
s->bap_buffer;
1287 for (ch = 0; ch <=
s->channels; ch++) {
1308 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1309 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1310 mant_cnt[
blk][4] = 1;
1335 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1336 s->ref_bap[ch][
blk] + start,
1347 int ch, max_end_freq;
1352 max_end_freq =
s->bandwidth_code * 3 + 73;
1353 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1357 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1375 snr_offset = (snr_offset - 240) * 4;
1387 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1388 s->start_freq[ch],
block->end_freq[ch],
1389 snr_offset,
s->bit_alloc.floor,
1406 int snr_offset, snr_incr;
1408 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1412 snr_offset =
s->coarse_snr_offset << 4;
1416 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1421 while (snr_offset >= 0 &&
1428 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1429 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1430 while (snr_offset + snr_incr <= 1023 &&
1432 snr_offset += snr_incr;
1433 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1436 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1439 s->coarse_snr_offset = snr_offset >> 4;
1440 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1441 s->fine_snr_offset[ch] = snr_offset & 0xF;
1475 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1493 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1494 m = (1 << (qbits-1));
1514 uint8_t *
exp, uint8_t *bap,
1515 int16_t *qmant,
int start_freq,
1520 for (
i = start_freq;
i < end_freq;
i++) {
1521 int c = fixed_coef[
i];
1529 switch (
s->mant1_cnt) {
1531 s->qmant1_ptr = &qmant[
i];
1536 *
s->qmant1_ptr += 3 * v;
1541 *
s->qmant1_ptr += v;
1549 switch (
s->mant2_cnt) {
1551 s->qmant2_ptr = &qmant[
i];
1556 *
s->qmant2_ptr += 5 * v;
1561 *
s->qmant2_ptr += v;
1572 switch (
s->mant4_cnt) {
1574 s->qmant4_ptr = &qmant[
i];
1579 *
s->qmant4_ptr += v;
1610 int blk, ch, ch0=0, got_cpl;
1616 got_cpl = !
block->cpl_in_use;
1617 for (ch = 1; ch <=
s->channels; ch++) {
1618 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1624 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1626 s->start_freq[ch],
block->end_freq[ch]);
1644 put_bits(pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1650 if (
s->channel_mode & 0x04)
1665 if (
s->bitstream_id == 6) {
1670 put_bits(pb, 3,
s->ltrt_center_mix_level);
1671 put_bits(pb, 3,
s->ltrt_surround_mix_level);
1672 put_bits(pb, 3,
s->loro_center_mix_level);
1673 put_bits(pb, 3,
s->loro_surround_mix_level);
1695 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1700 for (ch = 0; ch <
s->fbw_channels; ch++)
1706 for (ch = 0; ch <
s->fbw_channels; ch++)
1720 if (
block->new_cpl_strategy) {
1723 if (
block->cpl_in_use) {
1724 int start_sub, end_sub;
1728 for (ch = 1; ch <=
s->fbw_channels; ch++)
1733 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1734 end_sub = (
s->cpl_end_freq - 37) / 12;
1741 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1748 if (
block->cpl_in_use) {
1749 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1750 if (
block->channel_in_cpl[ch]) {
1751 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1753 if (
block->new_cpl_coords[ch]) {
1755 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1766 if (!
s->eac3 ||
blk > 0)
1768 if (
block->new_rematrixing_strategy) {
1770 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1777 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1784 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1792 int cpl = (ch ==
CPL_CH);
1802 for (
i = 1;
i <= nb_groups;
i++)
1806 if (ch !=
s->lfe_channel && !cpl)
1826 if (
block->new_snr_offsets) {
1829 put_bits(pb, 4,
s->fine_snr_offset[ch]);
1838 if (
block->cpl_in_use) {
1839 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1841 if (
block->new_cpl_leak) {
1842 put_bits(pb, 3,
s->bit_alloc.cpl_fast_leak);
1843 put_bits(pb, 3,
s->bit_alloc.cpl_slow_leak);
1853 got_cpl = !
block->cpl_in_use;
1854 for (ch = 1; ch <=
s->channels; ch++) {
1857 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1862 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1864 b =
s->ref_bap[ch][
blk][
i];
1867 case 1:
if (q != 128)
put_bits (pb, 5, q);
break;
1868 case 2:
if (q != 128)
put_bits (pb, 7, q);
break;
1870 case 4:
if (q != 128)
put_bits (pb, 7, q);
break;
1883 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1886 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1903 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1923 int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1926 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1939 crc2 =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 4);
1944 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1950 s->frame_size - frame_size_58 - 2);
1954 if (crc2 == 0x0B77) {
1957 frame[
s->frame_size - 3] ^= 0x1;
1977 s->output_frame_header(
s, &pb);
1991 if (
s->options.allow_per_frame_metadata) {
1997 if (
s->bit_alloc.sr_code == 1 ||
s->eac3)
2000 s->encode_frame(
s,
frame->extended_data);
2024 *got_packet_ptr = 1;
2036 switch (
s->bitstream_id) {
2037 case 6: msg =
"AC-3 (alt syntax)";
break;
2038 case 8: msg =
"AC-3 (standard)";
break;
2039 case 16: msg =
"E-AC-3 (enhanced)";
break;
2040 default: msg =
"ERROR";
2042 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", msg,
s->bitstream_id);
2045 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
2046 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
2047 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
2048 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
2050 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
2052 ff_dlog(avctx,
"per_frame_metadata: %s\n",
2056 s->center_mix_level);
2058 ff_dlog(avctx,
"center_mixlev: {not written}\n");
2059 if (
s->has_surround)
2061 s->surround_mix_level);
2063 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
2074 ff_dlog(avctx,
"room_type: %s\n", msg);
2076 ff_dlog(avctx,
"mixing_level: {not written}\n");
2077 ff_dlog(avctx,
"room_type: {not written}\n");
2090 ff_dlog(avctx,
"dsur_mode: %s\n", msg);
2092 ff_dlog(avctx,
"dsur_mode: {not written}\n");
2096 if (
s->bitstream_id == 6) {
2106 ff_dlog(avctx,
"dmix_mode: %s\n", msg);
2107 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
2109 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
2111 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
2113 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
2116 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
2127 ff_dlog(avctx,
"dsurex_mode: %s\n", msg);
2136 ff_dlog(avctx,
"dheadphone_mode: %s\n", msg);
2145 ff_dlog(avctx,
"ad_conv_type: %s\n", msg);
2147 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
2162 for (
int ch = 0; ch <
s->channels; ch++)
2195 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2209 s->has_surround =
s->channel_mode & 0x04;
2222 for (
int i = 0;;
i++) {
2224 s->bit_alloc.sr_code =
i;
2230 s->bitstream_id =
s->eac3 ? 16 : 8;
2234 switch (
s->fbw_channels) {
2235 case 1: avctx->
bit_rate = 96000;
break;
2236 case 2: avctx->
bit_rate = 192000;
break;
2237 case 3: avctx->
bit_rate = 320000;
break;
2238 case 4: avctx->
bit_rate = 384000;
break;
2239 case 5: avctx->
bit_rate = 448000;
break;
2245 int max_br, min_br, wpf, min_br_code;
2247 long long min_br_dist;
2253 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2254 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2263 "for this sample rate\n", min_br, max_br);
2266 s->num_blks_code = num_blks_code;
2267 s->num_blocks = num_blocks;
2277 min_br_dist = INT64_MAX;
2278 for (
int i = 0;
i < 19;
i++) {
2280 if (br_dist < min_br_dist) {
2281 min_br_dist = br_dist;
2287 s->frame_size_code = min_br_code << 1;
2290 s->frame_size_min = 2 * wpf;
2292 int best_br = 0, best_code = 0;
2293 long long best_diff = INT64_MAX;
2294 for (
int i = 0;
i < 19;
i++) {
2297 if (
diff < best_diff) {
2306 s->frame_size_code = best_code << 1;
2308 s->num_blks_code = 0x3;
2312 s->frame_size =
s->frame_size_min;
2320 if (
s->cutoff > (
s->sample_rate >> 1))
2321 s->cutoff =
s->sample_rate >> 1;
2327 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2330 s->cpl_enabled =
s->options.channel_coupling &&
2350 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2353 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2357 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2358 s->start_freq[ch] = 0;
2360 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2364 s->start_freq[
s->lfe_channel] = 0;
2366 s->blocks[
blk].end_freq[ch] = 7;
2370 if (
s->cpl_enabled) {
2372 cpl_start =
s->options.cpl_start;
2375 if (cpl_start < 0) {
2383 if (
s->cpl_enabled) {
2384 int i, cpl_start_band, cpl_end_band;
2385 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2387 cpl_end_band =
s->bandwidth_code / 4 + 3;
2388 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2390 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2392 s->num_cpl_bands = 1;
2393 *cpl_band_sizes = 12;
2394 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2396 *cpl_band_sizes += 12;
2400 *cpl_band_sizes = 12;
2404 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2405 s->cpl_end_freq = cpl_end_band * 12 + 37;
2407 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2416 int channel_blocks =
channels *
s->num_blocks;
2418 uint8_t *cpl_coord_mant_buffer;
2421 for (
int ch = 0; ch <
s->channels; ch++) {
2423 if (!
s->planar_samples[ch])
2438 if (!
s->fixed_point) {
2442 if (
s->cpl_enabled) {
2445 cpl_coord_mant_buffer =
s->cpl_coord_buffer + 16 * channel_blocks;
2450 for (ch = 0; ch <
channels; ch++) {
2457 if (
s->cpl_enabled) {
2459 block->cpl_coord_mant[ch] = &cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2480 int ret, frame_size_58;
2493 s->bitstream_mode = 0x7;
2495 s->bits_written = 0;
2496 s->samples_written = 0;
2499 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2501 if (
s->bit_alloc.sr_code == 1) {
2502 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2506 if (!
s->output_frame_header)