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++) {
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)
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++) {
1213 int c = fixed_coef[i];
1303 int blk, ch, ch0=0, got_cpl;
1310 for (ch = 1; ch <= s->
channels; ch++) {
1388 int ch, i, baie, bnd, got_cpl, ch0;
1417 int start_sub, end_sub;
1434 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1459 if (!s->
eac3 || blk > 0)
1470 for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
1483 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1485 int cpl = (ch ==
CPL_CH);
1495 for (i = 1; i <= nb_groups; i++)
1521 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1547 for (ch = 1; ch <= s->
channels; ch++) {
1555 for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
1556 q = block->
qmant[ch][i];
1560 case 1:
if (q != 128)
put_bits (&s->
pb, 5, q);
break;
1561 case 2:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1563 case 4:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1576 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1616 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1642 crc2_partial =
av_crc(crc_ctx, 0, frame + frame_size_58,
1647 if (crc2 == 0x770B) {
1685 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1686 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1687 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1688 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1689 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1690 default: snprintf(strbuf, 32,
"ERROR");
1695 av_dlog(avctx,
"channel_layout: %s\n", strbuf);
1702 av_dlog(avctx,
"per_frame_metadata: %s\n",
1708 av_dlog(avctx,
"center_mixlev: {not written}\n");
1713 av_dlog(avctx,
"surround_mixlev: {not written}\n");
1720 default: snprintf(strbuf, 32,
"ERROR (%d)", opt->
room_type);
1722 av_dlog(avctx,
"room_type: %s\n", strbuf);
1724 av_dlog(avctx,
"mixing_level: {not written}\n");
1725 av_dlog(avctx,
"room_type: {not written}\n");
1736 av_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1738 av_dlog(avctx,
"dsur_mode: {not written}\n");
1750 av_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1751 av_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1753 av_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1755 av_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1757 av_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1760 av_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1769 av_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1776 av_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1783 av_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1785 av_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1792 #define FLT_OPTION_THRESHOLD 0.01
1798 for (i = 0; i < v_list_size; i++) {
1803 if (i == v_list_size)
1811 float *opt_param,
const float *list,
1812 int list_size,
int default_value,
int min_value,
1816 if (mixlev < min_value) {
1817 mixlev = default_value;
1818 if (*opt_param >= 0.0) {
1820 "default value: %0.3f\n", opt_name, list[mixlev]);
1823 *opt_param = list[mixlev];
1824 *ctx_param = mixlev;
1944 "specified number of channels\n");
1981 "room_type is set\n");
1986 "80dB and 111dB\n");
1997 static int warn_once = 1;
2000 "not compatible with reduced samplerates. writing of "
2001 "extended bitstream information will be disabled.\n");
2024 for (ch = 0; ch < s->
channels; ch++)
2055 #if FF_API_OLD_ENCODE_AUDIO
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++) {
2148 case 1: avctx->
bit_rate = 96000;
break;
2149 case 2: avctx->
bit_rate = 192000;
break;
2150 case 3: avctx->
bit_rate = 320000;
break;
2151 case 4: avctx->
bit_rate = 384000;
break;
2152 case 5: avctx->
bit_rate = 448000;
break;
2158 int max_br, min_br, wpf, min_br_dist, min_br_code;
2159 int num_blks_code, num_blocks, frame_samples;
2165 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2166 num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
2168 max_br = 2048 * s->
sample_rate / frame_samples * 16;
2169 min_br = ((s->
sample_rate + (frame_samples-1)) / frame_samples) * 16;
2175 "for this sample rate\n", min_br, max_br);
2189 min_br_dist = INT_MAX;
2190 for (i = 0; i < 19; i++) {
2192 if (br_dist < min_br_dist) {
2193 min_br_dist = br_dist;
2204 int best_br = 0, best_code = 0, best_diff = INT_MAX;
2205 for (i = 0; i < 19; i++) {
2207 int diff = abs(br - avctx->
bit_rate);
2208 if (diff < best_diff) {
2255 int blk, ch, cpl_start;
2286 if (cpl_start < 0) {
2295 int i, cpl_start_band, cpl_end_band;
2299 cpl_start_band = av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2304 *cpl_band_sizes = 12;
2305 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2307 *cpl_band_sizes += 12;
2311 *cpl_band_sizes = 12;
2328 int channel_blocks = channels * s->
num_blocks;
2381 for (ch = 0; ch < channels; ch++) {
2406 for (ch = 0; ch < channels; ch++)
2414 for (ch = 0; ch < channels; ch++)
2428 int ret, frame_size_58;
2487 #if FF_API_OLD_ENCODE_AUDIO