| 1 |
/* |
| 2 |
* Copyright (C) 2004 Michael Niedermayer |
| 3 |
* |
| 4 |
* This file may be used under the terms of the GNU Lesser General Public |
| 5 |
* License version 2.1, a copy of which is found in LICENSE included in the |
| 6 |
* packaging of this file. |
| 7 |
*/ |
| 8 |
|
| 9 |
/* This was borrowed from FFmpeg */ |
| 10 |
|
| 11 |
#ifndef GET_BITS_H |
| 12 |
#define GET_BITS_H |
| 13 |
|
| 14 |
#include <stdint.h> |
| 15 |
|
| 16 |
struct get_bit_context { |
| 17 |
const uint8_t *buffer, *buffer_end; |
| 18 |
unsigned index; |
| 19 |
unsigned size_in_bits; |
| 20 |
}; |
| 21 |
|
| 22 |
static inline void skip_bits(struct get_bit_context *s, int n) |
| 23 |
{ |
| 24 |
unsigned index = s->index; |
| 25 |
index += n; |
| 26 |
s->index = index; |
| 27 |
} |
| 28 |
|
| 29 |
static inline void init_get_bits(struct get_bit_context *s, const uint8_t *buffer, unsigned bit_size) |
| 30 |
{ |
| 31 |
s->buffer = buffer; |
| 32 |
s->buffer_end = buffer + ((bit_size + 7) >> 3); |
| 33 |
s->size_in_bits = bit_size; |
| 34 |
s->index = 0; |
| 35 |
} |
| 36 |
|
| 37 |
static inline unsigned get_bits1(struct get_bit_context *s) |
| 38 |
{ |
| 39 |
unsigned index = s->index; |
| 40 |
uint8_t result = s->buffer[index >> 3]; |
| 41 |
result <<= (index & 0x07); |
| 42 |
result >>= 8 - 1; |
| 43 |
index++; |
| 44 |
s->index = index; |
| 45 |
return result; |
| 46 |
} |
| 47 |
|
| 48 |
union unaligned_16 { uint16_t l; } __attribute__((packed)); |
| 49 |
union unaligned_32 { uint32_t l; } __attribute__((packed)); |
| 50 |
|
| 51 |
#define AV_RN(s, p) (((const union unaligned_##s *) (p))->l) |
| 52 |
|
| 53 |
#if defined(__i386__) |
| 54 |
static inline uint16_t av_bswap16(uint16_t x) |
| 55 |
{ |
| 56 |
__asm__("rorw $8, %0" : "+r"(x)); |
| 57 |
return x; |
| 58 |
} |
| 59 |
static inline uint32_t av_bswap32(uint32_t x) |
| 60 |
{ |
| 61 |
__asm__("bswap %0" : "+r" (x)); |
| 62 |
return x; |
| 63 |
} |
| 64 |
#define AV_RB16(x) av_bswap16(AV_RN(16, x)) |
| 65 |
#define AV_RB32(x) av_bswap32(AV_RN(32, x)) |
| 66 |
#elif defined(__arm__) |
| 67 |
static inline uint16_t av_read16_be(const uint8_t *p) |
| 68 |
{ |
| 69 |
uint16_t v; |
| 70 |
__asm__("ldrh %0, %1\n\trev16 %0, %0" : "=r"(v) : "m"(*(const uint16_t *)p)); |
| 71 |
return v; |
| 72 |
} |
| 73 |
static inline uint32_t av_read32_be(const uint8_t *p) |
| 74 |
{ |
| 75 |
uint32_t v; |
| 76 |
__asm__("ldr %0, %1" : "=r"(v) : "m"(*(const uint32_t *)p)); |
| 77 |
__asm__("rev %0, %0" : "+r"(v)); |
| 78 |
return v; |
| 79 |
} |
| 80 |
#define AV_RB16(x) av_read16_be(x) |
| 81 |
#define AV_RB32(x) av_read32_be(x) |
| 82 |
#endif |
| 83 |
|
| 84 |
#ifndef AV_RB16 |
| 85 |
#define AV_RB16(x) \ |
| 86 |
((((const uint8_t *)(x))[0] << 8) | \ |
| 87 |
((const uint8_t *)(x))[1]) |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifndef AV_RB32 |
| 91 |
#define AV_RB32(x) \ |
| 92 |
((((const uint8_t *)(x))[0] << 24) | \ |
| 93 |
(((const uint8_t *)(x))[1] << 16) | \ |
| 94 |
(((const uint8_t *)(x))[2] << 8) | \ |
| 95 |
((const uint8_t *)(x))[3]) |
| 96 |
#endif |
| 97 |
|
| 98 |
static inline unsigned get_bits(struct get_bit_context *s, int n) |
| 99 |
{ |
| 100 |
unsigned index = s->index; |
| 101 |
int re_cache = 0; |
| 102 |
register int tmp; |
| 103 |
re_cache = AV_RB32(((const uint8_t *)s->buffer) + (index >> 3)) << (index & 0x07); |
| 104 |
tmp = ((uint32_t)re_cache) >> (32 - n); |
| 105 |
index += n; |
| 106 |
s->index = index; |
| 107 |
return tmp; |
| 108 |
} |
| 109 |
|
| 110 |
static inline unsigned show_bits(struct get_bit_context *s, int n) |
| 111 |
{ |
| 112 |
unsigned index = s->index; |
| 113 |
int re_cache = 0; |
| 114 |
register int tmp; |
| 115 |
re_cache = AV_RB32(((const uint8_t *)s->buffer) + (index >> 3)) << (index & 0x07); |
| 116 |
tmp = ((uint32_t)re_cache) >> (32 - n); |
| 117 |
return tmp; |
| 118 |
} |
| 119 |
|
| 120 |
static inline unsigned get_bits_count(const struct get_bit_context *s) |
| 121 |
{ |
| 122 |
return s->index; |
| 123 |
} |
| 124 |
|
| 125 |
static inline int get_bits_left(const struct get_bit_context *s) |
| 126 |
{ |
| 127 |
return s->size_in_bits - get_bits_count(s); |
| 128 |
} |
| 129 |
|
| 130 |
#endif |