Always call getbit_context structures gbc.
[paraslash.git] / bitstream.h
1 /*
2 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.h.
3 *
4 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * Licensed under the GNU Lesser General Public License.
7 * For licencing details see COPYING.LIB.
8 */
9
10 /** \file bitstream.h Bitstream structures and inline functions. */
11
12 #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
13 (((const uint8_t*)(x))[1] << 16) | \
14 (((const uint8_t*)(x))[2] << 8) | \
15 ((const uint8_t*)(x))[3])
16
17 /** Structure for bistream I/O. */
18 struct getbit_context {
19 /* Start of the internal buffer. */
20 const uint8_t *buffer;
21 /* End of the internal buffer. */
22 const uint8_t *buffer_end;
23 /** Bit counter. */
24 int index;
25 };
26
27 #define VLC_TYPE int16_t
28
29 struct vlc {
30 int bits;
31 VLC_TYPE(*table)[2]; ///< code, bits
32 int table_size, table_allocated;
33 };
34
35 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
36 {
37 int idx = gbc->index;
38 uint32_t x = AV_RB32(gbc->buffer + (idx >> 3)) << (idx & 7);
39 return x >> (32 - num);
40 }
41
42 static inline int get_bits_count(struct getbit_context *gbc)
43 {
44 return gbc->index;
45 }
46
47 static inline void skip_bits(struct getbit_context *gbc, int n)
48 {
49 gbc->index += n;
50 }
51
52 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
53 {
54 unsigned int ret = show_bits(gbc, n);
55 skip_bits(gbc, n);
56 return ret;
57 }
58
59 static inline unsigned int get_bits1(struct getbit_context *gbc)
60 {
61 int idx = gbc->index;
62 uint8_t result = gbc->buffer[idx >> 3];
63
64 result <<= (idx & 0x07);
65 result >>= 8 - 1;
66 idx++;
67 gbc->index = idx;
68 return result;
69 }
70
71 /**
72 * Initialize a getbit_context structure.
73 *
74 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
75 * bytes larger then the actual read bits * because some optimized bitstream
76 * readers read 32 or 64 bit at once and could read over the end.
77 *
78 * \param bit_size The size of the buffer in bytes.
79 */
80 static inline void init_get_bits(struct getbit_context *gbc,
81 const uint8_t *buffer, int size)
82 {
83 gbc->buffer = buffer;
84 gbc->buffer_end = buffer + size;
85 gbc->index = 0;
86 }
87
88 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
89 const void *codes, int codes_size);
90
91 void free_vlc(struct vlc *vlc);
92
93 /**
94 * Parse a vlc code.
95 *
96 * \param bits The number of bits which will be read at once, must be
97 * identical to nb_bits in init_vlc()
98 *
99 * \param max_depth The number of times bits bits must be read to completely
100 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
101 */
102 static inline int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2],
103 int bits, int max_depth)
104 {
105 int n, idx, nb_bits, code;
106
107 idx = show_bits(gbc, bits);
108 code = table[idx][0];
109 n = table[idx][1];
110 if (max_depth > 1 && n < 0) {
111 skip_bits(gbc, bits);
112 nb_bits = -n;
113 idx = show_bits(gbc, nb_bits) + code;
114 code = table[idx][0];
115 n = table[idx][1];
116 if (max_depth > 2 && n < 0) {
117 skip_bits(gbc, nb_bits);
118 nb_bits = -n;
119 idx = show_bits(gbc, nb_bits) + code;
120 code = table[idx][0];
121 n = table[idx][1];
122 }
123 }
124 skip_bits(gbc, n);
125 return code;
126 }