blob: bb98ebdcd8a70b6feebfd6701951e8f437ce4162
1 | #ifndef AVCODEC_GET_BITS_H |
2 | #define AVCODEC_GET_BITS_H |
3 | |
4 | #include <linux/kernel.h> |
5 | #include <linux/types.h> |
6 | #include "common.h" |
7 | |
8 | /* |
9 | * Safe bitstream reading: |
10 | * optionally, the get_bits API can check to ensure that we |
11 | * don't read past input buffer boundaries. This is protected |
12 | * with CONFIG_SAFE_BITSTREAM_READER at the global level, and |
13 | * then below that with UNCHECKED_BITSTREAM_READER at the per- |
14 | * decoder level. This means that decoders that check internally |
15 | * can "#define UNCHECKED_BITSTREAM_READER 1" to disable |
16 | * overread checks. |
17 | * Boundary checking causes a minor performance penalty so for |
18 | * applications that won't want/need this, it can be disabled |
19 | * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0". |
20 | */ |
21 | |
22 | struct get_bits_context { |
23 | const u8 *buffer; |
24 | const u8 *buffer_end; |
25 | int index; |
26 | int size_in_bits; |
27 | int size_in_bits_plus8; |
28 | }; |
29 | |
30 | /* Bitstream reader API docs: |
31 | * name |
32 | * arbitrary name which is used as prefix for the internal variables |
33 | * |
34 | * gb |
35 | * struct get_bits_context |
36 | * |
37 | * OPEN_READER(name, gb) |
38 | * load gb into local variables |
39 | * |
40 | * CLOSE_READER(name, gb) |
41 | * store local vars in gb |
42 | * |
43 | * UPDATE_CACHE(name, gb) |
44 | * Refill the internal cache from the bitstream. |
45 | * After this call at least MIN_CACHE_BITS will be available. |
46 | * |
47 | * GET_CACHE(name, gb) |
48 | * Will output the contents of the internal cache, |
49 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). |
50 | * |
51 | * SHOW_UBITS(name, gb, num) |
52 | * Will return the next num bits. |
53 | * |
54 | * SHOW_SBITS(name, gb, num) |
55 | * Will return the next num bits and do sign extension. |
56 | * |
57 | * SKIP_BITS(name, gb, num) |
58 | * Will skip over the next num bits. |
59 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. |
60 | * |
61 | * SKIP_CACHE(name, gb, num) |
62 | * Will remove the next num bits from the cache (note SKIP_COUNTER |
63 | * MUST be called before UPDATE_CACHE / CLOSE_READER). |
64 | * |
65 | * SKIP_COUNTER(name, gb, num) |
66 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). |
67 | * |
68 | * LAST_SKIP_BITS(name, gb, num) |
69 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. |
70 | * |
71 | * BITS_LEFT(name, gb) |
72 | * Return the number of bits left |
73 | * |
74 | * For examples see get_bits, show_bits, skip_bits, get_vlc. |
75 | */ |
76 | |
77 | #define OPEN_READER_NOSIZE(name, gb) \ |
78 | u32 name ## _index = (gb)->index; \ |
79 | u32 name ## _cache |
80 | |
81 | #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) |
82 | #define BITS_AVAILABLE(name, gb) 1 |
83 | |
84 | #define CLOSE_READER(name, gb) (gb)->index = name ## _index |
85 | |
86 | #define UPDATE_CACHE_LE(name, gb) name ##_cache = \ |
87 | AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) |
88 | |
89 | #define UPDATE_CACHE_BE(name, gb) name ## _cache = \ |
90 | AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) |
91 | |
92 | #define SKIP_COUNTER(name, gb, num) name ## _index += (num) |
93 | |
94 | #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) |
95 | |
96 | #define SKIP_BITS(name, gb, num) \ |
97 | do { \ |
98 | SKIP_CACHE(name, gb, num); \ |
99 | SKIP_COUNTER(name, gb, num); \ |
100 | } while (0) |
101 | |
102 | #define GET_CACHE(name, gb) ((u32) name ## _cache) |
103 | |
104 | #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) |
105 | |
106 | #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num) |
107 | #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num) |
108 | |
109 | #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num) |
110 | #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num) |
111 | |
112 | #ifdef BITSTREAM_READER_LE |
113 | #define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) |
114 | #define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) |
115 | |
116 | #define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num) |
117 | #define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num) |
118 | #else |
119 | #define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) |
120 | #define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) |
121 | |
122 | #define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num) |
123 | #define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num) |
124 | #endif |
125 | |
126 | static inline const int sign_extend(int val, u32 bits) |
127 | { |
128 | u32 shift = 8 * sizeof(int) - bits; |
129 | |
130 | union { u32 u; int s; } v = { (u32) val << shift }; |
131 | return v.s >> shift; |
132 | } |
133 | |
134 | static inline u32 zero_extend(u32 val, u32 bits) |
135 | { |
136 | return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits); |
137 | } |
138 | |
139 | static inline int get_bits_count(const struct get_bits_context *s) |
140 | { |
141 | return s->index; |
142 | } |
143 | |
144 | /** |
145 | * Skips the specified number of bits. |
146 | * @param n the number of bits to skip, |
147 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance |
148 | * from the start to overflow int. Staying within the bitstream + padding |
149 | * is sufficient, too. |
150 | */ |
151 | static inline void skip_bits_long(struct get_bits_context *s, int n) |
152 | { |
153 | s->index += n; |
154 | } |
155 | |
156 | /** |
157 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). |
158 | * if MSB not set it is negative |
159 | * @param n length in bits |
160 | */ |
161 | static inline int get_xbits(struct get_bits_context *s, int n) |
162 | { |
163 | register int sign; |
164 | register int cache; |
165 | |
166 | OPEN_READER(re, s); |
167 | UPDATE_CACHE(re, s); |
168 | cache = GET_CACHE(re, s); |
169 | sign = ~cache >> 31; |
170 | LAST_SKIP_BITS(re, s, n); |
171 | CLOSE_READER(re, s); |
172 | |
173 | return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; |
174 | } |
175 | |
176 | |
177 | static inline int get_xbits_le(struct get_bits_context *s, int n) |
178 | { |
179 | register int sign; |
180 | register int cache; |
181 | |
182 | OPEN_READER(re, s); |
183 | UPDATE_CACHE_LE(re, s); |
184 | cache = GET_CACHE(re, s); |
185 | sign = sign_extend(~cache, n) >> 31; |
186 | LAST_SKIP_BITS(re, s, n); |
187 | CLOSE_READER(re, s); |
188 | |
189 | return (zero_extend(sign ^ cache, n) ^ sign) - sign; |
190 | } |
191 | |
192 | static inline int get_sbits(struct get_bits_context *s, int n) |
193 | { |
194 | register int tmp; |
195 | |
196 | OPEN_READER(re, s); |
197 | UPDATE_CACHE(re, s); |
198 | tmp = SHOW_SBITS(re, s, n); |
199 | LAST_SKIP_BITS(re, s, n); |
200 | CLOSE_READER(re, s); |
201 | |
202 | return tmp; |
203 | } |
204 | |
205 | /** |
206 | * Read 1-25 bits. |
207 | */ |
208 | static inline u32 get_bits(struct get_bits_context *s, int n) |
209 | { |
210 | register u32 tmp; |
211 | |
212 | OPEN_READER(re, s); |
213 | UPDATE_CACHE(re, s); |
214 | tmp = SHOW_UBITS(re, s, n); |
215 | LAST_SKIP_BITS(re, s, n); |
216 | CLOSE_READER(re, s); |
217 | |
218 | return tmp; |
219 | } |
220 | |
221 | /** |
222 | * Read 0-25 bits. |
223 | */ |
224 | static inline int get_bitsz(struct get_bits_context *s, int n) |
225 | { |
226 | return n ? get_bits(s, n) : 0; |
227 | } |
228 | |
229 | static inline u32 get_bits_le(struct get_bits_context *s, int n) |
230 | { |
231 | register int tmp; |
232 | |
233 | OPEN_READER(re, s); |
234 | UPDATE_CACHE_LE(re, s); |
235 | tmp = SHOW_UBITS_LE(re, s, n); |
236 | LAST_SKIP_BITS(re, s, n); |
237 | CLOSE_READER(re, s); |
238 | |
239 | return tmp; |
240 | } |
241 | |
242 | /** |
243 | * Show 1-25 bits. |
244 | */ |
245 | static inline u32 show_bits(struct get_bits_context *s, int n) |
246 | { |
247 | register u32 tmp; |
248 | |
249 | OPEN_READER_NOSIZE(re, s); |
250 | UPDATE_CACHE(re, s); |
251 | tmp = SHOW_UBITS(re, s, n); |
252 | |
253 | return tmp; |
254 | } |
255 | |
256 | static inline void skip_bits(struct get_bits_context *s, int n) |
257 | { |
258 | u32 re_index = s->index; |
259 | LAST_SKIP_BITS(re, s, n); |
260 | CLOSE_READER(re, s); |
261 | } |
262 | |
263 | static inline u32 get_bits1(struct get_bits_context *s) |
264 | { |
265 | u32 index = s->index; |
266 | u8 result = s->buffer[index >> 3]; |
267 | |
268 | #ifdef BITSTREAM_READER_LE |
269 | result >>= index & 7; |
270 | result &= 1; |
271 | #else |
272 | result <<= index & 7; |
273 | result >>= 8 - 1; |
274 | #endif |
275 | |
276 | index++; |
277 | s->index = index; |
278 | |
279 | return result; |
280 | } |
281 | |
282 | static inline u32 show_bits1(struct get_bits_context *s) |
283 | { |
284 | return show_bits(s, 1); |
285 | } |
286 | |
287 | static inline void skip_bits1(struct get_bits_context *s) |
288 | { |
289 | skip_bits(s, 1); |
290 | } |
291 | |
292 | /** |
293 | * Read 0-32 bits. |
294 | */ |
295 | static inline u32 get_bits_long(struct get_bits_context *s, int n) |
296 | { |
297 | if (!n) { |
298 | return 0; |
299 | } else if (n <= MIN_CACHE_BITS) { |
300 | return get_bits(s, n); |
301 | } else { |
302 | #ifdef BITSTREAM_READER_LE |
303 | u32 ret = get_bits(s, 16); |
304 | return ret | (get_bits(s, n - 16) << 16); |
305 | #else |
306 | u32 ret = get_bits(s, 16) << (n - 16); |
307 | return ret | get_bits(s, n - 16); |
308 | #endif |
309 | } |
310 | } |
311 | |
312 | /** |
313 | * Read 0-64 bits. |
314 | */ |
315 | static inline u64 get_bits64(struct get_bits_context *s, int n) |
316 | { |
317 | if (n <= 32) { |
318 | return get_bits_long(s, n); |
319 | } else { |
320 | #ifdef BITSTREAM_READER_LE |
321 | u64 ret = get_bits_long(s, 32); |
322 | return ret | (u64) get_bits_long(s, n - 32) << 32; |
323 | #else |
324 | u64 ret = (u64) get_bits_long(s, n - 32) << 32; |
325 | return ret | get_bits_long(s, 32); |
326 | #endif |
327 | } |
328 | } |
329 | |
330 | /** |
331 | * Read 0-32 bits as a signed integer. |
332 | */ |
333 | static inline int get_sbits_long(struct get_bits_context *s, int n) |
334 | { |
335 | if (!n) |
336 | return 0; |
337 | |
338 | return sign_extend(get_bits_long(s, n), n); |
339 | } |
340 | |
341 | /** |
342 | * Show 0-32 bits. |
343 | */ |
344 | static inline u32 show_bits_long(struct get_bits_context *s, int n) |
345 | { |
346 | if (n <= MIN_CACHE_BITS) { |
347 | return show_bits(s, n); |
348 | } else { |
349 | struct get_bits_context gb = *s; |
350 | |
351 | return get_bits_long(&gb, n); |
352 | } |
353 | } |
354 | |
355 | static inline int check_marker(struct get_bits_context *s, const char *msg) |
356 | { |
357 | int bit = get_bits1(s); |
358 | |
359 | if (!bit) |
360 | pr_err("Marker bit missing at %d of %d %s\n", |
361 | get_bits_count(s) - 1, s->size_in_bits, msg); |
362 | return bit; |
363 | } |
364 | |
365 | static inline int init_get_bits_xe(struct get_bits_context *s, |
366 | const u8 *buffer, int bit_size, int is_le) |
367 | { |
368 | int buffer_size; |
369 | int ret = 0; |
370 | |
371 | if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE * 8) || |
372 | bit_size < 0 || !buffer) { |
373 | bit_size = 0; |
374 | buffer = NULL; |
375 | ret = -1; |
376 | } |
377 | |
378 | buffer_size = (bit_size + 7) >> 3; |
379 | |
380 | s->buffer = buffer; |
381 | s->size_in_bits = bit_size; |
382 | s->size_in_bits_plus8 = bit_size + 8; |
383 | s->buffer_end = buffer + buffer_size; |
384 | s->index = 0; |
385 | |
386 | return ret; |
387 | } |
388 | |
389 | /** |
390 | * Initialize struct get_bits_context. |
391 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes |
392 | * larger than the actual read bits because some optimized bitstream |
393 | * readers read 32 or 64 bit at once and could read over the end |
394 | * @param bit_size the size of the buffer in bits |
395 | * @return 0 on success, -1 if the buffer_size would overflow. |
396 | */ |
397 | static inline int init_get_bits(struct get_bits_context *s, |
398 | const u8 *buffer, int bit_size) |
399 | { |
400 | #ifdef BITSTREAM_READER_LE |
401 | return init_get_bits_xe(s, buffer, bit_size, 1); |
402 | #else |
403 | return init_get_bits_xe(s, buffer, bit_size, 0); |
404 | #endif |
405 | } |
406 | |
407 | /** |
408 | * Initialize struct get_bits_context. |
409 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes |
410 | * larger than the actual read bits because some optimized bitstream |
411 | * readers read 32 or 64 bit at once and could read over the end |
412 | * @param byte_size the size of the buffer in bytes |
413 | * @return 0 on success, -1 if the buffer_size would overflow. |
414 | */ |
415 | static inline int init_get_bits8(struct get_bits_context *s, |
416 | const u8 *buffer, int byte_size) |
417 | { |
418 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
419 | byte_size = -1; |
420 | return init_get_bits(s, buffer, byte_size * 8); |
421 | } |
422 | |
423 | static inline int init_get_bits8_le(struct get_bits_context *s, |
424 | const u8 *buffer, int byte_size) |
425 | { |
426 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
427 | byte_size = -1; |
428 | return init_get_bits_xe(s, buffer, byte_size * 8, 1); |
429 | } |
430 | |
431 | static inline const u8 *align_get_bits(struct get_bits_context *s) |
432 | { |
433 | int n = -get_bits_count(s) & 7; |
434 | |
435 | if (n) |
436 | skip_bits(s, n); |
437 | return s->buffer + (s->index >> 3); |
438 | } |
439 | |
440 | /** |
441 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. |
442 | * If the vlc code is invalid and max_depth>1, then the number of bits removed |
443 | * is undefined. |
444 | */ |
445 | #define GET_VLC(code, name, gb, table, bits, max_depth) \ |
446 | do { \ |
447 | int n, nb_bits; \ |
448 | u32 index; \ |
449 | \ |
450 | index = SHOW_UBITS(name, gb, bits); \ |
451 | code = table[index][0]; \ |
452 | n = table[index][1]; \ |
453 | \ |
454 | if (max_depth > 1 && n < 0) { \ |
455 | LAST_SKIP_BITS(name, gb, bits); \ |
456 | UPDATE_CACHE(name, gb); \ |
457 | \ |
458 | nb_bits = -n; \ |
459 | \ |
460 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ |
461 | code = table[index][0]; \ |
462 | n = table[index][1]; \ |
463 | if (max_depth > 2 && n < 0) { \ |
464 | LAST_SKIP_BITS(name, gb, nb_bits); \ |
465 | UPDATE_CACHE(name, gb); \ |
466 | \ |
467 | nb_bits = -n; \ |
468 | \ |
469 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ |
470 | code = table[index][0]; \ |
471 | n = table[index][1]; \ |
472 | } \ |
473 | } \ |
474 | SKIP_BITS(name, gb, n); \ |
475 | } while (0) |
476 | |
477 | #define GET_RL_VLC(level, run, name, gb, table, bits, \ |
478 | max_depth, need_update) \ |
479 | do { \ |
480 | int n, nb_bits; \ |
481 | u32 index; \ |
482 | \ |
483 | index = SHOW_UBITS(name, gb, bits); \ |
484 | level = table[index].level; \ |
485 | n = table[index].len; \ |
486 | \ |
487 | if (max_depth > 1 && n < 0) { \ |
488 | SKIP_BITS(name, gb, bits); \ |
489 | if (need_update) { \ |
490 | UPDATE_CACHE(name, gb); \ |
491 | } \ |
492 | \ |
493 | nb_bits = -n; \ |
494 | \ |
495 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ |
496 | level = table[index].level; \ |
497 | n = table[index].len; \ |
498 | if (max_depth > 2 && n < 0) { \ |
499 | LAST_SKIP_BITS(name, gb, nb_bits); \ |
500 | if (need_update) { \ |
501 | UPDATE_CACHE(name, gb); \ |
502 | } \ |
503 | nb_bits = -n; \ |
504 | \ |
505 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ |
506 | level = table[index].level; \ |
507 | n = table[index].len; \ |
508 | } \ |
509 | } \ |
510 | run = table[index].run; \ |
511 | SKIP_BITS(name, gb, n); \ |
512 | } while (0) |
513 | |
514 | /* Return the LUT element for the given bitstream configuration. */ |
515 | static inline int set_idx(struct get_bits_context *s, |
516 | int code, int *n, int *nb_bits, int (*table)[2]) |
517 | { |
518 | u32 idx; |
519 | |
520 | *nb_bits = -*n; |
521 | idx = show_bits(s, *nb_bits) + code; |
522 | *n = table[idx][1]; |
523 | |
524 | return table[idx][0]; |
525 | } |
526 | |
527 | /** |
528 | * Parse a vlc code. |
529 | * @param bits is the number of bits which will be read at once, must be |
530 | * identical to nb_bits in init_vlc() |
531 | * @param max_depth is the number of times bits bits must be read to completely |
532 | * read the longest vlc code |
533 | * = (max_vlc_length + bits - 1) / bits |
534 | * @returns the code parsed or -1 if no vlc matches |
535 | */ |
536 | static inline int get_vlc2(struct get_bits_context *s, |
537 | int (*table)[2], int bits, int max_depth) |
538 | { |
539 | int code; |
540 | |
541 | OPEN_READER(re, s); |
542 | UPDATE_CACHE(re, s); |
543 | |
544 | GET_VLC(code, re, s, table, bits, max_depth); |
545 | |
546 | CLOSE_READER(re, s); |
547 | |
548 | return code; |
549 | } |
550 | |
551 | static inline int decode012(struct get_bits_context *gb) |
552 | { |
553 | int n; |
554 | |
555 | n = get_bits1(gb); |
556 | if (n == 0) |
557 | return 0; |
558 | else |
559 | return get_bits1(gb) + 1; |
560 | } |
561 | |
562 | static inline int decode210(struct get_bits_context *gb) |
563 | { |
564 | if (get_bits1(gb)) |
565 | return 0; |
566 | else |
567 | return 2 - get_bits1(gb); |
568 | } |
569 | |
570 | static inline int get_bits_left(struct get_bits_context *gb) |
571 | { |
572 | return gb->size_in_bits - get_bits_count(gb); |
573 | } |
574 | |
575 | static inline int skip_1stop_8data_bits(struct get_bits_context *gb) |
576 | { |
577 | if (get_bits_left(gb) <= 0) |
578 | return -1; |
579 | |
580 | while (get_bits1(gb)) { |
581 | skip_bits(gb, 8); |
582 | if (get_bits_left(gb) <= 0) |
583 | return -1; |
584 | } |
585 | |
586 | return 0; |
587 | } |
588 | |
589 | #endif /* AVCODEC_GET_BITS_H */ |
590 | |
591 |