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