]> git.tuebingen.mpg.de Git - paraslash.git/blob - bitstream.h
6765ed8c4a16f698781c0d8f523920a80ae4d1c5
[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 /** Load \a gb into local variables. */
36 #define OPEN_READER(name, gb)\
37         int name##_index= (gb)->index;\
38         int name##_cache= 0;\
39
40 /** Store local vars in gb. */
41 #define CLOSE_READER(name, gb)\
42         (gb)->index= name##_index;\
43
44 /**
45  * Refill the internal cache from the bitstream.
46  */
47 #define UPDATE_CACHE(name, gb)\
48         name##_cache= AV_RB32( ((gb)->buffer) \
49                 + (name##_index >> 3) ) << (name##_index & 0x07);\
50
51 /**
52  * Remove the next num bits from the cache.
53  *
54  * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
55  */
56 #define SKIP_CACHE(name, gb, num)\
57         name##_cache <<= (num);
58
59 /**
60  * Increment the internal bit counter.
61  *
62  * \sa SKIP_CACHE.
63  */
64 #define SKIP_COUNTER(name, gb, num)\
65         name##_index += (num);\
66
67 /** Return the next num bits. */
68 #define SHOW_UBITS(name, gb, num) \
69         (((uint32_t)(name##_cache)) >> (32 - (num)))
70
71 static inline int get_bits_count(struct getbit_context *s)
72 {
73         return s->index;
74 }
75
76 /**
77  * Read 1-17 bits.
78  */
79 static inline unsigned int get_bits(struct getbit_context *s, int n)
80 {
81         register int tmp;
82         OPEN_READER(re, s)
83         UPDATE_CACHE(re, s)
84         tmp = SHOW_UBITS(re, s, n);
85         SKIP_COUNTER(re, s, n)
86         CLOSE_READER(re, s)
87         return tmp;
88 }
89
90 static inline void skip_bits(struct getbit_context *s, int n)
91 {
92         s->index += n;
93 }
94
95 static inline unsigned int get_bits1(struct getbit_context *s)
96 {
97         int idx = s->index;
98         uint8_t result = s->buffer[idx >> 3];
99
100         result <<= (idx & 0x07);
101         result >>= 8 - 1;
102         idx++;
103         s->index = idx;
104         return result;
105 }
106
107 /**
108  * Initialize a getbit_context structure.
109  *
110  * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
111  * bytes larger then the actual read bits * because some optimized bitstream
112  * readers read 32 or 64 bit at once and could read over the end.
113  *
114  * \param bit_size The size of the buffer in bytes.
115  */
116 static inline void init_get_bits(struct getbit_context *s,
117                 const uint8_t *buffer, int size)
118 {
119         s->buffer = buffer;
120         s->buffer_end = buffer + size;
121         s->index = 0;
122 }
123
124 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
125                 const void *codes, int codes_size);
126
127 void free_vlc(struct vlc *vlc);
128
129 /**
130  * Parse a vlc code.
131  *
132  * \param bits The number of bits which will be read at once, must be
133  * identical to nb_bits in init_vlc()
134  *
135  * \param max_depth The number of times bits bits must be read to completely
136  * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
137  */
138 static inline int get_vlc(struct getbit_context *gb, VLC_TYPE(*table)[2],
139                 int bits, int max_depth)
140 {
141         int n, idx, nb_bits, code;
142
143         OPEN_READER(re, gb)
144         UPDATE_CACHE(re, gb)
145         idx = SHOW_UBITS(re, gb, bits);
146         code = table[idx][0];
147         n = table[idx][1];
148         if (max_depth > 1 && n < 0) {
149                 SKIP_COUNTER(re, gb, bits)
150                 UPDATE_CACHE(re, gb)
151                 nb_bits = -n;
152                 idx = SHOW_UBITS(re, gb, nb_bits) + code;
153                 code = table[idx][0];
154                 n = table[idx][1];
155                 if (max_depth > 2 && n < 0) {
156                         SKIP_COUNTER(re, gb, nb_bits)
157                         UPDATE_CACHE(re, gb)
158                         nb_bits = -n;
159                         idx = SHOW_UBITS(re, gb, nb_bits) + code;
160                         code = table[idx][0];
161                         n = table[idx][1];
162                 }
163         }
164         SKIP_CACHE(re, gb, n)
165         SKIP_COUNTER(re, gb, n)
166         CLOSE_READER(re, gb)
167         return code;
168 }