42 #include "config_components.h"
54 #define SAMPLETYPE_SIZE(ctx) (sizeof(float) == sizeof(int32_t) ? sizeof(float) : \
55 (ctx)->fixed_point ? sizeof(int32_t) : sizeof(float))
62 #define CMIXLEV_NUM_OPTIONS 3
67 #define SURMIXLEV_NUM_OPTIONS 3
72 #define EXTMIXLEV_NUM_OPTIONS 8
73 #define extmixlev_options ff_ac3_gain_levels
78 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
79 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
198 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
199 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
210 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
211 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
212 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
214 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
215 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
216 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
218 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
219 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
220 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
222 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
223 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
224 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
226 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
227 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
228 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
244 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
245 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
246 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
249 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
250 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
251 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
256 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
259 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
260 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
261 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
264 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
265 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
266 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
269 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
270 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
271 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
275 #define FLT_OPTION_THRESHOLD 0.01
281 for (
i = 0;
i < v_list_size;
i++) {
286 if (
i == v_list_size)
294 float *opt_param,
const float *
list,
295 int list_size,
int default_value,
int min_value,
299 if (mixlev < min_value) {
300 mixlev = default_value;
301 if (*opt_param >= 0.0) {
303 "default value: %0.3f\n", opt_name,
list[mixlev]);
306 *opt_param =
list[mixlev];
338 if (
s->has_surround &&
379 &
s->center_mix_level);
381 if (
s->has_surround) {
384 &
s->surround_mix_level);
393 if (!
s->eac3 ||
s->has_center) {
398 &
s->ltrt_center_mix_level);
403 &
s->loro_center_mix_level);
405 if (!
s->eac3 ||
s->has_surround) {
410 &
s->ltrt_surround_mix_level);
415 &
s->loro_surround_mix_level);
427 "specified number of channels\n");
464 "room_type is set\n");
492 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
493 s->bits_written -=
s->bit_rate;
494 s->samples_written -=
s->sample_rate;
496 s->frame_size =
s->frame_size_min +
497 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
498 s->bits_written +=
s->frame_size * 8;
517 for (ch = 1; ch <=
s->fbw_channels; ch++)
518 block->channel_in_cpl[ch] =
s->cpl_on;
527 block->num_cpl_channels = 0;
528 for (ch = 1; ch <=
s->fbw_channels; ch++)
529 block->num_cpl_channels +=
block->channel_in_cpl[ch];
530 block->cpl_in_use =
block->num_cpl_channels > 1;
531 num_cpl_blocks +=
block->cpl_in_use;
533 block->num_cpl_channels = 0;
534 for (ch = 1; ch <=
s->fbw_channels; ch++)
535 block->channel_in_cpl[ch] = 0;
540 for (ch = 1; ch <=
s->fbw_channels; ch++) {
541 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
542 block->new_cpl_strategy = 1;
547 block->new_cpl_leak =
block->new_cpl_strategy;
549 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
550 block->new_snr_offsets = 1;
551 if (
block->cpl_in_use)
554 block->new_snr_offsets = 0;
563 for (ch = 1; ch <=
s->fbw_channels; ch++) {
564 if (
block->channel_in_cpl[ch])
567 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
585 if (!
s->rematrixing_enabled)
590 if (
block->new_rematrixing_strategy)
593 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
597 for (
i = start;
i < end;
i++) {
600 block->fixed_coef[1][
i] = (lt + rt) >> 1;
601 block->fixed_coef[2][
i] = (lt - rt) >> 1;
614 int expstr,
i, grpsize;
617 grpsize = 3 << expstr;
618 for (
i = 12;
i < 256;
i++) {
634 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
637 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
645 #define EXP_DIFF_THRESHOLD 500
665 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
666 uint8_t *exp_strategy =
s->exp_strategy[ch];
667 uint8_t *
exp =
s->blocks[0].exp[ch];
676 if (!
s->blocks[
blk-1].cpl_in_use) {
679 }
else if (!
s->blocks[
blk].cpl_in_use) {
683 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
698 while (blk < s->num_blocks) {
700 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
714 if (CONFIG_EAC3_ENCODER &&
s->eac3)
735 switch(exp_strategy) {
737 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
738 uint8_t exp_min =
exp[k];
739 if (
exp[k+1] < exp_min)
741 exp[
i-cpl] = exp_min;
746 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
747 uint8_t exp_min =
exp[k];
748 if (
exp[k+1] < exp_min)
750 if (
exp[k+2] < exp_min)
752 if (
exp[k+3] < exp_min)
754 exp[
i-cpl] = exp_min;
761 if (!cpl &&
exp[0] > 15)
766 for (
i = 1;
i <= nb_groups;
i++)
776 switch (exp_strategy) {
778 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
779 uint8_t exp1 =
exp[
i-cpl];
785 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
802 int blk, blk1, ch, cpl;
803 uint8_t *
exp, *exp_strategy;
806 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
807 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
808 exp_strategy =
s->exp_strategy[ch];
812 while (blk < s->num_blocks) {
814 if (cpl && !
block->cpl_in_use) {
824 s->exp_ref_block[ch][
blk] =
blk;
825 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
826 s->exp_ref_block[ch][blk1] =
blk;
829 num_reuse_blocks = blk1 -
blk - 1;
832 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
853 int nb_groups, bit_count;
859 int exp_strategy =
s->exp_strategy[ch][
blk];
867 bit_count += 4 + (nb_groups * 7);
885 int group_size, nb_groups;
887 int delta0, delta1, delta2;
893 int exp_strategy =
s->exp_strategy[ch][
blk];
897 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
899 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
903 block->grouped_exp[ch][0] = exp1;
906 for (
i = 1;
i <= nb_groups;
i++) {
911 delta0 = exp1 - exp0 + 2;
917 delta1 = exp1 - exp0 + 2;
923 delta2 = exp1 - exp0 + 2;
926 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
956 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
975 if (
s->num_blocks != 0x6)
979 if (
s->num_blocks == 6)
983 if (
s->use_frame_exp_strategy)
984 frame_bits += 5 *
s->fbw_channels;
986 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
988 frame_bits +=
s->num_blocks;
990 if (
s->num_blks_code != 0x3)
993 frame_bits +=
s->fbw_channels * 5;
997 if (
s->num_blocks != 1)
1001 frame_bits += frame_bits_inc[
s->channel_mode];
1008 frame_bits +=
s->fbw_channels;
1011 frame_bits +=
s->fbw_channels;
1027 frame_bits += 2 *
s->fbw_channels;
1034 frame_bits += 2 + 2 + 2 + 2 + 3;
1053 frame_bits += 1 + 16;
1055 s->frame_bits_fixed = frame_bits;
1068 s->slow_decay_code = 2;
1069 s->fast_decay_code = 1;
1070 s->slow_gain_code = 1;
1071 s->db_per_bit_code =
s->eac3 ? 2 : 3;
1073 for (ch = 0; ch <=
s->channels; ch++)
1074 s->fast_gain_code[ch] = 4;
1077 s->coarse_snr_offset = 40;
1087 s->bit_alloc.cpl_fast_leak = 0;
1088 s->bit_alloc.cpl_slow_leak = 0;
1112 if (
s->has_surround)
1114 frame_bits +=
s->lfe_on;
1115 frame_bits += 1 + 1 + 2;
1121 frame_bits += 3 + 1 + 1;
1123 frame_bits += 2 + 2;
1128 frame_bits += 5 + 2 + 1;
1137 if (
block->new_cpl_strategy)
1143 if (
s->use_frame_exp_strategy) {
1147 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
1153 if (
s->bitstream_id == 6) {
1166 if (
block->new_cpl_strategy) {
1169 if (
block->cpl_in_use) {
1173 frame_bits +=
s->fbw_channels;
1176 frame_bits += 4 + 4;
1180 frame_bits +=
s->num_cpl_subbands - 1;
1185 if (
block->cpl_in_use) {
1186 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1187 if (
block->channel_in_cpl[ch]) {
1188 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1190 if (
block->new_cpl_coords[ch]) {
1192 frame_bits += (4 + 4) *
s->num_cpl_bands;
1200 if (!
s->eac3 ||
blk > 0)
1202 if (
s->blocks[
blk].new_rematrixing_strategy)
1203 frame_bits +=
block->num_rematrixing_bands;
1207 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1209 if (!
block->channel_in_cpl[ch])
1216 if (!
s->eac3 &&
block->cpl_in_use)
1221 if (
block->new_snr_offsets)
1222 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1226 if (
block->cpl_in_use) {
1227 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1229 if (
block->new_cpl_leak)
1230 frame_bits += 3 + 3;
1234 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1255 block->band_psd[ch]);
1257 s->start_freq[ch],
block->end_freq[ch],
1259 ch ==
s->lfe_channel,
1277 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1280 ref_bap =
s->bap_buffer;
1281 for (ch = 0; ch <=
s->channels; ch++) {
1302 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1303 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1304 mant_cnt[
blk][4] = 1;
1329 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1330 s->ref_bap[ch][
blk] + start,
1341 int ch, max_end_freq;
1346 max_end_freq =
s->bandwidth_code * 3 + 73;
1347 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1351 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1369 snr_offset = (snr_offset - 240) * 4;
1381 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1382 s->start_freq[ch],
block->end_freq[ch],
1383 snr_offset,
s->bit_alloc.floor,
1400 int snr_offset, snr_incr;
1402 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1406 snr_offset =
s->coarse_snr_offset << 4;
1410 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1415 while (snr_offset >= 0 &&
1422 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1423 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1424 while (snr_offset + snr_incr <= 1023 &&
1426 snr_offset += snr_incr;
1427 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1430 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1433 s->coarse_snr_offset = snr_offset >> 4;
1434 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1435 s->fine_snr_offset[ch] = snr_offset & 0xF;
1469 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1487 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1488 m = (1 << (qbits-1));
1508 uint8_t *
exp, uint8_t *bap,
1509 int16_t *qmant,
int start_freq,
1514 for (
i = start_freq;
i < end_freq;
i++) {
1515 int c = fixed_coef[
i];
1523 switch (
s->mant1_cnt) {
1525 s->qmant1_ptr = &qmant[
i];
1530 *
s->qmant1_ptr += 3 * v;
1535 *
s->qmant1_ptr += v;
1543 switch (
s->mant2_cnt) {
1545 s->qmant2_ptr = &qmant[
i];
1550 *
s->qmant2_ptr += 5 * v;
1555 *
s->qmant2_ptr += v;
1566 switch (
s->mant4_cnt) {
1568 s->qmant4_ptr = &qmant[
i];
1573 *
s->qmant4_ptr += v;
1604 int blk, ch, ch0=0, got_cpl;
1610 got_cpl = !
block->cpl_in_use;
1611 for (ch = 1; ch <=
s->channels; ch++) {
1612 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1618 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1620 s->start_freq[ch],
block->end_freq[ch]);
1640 put_bits(pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1646 if (
s->channel_mode & 0x04)
1661 if (
s->bitstream_id == 6) {
1666 put_bits(pb, 3,
s->ltrt_center_mix_level);
1667 put_bits(pb, 3,
s->ltrt_surround_mix_level);
1668 put_bits(pb, 3,
s->loro_center_mix_level);
1669 put_bits(pb, 3,
s->loro_surround_mix_level);
1691 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1696 for (ch = 0; ch <
s->fbw_channels; ch++)
1702 for (ch = 0; ch <
s->fbw_channels; ch++)
1716 if (
block->new_cpl_strategy) {
1719 if (
block->cpl_in_use) {
1720 int start_sub, end_sub;
1724 for (ch = 1; ch <=
s->fbw_channels; ch++)
1729 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1730 end_sub = (
s->cpl_end_freq - 37) / 12;
1737 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1744 if (
block->cpl_in_use) {
1745 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1746 if (
block->channel_in_cpl[ch]) {
1747 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1749 if (
block->new_cpl_coords[ch]) {
1751 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1762 if (!
s->eac3 ||
blk > 0)
1764 if (
block->new_rematrixing_strategy) {
1766 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1773 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1780 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1788 int cpl = (ch ==
CPL_CH);
1798 for (
i = 1;
i <= nb_groups;
i++)
1802 if (ch !=
s->lfe_channel && !cpl)
1822 if (
block->new_snr_offsets) {
1825 put_bits(pb, 4,
s->fine_snr_offset[ch]);
1834 if (
block->cpl_in_use) {
1835 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1837 if (
block->new_cpl_leak) {
1838 put_bits(pb, 3,
s->bit_alloc.cpl_fast_leak);
1839 put_bits(pb, 3,
s->bit_alloc.cpl_slow_leak);
1849 got_cpl = !
block->cpl_in_use;
1850 for (ch = 1; ch <=
s->channels; ch++) {
1853 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1858 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1860 b =
s->ref_bap[ch][
blk][
i];
1863 case 1:
if (q != 128)
put_bits (pb, 5, q);
break;
1864 case 2:
if (q != 128)
put_bits (pb, 7, q);
break;
1866 case 4:
if (q != 128)
put_bits (pb, 7, q);
break;
1879 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1882 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1899 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1919 int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1922 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1935 crc2 =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 4);
1940 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1946 s->frame_size - frame_size_58 - 2);
1950 if (crc2 == 0x0B77) {
1953 frame[
s->frame_size - 3] ^= 0x1;
1973 s->output_frame_header(
s, &pb);
1987 if (
s->options.allow_per_frame_metadata) {
1993 if (
s->bit_alloc.sr_code == 1 ||
s->eac3)
1996 s->encode_frame(
s,
frame->extended_data);
2020 *got_packet_ptr = 1;
2032 switch (
s->bitstream_id) {
2033 case 6: msg =
"AC-3 (alt syntax)";
break;
2034 case 8: msg =
"AC-3 (standard)";
break;
2035 case 16: msg =
"E-AC-3 (enhanced)";
break;
2036 default: msg =
"ERROR";
2038 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", msg,
s->bitstream_id);
2041 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
2042 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
2043 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
2044 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
2046 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
2048 ff_dlog(avctx,
"per_frame_metadata: %s\n",
2052 s->center_mix_level);
2054 ff_dlog(avctx,
"center_mixlev: {not written}\n");
2055 if (
s->has_surround)
2057 s->surround_mix_level);
2059 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
2070 ff_dlog(avctx,
"room_type: %s\n", msg);
2072 ff_dlog(avctx,
"mixing_level: {not written}\n");
2073 ff_dlog(avctx,
"room_type: {not written}\n");
2086 ff_dlog(avctx,
"dsur_mode: %s\n", msg);
2088 ff_dlog(avctx,
"dsur_mode: {not written}\n");
2092 if (
s->bitstream_id == 6) {
2102 ff_dlog(avctx,
"dmix_mode: %s\n", msg);
2103 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
2105 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
2107 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
2109 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
2112 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
2123 ff_dlog(avctx,
"dsurex_mode: %s\n", msg);
2132 ff_dlog(avctx,
"dheadphone_mode: %s\n", msg);
2141 ff_dlog(avctx,
"ad_conv_type: %s\n", msg);
2143 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
2158 for (
int ch = 0; ch <
s->channels; ch++)
2191 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2205 s->has_surround =
s->channel_mode & 0x04;
2218 for (
int i = 0;;
i++) {
2220 s->bit_alloc.sr_code =
i;
2226 s->bitstream_id =
s->eac3 ? 16 : 8;
2230 switch (
s->fbw_channels) {
2231 case 1: avctx->
bit_rate = 96000;
break;
2232 case 2: avctx->
bit_rate = 192000;
break;
2233 case 3: avctx->
bit_rate = 320000;
break;
2234 case 4: avctx->
bit_rate = 384000;
break;
2235 case 5: avctx->
bit_rate = 448000;
break;
2241 int max_br, min_br, wpf, min_br_code;
2243 long long min_br_dist;
2249 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2250 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2259 "for this sample rate\n", min_br, max_br);
2262 s->num_blks_code = num_blks_code;
2263 s->num_blocks = num_blocks;
2273 min_br_dist = INT64_MAX;
2274 for (
int i = 0;
i < 19;
i++) {
2276 if (br_dist < min_br_dist) {
2277 min_br_dist = br_dist;
2283 s->frame_size_code = min_br_code << 1;
2286 s->frame_size_min = 2 * wpf;
2288 int best_br = 0, best_code = 0;
2289 long long best_diff = INT64_MAX;
2290 for (
int i = 0;
i < 19;
i++) {
2293 if (
diff < best_diff) {
2302 s->frame_size_code = best_code << 1;
2304 s->num_blks_code = 0x3;
2308 s->frame_size =
s->frame_size_min;
2316 if (
s->cutoff > (
s->sample_rate >> 1))
2317 s->cutoff =
s->sample_rate >> 1;
2323 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2326 s->cpl_enabled =
s->options.channel_coupling &&
2346 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2349 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2353 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2354 s->start_freq[ch] = 0;
2356 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2360 s->start_freq[
s->lfe_channel] = 0;
2362 s->blocks[
blk].end_freq[ch] = 7;
2366 if (
s->cpl_enabled) {
2368 cpl_start =
s->options.cpl_start;
2371 if (cpl_start < 0) {
2379 if (
s->cpl_enabled) {
2380 int i, cpl_start_band, cpl_end_band;
2381 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2383 cpl_end_band =
s->bandwidth_code / 4 + 3;
2384 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2386 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2388 s->num_cpl_bands = 1;
2389 *cpl_band_sizes = 12;
2390 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2392 *cpl_band_sizes += 12;
2396 *cpl_band_sizes = 12;
2400 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2401 s->cpl_end_freq = cpl_end_band * 12 + 37;
2403 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2412 int channel_blocks =
channels *
s->num_blocks;
2414 uint8_t *cpl_coord_mant_buffer;
2417 for (
int ch = 0; ch <
s->channels; ch++) {
2419 if (!
s->planar_samples[ch])
2434 if (!
s->fixed_point) {
2438 if (
s->cpl_enabled) {
2441 cpl_coord_mant_buffer =
s->cpl_coord_buffer + 16 * channel_blocks;
2446 for (ch = 0; ch <
channels; ch++) {
2453 if (
s->cpl_enabled) {
2455 block->cpl_coord_mant[ch] = &cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2476 int ret, frame_size_58;
2489 s->bitstream_mode = 0x7;
2491 s->bits_written = 0;
2492 s->samples_written = 0;
2495 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2497 if (
s->bit_alloc.sr_code == 1) {
2498 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2502 if (!
s->output_frame_header)