42 #include "config_components.h"
59 #define CMIXLEV_NUM_OPTIONS 3
64 #define SURMIXLEV_NUM_OPTIONS 3
69 #define EXTMIXLEV_NUM_OPTIONS 8
78 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
79 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
152 #if FF_API_OLD_CHANNEL_LAYOUT
153 const uint64_t ff_ac3_channel_layouts[19] = {
222 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
223 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
234 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
235 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
236 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
238 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
239 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
240 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
242 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
243 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
244 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
246 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
247 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
248 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
250 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
251 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
252 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
268 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
269 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
270 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
273 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
274 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
275 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
278 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
279 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
280 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
283 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
284 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
285 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
288 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
289 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
290 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
293 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
294 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
295 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
307 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
308 s->bits_written -=
s->bit_rate;
309 s->samples_written -=
s->sample_rate;
311 s->frame_size =
s->frame_size_min +
312 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
313 s->bits_written +=
s->frame_size * 8;
333 for (ch = 1; ch <=
s->fbw_channels; ch++)
334 block->channel_in_cpl[ch] =
s->cpl_on;
343 block->num_cpl_channels = 0;
344 for (ch = 1; ch <=
s->fbw_channels; ch++)
345 block->num_cpl_channels +=
block->channel_in_cpl[ch];
346 block->cpl_in_use =
block->num_cpl_channels > 1;
347 num_cpl_blocks +=
block->cpl_in_use;
349 block->num_cpl_channels = 0;
350 for (ch = 1; ch <=
s->fbw_channels; ch++)
351 block->channel_in_cpl[ch] = 0;
356 for (ch = 1; ch <=
s->fbw_channels; ch++) {
357 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
358 block->new_cpl_strategy = 1;
363 block->new_cpl_leak =
block->new_cpl_strategy;
365 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
366 block->new_snr_offsets = 1;
367 if (
block->cpl_in_use)
370 block->new_snr_offsets = 0;
379 for (ch = 1; ch <=
s->fbw_channels; ch++) {
380 if (
block->channel_in_cpl[ch])
383 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
401 if (!
s->rematrixing_enabled)
406 if (
block->new_rematrixing_strategy)
409 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
413 for (
i = start;
i < end;
i++) {
416 block->fixed_coef[1][
i] = (lt + rt) >> 1;
417 block->fixed_coef[2][
i] = (lt - rt) >> 1;
430 int expstr,
i, grpsize;
433 grpsize = 3 << expstr;
434 for (
i = 12;
i < 256;
i++) {
450 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
453 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
461 #define EXP_DIFF_THRESHOLD 500
481 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
482 uint8_t *exp_strategy =
s->exp_strategy[ch];
483 uint8_t *
exp =
s->blocks[0].exp[ch];
492 if (!
s->blocks[
blk-1].cpl_in_use) {
495 }
else if (!
s->blocks[
blk].cpl_in_use) {
499 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
514 while (blk < s->num_blocks) {
516 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
530 if (CONFIG_EAC3_ENCODER &&
s->eac3)
551 switch(exp_strategy) {
553 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
554 uint8_t exp_min =
exp[k];
555 if (
exp[k+1] < exp_min)
557 exp[
i-cpl] = exp_min;
562 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
563 uint8_t exp_min =
exp[k];
564 if (
exp[k+1] < exp_min)
566 if (
exp[k+2] < exp_min)
568 if (
exp[k+3] < exp_min)
570 exp[
i-cpl] = exp_min;
577 if (!cpl &&
exp[0] > 15)
582 for (
i = 1;
i <= nb_groups;
i++)
592 switch (exp_strategy) {
594 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
595 uint8_t exp1 =
exp[
i-cpl];
601 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
618 int blk, blk1, ch, cpl;
619 uint8_t *
exp, *exp_strategy;
622 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
623 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
624 exp_strategy =
s->exp_strategy[ch];
628 while (blk < s->num_blocks) {
630 if (cpl && !
block->cpl_in_use) {
640 s->exp_ref_block[ch][
blk] =
blk;
641 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
642 s->exp_ref_block[ch][blk1] =
blk;
645 num_reuse_blocks = blk1 -
blk - 1;
648 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
669 int nb_groups, bit_count;
675 int exp_strategy =
s->exp_strategy[ch][
blk];
683 bit_count += 4 + (nb_groups * 7);
701 int group_size, nb_groups;
703 int delta0, delta1, delta2;
709 int exp_strategy =
s->exp_strategy[ch][
blk];
713 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
715 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
719 block->grouped_exp[ch][0] = exp1;
722 for (
i = 1;
i <= nb_groups;
i++) {
727 delta0 = exp1 - exp0 + 2;
733 delta1 = exp1 - exp0 + 2;
739 delta2 = exp1 - exp0 + 2;
742 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
774 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
793 if (
s->num_blocks != 0x6)
797 if (
s->num_blocks == 6)
801 if (
s->use_frame_exp_strategy)
802 frame_bits += 5 *
s->fbw_channels;
804 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
806 frame_bits +=
s->num_blocks;
808 if (
s->num_blks_code != 0x3)
811 frame_bits +=
s->fbw_channels * 5;
815 if (
s->num_blocks != 1)
819 frame_bits += frame_bits_inc[
s->channel_mode];
826 frame_bits +=
s->fbw_channels;
829 frame_bits +=
s->fbw_channels;
845 frame_bits += 2 *
s->fbw_channels;
852 frame_bits += 2 + 2 + 2 + 2 + 3;
871 frame_bits += 1 + 16;
873 s->frame_bits_fixed = frame_bits;
886 s->slow_decay_code = 2;
887 s->fast_decay_code = 1;
888 s->slow_gain_code = 1;
889 s->db_per_bit_code =
s->eac3 ? 2 : 3;
891 for (ch = 0; ch <=
s->channels; ch++)
892 s->fast_gain_code[ch] = 4;
895 s->coarse_snr_offset = 40;
905 s->bit_alloc.cpl_fast_leak = 0;
906 s->bit_alloc.cpl_slow_leak = 0;
932 frame_bits +=
s->lfe_on;
933 frame_bits += 1 + 1 + 2;
939 frame_bits += 3 + 1 + 1;
946 frame_bits += 5 + 2 + 1;
955 if (
block->new_cpl_strategy)
961 if (
s->use_frame_exp_strategy) {
965 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
971 if (
s->bitstream_id == 6) {
984 if (
block->new_cpl_strategy) {
987 if (
block->cpl_in_use) {
991 frame_bits +=
s->fbw_channels;
998 frame_bits +=
s->num_cpl_subbands - 1;
1003 if (
block->cpl_in_use) {
1004 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1005 if (
block->channel_in_cpl[ch]) {
1006 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1008 if (
block->new_cpl_coords[ch]) {
1010 frame_bits += (4 + 4) *
s->num_cpl_bands;
1018 if (!
s->eac3 ||
blk > 0)
1020 if (
s->blocks[
blk].new_rematrixing_strategy)
1021 frame_bits +=
block->num_rematrixing_bands;
1025 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1027 if (!
block->channel_in_cpl[ch])
1034 if (!
s->eac3 &&
block->cpl_in_use)
1039 if (
block->new_snr_offsets)
1040 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1044 if (
block->cpl_in_use) {
1045 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1047 if (
block->new_cpl_leak)
1048 frame_bits += 3 + 3;
1052 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1073 block->band_psd[ch]);
1075 s->start_freq[ch],
block->end_freq[ch],
1077 ch ==
s->lfe_channel,
1095 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1098 ref_bap =
s->bap_buffer;
1099 for (ch = 0; ch <=
s->channels; ch++) {
1120 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1121 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1122 mant_cnt[
blk][4] = 1;
1147 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1148 s->ref_bap[ch][
blk] + start,
1159 int ch, max_end_freq;
1164 max_end_freq =
s->bandwidth_code * 3 + 73;
1165 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1169 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1187 snr_offset = (snr_offset - 240) * 4;
1199 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1200 s->start_freq[ch],
block->end_freq[ch],
1201 snr_offset,
s->bit_alloc.floor,
1218 int snr_offset, snr_incr;
1220 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1224 snr_offset =
s->coarse_snr_offset << 4;
1228 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1233 while (snr_offset >= 0 &&
1240 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1241 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1242 while (snr_offset + snr_incr <= 1023 &&
1244 snr_offset += snr_incr;
1245 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1248 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1251 s->coarse_snr_offset = snr_offset >> 4;
1252 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1253 s->fine_snr_offset[ch] = snr_offset & 0xF;
1287 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1305 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1306 m = (1 << (qbits-1));
1326 uint8_t *
exp, uint8_t *bap,
1327 int16_t *qmant,
int start_freq,
1332 for (
i = start_freq;
i < end_freq;
i++) {
1333 int c = fixed_coef[
i];
1341 switch (
s->mant1_cnt) {
1343 s->qmant1_ptr = &qmant[
i];
1348 *
s->qmant1_ptr += 3 * v;
1353 *
s->qmant1_ptr += v;
1361 switch (
s->mant2_cnt) {
1363 s->qmant2_ptr = &qmant[
i];
1368 *
s->qmant2_ptr += 5 * v;
1373 *
s->qmant2_ptr += v;
1384 switch (
s->mant4_cnt) {
1386 s->qmant4_ptr = &qmant[
i];
1391 *
s->qmant4_ptr += v;
1422 int blk, ch, ch0=0, got_cpl;
1428 got_cpl = !
block->cpl_in_use;
1429 for (ch = 1; ch <=
s->channels; ch++) {
1430 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1436 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1438 s->start_freq[ch],
block->end_freq[ch]);
1456 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1462 if (
s->channel_mode & 0x04)
1477 if (
s->bitstream_id == 6) {
1482 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1483 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1484 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1485 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1507 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1512 for (ch = 0; ch <
s->fbw_channels; ch++)
1518 for (ch = 0; ch <
s->fbw_channels; ch++)
1532 if (
block->new_cpl_strategy) {
1535 if (
block->cpl_in_use) {
1536 int start_sub, end_sub;
1540 for (ch = 1; ch <=
s->fbw_channels; ch++)
1545 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1546 end_sub = (
s->cpl_end_freq - 37) / 12;
1553 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1560 if (
block->cpl_in_use) {
1561 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1562 if (
block->channel_in_cpl[ch]) {
1563 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1565 if (
block->new_cpl_coords[ch]) {
1567 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1578 if (!
s->eac3 ||
blk > 0)
1580 if (
block->new_rematrixing_strategy) {
1582 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1589 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1596 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1604 int cpl = (ch ==
CPL_CH);
1614 for (
i = 1;
i <= nb_groups;
i++)
1618 if (ch !=
s->lfe_channel && !cpl)
1638 if (
block->new_snr_offsets) {
1650 if (
block->cpl_in_use) {
1651 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1653 if (
block->new_cpl_leak) {
1654 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1655 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1665 got_cpl = !
block->cpl_in_use;
1666 for (ch = 1; ch <=
s->channels; ch++) {
1669 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1674 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1676 b =
s->ref_bap[ch][
blk][
i];
1679 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1680 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1682 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1695 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1698 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1715 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1735 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1738 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1751 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1756 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1761 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1762 s->frame_size - frame_size_58 - 3);
1764 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1766 if (crc2 == 0x770B) {
1767 frame[
s->frame_size - 3] ^= 0x1;
1768 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1787 s->output_frame_header(
s);
1823 *got_packet_ptr = 1;
1834 switch (
s->bitstream_id) {
1835 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1836 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1837 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1838 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1839 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1840 default:
snprintf(strbuf, 32,
"ERROR");
1842 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1845 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1846 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1847 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1848 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1850 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1852 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1856 s->center_mix_level);
1858 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1859 if (
s->has_surround)
1861 s->surround_mix_level);
1863 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1872 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1874 ff_dlog(avctx,
"mixing_level: {not written}\n");
1875 ff_dlog(avctx,
"room_type: {not written}\n");
1886 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1888 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1892 if (
s->bitstream_id == 6) {
1900 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1901 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1903 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1905 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1907 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1910 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1919 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1926 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1933 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1935 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1942 #define FLT_OPTION_THRESHOLD 0.01
1948 for (
i = 0;
i < v_list_size;
i++) {
1953 if (
i == v_list_size)
1961 float *opt_param,
const float *
list,
1962 int list_size,
int default_value,
int min_value,
1966 if (mixlev < min_value) {
1967 mixlev = default_value;
1968 if (*opt_param >= 0.0) {
1970 "default value: %0.3f\n", opt_name,
list[mixlev]);
1973 *opt_param =
list[mixlev];
1974 *ctx_param = mixlev;
2000 if (
s->has_center &&
2005 if (
s->has_surround &&
2043 if (
s->has_center) {
2046 &
s->center_mix_level);
2048 if (
s->has_surround) {
2051 &
s->surround_mix_level);
2060 if (!
s->eac3 ||
s->has_center) {
2065 &
s->ltrt_center_mix_level);
2070 &
s->loro_center_mix_level);
2072 if (!
s->eac3 ||
s->has_surround) {
2077 &
s->ltrt_surround_mix_level);
2082 &
s->loro_surround_mix_level);
2094 "specified number of channels\n");
2131 "room_type is set\n");
2136 "80dB and 111dB\n");
2146 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2147 if (!
s->warned_alternate_bitstream) {
2149 "not compatible with reduced samplerates. writing of "
2150 "extended bitstream information will be disabled.\n");
2151 s->warned_alternate_bitstream = 1;
2154 s->bitstream_id = 6;
2174 if (
s->planar_samples)
2175 for (ch = 0; ch <
s->channels; ch++)
2232 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2250 s->has_surround =
s->channel_mode & 0x04;
2269 "encoder will guess the layout, but it "
2270 "might be incorrect.\n");
2282 max_sr =
s->eac3 ? 2 : 8;
2283 for (
i = 0;
i <= max_sr;
i++) {
2292 s->bit_alloc.sr_shift =
i / 3;
2293 s->bit_alloc.sr_code =
i % 3;
2294 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2298 switch (
s->fbw_channels) {
2299 case 1: avctx->
bit_rate = 96000;
break;
2300 case 2: avctx->
bit_rate = 192000;
break;
2301 case 3: avctx->
bit_rate = 320000;
break;
2302 case 4: avctx->
bit_rate = 384000;
break;
2303 case 5: avctx->
bit_rate = 448000;
break;
2309 int max_br, min_br, wpf, min_br_code;
2310 int num_blks_code, num_blocks, frame_samples;
2311 long long min_br_dist;
2317 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2318 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2320 max_br = 2048 *
s->sample_rate / frame_samples * 16;
2321 min_br = ((
s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2327 "for this sample rate\n", min_br, max_br);
2330 s->num_blks_code = num_blks_code;
2331 s->num_blocks = num_blocks;
2334 wpf = (avctx->
bit_rate / 16) * frame_samples /
s->sample_rate;
2341 min_br_dist = INT64_MAX;
2342 for (
i = 0;
i < 19;
i++) {
2344 if (br_dist < min_br_dist) {
2345 min_br_dist = br_dist;
2351 s->frame_size_code = min_br_code << 1;
2354 s->frame_size_min = 2 * wpf;
2356 int best_br = 0, best_code = 0;
2357 long long best_diff = INT64_MAX;
2358 for (
i = 0;
i < 19;
i++) {
2361 if (
diff < best_diff) {
2370 s->frame_size_code = best_code << 1;
2372 s->num_blks_code = 0x3;
2376 s->frame_size =
s->frame_size_min;
2384 if (
s->cutoff > (
s->sample_rate >> 1))
2385 s->cutoff =
s->sample_rate >> 1;
2391 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2394 s->cpl_enabled =
s->options.channel_coupling &&
2414 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2417 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2421 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2422 s->start_freq[ch] = 0;
2424 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2428 s->start_freq[
s->lfe_channel] = 0;
2430 s->blocks[
blk].end_freq[ch] = 7;
2434 if (
s->cpl_enabled) {
2436 cpl_start =
s->options.cpl_start;
2439 if (cpl_start < 0) {
2447 if (
s->cpl_enabled) {
2448 int i, cpl_start_band, cpl_end_band;
2449 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2451 cpl_end_band =
s->bandwidth_code / 4 + 3;
2452 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2454 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2456 s->num_cpl_bands = 1;
2457 *cpl_band_sizes = 12;
2458 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2460 *cpl_band_sizes += 12;
2464 *cpl_band_sizes = 12;
2468 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2469 s->cpl_end_freq = cpl_end_band * 12 + 37;
2471 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2480 int channel_blocks =
channels *
s->num_blocks;
2483 if (
s->allocate_sample_buffers(
s))
2497 if (
s->cpl_enabled) {
2514 if (
s->cpl_enabled) {
2520 for (ch = 0; ch <
channels; ch++) {
2527 if (
s->cpl_enabled) {
2528 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2529 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2538 if (!
s->fixed_point) {
2566 int ret, frame_size_58;
2581 s->bitstream_mode = 0x7;
2583 s->bits_written = 0;
2584 s->samples_written = 0;
2587 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2589 if (
s->bit_alloc.sr_code == 1) {
2590 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2594 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2605 ret =
s->mdct_init(
s);