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++) {
319 if (CONFIG_EAC3_ENCODER && s->
eac3)
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)
410 if (CONFIG_EAC3_ENCODER && s->
eac3)
431 switch(exp_strategy) {
433 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
435 if (exp[k+1] < exp_min)
437 exp[i-cpl] = exp_min;
442 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
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--) {
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;
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));
1207 int16_t *qmant,
int start_freq,
1212 for (i = start_freq; i < end_freq; i++) {
1213 int c = fixed_coef[i];
1301 int blk, ch, ch0=0, got_cpl;
1308 for (ch = 1; ch <= s->
channels; ch++) {
1386 int ch, i, baie, bnd, got_cpl,
av_uninit(ch0);
1415 int start_sub, end_sub;
1432 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1457 if (!s->
eac3 || blk > 0)
1468 for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
1481 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1483 int cpl = (ch ==
CPL_CH);
1493 for (i = 1; i <= nb_groups; i++)
1519 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1545 for (ch = 1; ch <= s->
channels; ch++) {
1553 for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
1554 q = block->
qmant[ch][i];
1558 case 1:
if (q != 128)
put_bits (&s->
pb, 5, q);
break;
1559 case 2:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1561 case 4:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1574 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1614 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1640 crc2_partial =
av_crc(crc_ctx, 0, frame + frame_size_58,
1645 if (crc2 == 0x770B) {
1683 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1684 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1685 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1686 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1687 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1688 default:
snprintf(strbuf, 32,
"ERROR");
1693 av_dlog(avctx,
"channel_layout: %s\n", strbuf);
1700 av_dlog(avctx,
"per_frame_metadata: %s\n",
1706 av_dlog(avctx,
"center_mixlev: {not written}\n");
1711 av_dlog(avctx,
"surround_mixlev: {not written}\n");
1720 av_dlog(avctx,
"room_type: %s\n", strbuf);
1722 av_dlog(avctx,
"mixing_level: {not written}\n");
1723 av_dlog(avctx,
"room_type: {not written}\n");
1734 av_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1736 av_dlog(avctx,
"dsur_mode: {not written}\n");
1748 av_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1749 av_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1751 av_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1753 av_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1755 av_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1758 av_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1767 av_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1774 av_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1781 av_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1783 av_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1790 #define FLT_OPTION_THRESHOLD 0.01
1796 for (i = 0; i < v_list_size; i++) {
1801 if (i == v_list_size)
1809 float *opt_param,
const float *list,
1810 int list_size,
int default_value,
int min_value,
1814 if (mixlev < min_value) {
1815 mixlev = default_value;
1816 if (*opt_param >= 0.0) {
1818 "default value: %0.3f\n", opt_name, list[mixlev]);
1821 *opt_param = list[mixlev];
1822 *ctx_param = mixlev;
1942 "specified number of channels\n");
1979 "room_type is set\n");
1984 "80dB and 111dB\n");
1995 static int warn_once = 1;
1998 "not compatible with reduced samplerates. writing of "
1999 "extended bitstream information will be disabled.\n");
2023 for (ch = 0; ch < s->
channels; ch++)
2063 uint64_t *channel_layout)
2069 if (*channel_layout > 0x7FF)
2071 ch_layout = *channel_layout;
2082 switch (ch_layout) {
2088 case AV_CH_LAYOUT_QUAD:
2090 case AV_CH_LAYOUT_5POINT0:
2099 *channel_layout = ch_layout;
2115 "encoder will guess the layout, but it "
2116 "might be incorrect.\n");
2128 max_sr = s->
eac3 ? 2 : 8;
2129 for (i = 0; i <= max_sr; i++) {
2145 case 1: avctx->
bit_rate = 96000;
break;
2146 case 2: avctx->
bit_rate = 192000;
break;
2147 case 3: avctx->
bit_rate = 320000;
break;
2148 case 4: avctx->
bit_rate = 384000;
break;
2149 case 5: avctx->
bit_rate = 448000;
break;
2155 int max_br, min_br, wpf, min_br_dist, min_br_code;
2156 int num_blks_code, num_blocks, frame_samples;
2162 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2163 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2165 max_br = 2048 * s->
sample_rate / frame_samples * 16;
2166 min_br = ((s->
sample_rate + (frame_samples-1)) / frame_samples) * 16;
2172 "for this sample rate\n", min_br, max_br);
2186 min_br_dist = INT_MAX;
2187 for (i = 0; i < 19; i++) {
2189 if (br_dist < min_br_dist) {
2190 min_br_dist = br_dist;
2201 int best_br = 0, best_code = 0, best_diff = INT_MAX;
2202 for (i = 0; i < 19; i++) {
2205 if (diff < best_diff) {
2283 if (cpl_start < 0) {
2292 int i, cpl_start_band, cpl_end_band;
2296 cpl_start_band = av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2301 *cpl_band_sizes = 12;
2302 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2304 *cpl_band_sizes += 12;
2308 *cpl_band_sizes = 12;
2325 int channel_blocks = channels * s->
num_blocks;
2378 for (ch = 0; ch < channels; ch++) {
2403 for (ch = 0; ch < channels; ch++)
2411 for (ch = 0; ch < channels; ch++)
2425 int ret, frame_size_58;
2460 }
else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
2465 if (CONFIG_EAC3_ENCODER && s->
eac3)