CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/at3_standalone/get_bits.h
Views: 1401
/*1* copyright (c) 2004 Michael Niedermayer <[email protected]>2*3* This file is part of FFmpeg.4*5* FFmpeg is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* FFmpeg is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with FFmpeg; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920#pragma once21#include <stdint.h>22#include <limits.h>2324#include "compat.h"25#include "intreadwrite.h"2627#ifndef NEG_SSR3228# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))29#endif3031#ifndef NEG_USR3232# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))33#endif3435static inline unsigned zero_extend(unsigned val, unsigned bits)36{37return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);38}3940static inline int sign_extend(int val, unsigned bits)41{42unsigned shift = 8 * sizeof(int) - bits;43union { unsigned u; int s; } v = { (unsigned)val << shift };44return v.s >> shift;45}4647/*48* Safe bitstream reading:49* optionally, the get_bits API can check to ensure that we50* don't read past input buffer boundaries. This is protected51* with CONFIG_SAFE_BITSTREAM_READER at the global level, and52* then below that with UNCHECKED_BITSTREAM_READER at the per-53* decoder level. This means that decoders that check internally54* can "#define UNCHECKED_BITSTREAM_READER 1" to disable55* overread checks.56* Boundary checking causes a minor performance penalty so for57* applications that won't want/need this, it can be disabled58* globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".59*/60#ifndef UNCHECKED_BITSTREAM_READER61#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER62#endif6364typedef struct GetBitContext {65const uint8_t *buffer, *buffer_end;66int index;67int size_in_bits;68int size_in_bits_plus8;69} GetBitContext;7071#define VLC_TYPE int16_t7273typedef struct VLC {74int bits;75VLC_TYPE (*table)[2]; ///< code, bits76int table_size, table_allocated;77} VLC;7879typedef struct RL_VLC_ELEM {80int16_t level;81int8_t len;82uint8_t run;83} RL_VLC_ELEM;8485/* Bitstream reader API docs:86* name87* arbitrary name which is used as prefix for the internal variables88*89* gb90* getbitcontext91*92* OPEN_READER(name, gb)93* load gb into local variables94*95* CLOSE_READER(name, gb)96* store local vars in gb97*98* UPDATE_CACHE(name, gb)99* Refill the internal cache from the bitstream.100* After this call at least MIN_CACHE_BITS will be available.101*102* GET_CACHE(name, gb)103* Will output the contents of the internal cache,104* next bit is MSB of 32 or 64 bit (FIXME 64bit).105*106* SHOW_UBITS(name, gb, num)107* Will return the next num bits.108*109* SHOW_SBITS(name, gb, num)110* Will return the next num bits and do sign extension.111*112* SKIP_BITS(name, gb, num)113* Will skip over the next num bits.114* Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.115*116* SKIP_CACHE(name, gb, num)117* Will remove the next num bits from the cache (note SKIP_COUNTER118* MUST be called before UPDATE_CACHE / CLOSE_READER).119*120* SKIP_COUNTER(name, gb, num)121* Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).122*123* LAST_SKIP_BITS(name, gb, num)124* Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.125*126* BITS_LEFT(name, gb)127* Return the number of bits left128*129* For examples see get_bits, show_bits, skip_bits, get_vlc.130*/131132#ifdef LONG_BITSTREAM_READER133# define MIN_CACHE_BITS 32134#else135# define MIN_CACHE_BITS 25136#endif137138#define OPEN_READER_NOSIZE(name, gb) \139unsigned int name ## _index = (gb)->index; \140unsigned int av_unused name ## _cache141142#if UNCHECKED_BITSTREAM_READER143#define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)144145#define BITS_AVAILABLE(name, gb) 1146#else147#define OPEN_READER(name, gb) \148OPEN_READER_NOSIZE(name, gb); \149unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8150151#define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8152#endif153154#define CLOSE_READER(name, gb) (gb)->index = name ## _index155156# ifdef LONG_BITSTREAM_READER157158# define UPDATE_CACHE_LE(name, gb) name ## _cache = \159AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)160161# define UPDATE_CACHE_BE(name, gb) name ## _cache = \162AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))163164#else165166# define UPDATE_CACHE_LE(name, gb) name ## _cache = \167AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)168169# define UPDATE_CACHE_BE(name, gb) name ## _cache = \170AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)171172#endif173174175#ifdef BITSTREAM_READER_LE176177# define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)178179# define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)180181#else182183# define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)184185# define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)186187#endif188189#if UNCHECKED_BITSTREAM_READER190# define SKIP_COUNTER(name, gb, num) name ## _index += (num)191#else192# define SKIP_COUNTER(name, gb, num) \193name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))194#endif195196#define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))197198#define SKIP_BITS(name, gb, num) \199do { \200SKIP_CACHE(name, gb, num); \201SKIP_COUNTER(name, gb, num); \202} while (0)203204#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)205206#define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)207#define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)208209#define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)210#define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)211212#ifdef BITSTREAM_READER_LE213# define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)214# define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)215#else216# define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)217# define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)218#endif219220#define GET_CACHE(name, gb) ((uint32_t) name ## _cache)221222static inline int get_bits_count(const GetBitContext *s)223{224return s->index;225}226227static inline int get_sbits(GetBitContext *s, int n)228{229int tmp;230OPEN_READER(re, s);231av_assert2(n>0 && n<=25);232UPDATE_CACHE(re, s);233tmp = SHOW_SBITS(re, s, n);234LAST_SKIP_BITS(re, s, n);235CLOSE_READER(re, s);236return tmp;237}238239/**240* Read 1-25 bits.241*/242static inline unsigned int get_bits(GetBitContext *s, int n)243{244int tmp;245OPEN_READER(re, s);246av_assert2(n>0 && n<=25);247UPDATE_CACHE(re, s);248tmp = SHOW_UBITS(re, s, n);249LAST_SKIP_BITS(re, s, n);250CLOSE_READER(re, s);251return tmp;252}253254/**255* Read 0-25 bits.256*/257static inline int get_bitsz(GetBitContext *s, int n)258{259return n ? get_bits(s, n) : 0;260}261262static inline unsigned int get_bits_le(GetBitContext *s, int n)263{264int tmp;265OPEN_READER(re, s);266av_assert2(n>0 && n<=25);267UPDATE_CACHE_LE(re, s);268tmp = SHOW_UBITS_LE(re, s, n);269LAST_SKIP_BITS(re, s, n);270CLOSE_READER(re, s);271return tmp;272}273274static inline void skip_bits(GetBitContext *s, int n)275{276OPEN_READER(re, s);277LAST_SKIP_BITS(re, s, n);278CLOSE_READER(re, s);279}280281static inline unsigned int get_bits1(GetBitContext *s)282{283unsigned int index = s->index;284uint8_t result = s->buffer[index >> 3];285#ifdef BITSTREAM_READER_LE286result >>= index & 7;287result &= 1;288#else289result <<= index & 7;290result >>= 8 - 1;291#endif292#if !UNCHECKED_BITSTREAM_READER293if (s->index < s->size_in_bits_plus8)294#endif295index++;296s->index = index;297298return result;299}300301/**302* Initialize GetBitContext.303* @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes304* larger than the actual read bits because some optimized bitstream305* readers read 32 or 64 bit at once and could read over the end306* @param bit_size the size of the buffer in bits307* @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.308*/309static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,310int bit_size)311{312int buffer_size;313int ret = 0;314315if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {316bit_size = 0;317buffer = NULL;318ret = AVERROR_INVALIDDATA;319}320321buffer_size = (bit_size + 7) >> 3;322323s->buffer = buffer;324s->size_in_bits = bit_size;325s->size_in_bits_plus8 = bit_size + 8;326s->buffer_end = buffer + buffer_size;327s->index = 0;328329return ret;330}331332/**333* Initialize GetBitContext.334* @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes335* larger than the actual read bits because some optimized bitstream336* readers read 32 or 64 bit at once and could read over the end337* @param byte_size the size of the buffer in bytes338* @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.339*/340static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,341int byte_size)342{343if (byte_size > INT_MAX / 8 || byte_size < 0)344byte_size = -1;345return init_get_bits(s, buffer, byte_size * 8);346}347348static inline const uint8_t *align_get_bits(GetBitContext *s)349{350int n = -get_bits_count(s) & 7;351if (n)352skip_bits(s, n);353return s->buffer + (s->index >> 3);354}355356#define init_vlc(vlc, nb_bits, nb_codes, \357bits, bits_wrap, bits_size, \358codes, codes_wrap, codes_size, \359flags) \360ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \361bits, bits_wrap, bits_size, \362codes, codes_wrap, codes_size, \363NULL, 0, 0, flags)364365int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,366const void *bits, int bits_wrap, int bits_size,367const void *codes, int codes_wrap, int codes_size,368const void *symbols, int symbols_wrap, int symbols_size,369int flags);370void ff_free_vlc(VLC *vlc);371372#define INIT_VLC_LE 2373#define INIT_VLC_USE_NEW_STATIC 4374375#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \376do { \377static VLC_TYPE table[static_size][2]; \378(vlc)->table = table; \379(vlc)->table_allocated = static_size; \380init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \381} while (0)382383/**384* If the vlc code is invalid and max_depth=1, then no bits will be removed.385* If the vlc code is invalid and max_depth>1, then the number of bits removed386* is undefined.387*/388#define GET_VLC(code, name, gb, table, bits, max_depth) \389do { \390int n, nb_bits; \391unsigned int index; \392\393index = SHOW_UBITS(name, gb, bits); \394code = table[index][0]; \395n = table[index][1]; \396\397if (max_depth > 1 && n < 0) { \398LAST_SKIP_BITS(name, gb, bits); \399UPDATE_CACHE(name, gb); \400\401nb_bits = -n; \402\403index = SHOW_UBITS(name, gb, nb_bits) + code; \404code = table[index][0]; \405n = table[index][1]; \406if (max_depth > 2 && n < 0) { \407LAST_SKIP_BITS(name, gb, nb_bits); \408UPDATE_CACHE(name, gb); \409\410nb_bits = -n; \411\412index = SHOW_UBITS(name, gb, nb_bits) + code; \413code = table[index][0]; \414n = table[index][1]; \415} \416} \417SKIP_BITS(name, gb, n); \418} while (0)419420/**421* Parse a vlc code.422* @param bits is the number of bits which will be read at once, must be423* identical to nb_bits in init_vlc()424* @param max_depth is the number of times bits bits must be read to completely425* read the longest vlc code426* = (max_vlc_length + bits - 1) / bits427*/428static inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],429int bits, int max_depth)430{431int code;432433OPEN_READER(re, s);434UPDATE_CACHE(re, s);435436GET_VLC(code, re, s, table, bits, max_depth);437438CLOSE_READER(re, s);439440return code;441}442443static inline int get_bits_left(GetBitContext *gb)444{445return gb->size_in_bits - get_bits_count(gb);446}447448#include "intreadwrite.h"449450typedef struct GetByteContext {451const uint8_t *buffer, *buffer_end, *buffer_start;452} GetByteContext;453454#define DEF(type, name, bytes, read, write) \455static inline type bytestream_get_ ## name(const uint8_t **b) \456{ \457(*b) += bytes; \458return read(*b - bytes); \459} \460static inline type bytestream2_get_ ## name ## u(GetByteContext *g) \461{ \462return bytestream_get_ ## name(&g->buffer); \463} \464static inline type bytestream2_get_ ## name(GetByteContext *g) \465{ \466if (g->buffer_end - g->buffer < bytes) { \467g->buffer = g->buffer_end; \468return 0; \469} \470return bytestream2_get_ ## name ## u(g); \471}472473DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)474DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)475DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)476DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)477DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)478DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)479480481