comparison bitstream.c @ 2663:b33be8b00488 libavcodec

LE bitstream reader based upon a patch by (Balatoni Denes <dbalatoni programozo hu)
author michael
date Wed, 11 May 2005 01:46:13 +0000
parents 582e635cfa08
children 5dfb90019814
comparison
equal deleted inserted replaced
2662:2fe9599170f6 2663:b33be8b00488
130 130
131 static int build_table(VLC *vlc, int table_nb_bits, 131 static int build_table(VLC *vlc, int table_nb_bits,
132 int nb_codes, 132 int nb_codes,
133 const void *bits, int bits_wrap, int bits_size, 133 const void *bits, int bits_wrap, int bits_size,
134 const void *codes, int codes_wrap, int codes_size, 134 const void *codes, int codes_wrap, int codes_size,
135 uint32_t code_prefix, int n_prefix, int use_static) 135 uint32_t code_prefix, int n_prefix, int flags)
136 { 136 {
137 int i, j, k, n, table_size, table_index, nb, n1, index; 137 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
138 uint32_t code; 138 uint32_t code;
139 VLC_TYPE (*table)[2]; 139 VLC_TYPE (*table)[2];
140 140
141 table_size = 1 << table_nb_bits; 141 table_size = 1 << table_nb_bits;
142 table_index = alloc_table(vlc, table_size, use_static); 142 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
143 #ifdef DEBUG_VLC 143 #ifdef DEBUG_VLC
144 printf("new table index=%d size=%d code_prefix=%x n=%d\n", 144 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
145 table_index, table_size, code_prefix, n_prefix); 145 table_index, table_size, code_prefix, n_prefix);
146 #endif 146 #endif
147 if (table_index < 0) 147 if (table_index < 0)
163 #if defined(DEBUG_VLC) && 0 163 #if defined(DEBUG_VLC) && 0
164 printf("i=%d n=%d code=0x%x\n", i, n, code); 164 printf("i=%d n=%d code=0x%x\n", i, n, code);
165 #endif 165 #endif
166 /* if code matches the prefix, it is in the table */ 166 /* if code matches the prefix, it is in the table */
167 n -= n_prefix; 167 n -= n_prefix;
168 if (n > 0 && (code >> n) == code_prefix) { 168 if(flags & INIT_VLC_LE)
169 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
170 else
171 code_prefix2= code >> n;
172 if (n > 0 && code_prefix2 == code_prefix) {
169 if (n <= table_nb_bits) { 173 if (n <= table_nb_bits) {
170 /* no need to add another table */ 174 /* no need to add another table */
171 j = (code << (table_nb_bits - n)) & (table_size - 1); 175 j = (code << (table_nb_bits - n)) & (table_size - 1);
172 nb = 1 << (table_nb_bits - n); 176 nb = 1 << (table_nb_bits - n);
173 for(k=0;k<nb;k++) { 177 for(k=0;k<nb;k++) {
178 if(flags & INIT_VLC_LE)
179 j = (code >> n_prefix) + (k<<n);
174 #ifdef DEBUG_VLC 180 #ifdef DEBUG_VLC
175 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", 181 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
176 j, i, n); 182 j, i, n);
177 #endif 183 #endif
178 if (table[j][1] /*bits*/ != 0) { 184 if (table[j][1] /*bits*/ != 0) {
183 table[j][0] = i; //code 189 table[j][0] = i; //code
184 j++; 190 j++;
185 } 191 }
186 } else { 192 } else {
187 n -= table_nb_bits; 193 n -= table_nb_bits;
188 j = (code >> n) & ((1 << table_nb_bits) - 1); 194 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
189 #ifdef DEBUG_VLC 195 #ifdef DEBUG_VLC
190 printf("%4x: n=%d (subtable)\n", 196 printf("%4x: n=%d (subtable)\n",
191 j, n); 197 j, n);
192 #endif 198 #endif
193 /* compute table size */ 199 /* compute table size */
209 table[i][1] = -n; //bits 215 table[i][1] = -n; //bits
210 } 216 }
211 index = build_table(vlc, n, nb_codes, 217 index = build_table(vlc, n, nb_codes,
212 bits, bits_wrap, bits_size, 218 bits, bits_wrap, bits_size,
213 codes, codes_wrap, codes_size, 219 codes, codes_wrap, codes_size,
214 (code_prefix << table_nb_bits) | i, 220 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
215 n_prefix + table_nb_bits, use_static); 221 n_prefix + table_nb_bits, flags);
216 if (index < 0) 222 if (index < 0)
217 return -1; 223 return -1;
218 /* note: realloc has been done, so reload tables */ 224 /* note: realloc has been done, so reload tables */
219 table = &vlc->table[table_index]; 225 table = &vlc->table[table_index];
220 table[i][0] = index; //code 226 table[i][0] = index; //code