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)
153 #if FF_API_OLD_CHANNEL_LAYOUT
154 const uint64_t ff_ac3_channel_layouts[19] = {
223 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
224 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
235 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
236 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
237 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
239 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
240 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
241 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
243 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
244 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
245 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
247 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
248 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
249 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
251 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
252 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
253 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
269 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
270 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
271 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -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 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
276 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
281 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
284 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
285 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
286 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
289 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
290 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
291 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
294 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
295 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
296 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
308 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
309 s->bits_written -=
s->bit_rate;
310 s->samples_written -=
s->sample_rate;
312 s->frame_size =
s->frame_size_min +
313 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
314 s->bits_written +=
s->frame_size * 8;
334 for (ch = 1; ch <=
s->fbw_channels; ch++)
335 block->channel_in_cpl[ch] =
s->cpl_on;
344 block->num_cpl_channels = 0;
345 for (ch = 1; ch <=
s->fbw_channels; ch++)
346 block->num_cpl_channels +=
block->channel_in_cpl[ch];
347 block->cpl_in_use =
block->num_cpl_channels > 1;
348 num_cpl_blocks +=
block->cpl_in_use;
350 block->num_cpl_channels = 0;
351 for (ch = 1; ch <=
s->fbw_channels; ch++)
352 block->channel_in_cpl[ch] = 0;
357 for (ch = 1; ch <=
s->fbw_channels; ch++) {
358 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
359 block->new_cpl_strategy = 1;
364 block->new_cpl_leak =
block->new_cpl_strategy;
366 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
367 block->new_snr_offsets = 1;
368 if (
block->cpl_in_use)
371 block->new_snr_offsets = 0;
380 for (ch = 1; ch <=
s->fbw_channels; ch++) {
381 if (
block->channel_in_cpl[ch])
384 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
402 if (!
s->rematrixing_enabled)
407 if (
block->new_rematrixing_strategy)
410 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
414 for (
i = start;
i < end;
i++) {
417 block->fixed_coef[1][
i] = (lt + rt) >> 1;
418 block->fixed_coef[2][
i] = (lt - rt) >> 1;
431 int expstr,
i, grpsize;
434 grpsize = 3 << expstr;
435 for (
i = 12;
i < 256;
i++) {
451 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
454 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
462 #define EXP_DIFF_THRESHOLD 500
482 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
483 uint8_t *exp_strategy =
s->exp_strategy[ch];
484 uint8_t *
exp =
s->blocks[0].exp[ch];
493 if (!
s->blocks[
blk-1].cpl_in_use) {
496 }
else if (!
s->blocks[
blk].cpl_in_use) {
500 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
515 while (blk < s->num_blocks) {
517 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
531 if (CONFIG_EAC3_ENCODER &&
s->eac3)
552 switch(exp_strategy) {
554 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
555 uint8_t exp_min =
exp[k];
556 if (
exp[k+1] < exp_min)
558 exp[
i-cpl] = exp_min;
563 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
564 uint8_t exp_min =
exp[k];
565 if (
exp[k+1] < exp_min)
567 if (
exp[k+2] < exp_min)
569 if (
exp[k+3] < exp_min)
571 exp[
i-cpl] = exp_min;
578 if (!cpl &&
exp[0] > 15)
583 for (
i = 1;
i <= nb_groups;
i++)
593 switch (exp_strategy) {
595 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
596 uint8_t exp1 =
exp[
i-cpl];
602 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
619 int blk, blk1, ch, cpl;
620 uint8_t *
exp, *exp_strategy;
623 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
624 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
625 exp_strategy =
s->exp_strategy[ch];
629 while (blk < s->num_blocks) {
631 if (cpl && !
block->cpl_in_use) {
641 s->exp_ref_block[ch][
blk] =
blk;
642 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
643 s->exp_ref_block[ch][blk1] =
blk;
646 num_reuse_blocks = blk1 -
blk - 1;
649 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
670 int nb_groups, bit_count;
676 int exp_strategy =
s->exp_strategy[ch][
blk];
684 bit_count += 4 + (nb_groups * 7);
702 int group_size, nb_groups;
704 int delta0, delta1, delta2;
710 int exp_strategy =
s->exp_strategy[ch][
blk];
714 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
716 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
720 block->grouped_exp[ch][0] = exp1;
723 for (
i = 1;
i <= nb_groups;
i++) {
728 delta0 = exp1 - exp0 + 2;
734 delta1 = exp1 - exp0 + 2;
740 delta2 = exp1 - exp0 + 2;
743 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
775 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
794 if (
s->num_blocks != 0x6)
798 if (
s->num_blocks == 6)
802 if (
s->use_frame_exp_strategy)
803 frame_bits += 5 *
s->fbw_channels;
805 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
807 frame_bits +=
s->num_blocks;
809 if (
s->num_blks_code != 0x3)
812 frame_bits +=
s->fbw_channels * 5;
816 if (
s->num_blocks != 1)
820 frame_bits += frame_bits_inc[
s->channel_mode];
827 frame_bits +=
s->fbw_channels;
830 frame_bits +=
s->fbw_channels;
846 frame_bits += 2 *
s->fbw_channels;
853 frame_bits += 2 + 2 + 2 + 2 + 3;
872 frame_bits += 1 + 16;
874 s->frame_bits_fixed = frame_bits;
887 s->slow_decay_code = 2;
888 s->fast_decay_code = 1;
889 s->slow_gain_code = 1;
890 s->db_per_bit_code =
s->eac3 ? 2 : 3;
892 for (ch = 0; ch <=
s->channels; ch++)
893 s->fast_gain_code[ch] = 4;
896 s->coarse_snr_offset = 40;
906 s->bit_alloc.cpl_fast_leak = 0;
907 s->bit_alloc.cpl_slow_leak = 0;
933 frame_bits +=
s->lfe_on;
934 frame_bits += 1 + 1 + 2;
940 frame_bits += 3 + 1 + 1;
947 frame_bits += 5 + 2 + 1;
956 if (
block->new_cpl_strategy)
962 if (
s->use_frame_exp_strategy) {
966 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
972 if (
s->bitstream_id == 6) {
985 if (
block->new_cpl_strategy) {
988 if (
block->cpl_in_use) {
992 frame_bits +=
s->fbw_channels;
999 frame_bits +=
s->num_cpl_subbands - 1;
1004 if (
block->cpl_in_use) {
1005 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1006 if (
block->channel_in_cpl[ch]) {
1007 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1009 if (
block->new_cpl_coords[ch]) {
1011 frame_bits += (4 + 4) *
s->num_cpl_bands;
1019 if (!
s->eac3 ||
blk > 0)
1021 if (
s->blocks[
blk].new_rematrixing_strategy)
1022 frame_bits +=
block->num_rematrixing_bands;
1026 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1028 if (!
block->channel_in_cpl[ch])
1035 if (!
s->eac3 &&
block->cpl_in_use)
1040 if (
block->new_snr_offsets)
1041 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1045 if (
block->cpl_in_use) {
1046 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1048 if (
block->new_cpl_leak)
1049 frame_bits += 3 + 3;
1053 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1074 block->band_psd[ch]);
1076 s->start_freq[ch],
block->end_freq[ch],
1078 ch ==
s->lfe_channel,
1096 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1099 ref_bap =
s->bap_buffer;
1100 for (ch = 0; ch <=
s->channels; ch++) {
1121 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1122 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1123 mant_cnt[
blk][4] = 1;
1148 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1149 s->ref_bap[ch][
blk] + start,
1160 int ch, max_end_freq;
1165 max_end_freq =
s->bandwidth_code * 3 + 73;
1166 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1170 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1188 snr_offset = (snr_offset - 240) * 4;
1200 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1201 s->start_freq[ch],
block->end_freq[ch],
1202 snr_offset,
s->bit_alloc.floor,
1219 int snr_offset, snr_incr;
1221 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1225 snr_offset =
s->coarse_snr_offset << 4;
1229 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1234 while (snr_offset >= 0 &&
1241 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1242 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1243 while (snr_offset + snr_incr <= 1023 &&
1245 snr_offset += snr_incr;
1246 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1249 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1252 s->coarse_snr_offset = snr_offset >> 4;
1253 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1254 s->fine_snr_offset[ch] = snr_offset & 0xF;
1288 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1306 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1307 m = (1 << (qbits-1));
1327 uint8_t *
exp, uint8_t *bap,
1328 int16_t *qmant,
int start_freq,
1333 for (
i = start_freq;
i < end_freq;
i++) {
1334 int c = fixed_coef[
i];
1342 switch (
s->mant1_cnt) {
1344 s->qmant1_ptr = &qmant[
i];
1349 *
s->qmant1_ptr += 3 * v;
1354 *
s->qmant1_ptr += v;
1362 switch (
s->mant2_cnt) {
1364 s->qmant2_ptr = &qmant[
i];
1369 *
s->qmant2_ptr += 5 * v;
1374 *
s->qmant2_ptr += v;
1385 switch (
s->mant4_cnt) {
1387 s->qmant4_ptr = &qmant[
i];
1392 *
s->qmant4_ptr += v;
1423 int blk, ch, ch0=0, got_cpl;
1429 got_cpl = !
block->cpl_in_use;
1430 for (ch = 1; ch <=
s->channels; ch++) {
1431 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1437 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1439 s->start_freq[ch],
block->end_freq[ch]);
1457 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1463 if (
s->channel_mode & 0x04)
1478 if (
s->bitstream_id == 6) {
1483 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1484 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1485 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1486 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1508 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1513 for (ch = 0; ch <
s->fbw_channels; ch++)
1519 for (ch = 0; ch <
s->fbw_channels; ch++)
1533 if (
block->new_cpl_strategy) {
1536 if (
block->cpl_in_use) {
1537 int start_sub, end_sub;
1541 for (ch = 1; ch <=
s->fbw_channels; ch++)
1546 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1547 end_sub = (
s->cpl_end_freq - 37) / 12;
1554 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1561 if (
block->cpl_in_use) {
1562 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1563 if (
block->channel_in_cpl[ch]) {
1564 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1566 if (
block->new_cpl_coords[ch]) {
1568 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1579 if (!
s->eac3 ||
blk > 0)
1581 if (
block->new_rematrixing_strategy) {
1583 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1590 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1597 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1605 int cpl = (ch ==
CPL_CH);
1615 for (
i = 1;
i <= nb_groups;
i++)
1619 if (ch !=
s->lfe_channel && !cpl)
1639 if (
block->new_snr_offsets) {
1651 if (
block->cpl_in_use) {
1652 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1654 if (
block->new_cpl_leak) {
1655 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1656 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1666 got_cpl = !
block->cpl_in_use;
1667 for (ch = 1; ch <=
s->channels; ch++) {
1670 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1675 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1677 b =
s->ref_bap[ch][
blk][
i];
1680 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1681 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1683 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1696 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1699 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1716 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1736 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1739 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1752 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1757 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1762 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1763 s->frame_size - frame_size_58 - 3);
1765 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1767 if (crc2 == 0x770B) {
1768 frame[
s->frame_size - 3] ^= 0x1;
1769 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1788 s->output_frame_header(
s);
1824 *got_packet_ptr = 1;
1835 switch (
s->bitstream_id) {
1836 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1837 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1838 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1839 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1840 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1841 default:
snprintf(strbuf, 32,
"ERROR");
1843 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1846 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1847 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1848 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1849 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1851 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1853 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1857 s->center_mix_level);
1859 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1860 if (
s->has_surround)
1862 s->surround_mix_level);
1864 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1873 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1875 ff_dlog(avctx,
"mixing_level: {not written}\n");
1876 ff_dlog(avctx,
"room_type: {not written}\n");
1887 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1889 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1893 if (
s->bitstream_id == 6) {
1901 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1902 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1904 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1906 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1908 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1911 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1920 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1927 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1934 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1936 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1943 #define FLT_OPTION_THRESHOLD 0.01
1949 for (
i = 0;
i < v_list_size;
i++) {
1954 if (
i == v_list_size)
1962 float *opt_param,
const float *
list,
1963 int list_size,
int default_value,
int min_value,
1967 if (mixlev < min_value) {
1968 mixlev = default_value;
1969 if (*opt_param >= 0.0) {
1971 "default value: %0.3f\n", opt_name,
list[mixlev]);
1974 *opt_param =
list[mixlev];
1975 *ctx_param = mixlev;
2001 if (
s->has_center &&
2006 if (
s->has_surround &&
2044 if (
s->has_center) {
2047 &
s->center_mix_level);
2049 if (
s->has_surround) {
2052 &
s->surround_mix_level);
2061 if (!
s->eac3 ||
s->has_center) {
2066 &
s->ltrt_center_mix_level);
2071 &
s->loro_center_mix_level);
2073 if (!
s->eac3 ||
s->has_surround) {
2078 &
s->ltrt_surround_mix_level);
2083 &
s->loro_surround_mix_level);
2095 "specified number of channels\n");
2132 "room_type is set\n");
2137 "80dB and 111dB\n");
2147 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2148 if (!
s->warned_alternate_bitstream) {
2150 "not compatible with reduced samplerates. writing of "
2151 "extended bitstream information will be disabled.\n");
2152 s->warned_alternate_bitstream = 1;
2155 s->bitstream_id = 6;
2175 if (
s->planar_samples)
2176 for (ch = 0; ch <
s->channels; ch++)
2233 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2251 s->has_surround =
s->channel_mode & 0x04;
2270 "encoder will guess the layout, but it "
2271 "might be incorrect.\n");
2283 max_sr =
s->eac3 ? 2 : 8;
2284 for (
i = 0;
i <= max_sr;
i++) {
2293 s->bit_alloc.sr_shift =
i / 3;
2294 s->bit_alloc.sr_code =
i % 3;
2295 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2299 switch (
s->fbw_channels) {
2300 case 1: avctx->
bit_rate = 96000;
break;
2301 case 2: avctx->
bit_rate = 192000;
break;
2302 case 3: avctx->
bit_rate = 320000;
break;
2303 case 4: avctx->
bit_rate = 384000;
break;
2304 case 5: avctx->
bit_rate = 448000;
break;
2310 int max_br, min_br, wpf, min_br_code;
2312 long long min_br_dist;
2318 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2319 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2328 "for this sample rate\n", min_br, max_br);
2331 s->num_blks_code = num_blks_code;
2332 s->num_blocks = num_blocks;
2342 min_br_dist = INT64_MAX;
2343 for (
i = 0;
i < 19;
i++) {
2345 if (br_dist < min_br_dist) {
2346 min_br_dist = br_dist;
2352 s->frame_size_code = min_br_code << 1;
2355 s->frame_size_min = 2 * wpf;
2357 int best_br = 0, best_code = 0;
2358 long long best_diff = INT64_MAX;
2359 for (
i = 0;
i < 19;
i++) {
2362 if (
diff < best_diff) {
2371 s->frame_size_code = best_code << 1;
2373 s->num_blks_code = 0x3;
2377 s->frame_size =
s->frame_size_min;
2385 if (
s->cutoff > (
s->sample_rate >> 1))
2386 s->cutoff =
s->sample_rate >> 1;
2392 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2395 s->cpl_enabled =
s->options.channel_coupling &&
2415 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2418 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2422 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2423 s->start_freq[ch] = 0;
2425 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2429 s->start_freq[
s->lfe_channel] = 0;
2431 s->blocks[
blk].end_freq[ch] = 7;
2435 if (
s->cpl_enabled) {
2437 cpl_start =
s->options.cpl_start;
2440 if (cpl_start < 0) {
2448 if (
s->cpl_enabled) {
2449 int i, cpl_start_band, cpl_end_band;
2450 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2452 cpl_end_band =
s->bandwidth_code / 4 + 3;
2453 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2455 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2457 s->num_cpl_bands = 1;
2458 *cpl_band_sizes = 12;
2459 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2461 *cpl_band_sizes += 12;
2465 *cpl_band_sizes = 12;
2469 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2470 s->cpl_end_freq = cpl_end_band * 12 + 37;
2472 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2481 int channel_blocks =
channels *
s->num_blocks;
2484 if (
s->allocate_sample_buffers(
s))
2498 if (
s->cpl_enabled) {
2515 if (
s->cpl_enabled) {
2521 for (ch = 0; ch <
channels; ch++) {
2528 if (
s->cpl_enabled) {
2529 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2530 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2539 if (!
s->fixed_point) {
2567 int ret, frame_size_58;
2582 s->bitstream_mode = 0x7;
2584 s->bits_written = 0;
2585 s->samples_written = 0;
2588 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2590 if (
s->bit_alloc.sr_code == 1) {
2591 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2595 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2606 ret =
s->mdct_init(
s);