42#include "config_components.h"
54#define SAMPLETYPE_SIZE(ctx) (sizeof(float) == sizeof(int32_t) ? sizeof(float) : \
55 (ctx)->fixed_point ? sizeof(int32_t) : sizeof(float))
62#define CMIXLEV_NUM_OPTIONS 3
67#define SURMIXLEV_NUM_OPTIONS 3
72#define EXTMIXLEV_NUM_OPTIONS 8
73#define extmixlev_options ff_ac3_gain_levels
78#define OFFSET(param) offsetof(AC3EncodeContext, options.param)
79#define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
131 .class_name =
"AC-3 Encoder",
198 { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
199 { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
210 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
211 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
212 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
214 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
215 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
216 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
218 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
219 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
220 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
222 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
223 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
224 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
226 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
227 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
228 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
244 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
245 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
246 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
249 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
250 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
251 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
254 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
255 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
256 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
259 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
260 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
261 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
264 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
265 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
266 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
269 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
270 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
271 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
275#define FLT_OPTION_THRESHOLD 0.01
281 for (
i = 0;
i < v_list_size;
i++) {
286 if (
i == v_list_size)
294 float *opt_param,
const float *list,
295 int list_size,
int default_value,
int min_value,
299 if (mixlev < min_value) {
300 mixlev = default_value;
301 if (*opt_param >= 0.0) {
303 "default value: %0.3f\n", opt_name, list[mixlev]);
306 *opt_param = list[mixlev];
338 if (
s->has_surround &&
379 &
s->center_mix_level);
381 if (
s->has_surround) {
384 &
s->surround_mix_level);
393 if (!
s->eac3 ||
s->has_center) {
398 &
s->ltrt_center_mix_level);
403 &
s->loro_center_mix_level);
405 if (!
s->eac3 ||
s->has_surround) {
410 &
s->ltrt_surround_mix_level);
415 &
s->loro_surround_mix_level);
427 "specified number of channels\n");
464 "room_type is set\n");
492 while (
s->bits_written >=
s->bit_rate &&
s->samples_written >=
s->sample_rate) {
493 s->bits_written -=
s->bit_rate;
494 s->samples_written -=
s->sample_rate;
496 s->frame_size =
s->frame_size_min +
497 2 * (
s->bits_written *
s->sample_rate <
s->samples_written *
s->bit_rate);
498 s->bits_written +=
s->frame_size * 8;
517 for (ch = 1; ch <=
s->fbw_channels; ch++)
518 block->channel_in_cpl[ch] =
s->cpl_on;
527 block->num_cpl_channels = 0;
528 for (ch = 1; ch <=
s->fbw_channels; ch++)
529 block->num_cpl_channels +=
block->channel_in_cpl[ch];
530 block->cpl_in_use =
block->num_cpl_channels > 1;
531 num_cpl_blocks +=
block->cpl_in_use;
532 if (!
block->cpl_in_use) {
533 block->num_cpl_channels = 0;
534 for (ch = 1; ch <=
s->fbw_channels; ch++)
535 block->channel_in_cpl[ch] = 0;
540 for (ch = 1; ch <=
s->fbw_channels; ch++) {
541 if (
block->channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
542 block->new_cpl_strategy = 1;
547 block->new_cpl_leak =
block->new_cpl_strategy;
549 if (!
blk || (
block->cpl_in_use && !got_cpl_snr)) {
550 block->new_snr_offsets = 1;
551 if (
block->cpl_in_use)
554 block->new_snr_offsets = 0;
563 for (ch = 1; ch <=
s->fbw_channels; ch++) {
564 if (
block->channel_in_cpl[ch])
567 block->end_freq[ch] =
s->bandwidth_code * 3 + 73;
585 if (!
s->rematrixing_enabled)
590 if (
block->new_rematrixing_strategy)
593 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++) {
597 for (
i = start;
i < end;
i++) {
600 block->fixed_coef[1][
i] = (lt + rt) >> 1;
601 block->fixed_coef[2][
i] = (lt - rt) >> 1;
614 int expstr,
i, grpsize;
617 grpsize = 3 << expstr;
618 for (
i = 12;
i < 256;
i++) {
634 int chan_size =
AC3_MAX_COEFS *
s->num_blocks * (
s->channels - ch + 1);
637 s->ac3dsp.extract_exponents(
block->exp[ch],
block->fixed_coef[ch], chan_size);
645#define EXP_DIFF_THRESHOLD 500
665 for (ch = !
s->cpl_on; ch <= s->fbw_channels; ch++) {
666 uint8_t *exp_strategy =
s->exp_strategy[ch];
667 uint8_t *
exp =
s->blocks[0].exp[ch];
676 if (!
s->blocks[
blk-1].cpl_in_use) {
679 }
else if (!
s->blocks[
blk].cpl_in_use) {
683 }
else if (
s->blocks[
blk].channel_in_cpl[ch] !=
s->blocks[
blk-1].channel_in_cpl[ch]) {
700 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
714 if (CONFIG_EAC3_ENCODER &&
s->eac3)
735 switch(exp_strategy) {
737 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
738 uint8_t exp_min =
exp[k];
739 if (
exp[k+1] < exp_min)
741 exp[
i-cpl] = exp_min;
746 for (
i = 1, k = 1-cpl;
i <= nb_groups;
i++) {
747 uint8_t exp_min =
exp[k];
748 if (
exp[k+1] < exp_min)
750 if (
exp[k+2] < exp_min)
752 if (
exp[k+3] < exp_min)
754 exp[
i-cpl] = exp_min;
761 if (!cpl &&
exp[0] > 15)
766 for (
i = 1;
i <= nb_groups;
i++)
776 switch (exp_strategy) {
778 for (
i = nb_groups, k = (nb_groups * 2)-cpl;
i > 0;
i--) {
779 uint8_t exp1 =
exp[
i-cpl];
785 for (
i = nb_groups, k = (nb_groups * 4)-cpl;
i > 0;
i--) {
802 int blk, blk1, ch, cpl;
803 uint8_t *
exp, *exp_strategy;
806 for (ch = !
s->cpl_on; ch <= s->
channels; ch++) {
807 exp =
s->blocks[0].exp[ch] +
s->start_freq[ch];
808 exp_strategy =
s->exp_strategy[ch];
814 if (cpl && !
block->cpl_in_use) {
824 s->exp_ref_block[ch][
blk] =
blk;
825 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
826 s->exp_ref_block[ch][blk1] =
blk;
829 num_reuse_blocks = blk1 -
blk - 1;
832 s->ac3dsp.ac3_exponent_min(
exp-
s->start_freq[ch], num_reuse_blocks,
853 int nb_groups, bit_count;
859 int exp_strategy =
s->exp_strategy[ch][
blk];
867 bit_count += 4 + (nb_groups * 7);
885 int group_size, nb_groups;
887 int delta0, delta1, delta2;
893 int exp_strategy =
s->exp_strategy[ch][
blk];
897 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
899 p =
block->exp[ch] +
s->start_freq[ch] - cpl;
903 block->grouped_exp[ch][0] = exp1;
906 for (
i = 1;
i <= nb_groups;
i++) {
911 delta0 = exp1 - exp0 + 2;
917 delta1 = exp1 - exp0 + 2;
923 delta2 = exp1 - exp0 + 2;
926 block->grouped_exp[ch][
i] = ((delta0 * 5 + delta1) * 5) + delta2;
956 static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
975 if (
s->num_blocks != 0x6)
979 if (
s->num_blocks == 6)
983 if (
s->use_frame_exp_strategy)
984 frame_bits += 5 *
s->fbw_channels;
986 frame_bits +=
s->num_blocks * 2 *
s->fbw_channels;
988 frame_bits +=
s->num_blocks;
990 if (
s->num_blks_code != 0x3)
993 frame_bits +=
s->fbw_channels * 5;
997 if (
s->num_blocks != 1)
1001 frame_bits += frame_bits_inc[
s->channel_mode];
1008 frame_bits +=
s->fbw_channels;
1011 frame_bits +=
s->fbw_channels;
1027 frame_bits += 2 *
s->fbw_channels;
1034 frame_bits += 2 + 2 + 2 + 2 + 3;
1053 frame_bits += 1 + 16;
1055 s->frame_bits_fixed = frame_bits;
1068 s->slow_decay_code = 2;
1069 s->fast_decay_code = 1;
1070 s->slow_gain_code = 1;
1071 s->db_per_bit_code =
s->eac3 ? 2 : 3;
1073 for (ch = 0; ch <=
s->channels; ch++)
1074 s->fast_gain_code[ch] = 4;
1077 s->coarse_snr_offset = 40;
1087 s->bit_alloc.cpl_fast_leak = 0;
1088 s->bit_alloc.cpl_slow_leak = 0;
1112 if (
s->has_surround)
1114 frame_bits +=
s->lfe_on;
1115 frame_bits += 1 + 1 + 2;
1121 frame_bits += 3 + 1 + 1;
1123 frame_bits += 2 + 2;
1128 frame_bits += 5 + 2 + 1;
1137 if (
block->new_cpl_strategy)
1143 if (
s->use_frame_exp_strategy) {
1147 frame_bits += 2 *
s->blocks[
blk].cpl_in_use;
1153 if (
s->bitstream_id == 6) {
1166 if (
block->new_cpl_strategy) {
1169 if (
block->cpl_in_use) {
1173 frame_bits +=
s->fbw_channels;
1176 frame_bits += 4 + 4;
1180 frame_bits +=
s->num_cpl_subbands - 1;
1185 if (
block->cpl_in_use) {
1186 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1187 if (
block->channel_in_cpl[ch]) {
1188 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1190 if (
block->new_cpl_coords[ch]) {
1192 frame_bits += (4 + 4) *
s->num_cpl_bands;
1200 if (!
s->eac3 ||
blk > 0)
1202 if (
s->blocks[
blk].new_rematrixing_strategy)
1203 frame_bits +=
block->num_rematrixing_bands;
1207 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1209 if (!
block->channel_in_cpl[ch])
1216 if (!
s->eac3 &&
block->cpl_in_use)
1221 if (
block->new_snr_offsets)
1222 frame_bits += 6 + (
s->channels +
block->cpl_in_use) * (4 + 3);
1226 if (
block->cpl_in_use) {
1227 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1229 if (
block->new_cpl_leak)
1230 frame_bits += 3 + 3;
1234 s->frame_bits =
s->frame_bits_fixed + frame_bits;
1255 block->band_psd[ch]);
1257 s->start_freq[ch],
block->end_freq[ch],
1259 ch ==
s->lfe_channel,
1277 if (
s->ref_bap[0][0] ==
s->bap_buffer &&
s->ref_bap_set)
1280 ref_bap =
s->bap_buffer;
1281 for (ch = 0; ch <=
s->channels; ch++) {
1302 memset(mant_cnt[
blk], 0,
sizeof(mant_cnt[
blk]));
1303 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1304 mant_cnt[
blk][4] = 1;
1329 s->ac3dsp.update_bap_counts(mant_cnt[
blk],
1330 s->ref_bap[ch][
blk] + start,
1341 int ch, max_end_freq;
1346 max_end_freq =
s->bandwidth_code * 3 + 73;
1347 for (ch = !
s->cpl_enabled; ch <= s->
channels; ch++)
1351 return s->ac3dsp.compute_mantissa_size(mant_cnt);
1369 snr_offset = (snr_offset - 240) * 4;
1381 s->ac3dsp.bit_alloc_calc_bap(
block->mask[ch],
block->psd[ch],
1382 s->start_freq[ch],
block->end_freq[ch],
1383 snr_offset,
s->bit_alloc.floor,
1400 int snr_offset, snr_incr;
1402 bits_left = 8 *
s->frame_size - (
s->frame_bits +
s->exponent_bits);
1406 snr_offset =
s->coarse_snr_offset << 4;
1410 if ((snr_offset |
s->fine_snr_offset[1]) == 1023) {
1415 while (snr_offset >= 0 &&
1422 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1423 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1424 while (snr_offset + snr_incr <= 1023 &&
1426 snr_offset += snr_incr;
1427 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1430 FFSWAP(uint8_t *,
s->bap_buffer,
s->bap1_buffer);
1433 s->coarse_snr_offset = snr_offset >> 4;
1434 for (ch = !
s->cpl_on; ch <= s->
channels; ch++)
1435 s->fine_snr_offset[ch] = snr_offset & 0xF;
1469 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1487 c = (((
c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1488 m = (1 << (qbits-1));
1508 uint8_t *
exp, uint8_t *bap,
1509 int16_t *qmant,
int start_freq,
1514 for (
i = start_freq;
i < end_freq;
i++) {
1515 int c = fixed_coef[
i];
1523 switch (
s->mant1_cnt) {
1525 s->qmant1_ptr = &qmant[
i];
1530 *
s->qmant1_ptr += 3 * v;
1535 *
s->qmant1_ptr += v;
1543 switch (
s->mant2_cnt) {
1545 s->qmant2_ptr = &qmant[
i];
1550 *
s->qmant2_ptr += 5 * v;
1555 *
s->qmant2_ptr += v;
1566 switch (
s->mant4_cnt) {
1568 s->qmant4_ptr = &qmant[
i];
1573 *
s->qmant4_ptr += v;
1604 int blk, ch, ch0=0, got_cpl;
1610 got_cpl = !
block->cpl_in_use;
1611 for (ch = 1; ch <=
s->channels; ch++) {
1612 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1618 s->blocks[
s->exp_ref_block[ch][
blk]].exp[ch],
1620 s->start_freq[ch],
block->end_freq[ch]);
1640 put_bits(pb, 6,
s->frame_size_code + (
s->frame_size -
s->frame_size_min) / 2);
1646 if (
s->channel_mode & 0x04)
1661 if (
s->bitstream_id == 6) {
1666 put_bits(pb, 3,
s->ltrt_center_mix_level);
1667 put_bits(pb, 3,
s->ltrt_surround_mix_level);
1668 put_bits(pb, 3,
s->loro_center_mix_level);
1669 put_bits(pb, 3,
s->loro_surround_mix_level);
1691 int ch,
i, baie, bnd, got_cpl,
av_uninit(ch0);
1696 for (ch = 0; ch <
s->fbw_channels; ch++)
1702 for (ch = 0; ch <
s->fbw_channels; ch++)
1716 if (
block->new_cpl_strategy) {
1719 if (
block->cpl_in_use) {
1720 int start_sub, end_sub;
1724 for (ch = 1; ch <=
s->fbw_channels; ch++)
1729 start_sub = (
s->start_freq[
CPL_CH] - 37) / 12;
1730 end_sub = (
s->cpl_end_freq - 37) / 12;
1737 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1744 if (
block->cpl_in_use) {
1745 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1746 if (
block->channel_in_cpl[ch]) {
1747 if (!
s->eac3 ||
block->new_cpl_coords[ch] != 2)
1749 if (
block->new_cpl_coords[ch]) {
1751 for (bnd = 0; bnd <
s->num_cpl_bands; bnd++) {
1762 if (!
s->eac3 ||
blk > 0)
1764 if (
block->new_rematrixing_strategy) {
1766 for (bnd = 0; bnd <
block->num_rematrixing_bands; bnd++)
1773 for (ch = !
block->cpl_in_use; ch <= s->fbw_channels; ch++)
1780 for (ch = 1; ch <=
s->fbw_channels; ch++) {
1788 int cpl = (ch ==
CPL_CH);
1798 for (
i = 1;
i <= nb_groups;
i++)
1802 if (ch !=
s->lfe_channel && !cpl)
1822 if (
block->new_snr_offsets) {
1825 put_bits(pb, 4,
s->fine_snr_offset[ch]);
1834 if (
block->cpl_in_use) {
1835 if (!
s->eac3 ||
block->new_cpl_leak != 2)
1837 if (
block->new_cpl_leak) {
1838 put_bits(pb, 3,
s->bit_alloc.cpl_fast_leak);
1839 put_bits(pb, 3,
s->bit_alloc.cpl_slow_leak);
1849 got_cpl = !
block->cpl_in_use;
1850 for (ch = 1; ch <=
s->channels; ch++) {
1853 if (!got_cpl && ch > 1 &&
block->channel_in_cpl[ch-1]) {
1860 b =
s->ref_bap[ch][
blk][
i];
1863 case 1:
if (q != 128)
put_bits (pb, 5, q);
break;
1864 case 2:
if (q != 128)
put_bits (pb, 7, q);
break;
1866 case 4:
if (q != 128)
put_bits (pb, 7, q);
break;
1879#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1882static unsigned int mul_poly(
unsigned int a,
unsigned int b,
unsigned int poly)
1899static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1919 int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1922 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
1935 crc2 =
av_crc(crc_ctx, 0,
frame + 2,
s->frame_size - 4);
1940 crc_inv =
s->crc_inv[
s->frame_size >
s->frame_size_min];
1946 s->frame_size - frame_size_58 - 2);
1950 if (crc2 == 0x0B77) {
1953 frame[
s->frame_size - 3] ^= 0x1;
1973 s->output_frame_header(
s, &pb);
1993 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
1997 if (
s->options.allow_per_frame_metadata) {
2003 if (
s->bit_alloc.sr_code == 1 ||
s->eac3)
2031 *got_packet_ptr = 1;
2043 switch (
s->bitstream_id) {
2044 case 6: msg =
"AC-3 (alt syntax)";
break;
2045 case 8: msg =
"AC-3 (standard)";
break;
2046 case 16: msg =
"E-AC-3 (enhanced)";
break;
2047 default: msg =
"ERROR";
2049 ff_dlog(avctx,
"bitstream_id: %s (%d)\n", msg,
s->bitstream_id);
2052 ff_dlog(avctx,
"channel_layout: %s\n", strbuf);
2053 ff_dlog(avctx,
"sample_rate: %d\n",
s->sample_rate);
2054 ff_dlog(avctx,
"bit_rate: %d\n",
s->bit_rate);
2055 ff_dlog(avctx,
"blocks/frame: %d (code=%d)\n",
s->num_blocks,
s->num_blks_code);
2057 ff_dlog(avctx,
"cutoff: %d\n",
s->cutoff);
2059 ff_dlog(avctx,
"per_frame_metadata: %s\n",
2063 s->center_mix_level);
2065 ff_dlog(avctx,
"center_mixlev: {not written}\n");
2066 if (
s->has_surround)
2068 s->surround_mix_level);
2070 ff_dlog(avctx,
"surround_mixlev: {not written}\n");
2081 ff_dlog(avctx,
"room_type: %s\n", msg);
2083 ff_dlog(avctx,
"mixing_level: {not written}\n");
2084 ff_dlog(avctx,
"room_type: {not written}\n");
2097 ff_dlog(avctx,
"dsur_mode: %s\n", msg);
2099 ff_dlog(avctx,
"dsur_mode: {not written}\n");
2103 if (
s->bitstream_id == 6) {
2113 ff_dlog(avctx,
"dmix_mode: %s\n", msg);
2114 ff_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
2116 ff_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
2118 ff_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
2120 ff_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
2123 ff_dlog(avctx,
"extended bitstream info 1: {not written}\n");
2134 ff_dlog(avctx,
"dsurex_mode: %s\n", msg);
2143 ff_dlog(avctx,
"dheadphone_mode: %s\n", msg);
2152 ff_dlog(avctx,
"ad_conv_type: %s\n", msg);
2154 ff_dlog(avctx,
"extended bitstream info 2: {not written}\n");
2169 for (
int ch = 0; ch <
s->channels; ch++)
2204 s->lfe_channel =
s->lfe_on ?
s->fbw_channels + 1 : -1;
2218 s->has_surround =
s->channel_mode & 0x04;
2231 for (
int i = 0;;
i++) {
2233 s->bit_alloc.sr_code =
i;
2239 s->bitstream_id =
s->eac3 ? 16 : 8;
2243 switch (
s->fbw_channels) {
2244 case 1: avctx->
bit_rate = 96000;
break;
2245 case 2: avctx->
bit_rate = 192000;
break;
2246 case 3: avctx->
bit_rate = 320000;
break;
2247 case 4: avctx->
bit_rate = 384000;
break;
2248 case 5: avctx->
bit_rate = 448000;
break;
2254 int max_br, min_br, wpf, min_br_code;
2256 long long min_br_dist;
2262 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2263 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2272 "for this sample rate\n", min_br, max_br);
2275 s->num_blks_code = num_blks_code;
2276 s->num_blocks = num_blocks;
2286 min_br_dist = INT64_MAX;
2287 for (
int i = 0;
i < 19;
i++) {
2289 if (br_dist < min_br_dist) {
2290 min_br_dist = br_dist;
2296 s->frame_size_code = min_br_code << 1;
2299 s->frame_size_min = 2 * wpf;
2301 int best_br = 0, best_code = 0;
2302 long long best_diff = INT64_MAX;
2303 for (
int i = 0;
i < 19;
i++) {
2306 if (
diff < best_diff) {
2315 s->frame_size_code = best_code << 1;
2317 s->num_blks_code = 0x3;
2321 s->frame_size =
s->frame_size_min;
2329 if (
s->cutoff > (
s->sample_rate >> 1))
2330 s->cutoff =
s->sample_rate >> 1;
2336 s->rematrixing_enabled =
s->options.stereo_rematrixing &&
2339 s->cpl_enabled =
s->options.channel_coupling &&
2359 s->bandwidth_code =
av_clip((fbw_coeffs - 73) / 3, 0, 60);
2362 s->bandwidth_code =
ac3_bandwidth_tab[
s->fbw_channels-1][
s->bit_alloc.sr_code][
s->frame_size_code/2];
2366 for (ch = 1; ch <=
s->fbw_channels; ch++) {
2367 s->start_freq[ch] = 0;
2369 s->blocks[
blk].end_freq[ch] =
s->bandwidth_code * 3 + 73;
2373 s->start_freq[
s->lfe_channel] = 0;
2375 s->blocks[
blk].end_freq[ch] = 7;
2379 if (
s->cpl_enabled) {
2381 cpl_start =
s->options.cpl_start;
2384 if (cpl_start < 0) {
2392 if (
s->cpl_enabled) {
2393 int i, cpl_start_band, cpl_end_band;
2394 uint8_t *cpl_band_sizes =
s->cpl_band_sizes;
2396 cpl_end_band =
s->bandwidth_code / 4 + 3;
2397 cpl_start_band =
av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2399 s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2401 s->num_cpl_bands = 1;
2402 *cpl_band_sizes = 12;
2403 for (
i = cpl_start_band + 1;
i < cpl_end_band;
i++) {
2405 *cpl_band_sizes += 12;
2409 *cpl_band_sizes = 12;
2413 s->start_freq[
CPL_CH] = cpl_start_band * 12 + 37;
2414 s->cpl_end_freq = cpl_end_band * 12 + 37;
2416 s->blocks[
blk].end_freq[
CPL_CH] =
s->cpl_end_freq;
2425 int channel_blocks =
channels *
s->num_blocks;
2427 uint8_t *cpl_coord_mant_buffer;
2430 for (
int ch = 0; ch <
s->channels; ch++) {
2432 if (!
s->planar_samples[ch])
2437 s->avctx->sample_fmt, 0);
2452 if (!
s->fixed_point) {
2456 if (
s->cpl_enabled) {
2459 cpl_coord_mant_buffer =
s->cpl_coord_buffer + 16 * channel_blocks;
2464 for (ch = 0; ch <
channels; ch++) {
2471 if (
s->cpl_enabled) {
2473 block->cpl_coord_mant[ch] = &cpl_coord_mant_buffer[16 * (
blk *
channels + ch)];
2494 int ret, frame_size_58;
2507 s->bitstream_mode = 0x7;
2509 s->bits_written = 0;
2510 s->samples_written = 0;
2513 frame_size_58 = ((
s->frame_size >> 2) + (
s->frame_size >> 4)) << 1;
2515 if (
s->bit_alloc.sr_code == 1) {
2516 frame_size_58 = (((
s->frame_size+2) >> 2) + ((
s->frame_size+2) >> 4)) << 1;
2520 if (!
s->output_frame_header)
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd, int start, int end, int fast_gain, int is_lfe, int dba_mode, int dba_nsegs, uint8_t *dba_offsets, uint8_t *dba_lengths, uint8_t *dba_values, int16_t *mask)
Calculate the masking curve.
void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd, int16_t *band_psd)
Calculate the log power-spectral density of the input signal.
Common code between the AC-3 encoder and decoder.
#define CPL_CH
coupling channel index
#define LEVEL_MINUS_4POINT5DB
static const int8_t ac3_coupling_start_tab[6][3][19]
LUT to select the coupling start band based on the bit rate, sample rate, and number of full-bandwidt...
static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
Initialize mantissa counts.
static void ac3_group_exponents(AC3EncodeContext *s)
Group exponents.
static int validate_float_option(float v, const float *v_list, int v_list_size)
av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
static void compute_exp_strategy(AC3EncodeContext *s)
static void extract_exponents(AC3EncodeContext *s)
static void output_audio_block(AC3EncodeContext *s, PutBitContext *pb, int blk)
#define CRC16_POLY
CRC-16 Polynomial.
#define FLT_OPTION_THRESHOLD
static const uint8_t exp_strategy_reuse_tab[4][6]
Table used to select exponent strategy based on exponent reuse block interval.
static void dprint_options(AC3EncodeContext *s)
static av_cold void bit_alloc_init(AC3EncodeContext *s)
static void ac3_process_exponents(AC3EncodeContext *s)
Calculate final exponents from the supplied MDCT coefficients and exponent shift.
static av_cold int allocate_buffers(AC3EncodeContext *s)
#define SURMIXLEV_NUM_OPTIONS
static void ac3_adjust_frame_size(AC3EncodeContext *s)
Adjust the frame size to make the average bit rate match the target bit rate.
static void output_frame_end(AC3EncodeContext *s, PutBitContext *pb)
static uint8_t exponent_group_tab[2][3][256]
LUT for number of exponent groups.
static int ac3_validate_metadata(AC3EncodeContext *s)
Validate metadata options as set by AVOption system.
#define CMIXLEV_NUM_OPTIONS
av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
#define SAMPLETYPE_SIZE(ctx)
static const uint8_t ac3_enc_channel_map[8][2][6]
Table to remap channels from SMPTE order to AC-3 order.
const AVClass ff_ac3enc_class
static av_cold void set_channel_info(AVCodecContext *avctx)
static const float cmixlev_options[CMIXLEV_NUM_OPTIONS]
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
Set the initial coupling strategy parameters prior to coupling analysis.
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
static void reset_block_bap(AC3EncodeContext *s)
static void encode_exponents(AC3EncodeContext *s)
static void count_frame_bits(AC3EncodeContext *s)
const AVOption ff_ac3_enc_options[]
static int asym_quant(int c, int e, int qbits)
Asymmetric quantization on 2^qbits levels.
static int sym_quant(int c, int e, int levels)
Symmetric quantization on 'levels' levels.
static av_cold void set_bandwidth(AC3EncodeContext *s)
static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy, int cpl)
Update the exponents so that they are the ones the decoder will decode.
static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
Write the frame to the output bitstream.
static void ac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb)
static void bit_alloc_masking(AC3EncodeContext *s)
static av_cold int validate_options(AC3EncodeContext *s)
static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef, uint8_t *exp, uint8_t *bap, int16_t *qmant, int start_freq, int end_freq)
Quantize a set of mantissas for a single channel in a single block.
static void count_frame_bits_fixed(AC3EncodeContext *s)
#define EXP_DIFF_THRESHOLD
Exponent Difference Threshold.
static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS]
static void ac3_quantize_mantissas(AC3EncodeContext *s)
Quantize mantissas using coefficients, exponents, and bit allocation pointers.
static const uint8_t ac3_bandwidth_tab[5][3][19]
LUT to select the bandwidth code based on the bit rate, sample rate, and number of full-bandwidth cha...
static void count_mantissa_bits_update_ch(AC3EncodeContext *s, int ch, uint16_t mant_cnt[AC3_MAX_BLOCKS][16], int start, int end)
Update mantissa bit counts for all blocks in 1 channel in a given bandwidth range.
const AVChannelLayout ff_ac3_ch_layouts[19]
List of supported channel layouts.
static av_cold void exponent_init(void)
static int count_mantissa_bits(AC3EncodeContext *s)
static int cbr_bit_allocation(AC3EncodeContext *s)
const FFCodecDefault ff_ac3_enc_defaults[]
static int count_exponent_bits(AC3EncodeContext *s)
static int ac3_compute_bit_allocation(AC3EncodeContext *s)
#define EXTMIXLEV_NUM_OPTIONS
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
static void validate_mix_level(void *log_ctx, const char *opt_name, float *opt_param, const float *list, int list_size, int default_value, int min_value, int *ctx_param)
int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
#define extmixlev_options
static void ac3_apply_rematrixing(AC3EncodeContext *s)
Apply stereo rematrixing to coefficients based on rematrixing flags.
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
AC-3 encoder & E-AC-3 encoder common header.
#define AC3ENC_OPT_ADCONV_STANDARD
#define AC3ENC_OPT_DOWNMIX_LORO
#define AC3ENC_OPT_DOWNMIX_LTRT
#define AC3ENC_OPT_DOWNMIX_DPLII
#define AC3ENC_OPT_LARGE_ROOM
#define AC3ENC_OPT_ADCONV_HDCD
#define AC3ENC_OPT_SMALL_ROOM
#define AC3ENC_OPT_MODE_OFF
#define AC3ENC_OPT_DSUREX_DPLIIZ
#define AC3ENC_OPT_MODE_ON
#define AC3ENC_OPT_NOT_INDICATED
const uint8_t ff_ac3_rematrix_band_tab[5]
Table of bin locations for rematrixing bands reference: Section 7.5.2 Rematrixing : Frequency Band De...
const uint16_t ff_ac3_bitrate_tab[19]
const uint8_t ff_ac3_fast_decay_tab[4]
const int ff_ac3_sample_rate_tab[]
const uint16_t ff_ac3_fast_gain_tab[8]
const uint16_t ff_ac3_slow_gain_tab[4]
const uint8_t ff_eac3_default_cpl_band_struct[18]
Table E2.16 Default Coupling Banding Structure.
const uint16_t ff_ac3_frame_size_tab[38][3]
Possible frame sizes.
const uint8_t ff_ac3_slow_decay_tab[4]
const int16_t ff_ac3_floor_tab[8]
const uint16_t ff_ac3_db_per_bit_tab[4]
const uint8_t ff_ac3_bap_tab[64]
#define COMMON_CHANNEL_MAP
static int nb_coefs(int length, int level, uint64_t sn)
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Libavcodec external API header.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
Public header for CRC hash function implementation.
@ AV_AUDIO_SERVICE_TYPE_VOICE_OVER
@ AV_AUDIO_SERVICE_TYPE_EMERGENCY
@ AV_AUDIO_SERVICE_TYPE_MAIN
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_COMMENTARY
void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
Determine frame exponent strategy use and indices.
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LOW_FREQUENCY
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_2_1
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
#define AV_CHANNEL_LAYOUT_QUAD
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly.
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
av_cold void ff_ac3dsp_init(AC3DSPContext *c)
av_cold void ff_audiodsp_init(AudioDSPContext *c)
Macro definitions for various function/variable attributes.
common internal API header
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
static int ff_thread_once(char *control, void(*routine)(void))
static const uint16_t mask[17]
#define FFSWAP(type, a, b)
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Memory handling functions.
#define LOCAL_ALIGNED_16(t, v,...)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_count(PutBitContext *s)
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
Data for a single audio block.
Encoding Options used by AVOption.
float ltrt_surround_mix_level
int dolby_surround_ex_mode
float loro_surround_mix_level
int audio_production_info
int allow_per_frame_metadata
float loro_center_mix_level
int preferred_stereo_downmix
float ltrt_center_mix_level
AC-3 encoder private context.
int mant4_cnt
mantissa counts for bap=1,2,4
int16_t * qmant4_ptr
mantissa pointers for bap=1,2,4
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)