From d4c2103bd30ff6ceea70c3696ae88a7d2ea72493 Mon Sep 17 00:00:00 2001 From: Diego Biurrun Date: Mon, 30 Jan 2017 20:24:38 +0100 Subject: [PATCH 01/10] golomb: Convert to the new bitstream reader --- 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 | 190 +++++------- libavcodec/golomb_legacy.h | 573 +++++++++++++++++++++++++++++++++++++ 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, 693 insertions(+), 160 deletions(-) create mode 100644 libavcodec/golomb_legacy.h diff --git a/libavcodec/aic.c b/libavcodec/aic.c index 2c9b6f8098..405ebf12d5 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 cf66629ffd..bf1a59a6f0 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 1c4f276373..5a28fc04ed 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 142af20579..cce9439d44 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 d3169ec7c5..2ebd6864a1 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 0eeccfffb4..adf70d8a6b 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -33,10 +33,10 @@ #include "libavutil/imgutils.h" #include "avcodec.h" +#include "golomb_legacy.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 b1286ebe65..1804104916 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 7af71f3c0c..78be2adab3 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 67f899f712..0c1e6b7673 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 9fafbcda70..648f9243fc 100644 --- a/libavcodec/golomb.h +++ b/libavcodec/golomb.h @@ -32,7 +32,7 @@ #include -#include "get_bits.h" +#include "bitstream.h" #include "put_bits.h" #define INVALID_VLC 0x80000000 @@ -50,26 +50,22 @@ 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; - 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]; } 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 +74,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 +117,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 +125,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 +135,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 +189,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 +201,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 +223,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 +245,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 +272,38 @@ 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); - } - SKIP_BITS(re, gb, 1); + for (i = 0; i < limit && bitstream_peek(bc, 1) == 0 && bitstream_bits_left(bc) > 0; i++) + bitstream_skip(bc, 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 +314,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 +331,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 +360,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 +375,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 +390,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_legacy.h b/libavcodec/golomb_legacy.h new file mode 100644 index 0000000000..9fafbcda70 --- /dev/null +++ b/libavcodec/golomb_legacy.h @@ -0,0 +1,573 @@ +/* + * exp golomb vlc stuff + * Copyright (c) 2003 Michael Niedermayer + * Copyright (c) 2004 Alex Beregszaszi + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * @brief + * exp golomb vlc stuff + * @author Michael Niedermayer and Alex Beregszaszi + */ + +#ifndef AVCODEC_GOLOMB_H +#define AVCODEC_GOLOMB_H + +#include + +#include "get_bits.h" +#include "put_bits.h" + +#define INVALID_VLC 0x80000000 + +extern const uint8_t ff_golomb_vlc_len[512]; +extern const uint8_t ff_ue_golomb_vlc_code[512]; +extern const int8_t ff_se_golomb_vlc_code[512]; +extern const uint8_t ff_ue_golomb_len[256]; + +extern const uint8_t ff_interleaved_golomb_vlc_len[256]; +extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256]; +extern const int8_t ff_interleaved_se_golomb_vlc_code[256]; +extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]; + +/** + * read unsigned exp golomb code. + */ +static inline int get_ue_golomb(GetBitContext *gb) +{ + unsigned int buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_ue_golomb_vlc_code[buf]; + } else { + int log = 2 * av_log2(buf) - 31; + buf >>= log; + buf--; + LAST_SKIP_BITS(re, gb, 32 - log); + CLOSE_READER(re, gb); + + return buf; + } +} + +/** + * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1. + */ +static inline unsigned get_ue_golomb_long(GetBitContext *gb) +{ + unsigned buf, log; + + buf = show_bits_long(gb, 32); + log = 31 - av_log2(buf); + skip_bits_long(gb, log); + + return get_bits_long(gb, 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) +{ + unsigned int buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_ue_golomb_vlc_code[buf]; +} + +static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) +{ + uint32_t buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_interleaved_ue_golomb_vlc_code[buf]; + } else { + unsigned ret = 1; + + do { + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, + 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; + ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; + 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)); + + CLOSE_READER(re, gb); + return ret - 1; + } +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int get_te0_golomb(GetBitContext *gb, int range) +{ + assert(range >= 1); + + if (range == 1) + return 0; + else if (range == 2) + return get_bits1(gb) ^ 1; + else + return get_ue_golomb(gb); +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int get_te_golomb(GetBitContext *gb, int range) +{ + assert(range >= 1); + + if (range == 2) + return get_bits1(gb) ^ 1; + else + return get_ue_golomb(gb); +} + +/** + * read signed exp golomb code. + */ +static inline int get_se_golomb(GetBitContext *gb) +{ + unsigned int buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + 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); + + if (buf & 1) + buf = -(buf >> 1); + else + buf = (buf >> 1); + + return buf; + } +} + +static inline int get_se_golomb_long(GetBitContext *gb) +{ + unsigned int buf = get_ue_golomb_long(gb); + + if (buf & 1) + buf = (buf + 1) >> 1; + else + buf = -(buf >> 1); + + return buf; +} + +static inline int get_interleaved_se_golomb(GetBitContext *gb) +{ + unsigned int buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + 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); + + if ((buf & 0xAAAAAAAA) == 0) + return INVALID_VLC; + + 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); + + return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; + } +} + +static inline int dirac_get_se_golomb(GetBitContext *gb) +{ + uint32_t ret = get_interleaved_ue_golomb(gb); + + 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); + ret = (ret ^ buf) - buf; + CLOSE_READER(re, gb); + } + + return ret; +} + +/** + * read unsigned golomb rice code (ffv1). + */ +static inline int get_ur_golomb(GetBitContext *gb, 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); + + 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); + + 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); + + return buf + limit - 1; + } +} + +/** + * read unsigned golomb rice code (jpegls). + */ +static inline int get_ur_golomb_jpegls(GetBitContext *gb, 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); + + log = av_log2(buf); + + if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) && + 32 - log < limit) { + buf >>= log - k; + buf += (30 - log) << k; + LAST_SKIP_BITS(re, gb, 32 + k - log); + CLOSE_READER(re, gb); + + 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); + } + SKIP_BITS(re, gb, 1); + + if (i < limit - 1) { + if (k) { + buf = SHOW_UBITS(re, gb, k); + LAST_SKIP_BITS(re, gb, 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); + + return buf + 1; + } else + return -1; + } +} + +/** + * read signed golomb rice code (ffv1). + */ +static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, + int esc_len) +{ + int v = get_ur_golomb(gb, k, limit, esc_len); + + v++; + if (v & 1) + return v >> 1; + else + return -(v >> 1); + +// return (v>>1) ^ -(v&1); +} + +/** + * read signed golomb rice code (flac). + */ +static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, + int esc_len) +{ + int v = get_ur_golomb_jpegls(gb, 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) +{ + return get_ur_golomb_jpegls(gb, k, INT_MAX, 0); +} + +/** + * read signed golomb rice code (shorten). + */ +static inline int get_sr_golomb_shorten(GetBitContext *gb, int k) +{ + int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0); + if (uvar & 1) + return ~(uvar >> 1); + else + return uvar >> 1; +} + +#ifdef TRACE + +static inline int get_ue(GetBitContext *s, const char *file, const char *func, + int line) +{ + int show = show_bits(s, 24); + int pos = get_bits_count(s); + int i = get_ue_golomb(s); + int len = get_bits_count(s) - pos; + int bits = show >> (24 - len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", + bits, len, i, pos, file, func, line); + + return i; +} + +static inline int get_se(GetBitContext *s, const char *file, const char *func, + int line) +{ + int show = show_bits(s, 24); + int pos = get_bits_count(s); + int i = get_se_golomb(s); + int len = get_bits_count(s) - pos; + int bits = show >> (24 - len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", + bits, len, i, pos, file, func, line); + + return i; +} + +static inline int get_te(GetBitContext *s, int r, char *file, const char *func, + int line) +{ + int show = show_bits(s, 24); + int pos = get_bits_count(s); + int i = get_te0_golomb(s, r); + int len = get_bits_count(s) - pos; + int bits = show >> (24 - len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", + bits, len, i, pos, file, func, line); + + 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__) + +#endif /* TRACE */ + +/** + * write unsigned exp golomb code. + */ +static inline void set_ue_golomb(PutBitContext *pb, int i) +{ + assert(i >= 0); + + if (i < 256) + put_bits(pb, ff_ue_golomb_len[i], i + 1); + else { + int e = av_log2(i + 1); + put_bits(pb, 2 * e + 1, i + 1); + } +} + +/** + * write truncated unsigned exp golomb code. + */ +static inline void set_te_golomb(PutBitContext *pb, int i, int range) +{ + assert(range >= 1); + assert(i <= range); + + if (range == 2) + put_bits(pb, 1, i ^ 1); + else + set_ue_golomb(pb, i); +} + +/** + * write signed exp golomb code. 16 bits at most. + */ +static inline void set_se_golomb(PutBitContext *pb, int i) +{ + i = 2 * i - 1; + if (i < 0) + i ^= -1; //FIXME check if gcc does the right thing + set_ue_golomb(pb, i); +} + +/** + * write unsigned golomb rice code (ffv1). + */ +static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, + int esc_len) +{ + int e; + + assert(i >= 0); + + e = i >> k; + if (e < limit) + put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1))); + else + put_bits(pb, limit + esc_len, i - limit + 1); +} + +/** + * write unsigned golomb rice code (jpegls). + */ +static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, + int limit, int esc_len) +{ + int e; + + assert(i >= 0); + + e = (i >> k) + 1; + if (e < limit) { + while (e > 31) { + put_bits(pb, 31, 0); + e -= 31; + } + put_bits(pb, e, 1); + if (k) + put_sbits(pb, k, i); + } else { + while (limit > 31) { + put_bits(pb, 31, 0); + limit -= 31; + } + put_bits(pb, limit, 1); + put_bits(pb, esc_len, i - 1); + } +} + +/** + * write signed golomb rice code (ffv1). + */ +static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, + int esc_len) +{ + int v; + + v = -2 * i - 1; + v ^= (v >> 31); + + set_ur_golomb(pb, v, k, limit, esc_len); +} + +/** + * write signed golomb rice code (flac). + */ +static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, + int limit, int esc_len) +{ + int v; + + v = -2 * i - 1; + v ^= (v >> 31); + + set_ur_golomb_jpegls(pb, v, k, limit, esc_len); +} + +#endif /* AVCODEC_GOLOMB_H */ diff --git a/libavcodec/h264_cavlc.c b/libavcodec/h264_cavlc.c index 4fa2de0184..9b950ede20 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 diff --git a/libavcodec/h264_parse.c b/libavcodec/h264_parse.c index d694558ecc..cde46fa9eb 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 b4da123e94..6de37c0b55 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 7ee3876c8b..b3a0e8a7ed 100644 --- a/libavcodec/h264_ps.c +++ b/libavcodec/h264_ps.c @@ -28,12 +28,13 @@ #include #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 3c55998424..b4dc49cc41 100644 --- a/libavcodec/h264_refs.c +++ b/libavcodec/h264_refs.c @@ -27,11 +27,11 @@ #include +#include "golomb_legacy.h" #include "internal.h" #include "avcodec.h" #include "h264.h" #include "h264dec.h" -#include "golomb.h" #include "mpegutils.h" #include diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c index 7eef3ee962..4bf001ae07 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 3749d1f2ca..f1f5fc05f9 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 834c60c38c..5137039188 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 74a0257073..5129e3a9c1 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 4a5a47e20e..3c98e4ce44 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 4864e6ebfc..1fb93b3020 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 8c913f93a7..1f8554ad5f 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 dcb2453a20..e24ce1e3c0 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 9f8ccecec6..8d1e763113 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 cb9b71e4cd..fb3c69f2f9 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 f25ef61f37..8624ea86ad 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 9812837570..6bc0f3fa0d 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 7218fa36dc..77e875bac9 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 4220195a52..9fb0760ccb 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 0da13124d6..d46a44a5dd 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 00d1661779..82fa2ab943 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 51657372ae..8bbd331de3 100644 --- a/libavcodec/svq3.c +++ b/libavcodec/svq3.c @@ -43,12 +43,13 @@ #include #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 7587402e24..d8fff71ca3 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 62eefc6d29..057f6517ff 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" From 0c89ff82e9ddc68ec92316d786cd8ddc7b6c2b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 23 Apr 2016 15:19:08 +0200 Subject: [PATCH 02/10] aic: Convert to the new bitstream reader --- libavcodec/aic.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/libavcodec/aic.c b/libavcodec/aic.c index 405ebf12d5..368b3bcf23 100644 --- a/libavcodec/aic.c +++ b/libavcodec/aic.c @@ -23,13 +23,13 @@ #include #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" +#include "golomb.h" #include "internal.h" -#include "get_bits.h" -#include "golomb_legacy.h" #include "idctdsp.h" #include "thread.h" -#include "unary_legacy.h" +#include "unary.h" #define AIC_HDR_SIZE 24 #define AIC_BAND_COEFFS (64 + 32 + 192 + 96) @@ -191,14 +191,14 @@ static int aic_decode_header(AICContext *ctx, const uint8_t *src, int size) #define GET_CODE(val, type, add_bits) \ do { \ if (type) \ - val = get_ue_golomb(gb); \ + val = get_ue_golomb(bc); \ else \ - val = get_unary(gb, 1, 31); \ + val = get_unary(bc, 1, 31); \ if (add_bits) \ - val = (val << add_bits) + get_bits(gb, add_bits); \ + val = (val << add_bits) + bitstream_read(bc, add_bits); \ } while (0) -static int aic_decode_coeffs(GetBitContext *gb, int16_t *dst, +static int aic_decode_coeffs(BitstreamContext *bc, int16_t *dst, int band, int slice_width, int force_chroma) { int has_skips, coeff_type, coeff_bits, skip_type, skip_bits; @@ -206,13 +206,13 @@ static int aic_decode_coeffs(GetBitContext *gb, int16_t *dst, const uint8_t *scan = aic_scan[band | force_chroma]; int mb, idx, val; - has_skips = get_bits1(gb); - coeff_type = get_bits1(gb); - coeff_bits = get_bits(gb, 3); + has_skips = bitstream_read_bit(bc); + coeff_type = bitstream_read_bit(bc); + coeff_bits = bitstream_read(bc, 3); if (has_skips) { - skip_type = get_bits1(gb); - skip_bits = get_bits(gb, 3); + skip_type = bitstream_read_bit(bc); + skip_bits = bitstream_read(bc, 3); for (mb = 0; mb < slice_width; mb++) { idx = -1; @@ -303,7 +303,7 @@ static void unquant_block(int16_t *block, int q) static int aic_decode_slice(AICContext *ctx, int mb_x, int mb_y, const uint8_t *src, int src_size) { - GetBitContext gb; + BitstreamContext bc; int ret, i, mb, blk; int slice_width = FFMIN(ctx->slice_width, ctx->mb_width - mb_x); uint8_t *Y, *C[2]; @@ -318,12 +318,12 @@ static int aic_decode_slice(AICContext *ctx, int mb_x, int mb_y, for (i = 0; i < 2; i++) C[i] = ctx->frame->data[i + 1] + mb_x * 8 + mb_y * 8 * ctx->frame->linesize[i + 1]; - init_get_bits(&gb, src, src_size * 8); + bitstream_init8(&bc, src, src_size); memset(ctx->slice_data, 0, sizeof(*ctx->slice_data) * slice_width * AIC_BAND_COEFFS); for (i = 0; i < NUM_BANDS; i++) - if ((ret = aic_decode_coeffs(&gb, ctx->data_ptr[i], + if ((ret = aic_decode_coeffs(&bc, ctx->data_ptr[i], i, slice_width, !ctx->interlaced)) < 0) return ret; From ffc00df0a61b656b3c2ba199c9d153b7787caefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 19 Mar 2016 12:39:03 +0100 Subject: [PATCH 03/10] cavs: Convert to the new bitstream reader --- libavcodec/cavs.c | 8 +- libavcodec/cavs.h | 4 +- libavcodec/cavsdec.c | 178 +++++++++++++++++++++---------------------- 3 files changed, 95 insertions(+), 95 deletions(-) diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c index bf1a59a6f0..6959f54404 100644 --- a/libavcodec/cavs.c +++ b/libavcodec/cavs.c @@ -26,8 +26,8 @@ */ #include "avcodec.h" -#include "get_bits.h" -#include "golomb_legacy.h" +#include "bitstream.h" +#include "golomb.h" #include "h264chroma.h" #include "idctdsp.h" #include "internal.h" @@ -603,8 +603,8 @@ void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC, mv_pred_median(h, mvP, mvA, mvB, mvC); if (mode < MV_PRED_PSKIP) { - mvP->x += get_se_golomb(&h->gb); - mvP->y += get_se_golomb(&h->gb); + mvP->x += get_se_golomb(&h->bc); + mvP->y += get_se_golomb(&h->bc); } set_mvs(mvP, size); } diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h index e8729d50cd..cb549f161e 100644 --- a/libavcodec/cavs.h +++ b/libavcodec/cavs.h @@ -22,11 +22,11 @@ #ifndef AVCODEC_CAVS_H #define AVCODEC_CAVS_H +#include "bitstream.h" #include "cavsdsp.h" #include "blockdsp.h" #include "h264chroma.h" #include "idctdsp.h" -#include "get_bits.h" #include "videodsp.h" #define SLICE_MAX_START_CODE 0x000001af @@ -167,7 +167,7 @@ typedef struct AVSContext { IDCTDSPContext idsp; VideoDSPContext vdsp; CAVSDSPContext cdsp; - GetBitContext gb; + BitstreamContext bc; AVSFrame cur; ///< currently decoded frame AVSFrame DPB[2]; ///< reference frames int dist[2]; ///< temporal distances from current frame to ref frames diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 5a28fc04ed..7f584ac1c0 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -26,9 +26,9 @@ */ #include "avcodec.h" -#include "get_bits.h" -#include "golomb_legacy.h" +#include "bitstream.h" #include "cavs.h" +#include "golomb.h" #include "internal.h" #include "mpeg12data.h" @@ -506,13 +506,13 @@ static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, ****************************************************************************/ /** kth-order exponential golomb code */ -static inline int get_ue_code(GetBitContext *gb, int order) +static inline int get_ue_code(BitstreamContext *bc, int order) { if (order) { - int ret = get_ue_golomb(gb) << order; - return ret + get_bits(gb, order); + int ret = get_ue_golomb(bc) << order; + return ret + bitstream_read(bc, order); } - return get_ue_golomb(gb); + return get_ue_golomb(bc); } static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf, @@ -545,7 +545,7 @@ static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf, * @param dst location of sample block * @param stride line stride in frame buffer */ -static int decode_residual_block(AVSContext *h, GetBitContext *gb, +static int decode_residual_block(AVSContext *h, BitstreamContext *bc, const struct dec_2dvlc *r, int esc_golomb_order, int qp, uint8_t *dst, ptrdiff_t stride) { @@ -555,10 +555,10 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb, int16_t *block = h->block; for (i = 0;i < 65; i++) { - level_code = get_ue_code(gb, r->golomb_order); + level_code = get_ue_code(bc, r->golomb_order); if (level_code >= ESCAPE_CODE) { run = ((level_code - ESCAPE_CODE) >> 1) + 1; - esc_code = get_ue_code(gb, esc_golomb_order); + esc_code = get_ue_code(bc, esc_golomb_order); level = esc_code + (run > r->max_run ? 1 : r->level_add[run]); while (level > r->inc_limit) r++; @@ -588,10 +588,10 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb, static inline void decode_residual_chroma(AVSContext *h) { if (h->cbp & (1 << 4)) - decode_residual_block(h, &h->gb, chroma_dec, 0, + decode_residual_block(h, &h->bc, chroma_dec, 0, cavs_chroma_qp[h->qp], h->cu, h->c_stride); if (h->cbp & (1 << 5)) - decode_residual_block(h, &h->gb, chroma_dec, 0, + decode_residual_block(h, &h->bc, chroma_dec, 0, cavs_chroma_qp[h->qp], h->cv, h->c_stride); } @@ -600,7 +600,7 @@ static inline int decode_residual_inter(AVSContext *h) int block; /* get coded block pattern */ - int cbp = get_ue_golomb(&h->gb); + int cbp = get_ue_golomb(&h->bc); if (cbp > 63 || cbp < 0) { av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp); return AVERROR_INVALIDDATA; @@ -609,10 +609,10 @@ static inline int decode_residual_inter(AVSContext *h) /* get quantizer */ if (h->cbp && !h->qp_fixed) - h->qp = (h->qp + get_se_golomb(&h->gb)) & 63; + h->qp = (h->qp + get_se_golomb(&h->bc)) & 63; for (block = 0; block < 4; block++) if (h->cbp & (1 << block)) - decode_residual_block(h, &h->gb, inter_dec, 0, h->qp, + decode_residual_block(h, &h->bc, inter_dec, 0, h->qp, h->cy + h->luma_scan[block], h->l_stride); decode_residual_chroma(h); @@ -637,7 +637,7 @@ static inline void set_mv_intra(AVSContext *h) static int decode_mb_i(AVSContext *h, int cbp_code) { - GetBitContext *gb = &h->gb; + BitstreamContext *bc = &h->bc; unsigned pred_mode_uv; int block; uint8_t top[18]; @@ -656,13 +656,13 @@ static int decode_mb_i(AVSContext *h, int cbp_code) predpred = FFMIN(nA, nB); if (predpred == NOT_AVAIL) // if either is not available predpred = INTRA_L_LP; - if (!get_bits1(gb)) { - int rem_mode = get_bits(gb, 2); + if (!bitstream_read_bit(bc)) { + int rem_mode = bitstream_read(bc, 2); predpred = rem_mode + (rem_mode >= predpred); } h->pred_mode_Y[pos] = predpred; } - pred_mode_uv = get_ue_golomb(gb); + pred_mode_uv = get_ue_golomb(bc); if (pred_mode_uv > 6) { av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n"); return AVERROR_INVALIDDATA; @@ -671,14 +671,14 @@ static int decode_mb_i(AVSContext *h, int cbp_code) /* get coded block pattern */ if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) - cbp_code = get_ue_golomb(gb); + cbp_code = get_ue_golomb(bc); if (cbp_code > 63 || cbp_code < 0) { av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n"); return AVERROR_INVALIDDATA; } h->cbp = cbp_tab[cbp_code][0]; if (h->cbp && !h->qp_fixed) - h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta + h->qp = (h->qp + get_se_golomb(bc)) & 63; // qp_delta /* luma intra prediction interleaved with residual decode/transform/add */ for (block = 0; block < 4; block++) { @@ -687,7 +687,7 @@ static int decode_mb_i(AVSContext *h, int cbp_code) h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]] (d, top, left, h->l_stride); if (h->cbp & (1<qp, d, h->l_stride); + decode_residual_block(h, bc, intra_dec, 1, h->qp, d, h->l_stride); } /* chroma intra prediction */ @@ -716,7 +716,7 @@ static inline void set_intra_mode_default(AVSContext *h) static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) { - GetBitContext *gb = &h->gb; + BitstreamContext *bc = &h->bc; int ref[4]; ff_cavs_init_mb(h); @@ -725,26 +725,26 @@ static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0); break; case P_16X16: - ref[0] = h->ref_flag ? 0 : get_bits1(gb); + ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc); ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]); break; case P_16X8: - ref[0] = h->ref_flag ? 0 : get_bits1(gb); - ref[2] = h->ref_flag ? 0 : get_bits1(gb); + ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc); + ref[2] = h->ref_flag ? 0 : bitstream_read_bit(bc); ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]); ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]); break; case P_8X16: - ref[0] = h->ref_flag ? 0 : get_bits1(gb); - ref[1] = h->ref_flag ? 0 : get_bits1(gb); + ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc); + ref[1] = h->ref_flag ? 0 : bitstream_read_bit(bc); ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]); ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]); break; case P_8X8: - ref[0] = h->ref_flag ? 0 : get_bits1(gb); - ref[1] = h->ref_flag ? 0 : get_bits1(gb); - ref[2] = h->ref_flag ? 0 : get_bits1(gb); - ref[3] = h->ref_flag ? 0 : get_bits1(gb); + ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc); + ref[1] = h->ref_flag ? 0 : bitstream_read_bit(bc); + ref[2] = h->ref_flag ? 0 : bitstream_read_bit(bc); + ref[3] = h->ref_flag ? 0 : bitstream_read_bit(bc); ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]); ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]); ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]); @@ -797,7 +797,7 @@ static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) break; case B_8X8: for (block = 0; block < 4; block++) - sub_type[block] = get_bits(&h->gb, 2); + sub_type[block] = bitstream_read(&h->bc, 2); for (block = 0; block < 4; block++) { switch (sub_type[block]) { case B_SUB_DIRECT: @@ -874,7 +874,7 @@ static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) * ****************************************************************************/ -static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) +static inline int decode_slice_header(AVSContext *h, BitstreamContext *bc) { if (h->stc > 0xAF) av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc); @@ -884,13 +884,13 @@ static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) /* mark top macroblocks as unavailable */ h->flags &= ~(B_AVAIL | C_AVAIL); if ((h->mby == 0) && (!h->qp_fixed)) { - h->qp_fixed = get_bits1(gb); - h->qp = get_bits(gb, 6); + h->qp_fixed = bitstream_read_bit(bc); + h->qp = bitstream_read(bc, 6); } /* inter frame or second slice can have weighting params */ if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width / 2)) - if (get_bits1(gb)) { //slice_weighting_flag + if (bitstream_read_bit(bc)) { // slice_weighting_flag av_log(h->avctx, AV_LOG_ERROR, "weighted prediction not yet supported\n"); } @@ -899,21 +899,21 @@ static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) static inline int check_for_slice(AVSContext *h) { - GetBitContext *gb = &h->gb; + BitstreamContext *bc = &h->bc; int align; if (h->mbx) return 0; - align = (-get_bits_count(gb)) & 7; + align = (-bitstream_tell(bc)) & 7; /* check for stuffing byte */ - if (!align && (show_bits(gb, 8) == 0x80)) + if (!align && (bitstream_peek(bc, 8) == 0x80)) align = 8; - if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) { - skip_bits_long(gb, 24 + align); - h->stc = get_bits(gb, 8); + if ((bitstream_peek(bc, 24 + align) & 0xFFFFFF) == 0x000001) { + bitstream_skip(bc, 24 + align); + h->stc = bitstream_read(bc, 8); if (h->stc >= h->mb_height) return 0; - decode_slice_header(h, gb); + decode_slice_header(h, bc); return 1; } return 0; @@ -938,9 +938,9 @@ static int decode_pic(AVSContext *h) av_frame_unref(h->cur.f); - skip_bits(&h->gb, 16);//bbv_dwlay + bitstream_skip(&h->bc, 16); // bbv_dwlay if (h->stc == PIC_PB_START_CODE) { - h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I; + h->cur.f->pict_type = bitstream_read(&h->bc, 2) + AV_PICTURE_TYPE_I; if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) { av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n"); return AVERROR_INVALIDDATA; @@ -951,17 +951,17 @@ static int decode_pic(AVSContext *h) return AVERROR_INVALIDDATA; } else { h->cur.f->pict_type = AV_PICTURE_TYPE_I; - if (get_bits1(&h->gb)) - skip_bits(&h->gb, 24);//time_code + if (bitstream_read_bit(&h->bc)) + bitstream_skip(&h->bc, 24); // time_code /* old sample clips were all progressive and no low_delay, bump stream revision if detected otherwise */ - if (h->low_delay || !(show_bits(&h->gb, 9) & 1)) + if (h->low_delay || !(bitstream_peek(&h->bc, 9) & 1)) h->stream_revision = 1; /* similarly test top_field_first and repeat_first_field */ - else if (show_bits(&h->gb, 11) & 3) + else if (bitstream_peek(&h->bc, 11) & 3) h->stream_revision = 1; if (h->stream_revision > 0) - skip_bits(&h->gb, 1); //marker_bit + bitstream_skip(&h->bc, 1); // marker_bit } ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ? @@ -977,7 +977,7 @@ static int decode_pic(AVSContext *h) } ff_cavs_init_pic(h); - h->cur.poc = get_bits(&h->gb, 8) * 2; + h->cur.poc = bitstream_read(&h->bc, 8) * 2; /* get temporal distances and MV scaling factors */ if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) { @@ -996,31 +996,31 @@ static int decode_pic(AVSContext *h) } if (h->low_delay) - get_ue_golomb(&h->gb); //bbv_check_times - h->progressive = get_bits1(&h->gb); + get_ue_golomb(&h->bc); // bbv_check_times + h->progressive = bitstream_read_bit(&h->bc); h->pic_structure = 1; if (!h->progressive) - h->pic_structure = get_bits1(&h->gb); + h->pic_structure = bitstream_read_bit(&h->bc); if (!h->pic_structure && h->stc == PIC_PB_START_CODE) - skip_bits1(&h->gb); //advanced_pred_mode_disable - skip_bits1(&h->gb); //top_field_first - skip_bits1(&h->gb); //repeat_first_field - h->qp_fixed = get_bits1(&h->gb); - h->qp = get_bits(&h->gb, 6); + bitstream_skip(&h->bc, 1); // advanced_pred_mode_disable + bitstream_skip(&h->bc, 1); // top_field_first + bitstream_skip(&h->bc, 1); // repeat_first_field + h->qp_fixed = bitstream_read_bit(&h->bc); + h->qp = bitstream_read(&h->bc, 6); if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) { if (!h->progressive && !h->pic_structure) - skip_bits1(&h->gb);//what is this? - skip_bits(&h->gb, 4); //reserved bits + bitstream_skip(&h->bc, 1); // what is this? + bitstream_skip(&h->bc, 4); // reserved bits } else { if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1)) - h->ref_flag = get_bits1(&h->gb); - skip_bits(&h->gb, 4); //reserved bits - h->skip_mode_flag = get_bits1(&h->gb); + h->ref_flag = bitstream_read_bit(&h->bc); + bitstream_skip(&h->bc, 4); // reserved bits + h->skip_mode_flag = bitstream_read_bit(&h->bc); } - h->loop_filter_disable = get_bits1(&h->gb); - if (!h->loop_filter_disable && get_bits1(&h->gb)) { - h->alpha_offset = get_se_golomb(&h->gb); - h->beta_offset = get_se_golomb(&h->gb); + h->loop_filter_disable = bitstream_read_bit(&h->bc); + if (!h->loop_filter_disable && bitstream_read_bit(&h->bc)) { + h->alpha_offset = get_se_golomb(&h->bc); + h->beta_offset = get_se_golomb(&h->bc); } else { h->alpha_offset = h->beta_offset = 0; } @@ -1034,11 +1034,11 @@ static int decode_pic(AVSContext *h) if (check_for_slice(h)) skip_count = -1; if (h->skip_mode_flag && (skip_count < 0)) - skip_count = get_ue_golomb(&h->gb); + skip_count = get_ue_golomb(&h->bc); if (h->skip_mode_flag && skip_count--) { decode_mb_p(h, P_SKIP); } else { - mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag; + mb_type = get_ue_golomb(&h->bc) + P_SKIP + h->skip_mode_flag; if (mb_type > P_8X8) decode_mb_i(h, mb_type - P_8X8 - 1); else @@ -1050,11 +1050,11 @@ static int decode_pic(AVSContext *h) if (check_for_slice(h)) skip_count = -1; if (h->skip_mode_flag && (skip_count < 0)) - skip_count = get_ue_golomb(&h->gb); + skip_count = get_ue_golomb(&h->bc); if (h->skip_mode_flag && skip_count--) { decode_mb_b(h, B_SKIP); } else { - mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag; + mb_type = get_ue_golomb(&h->bc) + B_SKIP + h->skip_mode_flag; if (mb_type > B_8X8) decode_mb_i(h, mb_type - B_8X8 - 1); else @@ -1081,12 +1081,12 @@ static int decode_seq_header(AVSContext *h) int frame_rate_code; int width, height; - h->profile = get_bits(&h->gb, 8); - h->level = get_bits(&h->gb, 8); - skip_bits1(&h->gb); //progressive sequence + h->profile = bitstream_read(&h->bc, 8); + h->level = bitstream_read(&h->bc, 8); + bitstream_skip(&h->bc, 1); // progressive sequence - width = get_bits(&h->gb, 14); - height = get_bits(&h->gb, 14); + width = bitstream_read(&h->bc, 14); + height = bitstream_read(&h->bc, 14); if ((h->width || h->height) && (h->width != width || h->height != height)) { avpriv_report_missing_feature(h->avctx, "Width/height changing in CAVS"); @@ -1095,14 +1095,14 @@ static int decode_seq_header(AVSContext *h) h->width = width; h->height = height; - skip_bits(&h->gb, 2); //chroma format - skip_bits(&h->gb, 3); //sample_precision - h->aspect_ratio = get_bits(&h->gb, 4); - frame_rate_code = get_bits(&h->gb, 4); - skip_bits(&h->gb, 18); //bit_rate_lower - skip_bits1(&h->gb); //marker_bit - skip_bits(&h->gb, 12); //bit_rate_upper - h->low_delay = get_bits1(&h->gb); + bitstream_skip(&h->bc, 2); // chroma format + bitstream_skip(&h->bc, 3); // sample_precision + h->aspect_ratio = bitstream_read(&h->bc, 4); + frame_rate_code = bitstream_read(&h->bc, 4); + bitstream_skip(&h->bc, 18); // bit_rate_lower + bitstream_skip(&h->bc, 1); // marker_bit + bitstream_skip(&h->bc, 12); // bit_rate_upper + h->low_delay = bitstream_read_bit(&h->bc); h->mb_width = (h->width + 15) >> 4; h->mb_height = (h->height + 15) >> 4; h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code]; @@ -1147,7 +1147,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, input_size = (buf_end - buf_ptr) * 8; switch (stc) { case CAVS_START_CODE: - init_get_bits(&h->gb, buf_ptr, input_size); + bitstream_init(&h->bc, buf_ptr, input_size); decode_seq_header(h); break; case PIC_I_START_CODE: @@ -1160,7 +1160,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, *got_frame = 0; if (!h->got_keyframe) break; - init_get_bits(&h->gb, buf_ptr, input_size); + bitstream_init(&h->bc, buf_ptr, input_size); h->stc = stc; if (decode_pic(h)) break; @@ -1184,8 +1184,8 @@ static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, break; default: if (stc <= SLICE_MAX_START_CODE) { - init_get_bits(&h->gb, buf_ptr, input_size); - decode_slice_header(h, &h->gb); + bitstream_init(&h->bc, buf_ptr, input_size); + decode_slice_header(h, &h->bc); } break; } From 6b1f559f9a0667390259374c738132ad9475fd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 19 Mar 2016 15:39:03 +0100 Subject: [PATCH 04/10] dirac: Convert to the new bitstream reader --- libavcodec/dirac.c | 89 +++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/libavcodec/dirac.c b/libavcodec/dirac.c index cce9439d44..5faf0a384d 100644 --- a/libavcodec/dirac.c +++ b/libavcodec/dirac.c @@ -28,8 +28,9 @@ #include "libavutil/imgutils.h" #include "avcodec.h" +#include "bitstream.h" #include "dirac.h" -#include "golomb_legacy.h" +#include "golomb.h" #include "internal.h" #include "mpeg12data.h" @@ -138,7 +139,7 @@ static const enum AVPixelFormat dirac_pix_fmt[2][3] = { /* [DIRAC_STD] 10.3 Parse Source Parameters. * source_parameters(base_video_format) */ -static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, +static int parse_source_parameters(AVDiracSeqHeader *dsh, BitstreamContext *bc, void *log_ctx) { AVRational frame_rate = { 0, 0 }; @@ -147,17 +148,17 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */ /* [DIRAC_STD] custom_dimensions_flag */ - if (get_bits1(gb)) { - dsh->width = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */ - dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */ + if (bitstream_read_bit(bc)) { + dsh->width = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_WIDTH */ + dsh->height = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_HEIGHT */ } /* [DIRAC_STD] 10.3.3 Chroma Sampling Format. * chroma_sampling_format(video_params) */ /* [DIRAC_STD] custom_chroma_format_flag */ - if (get_bits1(gb)) + if (bitstream_read_bit(bc)) /* [DIRAC_STD] CHROMA_FORMAT_INDEX */ - dsh->chroma_format = get_interleaved_ue_golomb(gb); + dsh->chroma_format = get_interleaved_ue_golomb(bc); if (dsh->chroma_format > 2) { if (log_ctx) av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n", @@ -167,24 +168,24 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */ /* [DIRAC_STD] custom_scan_format_flag */ - if (get_bits1(gb)) + if (bitstream_read_bit(bc)) /* [DIRAC_STD] SOURCE_SAMPLING */ - dsh->interlaced = get_interleaved_ue_golomb(gb); + dsh->interlaced = get_interleaved_ue_golomb(bc); if (dsh->interlaced > 1) return AVERROR_INVALIDDATA; /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */ - if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */ - dsh->frame_rate_index = get_interleaved_ue_golomb(gb); + if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_frame_rate_flag */ + dsh->frame_rate_index = get_interleaved_ue_golomb(bc); if (dsh->frame_rate_index > 10) return AVERROR_INVALIDDATA; if (!dsh->frame_rate_index) { /* [DIRAC_STD] FRAME_RATE_NUMER */ - frame_rate.num = get_interleaved_ue_golomb(gb); + frame_rate.num = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_RATE_DENOM */ - frame_rate.den = get_interleaved_ue_golomb(gb); + frame_rate.den = get_interleaved_ue_golomb(bc); } } /* [DIRAC_STD] preset_frame_rate(video_params, index) */ @@ -199,16 +200,16 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio. * pixel_aspect_ratio(video_params) */ - if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */ + if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */ /* [DIRAC_STD] index */ - dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb); + dsh->aspect_ratio_index = get_interleaved_ue_golomb(bc); if (dsh->aspect_ratio_index > 6) return AVERROR_INVALIDDATA; if (!dsh->aspect_ratio_index) { - dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb); - dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb); + dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(bc); + dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(bc); } } /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel @@ -218,33 +219,33 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1]; /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */ - if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */ + if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_clean_area_flag */ /* [DIRAC_STD] CLEAN_WIDTH */ - dsh->clean_width = get_interleaved_ue_golomb(gb); + dsh->clean_width = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] CLEAN_HEIGHT */ - dsh->clean_height = get_interleaved_ue_golomb(gb); + dsh->clean_height = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] CLEAN_LEFT_OFFSET */ - dsh->clean_left_offset = get_interleaved_ue_golomb(gb); + dsh->clean_left_offset = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */ - dsh->clean_right_offset = get_interleaved_ue_golomb(gb); + dsh->clean_right_offset = get_interleaved_ue_golomb(bc); } /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params) * WARNING: Some adaptation seems to be done using the * AVCOL_RANGE_MPEG/JPEG values */ - if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */ + if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_signal_range_flag */ /* [DIRAC_STD] index */ - dsh->pixel_range_index = get_interleaved_ue_golomb(gb); + dsh->pixel_range_index = get_interleaved_ue_golomb(bc); if (dsh->pixel_range_index > 4) return AVERROR_INVALIDDATA; // This assumes either fullrange or MPEG levels only if (!dsh->pixel_range_index) { - luma_offset = get_interleaved_ue_golomb(gb); - luma_depth = av_log2(get_interleaved_ue_golomb(gb)) + 1; - get_interleaved_ue_golomb(gb); /* chroma offset */ - get_interleaved_ue_golomb(gb); /* chroma excursion */ + luma_offset = get_interleaved_ue_golomb(bc); + luma_depth = av_log2(get_interleaved_ue_golomb(bc)) + 1; + get_interleaved_ue_golomb(bc); /* chroma offset */ + get_interleaved_ue_golomb(bc); /* chroma excursion */ dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; } @@ -263,9 +264,9 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format]; /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */ - if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */ + if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_colour_spec_flag */ /* [DIRAC_STD] index */ - idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb); + idx = dsh->color_spec_index = get_interleaved_ue_golomb(bc); if (dsh->color_spec_index > 4) return AVERROR_INVALIDDATA; @@ -276,21 +277,21 @@ static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb, if (!dsh->color_spec_index) { /* [DIRAC_STD] 10.3.9.1 Colour primaries */ - if (get_bits1(gb)) { - idx = get_interleaved_ue_golomb(gb); + if (bitstream_read_bit(bc)) { + idx = get_interleaved_ue_golomb(bc); if (idx < 3) dsh->color_primaries = dirac_primaries[idx]; } /* [DIRAC_STD] 10.3.9.2 Colour matrix */ - if (get_bits1(gb)) { - idx = get_interleaved_ue_golomb(gb); + if (bitstream_read_bit(bc)) { + idx = get_interleaved_ue_golomb(bc); if (!idx) dsh->colorspace = AVCOL_SPC_BT709; else if (idx == 1) dsh->colorspace = AVCOL_SPC_BT470BG; } /* [DIRAC_STD] 10.3.9.3 Transfer function */ - if (get_bits1(gb) && !get_interleaved_ue_golomb(gb)) + if (bitstream_read_bit(bc) && !get_interleaved_ue_golomb(bc)) dsh->color_trc = AVCOL_TRC_BT709; } } else { @@ -309,7 +310,7 @@ int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, void *log_ctx) { AVDiracSeqHeader *dsh; - GetBitContext gb; + BitstreamContext bc; unsigned version_major; unsigned video_format, picture_coding_mode; int ret; @@ -318,18 +319,18 @@ int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, if (!dsh) return AVERROR(ENOMEM); - ret = init_get_bits8(&gb, buf, buf_size); + ret = bitstream_init8(&bc, buf, buf_size); if (ret < 0) goto fail; /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */ - version_major = get_interleaved_ue_golomb(&gb); - get_interleaved_ue_golomb(&gb); /* version_minor */ - dsh->profile = get_interleaved_ue_golomb(&gb); - dsh->level = get_interleaved_ue_golomb(&gb); + version_major = get_interleaved_ue_golomb(&bc); + get_interleaved_ue_golomb(&bc); /* version_minor */ + dsh->profile = get_interleaved_ue_golomb(&bc); + dsh->level = get_interleaved_ue_golomb(&bc); /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */ - video_format = get_interleaved_ue_golomb(&gb); + video_format = get_interleaved_ue_golomb(&bc); if (log_ctx) { if (version_major < 2) @@ -360,13 +361,13 @@ int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, /* [DIRAC_STD] 10.3 Source Parameters * Override the defaults. */ - ret = parse_source_parameters(dsh, &gb, log_ctx); + ret = parse_source_parameters(dsh, &bc, log_ctx); if (ret < 0) goto fail; /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames * currently only used to signal field coding */ - picture_coding_mode = get_interleaved_ue_golomb(&gb); + picture_coding_mode = get_interleaved_ue_golomb(&bc); if (picture_coding_mode != 0) { if (log_ctx) { av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d", From 0f94de8a092b1a9f1fe45f830c0f134699c16de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 19 Mar 2016 17:40:55 +0100 Subject: [PATCH 05/10] fic: Convert to the new bitstream reader --- libavcodec/fic.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libavcodec/fic.c b/libavcodec/fic.c index 1804104916..a038af6e60 100644 --- a/libavcodec/fic.c +++ b/libavcodec/fic.c @@ -24,9 +24,9 @@ #include "libavutil/common.h" #include "avcodec.h" -#include "golomb_legacy.h" +#include "bitstream.h" +#include "golomb.h" #include "internal.h" -#include "get_bits.h" typedef struct FICThreadContext { DECLARE_ALIGNED(16, int16_t, block)[64]; @@ -129,13 +129,13 @@ static void fic_idct_put(uint8_t *dst, int stride, int16_t *block) ptr += 8; } } -static int fic_decode_block(FICContext *ctx, GetBitContext *gb, +static int fic_decode_block(FICContext *ctx, BitstreamContext *bc, uint8_t *dst, int stride, int16_t *block) { int i, num_coeff; /* Is it a skip block? */ - if (get_bits1(gb)) { + if (bitstream_read_bit(bc)) { /* This is a P-frame. */ ctx->frame->key_frame = 0; ctx->frame->pict_type = AV_PICTURE_TYPE_P; @@ -145,12 +145,12 @@ static int fic_decode_block(FICContext *ctx, GetBitContext *gb, memset(block, 0, sizeof(*block) * 64); - num_coeff = get_bits(gb, 7); + num_coeff = bitstream_read(bc, 7); if (num_coeff > 64) return AVERROR_INVALIDDATA; for (i = 0; i < num_coeff; i++) - block[ff_zigzag_direct[i]] = get_se_golomb(gb) * + block[ff_zigzag_direct[i]] = get_se_golomb(bc) * ctx->qmat[ff_zigzag_direct[i]]; fic_idct_put(dst, stride, block); @@ -162,14 +162,14 @@ static int fic_decode_slice(AVCodecContext *avctx, void *tdata) { FICContext *ctx = avctx->priv_data; FICThreadContext *tctx = tdata; - GetBitContext gb; + BitstreamContext bc; uint8_t *src = tctx->src; int slice_h = tctx->slice_h; int src_size = tctx->src_size; int y_off = tctx->y_off; int x, y, p; - init_get_bits(&gb, src, src_size * 8); + bitstream_init8(&bc, src, src_size); for (p = 0; p < 3; p++) { int stride = ctx->frame->linesize[p]; @@ -179,7 +179,7 @@ static int fic_decode_slice(AVCodecContext *avctx, void *tdata) for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) { int ret; - if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0) + if ((ret = fic_decode_block(ctx, &bc, dst + x, stride, tctx->block)) != 0) return ret; } From d85b37a955317f176f3443a40859a21c15d7c3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Mon, 21 Mar 2016 20:23:36 +0100 Subject: [PATCH 06/10] loco: Convert to the new bitstream reader --- libavcodec/loco.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libavcodec/loco.c b/libavcodec/loco.c index 8624ea86ad..fa4c5edb40 100644 --- a/libavcodec/loco.c +++ b/libavcodec/loco.c @@ -25,8 +25,8 @@ */ #include "avcodec.h" -#include "get_bits.h" -#include "golomb_legacy.h" +#include "bitstream.h" +#include "golomb.h" #include "internal.h" #include "mathops.h" @@ -50,7 +50,7 @@ typedef struct LOCOContext { } LOCOContext; typedef struct RICEContext { - GetBitContext gb; + BitstreamContext bc; int save, run, run2; /* internal rice decoder state */ int sum, count; /* sum and count for getting rice parameter */ int lossy; @@ -88,11 +88,11 @@ static inline int loco_get_rice(RICEContext *r) loco_update_rice_param(r, 0); return 0; } - v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0); + v = get_ur_golomb_jpegls(&r->bc, loco_get_rice_param(r), INT_MAX, 0); loco_update_rice_param(r, (v + 1) >> 1); if (!v) { if (r->save >= 0) { - r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0); + r->run = get_ur_golomb_jpegls(&r->bc, 2, INT_MAX, 0); if (r->run > 1) r->save += r->run + 1; else @@ -132,7 +132,7 @@ static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int heigh int val; int i, j; - init_get_bits(&rc.gb, buf, buf_size*8); + bitstream_init8(&rc.bc, buf, buf_size); rc.save = 0; rc.run = 0; rc.run2 = 0; @@ -162,7 +162,7 @@ static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int heigh data += stride; } - return (get_bits_count(&rc.gb) + 7) >> 3; + return (bitstream_tell(&rc.bc) + 7) >> 3; } static int decode_frame(AVCodecContext *avctx, From 5a6da49dd0c078b7d20e130ab74f326abc4678a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 22 Mar 2016 10:26:03 +0100 Subject: [PATCH 07/10] ralf: Convert to the new bitstream reader --- libavcodec/ralf.c | 68 ++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/libavcodec/ralf.c b/libavcodec/ralf.c index 6bc0f3fa0d..1003b10c11 100644 --- a/libavcodec/ralf.c +++ b/libavcodec/ralf.c @@ -28,11 +28,13 @@ #include "libavutil/attributes.h" #include "libavutil/channel_layout.h" + #include "avcodec.h" -#include "get_bits.h" -#include "golomb_legacy.h" +#include "bitstream.h" +#include "golomb.h" #include "internal.h" -#include "unary_legacy.h" +#include "unary.h" +#include "vlc.h" #include "ralfdata.h" #define FILTER_NONE 0 @@ -210,21 +212,21 @@ static av_cold int decode_init(AVCodecContext *avctx) return 0; } -static inline int extend_code(GetBitContext *gb, int val, int range, int bits) +static inline int extend_code(BitstreamContext *bc, int val, int range, int bits) { if (val == 0) { - val = -range - get_ue_golomb(gb); + val = -range - get_ue_golomb(bc); } else if (val == range * 2) { - val = range + get_ue_golomb(gb); + val = range + get_ue_golomb(bc); } else { val -= range; } if (bits) - val = (val << bits) | get_bits(gb, bits); + val = (val << bits) | bitstream_read(bc, bits); return val; } -static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, +static int decode_channel(RALFContext *ctx, BitstreamContext *bc, int ch, int length, int mode, int bits) { int i, t; @@ -233,19 +235,19 @@ static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, VLC *code_vlc; int range, range2, add_bits; int *dst = ctx->channel_data[ch]; - ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2); + ctx->filter_params = bitstream_read_vlc(bc, set->filter_params.table, 9, 2); ctx->filter_bits = (ctx->filter_params - 2) >> 6; ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1; if (ctx->filter_params == FILTER_RAW) { for (i = 0; i < length; i++) - dst[i] = get_bits(gb, bits); + dst[i] = bitstream_read(bc, bits); ctx->bias[ch] = 0; return 0; } - ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2); - ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4); + ctx->bias[ch] = bitstream_read_vlc(bc, set->bias.table, 9, 2); + ctx->bias[ch] = extend_code(bc, ctx->bias[ch], 127, 4); if (ctx->filter_params == FILTER_NONE) { memset(dst, 0, sizeof(*dst) * length); @@ -259,8 +261,8 @@ static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, add_bits = ctx->filter_bits; for (i = 0; i < ctx->filter_length; i++) { - t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2); - t = extend_code(gb, t, 21, add_bits); + t = bitstream_read_vlc(bc, vlc[cmode].table, vlc[cmode].bits, 2); + t = extend_code(bc, t, 21, add_bits); if (!cmode) coeff -= 12 << add_bits; coeff = t - coeff; @@ -279,7 +281,7 @@ static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, } } - code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2); + code_params = bitstream_read_vlc(bc, set->coding_mode.table, set->coding_mode.bits, 2); if (code_params >= 15) { add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10); if (add_bits > 9 && (code_params % 5) != 2) @@ -297,14 +299,14 @@ static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, for (i = 0; i < length; i += 2) { int code1, code2; - t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2); + t = bitstream_read_vlc(bc, code_vlc->table, code_vlc->bits, 2); code1 = t / range2; code2 = t % range2; - dst[i] = extend_code(gb, code1, range, 0) << add_bits; - dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits; + dst[i] = extend_code(bc, code1, range, 0) << add_bits; + dst[i + 1] = extend_code(bc, code2, range, 0) << add_bits; if (add_bits) { - dst[i] |= get_bits(gb, add_bits); - dst[i + 1] |= get_bits(gb, add_bits); + dst[i] |= bitstream_read(bc, add_bits); + dst[i + 1] |= bitstream_read(bc, add_bits); } } @@ -335,7 +337,7 @@ static void apply_lpc(RALFContext *ctx, int ch, int length, int bits) } } -static int decode_block(AVCodecContext *avctx, GetBitContext *gb, +static int decode_block(AVCodecContext *avctx, BitstreamContext *bc, int16_t *dst0, int16_t *dst1) { RALFContext *ctx = avctx->priv_data; @@ -344,7 +346,7 @@ static int decode_block(AVCodecContext *avctx, GetBitContext *gb, int *ch0, *ch1; int i, t, t2; - len = 12 - get_unary(gb, 0, 6); + len = 12 - get_unary(bc, 0, 6); if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped len = 1 << len; @@ -356,7 +358,7 @@ static int decode_block(AVCodecContext *avctx, GetBitContext *gb, } if (avctx->channels > 1) - dmode = get_bits(gb, 2) + 1; + dmode = bitstream_read(bc, 2) + 1; else dmode = 0; @@ -366,13 +368,13 @@ static int decode_block(AVCodecContext *avctx, GetBitContext *gb, bits[1] = (mode[1] == 2) ? 17 : 16; for (ch = 0; ch < avctx->channels; ch++) { - if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0) + if ((ret = decode_channel(ctx, bc, ch, len, mode[ch], bits[ch])) < 0) return ret; if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) { ctx->filter_bits += 3; apply_lpc(ctx, ch, len, bits[ch]); } - if (get_bits_left(gb) < 0) + if (bitstream_bits_left(bc) < 0) return AVERROR_INVALIDDATA; } ch0 = ctx->channel_data[0]; @@ -426,7 +428,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, int16_t *samples0; int16_t *samples1; int ret; - GetBitContext gb; + BitstreamContext bc; int table_size, table_bytes, i; const uint8_t *src, *block_pointer; int src_size; @@ -478,12 +480,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, av_log(avctx, AV_LOG_ERROR, "short packets are short!\n"); return AVERROR_INVALIDDATA; } - init_get_bits(&gb, src + 2, table_size); + bitstream_init(&bc, src + 2, table_size); ctx->num_blocks = 0; - while (get_bits_left(&gb) > 0) { - ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15); - if (get_bits1(&gb)) { - ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9); + while (bitstream_bits_left(&bc) > 0) { + ctx->block_size[ctx->num_blocks] = bitstream_read(&bc, 15); + if (bitstream_read_bit(&bc)) { + ctx->block_pts[ctx->num_blocks] = bitstream_read(&bc, 9); } else { ctx->block_pts[ctx->num_blocks] = 0; } @@ -498,8 +500,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n"); break; } - init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8); - if (decode_block(avctx, &gb, samples0 + ctx->sample_offset, + bitstream_init8(&bc, block_pointer, ctx->block_size[i]); + if (decode_block(avctx, &bc, samples0 + ctx->sample_offset, samples1 + ctx->sample_offset) < 0) { av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n"); break; From 2b94ed12de7b6b7f444ed67e1a7068141af3d4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Tue, 22 Mar 2016 16:09:39 +0100 Subject: [PATCH 08/10] shorten: Convert to the new bitstream reader --- libavcodec/shorten.c | 49 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c index 82fa2ab943..e040b9cfed 100644 --- a/libavcodec/shorten.c +++ b/libavcodec/shorten.c @@ -26,10 +26,11 @@ */ #include + #include "avcodec.h" +#include "bitstream.h" #include "bytestream.h" -#include "get_bits.h" -#include "golomb_legacy.h" +#include "golomb.h" #include "internal.h" #define MAX_CHANNELS 8 @@ -79,7 +80,7 @@ static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 }; typedef struct ShortenContext { AVCodecContext *avctx; - GetBitContext gb; + BitstreamContext bc; int min_framesize, max_framesize; unsigned channels; @@ -154,8 +155,8 @@ static int allocate_buffers(ShortenContext *s) static inline unsigned int get_uint(ShortenContext *s, int k) { if (s->version != 0) - k = get_ur_golomb_shorten(&s->gb, ULONGSIZE); - return get_ur_golomb_shorten(&s->gb, k); + k = get_ur_golomb_shorten(&s->bc, ULONGSIZE); + return get_ur_golomb_shorten(&s->bc, k); } static void fix_bitshift(ShortenContext *s, int32_t *buffer) @@ -280,7 +281,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, if (command == FN_QLPC) { /* read/validate prediction order */ - pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE); + pred_order = get_ur_golomb_shorten(&s->bc, LPCQSIZE); if (pred_order > s->nwrap) { av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", pred_order); @@ -288,7 +289,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, } /* read LPC coefficients */ for (i = 0; i < pred_order; i++) - s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT); + s->coeffs[i] = get_sr_golomb_shorten(&s->bc, LPCQUANT); coeffs = s->coeffs; qshift = LPCQUANT; @@ -315,7 +316,7 @@ static int decode_subframe_lpc(ShortenContext *s, int command, int channel, sum = init_sum; for (j = 0; j < pred_order; j++) sum += coeffs[j] * s->decoded[channel][i - j - 1]; - s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + + s->decoded[channel][i] = get_sr_golomb_shorten(&s->bc, residual_size) + (sum >> qshift); } @@ -332,7 +333,7 @@ static int read_header(ShortenContext *s) int i, ret; int maxnlpc = 0; /* shorten signature */ - if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) { + if (bitstream_read(&s->bc, 32) != AV_RB32("ajkg")) { av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n"); return AVERROR_INVALIDDATA; } @@ -340,7 +341,7 @@ static int read_header(ShortenContext *s) s->lpcqoffset = 0; s->blocksize = DEFAULT_BLOCK_SIZE; s->nmean = -1; - s->version = get_bits(&s->gb, 8); + s->version = bitstream_read(&s->bc, 8); s->internal_ftype = get_uint(s, TYPESIZE); s->channels = get_uint(s, CHANSIZE); @@ -374,7 +375,7 @@ static int read_header(ShortenContext *s) skip_bytes = get_uint(s, NSKIPSIZE); for (i = 0; i < skip_bytes; i++) - skip_bits(&s->gb, 8); + bitstream_skip(&s->bc, 8); } s->nwrap = FFMAX(NWRAP, maxnlpc); @@ -387,13 +388,13 @@ static int read_header(ShortenContext *s) if (s->version > 1) s->lpcqoffset = V2LPCQOFFSET; - if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) { + if (get_ur_golomb_shorten(&s->bc, FNSIZE) != FN_VERBATIM) { av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n"); return AVERROR_INVALIDDATA; } - s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); + s->header_size = get_ur_golomb_shorten(&s->bc, VERBATIM_CKSIZE_SIZE); if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) { av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", @@ -402,7 +403,7 @@ static int read_header(ShortenContext *s) } for (i = 0; i < s->header_size; i++) - s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); + s->header[i] = (char)get_ur_golomb_shorten(&s->bc, VERBATIM_BYTE_SIZE); if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0) return ret; @@ -464,8 +465,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, } } /* init and position bitstream reader */ - init_get_bits(&s->gb, buf, buf_size * 8); - skip_bits(&s->gb, s->bitindex); + bitstream_init8(&s->bc, buf, buf_size); + bitstream_skip(&s->bc, s->bitindex); /* process header or next subblock */ if (!s->got_header) { @@ -486,12 +487,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, unsigned cmd; int len; - if (get_bits_left(&s->gb) < 3 + FNSIZE) { + if (bitstream_bits_left(&s->bc) < 3 + FNSIZE) { *got_frame_ptr = 0; break; } - cmd = get_ur_golomb_shorten(&s->gb, FNSIZE); + cmd = get_ur_golomb_shorten(&s->bc, FNSIZE); if (cmd > FN_VERBATIM) { av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd); @@ -503,12 +504,12 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, /* process non-audio command */ switch (cmd) { case FN_VERBATIM: - len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); + len = get_ur_golomb_shorten(&s->bc, VERBATIM_CKSIZE_SIZE); while (len--) - get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); + get_ur_golomb_shorten(&s->bc, VERBATIM_BYTE_SIZE); break; case FN_BITSHIFT: - s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE); + s->bitshift = get_ur_golomb_shorten(&s->bc, BITSHIFTSIZE); if (s->bitshift < 0) return AVERROR_INVALIDDATA; break; @@ -543,7 +544,7 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, /* get Rice code for residual decoding */ if (cmd != FN_ZERO) { - residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE); + residual_size = get_ur_golomb_shorten(&s->bc, ENERGYSIZE); /* This is a hack as version 0 differed in the definition * of get_sr_golomb_shorten(). */ if (s->version == 0) @@ -616,8 +617,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, void *data, *got_frame_ptr = 0; finish_frame: - s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8); - i = get_bits_count(&s->gb) / 8; + s->bitindex = bitstream_tell(&s->bc) - 8 * (bitstream_tell(&s->bc) / 8); + i = bitstream_tell(&s->bc) / 8; if (i > buf_size) { av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size); s->bitstream_size = 0; From 2d72219554adb09bc3ba044ac3e579a84550067b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sun, 10 Apr 2016 11:44:20 +0200 Subject: [PATCH 09/10] h261dec: Convert to the new bitstream reader --- libavcodec/h261dec.c | 92 +++++++++++++++++++++--------------------- libavcodec/mpegvideo.h | 3 ++ libavformat/h261dec.c | 11 ++--- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/libavcodec/h261dec.c b/libavcodec/h261dec.c index 9a323ec7d1..b08598ec99 100644 --- a/libavcodec/h261dec.c +++ b/libavcodec/h261dec.c @@ -26,12 +26,14 @@ */ #include "avcodec.h" +#include "bitstream.h" #include "mpeg_er.h" #include "mpegutils.h" #include "mpegvideo.h" #include "h263.h" #include "h261.h" #include "internal.h" +#include "vlc.h" #define H261_MBA_VLC_BITS 9 #define H261_MTYPE_VLC_BITS 6 @@ -103,18 +105,18 @@ static int h261_decode_gob_header(H261Context *h) if (!h->gob_start_code_skipped) { /* Check for GOB Start Code */ - val = show_bits(&s->gb, 15); + val = bitstream_peek(&s->bc, 15); if (val) return -1; /* We have a GBSC */ - skip_bits(&s->gb, 16); + bitstream_skip(&s->bc, 16); } h->gob_start_code_skipped = 0; - h->gob_number = get_bits(&s->gb, 4); /* GN */ - s->qscale = get_bits(&s->gb, 5); /* GQUANT */ + h->gob_number = bitstream_read(&s->bc, 4); /* GN */ + s->qscale = bitstream_read(&s->bc, 5); /* GQUANT */ /* Check if gob_number is valid */ if (s->mb_height == 18) { // CIF @@ -127,8 +129,8 @@ static int h261_decode_gob_header(H261Context *h) } /* GEI */ - while (get_bits1(&s->gb) != 0) - skip_bits(&s->gb, 8); + while (bitstream_read_bit(&s->bc) != 0) + bitstream_skip(&s->bc, 8); if (s->qscale == 0) { av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n"); @@ -160,27 +162,27 @@ static int h261_resync(H261Context *h) if (ret >= 0) return 0; } else { - if (show_bits(&s->gb, 15) == 0) { + if (bitstream_peek(&s->bc, 15) == 0) { ret = h261_decode_gob_header(h); if (ret >= 0) return 0; } // OK, it is not where it is supposed to be ... - s->gb = s->last_resync_gb; - align_get_bits(&s->gb); - left = get_bits_left(&s->gb); + s->bc = s->last_resync_bc; + bitstream_align(&s->bc); + left = bitstream_bits_left(&s->bc); for (; left > 15 + 1 + 4 + 5; left -= 8) { - if (show_bits(&s->gb, 15) == 0) { - GetBitContext bak = s->gb; + if (bitstream_peek(&s->bc, 15) == 0) { + BitstreamContext bak = s->bc; ret = h261_decode_gob_header(h); if (ret >= 0) return 0; - s->gb = bak; + s->bc = bak; } - skip_bits(&s->gb, 8); + bitstream_skip(&s->bc, 8); } } @@ -228,9 +230,9 @@ static const int mvmap[17] = { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16 }; -static int decode_mv_component(GetBitContext *gb, int v) +static int decode_mv_component(BitstreamContext *bc, int v) { - int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); + int mv_diff = bitstream_read_vlc(bc, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); /* check if mv_diff is valid */ if (mv_diff < 0) @@ -238,7 +240,7 @@ static int decode_mv_component(GetBitContext *gb, int v) mv_diff = mvmap[mv_diff]; - if (mv_diff && !get_bits1(gb)) + if (mv_diff && !bitstream_read_bit(bc)) mv_diff = -mv_diff; v += mv_diff; @@ -270,7 +272,7 @@ static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded) scan_table = s->intra_scantable.permutated; if (s->mb_intra) { /* DC coef */ - level = get_bits(&s->gb, 8); + level = bitstream_read(&s->bc, 8); // 0 (00000000b) and -128 (10000000b) are FORBIDDEN if ((level & 0x7F) == 0) { av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", @@ -288,10 +290,10 @@ static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded) // EOB Not possible for first level when cbp is available (that's why the table is different) // 0 1 1s // * * 0* - int check = show_bits(&s->gb, 2); + int check = bitstream_peek(&s->bc, 2); i = 0; if (check & 0x2) { - skip_bits(&s->gb, 2); + bitstream_skip(&s->bc, 2); block[0] = (check & 0x1) ? -1 : 1; i = 1; } @@ -303,7 +305,7 @@ static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded) return 0; } for (;;) { - code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2); + code = bitstream_read_vlc(&s->bc, rl->vlc.table, TCOEFF_VLC_BITS, 2); if (code < 0) { av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); @@ -314,14 +316,14 @@ static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded) /* The remaining combinations of (run, level) are encoded with a * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits * level. */ - run = get_bits(&s->gb, 6); - level = get_sbits(&s->gb, 8); + run = bitstream_read(&s->bc, 6); + level = bitstream_read_signed(&s->bc, 8); } else if (code == 0) { break; } else { run = rl->table_run[code]; level = rl->table_level[code]; - if (get_bits1(&s->gb)) + if (bitstream_read_bit(&s->bc)) level = -level; } i += run; @@ -346,8 +348,8 @@ static int h261_decode_mb(H261Context *h) cbp = 63; // Read mba do { - h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, - H261_MBA_VLC_BITS, 2); + h->mba_diff = bitstream_read_vlc(&s->bc, h261_mba_vlc.table, + H261_MBA_VLC_BITS, 2); /* Check for slice end */ /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ @@ -358,7 +360,7 @@ static int h261_decode_mb(H261Context *h) } while (h->mba_diff == MBA_STUFFING); // stuffing if (h->mba_diff < 0) { - if (get_bits_left(&s->gb) <= 7) + if (bitstream_bits_left(&s->bc) <= 7) return SLICE_END; av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); @@ -378,7 +380,7 @@ static int h261_decode_mb(H261Context *h) ff_update_block_index(s); // Read mtype - h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); + h->mtype = bitstream_read_vlc(&s->bc, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(ff_h261_mtype_map)) { av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n", h->mtype); @@ -388,7 +390,7 @@ static int h261_decode_mb(H261Context *h) // Read mquant if (IS_QUANT(h->mtype)) - ff_set_qscale(s, get_bits(&s->gb, 5)); + ff_set_qscale(s, bitstream_read(&s->bc, 5)); s->mb_intra = IS_INTRA4x4(h->mtype); @@ -408,8 +410,8 @@ static int h261_decode_mb(H261Context *h) h->current_mv_y = 0; } - h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x); - h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y); + h->current_mv_x = decode_mv_component(&s->bc, h->current_mv_x); + h->current_mv_y = decode_mv_component(&s->bc, h->current_mv_y); } else { h->current_mv_x = 0; h->current_mv_y = 0; @@ -417,7 +419,7 @@ static int h261_decode_mb(H261Context *h) // Read cbp if (HAS_CBP(h->mtype)) - cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1; + cbp = bitstream_read_vlc(&s->bc, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1; if (s->mb_intra) { s->current_picture.mb_type[xy] = MB_TYPE_INTRA; @@ -460,8 +462,8 @@ static int h261_decode_picture_header(H261Context *h) int format, i; uint32_t startcode = 0; - for (i = get_bits_left(&s->gb); i > 24; i -= 1) { - startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF; + for (i = bitstream_bits_left(&s->bc); i > 24; i -= 1) { + startcode = ((startcode << 1) | bitstream_read(&s->bc, 1)) & 0x000FFFFF; if (startcode == 0x10) break; @@ -473,7 +475,7 @@ static int h261_decode_picture_header(H261Context *h) } /* temporal reference */ - i = get_bits(&s->gb, 5); /* picture timestamp */ + i = bitstream_read(&s->bc, 5); /* picture timestamp */ if (i < (s->picture_number & 31)) i += 32; s->picture_number = (s->picture_number & ~31) + i; @@ -481,11 +483,11 @@ static int h261_decode_picture_header(H261Context *h) s->avctx->framerate = (AVRational) { 30000, 1001 }; /* PTYPE starts here */ - skip_bits1(&s->gb); /* split screen off */ - skip_bits1(&s->gb); /* camera off */ - skip_bits1(&s->gb); /* freeze picture release off */ + bitstream_skip(&s->bc, 1); /* split screen off */ + bitstream_skip(&s->bc, 1); /* camera off */ + bitstream_skip(&s->bc, 1); /* freeze picture release off */ - format = get_bits1(&s->gb); + format = bitstream_read_bit(&s->bc); // only 2 formats possible if (format == 0) { // QCIF @@ -502,12 +504,12 @@ static int h261_decode_picture_header(H261Context *h) s->mb_num = s->mb_width * s->mb_height; - skip_bits1(&s->gb); /* still image mode off */ - skip_bits1(&s->gb); /* Reserved */ + bitstream_skip(&s->bc, 1); /* still image mode off */ + bitstream_skip(&s->bc, 1); /* Reserved */ /* PEI */ - while (get_bits1(&s->gb) != 0) - skip_bits(&s->gb, 8); + while (bitstream_read_bit(&s->bc) != 0) + bitstream_skip(&s->bc, 8); /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first * frame, the codec crashes if it does not contain all I-blocks @@ -552,7 +554,7 @@ static int h261_decode_gob(H261Context *h) */ static int get_consumed_bytes(MpegEncContext *s, int buf_size) { - int pos = get_bits_count(&s->gb) >> 3; + int pos = bitstream_tell(&s->bc) >> 3; if (pos == 0) pos = 1; // avoid infinite loops (i doubt that is needed but ...) if (pos + 10 > buf_size) @@ -577,7 +579,7 @@ static int h261_decode_frame(AVCodecContext *avctx, void *data, h->gob_start_code_skipped = 0; retry: - init_get_bits(&s->gb, buf, buf_size * 8); + bitstream_init8(&s->bc, buf, buf_size); if (!s->context_initialized) // we need the IDCT permutation for reading a custom matrix diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index f096b8a0fa..5d8c31ce02 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -31,6 +31,7 @@ #include #include "avcodec.h" +#include "bitstream.h" #include "blockdsp.h" #include "error_resilience.h" #include "fdctdsp.h" @@ -344,6 +345,7 @@ typedef struct MpegEncContext { int resync_mb_x; ///< x position of last resync marker int resync_mb_y; ///< y position of last resync marker GetBitContext last_resync_gb; ///< used to search for the next resync marker + BitstreamContext last_resync_bc; ///< used to search for the next resync marker int mb_num_left; ///< number of MBs left in this video packet (for partitioned Slices only) int next_p_frame_damaged; ///< set if the next p frame is damaged, to avoid showing trashed B-frames @@ -428,6 +430,7 @@ typedef struct MpegEncContext { /* decompression specific */ GetBitContext gb; + BitstreamContext bc; /* MPEG-1 specific */ int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & MPEG-1 specific diff --git a/libavformat/h261dec.c b/libavformat/h261dec.c index 4a5805020f..d8a2cecc77 100644 --- a/libavformat/h261dec.c +++ b/libavformat/h261dec.c @@ -19,7 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavcodec/get_bits.h" +#include "libavcodec/bitstream.h" + #include "avformat.h" #include "rawdec.h" @@ -31,16 +32,16 @@ static int h261_probe(AVProbeData *p) int invalid_psc=0; int next_gn=0; int src_fmt=0; - GetBitContext gb; + BitstreamContext bc; - init_get_bits(&gb, p->buf, p->buf_size*8); + bitstream_init(&bc, p->buf, p->buf_size * 8); for(i=0; ibuf_size*8; i++){ if ((code & 0x01ff0000) || !(code & 0xff00)) { - code = (code<<8) + get_bits(&gb, 8); + code = (code << 8) + bitstream_read(&bc, 8); i += 7; } else - code = (code<<1) + get_bits1(&gb); + code = (code << 1) + bitstream_read_bit(&bc); if ((code & 0xffff0000) == 0x10000) { int gn= (code>>12)&0xf; if(!gn) From ab2539bd374fe7ddbc6e2f058b62645cd5076192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= Date: Sat, 19 Mar 2016 17:32:04 +0100 Subject: [PATCH 10/10] ffv1: Convert to the new bitstream reader --- libavcodec/ffv1.h | 4 ++-- libavcodec/ffv1dec.c | 24 ++++++++++++------------ libavcodec/ffv1enc.c | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index 34370fa143..7e0465abaa 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -26,7 +26,7 @@ #include #include "avcodec.h" -#include "get_bits.h" +#include "bitstream.h" #include "put_bits.h" #include "rangecoder.h" @@ -70,7 +70,7 @@ typedef struct FFV1Context { AVClass *class; AVCodecContext *avctx; RangeCoder c; - GetBitContext gb; + BitstreamContext bc; PutBitContext pb; uint64_t rc_stat[256][2]; uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c index 2ebd6864a1..07e66b9dbb 100644 --- a/libavcodec/ffv1dec.c +++ b/libavcodec/ffv1dec.c @@ -33,9 +33,9 @@ #include "libavutil/timer.h" #include "avcodec.h" -#include "golomb_legacy.h" +#include "bitstream.h" +#include "golomb.h" #include "internal.h" -#include "get_bits.h" #include "put_bits.h" #include "rangecoder.h" #include "mathops.h" @@ -66,7 +66,7 @@ static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) return get_symbol_inline(c, state, is_signed); } -static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state, +static inline int get_vlc_symbol(BitstreamContext *bc, VlcState *const state, int bits) { int k, i, v, ret; @@ -80,7 +80,7 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state, assert(k <= 8); - v = get_sr_golomb(gb, k, 12, bits); + v = get_sr_golomb(bc, k, 12, bits); ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); @@ -124,13 +124,13 @@ static av_always_inline void decode_line(FFV1Context *s, int w, if (run_mode) { if (run_count == 0 && run_mode == 1) { - if (get_bits1(&s->gb)) { + if (bitstream_read_bit(&s->bc)) { run_count = 1 << ff_log2_run[run_index]; if (x + run_count <= w) run_index++; } else { if (ff_log2_run[run_index]) - run_count = get_bits(&s->gb, ff_log2_run[run_index]); + run_count = bitstream_read(&s->bc, ff_log2_run[run_index]); else run_count = 0; if (run_index) @@ -142,17 +142,17 @@ static av_always_inline void decode_line(FFV1Context *s, int w, if (run_count < 0) { run_mode = 0; run_count = 0; - diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], + diff = get_vlc_symbol(&s->bc, &p->vlc_state[context], bits); if (diff >= 0) diff++; } else diff = 0; } else - diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); + diff = get_vlc_symbol(&s->bc, &p->vlc_state[context], bits); ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n", - run_count, run_index, run_mode, x, get_bits_count(&s->gb)); + run_count, run_index, run_mode, x, bitstream_tell(&s->bc)); } if (sign) @@ -364,9 +364,9 @@ static int decode_slice(AVCodecContext *c, void *arg) if (f->version == 3 && f->minor_version > 1 || f->version > 3) get_rac(&fs->c, (uint8_t[]) { 129 }); fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0; - init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count, - (fs->c.bytestream_end - fs->c.bytestream_start - - fs->ac_byte_count) * 8); + bitstream_init8(&fs->bc, fs->c.bytestream_start + fs->ac_byte_count, + (fs->c.bytestream_end - fs->c.bytestream_start - + fs->ac_byte_count)); } av_assert1(width && height); diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index adf70d8a6b..c5088bb545 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -33,7 +33,7 @@ #include "libavutil/imgutils.h" #include "avcodec.h" -#include "golomb_legacy.h" +#include "golomb.h" #include "internal.h" #include "put_bits.h" #include "rangecoder.h"