[01/10] ac3: Use local contexts

Message ID 1468855510-71020-2-git-send-email-lu_zero@gentoo.org
State New
Headers show

Commit Message

Luca Barbato July 18, 2016, 3:25 p.m.
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
---
 libavcodec/ac3_parser.c | 45 ++++++++++++++++---------------
 libavcodec/ac3dec.c     | 70 ++++++++++++++++++++++++++-----------------------
 2 files changed, 61 insertions(+), 54 deletions(-)

Patch

diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c
index a7db17e..66dc27f 100644
--- a/libavcodec/ac3_parser.c
+++ b/libavcodec/ac3_parser.c
@@ -48,18 +48,19 @@  static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
 static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
 
 
-int avpriv_ac3_parse_header(BitstreamContext *bc, AC3HeaderInfo *hdr)
+int avpriv_ac3_parse_header(BitstreamContext *bcp, AC3HeaderInfo *hdr)
 {
+    BitstreamContext bc = *bcp;
     int frame_size_code;
 
     memset(hdr, 0, sizeof(*hdr));
 
-    hdr->sync_word = bitstream_read(bc, 16);
+    hdr->sync_word = bitstream_read(&bc, 16);
     if(hdr->sync_word != 0x0B77)
         return AAC_AC3_PARSE_ERROR_SYNC;
 
     /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
-    hdr->bitstream_id = bitstream_peek(bc, 29) & 0x1F;
+    hdr->bitstream_id = bitstream_peek(&bc, 29) & 0x1F;
     if(hdr->bitstream_id > 16)
         return AAC_AC3_PARSE_ERROR_BSID;
 
@@ -74,29 +75,29 @@  int avpriv_ac3_parse_header(BitstreamContext *bc, AC3HeaderInfo *hdr)
 
     if(hdr->bitstream_id <= 10) {
         /* Normal AC-3 */
-        hdr->crc1    = bitstream_read(bc, 16);
-        hdr->sr_code = bitstream_read(bc,  2);
+        hdr->crc1    = bitstream_read(&bc, 16);
+        hdr->sr_code = bitstream_read(&bc,  2);
         if(hdr->sr_code == 3)
             return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
 
-        frame_size_code = bitstream_read(bc, 6);
+        frame_size_code = bitstream_read(&bc, 6);
         if(frame_size_code > 37)
             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
 
-        bitstream_skip(bc, 5); // skip bsid, already got it
+        bitstream_skip(&bc, 5); // skip bsid, already got it
 
-        hdr->bitstream_mode = bitstream_read(bc, 3);
-        hdr->channel_mode   = bitstream_read(bc, 3);
+        hdr->bitstream_mode = bitstream_read(&bc, 3);
+        hdr->channel_mode   = bitstream_read(&bc, 3);
 
         if(hdr->channel_mode == AC3_CHMODE_STEREO) {
-            hdr->dolby_surround_mode = bitstream_read(bc, 2);
+            hdr->dolby_surround_mode = bitstream_read(&bc, 2);
         } else {
             if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
-                hdr->center_mix_level   = center_levels[bitstream_read(bc, 2)];
+                hdr->center_mix_level   = center_levels[bitstream_read(&bc, 2)];
             if(hdr->channel_mode & 4)
-                hdr->surround_mix_level = surround_levels[bitstream_read(bc, 2)];
+                hdr->surround_mix_level = surround_levels[bitstream_read(&bc, 2)];
         }
-        hdr->lfe_on = bitstream_read_bit(bc);
+        hdr->lfe_on = bitstream_read_bit(&bc);
 
         hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
         hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
@@ -108,31 +109,31 @@  int avpriv_ac3_parse_header(BitstreamContext *bc, AC3HeaderInfo *hdr)
     } else {
         /* Enhanced AC-3 */
         hdr->crc1 = 0;
-        hdr->frame_type = bitstream_read(bc, 2);
+        hdr->frame_type = bitstream_read(&bc, 2);
         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
             return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
 
-        hdr->substreamid = bitstream_read(bc, 3);
+        hdr->substreamid = bitstream_read(&bc, 3);
 
-        hdr->frame_size = (bitstream_read(bc, 11) + 1) << 1;
+        hdr->frame_size = (bitstream_read(&bc, 11) + 1) << 1;
         if(hdr->frame_size < AC3_HEADER_SIZE)
             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
 
-        hdr->sr_code = bitstream_read(bc, 2);
+        hdr->sr_code = bitstream_read(&bc, 2);
         if (hdr->sr_code == 3) {
-            int sr_code2 = bitstream_read(bc, 2);
+            int sr_code2 = bitstream_read(&bc, 2);
             if(sr_code2 == 3)
                 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
             hdr->sr_shift = 1;
         } else {
-            hdr->num_blocks = eac3_blocks[bitstream_read(bc, 2)];
+            hdr->num_blocks = eac3_blocks[bitstream_read(&bc, 2)];
             hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
             hdr->sr_shift = 0;
         }
 
-        hdr->channel_mode = bitstream_read(bc, 3);
-        hdr->lfe_on       = bitstream_read_bit(bc);
+        hdr->channel_mode = bitstream_read(&bc, 3);
+        hdr->lfe_on       = bitstream_read_bit(&bc);
 
         hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
                         (hdr->num_blocks * 256.0));
@@ -142,6 +143,8 @@  int avpriv_ac3_parse_header(BitstreamContext *bc, AC3HeaderInfo *hdr)
     if (hdr->lfe_on)
         hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
 
+    *bcp = bc;
+
     return 0;
 }
 
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index b1a3c8d..62e9868 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -213,52 +213,54 @@  static av_cold int ac3_decode_init(AVCodecContext *avctx)
  */
 static int ac3_parse_header(AC3DecodeContext *s)
 {
-    BitstreamContext *bc = &s->bc;
+    BitstreamContext bc = s->bc;
     int i;
 
     /* read the rest of the bsi. read twice for dual mono mode. */
     i = !s->channel_mode;
     do {
-        bitstream_skip(bc, 5); // skip dialog normalization
-        if (bitstream_read_bit(bc))
-            bitstream_skip(bc, 8); // skip compression
-        if (bitstream_read_bit(bc))
-            bitstream_skip(bc, 8); // skip language code
-        if (bitstream_read_bit(bc))
-            bitstream_skip(bc, 7); // skip audio production information
+        bitstream_skip(&bc, 5); // skip dialog normalization
+        if (bitstream_read_bit(&bc))
+            bitstream_skip(&bc, 8); // skip compression
+        if (bitstream_read_bit(&bc))
+            bitstream_skip(&bc, 8); // skip language code
+        if (bitstream_read_bit(&bc))
+            bitstream_skip(&bc, 7); // skip audio production information
     } while (i--);
 
-    bitstream_skip(bc, 2); // skip copyright bit and original bitstream bit
+    bitstream_skip(&bc, 2); // skip copyright bit and original bitstream bit
 
     /* skip the timecodes or parse the Alternate Bit Stream Syntax */
     if (s->bitstream_id != 6) {
-        if (bitstream_read_bit(bc))
-            bitstream_skip(bc, 14); // skip timecode1
-        if (bitstream_read_bit(bc))
-            bitstream_skip(bc, 14); // skip timecode2
+        if (bitstream_read_bit(&bc))
+            bitstream_skip(&bc, 14); // skip timecode1
+        if (bitstream_read_bit(&bc))
+            bitstream_skip(&bc, 14); // skip timecode2
     } else {
-        if (bitstream_read_bit(bc)) {
-            s->preferred_downmix       = bitstream_read(bc, 2);
-            s->center_mix_level_ltrt   = bitstream_read(bc, 3);
-            s->surround_mix_level_ltrt = av_clip(bitstream_read(bc, 3), 3, 7);
-            s->center_mix_level        = bitstream_read(bc, 3);
-            s->surround_mix_level      = av_clip(bitstream_read(bc, 3), 3, 7);
+        if (bitstream_read_bit(&bc)) {
+            s->preferred_downmix       = bitstream_read(&bc, 2);
+            s->center_mix_level_ltrt   = bitstream_read(&bc, 3);
+            s->surround_mix_level_ltrt = av_clip(bitstream_read(&bc, 3), 3, 7);
+            s->center_mix_level        = bitstream_read(&bc, 3);
+            s->surround_mix_level      = av_clip(bitstream_read(&bc, 3), 3, 7);
         }
-        if (bitstream_read_bit(bc)) {
-            s->dolby_surround_ex_mode = bitstream_read(bc, 2);
-            s->dolby_headphone_mode   = bitstream_read(bc, 2);
-            bitstream_skip(bc, 10); // skip adconvtyp (1), xbsi2 (8), encinfo (1)
+        if (bitstream_read_bit(&bc)) {
+            s->dolby_surround_ex_mode = bitstream_read(&bc, 2);
+            s->dolby_headphone_mode   = bitstream_read(&bc, 2);
+            bitstream_skip(&bc, 10); // skip adconvtyp (1), xbsi2 (8), encinfo (1)
         }
     }
 
     /* skip additional bitstream info */
-    if (bitstream_read_bit(bc)) {
-        i = bitstream_read(bc, 6);
+    if (bitstream_read_bit(&bc)) {
+        i = bitstream_read(&bc, 6);
         do {
-            bitstream_skip(bc, 8);
+            bitstream_skip(&bc, 8);
         } while (i--);
     }
 
+    s->bc = bc;
+
     return 0;
 }
 
@@ -465,7 +467,7 @@  static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
     int8_t *exps   = s->dexps[ch_index];
     int32_t *coeffs = s->fixed_coeffs[ch_index];
     int dither     = (ch_index == CPL_CH) || s->dither_flag[ch_index];
-    BitstreamContext *bc = &s->bc;
+    BitstreamContext bc = s->bc;
     int freq;
 
     for (freq = start_freq; freq < end_freq; freq++) {
@@ -484,7 +486,7 @@  static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 m->b1--;
                 mantissa = m->b1_mant[m->b1];
             } else {
-                int bits      = bitstream_read(bc, 5);
+                int bits      = bitstream_read(&bc, 5);
                 mantissa      = b1_mantissas[bits][0];
                 m->b1_mant[1] = b1_mantissas[bits][1];
                 m->b1_mant[0] = b1_mantissas[bits][2];
@@ -496,7 +498,7 @@  static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 m->b2--;
                 mantissa = m->b2_mant[m->b2];
             } else {
-                int bits      = bitstream_read(bc, 7);
+                int bits      = bitstream_read(&bc, 7);
                 mantissa      = b2_mantissas[bits][0];
                 m->b2_mant[1] = b2_mantissas[bits][1];
                 m->b2_mant[0] = b2_mantissas[bits][2];
@@ -504,30 +506,32 @@  static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
             }
             break;
         case 3:
-            mantissa = b3_mantissas[bitstream_read(bc, 3)];
+            mantissa = b3_mantissas[bitstream_read(&bc, 3)];
             break;
         case 4:
             if (m->b4) {
                 m->b4 = 0;
                 mantissa = m->b4_mant;
             } else {
-                int bits   = bitstream_read(bc, 7);
+                int bits   = bitstream_read(&bc, 7);
                 mantissa   = b4_mantissas[bits][0];
                 m->b4_mant = b4_mantissas[bits][1];
                 m->b4      = 1;
             }
             break;
         case 5:
-            mantissa = b5_mantissas[bitstream_read(bc, 4)];
+            mantissa = b5_mantissas[bitstream_read(&bc, 4)];
             break;
         default: /* 6 to 15 */
             /* Shift mantissa and sign-extend it. */
-            mantissa = bitstream_read_signed(bc, quantization_tab[bap]);
+            mantissa = bitstream_read_signed(&bc, quantization_tab[bap]);
             mantissa <<= 24 - quantization_tab[bap];
             break;
         }
         coeffs[freq] = mantissa >> exps[freq];
     }
+
+    s->bc = bc;
 }
 
 /**