56 #define CMIXLEV_NUM_OPTIONS 3
61 #define SURMIXLEV_NUM_OPTIONS 3
66 #define EXTMIXLEV_NUM_OPTIONS 8
75 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
76 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
170 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
171 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
182 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
183 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
184 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
186 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
187 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
188 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
190 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
191 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
192 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
194 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
195 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
196 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
198 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
199 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
200 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
216 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
217 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
218 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
221 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
222 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
223 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
226 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
227 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
228 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
231 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
232 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
233 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
236 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
237 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
238 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
241 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
242 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
243 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
255 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
256 s->bits_written -=
s->bit_rate;
257 s->samples_written -=
s->sample_rate;
259 s->frame_size =
s->frame_size_min +
260 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
261 s->bits_written +=
s->frame_size * 8;
281 for (ch = 1; ch <=
s->fbw_channels; ch++)
282 block->channel_in_cpl[ch] =
s->cpl_on;
291 block->num_cpl_channels = 0;
292 for (ch = 1; ch <=
s->fbw_channels; ch++)
293 block->num_cpl_channels +=
block->channel_in_cpl[ch];
294 block->cpl_in_use =
block->num_cpl_channels > 1;
295 num_cpl_blocks +=
block->cpl_in_use;
297 block->num_cpl_channels = 0;
298 for (ch = 1; ch <=
s->fbw_channels; ch++)
299 block->channel_in_cpl[ch] = 0;
304 for (ch = 1; ch <=
s->fbw_channels; ch++) {
305 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
306 block->new_cpl_strategy = 1;
311 block->new_cpl_leak =
block->new_cpl_strategy;
313 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
314 block->new_snr_offsets = 1;
315 if (
block->cpl_in_use)
318 block->new_snr_offsets = 0;
327 for (ch = 1; ch <=
s->fbw_channels; ch++) {
328 if (
block->channel_in_cpl[ch])
331 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
349 if (!
s->rematrixing_enabled)
354 if (
block->new_rematrixing_strategy)
357 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
361 for (
i = start;
i < end;
i++) {
364 block->fixed_coef[1][
i] = (lt + rt) >> 1;
365 block->fixed_coef[2][
i] = (lt - rt) >> 1;
378 int expstr,
i, grpsize;
381 grpsize = 3 << expstr;
382 for (
i = 12;
i < 256;
i++) {
398 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
401 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
409 #define EXP_DIFF_THRESHOLD 500
429 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
430 uint8_t *exp_strategy =
s->exp_strategy[ch];
440 if (!
s->blocks[
blk-1].cpl_in_use) {
443 }
else if (!
s->blocks[
blk].cpl_in_use) {
447 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
462 while (blk < s->num_blocks) {
464 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
478 if (CONFIG_EAC3_ENCODER &&
s->eac3)
499 switch(exp_strategy) {
501 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
503 if (
exp[k+1] < exp_min)
505 exp[
i-cpl] = exp_min;
510 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
512 if (
exp[k+1] < exp_min)
514 if (
exp[k+2] < exp_min)
516 if (
exp[k+3] < exp_min)
518 exp[
i-cpl] = exp_min;
525 if (!cpl &&
exp[0] > 15)
530 for (
i = 1;
i <= nb_groups;
i++)
540 switch (exp_strategy) {
542 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
549 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
566 int blk, blk1, ch, cpl;
568 int nb_coefs, num_reuse_blocks;
570 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
571 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
572 exp_strategy =
s->exp_strategy[ch];
576 while (blk < s->num_blocks) {
578 if (cpl && !
block->cpl_in_use) {
583 nb_coefs =
block->end_freq[ch] -
s->start_freq[ch];
588 s->exp_ref_block[ch][
blk] =
blk;
589 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
590 s->exp_ref_block[ch][blk1] =
blk;
593 num_reuse_blocks = blk1 -
blk - 1;
596 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
617 int nb_groups, bit_count;
623 int exp_strategy =
s->exp_strategy[ch][
blk];
625 int nb_coefs =
block->end_freq[ch] -
s->start_freq[ch];
631 bit_count += 4 + (nb_groups * 7);
649 int group_size, nb_groups;
651 int delta0, delta1, delta2;
657 int exp_strategy =
s->exp_strategy[ch][
blk];
661 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
663 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
667 block->grouped_exp[ch][0] = exp1;
670 for (
i = 1;
i <= nb_groups;
i++) {
675 delta0 = exp1 - exp0 + 2;
681 delta1 = exp1 - exp0 + 2;
687 delta2 = exp1 - exp0 + 2;
690 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
722 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
741 if (
s->num_blocks != 0x6)
745 if (
s->num_blocks == 6)
749 if (
s->use_frame_exp_strategy)
750 frame_bits += 5 *
s->fbw_channels;
752 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
754 frame_bits +=
s->num_blocks;
756 if (
s->num_blks_code != 0x3)
759 frame_bits +=
s->fbw_channels * 5;
763 if (
s->num_blocks != 1)
767 frame_bits += frame_bits_inc[
s->channel_mode];
774 frame_bits +=
s->fbw_channels;
777 frame_bits +=
s->fbw_channels;
789 frame_bits += 2 *
s->fbw_channels;
796 frame_bits += 2 + 2 + 2 + 2 + 3;
816 frame_bits += 1 + 16;
818 s->frame_bits_fixed = frame_bits;
831 s->slow_decay_code = 2;
832 s->fast_decay_code = 1;
833 s->slow_gain_code = 1;
834 s->db_per_bit_code =
s->eac3 ? 2 : 3;
836 for (ch = 0; ch <=
s->channels; ch++)
837 s->fast_gain_code[ch] = 4;
840 s->coarse_snr_offset = 40;
850 s->bit_alloc.cpl_fast_leak = 0;
851 s->bit_alloc.cpl_slow_leak = 0;
877 frame_bits +=
s->lfe_on;
878 frame_bits += 1 + 1 + 2;
884 frame_bits += 3 + 1 + 1;
891 frame_bits += 5 + 2 + 1;
900 if (
block->new_cpl_strategy)
906 if (
s->use_frame_exp_strategy) {
907 frame_bits += 5 *
s->cpl_on;
910 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
916 if (
s->bitstream_id == 6) {
931 if (
block->new_cpl_strategy) {
934 if (
block->cpl_in_use) {
938 frame_bits +=
s->fbw_channels;
945 frame_bits +=
s->num_cpl_subbands - 1;
950 if (
block->cpl_in_use) {
951 for (ch = 1; ch <=
s->fbw_channels; ch++) {
952 if (
block->channel_in_cpl[ch]) {
953 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
955 if (
block->new_cpl_coords[ch]) {
957 frame_bits += (4 + 4) *
s->num_cpl_bands;
965 if (!
s->eac3 ||
blk > 0)
967 if (
s->blocks[
blk].new_rematrixing_strategy)
968 frame_bits +=
block->num_rematrixing_bands;
972 for (ch = 1; ch <=
s->fbw_channels; ch++) {
974 if (!
block->channel_in_cpl[ch])
981 if (!
s->eac3 &&
block->cpl_in_use)
987 if (
block->new_snr_offsets)
988 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
992 if (
block->cpl_in_use) {
993 if (!
s->eac3 ||
block->new_cpl_leak != 2)
995 if (
block->new_cpl_leak)
1000 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1021 block->band_psd[ch]);
1023 s->start_freq[ch],
block->end_freq[ch],
1025 ch ==
s->lfe_channel,
1043 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1046 ref_bap =
s->bap_buffer;
1047 for (ch = 0; ch <=
s->channels; ch++) {
1068 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1069 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1070 mant_cnt[
blk][4] = 1;
1095 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1096 s->ref_bap[ch][
blk] + start,
1107 int ch, max_end_freq;
1112 max_end_freq =
s->bandwidth_code * 3 + 73;
1113 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1117 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1135 snr_offset = (snr_offset - 240) * 4;
1147 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1148 s->start_freq[ch],
block->end_freq[ch],
1149 snr_offset,
s->bit_alloc.floor,
1166 int snr_offset, snr_incr;
1168 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1172 snr_offset =
s->coarse_snr_offset << 4;
1176 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1181 while (snr_offset >= 0 &&
1189 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1190 while (snr_offset + snr_incr <= 1023 &&
1191 bit_alloc(
s, snr_offset + snr_incr) <= bits_left) {
1192 snr_offset += snr_incr;
1199 s->coarse_snr_offset = snr_offset >> 4;
1200 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1201 s->fine_snr_offset[ch] = snr_offset & 0xF;
1235 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1253 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1254 m = (1 << (qbits-1));
1275 int16_t *qmant,
int start_freq,
1280 for (
i = start_freq;
i < end_freq;
i++) {
1281 int c = fixed_coef[
i];
1288 switch (
s->mant1_cnt) {
1290 s->qmant1_ptr = &qmant[
i];
1295 *
s->qmant1_ptr += 3 * v;
1300 *
s->qmant1_ptr += v;
1308 switch (
s->mant2_cnt) {
1310 s->qmant2_ptr = &qmant[
i];
1315 *
s->qmant2_ptr += 5 * v;
1320 *
s->qmant2_ptr += v;
1331 switch (
s->mant4_cnt) {
1333 s->qmant4_ptr = &qmant[
i];
1338 *
s->qmant4_ptr += v;
1369 int blk, ch, ch0=0, got_cpl;
1375 got_cpl = !
block->cpl_in_use;
1376 for (ch = 1; ch <=
s->channels; ch++) {
1377 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1383 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1385 s->start_freq[ch],
block->end_freq[ch]);
1403 put_bits(&
s->pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1409 if (
s->channel_mode & 0x04)
1424 if (
s->bitstream_id == 6) {
1429 put_bits(&
s->pb, 3,
s->ltrt_center_mix_level);
1430 put_bits(&
s->pb, 3,
s->ltrt_surround_mix_level);
1431 put_bits(&
s->pb, 3,
s->loro_center_mix_level);
1432 put_bits(&
s->pb, 3,
s->loro_surround_mix_level);
1454 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1459 for (ch = 0; ch <
s->fbw_channels; ch++)
1465 for (ch = 0; ch <
s->fbw_channels; ch++)
1479 if (
block->new_cpl_strategy) {
1482 if (
block->cpl_in_use) {
1483 int start_sub, end_sub;
1487 for (ch = 1; ch <=
s->fbw_channels; ch++)
1492 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1493 end_sub = (
s->cpl_end_freq - 37) / 12;
1500 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1507 if (
block->cpl_in_use) {
1508 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1509 if (
block->channel_in_cpl[ch]) {
1510 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1512 if (
block->new_cpl_coords[ch]) {
1514 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1525 if (!
s->eac3 ||
blk > 0)
1527 if (
block->new_rematrixing_strategy) {
1529 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1536 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1543 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1551 int cpl = (ch ==
CPL_CH);
1561 for (
i = 1;
i <= nb_groups;
i++)
1565 if (ch !=
s->lfe_channel && !cpl)
1585 if (
block->new_snr_offsets) {
1597 if (
block->cpl_in_use) {
1598 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1600 if (
block->new_cpl_leak) {
1601 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_fast_leak);
1602 put_bits(&
s->pb, 3,
s->bit_alloc.cpl_slow_leak);
1612 got_cpl = !
block->cpl_in_use;
1613 for (ch = 1; ch <=
s->channels; ch++) {
1616 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1621 for (
i =
s->start_freq[ch]; i < block->end_freq[ch];
i++) {
1623 b =
s->ref_bap[ch][
blk][
i];
1626 case 1:
if (q != 128)
put_bits (&
s->pb, 5, q);
break;
1627 case 2:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1629 case 4:
if (q != 128)
put_bits (&
s->pb, 7, q);
break;
1642 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1645 static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1662 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1682 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1685 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1698 crc2_partial =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 5);
1703 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1708 crc2_partial =
av_crc(crc_ctx, 0,
frame + frame_size_58,
1709 s->frame_size - frame_size_58 - 3);
1711 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1713 if (crc2 == 0x770B) {
1714 frame[
s->frame_size - 3] ^= 0x1;
1715 crc2 =
av_crc(crc_ctx, crc2_partial,
frame +
s->frame_size - 3, 1);
1734 s->output_frame_header(
s);
1769 *got_packet_ptr = 1;
1780 switch (
s->bitstream_id) {
1781 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1782 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1783 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1784 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1785 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1786 default:
snprintf(strbuf, 32,
"ERROR");
1788 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", strbuf,
s->bitstream_id);
1791 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
1792 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
1793 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
1794 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
1796 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
1798 ff_dlog(avctx,
"per_frame_metadata: %s\n",
1802 s->center_mix_level);
1804 ff_dlog(avctx,
"center_mixlev: {not written}\n");
1805 if (
s->has_surround)
1807 s->surround_mix_level);
1809 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
1818 ff_dlog(avctx,
"room_type: %s\n", strbuf);
1820 ff_dlog(avctx,
"mixing_level: {not written}\n");
1821 ff_dlog(avctx,
"room_type: {not written}\n");
1832 ff_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1834 ff_dlog(avctx,
"dsur_mode: {not written}\n");
1838 if (
s->bitstream_id == 6) {
1846 ff_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1847 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1849 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1851 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1853 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1856 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1865 ff_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1872 ff_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1879 ff_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1881 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1888 #define FLT_OPTION_THRESHOLD 0.01
1894 for (
i = 0;
i < v_list_size;
i++) {
1899 if (
i == v_list_size)
1907 float *opt_param,
const float *
list,
1908 int list_size,
int default_value,
int min_value,
1912 if (mixlev < min_value) {
1913 mixlev = default_value;
1914 if (*opt_param >= 0.0) {
1916 "default value: %0.3f\n", opt_name,
list[mixlev]);
1919 *opt_param =
list[mixlev];
1920 *ctx_param = mixlev;
1946 if (
s->has_center &&
1951 if (
s->has_surround &&
1989 if (
s->has_center) {
1992 &
s->center_mix_level);
1994 if (
s->has_surround) {
1997 &
s->surround_mix_level);
2006 if (!
s->eac3 ||
s->has_center) {
2011 &
s->ltrt_center_mix_level);
2016 &
s->loro_center_mix_level);
2018 if (!
s->eac3 ||
s->has_surround) {
2023 &
s->ltrt_surround_mix_level);
2028 &
s->loro_surround_mix_level);
2040 "specified number of channels\n");
2077 "room_type is set\n");
2082 "80dB and 111dB\n");
2092 if (
s->bitstream_id > 8 &&
s->bitstream_id < 11) {
2093 if (!
s->warned_alternate_bitstream) {
2095 "not compatible with reduced samplerates. writing of "
2096 "extended bitstream information will be disabled.\n");
2097 s->warned_alternate_bitstream = 1;
2100 s->bitstream_id = 6;
2120 if (
s->planar_samples)
2121 for (ch = 0; ch <
s->channels; ch++)
2161 uint64_t *channel_layout)
2167 if (*channel_layout > 0x7FF)
2169 ch_layout = *channel_layout;
2176 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2180 switch (ch_layout) {
2194 s->has_surround =
s->channel_mode & 0x04;
2197 *channel_layout = ch_layout;
2213 "encoder will guess the layout, but it "
2214 "might be incorrect.\n");
2226 max_sr =
s->eac3 ? 2 : 8;
2227 for (
i = 0;
i <= max_sr;
i++) {
2236 s->bit_alloc.sr_shift =
i / 3;
2237 s->bit_alloc.sr_code =
i % 3;
2238 s->bitstream_id =
s->eac3 ? 16 : 8 +
s->bit_alloc.sr_shift;
2242 switch (
s->fbw_channels) {
2243 case 1: avctx->
bit_rate = 96000;
break;
2244 case 2: avctx->
bit_rate = 192000;
break;
2245 case 3: avctx->
bit_rate = 320000;
break;
2246 case 4: avctx->
bit_rate = 384000;
break;
2247 case 5: avctx->
bit_rate = 448000;
break;
2253 int max_br, min_br, wpf, min_br_code;
2254 int num_blks_code, num_blocks, frame_samples;
2255 long long min_br_dist;
2261 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2262 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2264 max_br = 2048 *
s->sample_rate / frame_samples * 16;
2265 min_br = ((
s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2271 "for this sample rate\n", min_br, max_br);
2274 s->num_blks_code = num_blks_code;
2275 s->num_blocks = num_blocks;
2278 wpf = (avctx->
bit_rate / 16) * frame_samples /
s->sample_rate;
2285 min_br_dist = INT64_MAX;
2286 for (
i = 0;
i < 19;
i++) {
2288 if (br_dist < min_br_dist) {
2289 min_br_dist = br_dist;
2295 s->frame_size_code = min_br_code << 1;
2298 s->frame_size_min = 2 * wpf;
2300 int best_br = 0, best_code = 0;
2301 long long best_diff = INT64_MAX;
2302 for (
i = 0;
i < 19;
i++) {
2305 if (
diff < best_diff) {
2314 s->frame_size_code = best_code << 1;
2316 s->num_blks_code = 0x3;
2320 s->frame_size =
s->frame_size_min;
2328 if (
s->cutoff > (
s->sample_rate >> 1))
2329 s->cutoff =
s->sample_rate >> 1;
2335 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2338 s->cpl_enabled =
s->options.channel_coupling &&
2358 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2361 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2365 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2366 s->start_freq[ch] = 0;
2368 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2372 s->start_freq[
s->lfe_channel] = 0;
2374 s->blocks[
blk].end_freq[ch] = 7;
2378 if (
s->cpl_enabled) {
2380 cpl_start =
s->options.cpl_start;
2383 if (cpl_start < 0) {
2391 if (
s->cpl_enabled) {
2392 int i, cpl_start_band, cpl_end_band;
2393 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2395 cpl_end_band =
s->bandwidth_code / 4 + 3;
2396 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2398 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2400 s->num_cpl_bands = 1;
2401 *cpl_band_sizes = 12;
2402 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2404 *cpl_band_sizes += 12;
2408 *cpl_band_sizes = 12;
2412 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2413 s->cpl_end_freq = cpl_end_band * 12 + 37;
2415 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2424 int channel_blocks =
channels *
s->num_blocks;
2427 if (
s->allocate_sample_buffers(
s))
2441 if (
s->cpl_enabled) {
2458 if (
s->cpl_enabled) {
2464 for (ch = 0; ch <
channels; ch++) {
2471 if (
s->cpl_enabled) {
2472 block->cpl_coord_exp[ch] = &
s->cpl_coord_exp_buffer [16 * (
blk *
channels + ch)];
2473 block->cpl_coord_mant[ch] = &
s->cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2482 if (!
s->fixed_point) {
2510 int ret, frame_size_58;
2525 s->bitstream_mode = 0x7;
2527 s->bits_written = 0;
2528 s->samples_written = 0;
2531 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2533 if (
s->bit_alloc.sr_code == 1) {
2534 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2538 if (CONFIG_EAC3_ENCODER &&
s->eac3) {
2549 ret =
s->mdct_init(
s);