From: Alexandra Hájková <alexan...@khirnov.net>

---
 libavcodec/aic.c                         |   2 +-
 libavcodec/cavs.c                        |   2 +-
 libavcodec/cavsdec.c                     |   2 +-
 libavcodec/dirac.c                       |   2 +-
 libavcodec/ffv1dec.c                     |   3 +-
 libavcodec/ffv1enc.c                     |   2 +-
 libavcodec/fic.c                         |   3 +-
 libavcodec/flacdec.c                     |   2 +-
 libavcodec/flacenc.c                     |   2 +-
 libavcodec/golomb.h                      | 193 ++++++++++++-------------------
 libavcodec/{golomb.h => golomb_legacy.h} |  24 +++-
 libavcodec/h264_cavlc.c                  |   2 +-
 libavcodec/h264_parse.c                  |   2 +-
 libavcodec/h264_parser.c                 |   2 +-
 libavcodec/h264_ps.c                     |   3 +-
 libavcodec/h264_refs.c                   |   2 +-
 libavcodec/h264_sei.c                    |   2 +-
 libavcodec/h264_slice.c                  |   2 +-
 libavcodec/h264dec.c                     |   2 +-
 libavcodec/hevc_parser.c                 |   2 +-
 libavcodec/hevc_ps.c                     |   2 +-
 libavcodec/hevc_ps_enc.c                 |   2 +-
 libavcodec/hevc_sei.c                    |   2 +-
 libavcodec/hevcdec.c                     |   2 +-
 libavcodec/jpeglsdec.c                   |   2 +-
 libavcodec/jpeglsenc.c                   |   2 +-
 libavcodec/loco.c                        |   2 +-
 libavcodec/ralf.c                        |   2 +-
 libavcodec/rv30.c                        |   2 +-
 libavcodec/rv34.c                        |   2 +-
 libavcodec/rv40.c                        |   2 +-
 libavcodec/shorten.c                     |   2 +-
 libavcodec/svq3.c                        |   3 +-
 libavcodec/tests/golomb.c                |  22 ++--
 libavformat/hevc.c                       |   2 +-
 35 files changed, 143 insertions(+), 164 deletions(-)
 copy libavcodec/{golomb.h => golomb_legacy.h} (96%)

diff --git a/libavcodec/aic.c b/libavcodec/aic.c
index 2c9b6f8..405ebf1 100644
--- a/libavcodec/aic.c
+++ b/libavcodec/aic.c
@@ -26,7 +26,7 @@
 #include "bytestream.h"
 #include "internal.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "idctdsp.h"
 #include "thread.h"
 #include "unary_legacy.h"
diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index cf66629..bf1a59a 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "h264chroma.h"
 #include "idctdsp.h"
 #include "internal.h"
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 1c4f276..5a28fc0 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "cavs.h"
 #include "internal.h"
 #include "mpeg12data.h"
diff --git a/libavcodec/dirac.c b/libavcodec/dirac.c
index 142af20..cce9439 100644
--- a/libavcodec/dirac.c
+++ b/libavcodec/dirac.c
@@ -29,7 +29,7 @@
 
 #include "avcodec.h"
 #include "dirac.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "mpeg12data.h"
 
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index d3169ec..2ebd686 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -31,12 +31,13 @@
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/timer.h"
+
 #include "avcodec.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "get_bits.h"
 #include "put_bits.h"
 #include "rangecoder.h"
-#include "golomb.h"
 #include "mathops.h"
 #include "ffv1.h"
 
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 0eeccff..c5088bb 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -33,10 +33,10 @@
 #include "libavutil/imgutils.h"
 
 #include "avcodec.h"
+#include "golomb.h"
 #include "internal.h"
 #include "put_bits.h"
 #include "rangecoder.h"
-#include "golomb.h"
 #include "mathops.h"
 #include "ffv1.h"
 
diff --git a/libavcodec/fic.c b/libavcodec/fic.c
index b1286eb..1804104 100644
--- a/libavcodec/fic.c
+++ b/libavcodec/fic.c
@@ -22,10 +22,11 @@
  */
 
 #include "libavutil/common.h"
+
 #include "avcodec.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "get_bits.h"
-#include "golomb.h"
 
 typedef struct FICThreadContext {
     DECLARE_ALIGNED(16, int16_t, block)[64];
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index 7af71f3..78be2ad 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -37,7 +37,7 @@
 #include "internal.h"
 #include "get_bits.h"
 #include "bytestream.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "flac.h"
 #include "flacdata.h"
 #include "flacdsp.h"
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 67f899f..0c1e6b7 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -26,7 +26,7 @@
 
 #include "avcodec.h"
 #include "bswapdsp.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "lpc.h"
 #include "flac.h"
diff --git a/libavcodec/golomb.h b/libavcodec/golomb.h
index 9fafbcd..ccb6818 100644
--- a/libavcodec/golomb.h
+++ b/libavcodec/golomb.h
@@ -32,7 +32,7 @@
 
 #include <stdint.h>
 
-#include "get_bits.h"
+#include "bitstream.h"
 #include "put_bits.h"
 
 #define INVALID_VLC           0x80000000
@@ -50,26 +50,24 @@ extern const uint8_t 
ff_interleaved_dirac_golomb_vlc_code[256];
 /**
  * read unsigned exp golomb code.
  */
-static inline int get_ue_golomb(GetBitContext *gb)
+static inline int get_ue_golomb(BitstreamContext *bc)
 {
     unsigned int buf;
+    int ret;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     if (buf >= (1 << 27)) {
         buf >>= 32 - 9;
-        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, ff_golomb_vlc_len[buf]);
 
-        return ff_ue_golomb_vlc_code[buf];
+        ret = ff_ue_golomb_vlc_code[buf];
+        return ret;
     } else {
         int log = 2 * av_log2(buf) - 31;
         buf >>= log;
         buf--;
-        LAST_SKIP_BITS(re, gb, 32 - log);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, 32 - log);
 
         return buf;
     }
@@ -78,48 +76,42 @@ static inline int get_ue_golomb(GetBitContext *gb)
 /**
  * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
  */
-static inline unsigned get_ue_golomb_long(GetBitContext *gb)
+static inline unsigned get_ue_golomb_long(BitstreamContext *bc)
 {
     unsigned buf, log;
 
-    buf = show_bits_long(gb, 32);
+    buf = bitstream_peek(bc, 32);
     log = 31 - av_log2(buf);
-    skip_bits_long(gb, log);
+    bitstream_skip(bc, log);
 
-    return get_bits_long(gb, log + 1) - 1;
+    return bitstream_read(bc, log + 1) - 1;
 }
 
 /**
  * read unsigned exp golomb code, constraint to a max of 31.
  * the return value is undefined if the stored value exceeds 31.
  */
-static inline int get_ue_golomb_31(GetBitContext *gb)
+static inline int get_ue_golomb_31(BitstreamContext *bc)
 {
     unsigned int buf;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     buf >>= 32 - 9;
-    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
-    CLOSE_READER(re, gb);
+    bitstream_skip(bc, ff_golomb_vlc_len[buf]);
 
     return ff_ue_golomb_vlc_code[buf];
 }
 
-static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
+static inline unsigned get_interleaved_ue_golomb(BitstreamContext *bc)
 {
     uint32_t buf;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     if (buf & 0xAA800000) {
         buf >>= 32 - 8;
-        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, ff_interleaved_golomb_vlc_len[buf]);
 
         return ff_interleaved_ue_golomb_vlc_code[buf];
     } else {
@@ -127,8 +119,7 @@ static inline unsigned 
get_interleaved_ue_golomb(GetBitContext *gb)
 
         do {
             buf >>= 32 - 8;
-            LAST_SKIP_BITS(re, gb,
-                           FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
+            bitstream_skip(bc, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
 
             if (ff_interleaved_golomb_vlc_len[buf] != 9) {
                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
@@ -136,11 +127,9 @@ static inline unsigned 
get_interleaved_ue_golomb(GetBitContext *gb)
                 break;
             }
             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
-            UPDATE_CACHE(re, gb);
-            buf = GET_CACHE(re, gb);
-        } while (BITS_AVAILABLE(re, gb));
+            buf = bitstream_peek(bc, 32);
+        } while (bitstream_bits_left(bc) > 0);
 
-        CLOSE_READER(re, gb);
         return ret - 1;
     }
 }
@@ -148,54 +137,50 @@ static inline unsigned 
get_interleaved_ue_golomb(GetBitContext *gb)
 /**
  * read unsigned truncated exp golomb code.
  */
-static inline int get_te0_golomb(GetBitContext *gb, int range)
+static inline int get_te0_golomb(BitstreamContext *bc, int range)
 {
     assert(range >= 1);
 
     if (range == 1)
         return 0;
     else if (range == 2)
-        return get_bits1(gb) ^ 1;
+        return bitstream_read_bit(bc) ^ 1;
     else
-        return get_ue_golomb(gb);
+        return get_ue_golomb(bc);
 }
 
 /**
  * read unsigned truncated exp golomb code.
  */
-static inline int get_te_golomb(GetBitContext *gb, int range)
+static inline int get_te_golomb(BitstreamContext *bc, int range)
 {
     assert(range >= 1);
 
     if (range == 2)
-        return get_bits1(gb) ^ 1;
+        return bitstream_read_bit(bc) ^ 1;
     else
-        return get_ue_golomb(gb);
+        return get_ue_golomb(bc);
 }
 
 /**
  * read signed exp golomb code.
  */
-static inline int get_se_golomb(GetBitContext *gb)
+static inline int get_se_golomb(BitstreamContext *bc)
 {
     unsigned int buf;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     if (buf >= (1 << 27)) {
         buf >>= 32 - 9;
-        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, ff_golomb_vlc_len[buf]);
 
         return ff_se_golomb_vlc_code[buf];
     } else {
         int log = 2 * av_log2(buf) - 31;
         buf >>= log;
 
-        LAST_SKIP_BITS(re, gb, 32 - log);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, 32 - log);
 
         if (buf & 1)
             buf = -(buf >> 1);
@@ -206,9 +191,9 @@ static inline int get_se_golomb(GetBitContext *gb)
     }
 }
 
-static inline int get_se_golomb_long(GetBitContext *gb)
+static inline int get_se_golomb_long(BitstreamContext *bc)
 {
-    unsigned int buf = get_ue_golomb_long(gb);
+    unsigned int buf = get_ue_golomb_long(bc);
 
     if (buf & 1)
         buf = (buf + 1) >> 1;
@@ -218,25 +203,21 @@ static inline int get_se_golomb_long(GetBitContext *gb)
     return buf;
 }
 
-static inline int get_interleaved_se_golomb(GetBitContext *gb)
+static inline int get_interleaved_se_golomb(BitstreamContext *bc)
 {
     unsigned int buf;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     if (buf & 0xAA800000) {
         buf >>= 32 - 8;
-        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, ff_interleaved_golomb_vlc_len[buf]);
 
         return ff_interleaved_se_golomb_vlc_code[buf];
     } else {
         int log;
-        LAST_SKIP_BITS(re, gb, 8);
-        UPDATE_CACHE(re, gb);
-        buf |= 1 | (GET_CACHE(re, gb) >> 8);
+        bitstream_skip(bc, 8);
+        buf |= 1 | bitstream_peek(bc, 24);
 
         if ((buf & 0xAAAAAAAA) == 0)
             return INVALID_VLC;
@@ -244,25 +225,20 @@ static inline int get_interleaved_se_golomb(GetBitContext 
*gb)
         for (log = 31; (buf & 0x80000000) == 0; log--)
             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
 
-        LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, 63 - 2 * log - 8);
 
         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 
1;
     }
 }
 
-static inline int dirac_get_se_golomb(GetBitContext *gb)
+static inline int dirac_get_se_golomb(BitstreamContext *bc)
 {
-    uint32_t ret = get_interleaved_ue_golomb(gb);
+    uint32_t ret = get_interleaved_ue_golomb(bc);
 
     if (ret) {
         uint32_t buf;
-        OPEN_READER(re, gb);
-        UPDATE_CACHE(re, gb);
-        buf = SHOW_SBITS(re, gb, 1);
-        LAST_SKIP_BITS(re, gb, 1);
+        buf = bitstream_read_signed(bc, 1);
         ret = (ret ^ buf) - buf;
-        CLOSE_READER(re, gb);
     }
 
     return ret;
@@ -271,33 +247,25 @@ static inline int dirac_get_se_golomb(GetBitContext *gb)
 /**
  * read unsigned golomb rice code (ffv1).
  */
-static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
+static inline int get_ur_golomb(BitstreamContext *bc, int k, int limit,
                                 int esc_len)
 {
     unsigned int buf;
     int log;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     log = av_log2(buf);
 
     if (log > 31 - limit) {
         buf >>= log - k;
         buf  += (30 - log) << k;
-        LAST_SKIP_BITS(re, gb, 32 + k - log);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, 32 + k - log);
 
         return buf;
     } else {
-        LAST_SKIP_BITS(re, gb, limit);
-        UPDATE_CACHE(re, gb);
-
-        buf = SHOW_UBITS(re, gb, esc_len);
-
-        LAST_SKIP_BITS(re, gb, esc_len);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, limit);
+        buf = bitstream_read(bc, esc_len);
 
         return buf + limit - 1;
     }
@@ -306,48 +274,39 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, 
int limit,
 /**
  * read unsigned golomb rice code (jpegls).
  */
-static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
+static inline int get_ur_golomb_jpegls(BitstreamContext *bc, int k, int limit,
                                        int esc_len)
 {
     unsigned int buf;
     int log;
 
-    OPEN_READER(re, gb);
-    UPDATE_CACHE(re, gb);
-    buf = GET_CACHE(re, gb);
+    buf = bitstream_peek(bc, 32);
 
     log = av_log2(buf);
 
-    if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
-        32 - log < limit) {
+    if (log - k >= 1 && 32 - log < limit) {
         buf >>= log - k;
         buf  += (30 - log) << k;
-        LAST_SKIP_BITS(re, gb, 32 + k - log);
-        CLOSE_READER(re, gb);
+        bitstream_skip(bc, 32 + k - log);
 
         return buf;
     } else {
         int i;
-        for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0 && 
BITS_AVAILABLE(re, gb); i++) {
-            LAST_SKIP_BITS(re, gb, 1);
-            UPDATE_CACHE(re, gb);
+        for (i = 0; i < limit && bitstream_peek(bc, 1) == 0 && 
bitstream_bits_left(bc) > 0; i++) {
+            bitstream_skip(bc, 1);
         }
-        SKIP_BITS(re, gb, 1);
+        bitstream_skip(bc, 1);
 
         if (i < limit - 1) {
             if (k) {
-                buf = SHOW_UBITS(re, gb, k);
-                LAST_SKIP_BITS(re, gb, k);
+                buf = bitstream_read(bc, k);
             } else {
                 buf = 0;
             }
 
-            CLOSE_READER(re, gb);
             return buf + (i << k);
         } else if (i == limit - 1) {
-            buf = SHOW_UBITS(re, gb, esc_len);
-            LAST_SKIP_BITS(re, gb, esc_len);
-            CLOSE_READER(re, gb);
+            buf = bitstream_read(bc, esc_len);
 
             return buf + 1;
         } else
@@ -358,10 +317,10 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, 
int k, int limit,
 /**
  * read signed golomb rice code (ffv1).
  */
-static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
+static inline int get_sr_golomb(BitstreamContext *bc, int k, int limit,
                                 int esc_len)
 {
-    int v = get_ur_golomb(gb, k, limit, esc_len);
+    int v = get_ur_golomb(bc, k, limit, esc_len);
 
     v++;
     if (v & 1)
@@ -375,27 +334,27 @@ static inline int get_sr_golomb(GetBitContext *gb, int k, 
int limit,
 /**
  * read signed golomb rice code (flac).
  */
-static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
+static inline int get_sr_golomb_flac(BitstreamContext *bc, int k, int limit,
                                      int esc_len)
 {
-    int v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
+    int v = get_ur_golomb_jpegls(bc, k, limit, esc_len);
     return (v >> 1) ^ -(v & 1);
 }
 
 /**
  * read unsigned golomb rice code (shorten).
  */
-static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
+static inline unsigned int get_ur_golomb_shorten(BitstreamContext *bc, int k)
 {
-    return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
+    return get_ur_golomb_jpegls(bc, k, INT_MAX, 0);
 }
 
 /**
  * read signed golomb rice code (shorten).
  */
-static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
+static inline int get_sr_golomb_shorten(BitstreamContext *bc, int k)
 {
-    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
+    int uvar = get_ur_golomb_jpegls(bc, k + 1, INT_MAX, 0);
     if (uvar & 1)
         return ~(uvar >> 1);
     else
@@ -404,13 +363,13 @@ static inline int get_sr_golomb_shorten(GetBitContext 
*gb, int k)
 
 #ifdef TRACE
 
-static inline int get_ue(GetBitContext *s, const char *file, const char *func,
+static inline int get_ue(BitstreamContext *s, const char *file, const char 
*func,
                          int line)
 {
-    int show = show_bits(s, 24);
-    int pos  = get_bits_count(s);
+    int show = bitstream_peek(s, 24);
+    int pos  = bitstream_tell(s);
     int i    = get_ue_golomb(s);
-    int len  = get_bits_count(s) - pos;
+    int len  = bitstream_tell(s) - pos;
     int bits = show >> (24 - len);
 
     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
@@ -419,13 +378,13 @@ static inline int get_ue(GetBitContext *s, const char 
*file, const char *func,
     return i;
 }
 
-static inline int get_se(GetBitContext *s, const char *file, const char *func,
+static inline int get_se(BitstreamContext *s, const char *file, const char 
*func,
                          int line)
 {
-    int show = show_bits(s, 24);
-    int pos  = get_bits_count(s);
+    int show = bitstream_peek(s, 24);
+    int pos  = bitstream_tell(s);
     int i    = get_se_golomb(s);
-    int len  = get_bits_count(s) - pos;
+    int len  = bitstream_tell(s) - pos;
     int bits = show >> (24 - len);
 
     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
@@ -434,13 +393,13 @@ static inline int get_se(GetBitContext *s, const char 
*file, const char *func,
     return i;
 }
 
-static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
+static inline int get_te(BitstreamContext *s, int r, char *file, const char 
*func,
                          int line)
 {
-    int show = show_bits(s, 24);
-    int pos  = get_bits_count(s);
+    int show = bitstream_peek(s, 24);
+    int pos  = bitstream_tell(s);
     int i    = get_te0_golomb(s, r);
-    int len  = get_bits_count(s) - pos;
+    int len  = bitstream_tell(s) - pos;
     int bits = show >> (24 - len);
 
     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
diff --git a/libavcodec/golomb.h b/libavcodec/golomb_legacy.h
similarity index 96%
copy from libavcodec/golomb.h
copy to libavcodec/golomb_legacy.h
index 9fafbcd..2d0be89 100644
--- a/libavcodec/golomb.h
+++ b/libavcodec/golomb_legacy.h
@@ -449,10 +449,10 @@ static inline int get_te(GetBitContext *s, int r, char 
*file, const char *func,
     return i;
 }
 
-#define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
-#define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
-#define get_te_golomb(a, r)  get_te(a, r, __FILE__, __func__, __LINE__)
-#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
+#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define get_te_golomb(a, r)  get_te(a, r, __FILE__, __PRETTY_FUNCTION__, 
__LINE__)
+#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, 
__LINE__)
 
 #endif /* TRACE */
 
@@ -463,6 +463,12 @@ static inline void set_ue_golomb(PutBitContext *pb, int i)
 {
     assert(i >= 0);
 
+#if 0
+    if (i = 0) {
+        put_bits(pb, 1, 1);
+        return;
+    }
+#endif
     if (i < 256)
         put_bits(pb, ff_ue_golomb_len[i], i + 1);
     else {
@@ -490,9 +496,19 @@ static inline void set_te_golomb(PutBitContext *pb, int i, 
int range)
  */
 static inline void set_se_golomb(PutBitContext *pb, int i)
 {
+#if 0
+    if (i <= 0)
+        i = -2 * i;
+    else
+        i = 2 * i - 1;
+#elif 1
     i = 2 * i - 1;
     if (i < 0)
         i ^= -1;    //FIXME check if gcc does the right thing
+#else
+    i  = 2 * i - 1;
+    i ^= (i >> 31);
+#endif
     set_ue_golomb(pb, i);
 }
 
diff --git a/libavcodec/h264_cavlc.c b/libavcodec/h264_cavlc.c
index 4fa2de0..9b950ed 100644
--- a/libavcodec/h264_cavlc.c
+++ b/libavcodec/h264_cavlc.c
@@ -32,7 +32,7 @@
 #include "h264dec.h"
 #include "h264_mvpred.h"
 #include "h264data.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "mpegutils.h"
 
 #include <assert.h>
diff --git a/libavcodec/h264_parse.c b/libavcodec/h264_parse.c
index d694558..cde46fa 100644
--- a/libavcodec/h264_parse.c
+++ b/libavcodec/h264_parse.c
@@ -18,7 +18,7 @@
 
 #include "bytestream.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "h264.h"
 #include "h264dec.h"
 #include "h264_parse.h"
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index b4da123..6de37c0 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -36,7 +36,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "h264.h"
 #include "h264_sei.h"
 #include "h264_ps.h"
diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c
index 7ee3876..b3a0e8a 100644
--- a/libavcodec/h264_ps.c
+++ b/libavcodec/h264_ps.c
@@ -28,12 +28,13 @@
 #include <inttypes.h>
 
 #include "libavutil/imgutils.h"
+
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "mathops.h"
 #include "avcodec.h"
 #include "h264data.h"
 #include "h264_ps.h"
-#include "golomb.h"
 
 #define MAX_LOG2_MAX_FRAME_NUM    (12 + 4)
 #define MIN_LOG2_MAX_FRAME_NUM    4
diff --git a/libavcodec/h264_refs.c b/libavcodec/h264_refs.c
index 3c55998..b4dc49c 100644
--- a/libavcodec/h264_refs.c
+++ b/libavcodec/h264_refs.c
@@ -27,11 +27,11 @@
 
 #include <inttypes.h>
 
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "avcodec.h"
 #include "h264.h"
 #include "h264dec.h"
-#include "golomb.h"
 #include "mpegutils.h"
 
 #include <assert.h>
diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c
index 7eef3ee..4bf001a 100644
--- a/libavcodec/h264_sei.c
+++ b/libavcodec/h264_sei.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "h264_ps.h"
 #include "h264_sei.h"
 #include "internal.h"
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index 3749d1f..f1f5fc0 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -34,6 +34,7 @@
 #include "cabac.h"
 #include "cabac_functions.h"
 #include "error_resilience.h"
+#include "golomb_legacy.h"
 #include "avcodec.h"
 #include "h264.h"
 #include "h264dec.h"
@@ -41,7 +42,6 @@
 #include "h264chroma.h"
 #include "h264_mvpred.h"
 #include "h264_ps.h"
-#include "golomb.h"
 #include "mathops.h"
 #include "mpegutils.h"
 #include "rectangle.h"
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 834c60c..5137039 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -36,6 +36,7 @@
 #include "cabac_functions.h"
 #include "error_resilience.h"
 #include "avcodec.h"
+#include "golomb_legacy.h"
 #include "h264.h"
 #include "h264dec.h"
 #include "h2645_parse.h"
@@ -43,7 +44,6 @@
 #include "h264chroma.h"
 #include "h264_mvpred.h"
 #include "h264_ps.h"
-#include "golomb.h"
 #include "mathops.h"
 #include "me_cmp.h"
 #include "mpegutils.h"
diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c
index 74a0257..5129e3a 100644
--- a/libavcodec/hevc_parser.c
+++ b/libavcodec/hevc_parser.c
@@ -22,7 +22,7 @@
 
 #include "libavutil/common.h"
 
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "hevc.h"
 #include "hevcdec.h"
 #include "h2645_parse.h"
diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index 4a5a47e..3c98e4c 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -25,7 +25,7 @@
 
 #include "libavutil/imgutils.h"
 
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "hevc_data.h"
 #include "hevc_ps.h"
 
diff --git a/libavcodec/hevc_ps_enc.c b/libavcodec/hevc_ps_enc.c
index 4864e6e..1fb93b3 100644
--- a/libavcodec/hevc_ps_enc.c
+++ b/libavcodec/hevc_ps_enc.c
@@ -18,7 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "hevc_ps.h"
 #include "put_bits.h"
 
diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index 8c913f9..1f8554a 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -22,7 +22,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "hevcdec.h"
 
 enum HEVC_SEI_TYPE {
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index dcb2453..e24ce1e 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -35,7 +35,7 @@
 #include "bswapdsp.h"
 #include "bytestream.h"
 #include "cabac_functions.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "hevc.h"
 #include "hevc_data.h"
 #include "hevcdec.h"
diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index 9f8ccec..8d1e763 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "mathops.h"
 #include "mjpeg.h"
diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c
index cb9b71e..fb3c69f 100644
--- a/libavcodec/jpeglsenc.c
+++ b/libavcodec/jpeglsenc.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "mathops.h"
 #include "mjpeg.h"
diff --git a/libavcodec/loco.c b/libavcodec/loco.c
index f25ef61..8624ea8 100644
--- a/libavcodec/loco.c
+++ b/libavcodec/loco.c
@@ -26,7 +26,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "mathops.h"
 
diff --git a/libavcodec/ralf.c b/libavcodec/ralf.c
index 9812837..6bc0f3f 100644
--- a/libavcodec/ralf.c
+++ b/libavcodec/ralf.c
@@ -30,7 +30,7 @@
 #include "libavutil/channel_layout.h"
 #include "avcodec.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "unary_legacy.h"
 #include "ralfdata.h"
diff --git a/libavcodec/rv30.c b/libavcodec/rv30.c
index 7218fa3..77e875b 100644
--- a/libavcodec/rv30.c
+++ b/libavcodec/rv30.c
@@ -25,9 +25,9 @@
  */
 
 #include "avcodec.h"
+#include "golomb_legacy.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
-#include "golomb.h"
 
 #include "rv34.h"
 #include "rv30data.h"
diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index 4220195..9fb0760 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -28,9 +28,9 @@
 
 #include "avcodec.h"
 #include "error_resilience.h"
+#include "golomb_legacy.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
-#include "golomb.h"
 #include "internal.h"
 #include "mathops.h"
 #include "mpeg_er.h"
diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index 0da1312..d46a44a 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -27,9 +27,9 @@
 #include "libavutil/imgutils.h"
 
 #include "avcodec.h"
+#include "golomb_legacy.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
-#include "golomb.h"
 
 #include "rv34.h"
 #include "rv40vlc2.h"
diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c
index 00d1661..82fa2ab 100644
--- a/libavcodec/shorten.c
+++ b/libavcodec/shorten.c
@@ -29,7 +29,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "get_bits.h"
-#include "golomb.h"
+#include "golomb_legacy.h"
 #include "internal.h"
 
 #define MAX_CHANNELS 8
diff --git a/libavcodec/svq3.c b/libavcodec/svq3.c
index 5165737..8bbd331 100644
--- a/libavcodec/svq3.c
+++ b/libavcodec/svq3.c
@@ -43,12 +43,13 @@
 #include <inttypes.h>
 
 #include "libavutil/attributes.h"
+
+#include "golomb_legacy.h"
 #include "internal.h"
 #include "avcodec.h"
 #include "mpegutils.h"
 #include "h264dec.h"
 #include "h264data.h"
-#include "golomb.h"
 #include "hpeldsp.h"
 #include "mathops.h"
 #include "rectangle.h"
diff --git a/libavcodec/tests/golomb.c b/libavcodec/tests/golomb.c
index 7587402..d8fff71 100644
--- a/libavcodec/tests/golomb.c
+++ b/libavcodec/tests/golomb.c
@@ -21,7 +21,7 @@
 
 #include "libavutil/mem.h"
 
-#include "libavcodec/get_bits.h"
+#include "libavcodec/bitstream.h"
 #include "libavcodec/put_bits.h"
 #include "libavcodec/golomb.h"
 
@@ -33,7 +33,7 @@ int main(void)
     int i, ret = 0;
     uint8_t *temp;
     PutBitContext pb;
-    GetBitContext gb;
+    BitstreamContext bc;
 
     temp = av_malloc(SIZE);
     if (!temp)
@@ -44,11 +44,11 @@ int main(void)
         set_ue_golomb(&pb, i);
     flush_put_bits(&pb);
 
-    init_get_bits(&gb, temp, 8 * SIZE);
+    bitstream_init8(&bc, temp, SIZE);
     for (i = 0; i < COUNT; i++) {
-        int j, s = show_bits(&gb, 25);
+        int j, s = bitstream_peek(&bc, 25);
 
-        j = get_ue_golomb(&gb);
+        j = get_ue_golomb(&bc);
         if (j != i) {
             fprintf(stderr, "get_ue_golomb: expected %d, got %d. bits: %7x\n",
                     i, j, s);
@@ -62,11 +62,11 @@ int main(void)
         set_ue_golomb(&pb, EXTEND(i));
     flush_put_bits(&pb);
 
-    init_get_bits(&gb, temp, 8 * SIZE);
+    bitstream_init8(&bc, temp, SIZE);
     for (i = 0; i < COUNT; i++) {
-        int j, s = show_bits_long(&gb, 32);
+        int j, s = bitstream_peek(&bc, 32);
 
-        j = get_ue_golomb_long(&gb);
+        j = get_ue_golomb_long(&bc);
         if (j != EXTEND(i)) {
             fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. "
                     "bits: %8x\n", EXTEND(i), j, s);
@@ -79,11 +79,11 @@ int main(void)
         set_se_golomb(&pb, i - COUNT / 2);
     flush_put_bits(&pb);
 
-    init_get_bits(&gb, temp, 8 * SIZE);
+    bitstream_init8(&bc, temp, SIZE);
     for (i = 0; i < COUNT; i++) {
-        int j, s = show_bits(&gb, 25);
+        int j, s = bitstream_peek(&bc, 25);
 
-        j = get_se_golomb(&gb);
+        j = get_se_golomb(&bc);
         if (j != i - COUNT / 2) {
             fprintf(stderr, "get_se_golomb: expected %d, got %d. bits: %7x\n",
                     i - COUNT / 2, j, s);
diff --git a/libavformat/hevc.c b/libavformat/hevc.c
index 62eefc6..057f651 100644
--- a/libavformat/hevc.c
+++ b/libavformat/hevc.c
@@ -20,7 +20,7 @@
 
 #include "libavcodec/avcodec.h"
 #include "libavcodec/get_bits.h"
-#include "libavcodec/golomb.h"
+#include "libavcodec/golomb_legacy.h"
 #include "libavcodec/hevc.h"
 #include "libavutil/intreadwrite.h"
 #include "avc.h"
-- 
2.1.4

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

Reply via email to