2 * Common bit I/O utils.
4 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.c.
6 * Copyright (c) 2000, 2001 Fabrice Bellard
7 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
8 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
10 * Licensed under the GNU Lesser General Public License.
11 * For licencing details see COPYING.LIB.
14 /** \file bitstream.c Bitstream API for the wma decoder. */
26 #include "bitstream.h"
28 /** Read an 8, 16, or 32 bit entity from a VLC table. */
29 #define GET_DATA(v, table, i, size) \
31 const uint8_t *ptr = (const uint8_t *)table + i * size; \
34 v = *(const uint8_t *)ptr; \
37 v = *(const uint16_t *)ptr; \
40 v = *(const uint32_t *)ptr; \
45 static void alloc_table(struct vlc
*vlc
, int size
)
47 vlc
->table_size
+= size
;
48 if (vlc
->table_size
> vlc
->table_allocated
) {
49 vlc
->table_allocated
+= (1 << vlc
->bits
);
50 vlc
->table
= para_realloc(vlc
->table
,
51 sizeof(VLC_TYPE
) * 2 * vlc
->table_allocated
);
55 static int build_table(struct vlc
*vlc
, int table_nb_bits
, int nb_codes
,
56 const void *bits
, const void *codes
, int codes_size
,
57 uint32_t code_prefix
, int n_prefix
)
59 int i
, j
, k
, n
, table_size
, table_index
, nb
, n1
, idx
;
63 table_size
= 1 << table_nb_bits
;
64 table_index
= vlc
->table_size
;
65 alloc_table(vlc
, table_size
);
66 table
= &vlc
->table
[table_index
];
68 for (i
= 0; i
< table_size
; i
++) {
69 table
[i
][1] = 0; /* bits */
70 table
[i
][0] = -1; /* codes */
73 /* map codes and compute auxiliary table sizes */
74 for (i
= 0; i
< nb_codes
; i
++) {
75 GET_DATA(n
, bits
, i
, 1);
76 /* we accept tables with holes */
80 GET_DATA(code
, codes
, i
, codes_size
);
81 /* if code matches the prefix, it is in the table */
82 if ((code
>> n
) != code_prefix
)
84 if (n
<= table_nb_bits
) {
85 /* no need to add another table */
86 j
= (code
<< (table_nb_bits
- n
)) & (table_size
- 1);
87 nb
= 1 << (table_nb_bits
- n
);
88 for (k
= 0; k
< nb
; k
++) {
89 assert(table
[j
][1] == 0); /* incorrect code */
90 table
[j
][1] = n
; /* bits */
96 j
= (code
>> n
) & ((1 << table_nb_bits
) - 1);
97 /* compute table size */
98 n1
= -table
[j
][1]; /* bits */
101 table
[j
][1] = -n1
; /* bits */
105 /* fill auxiliary tables recursively */
106 for (i
= 0; i
< table_size
; i
++) {
107 n
= table
[i
][1]; /* bits */
110 if (n
> table_nb_bits
) {
112 table
[i
][1] = -n
; /* bits */
114 idx
= build_table(vlc
, n
, nb_codes
, bits
, codes
,
115 codes_size
, (code_prefix
<< table_nb_bits
) | i
,
116 n_prefix
+ table_nb_bits
);
117 /* vlc->table might have changed */
118 table
= &vlc
->table
[table_index
];
119 table
[i
][0] = idx
; /* code */
126 * Build VLC decoding tables suitable for use with get_vlc().
128 * \param vlc The structure to be initialized.
129 * \param nb_bits Set the decoding table size (2^nb_bits) entries.
130 * \param nb_codes Number of vlc codes.
131 * \param bits Table which gives the size (in bits) of each vlc code.
132 * \param codes Table which gives the bit pattern of of each vlc code.
133 * \param codes_size The number of bytes of each entry of the \a codes tables.
135 * The bigger \a nb_bits is, the faster is the decoding. But it should not be
136 * too big to save memory and L1 cache. '9' is a good compromise.
138 * The wrap and size parameters allow to use any memory configuration and
139 * types (byte/word/long) to store the bits and codes tables.
141 void init_vlc(struct vlc
*vlc
, int nb_bits
, int nb_codes
, const void *bits
,
142 const void *codes
, int codes_size
)
144 PARA_INFO_LOG("nb_codes: %d\n", nb_codes
);
147 vlc
->table_allocated
= 0;
149 build_table(vlc
, nb_bits
, nb_codes
, bits
, codes
, codes_size
, 0, 0);
153 * Deallocate all resources of a VLC table.
155 * \param vlc Pointer to an initialized vlc structure.
157 * The table given by \a vlc must have been initialized earlier via \ref
160 void free_vlc(struct vlc
*vlc
)
168 * \param gbc The getbit context structure.
169 * \param table The vlc tables to use.
170 * \param bits The number of bits which will be read at once.
171 * \param max_depth The number of times \a bits bits must be read to completely
172 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
174 * The \a bits parameter must be identical to the \a nb_bits value supplied to
177 * \return The vlc code.
179 int get_vlc(struct getbit_context
*gbc
, VLC_TYPE(*table
)[2], int bits
,
182 int n
, idx
, nb_bits
, code
;
184 idx
= show_bits(gbc
, bits
);
185 code
= table
[idx
][0];
187 if (max_depth
> 1 && n
< 0) {
188 skip_bits(gbc
, bits
);
190 idx
= show_bits(gbc
, nb_bits
) + code
;
191 code
= table
[idx
][0];
193 if (max_depth
> 2 && n
< 0) {
194 skip_bits(gbc
, nb_bits
);
196 idx
= show_bits(gbc
, nb_bits
) + code
;
197 code
= table
[idx
][0];
202 return code
>= 0? code
: -E_VLC
;