Quoting Diego Biurrun (2016-06-09 17:11:48)
> From: Alexandra Hájková <alexan...@khirnov.net>
> 
> ---
>  libavcodec/eatqi.c     |   8 +-
>  libavcodec/mdec.c      |  34 ++--
>  libavcodec/mpeg12.c    |  31 ++--
>  libavcodec/mpeg12.h    |  12 +-
>  libavcodec/mpeg12dec.c | 415 
> ++++++++++++++++++++++---------------------------
>  libavcodec/mpegvideo.h |   3 +
>  6 files changed, 227 insertions(+), 276 deletions(-)
> 
> diff --git a/libavcodec/eatqi.c b/libavcodec/eatqi.c
> index f4cad9c..f071820 100644
> --- a/libavcodec/eatqi.c
> +++ b/libavcodec/eatqi.c
> @@ -27,9 +27,9 @@
>   */
>  
>  #include "avcodec.h"
> +#include "bitstream.h"
>  #include "blockdsp.h"
>  #include "bswapdsp.h"
> -#include "get_bits.h"
>  #include "aandcttab.h"
>  #include "eaidct.h"
>  #include "idctdsp.h"
> @@ -37,7 +37,7 @@
>  #include "mpeg12.h"
>  
>  typedef struct TqiContext {
> -    GetBitContext gb;
> +    BitstreamContext bc;
>      BlockDSPContext bdsp;
>      BswapDSPContext bsdsp;
>      IDCTDSPContext idsp;
> @@ -75,7 +75,7 @@ static int tqi_decode_mb(TqiContext *t, int16_t 
> (*block)[64])
>  
>      t->bdsp.clear_blocks(block[0]);
>      for (n = 0; n < 6; n++) {
> -        int ret = ff_mpeg1_decode_block_intra(&t->gb,
> +        int ret = ff_mpeg1_decode_block_intra(&t->bc,
>                                                t->intra_matrix,
>                                                t->intra_scantable.permutated,
>                                                t->last_dc, block[n], n, 1);
> @@ -147,7 +147,7 @@ static int tqi_decode_frame(AVCodecContext *avctx,
>          return AVERROR(ENOMEM);
>      t->bsdsp.bswap_buf(t->bitstream_buf, (const uint32_t *) buf,
>                         (buf_end - buf) / 4);
> -    init_get_bits(&t->gb, t->bitstream_buf, 8 * (buf_end - buf));
> +    bitstream_init8(&t->bc, t->bitstream_buf, buf_end - buf);
>  
>      t->last_dc[0] =
>      t->last_dc[1] =
> diff --git a/libavcodec/mdec.c b/libavcodec/mdec.c
> index 4b6056e..51c0e57 100644
> --- a/libavcodec/mdec.c
> +++ b/libavcodec/mdec.c

It's not clear to me why mdec is here, since it does not seem to depend
on mpeg2.

> @@ -28,17 +28,19 @@
>   */
>  
>  #include "avcodec.h"
> +#include "bitstream.h"
>  #include "blockdsp.h"
>  #include "idctdsp.h"
>  #include "mpeg12.h"
>  #include "thread.h"
> +#include "vlc.h"
>  
>  typedef struct MDECContext {
>      AVCodecContext *avctx;
>      BlockDSPContext bdsp;
>      IDCTDSPContext idsp;
>      ThreadFrame frame;
> -    GetBitContext gb;
> +    BitstreamContext bc;
>      ScanTable scantable;
>      int version;
>      int qscale;
> @@ -64,10 +66,10 @@ static inline int mdec_decode_block_intra(MDECContext *a, 
> int16_t *block, int n)
>  
>      /* DC coefficient */
>      if (a->version == 2) {
> -        block[0] = 2 * get_sbits(&a->gb, 10) + 1024;
> +        block[0] = 2 * bitstream_read_signed(&a->bc, 10) + 1024;
>      } else {
>          component = (n <= 3 ? 0 : n - 4 + 1);
> -        diff = decode_dc(&a->gb, component);
> +        diff = decode_dc(&a->bc, component);
>          if (diff >= 0xffff)
>              return AVERROR_INVALIDDATA;
>          a->last_dc[component] += diff;
> @@ -76,11 +78,9 @@ static inline int mdec_decode_block_intra(MDECContext *a, 
> int16_t *block, int n)
>  
>      i = 0;
>      {
> -        OPEN_READER(re, &a->gb);
>          /* now quantify & encode AC coefficients */
>          for (;;) {
> -            UPDATE_CACHE(re, &a->gb);
> -            GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 
> 2, 0);
> +            BITSTREAM_RL_VLC(level, run, &a->bc, rl->rl_vlc[0], 
> TEX_VLC_BITS, 2);
>  
>              if (level == 127) {
>                  break;
> @@ -93,13 +93,12 @@ static inline int mdec_decode_block_intra(MDECContext *a, 
> int16_t *block, int n)
>                  }
>                  j     = scantable[i];
>                  level = (level * qscale * quant_matrix[j]) >> 3;
> -                level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, 
> &a->gb, 1);
> -                LAST_SKIP_BITS(re, &a->gb, 1);
> +                level = (level ^ bitstream_peek_signed(&a->bc, 1)) - 
> bitstream_peek_signed(&a->bc, 1);
> +                bitstream_skip(&a->bc, 1);

Exactly the same as my previous comment in eamad.

>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, &a->gb, 6)+1; LAST_SKIP_BITS(re, 
> &a->gb, 6);
> -                UPDATE_CACHE(re, &a->gb);
> -                level = SHOW_SBITS(re, &a->gb, 10); SKIP_BITS(re, &a->gb, 
> 10);
> +                run = bitstream_read(&a->bc, 6) + 1;
> +                level = bitstream_read_signed(&a->bc, 10);
>                  i += run;
>                  if (i > 63) {
>                      av_log(a->avctx, AV_LOG_ERROR,
> @@ -120,7 +119,6 @@ static inline int mdec_decode_block_intra(MDECContext *a, 
> int16_t *block, int n)
>  
>              block[j] = level;
>          }
> -        CLOSE_READER(re, &a->gb);
>      }
>      a->block_last_index[n] = i;
>      return 0;
> @@ -137,7 +135,7 @@ static inline int decode_mb(MDECContext *a, int16_t 
> block[6][64])
>          if ((ret = mdec_decode_block_intra(a, block[block_index[i]],
>                                             block_index[i])) < 0)
>              return ret;
> -        if (get_bits_left(&a->gb) < 0)
> +        if (bitstream_bits_left(&a->bc) < 0)
>              return AVERROR_INVALIDDATA;
>      }
>      return 0;
> @@ -187,13 +185,13 @@ static int decode_frame(AVCodecContext *avctx,
>          a->bitstream_buffer[i]     = buf[i + 1];
>          a->bitstream_buffer[i + 1] = buf[i];
>      }
> -    init_get_bits(&a->gb, a->bitstream_buffer, buf_size * 8);
> +    bitstream_init8(&a->bc, a->bitstream_buffer, buf_size);
>  
>      /* skip over 4 preamble bytes in stream (typically 0xXX 0xXX 0x00 0x38) 
> */
> -    skip_bits(&a->gb, 32);
> +    bitstream_skip(&a->bc, 32);
>  
> -    a->qscale  = get_bits(&a->gb, 16);
> -    a->version = get_bits(&a->gb, 16);
> +    a->qscale  = bitstream_read(&a->bc, 16);
> +    a->version = bitstream_read(&a->bc, 16);
>  
>      a->last_dc[0] = a->last_dc[1] = a->last_dc[2] = 128;
>  
> @@ -208,7 +206,7 @@ static int decode_frame(AVCodecContext *avctx,
>  
>      *got_frame = 1;
>  
> -    return (get_bits_count(&a->gb) + 31) / 32 * 4;
> +    return (bitstream_tell(&a->bc) + 31) / 32 * 4;
>  }
>  
>  static av_cold int decode_init(AVCodecContext *avctx)
> diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
> index c0c680d..c3f5f32 100644
> --- a/libavcodec/mpeg12.c
> +++ b/libavcodec/mpeg12.c
> @@ -26,8 +26,10 @@
>   */
>  
>  #include "libavutil/attributes.h"
> +
>  #include "internal.h"
>  #include "avcodec.h"
> +#include "bitstream.h"
>  #include "mpegvideo.h"
>  #include "error_resilience.h"
>  #include "mpeg12.h"
> @@ -35,6 +37,7 @@
>  #include "mpegvideodata.h"
>  #include "bytestream.h"
>  #include "thread.h"
> +#include "vlc.h"
>  
>  uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
>  
> @@ -239,7 +242,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const 
> uint8_t *buf, int buf_size,
>  
>  #define MAX_INDEX (64 - 1)
>  
> -int ff_mpeg1_decode_block_intra(GetBitContext *gb,
> +int ff_mpeg1_decode_block_intra(BitstreamContext *bc,
>                                  const uint16_t *quant_matrix,
>                                  uint8_t *const scantable, int last_dc[3],
>                                  int16_t *block, int index, int qscale)
> @@ -250,7 +253,7 @@ int ff_mpeg1_decode_block_intra(GetBitContext *gb,
>      /* DC coefficient */
>      component = index <= 3 ? 0 : index - 4 + 1;
>  
> -    diff = decode_dc(gb, component);
> +    diff = decode_dc(bc, component);
>      if (diff >= 0xffff)
>          return AVERROR_INVALIDDATA;
>  
> @@ -261,13 +264,11 @@ int ff_mpeg1_decode_block_intra(GetBitContext *gb,
>      block[0] = dc * quant_matrix[0];
>  
>      {
> -        OPEN_READER(re, gb);
>          /* now quantify & encode AC coefficients */
>          while (1) {
>              int level, run, j;
>  
> -            UPDATE_CACHE(re, gb);
> -            GET_RL_VLC(level, run, re, gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 
> 0);
> +            BITSTREAM_RL_VLC(level, run, bc, rl->rl_vlc[0], TEX_VLC_BITS, 2);
>  
>              if (level == 127) {
>                  break;
> @@ -279,23 +280,18 @@ int ff_mpeg1_decode_block_intra(GetBitContext *gb,
>                  j = scantable[i];
>                  level = (level * qscale * quant_matrix[j]) >> 4;
>                  level = (level - 1) | 1;
> -                level = (level ^ SHOW_SBITS(re, gb, 1)) -
> -                        SHOW_SBITS(re, gb, 1);
> -                LAST_SKIP_BITS(re, gb, 1);
> +                level = (level ^ bitstream_peek_signed(bc, 1)) -
> +                        bitstream_peek_signed(bc, 1);
> +                bitstream_skip(bc, 1);

And again. Maybe we should have a function for this.

>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, gb, 6) + 1;
> -                LAST_SKIP_BITS(re, gb, 6);
> -                UPDATE_CACHE(re, gb);
> -                level = SHOW_SBITS(re, gb, 8);
> -                SKIP_BITS(re, gb, 8);
> +                run = bitstream_read(bc, 6) + 1;
> +                level = bitstream_read_signed(bc, 8);
>  
>                  if (level == -128) {
> -                    level = SHOW_UBITS(re, gb, 8) - 256;
> -                    LAST_SKIP_BITS(re, gb, 8);
> +                    level = bitstream_read(bc, 8) - 256;
>                  } else if (level == 0) {
> -                    level = SHOW_UBITS(re, gb, 8);
> -                    LAST_SKIP_BITS(re, gb, 8);
> +                    level = bitstream_read(bc, 8);
>                  }
>  
>                  i += run;
> @@ -316,7 +312,6 @@ int ff_mpeg1_decode_block_intra(GetBitContext *gb,
>  
>              block[j] = level;
>          }
> -        CLOSE_READER(re, gb);
>      }
>  
>      if (i > MAX_INDEX)
> diff --git a/libavcodec/mpeg12.h b/libavcodec/mpeg12.h
> index 17f0b78..588ac03 100644
> --- a/libavcodec/mpeg12.h
> +++ b/libavcodec/mpeg12.h
> @@ -22,21 +22,23 @@
>  #ifndef AVCODEC_MPEG12_H
>  #define AVCODEC_MPEG12_H
>  
> +#include "bitstream.h"
>  #include "mpeg12vlc.h"
>  #include "mpegvideo.h"
> +#include "vlc.h"
>  
>  extern uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 
> 3];
>  
>  void ff_mpeg12_common_init(MpegEncContext *s);
>  
> -static inline int decode_dc(GetBitContext *gb, int component)
> +static inline int decode_dc(BitstreamContext *bc, int component)
>  {
>      int code, diff;
>  
>      if (component == 0) {
> -        code = get_vlc2(gb, ff_dc_lum_vlc.table, DC_VLC_BITS, 2);
> +        code = bitstream_read_vlc(bc, ff_dc_lum_vlc.table, DC_VLC_BITS, 2);
>      } else {
> -        code = get_vlc2(gb, ff_dc_chroma_vlc.table, DC_VLC_BITS, 2);
> +        code = bitstream_read_vlc(bc, ff_dc_chroma_vlc.table, DC_VLC_BITS, 
> 2);
>      }
>      if (code < 0){
>          av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
> @@ -45,12 +47,12 @@ static inline int decode_dc(GetBitContext *gb, int 
> component)
>      if (code == 0) {
>          diff = 0;
>      } else {
> -        diff = get_xbits(gb, code);
> +        diff = bitstream_read_xbits(bc, code);
>      }
>      return diff;
>  }
>  
> -int ff_mpeg1_decode_block_intra(GetBitContext *gb,
> +int ff_mpeg1_decode_block_intra(BitstreamContext *bc,
>                                  const uint16_t *quant_matrix,
>                                  uint8_t *const scantable, int last_dc[3],
>                                  int16_t *block, int index, int qscale);
> diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
> index 2d9c99d..c8a3dda 100644
> --- a/libavcodec/mpeg12dec.c
> +++ b/libavcodec/mpeg12dec.c
> @@ -32,6 +32,7 @@
>  #include "libavutil/stereo3d.h"
>  
>  #include "avcodec.h"
> +#include "bitstream.h"
>  #include "bytestream.h"
>  #include "error_resilience.h"
>  #include "idctdsp.h"
> @@ -46,6 +47,7 @@
>  #include "thread.h"
>  #include "version.h"
>  #include "xvmc_internal.h"
> +#include "vlc.h"
>  
>  typedef struct Mpeg1Context {
>      MpegEncContext mpeg_enc_ctx;
> @@ -106,18 +108,18 @@ static int mpeg_decode_motion(MpegEncContext *s, int 
> fcode, int pred)
>  {
>      int code, sign, val, shift;
>  
> -    code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
> +    code = bitstream_read_vlc(&s->bc, ff_mv_vlc.table, MV_VLC_BITS, 2);
>      if (code == 0)
>          return pred;
>      if (code < 0)
>          return 0xffff;
>  
> -    sign  = get_bits1(&s->gb);
> +    sign  = bitstream_read_bit(&s->bc);
>      shift = fcode - 1;
>      val   = code;
>      if (shift) {
>          val  = (val - 1) << shift;
> -        val |= get_bits(&s->gb, shift);
> +        val |= bitstream_read(&s->bc, shift);
>          val++;
>      }
>      if (sign)
> @@ -148,25 +150,22 @@ static inline int 
> mpeg1_decode_block_inter(MpegEncContext *s,
>      const int qscale             = s->qscale;
>  
>      {
> -        OPEN_READER(re, &s->gb);
>          i = -1;
>          // special case for first coefficient, no need to add second VLC 
> table
> -        UPDATE_CACHE(re, &s->gb);
> -        if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
> +        if (((int32_t) bitstream_peek(&s->bc, 32)) < 0) {

bitstream_peek_signed?

>              level = (3 * qscale * quant_matrix[0]) >> 5;
>              level = (level - 1) | 1;
> -            if (GET_CACHE(re, &s->gb) & 0x40000000)
> +            if (bitstream_peek(&s->bc, 32) & 0x40000000)
>                  level = -level;
>              block[0] = level;
>              i++;
> -            SKIP_BITS(re, &s->gb, 2);
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            bitstream_skip(&s->bc, 2);
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

Same.

>                  goto end;
>          }
>          /* now quantify & encode AC coefficients */
>          for (;;) {
> -            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
> -                       TEX_VLC_BITS, 2, 0);
> +            BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], 
> TEX_VLC_BITS, 2);
>  
>              if (level != 0) {
>                  i += run;
> @@ -175,22 +174,17 @@ static inline int 
> mpeg1_decode_block_inter(MpegEncContext *s,
>                  j = scantable[i];
>                  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
>                  level = (level - 1) | 1;
> -                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
> -                        SHOW_SBITS(re, &s->gb, 1);
> -                SKIP_BITS(re, &s->gb, 1);
> +                level = (level ^ bitstream_peek_signed(&s->bc, 1)) -
> +                         bitstream_peek_signed(&s->bc, 1);
> +                bitstream_skip(&s->bc, 1);

And again the sign thing.

>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, &s->gb, 6) + 1;
> -                LAST_SKIP_BITS(re, &s->gb, 6);
> -                UPDATE_CACHE(re, &s->gb);
> -                level = SHOW_SBITS(re, &s->gb, 8);
> -                SKIP_BITS(re, &s->gb, 8);
> +                run   = bitstream_read(&s->bc, 6) + 1;
> +                level = bitstream_read_signed(&s->bc, 8);
>                  if (level == -128) {
> -                    level = SHOW_UBITS(re, &s->gb, 8) - 256;
> -                    SKIP_BITS(re, &s->gb, 8);
> +                    level = bitstream_read(&s->bc, 8) - 256;
>                  } else if (level == 0) {
> -                    level = SHOW_UBITS(re, &s->gb, 8);
> -                    SKIP_BITS(re, &s->gb, 8);
> +                    level = bitstream_read(&s->bc, 8);
>                  }
>                  i += run;
>                  if (i > MAX_INDEX)
> @@ -208,13 +202,11 @@ static inline int 
> mpeg1_decode_block_inter(MpegEncContext *s,
>              }
>  
>              block[j] = level;
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

Again bitstream_peek_signed

>                  break;
> -            UPDATE_CACHE(re, &s->gb);
>          }
>  end:
> -        LAST_SKIP_BITS(re, &s->gb, 2);
> -        CLOSE_READER(re, &s->gb);
> +        bitstream_skip(&s->bc, 2);
>      }
>  
>      check_scantable_index(s, i);
> @@ -232,26 +224,23 @@ static inline int 
> mpeg1_fast_decode_block_inter(MpegEncContext *s,
>      const int qscale         = s->qscale;
>  
>      {
> -        OPEN_READER(re, &s->gb);
>          i = -1;
>          // Special case for first coefficient, no need to add second VLC 
> table.
> -        UPDATE_CACHE(re, &s->gb);
> -        if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
> +        if (((int32_t) bitstream_peek(&s->bc, 32)) < 0) {

And again.

>              level = (3 * qscale) >> 1;
>              level = (level - 1) | 1;
> -            if (GET_CACHE(re, &s->gb) & 0x40000000)
> +            if (bitstream_peek(&s->bc, 32) & 0x40000000)
>                  level = -level;
>              block[0] = level;
>              i++;
> -            SKIP_BITS(re, &s->gb, 2);
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            bitstream_skip(&s->bc, 2);
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

And here.

>                  goto end;
>          }
>  
>          /* now quantify & encode AC coefficients */
>          for (;;) {
> -            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
> -                       TEX_VLC_BITS, 2, 0);
> +            BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], 
> TEX_VLC_BITS, 2);
>  
>              if (level != 0) {
>                  i += run;
> @@ -260,22 +249,17 @@ static inline int 
> mpeg1_fast_decode_block_inter(MpegEncContext *s,
>                  j = scantable[i];
>                  level = ((level * 2 + 1) * qscale) >> 1;
>                  level = (level - 1) | 1;
> -                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
> -                        SHOW_SBITS(re, &s->gb, 1);
> -                SKIP_BITS(re, &s->gb, 1);
> +                level = (level ^ bitstream_peek_signed(&s->bc, 1)) -
> +                         bitstream_peek_signed(&s->bc, 1);
> +                bitstream_skip(&s->bc, 1);

sign yet again

>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, &s->gb, 6) + 1;
> -                LAST_SKIP_BITS(re, &s->gb, 6);
> -                UPDATE_CACHE(re, &s->gb);
> -                level = SHOW_SBITS(re, &s->gb, 8);
> -                SKIP_BITS(re, &s->gb, 8);
> +                run   = bitstream_read(&s->bc, 6) + 1;
> +                level = bitstream_read_signed(&s->bc, 8);
>                  if (level == -128) {
> -                    level = SHOW_UBITS(re, &s->gb, 8) - 256;
> -                    SKIP_BITS(re, &s->gb, 8);
> +                    level = bitstream_read(&s->bc, 8) - 256;
>                  } else if (level == 0) {
> -                    level = SHOW_UBITS(re, &s->gb, 8);
> -                    SKIP_BITS(re, &s->gb, 8);
> +                    level = bitstream_read(&s->bc, 8);
>                  }
>                  i += run;
>                  if (i > MAX_INDEX)
> @@ -293,13 +277,11 @@ static inline int 
> mpeg1_fast_decode_block_inter(MpegEncContext *s,
>              }
>  
>              block[j] = level;
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

$$

>                  break;
> -            UPDATE_CACHE(re, &s->gb);
>          }
>  end:
> -        LAST_SKIP_BITS(re, &s->gb, 2);
> -        CLOSE_READER(re, &s->gb);
> +        bitstream_skip(&s->bc, 2);
>      }
>  
>      check_scantable_index(s, i);
> @@ -321,7 +303,6 @@ static inline int 
> mpeg2_decode_block_non_intra(MpegEncContext *s,
>      mismatch = 1;
>  
>      {
> -        OPEN_READER(re, &s->gb);
>          i = -1;
>          if (n < 4)
>              quant_matrix = s->inter_matrix;
> @@ -329,23 +310,21 @@ static inline int 
> mpeg2_decode_block_non_intra(MpegEncContext *s,
>              quant_matrix = s->chroma_inter_matrix;
>  
>          // Special case for first coefficient, no need to add second VLC 
> table.
> -        UPDATE_CACHE(re, &s->gb);
> -        if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
> +        if (((int32_t) bitstream_peek(&s->bc, 32)) < 0) {

$$

>              level = (3 * qscale * quant_matrix[0]) >> 5;
> -            if (GET_CACHE(re, &s->gb) & 0x40000000)
> +            if (bitstream_peek(&s->bc, 32) & 0x40000000)
>                  level = -level;
>              block[0]  = level;
>              mismatch ^= level;
>              i++;
> -            SKIP_BITS(re, &s->gb, 2);
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            bitstream_skip(&s->bc, 2);
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

$$

>                  goto end;
>          }
>  
>          /* now quantify & encode AC coefficients */
>          for (;;) {
> -            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
> -                       TEX_VLC_BITS, 2, 0);
> +            BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], 
> TEX_VLC_BITS, 2);
>  
>              if (level != 0) {
>                  i += run;
> @@ -353,16 +332,13 @@ static inline int 
> mpeg2_decode_block_non_intra(MpegEncContext *s,
>                      break;
>                  j = scantable[i];
>                  level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
> -                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
> -                        SHOW_SBITS(re, &s->gb, 1);
> -                SKIP_BITS(re, &s->gb, 1);
> +                level = (level ^ bitstream_peek_signed(&s->bc, 1)) -
> +                         bitstream_peek_signed(&s->bc, 1);
> +                bitstream_skip(&s->bc, 1);

##

>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, &s->gb, 6) + 1;
> -                LAST_SKIP_BITS(re, &s->gb, 6);
> -                UPDATE_CACHE(re, &s->gb);
> -                level = SHOW_SBITS(re, &s->gb, 12);
> -                SKIP_BITS(re, &s->gb, 12);
> +                run   = bitstream_read(&s->bc, 6) + 1;
> +                level = bitstream_read_signed(&s->bc, 12);
>  
>                  i += run;
>                  if (i > MAX_INDEX)
> @@ -378,13 +354,11 @@ static inline int 
> mpeg2_decode_block_non_intra(MpegEncContext *s,
>  
>              mismatch ^= level;
>              block[j]  = level;
> -            if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +            if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 
> 0xBFFFFFFF)

$$

>                  break;
> -            UPDATE_CACHE(re, &s->gb);
>          }
>  end:
> -        LAST_SKIP_BITS(re, &s->gb, 2);
> -        CLOSE_READER(re, &s->gb);
> +        bitstream_skip(&s->bc, 2);
>      }
>      block[63] ^= (mismatch & 1);
>  
> @@ -401,25 +375,23 @@ static inline int 
> mpeg2_fast_decode_block_non_intra(MpegEncContext *s,

The usual two comments for this function as well.

>      RLTable *rl              = &ff_rl_mpeg1;
>      uint8_t *const scantable = s->intra_scantable.permutated;
>      const int qscale         = s->qscale;
> -    OPEN_READER(re, &s->gb);
>      i = -1;
>  
>      // special case for first coefficient, no need to add second VLC table
> -    UPDATE_CACHE(re, &s->gb);
> -    if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
> +    if (((int32_t) bitstream_peek(&s->bc, 32)) < 0) {
>          level = (3 * qscale) >> 1;
> -        if (GET_CACHE(re, &s->gb) & 0x40000000)
> +        if (bitstream_peek(&s->bc, 32) & 0x40000000)
>              level = -level;
>          block[0] = level;
>          i++;
> -        SKIP_BITS(re, &s->gb, 2);
> -        if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +        bitstream_skip(&s->bc, 2);
> +        if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 0xBFFFFFFF)
>              goto end;
>      }
>  
>      /* now quantify & encode AC coefficients */
>      for (;;) {
> -        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 
> 0);
> +        BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], TEX_VLC_BITS, 2);
>  
>          if (level != 0) {
>              i += run;
> @@ -427,16 +399,13 @@ static inline int 
> mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
>                  break;
>              j = scantable[i];
>              level = ((level * 2 + 1) * qscale) >> 1;
> -            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
> -                    SHOW_SBITS(re, &s->gb, 1);
> -            SKIP_BITS(re, &s->gb, 1);
> +            level = (level ^ bitstream_peek_signed(&s->bc, 1)) -
> +                     bitstream_peek_signed(&s->bc, 1);
> +            bitstream_skip(&s->bc, 1);
>          } else {
>              /* escape */
> -            run = SHOW_UBITS(re, &s->gb, 6) + 1;
> -            LAST_SKIP_BITS(re, &s->gb, 6);
> -            UPDATE_CACHE(re, &s->gb);
> -            level = SHOW_SBITS(re, &s->gb, 12);
> -            SKIP_BITS(re, &s->gb, 12);
> +            run   = bitstream_read(&s->bc, 6) + 1;
> +            level = bitstream_read_signed(&s->bc, 12);
>  
>              i += run;
>              if (i > MAX_INDEX)
> @@ -451,13 +420,11 @@ static inline int 
> mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
>          }
>  
>          block[j] = level;
> -        if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
> +        if (((int32_t) bitstream_peek(&s->bc, 32)) <= (int32_t) 0xBFFFFFFF)
>              break;
> -        UPDATE_CACHE(re, &s->gb);
>      }
>  end:
> -    LAST_SKIP_BITS(re, &s->gb, 2);
> -    CLOSE_READER(re, &s->gb);
> +    bitstream_skip(&s->bc, 2);
>  
>      check_scantable_index(s, i);
>  
> @@ -484,7 +451,7 @@ static inline int mpeg2_decode_block_intra(MpegEncContext 
> *s,

And this one too.

>          quant_matrix = s->chroma_intra_matrix;
>          component    = (n & 1) + 1;
>      }
> -    diff = decode_dc(&s->gb, component);
> +    diff = decode_dc(&s->bc, component);
>      if (diff >= 0xffff)
>          return AVERROR_INVALIDDATA;
>      dc  = s->last_dc[component];
> @@ -500,12 +467,9 @@ static inline int 
> mpeg2_decode_block_intra(MpegEncContext *s,
>          rl = &ff_rl_mpeg1;
>  
>      {
> -        OPEN_READER(re, &s->gb);
>          /* now quantify & encode AC coefficients */
>          for (;;) {
> -            UPDATE_CACHE(re, &s->gb);
> -            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
> -                       TEX_VLC_BITS, 2, 0);
> +            BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], 
> TEX_VLC_BITS, 2);
>  
>              if (level == 127) {
>                  break;
> @@ -515,16 +479,13 @@ static inline int 
> mpeg2_decode_block_intra(MpegEncContext *s,
>                      break;
>                  j = scantable[i];
>                  level = (level * qscale * quant_matrix[j]) >> 4;
> -                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
> -                        SHOW_SBITS(re, &s->gb, 1);
> -                LAST_SKIP_BITS(re, &s->gb, 1);
> +                level = (level ^ bitstream_peek_signed(&s->bc, 1)) -
> +                         bitstream_peek_signed(&s->bc, 1);
> +                bitstream_skip(&s->bc, 1);
>              } else {
>                  /* escape */
> -                run = SHOW_UBITS(re, &s->gb, 6) + 1;
> -                LAST_SKIP_BITS(re, &s->gb, 6);
> -                UPDATE_CACHE(re, &s->gb);
> -                level = SHOW_SBITS(re, &s->gb, 12);
> -                SKIP_BITS(re, &s->gb, 12);
> +                run   = bitstream_read(&s->bc, 6) + 1;
> +                level = bitstream_read_signed(&s->bc, 12);
>                  i += run;
>                  if (i > MAX_INDEX)
>                      break;
> @@ -540,7 +501,6 @@ static inline int mpeg2_decode_block_intra(MpegEncContext 
> *s,
>              mismatch ^= level;
>              block[j]  = level;
>          }
> -        CLOSE_READER(re, &s->gb);
>      }
>      block[63] ^= mismatch & 1;
>  
> @@ -568,7 +528,7 @@ static inline int 
> mpeg2_fast_decode_block_intra(MpegEncContext *s,

And again.

-- 
Anton Khirnov
_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to