Browse code

ac3enc: add support for E-AC-3 encoding.

This adds basic stream format support and allows for arbitrary bit rates
rather than just those supported in AC-3.

Justin Ruggles authored on 2011/05/25 04:20:56
Showing 9 changed files
... ...
@@ -4,6 +4,8 @@ releases are sorted from youngest to oldest.
4 4
 
5 5
 version <next>:
6 6
 
7
+- E-AC-3 audio encoder
8
+
7 9
 
8 10
 version 0.7_beta2:
9 11
 
... ...
@@ -1261,6 +1261,7 @@ dca_decoder_select="mdct"
1261 1261
 dnxhd_encoder_select="aandct"
1262 1262
 dxa_decoder_select="zlib"
1263 1263
 eac3_decoder_select="ac3_decoder"
1264
+eac3_encoder_select="mdct ac3dsp"
1264 1265
 eamad_decoder_select="aandct"
1265 1266
 eatgq_decoder_select="aandct"
1266 1267
 eatqi_decoder_select="aandct"
... ...
@@ -614,7 +614,7 @@ following image formats are supported:
614 614
     @tab Used in Origin's Wing Commander IV AVI files.
615 615
 @item DSP Group TrueSpeech   @tab     @tab  X
616 616
 @item DV audio               @tab     @tab  X
617
-@item Enhanced AC-3          @tab     @tab  X
617
+@item Enhanced AC-3          @tab  X  @tab  X
618 618
 @item FLAC (Free Lossless Audio Codec)  @tab  X  @tab  IX
619 619
 @item GSM                    @tab  E  @tab  X
620 620
     @tab encoding supported through external library libgsm
... ...
@@ -124,6 +124,7 @@ OBJS-$(CONFIG_DVVIDEO_DECODER)         += dv.o dvdata.o
124 124
 OBJS-$(CONFIG_DVVIDEO_ENCODER)         += dv.o dvdata.o
125 125
 OBJS-$(CONFIG_DXA_DECODER)             += dxa.o
126 126
 OBJS-$(CONFIG_EAC3_DECODER)            += eac3dec.o eac3dec_data.o
127
+OBJS-$(CONFIG_EAC3_ENCODER)            += ac3enc_float.o ac3tab.o ac3.o kbdwin.o
127 128
 OBJS-$(CONFIG_EACMV_DECODER)           += eacmv.o
128 129
 OBJS-$(CONFIG_EAMAD_DECODER)           += eamad.o eaidct.o mpeg12.o \
129 130
                                           mpeg12data.o mpegvideo.o  \
... ...
@@ -147,6 +147,7 @@ typedef struct AC3EncodeContext {
147 147
 
148 148
     AC3Block blocks[AC3_MAX_BLOCKS];        ///< per-block info
149 149
 
150
+    int eac3;                               ///< indicates if this is E-AC-3 vs. AC-3
150 151
     int bitstream_id;                       ///< bitstream id                           (bsid)
151 152
     int bitstream_mode;                     ///< bitstream mode                         (bsmod)
152 153
 
... ...
@@ -157,8 +158,8 @@ typedef struct AC3EncodeContext {
157 157
     int frame_size;                         ///< current frame size in bytes
158 158
     int frame_size_code;                    ///< frame size code                        (frmsizecod)
159 159
     uint16_t crc_inv[2];
160
-    int bits_written;                       ///< bit count    (used to avg. bitrate)
161
-    int samples_written;                    ///< sample count (used to avg. bitrate)
160
+    int64_t bits_written;                   ///< bit count    (used to avg. bitrate)
161
+    int64_t samples_written;                ///< sample count (used to avg. bitrate)
162 162
 
163 163
     int fbw_channels;                       ///< number of full-bandwidth channels      (nfchans)
164 164
     int channels;                           ///< total number of channels               (nchans)
... ...
@@ -247,15 +248,20 @@ static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
247 247
 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
248 248
 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
249 249
 
250
-
251 250
 #define AC3ENC_TYPE_AC3_FIXED   0
252 251
 #define AC3ENC_TYPE_AC3         1
252
+#define AC3ENC_TYPE_EAC3        2
253 253
 
254 254
 #if CONFIG_AC3ENC_FLOAT
255 255
 #define AC3ENC_TYPE AC3ENC_TYPE_AC3
256 256
 #include "ac3enc_opts_template.c"
257 257
 static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
258 258
                                 ac3_options, LIBAVUTIL_VERSION_INT };
259
+#undef AC3ENC_TYPE
260
+#define AC3ENC_TYPE AC3ENC_TYPE_EAC3
261
+#include "ac3enc_opts_template.c"
262
+static AVClass eac3enc_class = { "E-AC-3 Encoder", av_default_item_name,
263
+                                 eac3_options, LIBAVUTIL_VERSION_INT };
259 264
 #else
260 265
 #define AC3ENC_TYPE AC3ENC_TYPE_AC3_FIXED
261 266
 #include "ac3enc_opts_template.c"
... ...
@@ -387,7 +393,7 @@ static const int8_t ac3_coupling_start_tab[6][3][19] = {
387 387
 
388 388
 /**
389 389
  * Adjust the frame size to make the average bit rate match the target bit rate.
390
- * This is only needed for 11025, 22050, and 44100 sample rates.
390
+ * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
391 391
  */
392 392
 static void adjust_frame_size(AC3EncodeContext *s)
393 393
 {
... ...
@@ -734,6 +740,35 @@ static void apply_channel_coupling(AC3EncodeContext *s)
734 734
             }
735 735
         }
736 736
     }
737
+
738
+    if (s->eac3) {
739
+        /* set first cpl coords */
740
+        int first_cpl_coords[AC3_MAX_CHANNELS];
741
+        for (ch = 1; ch <= s->fbw_channels; ch++)
742
+            first_cpl_coords[ch] = 1;
743
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
744
+            AC3Block *block = &s->blocks[blk];
745
+            for (ch = 1; ch <= s->fbw_channels; ch++) {
746
+                if (block->channel_in_cpl[ch]) {
747
+                    if (first_cpl_coords[ch]) {
748
+                        block->new_cpl_coords = 2;
749
+                        first_cpl_coords[ch]  = 0;
750
+                    }
751
+                } else {
752
+                    first_cpl_coords[ch] = 1;
753
+                }
754
+            }
755
+        }
756
+
757
+        /* set first cpl leak */
758
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
759
+            AC3Block *block = &s->blocks[blk];
760
+            if (block->cpl_in_use) {
761
+                block->new_cpl_leak = 2;
762
+                break;
763
+            }
764
+        }
765
+    }
737 766
 #endif /* CONFIG_AC3ENC_FLOAT */
738 767
 }
739 768
 
... ...
@@ -1151,23 +1186,50 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
1151 1151
      *   no delta bit allocation
1152 1152
      *   no skipped data
1153 1153
      *   no auxilliary data
1154
+     *   no E-AC-3 metadata
1154 1155
      */
1155 1156
 
1156 1157
     /* header */
1157
-    frame_bits = 65;
1158
-    frame_bits += frame_bits_inc[s->channel_mode];
1158
+    frame_bits = 16; /* sync info */
1159
+    if (s->eac3) {
1160
+        /* bitstream info header */
1161
+        frame_bits += 35;
1162
+        frame_bits += 1 + 1 + 1;
1163
+        /* audio frame header */
1164
+        frame_bits += 2;
1165
+        frame_bits += 10;
1166
+        /* exponent strategy */
1167
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1168
+            frame_bits += 2 * s->fbw_channels + s->lfe_on;
1169
+        /* converter exponent strategy */
1170
+        frame_bits += s->fbw_channels * 5;
1171
+        /* snr offsets */
1172
+        frame_bits += 10;
1173
+        /* block start info */
1174
+        frame_bits++;
1175
+    } else {
1176
+        frame_bits += 49;
1177
+        frame_bits += frame_bits_inc[s->channel_mode];
1178
+    }
1159 1179
 
1160 1180
     /* audio blocks */
1161 1181
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1182
+        if (!s->eac3) {
1162 1183
         /* block switch flags */
1163 1184
         frame_bits += s->fbw_channels;
1164 1185
 
1165 1186
         /* dither flags */
1166 1187
         frame_bits += s->fbw_channels;
1188
+        }
1167 1189
 
1168 1190
         /* dynamic range */
1169 1191
         frame_bits++;
1170 1192
 
1193
+        /* spectral extension */
1194
+        if (s->eac3)
1195
+            frame_bits++;
1196
+
1197
+        if (!s->eac3) {
1171 1198
         /* exponent strategy */
1172 1199
         frame_bits += 2 * s->fbw_channels;
1173 1200
         if (s->lfe_on)
... ...
@@ -1177,12 +1239,19 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
1177 1177
         frame_bits++;
1178 1178
         if (!blk)
1179 1179
             frame_bits += 2 + 2 + 2 + 2 + 3;
1180
+        }
1180 1181
 
1182
+        /* converter snr offset */
1183
+        if (s->eac3)
1184
+            frame_bits++;
1185
+
1186
+        if (!s->eac3) {
1181 1187
         /* delta bit allocation */
1182 1188
         frame_bits++;
1183 1189
 
1184 1190
         /* skipped data */
1185 1191
         frame_bits++;
1192
+        }
1186 1193
     }
1187 1194
 
1188 1195
     /* auxiliary data */
... ...
@@ -1207,7 +1276,7 @@ static void bit_alloc_init(AC3EncodeContext *s)
1207 1207
     s->slow_decay_code = 2;
1208 1208
     s->fast_decay_code = 1;
1209 1209
     s->slow_gain_code  = 1;
1210
-    s->db_per_bit_code = 3;
1210
+    s->db_per_bit_code = s->eac3 ? 2 : 3;
1211 1211
     s->floor_code      = 7;
1212 1212
     for (ch = 0; ch <= s->channels; ch++)
1213 1213
         s->fast_gain_code[ch] = 4;
... ...
@@ -1242,6 +1311,21 @@ static void count_frame_bits(AC3EncodeContext *s)
1242 1242
     int frame_bits = 0;
1243 1243
 
1244 1244
     /* header */
1245
+    if (s->eac3) {
1246
+        /* coupling */
1247
+        if (s->channel_mode > AC3_CHMODE_MONO) {
1248
+            frame_bits++;
1249
+            for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
1250
+                AC3Block *block = &s->blocks[blk];
1251
+                frame_bits++;
1252
+                if (block->new_cpl_strategy)
1253
+                    frame_bits++;
1254
+            }
1255
+        }
1256
+        /* coupling exponent strategy */
1257
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1258
+            frame_bits += 2 * s->blocks[blk].cpl_in_use;
1259
+    } else {
1245 1260
     if (opt->audio_production_info)
1246 1261
         frame_bits += 7;
1247 1262
     if (s->bitstream_id == 6) {
... ...
@@ -1250,20 +1334,29 @@ static void count_frame_bits(AC3EncodeContext *s)
1250 1250
         if (opt->extended_bsi_2)
1251 1251
             frame_bits += 14;
1252 1252
     }
1253
+    }
1253 1254
 
1254 1255
     /* audio blocks */
1255 1256
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1256 1257
         AC3Block *block = &s->blocks[blk];
1257 1258
 
1258 1259
         /* coupling strategy */
1260
+        if (!s->eac3)
1259 1261
         frame_bits++;
1260 1262
         if (block->new_cpl_strategy) {
1263
+            if (!s->eac3)
1261 1264
             frame_bits++;
1262 1265
             if (block->cpl_in_use) {
1266
+                if (s->eac3)
1267
+                    frame_bits++;
1268
+                if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO)
1263 1269
                 frame_bits += s->fbw_channels;
1264 1270
                 if (s->channel_mode == AC3_CHMODE_STEREO)
1265 1271
                     frame_bits++;
1266 1272
                 frame_bits += 4 + 4;
1273
+                if (s->eac3)
1274
+                    frame_bits++;
1275
+                else
1267 1276
                 frame_bits += s->num_cpl_subbands - 1;
1268 1277
             }
1269 1278
         }
... ...
@@ -1272,6 +1365,7 @@ static void count_frame_bits(AC3EncodeContext *s)
1272 1272
         if (block->cpl_in_use) {
1273 1273
             for (ch = 1; ch <= s->fbw_channels; ch++) {
1274 1274
                 if (block->channel_in_cpl[ch]) {
1275
+                    if (!s->eac3 || block->new_cpl_coords != 2)
1275 1276
                     frame_bits++;
1276 1277
                     if (block->new_cpl_coords) {
1277 1278
                         frame_bits += 2;
... ...
@@ -1283,6 +1377,7 @@ static void count_frame_bits(AC3EncodeContext *s)
1283 1283
 
1284 1284
         /* stereo rematrixing */
1285 1285
         if (s->channel_mode == AC3_CHMODE_STEREO) {
1286
+            if (!s->eac3 || blk > 0)
1286 1287
             frame_bits++;
1287 1288
             if (s->blocks[blk].new_rematrixing_strategy)
1288 1289
                 frame_bits += block->num_rematrixing_bands;
... ...
@@ -1298,16 +1393,19 @@ static void count_frame_bits(AC3EncodeContext *s)
1298 1298
         }
1299 1299
 
1300 1300
         /* coupling exponent strategy */
1301
-        if (block->cpl_in_use)
1301
+        if (!s->eac3 && block->cpl_in_use)
1302 1302
             frame_bits += 2;
1303 1303
 
1304 1304
         /* snr offsets and fast gain codes */
1305
+        if (!s->eac3) {
1305 1306
         frame_bits++;
1306 1307
         if (block->new_snr_offsets)
1307 1308
             frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
1309
+        }
1308 1310
 
1309 1311
         /* coupling leak info */
1310 1312
         if (block->cpl_in_use) {
1313
+            if (!s->eac3 || block->new_cpl_leak != 2)
1311 1314
             frame_bits++;
1312 1315
             if (block->new_cpl_leak)
1313 1316
                 frame_bits += 3 + 3;
... ...
@@ -1736,7 +1834,7 @@ static void quantize_mantissas(AC3EncodeContext *s)
1736 1736
 /**
1737 1737
  * Write the AC-3 frame header to the output bitstream.
1738 1738
  */
1739
-static void output_frame_header(AC3EncodeContext *s)
1739
+static void ac3_output_frame_header(AC3EncodeContext *s)
1740 1740
 {
1741 1741
     AC3EncOptions *opt = &s->options;
1742 1742
 
... ...
@@ -1790,6 +1888,79 @@ static void output_frame_header(AC3EncodeContext *s)
1790 1790
 
1791 1791
 
1792 1792
 /**
1793
+ * Write the E-AC-3 frame header to the output bitstream.
1794
+ */
1795
+static void eac3_output_frame_header(AC3EncodeContext *s)
1796
+{
1797
+    int blk, ch;
1798
+    AC3EncOptions *opt = &s->options;
1799
+
1800
+    put_bits(&s->pb, 16, 0x0b77);                   /* sync word */
1801
+
1802
+    /* BSI header */
1803
+    put_bits(&s->pb,  2, 0);                        /* stream type = independent */
1804
+    put_bits(&s->pb,  3, 0);                        /* substream id = 0 */
1805
+    put_bits(&s->pb, 11, (s->frame_size / 2) - 1);  /* frame size */
1806
+    if (s->bit_alloc.sr_shift) {
1807
+        put_bits(&s->pb, 2, 0x3);                   /* fscod2 */
1808
+        put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
1809
+    } else {
1810
+        put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
1811
+        put_bits(&s->pb, 2, 0x3);                   /* number of blocks = 6 */
1812
+    }
1813
+    put_bits(&s->pb, 3, s->channel_mode);           /* audio coding mode */
1814
+    put_bits(&s->pb, 1, s->lfe_on);                 /* LFE channel indicator */
1815
+    put_bits(&s->pb, 5, s->bitstream_id);           /* bitstream id (EAC3=16) */
1816
+    put_bits(&s->pb, 5, -opt->dialogue_level);      /* dialogue normalization level */
1817
+    put_bits(&s->pb, 1, 0);                         /* no compression gain */
1818
+    put_bits(&s->pb, 1, 0);                         /* no mixing metadata */
1819
+    /* TODO: mixing metadata */
1820
+    put_bits(&s->pb, 1, 0);                         /* no info metadata */
1821
+    /* TODO: info metadata */
1822
+    put_bits(&s->pb, 1, 0);                         /* no additional bit stream info */
1823
+
1824
+    /* frame header */
1825
+    put_bits(&s->pb, 1, 1);                         /* exponent strategy syntax = each block */
1826
+    put_bits(&s->pb, 1, 0);                         /* aht enabled = no */
1827
+    put_bits(&s->pb, 2, 0);                         /* snr offset strategy = 1 */
1828
+    put_bits(&s->pb, 1, 0);                         /* transient pre-noise processing enabled = no */
1829
+    put_bits(&s->pb, 1, 0);                         /* block switch syntax enabled = no */
1830
+    put_bits(&s->pb, 1, 0);                         /* dither flag syntax enabled = no */
1831
+    put_bits(&s->pb, 1, 0);                         /* bit allocation model syntax enabled = no */
1832
+    put_bits(&s->pb, 1, 0);                         /* fast gain codes enabled = no */
1833
+    put_bits(&s->pb, 1, 0);                         /* dba syntax enabled = no */
1834
+    put_bits(&s->pb, 1, 0);                         /* skip field syntax enabled = no */
1835
+    put_bits(&s->pb, 1, 0);                         /* spx enabled = no */
1836
+    /* coupling strategy use flags */
1837
+    if (s->channel_mode > AC3_CHMODE_MONO) {
1838
+        put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
1839
+        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
1840
+            AC3Block *block = &s->blocks[blk];
1841
+            put_bits(&s->pb, 1, block->new_cpl_strategy);
1842
+            if (block->new_cpl_strategy)
1843
+                put_bits(&s->pb, 1, block->cpl_in_use);
1844
+        }
1845
+    }
1846
+    /* exponent strategy */
1847
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1848
+        for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
1849
+            put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
1850
+    if (s->lfe_on) {
1851
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1852
+            put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
1853
+    }
1854
+    /* E-AC-3 to AC-3 converter exponent strategy (unfortunately not optional...) */
1855
+    for (ch = 1; ch <= s->fbw_channels; ch++)
1856
+        put_bits(&s->pb, 5, 0);
1857
+    /* snr offsets */
1858
+    put_bits(&s->pb, 6, s->coarse_snr_offset);
1859
+    put_bits(&s->pb, 4, s->fine_snr_offset[1]);
1860
+    /* block start info */
1861
+    put_bits(&s->pb, 1, 0);
1862
+}
1863
+
1864
+
1865
+/**
1793 1866
  * Write one audio block to the output bitstream.
1794 1867
  */
1795 1868
 static void output_audio_block(AC3EncodeContext *s, int blk)
... ...
@@ -1799,32 +1970,51 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1799 1799
     AC3Block *block = &s->blocks[blk];
1800 1800
 
1801 1801
     /* block switching */
1802
+    if (!s->eac3) {
1802 1803
     for (ch = 0; ch < s->fbw_channels; ch++)
1803 1804
         put_bits(&s->pb, 1, 0);
1805
+    }
1804 1806
 
1805 1807
     /* dither flags */
1808
+    if (!s->eac3) {
1806 1809
     for (ch = 0; ch < s->fbw_channels; ch++)
1807 1810
         put_bits(&s->pb, 1, 1);
1811
+    }
1808 1812
 
1809 1813
     /* dynamic range codes */
1810 1814
     put_bits(&s->pb, 1, 0);
1811 1815
 
1816
+    /* spectral extension */
1817
+    if (s->eac3)
1818
+        put_bits(&s->pb, 1, 0);
1819
+
1812 1820
     /* channel coupling */
1821
+    if (!s->eac3)
1813 1822
     put_bits(&s->pb, 1, block->new_cpl_strategy);
1814 1823
     if (block->new_cpl_strategy) {
1824
+        if (!s->eac3)
1815 1825
         put_bits(&s->pb, 1, block->cpl_in_use);
1816 1826
         if (block->cpl_in_use) {
1817 1827
             int start_sub, end_sub;
1828
+            if (s->eac3)
1829
+                put_bits(&s->pb, 1, 0); /* enhanced coupling */
1830
+            if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
1818 1831
             for (ch = 1; ch <= s->fbw_channels; ch++)
1819 1832
                 put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
1833
+            }
1820 1834
             if (s->channel_mode == AC3_CHMODE_STEREO)
1821 1835
                 put_bits(&s->pb, 1, 0); /* phase flags in use */
1822 1836
             start_sub = (s->start_freq[CPL_CH] - 37) / 12;
1823 1837
             end_sub   = (s->cpl_end_freq       - 37) / 12;
1824 1838
             put_bits(&s->pb, 4, start_sub);
1825 1839
             put_bits(&s->pb, 4, end_sub - 3);
1840
+            /* coupling band structure */
1841
+            if (s->eac3) {
1842
+                put_bits(&s->pb, 1, 0); /* use default */
1843
+            } else {
1826 1844
             for (bnd = start_sub+1; bnd < end_sub; bnd++)
1827 1845
                 put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
1846
+            }
1828 1847
         }
1829 1848
     }
1830 1849
 
... ...
@@ -1832,6 +2022,7 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1832 1832
     if (block->cpl_in_use) {
1833 1833
         for (ch = 1; ch <= s->fbw_channels; ch++) {
1834 1834
             if (block->channel_in_cpl[ch]) {
1835
+                if (!s->eac3 || block->new_cpl_coords != 2)
1835 1836
                 put_bits(&s->pb, 1, block->new_cpl_coords);
1836 1837
                 if (block->new_cpl_coords) {
1837 1838
                     put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
... ...
@@ -1846,6 +2037,7 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1846 1846
 
1847 1847
     /* stereo rematrixing */
1848 1848
     if (s->channel_mode == AC3_CHMODE_STEREO) {
1849
+        if (!s->eac3 || blk > 0)
1849 1850
         put_bits(&s->pb, 1, block->new_rematrixing_strategy);
1850 1851
         if (block->new_rematrixing_strategy) {
1851 1852
             /* rematrixing flags */
... ...
@@ -1855,10 +2047,12 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1855 1855
     }
1856 1856
 
1857 1857
     /* exponent strategy */
1858
+    if (!s->eac3) {
1858 1859
     for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
1859 1860
         put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
1860 1861
     if (s->lfe_on)
1861 1862
         put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
1863
+    }
1862 1864
 
1863 1865
     /* bandwidth */
1864 1866
     for (ch = 1; ch <= s->fbw_channels; ch++) {
... ...
@@ -1888,6 +2082,7 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1888 1888
     }
1889 1889
 
1890 1890
     /* bit allocation info */
1891
+    if (!s->eac3) {
1891 1892
     baie = (blk == 0);
1892 1893
     put_bits(&s->pb, 1, baie);
1893 1894
     if (baie) {
... ...
@@ -1897,8 +2092,10 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1897 1897
         put_bits(&s->pb, 2, s->db_per_bit_code);
1898 1898
         put_bits(&s->pb, 3, s->floor_code);
1899 1899
     }
1900
+    }
1900 1901
 
1901 1902
     /* snr offset */
1903
+    if (!s->eac3) {
1902 1904
     put_bits(&s->pb, 1, block->new_snr_offsets);
1903 1905
     if (block->new_snr_offsets) {
1904 1906
         put_bits(&s->pb, 6, s->coarse_snr_offset);
... ...
@@ -1907,9 +2104,13 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1907 1907
             put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1908 1908
         }
1909 1909
     }
1910
+    } else {
1911
+        put_bits(&s->pb, 1, 0); /* no converter snr offset */
1912
+    }
1910 1913
 
1911 1914
     /* coupling leak */
1912 1915
     if (block->cpl_in_use) {
1916
+        if (!s->eac3 || block->new_cpl_leak != 2)
1913 1917
         put_bits(&s->pb, 1, block->new_cpl_leak);
1914 1918
         if (block->new_cpl_leak) {
1915 1919
             put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
... ...
@@ -1917,8 +2118,10 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
1917 1917
         }
1918 1918
     }
1919 1919
 
1920
+    if (!s->eac3) {
1920 1921
     put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1921 1922
     put_bits(&s->pb, 1, 0); /* no data to skip */
1923
+    }
1922 1924
 
1923 1925
     /* mantissas */
1924 1926
     got_cpl = !block->cpl_in_use;
... ...
@@ -2007,6 +2210,10 @@ static void output_frame_end(AC3EncodeContext *s)
2007 2007
     if (pad_bytes > 0)
2008 2008
         memset(put_bits_ptr(&s->pb), 0, pad_bytes);
2009 2009
 
2010
+    if (s->eac3) {
2011
+        /* compute crc2 */
2012
+        crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
2013
+    } else {
2010 2014
     /* compute crc1 */
2011 2015
     /* this is not so easy because it is at the beginning of the data... */
2012 2016
     crc1    = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
... ...
@@ -2017,6 +2224,7 @@ static void output_frame_end(AC3EncodeContext *s)
2017 2017
     /* compute crc2 */
2018 2018
     crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
2019 2019
                           s->frame_size - frame_size_58 - 3);
2020
+    }
2020 2021
     crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
2021 2022
     /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
2022 2023
     if (crc2 == 0x770B) {
... ...
@@ -2037,7 +2245,10 @@ static void output_frame(AC3EncodeContext *s, unsigned char *frame)
2037 2037
 
2038 2038
     init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
2039 2039
 
2040
-    output_frame_header(s);
2040
+    if (s->eac3)
2041
+        eac3_output_frame_header(s);
2042
+    else
2043
+        ac3_output_frame_header(s);
2041 2044
 
2042 2045
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
2043 2046
         output_audio_block(s, blk);
... ...
@@ -2058,6 +2269,7 @@ static void dprint_options(AVCodecContext *avctx)
2058 2058
     case  8:  av_strlcpy(strbuf, "AC-3 (standard)", 32);        break;
2059 2059
     case  9:  av_strlcpy(strbuf, "AC-3 (dnet half-rate)", 32);  break;
2060 2060
     case 10:  av_strlcpy(strbuf, "AC-3 (dnet quater-rate", 32); break;
2061
+    case 16:  av_strlcpy(strbuf, "E-AC-3 (enhanced)", 32);      break;
2061 2062
     default: snprintf(strbuf, 32, "ERROR");
2062 2063
     }
2063 2064
     av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
... ...
@@ -2318,13 +2530,13 @@ static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
2318 2318
     const SampleType *samples = data;
2319 2319
     int ret;
2320 2320
 
2321
-    if (s->options.allow_per_frame_metadata) {
2321
+    if (!s->eac3 && s->options.allow_per_frame_metadata) {
2322 2322
         ret = validate_metadata(avctx);
2323 2323
         if (ret)
2324 2324
             return ret;
2325 2325
     }
2326 2326
 
2327
-    if (s->bit_alloc.sr_code == 1)
2327
+    if (s->bit_alloc.sr_code == 1 || s->eac3)
2328 2328
         adjust_frame_size(s);
2329 2329
 
2330 2330
     deinterleave_input_samples(s, samples);
... ...
@@ -2450,7 +2662,7 @@ static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
2450 2450
 
2451 2451
 static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2452 2452
 {
2453
-    int i, ret;
2453
+    int i, ret, max_sr;
2454 2454
 
2455 2455
     /* validate channel layout */
2456 2456
     if (!avctx->channel_layout) {
... ...
@@ -2465,20 +2677,59 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2465 2465
     }
2466 2466
 
2467 2467
     /* validate sample rate */
2468
-    for (i = 0; i < 9; i++) {
2469
-        if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
2468
+    /* note: max_sr could be changed from 2 to 5 for E-AC-3 once we find a
2469
+             decoder that supports half sample rate so we can validate that
2470
+             the generated files are correct. */
2471
+    max_sr = s->eac3 ? 2 : 8;
2472
+    for (i = 0; i <= max_sr; i++) {
2473
+        if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)
2470 2474
             break;
2471 2475
     }
2472
-    if (i == 9) {
2476
+    if (i > max_sr) {
2473 2477
         av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
2474 2478
         return AVERROR(EINVAL);
2475 2479
     }
2476 2480
     s->sample_rate        = avctx->sample_rate;
2477
-    s->bit_alloc.sr_shift = i % 3;
2478
-    s->bit_alloc.sr_code  = i / 3;
2479
-    s->bitstream_id       = 8 + s->bit_alloc.sr_shift;
2481
+    s->bit_alloc.sr_shift = i / 3;
2482
+    s->bit_alloc.sr_code  = i % 3;
2483
+    s->bitstream_id       = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;
2480 2484
 
2481 2485
     /* validate bit rate */
2486
+    if (s->eac3) {
2487
+        int max_br, min_br, wpf, min_br_dist, min_br_code;
2488
+
2489
+        /* calculate min/max bitrate */
2490
+        max_br = 2048 * s->sample_rate / AC3_FRAME_SIZE * 16;
2491
+        min_br = ((s->sample_rate + (AC3_FRAME_SIZE-1)) / AC3_FRAME_SIZE) * 16;
2492
+        if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {
2493
+            av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "
2494
+                   "for this sample rate\n", min_br, max_br);
2495
+            return AVERROR(EINVAL);
2496
+        }
2497
+
2498
+        /* calculate words-per-frame for the selected bitrate */
2499
+        wpf = (avctx->bit_rate / 16) * AC3_FRAME_SIZE / s->sample_rate;
2500
+        av_assert1(wpf > 0 && wpf <= 2048);
2501
+
2502
+        /* find the closest AC-3 bitrate code to the selected bitrate.
2503
+           this is needed for lookup tables for bandwidth and coupling
2504
+           parameter selection */
2505
+        min_br_code = -1;
2506
+        min_br_dist = INT_MAX;
2507
+        for (i = 0; i < 19; i++) {
2508
+            int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);
2509
+            if (br_dist < min_br_dist) {
2510
+                min_br_dist = br_dist;
2511
+                min_br_code = i;
2512
+            }
2513
+        }
2514
+
2515
+        /* make sure the minimum frame size is below the average frame size */
2516
+        s->frame_size_code = min_br_code << 1;
2517
+        while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)
2518
+            wpf--;
2519
+        s->frame_size_min = 2 * wpf;
2520
+    } else {
2482 2521
     for (i = 0; i < 19; i++) {
2483 2522
         if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
2484 2523
             break;
... ...
@@ -2487,8 +2738,11 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2487 2487
         av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
2488 2488
         return AVERROR(EINVAL);
2489 2489
     }
2490
-    s->bit_rate        = avctx->bit_rate;
2491 2490
     s->frame_size_code = i << 1;
2491
+    s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
2492
+    }
2493
+    s->bit_rate   = avctx->bit_rate;
2494
+    s->frame_size = s->frame_size_min;
2492 2495
 
2493 2496
     /* validate cutoff */
2494 2497
     if (avctx->cutoff < 0) {
... ...
@@ -2511,9 +2765,11 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
2511 2511
         return AVERROR(EINVAL);
2512 2512
     }
2513 2513
 
2514
+    if (!s->eac3) {
2514 2515
     ret = validate_metadata(avctx);
2515 2516
     if (ret)
2516 2517
         return ret;
2518
+    }
2517 2519
 
2518 2520
     s->rematrixing_enabled = s->options.stereo_rematrixing &&
2519 2521
                              (s->channel_mode == AC3_CHMODE_STEREO);
... ...
@@ -2713,6 +2969,8 @@ static av_cold int ac3_encode_init(AVCodecContext *avctx)
2713 2713
     AC3EncodeContext *s = avctx->priv_data;
2714 2714
     int ret, frame_size_58;
2715 2715
 
2716
+    s->eac3 = avctx->codec_id == CODEC_ID_EAC3;
2717
+
2716 2718
     avctx->frame_size = AC3_FRAME_SIZE;
2717 2719
 
2718 2720
     ff_ac3_common_init();
... ...
@@ -2725,10 +2983,8 @@ static av_cold int ac3_encode_init(AVCodecContext *avctx)
2725 2725
     if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
2726 2726
         s->bitstream_mode = 0x7;
2727 2727
 
2728
-    s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
2729 2728
     s->bits_written    = 0;
2730 2729
     s->samples_written = 0;
2731
-    s->frame_size      = s->frame_size_min;
2732 2730
 
2733 2731
     /* calculate crc_inv for both possible frame sizes */
2734 2732
     frame_size_58 = (( s->frame_size    >> 2) + ( s->frame_size    >> 4)) << 1;
... ...
@@ -100,6 +100,7 @@ static void scale_coefficients(AC3EncodeContext *s)
100 100
 }
101 101
 
102 102
 
103
+#if CONFIG_AC3_ENCODER
103 104
 AVCodec ff_ac3_encoder = {
104 105
     "ac3",
105 106
     AVMEDIA_TYPE_AUDIO,
... ...
@@ -114,3 +115,20 @@ AVCodec ff_ac3_encoder = {
114 114
     .priv_class = &ac3enc_class,
115 115
     .channel_layouts = ac3_channel_layouts,
116 116
 };
117
+#endif
118
+
119
+#if CONFIG_EAC3_ENCODER
120
+AVCodec ff_eac3_encoder = {
121
+    .name            = "eac3",
122
+    .type            = AVMEDIA_TYPE_AUDIO,
123
+    .id              = CODEC_ID_EAC3,
124
+    .priv_data_size  = sizeof(AC3EncodeContext),
125
+    .init            = ac3_encode_init,
126
+    .encode          = ac3_encode_frame,
127
+    .close           = ac3_encode_close,
128
+    .sample_fmts     = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
129
+    .long_name       = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
130
+    .priv_class      = &eac3enc_class,
131
+    .channel_layouts = ac3_channel_layouts,
132
+};
133
+#endif
... ...
@@ -23,7 +23,10 @@
23 23
 static const AVOption ac3fixed_options[] = {
24 24
 #elif AC3ENC_TYPE == AC3ENC_TYPE_AC3
25 25
 static const AVOption ac3_options[] = {
26
+#else /* AC3ENC_TYPE_EAC3 */
27
+static const AVOption eac3_options[] = {
26 28
 #endif
29
+#if AC3ENC_TYPE != AC3ENC_TYPE_EAC3
27 30
 /* Metadata Options */
28 31
 {"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, AC3ENC_PARAM},
29 32
 /* downmix levels */
... ...
@@ -37,7 +40,9 @@ static const AVOption ac3_options[] = {
37 37
     {"small",        "Small Room",              0, FF_OPT_TYPE_CONST, {.dbl = 2 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
38 38
 /* other metadata options */
39 39
 {"copyright", "Copyright Bit", OFFSET(copyright), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, AC3ENC_PARAM},
40
+#endif
40 41
 {"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), FF_OPT_TYPE_INT, {.dbl = -31 }, -31, -1, AC3ENC_PARAM},
42
+#if AC3ENC_TYPE != AC3ENC_TYPE_EAC3
41 43
 {"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, 2, AC3ENC_PARAM, "dsur_mode"},
42 44
     {"notindicated", "Not Indicated (default)",    0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
43 45
     {"on",           "Dolby Surround Encoded",     0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
... ...
@@ -63,6 +68,7 @@ static const AVOption ac3_options[] = {
63 63
 {"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), FF_OPT_TYPE_INT, {.dbl = -1 }, -1, 1, AC3ENC_PARAM, "ad_conv_type"},
64 64
     {"standard", "Standard (default)", 0, FF_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
65 65
     {"hdcd",     "HDCD",               0, FF_OPT_TYPE_CONST, {.dbl = 1 }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
66
+#endif
66 67
 /* Other Encoding Options */
67 68
 {"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), FF_OPT_TYPE_INT, {.dbl = 1 }, 0, 1, AC3ENC_PARAM},
68 69
 #if AC3ENC_TYPE != AC3ENC_TYPE_AC3_FIXED
... ...
@@ -241,7 +241,7 @@ void avcodec_register_all(void)
241 241
     REGISTER_DECODER (COOK, cook);
242 242
     REGISTER_DECODER (DCA, dca);
243 243
     REGISTER_DECODER (DSICINAUDIO, dsicinaudio);
244
-    REGISTER_DECODER (EAC3, eac3);
244
+    REGISTER_ENCDEC  (EAC3, eac3);
245 245
     REGISTER_ENCDEC  (FLAC, flac);
246 246
     REGISTER_DECODER (GSM, gsm);
247 247
     REGISTER_DECODER (GSM_MS, gsm_ms);
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR  4
24
+#define LIBAVCODEC_VERSION_MINOR  5
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \