From f9d58a0659bb75333fe058a991d1adcf838ec5f2 Mon Sep 17 00:00:00 2001 From: Alexandra Hájková Date: Thu, 20 Jan 2022 11:13:04 +0100 Subject: lavc/golomb: unmerge the cached bitstream reader code Move the cached bitstream reader code to a new header, which can either be used on its own with the cached bitstream reader, or from get_bits code by #defining CACHED_BITSTREAM_READER. Restore proper attribution for this code. Integration by Anton Khirnov --- libavcodec/golomb.h | 186 ++++---------------------- libavcodec/golomb_bs.h | 317 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/golomb_common.h | 35 +++++ 3 files changed, 376 insertions(+), 162 deletions(-) create mode 100644 libavcodec/golomb_bs.h create mode 100644 libavcodec/golomb_common.h diff --git a/libavcodec/golomb.h b/libavcodec/golomb.h index 164c2583b6..905da4f51c 100644 --- a/libavcodec/golomb.h +++ b/libavcodec/golomb.h @@ -33,17 +33,30 @@ #include #include "get_bits.h" +#include "golomb_common.h" -#define INVALID_VLC 0x80000000 +#if CACHED_BITSTREAM_READER -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]; +#include "golomb_bs.h" + +#define get_ue_golomb golomb_bs_get_ue +#define get_ue_golomb_long golomb_bs_get_ue_long +#define get_ue_golomb_31 golomb_bs_get_ue_31 +#define get_interleaved_ue_golomb golomb_bs_get_ue_interleaved +#define get_te0_golomb golomb_bs_get_te0 +#define get_te_golomb golomb_bs_get_te +#define get_se_golomb golomb_bs_get_se +#define get_se_golomb_long golomb_bs_get_se_long +#define get_interleaved_se_golomb golomb_bs_get_se_interleaved +#define dirac_get_se_golomb golomb_bs_get_se_dirac +#define get_ur_golomb golomb_bs_get_ur +#define get_ur_golomb_jpegls golomb_bs_get_ur_jpegls +#define get_sr_golomb golomb_bs_get_sr +#define get_sr_golomb_flac golomb_bs_get_sr_flac +#define get_ur_golomb_shorten golomb_bs_get_ur_shorten +#define get_sr_golomb_shorten golomb_bs_get_sr_shorten -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]; +#else /** * Read an unsigned Exp-Golomb code in the range 0 to 8190. @@ -54,26 +67,6 @@ static inline int get_ue_golomb(GetBitContext *gb) { unsigned int buf; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - if (buf >= (1 << 27)) { - buf >>= 32 - 9; - skip_bits_long(gb, ff_golomb_vlc_len[buf]); - - return ff_ue_golomb_vlc_code[buf]; - } else { - int log = 2 * av_log2(buf) - 31; - - skip_bits_long(gb, 32 - log); - if (log < 7) - return AVERROR_INVALIDDATA; - buf >>= log; - buf--; - - return buf; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -95,7 +88,6 @@ static inline int get_ue_golomb(GetBitContext *gb) return buf; } -#endif } /** @@ -121,13 +113,6 @@ static inline int get_ue_golomb_31(GetBitContext *gb) { unsigned int buf; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - buf >>= 32 - 9; - skip_bits_long(gb, ff_golomb_vlc_len[buf]); -#else - OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -135,7 +120,6 @@ static inline int get_ue_golomb_31(GetBitContext *gb) buf >>= 32 - 9; LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); CLOSE_READER(re, gb); -#endif return ff_ue_golomb_vlc_code[buf]; } @@ -144,33 +128,6 @@ static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) { uint32_t buf; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - if (buf & 0xAA800000) { - buf >>= 32 - 8; - skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); - - return ff_interleaved_ue_golomb_vlc_code[buf]; - } else { - unsigned ret = 1; - - do { - buf >>= 32 - 8; - skip_bits_long(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]; - buf = show_bits_long(gb, 32); - } while (get_bits_left(gb) > 0); - - return ret - 1; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -202,7 +159,6 @@ static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) CLOSE_READER(re, gb); return ret - 1; } -#endif } /** @@ -240,28 +196,6 @@ static inline int get_se_golomb(GetBitContext *gb) { unsigned int buf; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - if (buf >= (1 << 27)) { - buf >>= 32 - 9; - skip_bits_long(gb, ff_golomb_vlc_len[buf]); - - return ff_se_golomb_vlc_code[buf]; - } else { - int log = 2 * av_log2(buf) - 31; - buf >>= log; - - skip_bits_long(gb, 32 - log); - - if (buf & 1) - buf = -(buf >> 1); - else - buf = (buf >> 1); - - return buf; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -288,7 +222,6 @@ static inline int get_se_golomb(GetBitContext *gb) return buf; } -#endif } static inline int get_se_golomb_long(GetBitContext *gb) @@ -302,30 +235,6 @@ static inline int get_interleaved_se_golomb(GetBitContext *gb) { unsigned int buf; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - if (buf & 0xAA800000) { - buf >>= 32 - 8; - skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); - - return ff_interleaved_se_golomb_vlc_code[buf]; - } else { - int log; - skip_bits(gb, 8); - buf |= 1 | show_bits(gb, 24); - - if ((buf & 0xAAAAAAAA) == 0) - return INVALID_VLC; - - for (log = 31; (buf & 0x80000000) == 0; log--) - buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); - - skip_bits_long(gb, 63 - 2 * log - 8); - - return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -353,7 +262,6 @@ static inline int get_interleaved_se_golomb(GetBitContext *gb) return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; } -#endif } static inline int dirac_get_se_golomb(GetBitContext *gb) @@ -377,24 +285,6 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, unsigned int buf; int log; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - log = av_log2(buf); - - if (log > 31 - limit) { - buf >>= log - k; - buf += (30 - log) << k; - skip_bits_long(gb, 32 + k - log); - - return buf; - } else { - skip_bits_long(gb, limit); - buf = get_bits_long(gb, esc_len); - - return buf + limit - 1; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -419,7 +309,6 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, return buf + limit - 1; } -#endif } /** @@ -431,35 +320,6 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, unsigned int buf; int log; -#if CACHED_BITSTREAM_READER - buf = show_bits_long(gb, 32); - - log = av_log2(buf); - - if (log - k >= 1 && 32 - log < limit) { - buf >>= log - k; - buf += (30 - log) << k; - skip_bits_long(gb, 32 + k - log); - - return buf; - } else { - int i; - for (i = 0; - i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0; - i++); - - if (i < limit - 1) { - buf = get_bits_long(gb, k); - - return buf + (i << k); - } else if (i == limit - 1) { - buf = get_bits_long(gb, esc_len); - - return buf + 1; - } else - return -1; - } -#else OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); @@ -520,7 +380,6 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, CLOSE_READER(re, gb); return buf; } -#endif } /** @@ -613,4 +472,7 @@ static inline int get_te(GetBitContext *s, int r, char *file, const char *func, #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__) #endif /* TRACE */ + +#endif /* CACHED_BITSTREAM_READER */ + #endif /* AVCODEC_GOLOMB_H */ diff --git a/libavcodec/golomb_bs.h b/libavcodec/golomb_bs.h new file mode 100644 index 0000000000..bba126bfc4 --- /dev/null +++ b/libavcodec/golomb_bs.h @@ -0,0 +1,317 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_GOLOMB_BS_H +#define AVCODEC_GOLOMB_BS_H + +#include + +#include "bitstream.h" +#include "golomb_common.h" + +/** + * Read an unsigned Exp-Golomb code in the range 0 to 8190. + * + * @returns the read value or a negative error code. + */ +static inline int golomb_bs_get_ue(BitstreamContext *bc) +{ + unsigned int buf = bitstream_peek(bc, 32); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]); + + return ff_ue_golomb_vlc_code[buf]; + } else { + int log = 2 * av_log2(buf) - 31; + + bitstream_skip_cache(bc, 32 - log); + if (log < 7) + return AVERROR_INVALIDDATA; + buf >>= log; + buf--; + + return buf; + } +} + +/** + * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1. + */ +static inline unsigned golomb_bs_get_ue_long(BitstreamContext *bc) +{ + unsigned int buf = bitstream_peek(bc, 32); + unsigned int log = 31 - av_log2(buf); + bitstream_skip_cache(bc, log); + return bitstream_read(bc, log + 1) - 1; +} + +/** + * read unsigned exp golomb code, constraint to a max of 31. + * If the value encountered is not in 0..31, the return value + * is outside the range 0..30. + */ +static inline int golomb_bs_get_ue_31(BitstreamContext *bc) +{ + unsigned int buf = bitstream_peek(bc, 32); + + buf >>= 32 - 9; + bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]); + + return ff_ue_golomb_vlc_code[buf]; +} + +static inline unsigned golomb_bs_get_ue_interleaved(BitstreamContext *bc) +{ + uint32_t buf = bitstream_peek(bc, 32); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]); + + return ff_interleaved_ue_golomb_vlc_code[buf]; + } else { + unsigned ret = 1; + + do { + buf >>= 32 - 8; + bitstream_skip_cache(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; + ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; + break; + } + ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; + buf = bitstream_peek(bc, 32); + } while (bitstream_bits_left(bc) > 0); + + return ret - 1; + } +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int golomb_bs_get_te0(BitstreamContext *bc, int range) +{ + av_assert2(range >= 1); + + if (range == 1) + return 0; + else if (range == 2) + return bitstream_read(bc, 1) ^ 1; + else + return golomb_bs_get_ue(bc); +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int golomb_bs_get_te(BitstreamContext *bc, int range) +{ + av_assert2(range >= 1); + + if (range == 2) + return bitstream_read(bc, 1) ^ 1; + else + return golomb_bs_get_ue(bc); +} + +/** + * read signed exp golomb code. + */ +static inline int golomb_bs_get_se(BitstreamContext *bc) +{ + unsigned int buf; + + buf = bitstream_peek(bc, 32); + + if (buf >= (1 << 27)) { + buf >>= 32 - 9; + bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]); + + return ff_se_golomb_vlc_code[buf]; + } else { + int log = 2 * av_log2(buf) - 31; + buf >>= log; + + bitstream_skip_cache(bc, 32 - log); + + if (buf & 1) + buf = -(buf >> 1); + else + buf = (buf >> 1); + + return buf; + } +} + +static inline int golomb_bs_get_se_long(BitstreamContext *bc) +{ + unsigned int buf = golomb_bs_get_ue_long(bc); + int sign = (buf & 1) - 1; + return ((buf >> 1) ^ sign) + 1; +} + +static inline int golomb_bs_get_se_interleaved(BitstreamContext *bc) +{ + unsigned int buf; + + buf = bitstream_peek(bc, 32); + + if (buf & 0xAA800000) { + buf >>= 32 - 8; + bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]); + + return ff_interleaved_se_golomb_vlc_code[buf]; + } else { + int log; + bitstream_skip_cache(bc, 8); + buf |= 1 | bitstream_peek(bc, 24); + + if ((buf & 0xAAAAAAAA) == 0) + return INVALID_VLC; + + for (log = 31; (buf & 0x80000000) == 0; log--) + buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); + + bitstream_skip(bc, 63 - 2 * log - 8); + + return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; + } +} + +static inline int golomb_bs_get_se_dirac(BitstreamContext *bc) +{ + uint32_t ret = golomb_bs_get_ue_interleaved(bc); + + if (ret) { + int sign = -bitstream_read(bc, 1); + ret = (ret ^ sign) - sign; + } + + return ret; +} + +/** + * read unsigned golomb rice code (ffv1). + */ +static inline int golomb_bs_get_ur(BitstreamContext *bc, int k, int limit, + int esc_len) +{ + unsigned int buf; + int log; + + buf = bitstream_peek(bc, 32); + + log = av_log2(buf); + + if (log > 31 - limit) { + buf >>= log - k; + buf += (30 - log) << k; + bitstream_skip(bc, 32 + k - log); + + return buf; + } else { + bitstream_skip(bc, limit); + buf = bitstream_read(bc, esc_len); + + return buf + limit - 1; + } +} + +/** + * read unsigned golomb rice code (jpegls). + */ +static inline int golomb_bs_get_ur_jpegls(BitstreamContext *bc, int k, int limit, + int esc_len) +{ + unsigned int buf; + int log; + + buf = bitstream_peek(bc, 32); + + log = av_log2(buf); + + if (log - k >= 1 && 32 - log < limit) { + buf >>= log - k; + buf += (30 - log) << k; + bitstream_skip(bc, 32 + k - log); + + return buf; + } else { + int i; + for (i = 0; + i < limit && bitstream_peek(bc, 1) == 0 && bitstream_bits_left(bc) > 0; + i++) + bitstream_skip_cache(bc, 1); + bitstream_skip(bc, 1); + + if (i < limit - 1) { + buf = bitstream_read(bc, k); + + return buf + (i << k); + } else if (i == limit - 1) { + buf = bitstream_read(bc, esc_len); + + return buf + 1; + } else + return -1; + } +} + +/** + * read signed golomb rice code (ffv1). + */ +static inline int golomb_bs_get_sr(BitstreamContext *bc, int k, int limit, + int esc_len) +{ + unsigned v = golomb_bs_get_ur(bc, k, limit, esc_len); + return (v >> 1) ^ -(v & 1); +} + +/** + * read signed golomb rice code (flac). + */ +static inline int golomb_bs_get_sr_flac(BitstreamContext *bc, int k, int limit, + int esc_len) +{ + unsigned v = golomb_bs_get_ur_jpegls(bc, k, limit, esc_len); + return (v >> 1) ^ -(v & 1); +} + +/** + * read unsigned golomb rice code (shorten). + */ +static inline unsigned int golomb_bs_get_ur_shorten(BitstreamContext *bc, int k) +{ + return golomb_bs_get_ur_jpegls(bc, k, INT_MAX, 0); +} + +/** + * read signed golomb rice code (shorten). + */ +static inline int golomb_bs_get_sr_shorten(BitstreamContext *bc, int k) +{ + int uvar = golomb_bs_get_ur_jpegls(bc, k + 1, INT_MAX, 0); + return (uvar >> 1) ^ -(uvar & 1); +} + +#endif /* AVCODEC_GOLOMB_BS_H */ diff --git a/libavcodec/golomb_common.h b/libavcodec/golomb_common.h new file mode 100644 index 0000000000..19136aae3c --- /dev/null +++ b/libavcodec/golomb_common.h @@ -0,0 +1,35 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_GOLOMB_COMMON_H +#define AVCODEC_GOLOMB_COMMON_H + +#include + +#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_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]; + +#endif /* AVCODEC_GOLOMB_COMMON_H */ -- cgit v1.2.3