51 #define CMIXLEV_NUM_OPTIONS 3 
   56 #define SURMIXLEV_NUM_OPTIONS 3 
   61 #define EXTMIXLEV_NUM_OPTIONS 8 
  109     { {  0,  0,  0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
 
  110       {  0,  0,  0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
 
  111       {  0,  0,  0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
 
  113     { {  0,  0,  0,  0,  0,  0,  0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
 
  114       {  0,  0,  0,  0,  0,  0,  4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
 
  115       {  0,  0,  0,  0,  0,  0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
 
  117     { {  0,  0,  0,  0,  0,  0,  0,  0,  0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
 
  118       {  0,  0,  0,  0,  0,  0,  0,  0,  4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
 
  119       {  0,  0,  0,  0,  0,  0,  0,  0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
 
  121     { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
 
  122       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
 
  123       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
 
  125     { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8, 20, 32, 40, 48, 48, 48, 48 },
 
  126       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12, 24, 36, 44, 56, 56, 56, 56 },
 
  127       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 28, 44, 60, 60, 60, 60, 60, 60 } }
 
  143     { {  0,  0,  0,  0,  0,  0,  0,  1,  1,  7,  8, 11, 12, -1, -1, -1, -1, -1, -1 },
 
  144       {  0,  0,  0,  0,  0,  0,  1,  3,  5,  7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
 
  145       {  0,  0,  0,  0,  1,  2,  2,  9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  148     { {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -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       { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  153     { {  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  6,  9, 11, 12, 13, -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       { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  158     { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
 
  159       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
 
  160       { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  163     { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
 
  164       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  2, 10, 11, 11, 12, 12, 14, -1 },
 
  165       { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  168     { {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  6,  8, 11, 12, 12, -1, -1 },
 
  169       {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  6,  8, 11, 12, 12, -1, -1 },
 
  170       { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
 
  240         if (!blk || (block->
cpl_in_use && !got_cpl_snr)) {
 
  288                 for (i = start; i < 
end; i++) {
 
  305     int expstr, i, grpsize;
 
  308         grpsize = 3 << expstr;
 
  309         for (i = 12; i < 256; i++) {
 
  317     if (CONFIG_EAC3_ENCODER && s->
eac3)
 
  339 #define EXP_DIFF_THRESHOLD 500 
  359     for (ch = !s->
cpl_on; ch <= s->fbw_channels; ch++) {
 
  392         while (blk < s->num_blocks) {
 
  394             while (blk1 < s->num_blocks && exp_strategy[blk1] == 
EXP_REUSE)
 
  408     if (CONFIG_EAC3_ENCODER && s->
eac3)
 
  429     switch(exp_strategy) {
 
  431         for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
 
  433             if (exp[k+1] < exp_min)
 
  435             exp[i-cpl] = exp_min;
 
  440         for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
 
  442             if (exp[k+1] < exp_min)
 
  444             if (exp[k+2] < exp_min)
 
  446             if (exp[k+3] < exp_min)
 
  448             exp[i-cpl] = exp_min;
 
  455     if (!cpl && exp[0] > 15)
 
  460     for (i = 1; i <= nb_groups; i++)
 
  461         exp[i] = 
FFMIN(exp[i], exp[i-1] + 2);
 
  464         exp[i] = 
FFMIN(exp[i], exp[i+1] + 2);
 
  467         exp[-1] = exp[0] & ~1;
 
  470     switch (exp_strategy) {
 
  472         for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
 
  479         for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
 
  480             exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
 
  496     int blk, blk1, ch, cpl;
 
  498     int nb_coefs, num_reuse_blocks;
 
  500     for (ch = !s->
cpl_on; ch <= s->channels; ch++) {
 
  506         while (blk < s->num_blocks) {
 
  519             while (blk1 < s->num_blocks && exp_strategy[blk1] == 
EXP_REUSE) {
 
  523             num_reuse_blocks = blk1 - blk - 1;
 
  547     int nb_groups, bit_count;
 
  552         for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
  561             bit_count += 4 + (nb_groups * 7);
 
  579     int group_size, nb_groups;
 
  581     int delta0, delta1, delta2;
 
  586         for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
  591             group_size = exp_strategy + (exp_strategy == 
EXP_D45);
 
  600             for (i = 1; i <= nb_groups; i++) {
 
  605                 delta0 = exp1 - exp0 + 2;
 
  611                 delta1 = exp1 - exp0 + 2;
 
  617                 delta2 = exp1 - exp0 + 2;
 
  620                 block->
grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
 
  652     static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
 
  726                 frame_bits += 2 + 2 + 2 + 2 + 3;
 
  746     frame_bits += 1 + 16;
 
  766     for (ch = 0; ch <= s->
channels; ch++)
 
  808             frame_bits += 1 + 1 + 2;
 
  814             frame_bits += 3 + 1 + 1;
 
  821                 frame_bits += 5 + 2 + 1;
 
  837                 frame_bits += 5 * s->
cpl_on;
 
  895             if (!s->
eac3 || blk > 0)
 
  944         for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
  977     for (ch = 0; ch <= s->
channels; ch++) {
 
  998         memset(mant_cnt[blk], 0, 
sizeof(mant_cnt[blk]));
 
  999         mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
 
 1000         mant_cnt[
blk][4] = 1;
 
 1037     int ch, max_end_freq;
 
 1043     for (ch = !s->
cpl_enabled; ch <= s->channels; ch++)
 
 1065     snr_offset = (snr_offset - 240) << 2;
 
 1071         for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
 1096     int snr_offset, snr_incr;
 
 1111     while (snr_offset >= 0 &&
 
 1119     for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
 
 1120         while (snr_offset + snr_incr <= 1023 &&
 
 1121                bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
 
 1122             snr_offset += snr_incr;
 
 1130     for (ch = !s->
cpl_on; ch <= s->channels; ch++)
 
 1165     int v = (((levels * 
c) >> (24 - e)) + levels) >> 1;
 
 1183     c = (((c << e) >> (24 - qbits)) + 1) >> 1;
 
 1184     m = (1 << (qbits-1));
 
 1205                                       int16_t *qmant, 
int start_freq,
 
 1210     for (i = start_freq; i < end_freq; i++) {
 
 1211         int c = fixed_coef[i];
 
 1299     int blk, ch, ch0=0, got_cpl;
 
 1306         for (ch = 1; ch <= s->
channels; ch++) {
 
 1384     int ch, i, baie, bnd, got_cpl, ch0;
 
 1413             int start_sub, end_sub;
 
 1430                 for (bnd = start_sub+1; bnd < end_sub; bnd++)
 
 1455         if (!s->
eac3 || blk > 0)
 
 1466         for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
 
 1479     for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
 1481         int cpl = (ch == 
CPL_CH);
 
 1491         for (i = 1; i <= nb_groups; i++)
 
 1517             for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
 
 1543     for (ch = 1; ch <= s->
channels; ch++) {
 
 1551         for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
 
 1552             q = block->
qmant[ch][i];
 
 1556             case 1: 
if (q != 128) 
put_bits (&s->
pb,   5, q); 
break;
 
 1557             case 2: 
if (q != 128) 
put_bits (&s->
pb,   7, q); 
break;
 
 1559             case 4: 
if (q != 128) 
put_bits (&s->
pb,   7, q); 
break;
 
 1572 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) 
 1612     int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
 
 1638     crc2_partial = 
av_crc(crc_ctx, 0, frame + frame_size_58,
 
 1643     if (crc2 == 0x770B) {
 
 1681     case  6:  
av_strlcpy(strbuf, 
"AC-3 (alt syntax)",       32); 
break;
 
 1682     case  8:  
av_strlcpy(strbuf, 
"AC-3 (standard)",         32); 
break;
 
 1683     case  9:  
av_strlcpy(strbuf, 
"AC-3 (dnet half-rate)",   32); 
break;
 
 1684     case 10:  
av_strlcpy(strbuf, 
"AC-3 (dnet quater-rate)", 32); 
break;
 
 1685     case 16:  
av_strlcpy(strbuf, 
"E-AC-3 (enhanced)",       32); 
break;
 
 1686     default: 
snprintf(strbuf, 32, 
"ERROR");
 
 1691     av_dlog(avctx, 
"channel_layout: %s\n", strbuf);
 
 1698     av_dlog(avctx, 
"per_frame_metadata: %s\n",
 
 1704         av_dlog(avctx, 
"center_mixlev: {not written}\n");
 
 1709         av_dlog(avctx, 
"surround_mixlev: {not written}\n");
 
 1718         av_dlog(avctx, 
"room_type: %s\n", strbuf);
 
 1720         av_dlog(avctx, 
"mixing_level: {not written}\n");
 
 1721         av_dlog(avctx, 
"room_type: {not written}\n");
 
 1732         av_dlog(avctx, 
"dsur_mode: %s\n", strbuf);
 
 1734         av_dlog(avctx, 
"dsur_mode: {not written}\n");
 
 1746             av_dlog(avctx, 
"dmix_mode: %s\n", strbuf);
 
 1747             av_dlog(avctx, 
"ltrt_cmixlev: %0.3f (%d)\n",
 
 1749             av_dlog(avctx, 
"ltrt_surmixlev: %0.3f (%d)\n",
 
 1751             av_dlog(avctx, 
"loro_cmixlev: %0.3f (%d)\n",
 
 1753             av_dlog(avctx, 
"loro_surmixlev: %0.3f (%d)\n",
 
 1756             av_dlog(avctx, 
"extended bitstream info 1: {not written}\n");
 
 1765             av_dlog(avctx, 
"dsurex_mode: %s\n", strbuf);
 
 1772             av_dlog(avctx, 
"dheadphone_mode: %s\n", strbuf);
 
 1779             av_dlog(avctx, 
"ad_conv_type: %s\n", strbuf);
 
 1781             av_dlog(avctx, 
"extended bitstream info 2: {not written}\n");
 
 1788 #define FLT_OPTION_THRESHOLD 0.01 
 1794     for (i = 0; i < v_list_size; i++) {
 
 1799     if (i == v_list_size)
 
 1807                                float *opt_param, 
const float *list,
 
 1808                                int list_size, 
int default_value, 
int min_value,
 
 1812     if (mixlev < min_value) {
 
 1813         mixlev = default_value;
 
 1814         if (*opt_param >= 0.0) {
 
 1816                    "default value: %0.3f\n", opt_name, list[mixlev]);
 
 1819     *opt_param = list[mixlev];
 
 1820     *ctx_param = mixlev;
 
 1940                                     "specified number of channels\n");
 
 1977                    "room_type is set\n");
 
 1982                    "80dB and 111dB\n");
 
 1993             static int warn_once = 1;
 
 1996                        "not compatible with reduced samplerates. writing of " 
 1997                        "extended bitstream information will be disabled.\n");
 
 2021     for (ch = 0; ch < s->
channels; ch++)
 
 2060                                     uint64_t *channel_layout)
 
 2066     if (*channel_layout > 0x7FF)
 
 2068     ch_layout = *channel_layout;
 
 2079     switch (ch_layout) {
 
 2085     case AV_CH_LAYOUT_QUAD:
 
 2087     case AV_CH_LAYOUT_5POINT0:
 
 2096     *channel_layout = ch_layout;
 
 2112                                       "encoder will guess the layout, but it " 
 2113                                       "might be incorrect.\n");
 
 2125     max_sr = s->
eac3 ? 2 : 8;
 
 2126     for (i = 0; i <= max_sr; i++) {
 
 2142         case 1: avctx->
bit_rate =  96000; 
break;
 
 2143         case 2: avctx->
bit_rate = 192000; 
break;
 
 2144         case 3: avctx->
bit_rate = 320000; 
break;
 
 2145         case 4: avctx->
bit_rate = 384000; 
break;
 
 2146         case 5: avctx->
bit_rate = 448000; 
break;
 
 2152         int max_br, min_br, wpf, min_br_dist, min_br_code;
 
 2153         int num_blks_code, num_blocks, frame_samples;
 
 2159         for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
 
 2160             num_blocks = ((
int[]){ 1, 2, 3, 6 })[num_blks_code];
 
 2162             max_br = 2048 * s->
sample_rate / frame_samples * 16;
 
 2163             min_br = ((s->
sample_rate + (frame_samples-1)) / frame_samples) * 16;
 
 2169                    "for this sample rate\n", min_br, max_br);
 
 2183         min_br_dist = INT_MAX;
 
 2184         for (i = 0; i < 19; i++) {
 
 2186             if (br_dist < min_br_dist) {
 
 2187                 min_br_dist = br_dist;
 
 2198         int best_br = 0, best_code = 0, best_diff = INT_MAX;
 
 2199         for (i = 0; i < 19; i++) {
 
 2201             int diff = abs(br - avctx->
bit_rate);
 
 2202             if (diff < best_diff) {
 
 2249     int blk, ch, cpl_start;
 
 2280             if (cpl_start < 0) {
 
 2289         int i, cpl_start_band, cpl_end_band;
 
 2293         cpl_start_band = av_clip(cpl_start, 0, 
FFMIN(cpl_end_band-1, 15));
 
 2298         *cpl_band_sizes  = 12;
 
 2299         for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
 
 2301                 *cpl_band_sizes += 12;
 
 2305                 *cpl_band_sizes = 12;
 
 2322     int channel_blocks = channels * s->
num_blocks;
 
 2375         for (ch = 0; ch < channels; ch++) {
 
 2400             for (ch = 0; ch < channels; ch++)
 
 2408             for (ch = 0; ch < channels; ch++)
 
 2422     int ret, frame_size_58;
 
 2457     } 
else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
 
 2462     if (CONFIG_EAC3_ENCODER && s->
eac3)