52 #define CMIXLEV_NUM_OPTIONS 3
57 #define SURMIXLEV_NUM_OPTIONS 3
62 #define EXTMIXLEV_NUM_OPTIONS 8
110 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
111 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
112 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
114 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
115 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
116 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
118 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
119 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
120 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
122 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
126 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
127 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
144 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
145 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
146 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
149 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -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 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
154 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -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 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
159 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
160 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
161 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
164 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
165 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
166 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
169 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
170 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
171 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
241 if (!blk || (block->
cpl_in_use && !got_cpl_snr)) {
289 for (i = start; i <
end; i++) {
306 int expstr, i, grpsize;
309 grpsize = 3 << expstr;
310 for (i = 12; i < 256; i++) {
318 if (CONFIG_EAC3_ENCODER && s->
eac3)
340 #define EXP_DIFF_THRESHOLD 500
360 for (ch = !s->
cpl_on; ch <= s->fbw_channels; ch++) {
393 while (blk < s->num_blocks) {
395 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
409 if (CONFIG_EAC3_ENCODER && s->
eac3)
430 switch(exp_strategy) {
432 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
434 if (exp[k+1] < exp_min)
436 exp[i-cpl] = exp_min;
441 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
443 if (exp[k+1] < exp_min)
445 if (exp[k+2] < exp_min)
447 if (exp[k+3] < exp_min)
449 exp[i-cpl] = exp_min;
456 if (!cpl && exp[0] > 15)
461 for (i = 1; i <= nb_groups; i++)
462 exp[i] =
FFMIN(exp[i], exp[i-1] + 2);
465 exp[i] =
FFMIN(exp[i], exp[i+1] + 2);
468 exp[-1] = exp[0] & ~1;
471 switch (exp_strategy) {
473 for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
480 for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
481 exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
497 int blk, blk1, ch, cpl;
499 int nb_coefs, num_reuse_blocks;
501 for (ch = !s->
cpl_on; ch <= s->channels; ch++) {
507 while (blk < s->num_blocks) {
520 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
524 num_reuse_blocks = blk1 - blk - 1;
548 int nb_groups, bit_count;
553 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
562 bit_count += 4 + (nb_groups * 7);
580 int group_size, nb_groups;
582 int delta0, delta1, delta2;
587 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
592 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
601 for (i = 1; i <= nb_groups; i++) {
606 delta0 = exp1 - exp0 + 2;
612 delta1 = exp1 - exp0 + 2;
618 delta2 = exp1 - exp0 + 2;
621 block->
grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
653 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
727 frame_bits += 2 + 2 + 2 + 2 + 3;
747 frame_bits += 1 + 16;
767 for (ch = 0; ch <= s->
channels; ch++)
809 frame_bits += 1 + 1 + 2;
815 frame_bits += 3 + 1 + 1;
822 frame_bits += 5 + 2 + 1;
838 frame_bits += 5 * s->
cpl_on;
896 if (!s->
eac3 || blk > 0)
945 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
978 for (ch = 0; ch <= s->
channels; ch++) {
999 memset(mant_cnt[blk], 0,
sizeof(mant_cnt[blk]));
1000 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1001 mant_cnt[
blk][4] = 1;
1038 int ch, max_end_freq;
1044 for (ch = !s->
cpl_enabled; ch <= s->channels; ch++)
1066 snr_offset = (snr_offset - 240) << 2;
1072 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1097 int snr_offset, snr_incr;
1112 while (snr_offset >= 0 &&
1120 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1121 while (snr_offset + snr_incr <= 1023 &&
1122 bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1123 snr_offset += snr_incr;
1131 for (ch = !s->
cpl_on; ch <= s->channels; ch++)
1166 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1184 c = (((c << e) >> (24 - qbits)) + 1) >> 1;
1185 m = (1 << (qbits-1));
1206 int16_t *qmant,
int start_freq,
1211 for (i = start_freq; i < end_freq; i++) {
1212 int c = fixed_coef[i];
1300 int blk, ch, ch0=0, got_cpl;
1307 for (ch = 1; ch <= s->
channels; ch++) {
1385 int ch, i, baie, bnd, got_cpl, ch0;
1414 int start_sub, end_sub;
1431 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1456 if (!s->
eac3 || blk > 0)
1467 for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
1480 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1482 int cpl = (ch ==
CPL_CH);
1492 for (i = 1; i <= nb_groups; i++)
1518 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1544 for (ch = 1; ch <= s->
channels; ch++) {
1552 for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
1553 q = block->
qmant[ch][i];
1557 case 1:
if (q != 128)
put_bits (&s->
pb, 5, q);
break;
1558 case 2:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1560 case 4:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1573 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1613 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1639 crc2_partial =
av_crc(crc_ctx, 0, frame + frame_size_58,
1644 if (crc2 == 0x770B) {
1682 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1683 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1684 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1685 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1686 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1687 default:
snprintf(strbuf, 32,
"ERROR");
1692 av_dlog(avctx,
"channel_layout: %s\n", strbuf);
1699 av_dlog(avctx,
"per_frame_metadata: %s\n",
1705 av_dlog(avctx,
"center_mixlev: {not written}\n");
1710 av_dlog(avctx,
"surround_mixlev: {not written}\n");
1719 av_dlog(avctx,
"room_type: %s\n", strbuf);
1721 av_dlog(avctx,
"mixing_level: {not written}\n");
1722 av_dlog(avctx,
"room_type: {not written}\n");
1733 av_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1735 av_dlog(avctx,
"dsur_mode: {not written}\n");
1747 av_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1748 av_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1750 av_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1752 av_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1754 av_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1757 av_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1766 av_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1773 av_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1780 av_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1782 av_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1789 #define FLT_OPTION_THRESHOLD 0.01
1795 for (i = 0; i < v_list_size; i++) {
1800 if (i == v_list_size)
1808 float *opt_param,
const float *list,
1809 int list_size,
int default_value,
int min_value,
1813 if (mixlev < min_value) {
1814 mixlev = default_value;
1815 if (*opt_param >= 0.0) {
1817 "default value: %0.3f\n", opt_name, list[mixlev]);
1820 *opt_param = list[mixlev];
1821 *ctx_param = mixlev;
1941 "specified number of channels\n");
1978 "room_type is set\n");
1983 "80dB and 111dB\n");
1994 static int warn_once = 1;
1997 "not compatible with reduced samplerates. writing of "
1998 "extended bitstream information will be disabled.\n");
2021 for (ch = 0; ch < s->
channels; ch++)
2052 #if FF_API_OLD_ENCODE_AUDIO
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) {
2252 int blk, ch, cpl_start;
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)
2484 #if FF_API_OLD_ENCODE_AUDIO