3bcd2759a703ab3d62567d9da4d5ae07dfdc1a71
[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 /** Structure for bistream I/O. */
13 struct getbit_context {
14         /** Start of the internal buffer. */
15         const uint8_t *buffer;
16         /** Length of buffer in bits (always a multiple of 8). */
17         uint32_t num_bits;
18         /** Bit counter. */
19         int index;
20 };
21
22 #define VLC_TYPE int16_t
23
24 /** A variable length code table. */
25 struct vlc {
26         /** Number of bits of the table. */
27         int bits;
28         /** The code and the bits table. */
29         VLC_TYPE(*table)[2];
30         /** The size of the table. */
31         int table_size;
32         /** Amount of memory allocated so far. */
33         int table_allocated;
34 };
35
36 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
37 {
38         int idx = gbc->index;
39         const char *p;
40         uint32_t x;
41
42         assert(idx + num <= gbc->num_bits);
43         p = (const char *)gbc->buffer + (idx >> 3);
44         x = read_u32_be(p);
45         return (x << (idx & 7)) >> (32 - num);
46 }
47
48 static inline int get_bits_count(struct getbit_context *gbc)
49 {
50         return gbc->index;
51 }
52
53 static inline void skip_bits(struct getbit_context *gbc, int n)
54 {
55         assert(gbc->index + n <= gbc->num_bits);
56         gbc->index += n;
57 }
58
59 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
60 {
61         unsigned int ret = show_bits(gbc, n); /* checks n */
62         skip_bits(gbc, n);
63         return ret;
64 }
65
66 /* This is rather hot, we can do better than get_bits(gbc, 1). */
67 static inline unsigned int get_bit(struct getbit_context *gbc)
68 {
69         int idx;
70         uint8_t tmp, mask;
71
72         assert(gbc->index < gbc->num_bits);
73         idx = gbc->index++;
74         tmp = gbc->buffer[idx >> 3];
75         mask = 1 << (7 - (idx & 7));
76         return !!(tmp & mask);
77 }
78
79 /**
80  * Initialize a getbit_context structure.
81  *
82  * \param gbc The structure to initialize.
83  * \param buffer The bitstream buffer.
84  * \param size The size of the buffer in bytes.
85  *
86  * The bitstream buffer must be 4 bytes larger then the actual read bits
87  * because the bitstream reader might read 32 bits at once and could read over
88  * the end.
89  */
90 static inline void init_get_bits(struct getbit_context *gbc,
91                 const uint8_t *buffer, int size)
92 {
93         gbc->buffer = buffer;
94         gbc->num_bits = size * 8;
95         gbc->index = 0;
96 }
97
98 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
99                 const void *codes, int codes_size);
100 void free_vlc(struct vlc *vlc);
101 int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2], int bits);