53 #define CMIXLEV_NUM_OPTIONS 3
58 #define SURMIXLEV_NUM_OPTIONS 3
63 #define EXTMIXLEV_NUM_OPTIONS 8
111 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
112 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
113 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
115 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
116 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
117 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
119 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
120 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
121 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
123 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
127 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
145 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
146 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
147 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
150 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -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 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
155 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -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 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
160 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
162 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
165 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
166 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
167 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
170 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
171 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
172 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
242 if (!blk || (block->
cpl_in_use && !got_cpl_snr)) {
290 for (i = start; i < end; i++) {
307 int expstr, i, grpsize;
310 grpsize = 3 << expstr;
311 for (i = 12; i < 256; i++) {
341 #define EXP_DIFF_THRESHOLD 500
361 for (ch = !s->
cpl_on; ch <= s->fbw_channels; ch++) {
394 while (blk < s->num_blocks) {
396 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
431 switch(exp_strategy) {
433 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
434 uint8_t exp_min = exp[k];
435 if (exp[k+1] < exp_min)
437 exp[i-cpl] = exp_min;
442 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
443 uint8_t exp_min = exp[k];
444 if (exp[k+1] < exp_min)
446 if (exp[k+2] < exp_min)
448 if (exp[k+3] < exp_min)
450 exp[i-cpl] = exp_min;
457 if (!cpl && exp[0] > 15)
462 for (i = 1; i <= nb_groups; i++)
463 exp[i] =
FFMIN(exp[i], exp[i-1] + 2);
466 exp[i] =
FFMIN(exp[i], exp[i+1] + 2);
469 exp[-1] = exp[0] & ~1;
472 switch (exp_strategy) {
474 for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
475 uint8_t exp1 = exp[i-cpl];
481 for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
482 exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
498 int blk, blk1, ch, cpl;
499 uint8_t *exp, *exp_strategy;
500 int nb_coefs, num_reuse_blocks;
502 for (ch = !s->
cpl_on; ch <= s->channels; ch++) {
508 while (blk < s->num_blocks) {
521 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
525 num_reuse_blocks = blk1 - blk - 1;
549 int nb_groups, bit_count;
554 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
563 bit_count += 4 + (nb_groups * 7);
581 int group_size, nb_groups;
583 int delta0, delta1, delta2;
588 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
593 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
602 for (i = 1; i <= nb_groups; i++) {
607 delta0 = exp1 - exp0 + 2;
613 delta1 = exp1 - exp0 + 2;
619 delta2 = exp1 - exp0 + 2;
622 block->
grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
654 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
728 frame_bits += 2 + 2 + 2 + 2 + 3;
748 frame_bits += 1 + 16;
768 for (ch = 0; ch <= s->
channels; ch++)
810 frame_bits += 1 + 1 + 2;
816 frame_bits += 3 + 1 + 1;
823 frame_bits += 5 + 2 + 1;
839 frame_bits += 5 * s->
cpl_on;
897 if (!s->
eac3 || blk > 0)
946 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
979 for (ch = 0; ch <= s->
channels; ch++) {
1000 memset(mant_cnt[blk], 0,
sizeof(mant_cnt[blk]));
1001 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1002 mant_cnt[
blk][4] = 1;
1039 int ch, max_end_freq;
1045 for (ch = !s->
cpl_enabled; ch <= s->channels; ch++)
1067 snr_offset = (snr_offset - 240) << 2;
1073 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1098 int snr_offset, snr_incr;
1113 while (snr_offset >= 0 &&
1121 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1122 while (snr_offset + snr_incr <= 1023 &&
1123 bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1124 snr_offset += snr_incr;
1132 for (ch = !s->
cpl_on; ch <= s->channels; ch++)
1167 int v = (((levels * c) >> (24 - e)) + levels) >> 1;
1185 c = (((c << e) >> (24 - qbits)) + 1) >> 1;
1186 m = (1 << (qbits-1));
1206 uint8_t *exp, uint8_t *bap,
1207 int16_t *qmant,
int start_freq,
1212 for (i = start_freq; i < end_freq; i++) {
1214 int c = fixed_coef[i];
1304 int blk, ch, ch0=0, got_cpl;
1311 for (ch = 1; ch <= s->
channels; ch++) {
1389 int ch, i, baie, bnd, got_cpl;
1419 int start_sub, end_sub;
1436 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1461 if (!s->
eac3 || blk > 0)
1472 for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
1485 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1487 int cpl = (ch ==
CPL_CH);
1497 for (i = 1; i <= nb_groups; i++)
1523 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1549 for (ch = 1; ch <= s->
channels; ch++) {
1557 for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
1558 q = block->
qmant[ch][i];
1562 case 1:
if (q != 128)
put_bits (&s->
pb, 5, q);
break;
1563 case 2:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1565 case 4:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1578 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1598 static unsigned int pow_poly(
unsigned int a,
unsigned int n,
unsigned int poly)
1618 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1644 crc2_partial =
av_crc(crc_ctx, 0, frame + frame_size_58,
1649 if (crc2 == 0x770B) {
1687 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1688 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1689 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1690 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1691 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1692 default: snprintf(strbuf, 32,
"ERROR");
1697 av_dlog(avctx,
"channel_layout: %s\n", strbuf);
1704 av_dlog(avctx,
"per_frame_metadata: %s\n",
1710 av_dlog(avctx,
"center_mixlev: {not written}\n");
1715 av_dlog(avctx,
"surround_mixlev: {not written}\n");
1722 default: snprintf(strbuf, 32,
"ERROR (%d)", opt->
room_type);
1724 av_dlog(avctx,
"room_type: %s\n", strbuf);
1726 av_dlog(avctx,
"mixing_level: {not written}\n");
1727 av_dlog(avctx,
"room_type: {not written}\n");
1738 av_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1740 av_dlog(avctx,
"dsur_mode: {not written}\n");
1752 av_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1753 av_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1755 av_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1757 av_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1759 av_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1762 av_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1771 av_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1778 av_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1785 av_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1787 av_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1794 #define FLT_OPTION_THRESHOLD 0.01
1800 for (i = 0; i < v_list_size; i++) {
1805 if (i == v_list_size)
1813 float *opt_param,
const float *list,
1814 int list_size,
int default_value,
int min_value,
1818 if (mixlev < min_value) {
1819 mixlev = default_value;
1820 if (*opt_param >= 0.0) {
1822 "default value: %0.3f\n", opt_name, list[mixlev]);
1825 *opt_param = list[mixlev];
1826 *ctx_param = mixlev;
1946 "specified number of channels\n");
1983 "room_type is set\n");
1988 "80dB and 111dB\n");
1999 static int warn_once = 1;
2002 "not compatible with reduced samplerates. writing of "
2003 "extended bitstream information will be disabled.\n");
2026 for (ch = 0; ch < s->
channels; ch++)
2066 uint64_t *channel_layout)
2072 if (*channel_layout > 0x7FF)
2074 ch_layout = *channel_layout;
2085 switch (ch_layout) {
2091 case AV_CH_LAYOUT_QUAD:
2093 case AV_CH_LAYOUT_5POINT0:
2102 *channel_layout = ch_layout;
2118 "encoder will guess the layout, but it "
2119 "might be incorrect.\n");
2131 max_sr = s->
eac3 ? 2 : 8;
2132 for (i = 0; i <= max_sr; i++) {
2147 int max_br, min_br, wpf, min_br_dist, min_br_code;
2148 int num_blks_code, num_blocks, frame_samples;
2154 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2155 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2157 max_br = 2048 * s->
sample_rate / frame_samples * 16;
2158 min_br = ((s->
sample_rate + (frame_samples-1)) / frame_samples) * 16;
2164 "for this sample rate\n", min_br, max_br);
2178 min_br_dist = INT_MAX;
2179 for (i = 0; i < 19; i++) {
2181 if (br_dist < min_br_dist) {
2182 min_br_dist = br_dist;
2193 for (i = 0; i < 19; i++) {
2271 if (cpl_start < 0) {
2280 int i, cpl_start_band, cpl_end_band;
2284 cpl_start_band = av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2289 *cpl_band_sizes = 12;
2290 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2292 *cpl_band_sizes += 12;
2296 *cpl_band_sizes = 12;
2313 int channel_blocks = channels * s->
num_blocks;
2366 for (ch = 0; ch < channels; ch++) {
2391 for (ch = 0; ch < channels; ch++)
2399 for (ch = 0; ch < channels; ch++)
2413 int ret, frame_size_58;