3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
25 * common internal api.
30 const uint8_t ff_sqrt_tab[128]={
31 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
32 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
33 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
34 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
37 const uint8_t ff_log2_tab[256]={
38 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
39 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
40 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
41 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
42 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
43 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
44 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
45 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
48 void align_put_bits(PutBitContext *s)
50 #ifdef ALT_BITSTREAM_WRITER
51 put_bits(s,( - s->index) & 7,0);
53 put_bits(s,s->bit_left & 7,0);
57 void put_string(PutBitContext * pbc, char *s, int put_zero)
67 /* bit input functions */
72 unsigned int get_bits_long(GetBitContext *s, int n){
73 if(n<=17) return get_bits(s, n);
75 int ret= get_bits(s, 16) << (n-16);
76 return ret | get_bits(s, n-16);
83 unsigned int show_bits_long(GetBitContext *s, int n){
84 if(n<=17) return show_bits(s, n);
87 int ret= get_bits_long(s, n);
93 void align_get_bits(GetBitContext *s)
95 int n= (-get_bits_count(s)) & 7;
96 if(n) skip_bits(s, n);
99 int check_marker(GetBitContext *s, const char *msg)
101 int bit= get_bits1(s);
103 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
112 #define GET_DATA(v, table, i, wrap, size) \
114 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
117 v = *(const uint8_t *)ptr;\
120 v = *(const uint16_t *)ptr;\
123 v = *(const uint32_t *)ptr;\
129 static int alloc_table(VLC *vlc, int size)
132 index = vlc->table_size;
133 vlc->table_size += size;
134 if (vlc->table_size > vlc->table_allocated) {
135 vlc->table_allocated += (1 << vlc->bits);
136 vlc->table = av_realloc(vlc->table,
137 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
144 static int build_table(VLC *vlc, int table_nb_bits,
146 const void *bits, int bits_wrap, int bits_size,
147 const void *codes, int codes_wrap, int codes_size,
148 uint32_t code_prefix, int n_prefix)
150 int i, j, k, n, table_size, table_index, nb, n1, index;
152 VLC_TYPE (*table)[2];
154 table_size = 1 << table_nb_bits;
155 table_index = alloc_table(vlc, table_size);
157 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
158 table_index, table_size, code_prefix, n_prefix);
162 table = &vlc->table[table_index];
164 for(i=0;i<table_size;i++) {
165 table[i][1] = 0; //bits
166 table[i][0] = -1; //codes
169 /* first pass: map codes and compute auxillary table sizes */
170 for(i=0;i<nb_codes;i++) {
171 GET_DATA(n, bits, i, bits_wrap, bits_size);
172 GET_DATA(code, codes, i, codes_wrap, codes_size);
173 /* we accept tables with holes */
176 #if defined(DEBUG_VLC) && 0
177 printf("i=%d n=%d code=0x%x\n", i, n, code);
179 /* if code matches the prefix, it is in the table */
181 if (n > 0 && (code >> n) == code_prefix) {
182 if (n <= table_nb_bits) {
183 /* no need to add another table */
184 j = (code << (table_nb_bits - n)) & (table_size - 1);
185 nb = 1 << (table_nb_bits - n);
188 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
191 if (table[j][1] /*bits*/ != 0) {
192 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
195 table[j][1] = n; //bits
196 table[j][0] = i; //code
201 j = (code >> n) & ((1 << table_nb_bits) - 1);
203 printf("%4x: n=%d (subtable)\n",
206 /* compute table size */
207 n1 = -table[j][1]; //bits
210 table[j][1] = -n1; //bits
215 /* second pass : fill auxillary tables recursively */
216 for(i=0;i<table_size;i++) {
217 n = table[i][1]; //bits
220 if (n > table_nb_bits) {
222 table[i][1] = -n; //bits
224 index = build_table(vlc, n, nb_codes,
225 bits, bits_wrap, bits_size,
226 codes, codes_wrap, codes_size,
227 (code_prefix << table_nb_bits) | i,
228 n_prefix + table_nb_bits);
231 /* note: realloc has been done, so reload tables */
232 table = &vlc->table[table_index];
233 table[i][0] = index; //code
240 /* Build VLC decoding tables suitable for use with get_vlc().
242 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
243 bigger it is, the faster is the decoding. But it should not be too
244 big to save memory and L1 cache. '9' is a good compromise.
246 'nb_codes' : number of vlcs codes
248 'bits' : table which gives the size (in bits) of each vlc code.
250 'codes' : table which gives the bit pattern of of each vlc code.
252 'xxx_wrap' : give the number of bytes between each entry of the
253 'bits' or 'codes' tables.
255 'xxx_size' : gives the number of bytes of each entry of the 'bits'
258 'wrap' and 'size' allows to use any memory configuration and types
259 (byte/word/long) to store the 'bits' and 'codes' tables.
261 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
262 const void *bits, int bits_wrap, int bits_size,
263 const void *codes, int codes_wrap, int codes_size)
267 vlc->table_allocated = 0;
270 printf("build table nb_codes=%d\n", nb_codes);
273 if (build_table(vlc, nb_bits, nb_codes,
274 bits, bits_wrap, bits_size,
275 codes, codes_wrap, codes_size,
284 void free_vlc(VLC *vlc)
289 int64_t ff_gcd(int64_t a, int64_t b){
290 if(b) return ff_gcd(b, a%b);