[12/14] jpeg: Convert to the new bitstream reader

Message ID 1485425455-24502-13-git-send-email-diego@biurrun.de
State New
Headers show

Commit Message

Diego Biurrun Jan. 26, 2017, 10:10 a.m.
From: Alexandra Hájková <alexandra@khirnov.net>

---
 libavcodec/jpeglsdec.c |  48 +++++-----
 libavcodec/jpeglsenc.c |  14 +--
 libavcodec/mjpegbdec.c |  44 ++++-----
 libavcodec/mjpegdec.c  | 242 +++++++++++++++++++++----------------------------
 libavcodec/mjpegdec.h  |   5 +-
 libavcodec/mxpegdec.c  |   9 +-
 6 files changed, 165 insertions(+), 197 deletions(-)

Comments

Vittorio Giovara Jan. 26, 2017, 12:30 p.m. | #1
On Thu, Jan 26, 2017 at 11:10 AM, Diego Biurrun <diego@biurrun.de> wrote:
> From: Alexandra Hájková <alexandra@khirnov.net>
>
> ---
>  libavcodec/jpeglsdec.c |  48 +++++-----
>  libavcodec/jpeglsenc.c |  14 +--
>  libavcodec/mjpegbdec.c |  44 ++++-----
>  libavcodec/mjpegdec.c  | 242 +++++++++++++++++++++----------------------------
>  libavcodec/mjpegdec.h  |   5 +-
>  libavcodec/mxpegdec.c  |   9 +-
>  6 files changed, 165 insertions(+), 197 deletions(-)

This is hardly a fringe codec ;)

> @@ -542,26 +533,14 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
>          return 0;
>      }
>
> -    {
> -        OPEN_READER(re, &s->gb);
>          for (i = ss; ; i++) {
> -            UPDATE_CACHE(re, &s->gb);
> -            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
> +            code = bitstream_read_vlc(&s->bc, s->vlcs[2][ac_index].table, 9, 2);
>
>              run = ((unsigned) code) >> 4;
>              code &= 0xF;
>              if (code) {
>                  i += run;
> -                if (code > MIN_CACHE_BITS - 16)
> -                    UPDATE_CACHE(re, &s->gb);
> -
> -                {
> -                    int cache = GET_CACHE(re, &s->gb);
> -                    int sign  = (~cache) >> 31;
> -                    level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
> -                }
> -
> -                LAST_SKIP_BITS(re, &s->gb, code);
> +                level = bitstream_read_xbits(&s->bc, code);
>
>                  if (i >= se) {
>                      if (i == se) {
> @@ -584,17 +563,13 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
>                  } else {
>                      val = (1 << run);
>                      if (run) {
> -                        UPDATE_CACHE(re, &s->gb);
> -                        val += NEG_USR32(GET_CACHE(re, &s->gb), run);
> -                        LAST_SKIP_BITS(re, &s->gb, run);
> +                        val += NEG_USR32(bitstream_read(&s->bc, 32), run);
>                      }
>                      *EOBRUN = val - 1;
>                      break;
>                  }
>              }
>          }
> -        CLOSE_READER(re, &s->gb);
> -    }
>
>      if (i > *last_nnz)
>          *last_nnz = i;

This chunk will be oddly indented after the patch, I would leave the
{} around it (and then realign optionally, or not since there are a
lot of places like that)

seems ok otherwise
Diego Biurrun Jan. 26, 2017, 2:25 p.m. | #2
On Thu, Jan 26, 2017 at 01:30:34PM +0100, Vittorio Giovara wrote:
> On Thu, Jan 26, 2017 at 11:10 AM, Diego Biurrun <diego@biurrun.de> wrote:
> > From: Alexandra Hájková <alexandra@khirnov.net>
> >
> > ---
> >  libavcodec/jpeglsdec.c |  48 +++++-----
> >  libavcodec/jpeglsenc.c |  14 +--
> >  libavcodec/mjpegbdec.c |  44 ++++-----
> >  libavcodec/mjpegdec.c  | 242 +++++++++++++++++++++----------------------------
> >  libavcodec/mjpegdec.h  |   5 +-
> >  libavcodec/mxpegdec.c  |   9 +-
> >  6 files changed, 165 insertions(+), 197 deletions(-)
> 
> This is hardly a fringe codec ;)

Feel free to benchmark it ;-p

> > @@ -542,26 +533,14 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
> >          return 0;
> >      }
> >
> > -    {
> > -        OPEN_READER(re, &s->gb);
> >          for (i = ss; ; i++) {
> > -            UPDATE_CACHE(re, &s->gb);
> > -            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
> > +            code = bitstream_read_vlc(&s->bc, s->vlcs[2][ac_index].table, 9, 2);
> >
> >              run = ((unsigned) code) >> 4;
> >              code &= 0xF;
> >              if (code) {
> >                  i += run;
> > -                if (code > MIN_CACHE_BITS - 16)
> > -                    UPDATE_CACHE(re, &s->gb);
> > -
> > -                {
> > -                    int cache = GET_CACHE(re, &s->gb);
> > -                    int sign  = (~cache) >> 31;
> > -                    level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
> > -                }
> > -
> > -                LAST_SKIP_BITS(re, &s->gb, code);
> > +                level = bitstream_read_xbits(&s->bc, code);
> >
> >                  if (i >= se) {
> >                      if (i == se) {
> > @@ -584,17 +563,13 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
> >                  } else {
> >                      val = (1 << run);
> >                      if (run) {
> > -                        UPDATE_CACHE(re, &s->gb);
> > -                        val += NEG_USR32(GET_CACHE(re, &s->gb), run);
> > -                        LAST_SKIP_BITS(re, &s->gb, run);
> > +                        val += NEG_USR32(bitstream_read(&s->bc, 32), run);
> >                      }
> >                      *EOBRUN = val - 1;
> >                      break;
> >                  }
> >              }
> >          }
> > -        CLOSE_READER(re, &s->gb);
> > -    }
> >
> >      if (i > *last_nnz)
> >          *last_nnz = i;
> 
> This chunk will be oddly indented after the patch, I would leave the
> {} around it (and then realign optionally, or not since there are a
> lot of places like that)

I'll leave the {} in, they are going away in a final cleanup patch.

Diego

Patch

diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index 8d1e763..6c49e46 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -26,8 +26,8 @@ 
  */
 
 #include "avcodec.h"
-#include "get_bits.h"
-#include "golomb_legacy.h"
+#include "bitstream.h"
+#include "golomb.h"
 #include "internal.h"
 #include "mathops.h"
 #include "mjpeg.h"
@@ -52,16 +52,16 @@  int ff_jpegls_decode_lse(MJpegDecodeContext *s)
 {
     int id;
 
-    skip_bits(&s->gb, 16);  /* length: FIXME: verify field validity */
-    id = get_bits(&s->gb, 8);
+    bitstream_skip(&s->bc, 16);  /* length: FIXME: verify field validity */
+    id = bitstream_read(&s->bc, 8);
 
     switch (id) {
     case 1:
-        s->maxval = get_bits(&s->gb, 16);
-        s->t1     = get_bits(&s->gb, 16);
-        s->t2     = get_bits(&s->gb, 16);
-        s->t3     = get_bits(&s->gb, 16);
-        s->reset  = get_bits(&s->gb, 16);
+        s->maxval = bitstream_read(&s->bc, 16);
+        s->t1     = bitstream_read(&s->bc, 16);
+        s->t2     = bitstream_read(&s->bc, 16);
+        s->t3     = bitstream_read(&s->bc, 16);
+        s->reset  = bitstream_read(&s->bc, 16);
 
 //        ff_jpegls_reset_coding_parameters(s, 0);
         //FIXME quant table?
@@ -85,7 +85,7 @@  int ff_jpegls_decode_lse(MJpegDecodeContext *s)
 /**
  * Get context-dependent Golomb code, decode it and update context
  */
-static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
+static inline int ls_get_code_regular(BitstreamContext *bc, JLSState *state, int Q)
 {
     int k, ret;
 
@@ -93,10 +93,10 @@  static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
         ;
 
 #ifdef JLS_BROKEN
-    if (!show_bits_long(gb, 32))
+    if (!bitstream_skip(bc, 32))
         return -1;
 #endif
-    ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
+    ret = get_ur_golomb_jpegls(bc, k, state->limit, state->qbpp);
 
     /* decode mapped error */
     if (ret & 1)
@@ -116,7 +116,7 @@  static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
 /**
  * Get Golomb code, decode it and update state for run termination
  */
-static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
+static inline int ls_get_code_runterm(BitstreamContext *bc, JLSState *state,
                                       int RItype, int limit_add)
 {
     int k, ret, temp, map;
@@ -130,10 +130,10 @@  static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
         ;
 
 #ifdef JLS_BROKEN
-    if (!show_bits_long(gb, 32))
+    if (!bitstream_skip(bc, 32))
         return -1;
 #endif
-    ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
+    ret = get_ur_golomb_jpegls(bc, k, state->limit - limit_add - 1,
                                state->qbpp);
 
     /* decode mapped error */
@@ -187,7 +187,7 @@  static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
             int RItype;
 
             /* decode full runs while available */
-            while (get_bits1(&s->gb)) {
+            while (bitstream_read_bit(&s->bc)) {
                 int r;
                 r = 1 << ff_log2_run[state->run_index[comp]];
                 if (x + r * stride > w)
@@ -207,7 +207,7 @@  static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
             /* decode aborted run */
             r = ff_log2_run[state->run_index[comp]];
             if (r)
-                r = get_bits_long(&s->gb, r);
+                r = bitstream_read(&s->bc, r);
             for (i = 0; i < r; i++) {
                 W(dst, x, Ra);
                 x += stride;
@@ -216,7 +216,7 @@  static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
             /* decode run termination value */
             Rb     = R(last, x);
             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
-            err    = ls_get_code_runterm(&s->gb, state, RItype,
+            err    = ls_get_code_runterm(&s->bc, state, RItype,
                                          ff_log2_run[state->run_index[comp]]);
             if (state->run_index[comp])
                 state->run_index[comp]--;
@@ -246,10 +246,10 @@  static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
 
             if (sign) {
                 pred = av_clip(pred - state->C[context], 0, state->maxval);
-                err  = -ls_get_code_regular(&s->gb, state, context);
+                err  = -ls_get_code_regular(&s->bc, state, context);
             } else {
                 pred = av_clip(pred + state->C[context], 0, state->maxval);
-                err  = ls_get_code_regular(&s->gb, state, context);
+                err  = ls_get_code_regular(&s->bc, state, context);
             }
 
             /* we have to do something more for near-lossless coding */
@@ -333,8 +333,8 @@  int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
             cur += s->picture_ptr->linesize[0];
 
             if (s->restart_interval && !--s->restart_count) {
-                align_get_bits(&s->gb);
-                skip_bits(&s->gb, 16); /* skip RSTn */
+                bitstream_align(&s->bc);
+                bitstream_skip(&s->bc, 16); /* skip RSTn */
             }
         }
     } else if (ilv == 1) { /* line interleaving */
@@ -349,8 +349,8 @@  int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
                 Rc[j] = last[j];
 
                 if (s->restart_interval && !--s->restart_count) {
-                    align_get_bits(&s->gb);
-                    skip_bits(&s->gb, 16); /* skip RSTn */
+                    bitstream_align(&s->bc);
+                    bitstream_skip(&s->bc, 16); /* skip RSTn */
                 }
             }
             last = cur;
diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c
index fb3c69f..1bdd2c6 100644
--- a/libavcodec/jpeglsenc.c
+++ b/libavcodec/jpeglsenc.c
@@ -26,8 +26,8 @@ 
  */
 
 #include "avcodec.h"
-#include "get_bits.h"
-#include "golomb_legacy.h"
+#include "bitstream.h"
+#include "golomb.h"
 #include "internal.h"
 #include "mathops.h"
 #include "mjpeg.h"
@@ -258,7 +258,7 @@  static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
     JPEGLSContext *ctx = avctx->priv_data;
     const AVFrame *const p = pict;
     PutBitContext pb, pb2;
-    GetBitContext gb;
+    BitstreamContext bc;
     uint8_t *buf2 = NULL;
     uint8_t *zero = NULL;
     uint8_t *cur  = NULL;
@@ -393,14 +393,14 @@  FF_ENABLE_DEPRECATION_WARNINGS
     size = put_bits_count(&pb2);
     flush_put_bits(&pb2);
     /* do escape coding */
-    init_get_bits(&gb, buf2, size);
+    bitstream_init(&bc, buf2, size);
     size -= 7;
-    while (get_bits_count(&gb) < size) {
+    while (bitstream_tell(&bc) < size) {
         int v;
-        v = get_bits(&gb, 8);
+        v = bitstream_read(&bc, 8);
         put_bits(&pb, 8, v);
         if (v == 0xFF) {
-            v = get_bits(&gb, 7);
+            v = bitstream_read(&bc, 7);
             put_bits(&pb, 8, v);
         }
     }
diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c
index 3775aa3..b1cb1b0 100644
--- a/libavcodec/mjpegbdec.c
+++ b/libavcodec/mjpegbdec.c
@@ -31,8 +31,10 @@ 
 #include "mjpeg.h"
 #include "mjpegdec.h"
 
-static uint32_t read_offs(AVCodecContext *avctx, GetBitContext *gb, uint32_t size, const char *err_msg){
-    uint32_t offs= get_bits_long(gb, 32);
+static uint32_t read_offs(AVCodecContext *avctx, BitstreamContext *bc,
+                          uint32_t size, const char *err_msg)
+{
+    uint32_t offs = bitstream_read(bc, 32);
     if(offs >= size){
         av_log(avctx, AV_LOG_WARNING, err_msg, offs, size);
         return 0;
@@ -48,7 +50,7 @@  static int mjpegb_decode_frame(AVCodecContext *avctx,
     int buf_size = avpkt->size;
     MJpegDecodeContext *s = avctx->priv_data;
     const uint8_t *buf_end, *buf_ptr;
-    GetBitContext hgb; /* for the header */
+    BitstreamContext hbc; /* for the header */
     uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
     uint32_t field_size, sod_offs;
     int ret;
@@ -65,62 +67,62 @@  read_header:
     if (buf_end - buf_ptr >= 1 << 28)
         return AVERROR_INVALIDDATA;
 
-    init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
+    bitstream_init(&hbc, buf_ptr, /* buf_size */ (buf_end - buf_ptr) * 8);
 
-    skip_bits(&hgb, 32); /* reserved zeros */
+    bitstream_skip(&hbc, 32); /* reserved zeros */
 
-    if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
-    {
+    if (bitstream_read(&hbc, 32) != MKBETAG('m','j','p','g')) {
         av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
         return AVERROR_INVALIDDATA;
     }
 
-    field_size = get_bits_long(&hgb, 32); /* field size */
+    field_size = bitstream_read(&hbc, 32); /* field size */
     av_log(avctx, AV_LOG_DEBUG, "field size: 0x%"PRIx32"\n", field_size);
-    skip_bits(&hgb, 32); /* padded field size */
-    second_field_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "second_field_offs is %d and size is %d\n");
+    bitstream_skip(&hbc, 32); /* padded field size */
+    second_field_offs = read_offs(avctx, &hbc, buf_end - buf_ptr,
+                                  "second_field_offs is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%"PRIx32"\n",
            second_field_offs);
 
-    dqt_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dqt is %d and size is %d\n");
+    dqt_offs = read_offs(avctx, &hbc, buf_end - buf_ptr, "dqt is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%"PRIx32"\n", dqt_offs);
     if (dqt_offs)
     {
-        init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
+        bitstream_init(&s->bc, buf_ptr + dqt_offs, (buf_end - (buf_ptr + dqt_offs)) * 8);
         s->start_code = DQT;
         if (ff_mjpeg_decode_dqt(s) < 0 &&
             (avctx->err_recognition & AV_EF_EXPLODE))
           return AVERROR_INVALIDDATA;
     }
 
-    dht_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dht is %d and size is %d\n");
+    dht_offs = read_offs(avctx, &hbc, buf_end - buf_ptr, "dht is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%"PRIx32"\n", dht_offs);
     if (dht_offs)
     {
-        init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
+        bitstream_init(&s->bc, buf_ptr + dht_offs, (buf_end - (buf_ptr + dht_offs)) * 8);
         s->start_code = DHT;
         ff_mjpeg_decode_dht(s);
     }
 
-    sof_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
+    sof_offs = read_offs(avctx, &hbc, buf_end - buf_ptr, "sof is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%"PRIx32"\n", sof_offs);
     if (sof_offs)
     {
-        init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
+        bitstream_init(&s->bc, buf_ptr + sof_offs, (buf_end - (buf_ptr + sof_offs)) * 8);
         s->start_code = SOF0;
         if (ff_mjpeg_decode_sof(s) < 0)
             return -1;
     }
 
-    sos_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sos is %d and size is %d\n");
+    sos_offs = read_offs(avctx, &hbc, buf_end - buf_ptr, "sos is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%"PRIx32"\n", sos_offs);
-    sod_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
+    sod_offs = read_offs(avctx, &hbc, buf_end - buf_ptr, "sof is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%"PRIx32"\n", sod_offs);
     if (sos_offs)
     {
-        init_get_bits(&s->gb, buf_ptr + sos_offs,
-                      8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
-        s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
+        bitstream_init(&s->bc, buf_ptr + sos_offs,
+                       8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
+        s->mjpb_skiptosod = sod_offs - sos_offs - bitstream_peek(&s->bc, 16);
         s->start_code = SOS;
         if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
             (avctx->err_recognition & AV_EF_EXPLODE))
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index f6e9038..0a2d03e 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -34,7 +34,9 @@ 
 
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
+
 #include "avcodec.h"
+#include "bitstream.h"
 #include "blockdsp.h"
 #include "idctdsp.h"
 #include "internal.h"
@@ -43,6 +45,7 @@ 
 #include "mjpegdec.h"
 #include "jpeglsdec.h"
 #include "put_bits.h"
+#include "vlc.h"
 
 
 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
@@ -131,7 +134,7 @@  av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
 
     if (s->extern_huff) {
         av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
-        if ((ret = init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8)) < 0)
+        if ((ret = bitstream_init8(&s->bc, avctx->extradata, avctx->extradata_size) < 0))
             return ret;
         if ((ret = ff_mjpeg_decode_dht(s))) {
             av_log(avctx, AV_LOG_ERROR,
@@ -155,22 +158,22 @@  int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
 {
     int len, index, i, j;
 
-    len = get_bits(&s->gb, 16) - 2;
+    len = bitstream_read(&s->bc, 16) - 2;
 
     while (len >= 65) {
         /* only 8-bit precision handled */
-        if (get_bits(&s->gb, 4) != 0) {
+        if (bitstream_read(&s->bc, 4) != 0) {
             av_log(s->avctx, AV_LOG_ERROR, "dqt: 16-bit precision\n");
             return -1;
         }
-        index = get_bits(&s->gb, 4);
+        index = bitstream_read(&s->bc, 4);
         if (index >= 4)
             return -1;
         av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
         /* read quant table */
         for (i = 0; i < 64; i++) {
             j = s->scantable.permutated[i];
-            s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
+            s->quant_matrixes[index][j] = bitstream_read(&s->bc, 8);
         }
 
         // XXX FIXME fine-tune, and perhaps add dc too
@@ -191,20 +194,20 @@  int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
     uint8_t val_table[256];
     int ret = 0;
 
-    len = get_bits(&s->gb, 16) - 2;
+    len = bitstream_read(&s->bc, 16) - 2;
 
     while (len > 0) {
         if (len < 17)
             return AVERROR_INVALIDDATA;
-        class = get_bits(&s->gb, 4);
+        class = bitstream_read(&s->bc, 4);
         if (class >= 2)
             return AVERROR_INVALIDDATA;
-        index = get_bits(&s->gb, 4);
+        index = bitstream_read(&s->bc, 4);
         if (index >= 4)
             return AVERROR_INVALIDDATA;
         n = 0;
         for (i = 1; i <= 16; i++) {
-            bits_table[i] = get_bits(&s->gb, 8);
+            bits_table[i] = bitstream_read(&s->bc, 8);
             n += bits_table[i];
         }
         len -= 17;
@@ -213,7 +216,7 @@  int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
 
         code_max = 0;
         for (i = 0; i < n; i++) {
-            v = get_bits(&s->gb, 8);
+            v = bitstream_read(&s->bc, 8);
             if (v > code_max)
                 code_max = v;
             val_table[i] = v;
@@ -245,8 +248,8 @@  int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
     int len, nb_components, i, width, height, bits, pix_fmt_id, ret;
 
     /* XXX: verify len field validity */
-    len     = get_bits(&s->gb, 16);
-    bits    = get_bits(&s->gb, 8);
+    len  = bitstream_read(&s->bc, 16);
+    bits = bitstream_read(&s->bc,  8);
 
     if (s->pegasus_rct)
         bits = 9;
@@ -258,8 +261,8 @@  int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
         return -1;
     }
 
-    height = get_bits(&s->gb, 16);
-    width  = get_bits(&s->gb, 16);
+    height = bitstream_read(&s->bc, 16);
+    width  = bitstream_read(&s->bc, 16);
 
     // HACK for odd_height.mov
     if (s->interlaced && s->width == width && s->height == height + 1)
@@ -269,7 +272,7 @@  int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
     if (av_image_check_size(width, height, 0, s->avctx) < 0)
         return AVERROR_INVALIDDATA;
 
-    nb_components = get_bits(&s->gb, 8);
+    nb_components = bitstream_read(&s->bc, 8);
     if (nb_components <= 0 ||
         nb_components > MAX_COMPONENTS)
         return -1;
@@ -291,15 +294,15 @@  int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
     s->v_max         = 1;
     for (i = 0; i < nb_components; i++) {
         /* component id */
-        s->component_id[i] = get_bits(&s->gb, 8) - 1;
-        h_count[i]         = get_bits(&s->gb, 4);
-        v_count[i]         = get_bits(&s->gb, 4);
+        s->component_id[i] = bitstream_read(&s->bc, 8) - 1;
+        h_count[i]         = bitstream_read(&s->bc, 4);
+        v_count[i]         = bitstream_read(&s->bc, 4);
         /* compute hmax and vmax (only used in interleaved case) */
         if (h_count[i] > s->h_max)
             s->h_max = h_count[i];
         if (v_count[i] > s->v_max)
             s->v_max = v_count[i];
-        s->quant_index[i] = get_bits(&s->gb, 8);
+        s->quant_index[i] = bitstream_read(&s->bc, 8);
         if (s->quant_index[i] >= 4)
             return AVERROR_INVALIDDATA;
         if (!h_count[i] || !v_count[i]) {
@@ -449,7 +452,7 @@  int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
 {
     int code;
-    code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
+    code = bitstream_read_vlc(&s->bc, s->vlcs[0][dc_index].table, 9, 2);
     if (code < 0) {
         av_log(s->avctx, AV_LOG_WARNING,
                "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
@@ -458,7 +461,7 @@  static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
     }
 
     if (code)
-        return get_xbits(&s->gb, code);
+        return bitstream_read_xbits(&s->bc, code);
     else
         return 0;
 }
@@ -480,24 +483,13 @@  static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
     block[0] = val;
     /* AC coefs */
     i = 0;
-    {OPEN_READER(re, &s->gb);
     do {
-        UPDATE_CACHE(re, &s->gb);
-        GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
+        code = bitstream_read_vlc(&s->bc, s->vlcs[1][ac_index].table, 9, 2);
 
         i += ((unsigned)code) >> 4;
             code &= 0xf;
         if (code) {
-            if (code > MIN_CACHE_BITS - 16)
-                UPDATE_CACHE(re, &s->gb);
-
-            {
-                int cache = GET_CACHE(re, &s->gb);
-                int sign  = (~cache) >> 31;
-                level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
-            }
-
-            LAST_SKIP_BITS(re, &s->gb, code);
+            level = bitstream_read_xbits(&s->bc, code);
 
             if (i > 63) {
                 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
@@ -507,7 +499,6 @@  static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
             block[j] = level * quant_matrix[j];
         }
     } while (i < 63);
-    CLOSE_READER(re, &s->gb);}
 
     return 0;
 }
@@ -542,26 +533,14 @@  static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
         return 0;
     }
 
-    {
-        OPEN_READER(re, &s->gb);
         for (i = ss; ; i++) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
+            code = bitstream_read_vlc(&s->bc, s->vlcs[2][ac_index].table, 9, 2);
 
             run = ((unsigned) code) >> 4;
             code &= 0xF;
             if (code) {
                 i += run;
-                if (code > MIN_CACHE_BITS - 16)
-                    UPDATE_CACHE(re, &s->gb);
-
-                {
-                    int cache = GET_CACHE(re, &s->gb);
-                    int sign  = (~cache) >> 31;
-                    level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
-                }
-
-                LAST_SKIP_BITS(re, &s->gb, code);
+                level = bitstream_read_xbits(&s->bc, code);
 
                 if (i >= se) {
                     if (i == se) {
@@ -584,17 +563,13 @@  static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
                 } else {
                     val = (1 << run);
                     if (run) {
-                        UPDATE_CACHE(re, &s->gb);
-                        val += NEG_USR32(GET_CACHE(re, &s->gb), run);
-                        LAST_SKIP_BITS(re, &s->gb, run);
+                        val += NEG_USR32(bitstream_read(&s->bc, 32), run);
                     }
                     *EOBRUN = val - 1;
                     break;
                 }
             }
         }
-        CLOSE_READER(re, &s->gb);
-    }
 
     if (i > *last_nnz)
         *last_nnz = i;
@@ -603,11 +578,9 @@  static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
 }
 
 #define REFINE_BIT(j) {                                             \
-    UPDATE_CACHE(re, &s->gb);                                       \
     sign = block[j] >> 15;                                          \
-    block[j] += SHOW_UBITS(re, &s->gb, 1) *                         \
+    block[j] += bitstream_read_bit(&s->bc) *                        \
                 ((quant_matrix[j] ^ sign) - sign) << Al;            \
-    LAST_SKIP_BITS(re, &s->gb, 1);                                  \
 }
 
 #define ZERO_RUN                                                    \
@@ -636,19 +609,15 @@  static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
     int code, i = ss, j, sign, val, run;
     int last    = FFMIN(se, *last_nnz);
 
-    OPEN_READER(re, &s->gb);
     if (*EOBRUN) {
         (*EOBRUN)--;
     } else {
         for (; ; i++) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
+            code = bitstream_read_vlc(&s->bc, s->vlcs[2][ac_index].table, 9, 2);
 
             if (code & 0xF) {
                 run = ((unsigned) code) >> 4;
-                UPDATE_CACHE(re, &s->gb);
-                val = SHOW_UBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
+                val = bitstream_read_bit(&s->bc);
                 ZERO_RUN;
                 j = s->scantable.permutated[i];
                 val--;
@@ -656,7 +625,6 @@  static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
                 if (i == se) {
                     if (i > *last_nnz)
                         *last_nnz = i;
-                    CLOSE_READER(re, &s->gb);
                     return 0;
                 }
             } else {
@@ -667,9 +635,7 @@  static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
                     val = run;
                     run = (1 << run);
                     if (val) {
-                        UPDATE_CACHE(re, &s->gb);
-                        run += SHOW_UBITS(re, &s->gb, val);
-                        LAST_SKIP_BITS(re, &s->gb, val);
+                        run += bitstream_read(&s->bc, val);
                     }
                     *EOBRUN = run - 1;
                     break;
@@ -686,7 +652,6 @@  static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
         if (block[j])
             REFINE_BIT(j)
     }
-    CLOSE_READER(re, &s->gb);
 
     return 0;
 }
@@ -739,8 +704,8 @@  static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
             }
 
             if (s->restart_interval && !--s->restart_count) {
-                align_get_bits(&s->gb);
-                skip_bits(&s->gb, 16); /* skip RSTn */
+                bitstream_align(&s->bc);
+                bitstream_skip(&s->bc, 16); /* skip RSTn */
             }
         }
 
@@ -848,8 +813,8 @@  static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
                 }
             }
             if (s->restart_interval && !--s->restart_count) {
-                align_get_bits(&s->gb);
-                skip_bits(&s->gb, 16); /* skip RSTn */
+                bitstream_align(&s->bc);
+                bitstream_skip(&s->bc, 16); /* skip RSTn */
             }
         }
     }
@@ -864,10 +829,10 @@  static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
     uint8_t *data[MAX_COMPONENTS];
     const uint8_t *reference_data[MAX_COMPONENTS];
     int linesize[MAX_COMPONENTS];
-    GetBitContext mb_bitmask_gb;
+    BitstreamContext mb_bitmask_bc;
 
     if (mb_bitmask)
-        init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
+        bitstream_init(&mb_bitmask_bc, mb_bitmask, s->mb_width * s->mb_height);
 
     for (i = 0; i < nb_components; i++) {
         int c   = s->comp_index[i];
@@ -879,14 +844,14 @@  static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
 
     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
-            const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
+            const int copy_mb = mb_bitmask && !bitstream_read_bit(&mb_bitmask_bc);
 
             if (s->restart_interval && !s->restart_count)
                 s->restart_count = s->restart_interval;
 
-            if (get_bits_left(&s->gb) < 0) {
+            if (bitstream_bits_left(&s->bc) < 0) {
                 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
-                       -get_bits_left(&s->gb));
+                       -bitstream_bits_left(&s->bc));
                 return AVERROR_INVALIDDATA;
             }
             for (i = 0; i < nb_components; i++) {
@@ -927,7 +892,7 @@  static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
                                          (h * mb_x + x);
                         int16_t *block = s->blocks[c][block_idx];
                         if (Ah)
-                            block[0] += get_bits1(&s->gb) *
+                            block[0] += bitstream_read_bit(&s->bc) *
                                         s->quant_matrixes[s->quant_index[c]][0] << Al;
                         else if (decode_dc_progressive(s, block, i, s->dc_index[i],
                                                        s->quant_matrixes[s->quant_index[c]],
@@ -950,18 +915,18 @@  static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
 
             if (s->restart_interval) {
                 s->restart_count--;
-                i = 8 + ((-get_bits_count(&s->gb)) & 7);
+                i = 8 + ((-bitstream_tell(&s->bc)) & 7);
                 /* skip RSTn */
-                if (show_bits(&s->gb, i) == (1 << i) - 1) {
-                    int pos = get_bits_count(&s->gb);
-                    align_get_bits(&s->gb);
-                    while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
-                        skip_bits(&s->gb, 8);
-                    if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
+                if (bitstream_peek(&s->bc, i) == (1 << i) - 1) {
+                    BitstreamContext tmp = s->bc;
+                    bitstream_align(&s->bc);
+                    while (bitstream_bits_left(&s->bc) >= 8 && bitstream_peek(&s->bc, 8) == 0xFF)
+                        bitstream_skip(&s->bc, 8);
+                    if ((bitstream_read(&s->bc, 8) & 0xF8) == 0xD0) {
                         for (i = 0; i < nb_components; i++) /* reset dc */
                             s->last_dc[i] = 1024;
                     } else
-                        skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
+                        s->bc = tmp;
                 }
             }
         }
@@ -982,7 +947,7 @@  static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
     int linesize  = s->linesize[c];
     int last_scan = 0;
     int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
-    GetBitContext mb_bitmask_gb;
+    BitstreamContext mb_bitmask_bc;
 
     if (ss < 0  || ss >= 64 ||
         se < ss || se >= 64 ||
@@ -990,7 +955,7 @@  static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
         return AVERROR_INVALIDDATA;
 
     if (mb_bitmask)
-        init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
+        bitstream_init(&mb_bitmask_bc, mb_bitmask, s->mb_width * s->mb_height);
 
     if (!Al) {
         // s->coefs_finished is a bitmask for coefficients coded
@@ -1012,7 +977,7 @@  static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
         int16_t (*block)[64] = &s->blocks[c][block_idx];
         uint8_t *last_nnz    = &s->last_nnz[c][block_idx];
         for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
-            const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
+            const int copy_mb = mb_bitmask && !bitstream_read_bit(&mb_bitmask_bc);
 
             if (!copy_mb) {
                 int ret;
@@ -1053,8 +1018,8 @@  int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
     int ilv, prev_shift;
 
     /* XXX: verify len field validity */
-    len = get_bits(&s->gb, 16);
-    nb_components = get_bits(&s->gb, 8);
+    len           = bitstream_read(&s->bc, 16);
+    nb_components = bitstream_read(&s->bc, 8);
     if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
         avpriv_report_missing_feature(s->avctx,
                                       "decode_sos: nb_components (%d)",
@@ -1066,7 +1031,7 @@  int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
         return AVERROR_INVALIDDATA;
     }
     for (i = 0; i < nb_components; i++) {
-        id = get_bits(&s->gb, 8) - 1;
+        id = bitstream_read(&s->bc, 8) - 1;
         av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
         /* find component index */
         for (index = 0; index < s->nb_components; index++)
@@ -1088,8 +1053,8 @@  int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
         s->h_scount[i]  = s->h_count[index];
         s->v_scount[i]  = s->v_count[index];
 
-        s->dc_index[i] = get_bits(&s->gb, 4);
-        s->ac_index[i] = get_bits(&s->gb, 4);
+        s->dc_index[i] = bitstream_read(&s->bc, 4);
+        s->ac_index[i] = bitstream_read(&s->bc, 4);
 
         if (s->dc_index[i] <  0 || s->ac_index[i] < 0 ||
             s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
@@ -1099,10 +1064,10 @@  int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
             goto out_of_range;
     }
 
-    predictor = get_bits(&s->gb, 8);       /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
-    ilv = get_bits(&s->gb, 8);             /* JPEG Se / JPEG-LS ILV */
-    prev_shift      = get_bits(&s->gb, 4); /* Ah */
-    point_transform = get_bits(&s->gb, 4); /* Al */
+    predictor       = bitstream_read(&s->bc, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
+    ilv             = bitstream_read(&s->bc, 8); /* JPEG Se / JPEG-LS ILV */
+    prev_shift      = bitstream_read(&s->bc, 4); /* Ah */
+    point_transform = bitstream_read(&s->bc, 4); /* Al */
 
     if (nb_components > 1) {
         /* interleaved stream */
@@ -1127,7 +1092,7 @@  int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
 
     /* mjpeg-b can have padding bytes between sos and image data, skip them */
     for (i = s->mjpb_skiptosod; i > 0; i--)
-        skip_bits(&s->gb, 8);
+        bitstream_skip(&s->bc, 8);
 
 next_field:
     for (i = 0; i < nb_components; i++)
@@ -1170,14 +1135,14 @@  next_field:
     }
 
     if (s->interlaced &&
-        get_bits_left(&s->gb) > 32 &&
-        show_bits(&s->gb, 8) == 0xFF) {
-        GetBitContext bak = s->gb;
-        align_get_bits(&bak);
-        if (show_bits(&bak, 16) == 0xFFD1) {
+        bitstream_bits_left(&s->bc) > 32 &&
+        bitstream_peek(&s->bc, 8) == 0xFF) {
+        BitstreamContext bak = s->bc;
+        bitstream_align(&bak);
+        if (bitstream_peek(&bak, 16) == 0xFFD1) {
             ff_dlog(s->avctx, "AVRn interlaced picture marker found\n");
-            s->gb = bak;
-            skip_bits(&s->gb, 16);
+            s->bc = bak;
+            bitstream_skip(&s->bc, 16);
             s->bottom_field ^= 1;
 
             goto next_field;
@@ -1193,9 +1158,9 @@  next_field:
 
 static int mjpeg_decode_dri(MJpegDecodeContext *s)
 {
-    if (get_bits(&s->gb, 16) != 4)
+    if (bitstream_read(&s->bc, 16) != 4)
         return AVERROR_INVALIDDATA;
-    s->restart_interval = get_bits(&s->gb, 16);
+    s->restart_interval = bitstream_read(&s->bc, 16);
     s->restart_count    = 0;
     av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
            s->restart_interval);
@@ -1207,13 +1172,13 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
 {
     int len, id, i;
 
-    len = get_bits(&s->gb, 16);
+    len = bitstream_read(&s->bc, 16);
     if (len < 5)
         return AVERROR_INVALIDDATA;
-    if (8 * len > get_bits_left(&s->gb))
+    if (8 * len > bitstream_bits_left(&s->bc))
         return AVERROR_INVALIDDATA;
 
-    id   = get_bits_long(&s->gb, 32);
+    id   = bitstream_read(&s->bc, 32);
     id   = av_be2ne32(id);
     len -= 6;
 
@@ -1232,7 +1197,7 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
             4bytes      field_size_less_padding
         */
         s->buggy_avid = 1;
-        i = get_bits(&s->gb, 8);
+        i = bitstream_read(&s->bc, 8);
         if (i == 2)
             s->bottom_field = 1;
         else if (i == 1)
@@ -1242,13 +1207,13 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
 
     if (id == AV_RL32("JFIF")) {
         int t_w, t_h, v1, v2;
-        skip_bits(&s->gb, 8); /* the trailing zero-byte */
-        v1 = get_bits(&s->gb, 8);
-        v2 = get_bits(&s->gb, 8);
-        skip_bits(&s->gb, 8);
+        bitstream_skip(&s->bc, 8); /* the trailing zero-byte */
+        v1 = bitstream_read(&s->bc, 8);
+        v2 = bitstream_read(&s->bc, 8);
+        bitstream_skip(&s->bc, 8);
 
-        s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
-        s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
+        s->avctx->sample_aspect_ratio.num = bitstream_read(&s->bc, 16);
+        s->avctx->sample_aspect_ratio.den = bitstream_read(&s->bc, 16);
         ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
 
         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
@@ -1258,8 +1223,8 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
                    s->avctx->sample_aspect_ratio.num,
                    s->avctx->sample_aspect_ratio.den);
 
-        t_w = get_bits(&s->gb, 8);
-        t_h = get_bits(&s->gb, 8);
+        t_w = bitstream_read(&s->bc, 8);
+        t_h = bitstream_read(&s->bc, 8);
         if (t_w && t_h) {
             /* skip thumbnail */
             if (len -10 - (t_w * t_h * 3) > 0)
@@ -1269,13 +1234,13 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
         goto out;
     }
 
-    if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
+    if (id == AV_RL32("Adob") && (bitstream_read(&s->bc, 8) == 'e')) {
         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
             av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
-        skip_bits(&s->gb, 16); /* version */
-        skip_bits(&s->gb, 16); /* flags0 */
-        skip_bits(&s->gb, 16); /* flags1 */
-        skip_bits(&s->gb,  8); /* transform */
+        bitstream_skip(&s->bc, 16); /* version */
+        bitstream_skip(&s->bc, 16); /* flags0 */
+        bitstream_skip(&s->bc, 16); /* flags1 */
+        bitstream_skip(&s->bc,  8); /* transform */
         len -= 7;
         goto out;
     }
@@ -1284,11 +1249,11 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
             av_log(s->avctx, AV_LOG_INFO,
                    "Pegasus lossless jpeg header found\n");
-        skip_bits(&s->gb, 16); /* version ? */
-        skip_bits(&s->gb, 16); /* unknown always 0? */
-        skip_bits(&s->gb, 16); /* unknown always 0? */
-        skip_bits(&s->gb, 16); /* unknown always 0? */
-        switch (get_bits(&s->gb, 8)) {
+        bitstream_skip(&s->bc, 16); /* version ? */
+        bitstream_skip(&s->bc, 16); /* unknown always 0? */
+        bitstream_skip(&s->bc, 16); /* unknown always 0? */
+        bitstream_skip(&s->bc, 16); /* unknown always 0? */
+        switch (bitstream_read(&s->bc, 8)) {
         case 1:
             s->rgb         = 1;
             s->pegasus_rct = 0;
@@ -1306,7 +1271,7 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
 
     /* Apple MJPEG-A */
     if ((s->start_code == APP1) && (len > (0x28 - 8))) {
-        id   = get_bits_long(&s->gb, 32);
+        id   = bitstream_read(&s->bc, 32);
         id   = av_be2ne32(id);
         len -= 4;
         /* Apple MJPEG-A */
@@ -1322,22 +1287,22 @@  out:
         av_log(s->avctx, AV_LOG_ERROR,
                "mjpeg: error, decode_app parser read over the end\n");
     while (--len > 0)
-        skip_bits(&s->gb, 8);
+        bitstream_skip(&s->bc, 8);
 
     return 0;
 }
 
 static int mjpeg_decode_com(MJpegDecodeContext *s)
 {
-    int len = get_bits(&s->gb, 16);
-    if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
+    int len = bitstream_read(&s->bc, 16);
+    if (len >= 2 && 8 * len - 16 <= bitstream_bits_left(&s->bc)) {
         int i;
         char *cbuf = av_malloc(len - 1);
         if (!cbuf)
             return AVERROR(ENOMEM);
 
         for (i = 0; i < len - 2; i++)
-            cbuf[i] = get_bits(&s->gb, 8);
+            cbuf[i] = bitstream_read(&s->bc, 8);
         if (i > 0 && cbuf[i - 1] == '\n')
             cbuf[i - 1] = 0;
         else
@@ -1513,8 +1478,7 @@  int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
                start_code, buf_end - buf_ptr);
 
-        ret = init_get_bits(&s->gb, unescaped_buf_ptr,
-                            unescaped_buf_size * 8);
+        ret = bitstream_init8(&s->bc, unescaped_buf_ptr, unescaped_buf_size);
         if (ret < 0)
             return ret;
 
@@ -1666,10 +1630,10 @@  eoi_parser:
 
 not_the_end:
         /* eof process start code */
-        buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
+        buf_ptr += (bitstream_tell(&s->bc) + 7) / 8;
         av_log(avctx, AV_LOG_DEBUG,
                "marker parser used %d bytes (%d bits)\n",
-               (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
+               (bitstream_tell(&s->bc) + 7) / 8, bitstream_tell(&s->bc));
     }
     if (s->got_picture) {
         av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h
index b80a47b..2c24149 100644
--- a/libavcodec/mjpegdec.h
+++ b/libavcodec/mjpegdec.h
@@ -33,17 +33,18 @@ 
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
+#include "bitstream.h"
 #include "blockdsp.h"
-#include "get_bits.h"
 #include "hpeldsp.h"
 #include "idctdsp.h"
+#include "vlc.h"
 
 #define MAX_COMPONENTS 4
 
 typedef struct MJpegDecodeContext {
     AVClass *class;
     AVCodecContext *avctx;
-    GetBitContext gb;
+    BitstreamContext bc;
 
     int start_code; /* current start code */
     int buffer_size;
diff --git a/libavcodec/mxpegdec.c b/libavcodec/mxpegdec.c
index a8ef6d0..a300c6a 100644
--- a/libavcodec/mxpegdec.c
+++ b/libavcodec/mxpegdec.c
@@ -25,6 +25,7 @@ 
  * MxPEG decoder
  */
 
+#include "bitstream.h"
 #include "internal.h"
 #include "mjpeg.h"
 #include "mjpegdec.h"
@@ -82,7 +83,7 @@  static int mxpeg_decode_app(MXpegDecodeContext *s,
     if (buf_size < 2)
         return 0;
     len = AV_RB16(buf_ptr);
-    skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
+    bitstream_skip(&s->jpg.bc, 8 * FFMIN(len, buf_size));
 
     return 0;
 }
@@ -149,7 +150,7 @@  static int mxpeg_decode_com(MXpegDecodeContext *s,
     if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
         ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
     }
-    skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
+    bitstream_skip(&s->jpg.bc, 8 * FFMIN(len, buf_size));
 
     return ret;
 }
@@ -203,7 +204,7 @@  static int mxpeg_decode_frame(AVCodecContext *avctx,
         if (start_code < 0)
             goto the_end;
         {
-            init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
+            bitstream_init8(&jpg->bc, unescaped_buf_ptr, unescaped_buf_size);
 
             if (start_code >= APP0 && start_code <= APP15) {
                 mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
@@ -310,7 +311,7 @@  static int mxpeg_decode_frame(AVCodecContext *avctx,
                 break;
             }
 
-            buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
+            buf_ptr += (bitstream_tell(&jpg->bc) + 7) >> 3;
         }
 
     }