57 #define CMIXLEV_NUM_OPTIONS 3
62 #define SURMIXLEV_NUM_OPTIONS 3
67 #define EXTMIXLEV_NUM_OPTIONS 8
76 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
77 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
178 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
179 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
190 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
191 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
192 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
194 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
195 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
196 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
198 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
199 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
200 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
202 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
203 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
204 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
206 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
207 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
208 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
224 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
225 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
226 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
229 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
230 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
231 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
234 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
235 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
236 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
239 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
240 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
241 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
244 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
245 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
246 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
249 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
250 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
251 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
263 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
264 s->bits_written -=
s->bit_rate;
265 s->samples_written -=
s->sample_rate;
267 s->frame_size =
s->frame_size_min +
268 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
269 s->bits_written +=
s->frame_size * 8;
289 for (ch = 1; ch <=
s->fbw_channels; ch++)
290 block->channel_in_cpl[ch] =
s->cpl_on;
299 block->num_cpl_channels = 0;
300 for (ch = 1; ch <=
s->fbw_channels; ch++)
301 block->num_cpl_channels +=
block->channel_in_cpl[ch];
302 block->cpl_in_use =
block->num_cpl_channels > 1;
303 num_cpl_blocks +=
block->cpl_in_use;
305 block->num_cpl_channels = 0;
306 for (ch = 1; ch <=
s->fbw_channels; ch++)
307 block->channel_in_cpl[ch] = 0;
312 for (ch = 1; ch <=
s->fbw_channels; ch++) {
313 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
314 block->new_cpl_strategy = 1;
319 block->new_cpl_leak =
block->new_cpl_strategy;
321 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
322 block->new_snr_offsets = 1;
323 if (
block->cpl_in_use)
326 block->new_snr_offsets = 0;
335 for (ch = 1; ch <=
s->fbw_channels; ch++) {
336 if (
block->channel_in_cpl[ch])
339 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
357 if (!
s->rematrixing_enabled)
362 if (
block->new_rematrixing_strategy)
365 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
369 for (
i = start;
i < end;
i++) {
372 block->fixed_coef[1][
i] = (lt + rt) >> 1;
373 block->fixed_coef[2][
i] = (lt - rt) >> 1;
386 int expstr,
i, grpsize;
389 grpsize = 3 << expstr;
390 for (
i = 12;
i < 256;
i++) {
406 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
409 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
417 #define EXP_DIFF_THRESHOLD 500
437 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
438 uint8_t *exp_strategy =
s->exp_strategy[ch];
439 uint8_t *
exp =
s->blocks[0].exp[ch];
448 if (!
s->blocks[
blk-1].cpl_in_use) {
451 }
else if (!
s->blocks[
blk].cpl_in_use) {
455 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
470 while (blk < s->num_blocks) {
472 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
486 if (CONFIG_EAC3_ENCODER &&
s->eac3)
507 switch(exp_strategy) {
509 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
510 uint8_t exp_min =
exp[k];
511 if (
exp[k+1] < exp_min)
513 exp[
i-cpl] = exp_min;
518 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
519 uint8_t exp_min =
exp[k];
520 if (
exp[k+1] < exp_min)
522 if (
exp[k+2] < exp_min)
524 if (
exp[k+3] < exp_min)
526 exp[
i-cpl] = exp_min;
533 if (!cpl &&
exp[0] > 15)
538 for (
i = 1;
i <= nb_groups;
i++)
548 switch (exp_strategy) {
550 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
551 uint8_t exp1 =
exp[
i-cpl];
557 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
574 int blk, blk1, ch, cpl;
575 uint8_t *
exp, *exp_strategy;
578 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
579 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
580 exp_strategy =
s->exp_strategy[ch];
584 while (blk < s->num_blocks) {
586 if (cpl && !
block->cpl_in_use) {
596 s->exp_ref_block[ch][
blk] =
blk;
597 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
598 s->exp_ref_block[ch][blk1] =
blk;
601 num_reuse_blocks = blk1 -
blk - 1;
604 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
625 int nb_groups, bit_count;
631 int exp_strategy =
s->exp_strategy[ch][
blk];
639 bit_count += 4 + (nb_groups * 7);
657 int group_size, nb_groups;
659 int delta0, delta1, delta2;
665 int exp_strategy =
s->exp_strategy[ch][
blk];
669 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
671 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
675 block->grouped_exp[ch][0] = exp1;
678 for (
i = 1;
i <= nb_groups;
i++) {
683 delta0 = exp1 - exp0 + 2;
689 delta1 = exp1 - exp0 + 2;
695 delta2 = exp1 - exp0 + 2;
698 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
730 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
749 if (
s->num_blocks != 0x6)
753 if (
s->num_blocks == 6)
757 if (
s->use_frame_exp_strategy)
758 frame_bits += 5 *
s->fbw_channels;
760 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
762 frame_bits +=
s->num_blocks;
764 if (
s->num_blks_code != 0x3)
767 frame_bits +=
s->fbw_channels * 5;
771 if (
s->num_blocks != 1)
775 frame_bits += frame_bits_inc[
s->channel_mode];
782 frame_bits +=
s->fbw_channels;
785 frame_bits +=
s->fbw_channels;
801 frame_bits += 2 *
s->fbw_channels;
808 frame_bits += 2 + 2 + 2 + 2 + 3;
827 frame_bits += 1 + 16;
829 s->frame_bits_fixed = frame_bits;
842 s->slow_decay_code = 2;
843 s->fast_decay_code = 1;
844 s->slow_gain_code = 1;
845 s->db_per_bit_code =
s->eac3 ? 2 : 3;
847 for (ch = 0; ch <=
s->channels; ch++)
848 s->fast_gain_code[ch] = 4;
851 s->coarse_snr_offset = 40;
861 s->bit_alloc.cpl_fast_leak = 0;
862 s->bit_alloc.cpl_slow_leak = 0;
888 frame_bits +=
s->lfe_on;
889 frame_bits += 1 + 1 + 2;
895 frame_bits += 3 + 1 + 1;
902 frame_bits += 5 + 2 + 1;
911 if (
block->new_cpl_strategy)
917 if (
s->use_frame_exp_strategy) {
921 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
927 if (
s->bitstream_id == 6) {
940 if (
block->new_cpl_strategy) {
943 if (
block->cpl_in_use) {
947 frame_bits +=
s->fbw_channels;
954 frame_bits +=
s->num_cpl_subbands - 1;
959 if (
block->cpl_in_use) {
960 for (ch = 1; ch <=
s->fbw_channels; ch++) {
961 if (
block->channel_in_cpl[ch]) {
962 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
964 if (
block->new_cpl_coords[ch]) {
966 frame_bits += (4 + 4) *
s->num_cpl_bands;
974 if (!
s->eac3 ||
blk > 0)
976 if (
s->blocks[
blk].new_rematrixing_strategy)
977 frame_bits +=
block->num_rematrixing_bands;
981 for (ch = 1; ch <=
s->fbw_channels; ch++) {
983 if (!
block->channel_in_cpl[ch])
990 if (!
s->eac3 &&
block->cpl_in_use)
995 if (
block->new_snr_offsets)
996 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1000 if (
block->cpl_in_use) {
1001 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1003 if (
block->new_cpl_leak)
1004 frame_bits += 3 + 3;
1008 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1029 block->band_psd[ch]);
1031 s->start_freq[ch],
block->end_freq[ch],
1033 ch ==
s->lfe_channel,
1051 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1054 ref_bap =
s->bap_buffer;
1055 for (ch = 0; ch <=
s->channels; ch++) {
1076 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1077 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1078 mant_cnt[
blk][4] = 1;
1103 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1104 s->ref_bap[ch][
blk] + start,
1115 int ch, max_end_freq;
1120 max_end_freq =
s->bandwidth_code * 3 + 73;
1121 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1125 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1143 snr_offset = (snr_offset - 240) * 4;
1155 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1156 s->start_freq[ch],
block->end_freq[ch],
1157 snr_offset,
s->bit_alloc.floor,
1174 int snr_offset, snr_incr;
1176 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1180 snr_offset =
s->coarse_snr_offset << 4;
1184 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1189 while (snr_offset >= 0 &&
1196 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1197 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1198 while (snr_offset + snr_incr <= 1023 &&
1199 bit_alloc(
s, snr_offset + snr_incr) <= bits_left) {
1200 snr_offset += snr_incr;
1201 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1204 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1207 s->coarse_snr_offset = snr_offset >> 4;
1208 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1209 s->fine_snr_offset[ch] = snr_offset & 0xF;
1243 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1261 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1262 m = (1 << (qbits-1));
1282 uint8_t *
exp, uint8_t *bap,
1283 int16_t *qmant,
int start_freq,
1288 for (
i = start_freq;
i < end_freq;
i++) {
1289 int c = fixed_coef[
i];
1297 switch (
s->mant1_cnt) {
1299 s->qmant1_ptr = &qmant[
i];
1304 *
s->qmant1_ptr += 3 * v;
1309 *
s->qmant1_ptr += v;
1317 switch (
s->mant2_cnt) {
1319 s->qmant2_ptr = &qmant[
i];
1324 *
s->qmant2_ptr += 5 * v;
1329 *
s->qmant2_ptr += v;
1340 switch (
s->mant4_cnt) {
1342 s->qmant4_ptr = &qmant[
i];
1347 *
s->qmant4_ptr += v;
1378 int blk, ch, ch0=0, got_cpl;
1384 got_cpl = !
block->cpl_in_use;
1385 for (ch = 1; ch <=
s->channels; ch++) {
1386 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1392 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1394 s->start_freq[ch],
block->end_freq[ch]);
1412 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1418 if (
s->channel_mode & 0x04)
1433 if (
s->bitstream_id == 6) {
1438 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1439 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1440 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1441 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1463 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1468 for (ch = 0; ch <
s->fbw_channels; ch++)
1474 for (ch = 0; ch <
s->fbw_channels; ch++)
1488 if (
block->new_cpl_strategy) {
1491 if (
block->cpl_in_use) {
1492 int start_sub, end_sub;
1496 for (ch = 1; ch <=
s->fbw_channels; ch++)
1501 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1502 end_sub = (
s->cpl_end_freq - 37) / 12;
1509 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1516 if (
block->cpl_in_use) {
1517 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1518 if (
block->channel_in_cpl[ch]) {
1519 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1521 if (
block->new_cpl_coords[ch]) {
1523 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1534 if (!
s->eac3 ||
blk > 0)
1536 if (
block->new_rematrixing_strategy) {
1538 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1545 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1552 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1560 int cpl = (ch ==
CPL_CH);
1570 for (
i = 1;
i <= nb_groups;
i++)
1574 if (ch !=
s->lfe_channel && !cpl)
1594 if (
block->new_snr_offsets) {
1606 if (
block->cpl_in_use) {
1607 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1609 if (
block->new_cpl_leak) {
1610 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1611 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1621 got_cpl = !
block->cpl_in_use;
1622 for (ch = 1; ch <=
s->channels; ch++) {
1625 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1630 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1632 b =
s->ref_bap[ch][
blk][
i];
1635 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1636 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1638 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1651 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1654 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1671 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1691 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1694 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1707 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1712 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1717 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1718 s->frame_size - frame_size_58 - 3);
1720 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1722 if (crc2 == 0x770B) {
1723 frame[
s->frame_size - 3] ^= 0x1;
1724 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1743 s->output_frame_header(
s);
1779 *got_packet_ptr = 1;
1790 switch (
s->bitstream_id) {
1791 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1792 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1793 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1794 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1795 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1796 default:
snprintf(strbuf, 32,
"ERROR");
1798 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1801 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1802 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1803 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1804 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1806 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1808 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1812 s->center_mix_level);
1814 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1815 if (
s->has_surround)
1817 s->surround_mix_level);
1819 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1828 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1830 ff_dlog(avctx,
"mixing_level: {not written}\n");
1831 ff_dlog(avctx,
"room_type: {not written}\n");
1842 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1844 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1848 if (
s->bitstream_id == 6) {
1856 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1857 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1859 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1861 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1863 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1866 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1875 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1882 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1889 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1891 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1898 #define FLT_OPTION_THRESHOLD 0.01
1904 for (
i = 0;
i < v_list_size;
i++) {
1909 if (
i == v_list_size)
1917 float *opt_param,
const float *
list,
1918 int list_size,
int default_value,
int min_value,
1922 if (mixlev < min_value) {
1923 mixlev = default_value;
1924 if (*opt_param >= 0.0) {
1926 "default value: %0.3f\n", opt_name,
list[mixlev]);
1929 *opt_param =
list[mixlev];
1930 *ctx_param = mixlev;
1956 if (
s->has_center &&
1961 if (
s->has_surround &&
1999 if (
s->has_center) {
2002 &
s->center_mix_level);
2004 if (
s->has_surround) {
2007 &
s->surround_mix_level);
2016 if (!
s->eac3 ||
s->has_center) {
2021 &
s->ltrt_center_mix_level);
2026 &
s->loro_center_mix_level);
2028 if (!
s->eac3 ||
s->has_surround) {
2033 &
s->ltrt_surround_mix_level);
2038 &
s->loro_surround_mix_level);
2050 "specified number of channels\n");
2087 "room_type is set\n");
2092 "80dB and 111dB\n");
2102 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2103 if (!
s->warned_alternate_bitstream) {
2105 "not compatible with reduced samplerates. writing of "
2106 "extended bitstream information will be disabled.\n");
2107 s->warned_alternate_bitstream = 1;
2110 s->bitstream_id = 6;
2130 if (
s->planar_samples)
2131 for (ch = 0; ch <
s->channels; ch++)
2171 uint64_t *channel_layout)
2177 if (*channel_layout > 0x7FF)
2179 ch_layout = *channel_layout;
2186 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2190 switch (ch_layout) {
2204 s->has_surround =
s->channel_mode & 0x04;
2207 *channel_layout = ch_layout;
2223 "encoder will guess the layout, but it "
2224 "might be incorrect.\n");
2236 max_sr =
s->eac3 ? 2 : 8;
2237 for (
i = 0;
i <= max_sr;
i++) {
2246 s->bit_alloc.sr_shift =
i / 3;
2247 s->bit_alloc.sr_code =
i % 3;
2248 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2252 switch (
s->fbw_channels) {
2253 case 1: avctx->
bit_rate = 96000;
break;
2254 case 2: avctx->
bit_rate = 192000;
break;
2255 case 3: avctx->
bit_rate = 320000;
break;
2256 case 4: avctx->
bit_rate = 384000;
break;
2257 case 5: avctx->
bit_rate = 448000;
break;
2263 int max_br, min_br, wpf, min_br_code;
2264 int num_blks_code, num_blocks, frame_samples;
2265 long long min_br_dist;
2271 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2272 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2274 max_br = 2048 *
s->sample_rate / frame_samples * 16;
2275 min_br = ((
s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2281 "for this sample rate\n", min_br, max_br);
2284 s->num_blks_code = num_blks_code;
2285 s->num_blocks = num_blocks;
2288 wpf = (avctx->
bit_rate / 16) * frame_samples /
s->sample_rate;
2295 min_br_dist = INT64_MAX;
2296 for (
i = 0;
i < 19;
i++) {
2298 if (br_dist < min_br_dist) {
2299 min_br_dist = br_dist;
2305 s->frame_size_code = min_br_code << 1;
2308 s->frame_size_min = 2 * wpf;
2310 int best_br = 0, best_code = 0;
2311 long long best_diff = INT64_MAX;
2312 for (
i = 0;
i < 19;
i++) {
2315 if (
diff < best_diff) {
2324 s->frame_size_code = best_code << 1;
2326 s->num_blks_code = 0x3;
2330 s->frame_size =
s->frame_size_min;
2338 if (
s->cutoff > (
s->sample_rate >> 1))
2339 s->cutoff =
s->sample_rate >> 1;
2345 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2348 s->cpl_enabled =
s->options.channel_coupling &&
2368 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2371 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2375 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2376 s->start_freq[ch] = 0;
2378 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2382 s->start_freq[
s->lfe_channel] = 0;
2384 s->blocks[
blk].end_freq[ch] = 7;
2388 if (
s->cpl_enabled) {
2390 cpl_start =
s->options.cpl_start;
2393 if (cpl_start < 0) {
2401 if (
s->cpl_enabled) {
2402 int i, cpl_start_band, cpl_end_band;
2403 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2405 cpl_end_band =
s->bandwidth_code / 4 + 3;
2406 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2408 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2410 s->num_cpl_bands = 1;
2411 *cpl_band_sizes = 12;
2412 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2414 *cpl_band_sizes += 12;
2418 *cpl_band_sizes = 12;
2422 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2423 s->cpl_end_freq = cpl_end_band * 12 + 37;
2425 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2434 int channel_blocks =
channels *
s->num_blocks;
2437 if (
s->allocate_sample_buffers(
s))
2451 if (
s->cpl_enabled) {
2468 if (
s->cpl_enabled) {
2474 for (ch = 0; ch <
channels; ch++) {
2481 if (
s->cpl_enabled) {
2482 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2483 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2492 if (!
s->fixed_point) {
2520 int ret, frame_size_58;
2535 s->bitstream_mode = 0x7;
2537 s->bits_written = 0;
2538 s->samples_written = 0;
2541 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2543 if (
s->bit_alloc.sr_code == 1) {
2544 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2548 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2559 ret =
s->mdct_init(
s);