43 #include "config_components.h"
60 #define CMIXLEV_NUM_OPTIONS 3
65 #define SURMIXLEV_NUM_OPTIONS 3
70 #define EXTMIXLEV_NUM_OPTIONS 8
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 } },
284 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
285 s->bits_written -=
s->bit_rate;
286 s->samples_written -=
s->sample_rate;
288 s->frame_size =
s->frame_size_min +
289 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
290 s->bits_written +=
s->frame_size * 8;
310 for (ch = 1; ch <=
s->fbw_channels; ch++)
311 block->channel_in_cpl[ch] =
s->cpl_on;
320 block->num_cpl_channels = 0;
321 for (ch = 1; ch <=
s->fbw_channels; ch++)
322 block->num_cpl_channels +=
block->channel_in_cpl[ch];
323 block->cpl_in_use =
block->num_cpl_channels > 1;
324 num_cpl_blocks +=
block->cpl_in_use;
326 block->num_cpl_channels = 0;
327 for (ch = 1; ch <=
s->fbw_channels; ch++)
328 block->channel_in_cpl[ch] = 0;
333 for (ch = 1; ch <=
s->fbw_channels; ch++) {
334 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
335 block->new_cpl_strategy = 1;
340 block->new_cpl_leak =
block->new_cpl_strategy;
342 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
343 block->new_snr_offsets = 1;
344 if (
block->cpl_in_use)
347 block->new_snr_offsets = 0;
356 for (ch = 1; ch <=
s->fbw_channels; ch++) {
357 if (
block->channel_in_cpl[ch])
360 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
378 if (!
s->rematrixing_enabled)
383 if (
block->new_rematrixing_strategy)
386 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
390 for (
i = start;
i < end;
i++) {
393 block->fixed_coef[1][
i] = (lt + rt) >> 1;
394 block->fixed_coef[2][
i] = (lt - rt) >> 1;
407 int expstr,
i, grpsize;
410 grpsize = 3 << expstr;
411 for (
i = 12;
i < 256;
i++) {
427 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
430 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
438 #define EXP_DIFF_THRESHOLD 500
458 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
459 uint8_t *exp_strategy =
s->exp_strategy[ch];
460 uint8_t *
exp =
s->blocks[0].exp[ch];
469 if (!
s->blocks[
blk-1].cpl_in_use) {
472 }
else if (!
s->blocks[
blk].cpl_in_use) {
476 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
491 while (blk < s->num_blocks) {
493 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
507 if (CONFIG_EAC3_ENCODER &&
s->eac3)
528 switch(exp_strategy) {
530 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
531 uint8_t exp_min =
exp[k];
532 if (
exp[k+1] < exp_min)
534 exp[
i-cpl] = exp_min;
539 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
540 uint8_t exp_min =
exp[k];
541 if (
exp[k+1] < exp_min)
543 if (
exp[k+2] < exp_min)
545 if (
exp[k+3] < exp_min)
547 exp[
i-cpl] = exp_min;
554 if (!cpl &&
exp[0] > 15)
559 for (
i = 1;
i <= nb_groups;
i++)
569 switch (exp_strategy) {
571 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
572 uint8_t exp1 =
exp[
i-cpl];
578 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
595 int blk, blk1, ch, cpl;
596 uint8_t *
exp, *exp_strategy;
599 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
600 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
601 exp_strategy =
s->exp_strategy[ch];
605 while (blk < s->num_blocks) {
607 if (cpl && !
block->cpl_in_use) {
617 s->exp_ref_block[ch][
blk] =
blk;
618 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
619 s->exp_ref_block[ch][blk1] =
blk;
622 num_reuse_blocks = blk1 -
blk - 1;
625 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
646 int nb_groups, bit_count;
652 int exp_strategy =
s->exp_strategy[ch][
blk];
660 bit_count += 4 + (nb_groups * 7);
678 int group_size, nb_groups;
680 int delta0, delta1, delta2;
686 int exp_strategy =
s->exp_strategy[ch][
blk];
690 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
692 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
696 block->grouped_exp[ch][0] = exp1;
699 for (
i = 1;
i <= nb_groups;
i++) {
704 delta0 = exp1 - exp0 + 2;
710 delta1 = exp1 - exp0 + 2;
716 delta2 = exp1 - exp0 + 2;
719 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
751 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
770 if (
s->num_blocks != 0x6)
774 if (
s->num_blocks == 6)
778 if (
s->use_frame_exp_strategy)
779 frame_bits += 5 *
s->fbw_channels;
781 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
783 frame_bits +=
s->num_blocks;
785 if (
s->num_blks_code != 0x3)
788 frame_bits +=
s->fbw_channels * 5;
792 if (
s->num_blocks != 1)
796 frame_bits += frame_bits_inc[
s->channel_mode];
803 frame_bits +=
s->fbw_channels;
806 frame_bits +=
s->fbw_channels;
822 frame_bits += 2 *
s->fbw_channels;
829 frame_bits += 2 + 2 + 2 + 2 + 3;
848 frame_bits += 1 + 16;
850 s->frame_bits_fixed = frame_bits;
863 s->slow_decay_code = 2;
864 s->fast_decay_code = 1;
865 s->slow_gain_code = 1;
866 s->db_per_bit_code =
s->eac3 ? 2 : 3;
868 for (ch = 0; ch <=
s->channels; ch++)
869 s->fast_gain_code[ch] = 4;
872 s->coarse_snr_offset = 40;
882 s->bit_alloc.cpl_fast_leak = 0;
883 s->bit_alloc.cpl_slow_leak = 0;
909 frame_bits +=
s->lfe_on;
910 frame_bits += 1 + 1 + 2;
916 frame_bits += 3 + 1 + 1;
923 frame_bits += 5 + 2 + 1;
932 if (
block->new_cpl_strategy)
938 if (
s->use_frame_exp_strategy) {
942 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
948 if (
s->bitstream_id == 6) {
961 if (
block->new_cpl_strategy) {
964 if (
block->cpl_in_use) {
968 frame_bits +=
s->fbw_channels;
975 frame_bits +=
s->num_cpl_subbands - 1;
980 if (
block->cpl_in_use) {
981 for (ch = 1; ch <=
s->fbw_channels; ch++) {
982 if (
block->channel_in_cpl[ch]) {
983 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
985 if (
block->new_cpl_coords[ch]) {
987 frame_bits += (4 + 4) *
s->num_cpl_bands;
995 if (!
s->eac3 ||
blk > 0)
997 if (
s->blocks[
blk].new_rematrixing_strategy)
998 frame_bits +=
block->num_rematrixing_bands;
1002 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1004 if (!
block->channel_in_cpl[ch])
1011 if (!
s->eac3 &&
block->cpl_in_use)
1016 if (
block->new_snr_offsets)
1017 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1021 if (
block->cpl_in_use) {
1022 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1024 if (
block->new_cpl_leak)
1025 frame_bits += 3 + 3;
1029 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1050 block->band_psd[ch]);
1052 s->start_freq[ch],
block->end_freq[ch],
1054 ch ==
s->lfe_channel,
1072 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1075 ref_bap =
s->bap_buffer;
1076 for (ch = 0; ch <=
s->channels; ch++) {
1097 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1098 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1099 mant_cnt[
blk][4] = 1;
1124 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1125 s->ref_bap[ch][
blk] + start,
1136 int ch, max_end_freq;
1141 max_end_freq =
s->bandwidth_code * 3 + 73;
1142 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1146 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1164 snr_offset = (snr_offset - 240) * 4;
1176 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1177 s->start_freq[ch],
block->end_freq[ch],
1178 snr_offset,
s->bit_alloc.floor,
1195 int snr_offset, snr_incr;
1197 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1201 snr_offset =
s->coarse_snr_offset << 4;
1205 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1210 while (snr_offset >= 0 &&
1217 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1218 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1219 while (snr_offset + snr_incr <= 1023 &&
1221 snr_offset += snr_incr;
1222 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1225 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1228 s->coarse_snr_offset = snr_offset >> 4;
1229 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1230 s->fine_snr_offset[ch] = snr_offset & 0xF;
1264 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1282 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1283 m = (1 << (qbits-1));
1303 uint8_t *
exp, uint8_t *bap,
1304 int16_t *qmant,
int start_freq,
1309 for (
i = start_freq;
i < end_freq;
i++) {
1310 int c = fixed_coef[
i];
1318 switch (
s->mant1_cnt) {
1320 s->qmant1_ptr = &qmant[
i];
1325 *
s->qmant1_ptr += 3 * v;
1330 *
s->qmant1_ptr += v;
1338 switch (
s->mant2_cnt) {
1340 s->qmant2_ptr = &qmant[
i];
1345 *
s->qmant2_ptr += 5 * v;
1350 *
s->qmant2_ptr += v;
1361 switch (
s->mant4_cnt) {
1363 s->qmant4_ptr = &qmant[
i];
1368 *
s->qmant4_ptr += v;
1399 int blk, ch, ch0=0, got_cpl;
1405 got_cpl = !
block->cpl_in_use;
1406 for (ch = 1; ch <=
s->channels; ch++) {
1407 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1413 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1415 s->start_freq[ch],
block->end_freq[ch]);
1433 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1439 if (
s->channel_mode & 0x04)
1454 if (
s->bitstream_id == 6) {
1459 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1460 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1461 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1462 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1484 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1489 for (ch = 0; ch <
s->fbw_channels; ch++)
1495 for (ch = 0; ch <
s->fbw_channels; ch++)
1509 if (
block->new_cpl_strategy) {
1512 if (
block->cpl_in_use) {
1513 int start_sub, end_sub;
1517 for (ch = 1; ch <=
s->fbw_channels; ch++)
1522 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1523 end_sub = (
s->cpl_end_freq - 37) / 12;
1530 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1537 if (
block->cpl_in_use) {
1538 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1539 if (
block->channel_in_cpl[ch]) {
1540 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1542 if (
block->new_cpl_coords[ch]) {
1544 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1555 if (!
s->eac3 ||
blk > 0)
1557 if (
block->new_rematrixing_strategy) {
1559 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1566 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1573 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1581 int cpl = (ch ==
CPL_CH);
1591 for (
i = 1;
i <= nb_groups;
i++)
1595 if (ch !=
s->lfe_channel && !cpl)
1615 if (
block->new_snr_offsets) {
1627 if (
block->cpl_in_use) {
1628 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1630 if (
block->new_cpl_leak) {
1631 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1632 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1642 got_cpl = !
block->cpl_in_use;
1643 for (ch = 1; ch <=
s->channels; ch++) {
1646 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1651 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1653 b =
s->ref_bap[ch][
blk][
i];
1656 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1657 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1659 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1672 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1675 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1692 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1712 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1715 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1728 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1733 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1738 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1739 s->frame_size - frame_size_58 - 3);
1741 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1743 if (crc2 == 0x770B) {
1744 frame[
s->frame_size - 3] ^= 0x1;
1745 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1764 s->output_frame_header(
s);
1800 *got_packet_ptr = 1;
1811 switch (
s->bitstream_id) {
1812 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1813 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1814 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1815 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1816 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1817 default:
snprintf(strbuf, 32,
"ERROR");
1819 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1822 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1823 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1824 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1825 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1827 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1829 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1833 s->center_mix_level);
1835 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1836 if (
s->has_surround)
1838 s->surround_mix_level);
1840 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1849 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1851 ff_dlog(avctx,
"mixing_level: {not written}\n");
1852 ff_dlog(avctx,
"room_type: {not written}\n");
1863 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1865 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1869 if (
s->bitstream_id == 6) {
1877 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1878 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1880 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1882 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1884 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1887 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1896 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1903 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1910 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1912 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1919 #define FLT_OPTION_THRESHOLD 0.01
1925 for (
i = 0;
i < v_list_size;
i++) {
1930 if (
i == v_list_size)
1938 float *opt_param,
const float *
list,
1939 int list_size,
int default_value,
int min_value,
1943 if (mixlev < min_value) {
1944 mixlev = default_value;
1945 if (*opt_param >= 0.0) {
1947 "default value: %0.3f\n", opt_name,
list[mixlev]);
1950 *opt_param =
list[mixlev];
1951 *ctx_param = mixlev;
1977 if (
s->has_center &&
1982 if (
s->has_surround &&
2020 if (
s->has_center) {
2023 &
s->center_mix_level);
2025 if (
s->has_surround) {
2028 &
s->surround_mix_level);
2037 if (!
s->eac3 ||
s->has_center) {
2042 &
s->ltrt_center_mix_level);
2047 &
s->loro_center_mix_level);
2049 if (!
s->eac3 ||
s->has_surround) {
2054 &
s->ltrt_surround_mix_level);
2059 &
s->loro_surround_mix_level);
2071 "specified number of channels\n");
2108 "room_type is set\n");
2113 "80dB and 111dB\n");
2123 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2124 if (!
s->warned_alternate_bitstream) {
2126 "not compatible with reduced samplerates. writing of "
2127 "extended bitstream information will be disabled.\n");
2128 s->warned_alternate_bitstream = 1;
2131 s->bitstream_id = 6;
2151 if (
s->planar_samples)
2152 for (ch = 0; ch <
s->channels; ch++)
2209 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2227 s->has_surround =
s->channel_mode & 0x04;
2246 "encoder will guess the layout, but it "
2247 "might be incorrect.\n");
2259 max_sr =
s->eac3 ? 2 : 8;
2260 for (
i = 0;
i <= max_sr;
i++) {
2269 s->bit_alloc.sr_shift =
i / 3;
2270 s->bit_alloc.sr_code =
i % 3;
2271 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2275 switch (
s->fbw_channels) {
2276 case 1: avctx->
bit_rate = 96000;
break;
2277 case 2: avctx->
bit_rate = 192000;
break;
2278 case 3: avctx->
bit_rate = 320000;
break;
2279 case 4: avctx->
bit_rate = 384000;
break;
2280 case 5: avctx->
bit_rate = 448000;
break;
2286 int max_br, min_br, wpf, min_br_code;
2288 long long min_br_dist;
2294 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2295 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2304 "for this sample rate\n", min_br, max_br);
2307 s->num_blks_code = num_blks_code;
2308 s->num_blocks = num_blocks;
2318 min_br_dist = INT64_MAX;
2319 for (
i = 0;
i < 19;
i++) {
2321 if (br_dist < min_br_dist) {
2322 min_br_dist = br_dist;
2328 s->frame_size_code = min_br_code << 1;
2331 s->frame_size_min = 2 * wpf;
2333 int best_br = 0, best_code = 0;
2334 long long best_diff = INT64_MAX;
2335 for (
i = 0;
i < 19;
i++) {
2338 if (
diff < best_diff) {
2347 s->frame_size_code = best_code << 1;
2349 s->num_blks_code = 0x3;
2353 s->frame_size =
s->frame_size_min;
2361 if (
s->cutoff > (
s->sample_rate >> 1))
2362 s->cutoff =
s->sample_rate >> 1;
2368 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2371 s->cpl_enabled =
s->options.channel_coupling &&
2391 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2394 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2398 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2399 s->start_freq[ch] = 0;
2401 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2405 s->start_freq[
s->lfe_channel] = 0;
2407 s->blocks[
blk].end_freq[ch] = 7;
2411 if (
s->cpl_enabled) {
2413 cpl_start =
s->options.cpl_start;
2416 if (cpl_start < 0) {
2424 if (
s->cpl_enabled) {
2425 int i, cpl_start_band, cpl_end_band;
2426 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2428 cpl_end_band =
s->bandwidth_code / 4 + 3;
2429 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2431 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2433 s->num_cpl_bands = 1;
2434 *cpl_band_sizes = 12;
2435 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2437 *cpl_band_sizes += 12;
2441 *cpl_band_sizes = 12;
2445 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2446 s->cpl_end_freq = cpl_end_band * 12 + 37;
2448 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2457 int channel_blocks =
channels *
s->num_blocks;
2460 if (
s->allocate_sample_buffers(
s))
2474 if (
s->cpl_enabled) {
2491 if (
s->cpl_enabled) {
2497 for (ch = 0; ch <
channels; ch++) {
2504 if (
s->cpl_enabled) {
2505 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2506 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2515 if (!
s->fixed_point) {
2543 int ret, frame_size_58;
2558 s->bitstream_mode = 0x7;
2560 s->bits_written = 0;
2561 s->samples_written = 0;
2564 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2566 if (
s->bit_alloc.sr_code == 1) {
2567 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2571 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2582 ret =
s->mdct_init(
s);