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
74 #define extmixlev_options ff_ac3_gain_levels
79 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
80 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
199 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
200 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
211 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
212 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
213 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
215 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
216 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
217 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
219 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
220 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
221 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
223 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
224 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
225 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
227 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
228 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
229 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
245 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
246 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
247 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -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 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
252 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
257 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
260 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
261 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
262 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
265 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
266 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
267 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
270 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
271 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
272 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
276 #define FLT_OPTION_THRESHOLD 0.01
282 for (
i = 0;
i < v_list_size;
i++) {
287 if (
i == v_list_size)
295 float *opt_param,
const float *
list,
296 int list_size,
int default_value,
int min_value,
300 if (mixlev < min_value) {
301 mixlev = default_value;
302 if (*opt_param >= 0.0) {
304 "default value: %0.3f\n", opt_name,
list[mixlev]);
307 *opt_param =
list[mixlev];
339 if (
s->has_surround &&
380 &
s->center_mix_level);
382 if (
s->has_surround) {
385 &
s->surround_mix_level);
394 if (!
s->eac3 ||
s->has_center) {
399 &
s->ltrt_center_mix_level);
404 &
s->loro_center_mix_level);
406 if (!
s->eac3 ||
s->has_surround) {
411 &
s->ltrt_surround_mix_level);
416 &
s->loro_surround_mix_level);
428 "specified number of channels\n");
465 "room_type is set\n");
493 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
494 s->bits_written -=
s->bit_rate;
495 s->samples_written -=
s->sample_rate;
497 s->frame_size =
s->frame_size_min +
498 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
499 s->bits_written +=
s->frame_size * 8;
518 for (ch = 1; ch <=
s->fbw_channels; ch++)
519 block->channel_in_cpl[ch] =
s->cpl_on;
528 block->num_cpl_channels = 0;
529 for (ch = 1; ch <=
s->fbw_channels; ch++)
530 block->num_cpl_channels +=
block->channel_in_cpl[ch];
531 block->cpl_in_use =
block->num_cpl_channels > 1;
532 num_cpl_blocks +=
block->cpl_in_use;
534 block->num_cpl_channels = 0;
535 for (ch = 1; ch <=
s->fbw_channels; ch++)
536 block->channel_in_cpl[ch] = 0;
541 for (ch = 1; ch <=
s->fbw_channels; ch++) {
542 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
543 block->new_cpl_strategy = 1;
548 block->new_cpl_leak =
block->new_cpl_strategy;
550 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
551 block->new_snr_offsets = 1;
552 if (
block->cpl_in_use)
555 block->new_snr_offsets = 0;
564 for (ch = 1; ch <=
s->fbw_channels; ch++) {
565 if (
block->channel_in_cpl[ch])
568 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
586 if (!
s->rematrixing_enabled)
591 if (
block->new_rematrixing_strategy)
594 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
598 for (
i = start;
i < end;
i++) {
601 block->fixed_coef[1][
i] = (lt + rt) >> 1;
602 block->fixed_coef[2][
i] = (lt - rt) >> 1;
615 int expstr,
i, grpsize;
618 grpsize = 3 << expstr;
619 for (
i = 12;
i < 256;
i++) {
635 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
638 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
646 #define EXP_DIFF_THRESHOLD 500
666 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
667 uint8_t *exp_strategy =
s->exp_strategy[ch];
668 uint8_t *
exp =
s->blocks[0].exp[ch];
677 if (!
s->blocks[
blk-1].cpl_in_use) {
680 }
else if (!
s->blocks[
blk].cpl_in_use) {
684 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
699 while (blk < s->num_blocks) {
701 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
715 if (CONFIG_EAC3_ENCODER &&
s->eac3)
736 switch(exp_strategy) {
738 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
739 uint8_t exp_min =
exp[k];
740 if (
exp[k+1] < exp_min)
742 exp[
i-cpl] = exp_min;
747 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
748 uint8_t exp_min =
exp[k];
749 if (
exp[k+1] < exp_min)
751 if (
exp[k+2] < exp_min)
753 if (
exp[k+3] < exp_min)
755 exp[
i-cpl] = exp_min;
762 if (!cpl &&
exp[0] > 15)
767 for (
i = 1;
i <= nb_groups;
i++)
777 switch (exp_strategy) {
779 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
780 uint8_t exp1 =
exp[
i-cpl];
786 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
803 int blk, blk1, ch, cpl;
804 uint8_t *
exp, *exp_strategy;
807 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
808 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
809 exp_strategy =
s->exp_strategy[ch];
813 while (blk < s->num_blocks) {
815 if (cpl && !
block->cpl_in_use) {
825 s->exp_ref_block[ch][
blk] =
blk;
826 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
827 s->exp_ref_block[ch][blk1] =
blk;
830 num_reuse_blocks = blk1 -
blk - 1;
833 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
854 int nb_groups, bit_count;
860 int exp_strategy =
s->exp_strategy[ch][
blk];
868 bit_count += 4 + (nb_groups * 7);
886 int group_size, nb_groups;
888 int delta0, delta1, delta2;
894 int exp_strategy =
s->exp_strategy[ch][
blk];
898 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
900 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
904 block->grouped_exp[ch][0] = exp1;
907 for (
i = 1;
i <= nb_groups;
i++) {
912 delta0 = exp1 - exp0 + 2;
918 delta1 = exp1 - exp0 + 2;
924 delta2 = exp1 - exp0 + 2;
927 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
959 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
978 if (
s->num_blocks != 0x6)
982 if (
s->num_blocks == 6)
986 if (
s->use_frame_exp_strategy)
987 frame_bits += 5 *
s->fbw_channels;
989 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
991 frame_bits +=
s->num_blocks;
993 if (
s->num_blks_code != 0x3)
996 frame_bits +=
s->fbw_channels * 5;
1000 if (
s->num_blocks != 1)
1004 frame_bits += frame_bits_inc[
s->channel_mode];
1011 frame_bits +=
s->fbw_channels;
1014 frame_bits +=
s->fbw_channels;
1030 frame_bits += 2 *
s->fbw_channels;
1037 frame_bits += 2 + 2 + 2 + 2 + 3;
1056 frame_bits += 1 + 16;
1058 s->frame_bits_fixed = frame_bits;
1071 s->slow_decay_code = 2;
1072 s->fast_decay_code = 1;
1073 s->slow_gain_code = 1;
1074 s->db_per_bit_code =
s->eac3 ? 2 : 3;
1076 for (ch = 0; ch <=
s->channels; ch++)
1077 s->fast_gain_code[ch] = 4;
1080 s->coarse_snr_offset = 40;
1090 s->bit_alloc.cpl_fast_leak = 0;
1091 s->bit_alloc.cpl_slow_leak = 0;
1115 if (
s->has_surround)
1117 frame_bits +=
s->lfe_on;
1118 frame_bits += 1 + 1 + 2;
1124 frame_bits += 3 + 1 + 1;
1126 frame_bits += 2 + 2;
1131 frame_bits += 5 + 2 + 1;
1140 if (
block->new_cpl_strategy)
1146 if (
s->use_frame_exp_strategy) {
1150 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
1156 if (
s->bitstream_id == 6) {
1169 if (
block->new_cpl_strategy) {
1172 if (
block->cpl_in_use) {
1176 frame_bits +=
s->fbw_channels;
1179 frame_bits += 4 + 4;
1183 frame_bits +=
s->num_cpl_subbands - 1;
1188 if (
block->cpl_in_use) {
1189 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1190 if (
block->channel_in_cpl[ch]) {
1191 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1193 if (
block->new_cpl_coords[ch]) {
1195 frame_bits += (4 + 4) *
s->num_cpl_bands;
1203 if (!
s->eac3 ||
blk > 0)
1205 if (
s->blocks[
blk].new_rematrixing_strategy)
1206 frame_bits +=
block->num_rematrixing_bands;
1210 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1212 if (!
block->channel_in_cpl[ch])
1219 if (!
s->eac3 &&
block->cpl_in_use)
1224 if (
block->new_snr_offsets)
1225 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1229 if (
block->cpl_in_use) {
1230 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1232 if (
block->new_cpl_leak)
1233 frame_bits += 3 + 3;
1237 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1258 block->band_psd[ch]);
1260 s->start_freq[ch],
block->end_freq[ch],
1262 ch ==
s->lfe_channel,
1280 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1283 ref_bap =
s->bap_buffer;
1284 for (ch = 0; ch <=
s->channels; ch++) {
1305 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1306 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1307 mant_cnt[
blk][4] = 1;
1332 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1333 s->ref_bap[ch][
blk] + start,
1344 int ch, max_end_freq;
1349 max_end_freq =
s->bandwidth_code * 3 + 73;
1350 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1354 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1372 snr_offset = (snr_offset - 240) * 4;
1384 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1385 s->start_freq[ch],
block->end_freq[ch],
1386 snr_offset,
s->bit_alloc.floor,
1403 int snr_offset, snr_incr;
1405 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1409 snr_offset =
s->coarse_snr_offset << 4;
1413 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1418 while (snr_offset >= 0 &&
1425 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1426 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1427 while (snr_offset + snr_incr <= 1023 &&
1429 snr_offset += snr_incr;
1430 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1433 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1436 s->coarse_snr_offset = snr_offset >> 4;
1437 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1438 s->fine_snr_offset[ch] = snr_offset & 0xF;
1472 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1490 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1491 m = (1 << (qbits-1));
1511 uint8_t *
exp, uint8_t *bap,
1512 int16_t *qmant,
int start_freq,
1517 for (
i = start_freq;
i < end_freq;
i++) {
1518 int c = fixed_coef[
i];
1526 switch (
s->mant1_cnt) {
1528 s->qmant1_ptr = &qmant[
i];
1533 *
s->qmant1_ptr += 3 * v;
1538 *
s->qmant1_ptr += v;
1546 switch (
s->mant2_cnt) {
1548 s->qmant2_ptr = &qmant[
i];
1553 *
s->qmant2_ptr += 5 * v;
1558 *
s->qmant2_ptr += v;
1569 switch (
s->mant4_cnt) {
1571 s->qmant4_ptr = &qmant[
i];
1576 *
s->qmant4_ptr += v;
1607 int blk, ch, ch0=0, got_cpl;
1613 got_cpl = !
block->cpl_in_use;
1614 for (ch = 1; ch <=
s->channels; ch++) {
1615 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1621 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1623 s->start_freq[ch],
block->end_freq[ch]);
1643 put_bits(pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1649 if (
s->channel_mode & 0x04)
1664 if (
s->bitstream_id == 6) {
1669 put_bits(pb, 3,
s->ltrt_center_mix_level);
1670 put_bits(pb, 3,
s->ltrt_surround_mix_level);
1671 put_bits(pb, 3,
s->loro_center_mix_level);
1672 put_bits(pb, 3,
s->loro_surround_mix_level);
1694 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1699 for (ch = 0; ch <
s->fbw_channels; ch++)
1705 for (ch = 0; ch <
s->fbw_channels; ch++)
1719 if (
block->new_cpl_strategy) {
1722 if (
block->cpl_in_use) {
1723 int start_sub, end_sub;
1727 for (ch = 1; ch <=
s->fbw_channels; ch++)
1732 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1733 end_sub = (
s->cpl_end_freq - 37) / 12;
1740 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1747 if (
block->cpl_in_use) {
1748 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1749 if (
block->channel_in_cpl[ch]) {
1750 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1752 if (
block->new_cpl_coords[ch]) {
1754 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1765 if (!
s->eac3 ||
blk > 0)
1767 if (
block->new_rematrixing_strategy) {
1769 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1776 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1783 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1791 int cpl = (ch ==
CPL_CH);
1801 for (
i = 1;
i <= nb_groups;
i++)
1805 if (ch !=
s->lfe_channel && !cpl)
1825 if (
block->new_snr_offsets) {
1828 put_bits(pb, 4,
s->fine_snr_offset[ch]);
1837 if (
block->cpl_in_use) {
1838 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1840 if (
block->new_cpl_leak) {
1841 put_bits(pb, 3,
s->bit_alloc.cpl_fast_leak);
1842 put_bits(pb, 3,
s->bit_alloc.cpl_slow_leak);
1852 got_cpl = !
block->cpl_in_use;
1853 for (ch = 1; ch <=
s->channels; ch++) {
1856 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1861 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1863 b =
s->ref_bap[ch][
blk][
i];
1866 case 1:
if (q != 128)
put_bits (pb, 5, q);
break;
1867 case 2:
if (q != 128)
put_bits (pb, 7, q);
break;
1869 case 4:
if (q != 128)
put_bits (pb, 7, q);
break;
1882 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1885 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1902 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1922 int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1925 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1938 crc2 =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 4);
1943 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1949 s->frame_size - frame_size_58 - 2);
1953 if (crc2 == 0x0B77) {
1956 frame[
s->frame_size - 3] ^= 0x1;
1976 s->output_frame_header(
s, &pb);
1990 if (
s->options.allow_per_frame_metadata) {
1996 if (
s->bit_alloc.sr_code == 1 ||
s->eac3)
1999 s->encode_frame(
s,
frame->extended_data);
2023 *got_packet_ptr = 1;
2035 switch (
s->bitstream_id) {
2036 case 6: msg =
"AC-3 (alt syntax)";
break;
2037 case 8: msg =
"AC-3 (standard)";
break;
2038 case 16: msg =
"E-AC-3 (enhanced)";
break;
2039 default: msg =
"ERROR";
2041 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", msg,
s->bitstream_id);
2044 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
2045 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
2046 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
2047 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
2049 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
2051 ff_dlog(avctx,
"per_frame_metadata: %s\n",
2055 s->center_mix_level);
2057 ff_dlog(avctx,
"center_mixlev: {not written}\n");
2058 if (
s->has_surround)
2060 s->surround_mix_level);
2062 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
2073 ff_dlog(avctx,
"room_type: %s\n", msg);
2075 ff_dlog(avctx,
"mixing_level: {not written}\n");
2076 ff_dlog(avctx,
"room_type: {not written}\n");
2089 ff_dlog(avctx,
"dsur_mode: %s\n", msg);
2091 ff_dlog(avctx,
"dsur_mode: {not written}\n");
2095 if (
s->bitstream_id == 6) {
2105 ff_dlog(avctx,
"dmix_mode: %s\n", msg);
2106 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
2108 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
2110 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
2112 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
2115 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
2126 ff_dlog(avctx,
"dsurex_mode: %s\n", msg);
2135 ff_dlog(avctx,
"dheadphone_mode: %s\n", msg);
2144 ff_dlog(avctx,
"ad_conv_type: %s\n", msg);
2146 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
2161 for (
int ch = 0; ch <
s->channels; ch++)
2194 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2208 s->has_surround =
s->channel_mode & 0x04;
2221 for (
int i = 0;;
i++) {
2223 s->bit_alloc.sr_code =
i;
2229 s->bitstream_id =
s->eac3 ? 16 : 8;
2233 switch (
s->fbw_channels) {
2234 case 1: avctx->
bit_rate = 96000;
break;
2235 case 2: avctx->
bit_rate = 192000;
break;
2236 case 3: avctx->
bit_rate = 320000;
break;
2237 case 4: avctx->
bit_rate = 384000;
break;
2238 case 5: avctx->
bit_rate = 448000;
break;
2244 int max_br, min_br, wpf, min_br_code;
2246 long long min_br_dist;
2252 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2253 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2262 "for this sample rate\n", min_br, max_br);
2265 s->num_blks_code = num_blks_code;
2266 s->num_blocks = num_blocks;
2276 min_br_dist = INT64_MAX;
2277 for (
int i = 0;
i < 19;
i++) {
2279 if (br_dist < min_br_dist) {
2280 min_br_dist = br_dist;
2286 s->frame_size_code = min_br_code << 1;
2289 s->frame_size_min = 2 * wpf;
2291 int best_br = 0, best_code = 0;
2292 long long best_diff = INT64_MAX;
2293 for (
int i = 0;
i < 19;
i++) {
2296 if (
diff < best_diff) {
2305 s->frame_size_code = best_code << 1;
2307 s->num_blks_code = 0x3;
2311 s->frame_size =
s->frame_size_min;
2319 if (
s->cutoff > (
s->sample_rate >> 1))
2320 s->cutoff =
s->sample_rate >> 1;
2326 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2329 s->cpl_enabled =
s->options.channel_coupling &&
2349 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2352 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2356 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2357 s->start_freq[ch] = 0;
2359 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2363 s->start_freq[
s->lfe_channel] = 0;
2365 s->blocks[
blk].end_freq[ch] = 7;
2369 if (
s->cpl_enabled) {
2371 cpl_start =
s->options.cpl_start;
2374 if (cpl_start < 0) {
2382 if (
s->cpl_enabled) {
2383 int i, cpl_start_band, cpl_end_band;
2384 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2386 cpl_end_band =
s->bandwidth_code / 4 + 3;
2387 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2389 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2391 s->num_cpl_bands = 1;
2392 *cpl_band_sizes = 12;
2393 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2395 *cpl_band_sizes += 12;
2399 *cpl_band_sizes = 12;
2403 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2404 s->cpl_end_freq = cpl_end_band * 12 + 37;
2406 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2415 int channel_blocks =
channels *
s->num_blocks;
2417 uint8_t *cpl_coord_mant_buffer;
2420 for (
int ch = 0; ch <
s->channels; ch++) {
2422 if (!
s->planar_samples[ch])
2437 if (!
s->fixed_point) {
2441 if (
s->cpl_enabled) {
2444 cpl_coord_mant_buffer =
s->cpl_coord_buffer + 16 * channel_blocks;
2449 for (ch = 0; ch <
channels; ch++) {
2456 if (
s->cpl_enabled) {
2458 block->cpl_coord_mant[ch] = &cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2479 int ret, frame_size_58;
2492 s->bitstream_mode = 0x7;
2494 s->bits_written = 0;
2495 s->samples_written = 0;
2498 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2500 if (
s->bit_alloc.sr_code == 1) {
2501 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2505 if (!
s->output_frame_header)