CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/at3_standalone/get_bits.h
Views: 1401
1
/*
2
* copyright (c) 2004 Michael Niedermayer <[email protected]>
3
*
4
* This file is part of FFmpeg.
5
*
6
* FFmpeg is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* FFmpeg is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with FFmpeg; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#pragma once
22
#include <stdint.h>
23
#include <limits.h>
24
25
#include "compat.h"
26
#include "intreadwrite.h"
27
28
#ifndef NEG_SSR32
29
# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
30
#endif
31
32
#ifndef NEG_USR32
33
# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
34
#endif
35
36
static inline unsigned zero_extend(unsigned val, unsigned bits)
37
{
38
return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
39
}
40
41
static inline int sign_extend(int val, unsigned bits)
42
{
43
unsigned shift = 8 * sizeof(int) - bits;
44
union { unsigned u; int s; } v = { (unsigned)val << shift };
45
return v.s >> shift;
46
}
47
48
/*
49
* Safe bitstream reading:
50
* optionally, the get_bits API can check to ensure that we
51
* don't read past input buffer boundaries. This is protected
52
* with CONFIG_SAFE_BITSTREAM_READER at the global level, and
53
* then below that with UNCHECKED_BITSTREAM_READER at the per-
54
* decoder level. This means that decoders that check internally
55
* can "#define UNCHECKED_BITSTREAM_READER 1" to disable
56
* overread checks.
57
* Boundary checking causes a minor performance penalty so for
58
* applications that won't want/need this, it can be disabled
59
* globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
60
*/
61
#ifndef UNCHECKED_BITSTREAM_READER
62
#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
63
#endif
64
65
typedef struct GetBitContext {
66
const uint8_t *buffer, *buffer_end;
67
int index;
68
int size_in_bits;
69
int size_in_bits_plus8;
70
} GetBitContext;
71
72
#define VLC_TYPE int16_t
73
74
typedef struct VLC {
75
int bits;
76
VLC_TYPE (*table)[2]; ///< code, bits
77
int table_size, table_allocated;
78
} VLC;
79
80
typedef struct RL_VLC_ELEM {
81
int16_t level;
82
int8_t len;
83
uint8_t run;
84
} RL_VLC_ELEM;
85
86
/* Bitstream reader API docs:
87
* name
88
* arbitrary name which is used as prefix for the internal variables
89
*
90
* gb
91
* getbitcontext
92
*
93
* OPEN_READER(name, gb)
94
* load gb into local variables
95
*
96
* CLOSE_READER(name, gb)
97
* store local vars in gb
98
*
99
* UPDATE_CACHE(name, gb)
100
* Refill the internal cache from the bitstream.
101
* After this call at least MIN_CACHE_BITS will be available.
102
*
103
* GET_CACHE(name, gb)
104
* Will output the contents of the internal cache,
105
* next bit is MSB of 32 or 64 bit (FIXME 64bit).
106
*
107
* SHOW_UBITS(name, gb, num)
108
* Will return the next num bits.
109
*
110
* SHOW_SBITS(name, gb, num)
111
* Will return the next num bits and do sign extension.
112
*
113
* SKIP_BITS(name, gb, num)
114
* Will skip over the next num bits.
115
* Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
116
*
117
* SKIP_CACHE(name, gb, num)
118
* Will remove the next num bits from the cache (note SKIP_COUNTER
119
* MUST be called before UPDATE_CACHE / CLOSE_READER).
120
*
121
* SKIP_COUNTER(name, gb, num)
122
* Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
123
*
124
* LAST_SKIP_BITS(name, gb, num)
125
* Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
126
*
127
* BITS_LEFT(name, gb)
128
* Return the number of bits left
129
*
130
* For examples see get_bits, show_bits, skip_bits, get_vlc.
131
*/
132
133
#ifdef LONG_BITSTREAM_READER
134
# define MIN_CACHE_BITS 32
135
#else
136
# define MIN_CACHE_BITS 25
137
#endif
138
139
#define OPEN_READER_NOSIZE(name, gb) \
140
unsigned int name ## _index = (gb)->index; \
141
unsigned int av_unused name ## _cache
142
143
#if UNCHECKED_BITSTREAM_READER
144
#define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
145
146
#define BITS_AVAILABLE(name, gb) 1
147
#else
148
#define OPEN_READER(name, gb) \
149
OPEN_READER_NOSIZE(name, gb); \
150
unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
151
152
#define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
153
#endif
154
155
#define CLOSE_READER(name, gb) (gb)->index = name ## _index
156
157
# ifdef LONG_BITSTREAM_READER
158
159
# define UPDATE_CACHE_LE(name, gb) name ## _cache = \
160
AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
161
162
# define UPDATE_CACHE_BE(name, gb) name ## _cache = \
163
AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
164
165
#else
166
167
# define UPDATE_CACHE_LE(name, gb) name ## _cache = \
168
AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
169
170
# define UPDATE_CACHE_BE(name, gb) name ## _cache = \
171
AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
172
173
#endif
174
175
176
#ifdef BITSTREAM_READER_LE
177
178
# define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
179
180
# define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
181
182
#else
183
184
# define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
185
186
# define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
187
188
#endif
189
190
#if UNCHECKED_BITSTREAM_READER
191
# define SKIP_COUNTER(name, gb, num) name ## _index += (num)
192
#else
193
# define SKIP_COUNTER(name, gb, num) \
194
name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
195
#endif
196
197
#define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
198
199
#define SKIP_BITS(name, gb, num) \
200
do { \
201
SKIP_CACHE(name, gb, num); \
202
SKIP_COUNTER(name, gb, num); \
203
} while (0)
204
205
#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
206
207
#define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
208
#define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
209
210
#define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
211
#define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
212
213
#ifdef BITSTREAM_READER_LE
214
# define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
215
# define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
216
#else
217
# define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
218
# define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
219
#endif
220
221
#define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
222
223
static inline int get_bits_count(const GetBitContext *s)
224
{
225
return s->index;
226
}
227
228
static inline int get_sbits(GetBitContext *s, int n)
229
{
230
int tmp;
231
OPEN_READER(re, s);
232
av_assert2(n>0 && n<=25);
233
UPDATE_CACHE(re, s);
234
tmp = SHOW_SBITS(re, s, n);
235
LAST_SKIP_BITS(re, s, n);
236
CLOSE_READER(re, s);
237
return tmp;
238
}
239
240
/**
241
* Read 1-25 bits.
242
*/
243
static inline unsigned int get_bits(GetBitContext *s, int n)
244
{
245
int tmp;
246
OPEN_READER(re, s);
247
av_assert2(n>0 && n<=25);
248
UPDATE_CACHE(re, s);
249
tmp = SHOW_UBITS(re, s, n);
250
LAST_SKIP_BITS(re, s, n);
251
CLOSE_READER(re, s);
252
return tmp;
253
}
254
255
/**
256
* Read 0-25 bits.
257
*/
258
static inline int get_bitsz(GetBitContext *s, int n)
259
{
260
return n ? get_bits(s, n) : 0;
261
}
262
263
static inline unsigned int get_bits_le(GetBitContext *s, int n)
264
{
265
int tmp;
266
OPEN_READER(re, s);
267
av_assert2(n>0 && n<=25);
268
UPDATE_CACHE_LE(re, s);
269
tmp = SHOW_UBITS_LE(re, s, n);
270
LAST_SKIP_BITS(re, s, n);
271
CLOSE_READER(re, s);
272
return tmp;
273
}
274
275
static inline void skip_bits(GetBitContext *s, int n)
276
{
277
OPEN_READER(re, s);
278
LAST_SKIP_BITS(re, s, n);
279
CLOSE_READER(re, s);
280
}
281
282
static inline unsigned int get_bits1(GetBitContext *s)
283
{
284
unsigned int index = s->index;
285
uint8_t result = s->buffer[index >> 3];
286
#ifdef BITSTREAM_READER_LE
287
result >>= index & 7;
288
result &= 1;
289
#else
290
result <<= index & 7;
291
result >>= 8 - 1;
292
#endif
293
#if !UNCHECKED_BITSTREAM_READER
294
if (s->index < s->size_in_bits_plus8)
295
#endif
296
index++;
297
s->index = index;
298
299
return result;
300
}
301
302
/**
303
* Initialize GetBitContext.
304
* @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
305
* larger than the actual read bits because some optimized bitstream
306
* readers read 32 or 64 bit at once and could read over the end
307
* @param bit_size the size of the buffer in bits
308
* @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
309
*/
310
static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
311
int bit_size)
312
{
313
int buffer_size;
314
int ret = 0;
315
316
if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
317
bit_size = 0;
318
buffer = NULL;
319
ret = AVERROR_INVALIDDATA;
320
}
321
322
buffer_size = (bit_size + 7) >> 3;
323
324
s->buffer = buffer;
325
s->size_in_bits = bit_size;
326
s->size_in_bits_plus8 = bit_size + 8;
327
s->buffer_end = buffer + buffer_size;
328
s->index = 0;
329
330
return ret;
331
}
332
333
/**
334
* Initialize GetBitContext.
335
* @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
336
* larger than the actual read bits because some optimized bitstream
337
* readers read 32 or 64 bit at once and could read over the end
338
* @param byte_size the size of the buffer in bytes
339
* @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
340
*/
341
static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
342
int byte_size)
343
{
344
if (byte_size > INT_MAX / 8 || byte_size < 0)
345
byte_size = -1;
346
return init_get_bits(s, buffer, byte_size * 8);
347
}
348
349
static inline const uint8_t *align_get_bits(GetBitContext *s)
350
{
351
int n = -get_bits_count(s) & 7;
352
if (n)
353
skip_bits(s, n);
354
return s->buffer + (s->index >> 3);
355
}
356
357
#define init_vlc(vlc, nb_bits, nb_codes, \
358
bits, bits_wrap, bits_size, \
359
codes, codes_wrap, codes_size, \
360
flags) \
361
ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
362
bits, bits_wrap, bits_size, \
363
codes, codes_wrap, codes_size, \
364
NULL, 0, 0, flags)
365
366
int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
367
const void *bits, int bits_wrap, int bits_size,
368
const void *codes, int codes_wrap, int codes_size,
369
const void *symbols, int symbols_wrap, int symbols_size,
370
int flags);
371
void ff_free_vlc(VLC *vlc);
372
373
#define INIT_VLC_LE 2
374
#define INIT_VLC_USE_NEW_STATIC 4
375
376
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
377
do { \
378
static VLC_TYPE table[static_size][2]; \
379
(vlc)->table = table; \
380
(vlc)->table_allocated = static_size; \
381
init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
382
} while (0)
383
384
/**
385
* If the vlc code is invalid and max_depth=1, then no bits will be removed.
386
* If the vlc code is invalid and max_depth>1, then the number of bits removed
387
* is undefined.
388
*/
389
#define GET_VLC(code, name, gb, table, bits, max_depth) \
390
do { \
391
int n, nb_bits; \
392
unsigned int index; \
393
\
394
index = SHOW_UBITS(name, gb, bits); \
395
code = table[index][0]; \
396
n = table[index][1]; \
397
\
398
if (max_depth > 1 && n < 0) { \
399
LAST_SKIP_BITS(name, gb, bits); \
400
UPDATE_CACHE(name, gb); \
401
\
402
nb_bits = -n; \
403
\
404
index = SHOW_UBITS(name, gb, nb_bits) + code; \
405
code = table[index][0]; \
406
n = table[index][1]; \
407
if (max_depth > 2 && n < 0) { \
408
LAST_SKIP_BITS(name, gb, nb_bits); \
409
UPDATE_CACHE(name, gb); \
410
\
411
nb_bits = -n; \
412
\
413
index = SHOW_UBITS(name, gb, nb_bits) + code; \
414
code = table[index][0]; \
415
n = table[index][1]; \
416
} \
417
} \
418
SKIP_BITS(name, gb, n); \
419
} while (0)
420
421
/**
422
* Parse a vlc code.
423
* @param bits is the number of bits which will be read at once, must be
424
* identical to nb_bits in init_vlc()
425
* @param max_depth is the number of times bits bits must be read to completely
426
* read the longest vlc code
427
* = (max_vlc_length + bits - 1) / bits
428
*/
429
static inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
430
int bits, int max_depth)
431
{
432
int code;
433
434
OPEN_READER(re, s);
435
UPDATE_CACHE(re, s);
436
437
GET_VLC(code, re, s, table, bits, max_depth);
438
439
CLOSE_READER(re, s);
440
441
return code;
442
}
443
444
static inline int get_bits_left(GetBitContext *gb)
445
{
446
return gb->size_in_bits - get_bits_count(gb);
447
}
448
449
#include "intreadwrite.h"
450
451
typedef struct GetByteContext {
452
const uint8_t *buffer, *buffer_end, *buffer_start;
453
} GetByteContext;
454
455
#define DEF(type, name, bytes, read, write) \
456
static inline type bytestream_get_ ## name(const uint8_t **b) \
457
{ \
458
(*b) += bytes; \
459
return read(*b - bytes); \
460
} \
461
static inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
462
{ \
463
return bytestream_get_ ## name(&g->buffer); \
464
} \
465
static inline type bytestream2_get_ ## name(GetByteContext *g) \
466
{ \
467
if (g->buffer_end - g->buffer < bytes) { \
468
g->buffer = g->buffer_end; \
469
return 0; \
470
} \
471
return bytestream2_get_ ## name ## u(g); \
472
}
473
474
DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
475
DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
476
DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
477
DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
478
DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
479
DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
480
481