54 #define CMIXLEV_NUM_OPTIONS 3
59 #define SURMIXLEV_NUM_OPTIONS 3
64 #define EXTMIXLEV_NUM_OPTIONS 8
112 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
113 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
114 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
116 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
117 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
118 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
120 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
121 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
122 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
124 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
126 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
128 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
130 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
146 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
147 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
148 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
151 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
152 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
153 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
156 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
157 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
158 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
161 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
162 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
163 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
166 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
167 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
168 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
171 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
172 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
173 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
185 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
186 s->bits_written -=
s->bit_rate;
187 s->samples_written -=
s->sample_rate;
189 s->frame_size =
s->frame_size_min +
190 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
191 s->bits_written +=
s->frame_size * 8;
211 for (ch = 1; ch <=
s->fbw_channels; ch++)
212 block->channel_in_cpl[ch] =
s->cpl_on;
221 block->num_cpl_channels = 0;
222 for (ch = 1; ch <=
s->fbw_channels; ch++)
223 block->num_cpl_channels +=
block->channel_in_cpl[ch];
224 block->cpl_in_use =
block->num_cpl_channels > 1;
225 num_cpl_blocks +=
block->cpl_in_use;
227 block->num_cpl_channels = 0;
228 for (ch = 1; ch <=
s->fbw_channels; ch++)
229 block->channel_in_cpl[ch] = 0;
234 for (ch = 1; ch <=
s->fbw_channels; ch++) {
235 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
236 block->new_cpl_strategy = 1;
241 block->new_cpl_leak =
block->new_cpl_strategy;
243 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
244 block->new_snr_offsets = 1;
245 if (
block->cpl_in_use)
248 block->new_snr_offsets = 0;
257 for (ch = 1; ch <=
s->fbw_channels; ch++) {
258 if (
block->channel_in_cpl[ch])
261 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
279 if (!
s->rematrixing_enabled)
284 if (
block->new_rematrixing_strategy)
287 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
291 for (
i = start;
i <
end;
i++) {
294 block->fixed_coef[1][
i] = (lt + rt) >> 1;
295 block->fixed_coef[2][
i] = (lt - rt) >> 1;
308 int expstr,
i, grpsize;
311 grpsize = 3 << expstr;
312 for (
i = 12;
i < 256;
i++) {
320 if (CONFIG_EAC3_ENCODER &&
s->eac3)
331 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
334 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
342 #define EXP_DIFF_THRESHOLD 500
362 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
363 uint8_t *exp_strategy =
s->exp_strategy[ch];
373 if (!
s->blocks[
blk-1].cpl_in_use) {
376 }
else if (!
s->blocks[
blk].cpl_in_use) {
380 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
395 while (blk < s->num_blocks) {
397 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
411 if (CONFIG_EAC3_ENCODER &&
s->eac3)
432 switch(exp_strategy) {
434 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
436 if (
exp[k+1] < exp_min)
438 exp[
i-cpl] = exp_min;
443 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
445 if (
exp[k+1] < exp_min)
447 if (
exp[k+2] < exp_min)
449 if (
exp[k+3] < exp_min)
451 exp[
i-cpl] = exp_min;
458 if (!cpl &&
exp[0] > 15)
463 for (
i = 1;
i <= nb_groups;
i++)
473 switch (exp_strategy) {
475 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
482 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
499 int blk, blk1, ch, cpl;
501 int nb_coefs, num_reuse_blocks;
503 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
504 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
505 exp_strategy =
s->exp_strategy[ch];
509 while (blk < s->num_blocks) {
511 if (cpl && !
block->cpl_in_use) {
516 nb_coefs =
block->end_freq[ch] -
s->start_freq[ch];
521 s->exp_ref_block[ch][
blk] =
blk;
522 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
523 s->exp_ref_block[ch][blk1] =
blk;
526 num_reuse_blocks = blk1 -
blk - 1;
529 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
550 int nb_groups, bit_count;
556 int exp_strategy =
s->exp_strategy[ch][
blk];
558 int nb_coefs =
block->end_freq[ch] -
s->start_freq[ch];
564 bit_count += 4 + (nb_groups * 7);
582 int group_size, nb_groups;
584 int delta0, delta1, delta2;
590 int exp_strategy =
s->exp_strategy[ch][
blk];
594 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
596 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
600 block->grouped_exp[ch][0] = exp1;
603 for (
i = 1;
i <= nb_groups;
i++) {
608 delta0 = exp1 - exp0 + 2;
614 delta1 = exp1 - exp0 + 2;
620 delta2 = exp1 - exp0 + 2;
623 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
655 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
674 if (
s->num_blocks != 0x6)
678 if (
s->num_blocks == 6)
682 if (
s->use_frame_exp_strategy)
683 frame_bits += 5 *
s->fbw_channels;
685 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
687 frame_bits +=
s->num_blocks;
689 if (
s->num_blks_code != 0x3)
692 frame_bits +=
s->fbw_channels * 5;
696 if (
s->num_blocks != 1)
700 frame_bits += frame_bits_inc[
s->channel_mode];
707 frame_bits +=
s->fbw_channels;
710 frame_bits +=
s->fbw_channels;
722 frame_bits += 2 *
s->fbw_channels;
729 frame_bits += 2 + 2 + 2 + 2 + 3;
749 frame_bits += 1 + 16;
751 s->frame_bits_fixed = frame_bits;
764 s->slow_decay_code = 2;
765 s->fast_decay_code = 1;
766 s->slow_gain_code = 1;
767 s->db_per_bit_code =
s->eac3 ? 2 : 3;
769 for (ch = 0; ch <=
s->channels; ch++)
770 s->fast_gain_code[ch] = 4;
773 s->coarse_snr_offset = 40;
783 s->bit_alloc.cpl_fast_leak = 0;
784 s->bit_alloc.cpl_slow_leak = 0;
810 frame_bits +=
s->lfe_on;
811 frame_bits += 1 + 1 + 2;
817 frame_bits += 3 + 1 + 1;
824 frame_bits += 5 + 2 + 1;
833 if (
block->new_cpl_strategy)
839 if (
s->use_frame_exp_strategy) {
840 frame_bits += 5 *
s->cpl_on;
843 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
849 if (
s->bitstream_id == 6) {
864 if (
block->new_cpl_strategy) {
867 if (
block->cpl_in_use) {
871 frame_bits +=
s->fbw_channels;
878 frame_bits +=
s->num_cpl_subbands - 1;
883 if (
block->cpl_in_use) {
884 for (ch = 1; ch <=
s->fbw_channels; ch++) {
885 if (
block->channel_in_cpl[ch]) {
886 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
888 if (
block->new_cpl_coords[ch]) {
890 frame_bits += (4 + 4) *
s->num_cpl_bands;
898 if (!
s->eac3 ||
blk > 0)
900 if (
s->blocks[
blk].new_rematrixing_strategy)
901 frame_bits +=
block->num_rematrixing_bands;
905 for (ch = 1; ch <=
s->fbw_channels; ch++) {
907 if (!
block->channel_in_cpl[ch])
914 if (!
s->eac3 &&
block->cpl_in_use)
920 if (
block->new_snr_offsets)
921 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
925 if (
block->cpl_in_use) {
926 if (!
s->eac3 ||
block->new_cpl_leak != 2)
928 if (
block->new_cpl_leak)
933 s->frame_bits =
s->frame_bits_fixed + frame_bits;
954 block->band_psd[ch]);
956 s->start_freq[ch],
block->end_freq[ch],
958 ch ==
s->lfe_channel,
976 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
979 ref_bap =
s->bap_buffer;
980 for (ch = 0; ch <=
s->channels; ch++) {
1001 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1002 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1003 mant_cnt[
blk][4] = 1;
1028 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1029 s->ref_bap[ch][
blk] + start,
1040 int ch, max_end_freq;
1045 max_end_freq =
s->bandwidth_code * 3 + 73;
1046 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1050 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1068 snr_offset = (snr_offset - 240) * 4;
1080 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1081 s->start_freq[ch],
block->end_freq[ch],
1082 snr_offset,
s->bit_alloc.floor,
1099 int snr_offset, snr_incr;
1101 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1105 snr_offset =
s->coarse_snr_offset << 4;
1109 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1114 while (snr_offset >= 0 &&
1122 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1123 while (snr_offset + snr_incr <= 1023 &&
1124 bit_alloc(
s, snr_offset + snr_incr) <= bits_left) {
1125 snr_offset += snr_incr;
1132 s->coarse_snr_offset = snr_offset >> 4;
1133 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1134 s->fine_snr_offset[ch] = snr_offset & 0xF;
1168 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1186 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1187 m = (1 << (qbits-1));
1208 int16_t *qmant,
int start_freq,
1213 for (
i = start_freq;
i < end_freq;
i++) {
1214 int c = fixed_coef[
i];
1221 switch (
s->mant1_cnt) {
1223 s->qmant1_ptr = &qmant[
i];
1228 *
s->qmant1_ptr += 3 * v;
1233 *
s->qmant1_ptr += v;
1241 switch (
s->mant2_cnt) {
1243 s->qmant2_ptr = &qmant[
i];
1248 *
s->qmant2_ptr += 5 * v;
1253 *
s->qmant2_ptr += v;
1264 switch (
s->mant4_cnt) {
1266 s->qmant4_ptr = &qmant[
i];
1271 *
s->qmant4_ptr += v;
1302 int blk, ch, ch0=0, got_cpl;
1308 got_cpl = !
block->cpl_in_use;
1309 for (ch = 1; ch <=
s->channels; ch++) {
1310 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1316 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1318 s->start_freq[ch],
block->end_freq[ch]);
1336 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1342 if (
s->channel_mode & 0x04)
1357 if (
s->bitstream_id == 6) {
1362 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1363 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1364 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1365 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1387 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1392 for (ch = 0; ch <
s->fbw_channels; ch++)
1398 for (ch = 0; ch <
s->fbw_channels; ch++)
1412 if (
block->new_cpl_strategy) {
1415 if (
block->cpl_in_use) {
1416 int start_sub, end_sub;
1420 for (ch = 1; ch <=
s->fbw_channels; ch++)
1425 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1426 end_sub = (
s->cpl_end_freq - 37) / 12;
1433 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1440 if (
block->cpl_in_use) {
1441 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1442 if (
block->channel_in_cpl[ch]) {
1443 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1445 if (
block->new_cpl_coords[ch]) {
1447 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1458 if (!
s->eac3 ||
blk > 0)
1460 if (
block->new_rematrixing_strategy) {
1462 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1469 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1476 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1484 int cpl = (ch ==
CPL_CH);
1494 for (
i = 1;
i <= nb_groups;
i++)
1498 if (ch !=
s->lfe_channel && !cpl)
1518 if (
block->new_snr_offsets) {
1530 if (
block->cpl_in_use) {
1531 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1533 if (
block->new_cpl_leak) {
1534 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1535 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1545 got_cpl = !
block->cpl_in_use;
1546 for (ch = 1; ch <=
s->channels; ch++) {
1549 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1554 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1556 b =
s->ref_bap[ch][
blk][
i];
1559 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1560 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1562 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1575 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1578 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1595 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1615 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1618 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1631 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1636 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1641 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1642 s->frame_size - frame_size_58 - 3);
1644 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1646 if (crc2 == 0x770B) {
1647 frame[
s->frame_size - 3] ^= 0x1;
1648 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1667 s->output_frame_header(
s);
1683 switch (
s->bitstream_id) {
1684 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1685 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1686 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1687 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1688 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1689 default:
snprintf(strbuf, 32,
"ERROR");
1691 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1694 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1695 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1696 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1697 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1699 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1701 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1705 s->center_mix_level);
1707 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1708 if (
s->has_surround)
1710 s->surround_mix_level);
1712 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1721 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1723 ff_dlog(avctx,
"mixing_level: {not written}\n");
1724 ff_dlog(avctx,
"room_type: {not written}\n");
1735 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1737 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1741 if (
s->bitstream_id == 6) {
1749 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1750 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1752 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1754 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1756 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1759 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1768 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1775 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1782 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1784 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1791 #define FLT_OPTION_THRESHOLD 0.01
1797 for (
i = 0;
i < v_list_size;
i++) {
1802 if (
i == v_list_size)
1810 float *opt_param,
const float *
list,
1811 int list_size,
int default_value,
int min_value,
1815 if (mixlev < min_value) {
1816 mixlev = default_value;
1817 if (*opt_param >= 0.0) {
1819 "default value: %0.3f\n", opt_name,
list[mixlev]);
1822 *opt_param =
list[mixlev];
1823 *ctx_param = mixlev;
1849 if (
s->has_center &&
1854 if (
s->has_surround &&
1892 if (
s->has_center) {
1895 &
s->center_mix_level);
1897 if (
s->has_surround) {
1900 &
s->surround_mix_level);
1909 if (!
s->eac3 ||
s->has_center) {
1914 &
s->ltrt_center_mix_level);
1919 &
s->loro_center_mix_level);
1921 if (!
s->eac3 ||
s->has_surround) {
1926 &
s->ltrt_surround_mix_level);
1931 &
s->loro_surround_mix_level);
1943 "specified number of channels\n");
1980 "room_type is set\n");
1985 "80dB and 111dB\n");
1995 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
1996 if (!
s->warned_alternate_bitstream) {
1998 "not compatible with reduced samplerates. writing of "
1999 "extended bitstream information will be disabled.\n");
2000 s->warned_alternate_bitstream = 1;
2003 s->bitstream_id = 6;
2022 if (
s->planar_samples)
2023 for (ch = 0; ch <
s->channels; ch++)
2064 uint64_t *channel_layout)
2070 if (*channel_layout > 0x7FF)
2072 ch_layout = *channel_layout;
2079 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2083 switch (ch_layout) {
2097 s->has_surround =
s->channel_mode & 0x04;
2100 *channel_layout = ch_layout;
2116 "encoder will guess the layout, but it "
2117 "might be incorrect.\n");
2129 max_sr =
s->eac3 ? 2 : 8;
2130 for (
i = 0;
i <= max_sr;
i++) {
2139 s->bit_alloc.sr_shift =
i / 3;
2140 s->bit_alloc.sr_code =
i % 3;
2141 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2145 switch (
s->fbw_channels) {
2146 case 1: avctx->
bit_rate = 96000;
break;
2147 case 2: avctx->
bit_rate = 192000;
break;
2148 case 3: avctx->
bit_rate = 320000;
break;
2149 case 4: avctx->
bit_rate = 384000;
break;
2150 case 5: avctx->
bit_rate = 448000;
break;
2156 int max_br, min_br, wpf, min_br_code;
2157 int num_blks_code, num_blocks, frame_samples;
2158 long long min_br_dist;
2164 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2165 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2167 max_br = 2048 *
s->sample_rate / frame_samples * 16;
2168 min_br = ((
s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2174 "for this sample rate\n", min_br, max_br);
2177 s->num_blks_code = num_blks_code;
2178 s->num_blocks = num_blocks;
2181 wpf = (avctx->
bit_rate / 16) * frame_samples /
s->sample_rate;
2188 min_br_dist = INT64_MAX;
2189 for (
i = 0;
i < 19;
i++) {
2191 if (br_dist < min_br_dist) {
2192 min_br_dist = br_dist;
2198 s->frame_size_code = min_br_code << 1;
2201 s->frame_size_min = 2 * wpf;
2203 int best_br = 0, best_code = 0;
2204 long long best_diff = INT64_MAX;
2205 for (
i = 0;
i < 19;
i++) {
2208 if (
diff < best_diff) {
2217 s->frame_size_code = best_code << 1;
2219 s->num_blks_code = 0x3;
2223 s->frame_size =
s->frame_size_min;
2231 if (
s->cutoff > (
s->sample_rate >> 1))
2232 s->cutoff =
s->sample_rate >> 1;
2238 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2241 s->cpl_enabled =
s->options.channel_coupling &&
2261 s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
2264 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2268 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2269 s->start_freq[ch] = 0;
2271 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2275 s->start_freq[
s->lfe_channel] = 0;
2277 s->blocks[
blk].end_freq[ch] = 7;
2281 if (
s->cpl_enabled) {
2283 cpl_start =
s->options.cpl_start;
2286 if (cpl_start < 0) {
2294 if (
s->cpl_enabled) {
2295 int i, cpl_start_band, cpl_end_band;
2296 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2298 cpl_end_band =
s->bandwidth_code / 4 + 3;
2299 cpl_start_band = av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2301 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2303 s->num_cpl_bands = 1;
2304 *cpl_band_sizes = 12;
2305 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2307 *cpl_band_sizes += 12;
2311 *cpl_band_sizes = 12;
2315 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2316 s->cpl_end_freq = cpl_end_band * 12 + 37;
2318 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2328 int channel_blocks =
channels *
s->num_blocks;
2331 if (
s->allocate_sample_buffers(
s))
2335 sizeof(*
s->bap_buffer), alloc_fail);
2337 sizeof(*
s->bap1_buffer), alloc_fail);
2339 sizeof(*
s->mdct_coef_buffer), alloc_fail);
2341 sizeof(*
s->exp_buffer), alloc_fail);
2343 sizeof(*
s->grouped_exp_buffer), alloc_fail);
2345 sizeof(*
s->psd_buffer), alloc_fail);
2347 sizeof(*
s->band_psd_buffer), alloc_fail);
2349 sizeof(*
s->mask_buffer), alloc_fail);
2351 sizeof(*
s->qmant_buffer), alloc_fail);
2352 if (
s->cpl_enabled) {
2354 sizeof(*
s->cpl_coord_exp_buffer), alloc_fail);
2356 sizeof(*
s->cpl_coord_mant_buffer), alloc_fail);
2374 if (
s->cpl_enabled) {
2381 for (ch = 0; ch <
channels; ch++) {
2388 if (
s->cpl_enabled) {
2389 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2390 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2399 if (!
s->fixed_point) {
2401 sizeof(*
s->fixed_coef_buffer), alloc_fail);
2405 sizeof(*
block->fixed_coef), alloc_fail);
2413 sizeof(*
block->fixed_coef), alloc_fail);
2428 int ret, frame_size_58;
2443 s->bitstream_mode = 0x7;
2445 s->bits_written = 0;
2446 s->samples_written = 0;
2449 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2451 if (
s->bit_alloc.sr_code == 1) {
2452 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2457 if (CONFIG_AC3_FIXED_ENCODER &&
s->fixed_point) {
2461 }
else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
2466 if (CONFIG_EAC3_ENCODER &&
s->eac3)
2477 ret =
s->mdct_init(
s);