42 #include "config_components.h"
61 #define CMIXLEV_NUM_OPTIONS 3
66 #define SURMIXLEV_NUM_OPTIONS 3
71 #define EXTMIXLEV_NUM_OPTIONS 8
80 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
81 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
154 #if FF_API_OLD_CHANNEL_LAYOUT
155 const uint64_t ff_ac3_channel_layouts[19] = {
224 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
225 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
236 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
237 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
238 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
240 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
241 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
242 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
244 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
245 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
246 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
248 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
249 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
250 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
252 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
253 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
254 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
270 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
271 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
272 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
275 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
276 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
277 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
280 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
281 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
282 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
285 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
286 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
287 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
290 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
291 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
292 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
295 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
296 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
297 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
309 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
310 s->bits_written -=
s->bit_rate;
311 s->samples_written -=
s->sample_rate;
313 s->frame_size =
s->frame_size_min +
314 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
315 s->bits_written +=
s->frame_size * 8;
335 for (ch = 1; ch <=
s->fbw_channels; ch++)
336 block->channel_in_cpl[ch] =
s->cpl_on;
345 block->num_cpl_channels = 0;
346 for (ch = 1; ch <=
s->fbw_channels; ch++)
347 block->num_cpl_channels +=
block->channel_in_cpl[ch];
348 block->cpl_in_use =
block->num_cpl_channels > 1;
349 num_cpl_blocks +=
block->cpl_in_use;
351 block->num_cpl_channels = 0;
352 for (ch = 1; ch <=
s->fbw_channels; ch++)
353 block->channel_in_cpl[ch] = 0;
358 for (ch = 1; ch <=
s->fbw_channels; ch++) {
359 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
360 block->new_cpl_strategy = 1;
365 block->new_cpl_leak =
block->new_cpl_strategy;
367 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
368 block->new_snr_offsets = 1;
369 if (
block->cpl_in_use)
372 block->new_snr_offsets = 0;
381 for (ch = 1; ch <=
s->fbw_channels; ch++) {
382 if (
block->channel_in_cpl[ch])
385 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
403 if (!
s->rematrixing_enabled)
408 if (
block->new_rematrixing_strategy)
411 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
415 for (
i = start;
i < end;
i++) {
418 block->fixed_coef[1][
i] = (lt + rt) >> 1;
419 block->fixed_coef[2][
i] = (lt - rt) >> 1;
432 int expstr,
i, grpsize;
435 grpsize = 3 << expstr;
436 for (
i = 12;
i < 256;
i++) {
452 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
455 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
463 #define EXP_DIFF_THRESHOLD 500
483 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
484 uint8_t *exp_strategy =
s->exp_strategy[ch];
485 uint8_t *
exp =
s->blocks[0].exp[ch];
494 if (!
s->blocks[
blk-1].cpl_in_use) {
497 }
else if (!
s->blocks[
blk].cpl_in_use) {
501 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
516 while (blk < s->num_blocks) {
518 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
532 if (CONFIG_EAC3_ENCODER &&
s->eac3)
553 switch(exp_strategy) {
555 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
556 uint8_t exp_min =
exp[k];
557 if (
exp[k+1] < exp_min)
559 exp[
i-cpl] = exp_min;
564 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
565 uint8_t exp_min =
exp[k];
566 if (
exp[k+1] < exp_min)
568 if (
exp[k+2] < exp_min)
570 if (
exp[k+3] < exp_min)
572 exp[
i-cpl] = exp_min;
579 if (!cpl &&
exp[0] > 15)
584 for (
i = 1;
i <= nb_groups;
i++)
594 switch (exp_strategy) {
596 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
597 uint8_t exp1 =
exp[
i-cpl];
603 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
620 int blk, blk1, ch, cpl;
621 uint8_t *
exp, *exp_strategy;
624 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
625 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
626 exp_strategy =
s->exp_strategy[ch];
630 while (blk < s->num_blocks) {
632 if (cpl && !
block->cpl_in_use) {
642 s->exp_ref_block[ch][
blk] =
blk;
643 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
644 s->exp_ref_block[ch][blk1] =
blk;
647 num_reuse_blocks = blk1 -
blk - 1;
650 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
671 int nb_groups, bit_count;
677 int exp_strategy =
s->exp_strategy[ch][
blk];
685 bit_count += 4 + (nb_groups * 7);
703 int group_size, nb_groups;
705 int delta0, delta1, delta2;
711 int exp_strategy =
s->exp_strategy[ch][
blk];
715 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
717 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
721 block->grouped_exp[ch][0] = exp1;
724 for (
i = 1;
i <= nb_groups;
i++) {
729 delta0 = exp1 - exp0 + 2;
735 delta1 = exp1 - exp0 + 2;
741 delta2 = exp1 - exp0 + 2;
744 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
776 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
795 if (
s->num_blocks != 0x6)
799 if (
s->num_blocks == 6)
803 if (
s->use_frame_exp_strategy)
804 frame_bits += 5 *
s->fbw_channels;
806 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
808 frame_bits +=
s->num_blocks;
810 if (
s->num_blks_code != 0x3)
813 frame_bits +=
s->fbw_channels * 5;
817 if (
s->num_blocks != 1)
821 frame_bits += frame_bits_inc[
s->channel_mode];
828 frame_bits +=
s->fbw_channels;
831 frame_bits +=
s->fbw_channels;
847 frame_bits += 2 *
s->fbw_channels;
854 frame_bits += 2 + 2 + 2 + 2 + 3;
873 frame_bits += 1 + 16;
875 s->frame_bits_fixed = frame_bits;
888 s->slow_decay_code = 2;
889 s->fast_decay_code = 1;
890 s->slow_gain_code = 1;
891 s->db_per_bit_code =
s->eac3 ? 2 : 3;
893 for (ch = 0; ch <=
s->channels; ch++)
894 s->fast_gain_code[ch] = 4;
897 s->coarse_snr_offset = 40;
907 s->bit_alloc.cpl_fast_leak = 0;
908 s->bit_alloc.cpl_slow_leak = 0;
934 frame_bits +=
s->lfe_on;
935 frame_bits += 1 + 1 + 2;
941 frame_bits += 3 + 1 + 1;
948 frame_bits += 5 + 2 + 1;
957 if (
block->new_cpl_strategy)
963 if (
s->use_frame_exp_strategy) {
967 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
973 if (
s->bitstream_id == 6) {
986 if (
block->new_cpl_strategy) {
989 if (
block->cpl_in_use) {
993 frame_bits +=
s->fbw_channels;
1000 frame_bits +=
s->num_cpl_subbands - 1;
1005 if (
block->cpl_in_use) {
1006 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1007 if (
block->channel_in_cpl[ch]) {
1008 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1010 if (
block->new_cpl_coords[ch]) {
1012 frame_bits += (4 + 4) *
s->num_cpl_bands;
1020 if (!
s->eac3 ||
blk > 0)
1022 if (
s->blocks[
blk].new_rematrixing_strategy)
1023 frame_bits +=
block->num_rematrixing_bands;
1027 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1029 if (!
block->channel_in_cpl[ch])
1036 if (!
s->eac3 &&
block->cpl_in_use)
1041 if (
block->new_snr_offsets)
1042 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1046 if (
block->cpl_in_use) {
1047 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1049 if (
block->new_cpl_leak)
1050 frame_bits += 3 + 3;
1054 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1075 block->band_psd[ch]);
1077 s->start_freq[ch],
block->end_freq[ch],
1079 ch ==
s->lfe_channel,
1097 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1100 ref_bap =
s->bap_buffer;
1101 for (ch = 0; ch <=
s->channels; ch++) {
1122 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1123 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1124 mant_cnt[
blk][4] = 1;
1149 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1150 s->ref_bap[ch][
blk] + start,
1161 int ch, max_end_freq;
1166 max_end_freq =
s->bandwidth_code * 3 + 73;
1167 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1171 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1189 snr_offset = (snr_offset - 240) * 4;
1201 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1202 s->start_freq[ch],
block->end_freq[ch],
1203 snr_offset,
s->bit_alloc.floor,
1220 int snr_offset, snr_incr;
1222 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1226 snr_offset =
s->coarse_snr_offset << 4;
1230 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1235 while (snr_offset >= 0 &&
1242 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1243 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1244 while (snr_offset + snr_incr <= 1023 &&
1245 bit_alloc(
s, snr_offset + snr_incr) <= bits_left) {
1246 snr_offset += snr_incr;
1247 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1250 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1253 s->coarse_snr_offset = snr_offset >> 4;
1254 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1255 s->fine_snr_offset[ch] = snr_offset & 0xF;
1289 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1307 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1308 m = (1 << (qbits-1));
1328 uint8_t *
exp, uint8_t *bap,
1329 int16_t *qmant,
int start_freq,
1334 for (
i = start_freq;
i < end_freq;
i++) {
1335 int c = fixed_coef[
i];
1343 switch (
s->mant1_cnt) {
1345 s->qmant1_ptr = &qmant[
i];
1350 *
s->qmant1_ptr += 3 * v;
1355 *
s->qmant1_ptr += v;
1363 switch (
s->mant2_cnt) {
1365 s->qmant2_ptr = &qmant[
i];
1370 *
s->qmant2_ptr += 5 * v;
1375 *
s->qmant2_ptr += v;
1386 switch (
s->mant4_cnt) {
1388 s->qmant4_ptr = &qmant[
i];
1393 *
s->qmant4_ptr += v;
1424 int blk, ch, ch0=0, got_cpl;
1430 got_cpl = !
block->cpl_in_use;
1431 for (ch = 1; ch <=
s->channels; ch++) {
1432 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1438 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1440 s->start_freq[ch],
block->end_freq[ch]);
1458 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1464 if (
s->channel_mode & 0x04)
1479 if (
s->bitstream_id == 6) {
1484 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1485 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1486 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1487 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1509 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1514 for (ch = 0; ch <
s->fbw_channels; ch++)
1520 for (ch = 0; ch <
s->fbw_channels; ch++)
1534 if (
block->new_cpl_strategy) {
1537 if (
block->cpl_in_use) {
1538 int start_sub, end_sub;
1542 for (ch = 1; ch <=
s->fbw_channels; ch++)
1547 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1548 end_sub = (
s->cpl_end_freq - 37) / 12;
1555 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1562 if (
block->cpl_in_use) {
1563 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1564 if (
block->channel_in_cpl[ch]) {
1565 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1567 if (
block->new_cpl_coords[ch]) {
1569 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1580 if (!
s->eac3 ||
blk > 0)
1582 if (
block->new_rematrixing_strategy) {
1584 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1591 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1598 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1606 int cpl = (ch ==
CPL_CH);
1616 for (
i = 1;
i <= nb_groups;
i++)
1620 if (ch !=
s->lfe_channel && !cpl)
1640 if (
block->new_snr_offsets) {
1652 if (
block->cpl_in_use) {
1653 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1655 if (
block->new_cpl_leak) {
1656 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1657 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1667 got_cpl = !
block->cpl_in_use;
1668 for (ch = 1; ch <=
s->channels; ch++) {
1671 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1676 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1678 b =
s->ref_bap[ch][
blk][
i];
1681 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1682 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1684 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1697 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1700 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1717 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1737 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1740 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1753 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1758 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1763 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1764 s->frame_size - frame_size_58 - 3);
1766 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1768 if (crc2 == 0x770B) {
1769 frame[
s->frame_size - 3] ^= 0x1;
1770 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1789 s->output_frame_header(
s);
1825 *got_packet_ptr = 1;
1836 switch (
s->bitstream_id) {
1837 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1838 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1839 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1840 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1841 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1842 default:
snprintf(strbuf, 32,
"ERROR");
1844 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1847 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1848 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1849 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1850 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1852 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1854 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1858 s->center_mix_level);
1860 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1861 if (
s->has_surround)
1863 s->surround_mix_level);
1865 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1874 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1876 ff_dlog(avctx,
"mixing_level: {not written}\n");
1877 ff_dlog(avctx,
"room_type: {not written}\n");
1888 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1890 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1894 if (
s->bitstream_id == 6) {
1902 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1903 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1905 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1907 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1909 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1912 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1921 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1928 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1935 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1937 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1944 #define FLT_OPTION_THRESHOLD 0.01
1950 for (
i = 0;
i < v_list_size;
i++) {
1955 if (
i == v_list_size)
1963 float *opt_param,
const float *
list,
1964 int list_size,
int default_value,
int min_value,
1968 if (mixlev < min_value) {
1969 mixlev = default_value;
1970 if (*opt_param >= 0.0) {
1972 "default value: %0.3f\n", opt_name,
list[mixlev]);
1975 *opt_param =
list[mixlev];
1976 *ctx_param = mixlev;
2002 if (
s->has_center &&
2007 if (
s->has_surround &&
2045 if (
s->has_center) {
2048 &
s->center_mix_level);
2050 if (
s->has_surround) {
2053 &
s->surround_mix_level);
2062 if (!
s->eac3 ||
s->has_center) {
2067 &
s->ltrt_center_mix_level);
2072 &
s->loro_center_mix_level);
2074 if (!
s->eac3 ||
s->has_surround) {
2079 &
s->ltrt_surround_mix_level);
2084 &
s->loro_surround_mix_level);
2096 "specified number of channels\n");
2133 "room_type is set\n");
2138 "80dB and 111dB\n");
2148 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2149 if (!
s->warned_alternate_bitstream) {
2151 "not compatible with reduced samplerates. writing of "
2152 "extended bitstream information will be disabled.\n");
2153 s->warned_alternate_bitstream = 1;
2156 s->bitstream_id = 6;
2176 if (
s->planar_samples)
2177 for (ch = 0; ch <
s->channels; ch++)
2234 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2252 s->has_surround =
s->channel_mode & 0x04;
2271 "encoder will guess the layout, but it "
2272 "might be incorrect.\n");
2284 max_sr =
s->eac3 ? 2 : 8;
2285 for (
i = 0;
i <= max_sr;
i++) {
2294 s->bit_alloc.sr_shift =
i / 3;
2295 s->bit_alloc.sr_code =
i % 3;
2296 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2300 switch (
s->fbw_channels) {
2301 case 1: avctx->
bit_rate = 96000;
break;
2302 case 2: avctx->
bit_rate = 192000;
break;
2303 case 3: avctx->
bit_rate = 320000;
break;
2304 case 4: avctx->
bit_rate = 384000;
break;
2305 case 5: avctx->
bit_rate = 448000;
break;
2311 int max_br, min_br, wpf, min_br_code;
2312 int num_blks_code, num_blocks, frame_samples;
2313 long long min_br_dist;
2319 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2320 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2322 max_br = 2048 *
s->sample_rate / frame_samples * 16;
2323 min_br = ((
s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2329 "for this sample rate\n", min_br, max_br);
2332 s->num_blks_code = num_blks_code;
2333 s->num_blocks = num_blocks;
2336 wpf = (avctx->
bit_rate / 16) * frame_samples /
s->sample_rate;
2343 min_br_dist = INT64_MAX;
2344 for (
i = 0;
i < 19;
i++) {
2346 if (br_dist < min_br_dist) {
2347 min_br_dist = br_dist;
2353 s->frame_size_code = min_br_code << 1;
2356 s->frame_size_min = 2 * wpf;
2358 int best_br = 0, best_code = 0;
2359 long long best_diff = INT64_MAX;
2360 for (
i = 0;
i < 19;
i++) {
2363 if (
diff < best_diff) {
2372 s->frame_size_code = best_code << 1;
2374 s->num_blks_code = 0x3;
2378 s->frame_size =
s->frame_size_min;
2386 if (
s->cutoff > (
s->sample_rate >> 1))
2387 s->cutoff =
s->sample_rate >> 1;
2393 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2396 s->cpl_enabled =
s->options.channel_coupling &&
2416 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2419 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2423 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2424 s->start_freq[ch] = 0;
2426 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2430 s->start_freq[
s->lfe_channel] = 0;
2432 s->blocks[
blk].end_freq[ch] = 7;
2436 if (
s->cpl_enabled) {
2438 cpl_start =
s->options.cpl_start;
2441 if (cpl_start < 0) {
2449 if (
s->cpl_enabled) {
2450 int i, cpl_start_band, cpl_end_band;
2451 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2453 cpl_end_band =
s->bandwidth_code / 4 + 3;
2454 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2456 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2458 s->num_cpl_bands = 1;
2459 *cpl_band_sizes = 12;
2460 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2462 *cpl_band_sizes += 12;
2466 *cpl_band_sizes = 12;
2470 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2471 s->cpl_end_freq = cpl_end_band * 12 + 37;
2473 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2482 int channel_blocks =
channels *
s->num_blocks;
2485 if (
s->allocate_sample_buffers(
s))
2499 if (
s->cpl_enabled) {
2516 if (
s->cpl_enabled) {
2522 for (ch = 0; ch <
channels; ch++) {
2529 if (
s->cpl_enabled) {
2530 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2531 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2540 if (!
s->fixed_point) {
2568 int ret, frame_size_58;
2583 s->bitstream_mode = 0x7;
2585 s->bits_written = 0;
2586 s->samples_written = 0;
2589 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2591 if (
s->bit_alloc.sr_code == 1) {
2592 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2596 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2607 ret =
s->mdct_init(
s);