Mercurial > libavcodec.hg
annotate common.c @ 234:5fc0c3af3fe4 libavcodec
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
| author | michaelni |
|---|---|
| date | Tue, 12 Feb 2002 15:43:16 +0000 |
| parents | 5b88ee1abf97 |
| children | 99a9f903f0e3 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Common bit i/o utils | |
| 3 * Copyright (c) 2000, 2001 Gerard Lantau. | |
| 4 * | |
| 5 * This program is free software; you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License as published by | |
| 7 * the Free Software Foundation; either version 2 of the License, or | |
| 8 * (at your option) any later version. | |
| 9 * | |
| 10 * This program is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 * GNU General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU General Public License | |
| 16 * along with this program; if not, write to the Free Software | |
| 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
18 * |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
19 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> |
| 0 | 20 */ |
| 64 | 21 #include "common.h" |
| 0 | 22 #include <math.h> |
| 8 | 23 |
| 0 | 24 void init_put_bits(PutBitContext *s, |
| 25 UINT8 *buffer, int buffer_size, | |
| 26 void *opaque, | |
| 27 void (*write_data)(void *, UINT8 *, int)) | |
| 28 { | |
| 29 s->buf = buffer; | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
30 s->buf_end = s->buf + buffer_size; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
31 s->data_out_size = 0; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
32 #ifdef ALT_BITSTREAM_WRITER |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
33 s->index=0; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
34 ((uint32_t*)(s->buf))[0]=0; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
35 // memset(buffer, 0, buffer_size); |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
36 if(write_data!=NULL) |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
37 { |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
38 fprintf(stderr, "write Data callback is not supported\n"); |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
39 } |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
40 #else |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
41 s->write_data = write_data; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
42 s->opaque = opaque; |
| 0 | 43 s->buf_ptr = s->buf; |
| 44 s->bit_cnt=0; | |
| 45 s->bit_buf=0; | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
46 #endif |
| 0 | 47 } |
| 48 | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
49 #ifndef ALT_BITSTREAM_WRITER |
| 0 | 50 static void flush_buffer(PutBitContext *s) |
| 51 { | |
| 52 int size; | |
| 53 if (s->write_data) { | |
| 54 size = s->buf_ptr - s->buf; | |
| 55 if (size > 0) | |
| 56 s->write_data(s->opaque, s->buf, size); | |
| 57 s->buf_ptr = s->buf; | |
| 58 s->data_out_size += size; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void put_bits(PutBitContext *s, int n, unsigned int value) | |
| 63 { | |
| 64 unsigned int bit_buf; | |
| 65 int bit_cnt; | |
| 66 | |
| 67 #ifdef STATS | |
| 68 st_out_bit_counts[st_current_index] += n; | |
| 69 #endif | |
| 70 // printf("put_bits=%d %x\n", n, value); | |
| 71 assert(n == 32 || value < (1U << n)); | |
| 72 | |
| 73 bit_buf = s->bit_buf; | |
| 74 bit_cnt = s->bit_cnt; | |
| 75 | |
| 76 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
| 77 /* XXX: optimize */ | |
| 78 if (n < (32-bit_cnt)) { | |
| 79 bit_buf |= value << (32 - n - bit_cnt); | |
| 80 bit_cnt+=n; | |
| 81 } else { | |
| 82 bit_buf |= value >> (n + bit_cnt - 32); | |
| 64 | 83 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf); |
| 0 | 84 //printf("bitbuf = %08x\n", bit_buf); |
| 85 s->buf_ptr+=4; | |
| 86 if (s->buf_ptr >= s->buf_end) | |
| 87 flush_buffer(s); | |
| 88 bit_cnt=bit_cnt + n - 32; | |
| 89 if (bit_cnt == 0) { | |
| 90 bit_buf = 0; | |
| 91 } else { | |
| 92 bit_buf = value << (32 - bit_cnt); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 s->bit_buf = bit_buf; | |
| 97 s->bit_cnt = bit_cnt; | |
| 98 } | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
99 #endif |
| 0 | 100 |
| 101 /* return the number of bits output */ | |
| 64 | 102 INT64 get_bit_count(PutBitContext *s) |
| 0 | 103 { |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
104 #ifdef ALT_BITSTREAM_WRITER |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
105 return s->data_out_size * 8 + s->index; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
106 #else |
| 64 | 107 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (INT64)s->bit_cnt; |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
108 #endif |
| 0 | 109 } |
| 110 | |
| 111 void align_put_bits(PutBitContext *s) | |
| 112 { | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
113 #ifdef ALT_BITSTREAM_WRITER |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
114 put_bits(s,( - s->index) & 7,0); |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
115 #else |
| 0 | 116 put_bits(s,(8 - s->bit_cnt) & 7,0); |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
117 #endif |
| 0 | 118 } |
| 119 | |
| 120 /* pad the end of the output stream with zeros */ | |
| 121 void flush_put_bits(PutBitContext *s) | |
| 122 { | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
123 #ifdef ALT_BITSTREAM_WRITER |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
124 align_put_bits(s); |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
125 #else |
| 0 | 126 while (s->bit_cnt > 0) { |
| 127 /* XXX: should test end of buffer */ | |
| 128 *s->buf_ptr++=s->bit_buf >> 24; | |
| 129 s->bit_buf<<=8; | |
| 130 s->bit_cnt-=8; | |
| 131 } | |
| 132 flush_buffer(s); | |
| 133 s->bit_cnt=0; | |
| 134 s->bit_buf=0; | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
135 #endif |
| 0 | 136 } |
| 137 | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
138 #ifndef ALT_BITSTREAM_WRITER |
| 24 | 139 /* for jpeg : escape 0xff with 0x00 after it */ |
| 0 | 140 void jput_bits(PutBitContext *s, int n, unsigned int value) |
| 141 { | |
| 142 unsigned int bit_buf, b; | |
| 143 int bit_cnt, i; | |
| 144 | |
| 145 assert(n == 32 || value < (1U << n)); | |
| 146 | |
| 147 bit_buf = s->bit_buf; | |
| 148 bit_cnt = s->bit_cnt; | |
| 149 | |
| 150 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
| 151 /* XXX: optimize */ | |
| 152 if (n < (32-bit_cnt)) { | |
| 153 bit_buf |= value << (32 - n - bit_cnt); | |
| 154 bit_cnt+=n; | |
| 155 } else { | |
| 156 bit_buf |= value >> (n + bit_cnt - 32); | |
| 157 /* handle escape */ | |
| 158 for(i=0;i<4;i++) { | |
| 159 b = (bit_buf >> 24); | |
| 160 *(s->buf_ptr++) = b; | |
| 161 if (b == 0xff) | |
| 162 *(s->buf_ptr++) = 0; | |
| 163 bit_buf <<= 8; | |
| 164 } | |
| 165 /* we flush the buffer sooner to handle worst case */ | |
| 166 if (s->buf_ptr >= (s->buf_end - 8)) | |
| 167 flush_buffer(s); | |
| 168 | |
| 169 bit_cnt=bit_cnt + n - 32; | |
| 170 if (bit_cnt == 0) { | |
| 171 bit_buf = 0; | |
| 172 } else { | |
| 173 bit_buf = value << (32 - bit_cnt); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 s->bit_buf = bit_buf; | |
| 178 s->bit_cnt = bit_cnt; | |
| 179 } | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
180 #endif |
| 0 | 181 |
| 182 /* pad the end of the output stream with zeros */ | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
183 #ifndef ALT_BITSTREAM_WRITER |
| 0 | 184 void jflush_put_bits(PutBitContext *s) |
| 185 { | |
| 186 unsigned int b; | |
|
219
5b88ee1abf97
(m)jpeg pad/flush with 1 instead of 0, fix by Rik Snel <rsnel@cube.dyndns.org>
arpi_esp
parents:
193
diff
changeset
|
187 s->bit_buf |= ~1U >> s->bit_cnt; /* set all the unused bits to one */ |
| 0 | 188 |
| 189 while (s->bit_cnt > 0) { | |
| 190 b = s->bit_buf >> 24; | |
| 191 *s->buf_ptr++ = b; | |
| 192 if (b == 0xff) | |
| 193 *s->buf_ptr++ = 0; | |
| 194 s->bit_buf<<=8; | |
| 195 s->bit_cnt-=8; | |
| 196 } | |
| 197 flush_buffer(s); | |
| 198 s->bit_cnt=0; | |
| 199 s->bit_buf=0; | |
| 200 } | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
201 #else |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
202 void jflush_put_bits(PutBitContext *s) |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
203 { |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
204 int num= ( - s->index) & 7; |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
205 jput_bits(s, num,0xFF>>(8-num)); |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
206 } |
|
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
207 #endif |
| 0 | 208 |
| 209 /* bit input functions */ | |
| 210 | |
| 211 void init_get_bits(GetBitContext *s, | |
| 212 UINT8 *buffer, int buffer_size) | |
| 213 { | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
214 #ifdef ALT_BITSTREAM_READER |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
215 s->index=0; |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
216 s->buffer= buffer; |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
217 #else |
| 0 | 218 s->buf = buffer; |
| 219 s->buf_ptr = buffer; | |
| 220 s->buf_end = buffer + buffer_size; | |
| 221 s->bit_cnt = 0; | |
| 222 s->bit_buf = 0; | |
| 223 while (s->buf_ptr < s->buf_end && | |
| 224 s->bit_cnt < 32) { | |
| 225 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
| 226 s->bit_cnt += 8; | |
| 227 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
228 #endif |
| 0 | 229 } |
| 230 | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
231 #ifndef ALT_BITSTREAM_READER |
| 0 | 232 /* n must be >= 1 and <= 32 */ |
|
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
233 /* also true: n > s->bit_cnt */ |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
234 unsigned int get_bits_long(GetBitContext *s, int n) |
| 0 | 235 { |
| 236 unsigned int val; | |
| 237 int bit_cnt; | |
| 238 unsigned int bit_buf; | |
| 239 | |
| 240 #ifdef STATS | |
| 241 st_bit_counts[st_current_index] += n; | |
| 242 #endif | |
| 243 | |
| 244 bit_buf = s->bit_buf; | |
|
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
245 bit_cnt = s->bit_cnt - n; |
| 0 | 246 |
|
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
247 // if (bit_cnt >= 0) { |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
248 // val = bit_buf >> (32 - n); |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
249 // bit_buf <<= n; |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
250 // } else |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
251 { |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
252 UINT8 *buf_ptr; |
| 0 | 253 val = bit_buf >> (32 - n); |
| 254 buf_ptr = s->buf_ptr; | |
| 255 buf_ptr += 4; | |
| 256 /* handle common case: we can read everything */ | |
| 257 if (buf_ptr <= s->buf_end) { | |
| 8 | 258 #if ARCH_X86 |
| 259 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
| 260 #else | |
| 261 bit_buf = (buf_ptr[-4] << 24) | | |
| 262 (buf_ptr[-3] << 16) | | |
| 0 | 263 (buf_ptr[-2] << 8) | |
| 8 | 264 (buf_ptr[-1]); |
| 265 #endif | |
| 0 | 266 } else { |
| 267 buf_ptr -= 4; | |
| 268 bit_buf = 0; | |
| 269 if (buf_ptr < s->buf_end) | |
| 270 bit_buf |= *buf_ptr++ << 24; | |
| 271 if (buf_ptr < s->buf_end) | |
| 272 bit_buf |= *buf_ptr++ << 16; | |
| 273 if (buf_ptr < s->buf_end) | |
| 274 bit_buf |= *buf_ptr++ << 8; | |
| 275 if (buf_ptr < s->buf_end) | |
| 276 bit_buf |= *buf_ptr++; | |
| 277 } | |
| 278 s->buf_ptr = buf_ptr; | |
| 279 val |= bit_buf >> (32 + bit_cnt); | |
| 280 bit_buf <<= - bit_cnt; | |
| 281 bit_cnt += 32; | |
| 282 } | |
| 283 s->bit_buf = bit_buf; | |
| 284 s->bit_cnt = bit_cnt; | |
| 285 return val; | |
| 286 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
287 #endif |
| 0 | 288 |
| 289 void align_get_bits(GetBitContext *s) | |
| 290 { | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
291 #ifdef ALT_BITSTREAM_READER |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
292 s->index= (s->index + 7) & (~7); |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
293 #else |
| 0 | 294 int n; |
| 295 n = s->bit_cnt & 7; | |
| 296 if (n > 0) { | |
| 297 get_bits(s, n); | |
| 298 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
299 #endif |
| 0 | 300 } |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
301 |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
302 #ifndef ALT_BITSTREAM_READER |
| 144 | 303 /* This function is identical to get_bits_long(), the */ |
| 304 /* only diference is that it doesn't touch the buffer */ | |
| 305 /* it is usefull to see the buffer. */ | |
| 306 | |
| 307 unsigned int show_bits_long(GetBitContext *s, int n) | |
| 308 { | |
| 309 unsigned int val; | |
| 310 int bit_cnt; | |
| 311 unsigned int bit_buf; | |
| 312 UINT8 *buf_ptr; | |
| 313 | |
| 314 bit_buf = s->bit_buf; | |
| 315 bit_cnt = s->bit_cnt - n; | |
| 316 | |
| 317 val = bit_buf >> (32 - n); | |
| 318 buf_ptr = s->buf_ptr; | |
| 319 buf_ptr += 4; | |
| 320 | |
| 321 /* handle common case: we can read everything */ | |
| 322 if (buf_ptr <= s->buf_end) { | |
| 323 #ifdef ARCH_X86 | |
| 324 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
| 325 #else | |
| 326 bit_buf = (buf_ptr[-4] << 24) | | |
| 327 (buf_ptr[-3] << 16) | | |
| 328 (buf_ptr[-2] << 8) | | |
| 329 (buf_ptr[-1]); | |
| 330 #endif | |
| 331 } else { | |
| 332 buf_ptr -= 4; | |
| 333 bit_buf = 0; | |
| 334 if (buf_ptr < s->buf_end) | |
| 335 bit_buf |= *buf_ptr++ << 24; | |
| 336 if (buf_ptr < s->buf_end) | |
| 337 bit_buf |= *buf_ptr++ << 16; | |
| 338 if (buf_ptr < s->buf_end) | |
| 339 bit_buf |= *buf_ptr++ << 8; | |
| 340 if (buf_ptr < s->buf_end) | |
| 341 bit_buf |= *buf_ptr++; | |
| 342 } | |
| 343 val |= bit_buf >> (32 + bit_cnt); | |
| 344 bit_buf <<= - bit_cnt; | |
| 345 bit_cnt += 32; | |
| 346 | |
| 347 return val; | |
| 348 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
349 #endif |
| 0 | 350 |
| 351 /* VLC decoding */ | |
| 352 | |
| 353 //#define DEBUG_VLC | |
| 354 | |
| 355 #define GET_DATA(v, table, i, wrap, size) \ | |
| 356 {\ | |
| 357 UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
| 358 switch(size) {\ | |
| 359 case 1:\ | |
| 360 v = *(UINT8 *)ptr;\ | |
| 361 break;\ | |
| 362 case 2:\ | |
| 363 v = *(UINT16 *)ptr;\ | |
| 364 break;\ | |
| 365 default:\ | |
| 366 v = *(UINT32 *)ptr;\ | |
| 367 break;\ | |
| 368 }\ | |
| 369 } | |
| 370 | |
| 371 | |
| 372 static int alloc_table(VLC *vlc, int size) | |
| 373 { | |
| 374 int index; | |
| 375 index = vlc->table_size; | |
| 376 vlc->table_size += size; | |
| 377 if (vlc->table_size > vlc->table_allocated) { | |
| 378 vlc->table_allocated += (1 << vlc->bits); | |
| 379 vlc->table_bits = realloc(vlc->table_bits, | |
| 380 sizeof(INT8) * vlc->table_allocated); | |
| 381 vlc->table_codes = realloc(vlc->table_codes, | |
| 382 sizeof(INT16) * vlc->table_allocated); | |
| 383 if (!vlc->table_bits || | |
| 384 !vlc->table_codes) | |
| 385 return -1; | |
| 386 } | |
| 387 return index; | |
| 388 } | |
| 389 | |
| 390 static int build_table(VLC *vlc, int table_nb_bits, | |
| 391 int nb_codes, | |
| 392 const void *bits, int bits_wrap, int bits_size, | |
| 393 const void *codes, int codes_wrap, int codes_size, | |
| 394 UINT32 code_prefix, int n_prefix) | |
| 395 { | |
| 396 int i, j, k, n, table_size, table_index, nb, n1, index; | |
| 397 UINT32 code; | |
| 398 INT8 *table_bits; | |
| 399 INT16 *table_codes; | |
| 400 | |
| 401 table_size = 1 << table_nb_bits; | |
| 402 table_index = alloc_table(vlc, table_size); | |
| 403 #ifdef DEBUG_VLC | |
| 404 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
| 405 table_index, table_size, code_prefix, n_prefix); | |
| 406 #endif | |
| 407 if (table_index < 0) | |
| 408 return -1; | |
| 409 table_bits = &vlc->table_bits[table_index]; | |
| 410 table_codes = &vlc->table_codes[table_index]; | |
| 411 | |
| 412 for(i=0;i<table_size;i++) { | |
| 413 table_bits[i] = 0; | |
| 414 table_codes[i] = -1; | |
| 415 } | |
| 416 | |
| 417 /* first pass: map codes and compute auxillary table sizes */ | |
| 418 for(i=0;i<nb_codes;i++) { | |
| 419 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
| 420 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
| 421 /* we accept tables with holes */ | |
| 422 if (n <= 0) | |
| 423 continue; | |
| 424 #if defined(DEBUG_VLC) && 0 | |
| 425 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
| 426 #endif | |
| 427 /* if code matches the prefix, it is in the table */ | |
| 428 n -= n_prefix; | |
| 429 if (n > 0 && (code >> n) == code_prefix) { | |
| 430 if (n <= table_nb_bits) { | |
| 431 /* no need to add another table */ | |
| 432 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
| 433 nb = 1 << (table_nb_bits - n); | |
| 434 for(k=0;k<nb;k++) { | |
| 435 #ifdef DEBUG_VLC | |
| 436 printf("%4x: code=%d n=%d\n", | |
| 437 j, i, n); | |
| 438 #endif | |
| 439 if (table_bits[j] != 0) { | |
| 440 fprintf(stderr, "incorrect codes\n"); | |
| 441 exit(1); | |
| 442 } | |
| 443 table_bits[j] = n; | |
| 444 table_codes[j] = i; | |
| 445 j++; | |
| 446 } | |
| 447 } else { | |
| 448 n -= table_nb_bits; | |
| 449 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
| 450 #ifdef DEBUG_VLC | |
| 451 printf("%4x: n=%d (subtable)\n", | |
| 452 j, n); | |
| 453 #endif | |
| 454 /* compute table size */ | |
| 455 n1 = -table_bits[j]; | |
| 456 if (n > n1) | |
| 457 n1 = n; | |
| 458 table_bits[j] = -n1; | |
| 459 } | |
| 460 } | |
| 461 } | |
| 462 | |
| 463 /* second pass : fill auxillary tables recursively */ | |
| 464 for(i=0;i<table_size;i++) { | |
| 465 n = table_bits[i]; | |
| 466 if (n < 0) { | |
| 467 n = -n; | |
| 468 if (n > table_nb_bits) { | |
| 469 n = table_nb_bits; | |
| 470 table_bits[i] = -n; | |
| 471 } | |
| 472 index = build_table(vlc, n, nb_codes, | |
| 473 bits, bits_wrap, bits_size, | |
| 474 codes, codes_wrap, codes_size, | |
| 475 (code_prefix << table_nb_bits) | i, | |
| 476 n_prefix + table_nb_bits); | |
| 477 if (index < 0) | |
| 478 return -1; | |
| 479 /* note: realloc has been done, so reload tables */ | |
| 480 table_bits = &vlc->table_bits[table_index]; | |
| 481 table_codes = &vlc->table_codes[table_index]; | |
| 482 table_codes[i] = index; | |
| 483 } | |
| 484 } | |
| 485 return table_index; | |
| 486 } | |
| 487 | |
| 488 | |
| 24 | 489 /* Build VLC decoding tables suitable for use with get_vlc(). |
| 490 | |
| 491 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
| 492 bigger it is, the faster is the decoding. But it should not be too | |
| 493 big to save memory and L1 cache. '9' is a good compromise. | |
| 494 | |
| 495 'nb_codes' : number of vlcs codes | |
| 496 | |
| 497 'bits' : table which gives the size (in bits) of each vlc code. | |
| 498 | |
| 499 'codes' : table which gives the bit pattern of of each vlc code. | |
| 500 | |
| 501 'xxx_wrap' : give the number of bytes between each entry of the | |
| 502 'bits' or 'codes' tables. | |
| 503 | |
| 504 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
| 505 or 'codes' tables. | |
| 506 | |
| 507 'wrap' and 'size' allows to use any memory configuration and types | |
| 508 (byte/word/long) to store the 'bits' and 'codes' tables. | |
| 509 */ | |
| 0 | 510 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
| 511 const void *bits, int bits_wrap, int bits_size, | |
| 512 const void *codes, int codes_wrap, int codes_size) | |
| 513 { | |
| 514 vlc->bits = nb_bits; | |
| 515 vlc->table_bits = NULL; | |
| 516 vlc->table_codes = NULL; | |
| 517 vlc->table_allocated = 0; | |
| 518 vlc->table_size = 0; | |
| 519 #ifdef DEBUG_VLC | |
| 520 printf("build table nb_codes=%d\n", nb_codes); | |
| 521 #endif | |
| 522 | |
| 523 if (build_table(vlc, nb_bits, nb_codes, | |
| 524 bits, bits_wrap, bits_size, | |
| 525 codes, codes_wrap, codes_size, | |
| 526 0, 0) < 0) { | |
| 527 if (vlc->table_bits) | |
| 528 free(vlc->table_bits); | |
| 529 if (vlc->table_codes) | |
| 530 free(vlc->table_codes); | |
| 531 return -1; | |
| 532 } | |
| 533 return 0; | |
| 534 } | |
| 535 | |
| 536 | |
| 537 void free_vlc(VLC *vlc) | |
| 538 { | |
| 539 free(vlc->table_bits); | |
| 540 free(vlc->table_codes); | |
| 541 } | |
| 542 |
