]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3dec.c
Simplify rms(): merge a few operations in the same statement
[ffmpeg] / libavcodec / ac3dec.c
index 627e38cecb5ff4a75cd645400d5f391118e4a088..d04209df2e507b501c46c5e36b379d480e0a3028 100644 (file)
 #include "bitstream.h"
 #include "dsputil.h"
 #include "ac3dec.h"
+#include "ac3dec_data.h"
 
 /** Maximum possible frame size when the specification limit is ignored */
 #define AC3_MAX_FRAME_SIZE 21695
 
 /**
- * Table of bin locations for rematrixing bands
- * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
+ * table for ungrouping 3 values in 7 bits.
+ * used for exponents and bap=2 mantissas
  */
-static const uint8_t rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
-
-/** table for grouping exponents */
-static uint8_t exp_ungroup_tab[128][3];
+static uint8_t ungroup_3_in_7_bits_tab[128][3];
 
 
 /** tables for ungrouping mantissas */
@@ -140,19 +138,27 @@ static av_cold void ac3_tables_init(void)
 {
     int i;
 
+    /* generate table for ungrouping 3 values in 7 bits
+       reference: Section 7.1.3 Exponent Decoding */
+    for(i=0; i<128; i++) {
+        ungroup_3_in_7_bits_tab[i][0] =  i / 25;
+        ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
+        ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
+    }
+
     /* generate grouped mantissa tables
        reference: Section 7.3.5 Ungrouping of Mantissas */
     for(i=0; i<32; i++) {
         /* bap=1 mantissas */
-        b1_mantissas[i][0] = symmetric_dequant( i / 9     , 3);
-        b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
-        b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
+        b1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
+        b1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
+        b1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
     }
     for(i=0; i<128; i++) {
         /* bap=2 mantissas */
-        b2_mantissas[i][0] = symmetric_dequant( i / 25     , 5);
-        b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
-        b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
+        b2_mantissas[i][0] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][0], 5);
+        b2_mantissas[i][1] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][1], 5);
+        b2_mantissas[i][2] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][2], 5);
 
         /* bap=4 mantissas */
         b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
@@ -175,14 +181,6 @@ static av_cold void ac3_tables_init(void)
         int v = (i >> 5) - ((i >> 7) << 3) - 5;
         dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
     }
-
-    /* generate exponent tables
-       reference: Section 7.1.3 Exponent Decoding */
-    for(i=0; i<128; i++) {
-        exp_ungroup_tab[i][0] =  i / 25;
-        exp_ungroup_tab[i][1] = (i % 25) / 5;
-        exp_ungroup_tab[i][2] = (i % 25) % 5;
-    }
 }
 
 
@@ -226,13 +224,14 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx)
             return AVERROR_NOMEM;
     }
 
+    avctx->sample_fmt = SAMPLE_FMT_S16;
     return 0;
 }
 
 /**
  * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream.
  * GetBitContext within AC3DecodeContext must point to
- * start of the synchronized ac3 bitstream.
+ * the start of the synchronized AC-3 bitstream.
  */
 static int ac3_parse_header(AC3DecodeContext *s)
 {
@@ -272,21 +271,17 @@ static int ac3_parse_header(AC3DecodeContext *s)
 }
 
 /**
- * Common function to parse AC3 or E-AC3 frame header
+ * Common function to parse AC-3 or E-AC-3 frame header
  */
 static int parse_frame_header(AC3DecodeContext *s)
 {
     AC3HeaderInfo hdr;
-    GetBitContext *gbc = &s->gbc;
     int err;
 
-    err = ff_ac3_parse_header(gbc, &hdr);
+    err = ff_ac3_parse_header(&s->gbc, &hdr);
     if(err)
         return err;
 
-    if(hdr.bitstream_id > 10)
-        return AC3_PARSE_ERROR_BSID;
-
     /* get decoding parameters from header info */
     s->bit_alloc_params.sr_code     = hdr.sr_code;
     s->channel_mode                 = hdr.channel_mode;
@@ -311,6 +306,9 @@ static int parse_frame_header(AC3DecodeContext *s)
         s->channel_in_cpl[s->lfe_ch] = 0;
     }
 
+    if(hdr.bitstream_id > 10)
+        return AC3_PARSE_ERROR_BSID;
+
     return ac3_parse_header(s);
 }
 
@@ -365,9 +363,9 @@ static void decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
     group_size = exp_strategy + (exp_strategy == EXP_D45);
     for(grp=0,i=0; grp<ngrps; grp++) {
         expacc = get_bits(gbc, 7);
-        dexp[i++] = exp_ungroup_tab[expacc][0];
-        dexp[i++] = exp_ungroup_tab[expacc][1];
-        dexp[i++] = exp_ungroup_tab[expacc][2];
+        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][0];
+        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][1];
+        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][2];
     }
 
     /* convert to absolute exps and expand groups */
@@ -385,7 +383,7 @@ static void decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
  * range using the coupling coefficients and coupling coordinates.
  * reference: Section 7.4.3 Coupling Coordinate Format
  */
-static void uncouple_channels(AC3DecodeContext *s)
+static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
 {
     int i, j, ch, bnd, subbnd;
 
@@ -442,7 +440,7 @@ static void get_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_grou
         tbap = bap[i];
         switch (tbap) {
             case 0:
-                coeffs[i] = (av_random(&s->dith_state) & 0x7FFFFF) - 4194304;
+                coeffs[i] = (av_random(&s->dith_state) & 0x7FFFFF) - 0x400000;
                 break;
 
             case 1:
@@ -548,7 +546,7 @@ static void get_transform_coeffs(AC3DecodeContext *s)
         if (s->channel_in_cpl[ch])  {
             if (!got_cplchan) {
                 get_transform_coeffs_ch(s, CPL_CH, &m);
-                uncouple_channels(s);
+                calc_transform_coeffs_cpl(s);
                 got_cplchan = 1;
             }
             end = s->end_freq[CPL_CH];
@@ -579,8 +577,8 @@ static void do_rematrixing(AC3DecodeContext *s)
 
     for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) {
         if(s->rematrixing_flags[bnd]) {
-            bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
-            for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
+            bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd+1]);
+            for(i=ff_ac3_rematrix_band_tab[bnd]; i<bndend; i++) {
                 tmp0 = s->fixed_coeffs[1][i];
                 tmp1 = s->fixed_coeffs[2][i];
                 s->fixed_coeffs[1][i] = tmp0 + tmp1;
@@ -713,9 +711,9 @@ static void ac3_upmix_delay(AC3DecodeContext *s)
 }
 
 /**
- * Parse an audio block from AC-3 bitstream.
+ * Decode a single audio block from the AC-3 bitstream.
  */
-static int ac3_parse_audio_block(AC3DecodeContext *s, int blk)
+static int decode_audio_block(AC3DecodeContext *s, int blk)
 {
     int fbw_channels = s->fbw_channels;
     int channel_mode = s->channel_mode;
@@ -758,8 +756,8 @@ static int ac3_parse_audio_block(AC3DecodeContext *s, int blk)
     /* coupling strategy */
     if (get_bits1(gbc)) {
         memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
-        cpl_in_use = get_bits1(gbc);
-        if (cpl_in_use) {
+        s->cpl_in_use[blk] = get_bits1(gbc);
+        if (s->cpl_in_use[blk]) {
             /* coupling in use */
             int cpl_begin_freq, cpl_end_freq;
 
@@ -802,9 +800,9 @@ static int ac3_parse_audio_block(AC3DecodeContext *s, int blk)
         av_log(s->avctx, AV_LOG_ERROR, "new coupling strategy must be present in block 0\n");
         return -1;
     } else {
-        cpl_in_use = s->cpl_in_use[blk-1];
+        s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
     }
-    s->cpl_in_use[blk] = cpl_in_use;
+    cpl_in_use = s->cpl_in_use[blk];
 
     /* coupling coordinates */
     if (cpl_in_use) {
@@ -1166,10 +1164,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
             s->output_mode  = s->out_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
     }
 
-    /* parse the audio blocks */
+    /* decode the audio blocks */
     for (blk = 0; blk < s->num_blocks; blk++) {
-        if (!err && ac3_parse_audio_block(s, blk)) {
-            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
+        if (!err && decode_audio_block(s, blk)) {
+            av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");
         }
 
         /* interleave output samples */
@@ -1203,5 +1201,5 @@ AVCodec ac3_decoder = {
     .init = ac3_decode_init,
     .close = ac3_decode_end,
     .decode = ac3_decode_frame,
-    .long_name = "ATSC A/52 / AC-3",
+    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 / AC-3"),
 };