comparison bitstream.c @ 5071:0d503c12092b libavcodec

add init_vlc_sparse(). faster than init_vlc() if there are lots of holes in the tables.
author lorenm
date Thu, 24 May 2007 17:38:56 +0000
parents c7904664624c
children 02dd5e0d7e46
comparison
equal deleted inserted replaced
5070:b2b6d7f4cda4 5071:0d503c12092b
102 102
103 static int build_table(VLC *vlc, int table_nb_bits, 103 static int build_table(VLC *vlc, int table_nb_bits,
104 int nb_codes, 104 int nb_codes,
105 const void *bits, int bits_wrap, int bits_size, 105 const void *bits, int bits_wrap, int bits_size,
106 const void *codes, int codes_wrap, int codes_size, 106 const void *codes, int codes_wrap, int codes_size,
107 const void *symbols, int symbols_wrap, int symbols_size,
107 uint32_t code_prefix, int n_prefix, int flags) 108 uint32_t code_prefix, int n_prefix, int flags)
108 { 109 {
109 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2; 110 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
110 uint32_t code; 111 uint32_t code;
111 VLC_TYPE (*table)[2]; 112 VLC_TYPE (*table)[2];
112 113
113 table_size = 1 << table_nb_bits; 114 table_size = 1 << table_nb_bits;
114 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC); 115 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
130 GET_DATA(n, bits, i, bits_wrap, bits_size); 131 GET_DATA(n, bits, i, bits_wrap, bits_size);
131 GET_DATA(code, codes, i, codes_wrap, codes_size); 132 GET_DATA(code, codes, i, codes_wrap, codes_size);
132 /* we accept tables with holes */ 133 /* we accept tables with holes */
133 if (n <= 0) 134 if (n <= 0)
134 continue; 135 continue;
136 if (!symbols)
137 symbol = i;
138 else
139 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
135 #if defined(DEBUG_VLC) && 0 140 #if defined(DEBUG_VLC) && 0
136 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code); 141 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
137 #endif 142 #endif
138 /* if code matches the prefix, it is in the table */ 143 /* if code matches the prefix, it is in the table */
139 n -= n_prefix; 144 n -= n_prefix;
156 if (table[j][1] /*bits*/ != 0) { 161 if (table[j][1] /*bits*/ != 0) {
157 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); 162 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
158 return -1; 163 return -1;
159 } 164 }
160 table[j][1] = n; //bits 165 table[j][1] = n; //bits
161 table[j][0] = i; //code 166 table[j][0] = symbol;
162 j++; 167 j++;
163 } 168 }
164 } else { 169 } else {
165 n -= table_nb_bits; 170 n -= table_nb_bits;
166 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); 171 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
187 table[i][1] = -n; //bits 192 table[i][1] = -n; //bits
188 } 193 }
189 index = build_table(vlc, n, nb_codes, 194 index = build_table(vlc, n, nb_codes,
190 bits, bits_wrap, bits_size, 195 bits, bits_wrap, bits_size,
191 codes, codes_wrap, codes_size, 196 codes, codes_wrap, codes_size,
197 symbols, symbols_wrap, symbols_size,
192 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i), 198 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
193 n_prefix + table_nb_bits, flags); 199 n_prefix + table_nb_bits, flags);
194 if (index < 0) 200 if (index < 0)
195 return -1; 201 return -1;
196 /* note: realloc has been done, so reload tables */ 202 /* note: realloc has been done, so reload tables */
212 218
213 'bits' : table which gives the size (in bits) of each vlc code. 219 'bits' : table which gives the size (in bits) of each vlc code.
214 220
215 'codes' : table which gives the bit pattern of of each vlc code. 221 'codes' : table which gives the bit pattern of of each vlc code.
216 222
223 'symbols' : table which gives the values to be returned from get_vlc().
224
217 'xxx_wrap' : give the number of bytes between each entry of the 225 'xxx_wrap' : give the number of bytes between each entry of the
218 'bits' or 'codes' tables. 226 'bits' or 'codes' tables.
219 227
220 'xxx_size' : gives the number of bytes of each entry of the 'bits' 228 'xxx_size' : gives the number of bytes of each entry of the 'bits'
221 or 'codes' tables. 229 or 'codes' tables.
222 230
223 'wrap' and 'size' allows to use any memory configuration and types 231 'wrap' and 'size' allows to use any memory configuration and types
224 (byte/word/long) to store the 'bits' and 'codes' tables. 232 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
225 233
226 'use_static' should be set to 1 for tables, which should be freed 234 'use_static' should be set to 1 for tables, which should be freed
227 with av_free_static(), 0 if free_vlc() will be used. 235 with av_free_static(), 0 if free_vlc() will be used.
228 */ 236 */
229 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, 237 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
230 const void *bits, int bits_wrap, int bits_size, 238 const void *bits, int bits_wrap, int bits_size,
231 const void *codes, int codes_wrap, int codes_size, 239 const void *codes, int codes_wrap, int codes_size,
240 const void *symbols, int symbols_wrap, int symbols_size,
232 int flags) 241 int flags)
233 { 242 {
234 vlc->bits = nb_bits; 243 vlc->bits = nb_bits;
235 if(!(flags & INIT_VLC_USE_STATIC)) { 244 if(!(flags & INIT_VLC_USE_STATIC)) {
236 vlc->table = NULL; 245 vlc->table = NULL;
248 #endif 257 #endif
249 258
250 if (build_table(vlc, nb_bits, nb_codes, 259 if (build_table(vlc, nb_bits, nb_codes,
251 bits, bits_wrap, bits_size, 260 bits, bits_wrap, bits_size,
252 codes, codes_wrap, codes_size, 261 codes, codes_wrap, codes_size,
262 symbols, symbols_wrap, symbols_size,
253 0, 0, flags) < 0) { 263 0, 0, flags) < 0) {
254 av_free(vlc->table); 264 av_free(vlc->table);
255 return -1; 265 return -1;
256 } 266 }
257 return 0; 267 return 0;