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.
15 * \file bitstream.c Bitstream API.
28 #include "bitstream.h"
30 #define GET_DATA(v, table, i, wrap, size) \
32 const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
35 v = *(const uint8_t *)ptr; \
38 v = *(const uint16_t *)ptr; \
41 v = *(const uint32_t *)ptr; \
46 static int alloc_table(struct vlc
*vlc
, int size
)
50 idx
= vlc
->table_size
;
51 vlc
->table_size
+= size
;
52 if (vlc
->table_size
> vlc
->table_allocated
) {
53 vlc
->table_allocated
+= (1 << vlc
->bits
);
54 vlc
->table
= para_realloc(vlc
->table
,
55 sizeof(VLC_TYPE
) * 2 * vlc
->table_allocated
);
60 static int build_table(struct vlc
*vlc
, int table_nb_bits
,
61 int nb_codes
, const void *bits
, int bits_wrap
, int bits_size
,
62 const void *codes
, int codes_wrap
, int codes_size
,
63 uint32_t code_prefix
, int n_prefix
)
65 int ret
, i
, j
, k
, n
, table_size
, table_index
, nb
, n1
, idx
, code_prefix2
,
70 table_size
= 1 << table_nb_bits
;
71 ret
= alloc_table(vlc
, table_size
);
75 table
= &vlc
->table
[table_index
];
77 for (i
= 0; i
< table_size
; i
++) {
78 table
[i
][1] = 0; //bits
79 table
[i
][0] = -1; //codes
82 /* first pass: map codes and compute auxillary table sizes */
83 for (i
= 0; i
< nb_codes
; i
++) {
84 GET_DATA(n
, bits
, i
, bits_wrap
, bits_size
);
85 GET_DATA(code
, codes
, i
, codes_wrap
, codes_size
);
86 /* we accept tables with holes */
90 /* if code matches the prefix, it is in the table */
92 code_prefix2
= code
>> n
;
93 if (n
> 0 && code_prefix2
== code_prefix
) {
94 if (n
<= table_nb_bits
) {
95 /* no need to add another table */
96 j
= (code
<< (table_nb_bits
- n
)) & (table_size
- 1);
97 nb
= 1 << (table_nb_bits
- n
);
98 for (k
= 0; k
< nb
; k
++) {
99 if (table
[j
][1] /* bits */ != 0)
101 table
[j
][1] = n
; //bits
102 table
[j
][0] = symbol
;
107 j
= (code
>> n
) & ((1 << table_nb_bits
) - 1);
108 /* compute table size */
109 n1
= -table
[j
][1]; //bits
112 table
[j
][1] = -n1
; //bits
117 /* second pass : fill auxillary tables recursively */
118 for (i
= 0; i
< table_size
; i
++) {
119 n
= table
[i
][1]; //bits
122 if (n
> table_nb_bits
) {
124 table
[i
][1] = -n
; //bits
126 ret
= build_table(vlc
, n
, nb_codes
,
127 bits
, bits_wrap
, bits_size
,
128 codes
, codes_wrap
, codes_size
,
129 (code_prefix
<< table_nb_bits
) | i
,
130 n_prefix
+ table_nb_bits
);
134 /* note: realloc has been done, so reload tables */
135 table
= &vlc
->table
[table_index
];
136 table
[i
][0] = idx
; //code
143 * Build VLC decoding tables suitable for use with get_vlc().
145 * \param nb_bits Set the decoding table size (2^nb_bits)
146 * entries. The bigger it is, the faster is the decoding. But
147 * it should not be too big to save memory and L1 cache. '9'
148 * is a good compromise.
150 * \param nb_codes Number of vlcs codes.
152 * \param bits Table which gives the size (in bits) of each
155 * \param codes Table which gives the bit pattern of of each
158 * \param bits_wrap The number of bytes between each entry of the
159 * 'bits' or 'codes' tables.
161 * \param bits_size The number of bytes of each entry of the
162 * 'bits' or 'codes' tables.
164 * \param codes_wrap Same as bits_wrap but uses the 'codes' table.
166 * \param codes_size Same as bits_size but for the 'codes' table.
168 * The wrap and size parameters allow to use any memory configuration and
169 * types (byte/word/long) to store the bits and codes tables.
171 int init_vlc(struct vlc
*vlc
, int nb_bits
, int nb_codes
,
172 const void *bits
, int bits_wrap
, int bits_size
,
173 const void *codes
, int codes_wrap
, int codes_size
)
177 PARA_INFO_LOG("nb_codes=%d\n", nb_codes
);
180 vlc
->table_allocated
= 0;
182 ret
= build_table(vlc
, nb_bits
, nb_codes
, bits
, bits_wrap
, bits_size
,
183 codes
, codes_wrap
, codes_size
, 0, 0);
189 void free_vlc(struct vlc
*vlc
)