Mercurial > libavcodec.hg
annotate common.c @ 382:b1663b0ffbbc libavcodec
* first shot for the new avcodec API
- comments, critics, improvements on the ffmpeg list are welcomed
| author | kabi |
|---|---|
| date | Tue, 14 May 2002 14:17:11 +0000 |
| parents | 2066dc543be4 |
| children | fce0a2520551 |
| 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 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
|
33 { |
|
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 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
|
35 } |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
36 #ifdef ALT_BITSTREAM_WRITER |
|
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
37 s->index=0; |
|
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
38 ((uint32_t*)(s->buf))[0]=0; |
|
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
39 // memset(buffer, 0, buffer_size); |
|
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
|
40 #else |
| 0 | 41 s->buf_ptr = s->buf; |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
42 s->bit_left=32; |
| 0 | 43 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
|
44 #endif |
| 0 | 45 } |
| 46 | |
| 47 /* return the number of bits output */ | |
| 64 | 48 INT64 get_bit_count(PutBitContext *s) |
| 0 | 49 { |
|
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
|
50 #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
|
51 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
|
52 #else |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
53 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left; |
|
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
|
54 #endif |
| 0 | 55 } |
| 56 | |
| 57 void align_put_bits(PutBitContext *s) | |
| 58 { | |
|
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
|
59 #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
|
60 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
|
61 #else |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
62 put_bits(s,s->bit_left & 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
|
63 #endif |
| 0 | 64 } |
| 65 | |
| 66 /* pad the end of the output stream with zeros */ | |
| 67 void flush_put_bits(PutBitContext *s) | |
| 68 { | |
|
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
|
69 #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
|
70 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
|
71 #else |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
72 s->bit_buf<<= s->bit_left; |
|
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
73 while (s->bit_left < 32) { |
| 0 | 74 /* XXX: should test end of buffer */ |
| 75 *s->buf_ptr++=s->bit_buf >> 24; | |
| 76 s->bit_buf<<=8; | |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
77 s->bit_left+=8; |
| 0 | 78 } |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
79 s->bit_left=32; |
| 0 | 80 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
|
81 #endif |
| 0 | 82 } |
| 83 | |
| 84 /* 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
|
85 #ifndef ALT_BITSTREAM_WRITER |
| 0 | 86 void jflush_put_bits(PutBitContext *s) |
| 87 { | |
| 88 unsigned int b; | |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
89 s->bit_buf<<= s->bit_left; |
|
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
90 s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */ |
| 0 | 91 |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
92 while (s->bit_left < 32) { |
| 0 | 93 b = s->bit_buf >> 24; |
| 94 *s->buf_ptr++ = b; | |
| 95 if (b == 0xff) | |
| 96 *s->buf_ptr++ = 0; | |
| 97 s->bit_buf<<=8; | |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
98 s->bit_left+=8; |
| 0 | 99 } |
|
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
100 s->bit_left=32; |
| 0 | 101 s->bit_buf=0; |
| 102 } | |
|
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
|
103 #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
|
104 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
|
105 { |
|
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 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
|
107 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
|
108 } |
|
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
|
109 #endif |
| 0 | 110 |
| 358 | 111 void put_string(PutBitContext * pbc, char *s) |
| 112 { | |
| 113 while(*s){ | |
| 114 put_bits(pbc, 8, *s); | |
| 115 s++; | |
| 116 } | |
| 117 put_bits(pbc, 8, 0); | |
| 118 } | |
| 119 | |
| 0 | 120 /* bit input functions */ |
| 121 | |
| 122 void init_get_bits(GetBitContext *s, | |
| 123 UINT8 *buffer, int buffer_size) | |
| 124 { | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
125 #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
|
126 s->index=0; |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
127 s->buffer= buffer; |
|
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
128 #else |
| 0 | 129 s->buf = buffer; |
| 130 s->buf_ptr = buffer; | |
| 131 s->buf_end = buffer + buffer_size; | |
| 132 s->bit_cnt = 0; | |
| 133 s->bit_buf = 0; | |
| 134 while (s->buf_ptr < s->buf_end && | |
| 135 s->bit_cnt < 32) { | |
| 136 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
| 137 s->bit_cnt += 8; | |
| 138 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
139 #endif |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
264
diff
changeset
|
140 s->size= buffer_size; |
| 0 | 141 } |
| 142 | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
143 #ifndef ALT_BITSTREAM_READER |
| 0 | 144 /* 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
|
145 /* 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
|
146 unsigned int get_bits_long(GetBitContext *s, int n) |
| 0 | 147 { |
| 148 unsigned int val; | |
| 149 int bit_cnt; | |
| 150 unsigned int bit_buf; | |
| 151 | |
| 152 #ifdef STATS | |
| 153 st_bit_counts[st_current_index] += n; | |
| 154 #endif | |
| 155 | |
| 156 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
|
157 bit_cnt = s->bit_cnt - n; |
| 0 | 158 |
|
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
159 // if (bit_cnt >= 0) { |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
160 // val = bit_buf >> (32 - n); |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
161 // bit_buf <<= n; |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
162 // } else |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
163 { |
|
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
164 UINT8 *buf_ptr; |
| 0 | 165 val = bit_buf >> (32 - n); |
| 166 buf_ptr = s->buf_ptr; | |
| 167 buf_ptr += 4; | |
| 168 /* handle common case: we can read everything */ | |
| 169 if (buf_ptr <= s->buf_end) { | |
| 8 | 170 #if ARCH_X86 |
| 171 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
| 172 #else | |
| 173 bit_buf = (buf_ptr[-4] << 24) | | |
| 174 (buf_ptr[-3] << 16) | | |
| 0 | 175 (buf_ptr[-2] << 8) | |
| 8 | 176 (buf_ptr[-1]); |
| 177 #endif | |
| 0 | 178 } else { |
| 179 buf_ptr -= 4; | |
| 180 bit_buf = 0; | |
| 181 if (buf_ptr < s->buf_end) | |
| 182 bit_buf |= *buf_ptr++ << 24; | |
| 183 if (buf_ptr < s->buf_end) | |
| 184 bit_buf |= *buf_ptr++ << 16; | |
| 185 if (buf_ptr < s->buf_end) | |
| 186 bit_buf |= *buf_ptr++ << 8; | |
| 187 if (buf_ptr < s->buf_end) | |
| 188 bit_buf |= *buf_ptr++; | |
| 189 } | |
| 190 s->buf_ptr = buf_ptr; | |
| 191 val |= bit_buf >> (32 + bit_cnt); | |
| 192 bit_buf <<= - bit_cnt; | |
| 193 bit_cnt += 32; | |
| 194 } | |
| 195 s->bit_buf = bit_buf; | |
| 196 s->bit_cnt = bit_cnt; | |
| 197 return val; | |
| 198 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
199 #endif |
| 0 | 200 |
| 201 void align_get_bits(GetBitContext *s) | |
| 202 { | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
203 #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
|
204 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
|
205 #else |
| 0 | 206 int n; |
| 207 n = s->bit_cnt & 7; | |
| 208 if (n > 0) { | |
| 209 get_bits(s, n); | |
| 210 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
211 #endif |
| 0 | 212 } |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
213 |
|
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
214 int check_marker(GetBitContext *s, char *msg) |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
215 { |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
216 int bit= get_bits1(s); |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
217 if(!bit) printf("Marker bit missing %s\n", msg); |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
218 |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
219 return bit; |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
220 } |
|
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
221 |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
222 #ifndef ALT_BITSTREAM_READER |
| 144 | 223 /* This function is identical to get_bits_long(), the */ |
| 224 /* only diference is that it doesn't touch the buffer */ | |
| 225 /* it is usefull to see the buffer. */ | |
| 226 | |
| 227 unsigned int show_bits_long(GetBitContext *s, int n) | |
| 228 { | |
| 229 unsigned int val; | |
| 230 int bit_cnt; | |
| 231 unsigned int bit_buf; | |
| 232 UINT8 *buf_ptr; | |
| 233 | |
| 234 bit_buf = s->bit_buf; | |
| 235 bit_cnt = s->bit_cnt - n; | |
| 236 | |
| 237 val = bit_buf >> (32 - n); | |
| 238 buf_ptr = s->buf_ptr; | |
| 239 buf_ptr += 4; | |
| 240 | |
| 241 /* handle common case: we can read everything */ | |
| 242 if (buf_ptr <= s->buf_end) { | |
| 243 #ifdef ARCH_X86 | |
| 244 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
| 245 #else | |
| 246 bit_buf = (buf_ptr[-4] << 24) | | |
| 247 (buf_ptr[-3] << 16) | | |
| 248 (buf_ptr[-2] << 8) | | |
| 249 (buf_ptr[-1]); | |
| 250 #endif | |
| 251 } else { | |
| 252 buf_ptr -= 4; | |
| 253 bit_buf = 0; | |
| 254 if (buf_ptr < s->buf_end) | |
| 255 bit_buf |= *buf_ptr++ << 24; | |
| 256 if (buf_ptr < s->buf_end) | |
| 257 bit_buf |= *buf_ptr++ << 16; | |
| 258 if (buf_ptr < s->buf_end) | |
| 259 bit_buf |= *buf_ptr++ << 8; | |
| 260 if (buf_ptr < s->buf_end) | |
| 261 bit_buf |= *buf_ptr++; | |
| 262 } | |
| 263 val |= bit_buf >> (32 + bit_cnt); | |
| 264 bit_buf <<= - bit_cnt; | |
| 265 bit_cnt += 32; | |
| 266 | |
| 267 return val; | |
| 268 } | |
|
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
269 #endif |
| 0 | 270 |
| 271 /* VLC decoding */ | |
| 272 | |
| 273 //#define DEBUG_VLC | |
| 274 | |
| 275 #define GET_DATA(v, table, i, wrap, size) \ | |
| 276 {\ | |
| 277 UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
| 278 switch(size) {\ | |
| 279 case 1:\ | |
| 280 v = *(UINT8 *)ptr;\ | |
| 281 break;\ | |
| 282 case 2:\ | |
| 283 v = *(UINT16 *)ptr;\ | |
| 284 break;\ | |
| 285 default:\ | |
| 286 v = *(UINT32 *)ptr;\ | |
| 287 break;\ | |
| 288 }\ | |
| 289 } | |
| 290 | |
| 291 | |
| 292 static int alloc_table(VLC *vlc, int size) | |
| 293 { | |
| 294 int index; | |
| 295 index = vlc->table_size; | |
| 296 vlc->table_size += size; | |
| 297 if (vlc->table_size > vlc->table_allocated) { | |
| 298 vlc->table_allocated += (1 << vlc->bits); | |
| 299 vlc->table_bits = realloc(vlc->table_bits, | |
| 300 sizeof(INT8) * vlc->table_allocated); | |
| 301 vlc->table_codes = realloc(vlc->table_codes, | |
| 302 sizeof(INT16) * vlc->table_allocated); | |
| 303 if (!vlc->table_bits || | |
| 304 !vlc->table_codes) | |
| 305 return -1; | |
| 306 } | |
| 307 return index; | |
| 308 } | |
| 309 | |
| 310 static int build_table(VLC *vlc, int table_nb_bits, | |
| 311 int nb_codes, | |
| 312 const void *bits, int bits_wrap, int bits_size, | |
| 313 const void *codes, int codes_wrap, int codes_size, | |
| 314 UINT32 code_prefix, int n_prefix) | |
| 315 { | |
| 316 int i, j, k, n, table_size, table_index, nb, n1, index; | |
| 317 UINT32 code; | |
| 318 INT8 *table_bits; | |
| 319 INT16 *table_codes; | |
| 320 | |
| 321 table_size = 1 << table_nb_bits; | |
| 322 table_index = alloc_table(vlc, table_size); | |
| 323 #ifdef DEBUG_VLC | |
| 324 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
| 325 table_index, table_size, code_prefix, n_prefix); | |
| 326 #endif | |
| 327 if (table_index < 0) | |
| 328 return -1; | |
| 329 table_bits = &vlc->table_bits[table_index]; | |
| 330 table_codes = &vlc->table_codes[table_index]; | |
| 331 | |
| 332 for(i=0;i<table_size;i++) { | |
| 333 table_bits[i] = 0; | |
| 334 table_codes[i] = -1; | |
| 335 } | |
| 336 | |
| 337 /* first pass: map codes and compute auxillary table sizes */ | |
| 338 for(i=0;i<nb_codes;i++) { | |
| 339 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
| 340 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
| 341 /* we accept tables with holes */ | |
| 342 if (n <= 0) | |
| 343 continue; | |
| 344 #if defined(DEBUG_VLC) && 0 | |
| 345 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
| 346 #endif | |
| 347 /* if code matches the prefix, it is in the table */ | |
| 348 n -= n_prefix; | |
| 349 if (n > 0 && (code >> n) == code_prefix) { | |
| 350 if (n <= table_nb_bits) { | |
| 351 /* no need to add another table */ | |
| 352 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
| 353 nb = 1 << (table_nb_bits - n); | |
| 354 for(k=0;k<nb;k++) { | |
| 355 #ifdef DEBUG_VLC | |
| 356 printf("%4x: code=%d n=%d\n", | |
| 357 j, i, n); | |
| 358 #endif | |
| 359 if (table_bits[j] != 0) { | |
| 360 fprintf(stderr, "incorrect codes\n"); | |
| 361 exit(1); | |
| 362 } | |
| 363 table_bits[j] = n; | |
| 364 table_codes[j] = i; | |
| 365 j++; | |
| 366 } | |
| 367 } else { | |
| 368 n -= table_nb_bits; | |
| 369 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
| 370 #ifdef DEBUG_VLC | |
| 371 printf("%4x: n=%d (subtable)\n", | |
| 372 j, n); | |
| 373 #endif | |
| 374 /* compute table size */ | |
| 375 n1 = -table_bits[j]; | |
| 376 if (n > n1) | |
| 377 n1 = n; | |
| 378 table_bits[j] = -n1; | |
| 379 } | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 /* second pass : fill auxillary tables recursively */ | |
| 384 for(i=0;i<table_size;i++) { | |
| 385 n = table_bits[i]; | |
| 386 if (n < 0) { | |
| 387 n = -n; | |
| 388 if (n > table_nb_bits) { | |
| 389 n = table_nb_bits; | |
| 390 table_bits[i] = -n; | |
| 391 } | |
| 392 index = build_table(vlc, n, nb_codes, | |
| 393 bits, bits_wrap, bits_size, | |
| 394 codes, codes_wrap, codes_size, | |
| 395 (code_prefix << table_nb_bits) | i, | |
| 396 n_prefix + table_nb_bits); | |
| 397 if (index < 0) | |
| 398 return -1; | |
| 399 /* note: realloc has been done, so reload tables */ | |
| 400 table_bits = &vlc->table_bits[table_index]; | |
| 401 table_codes = &vlc->table_codes[table_index]; | |
| 402 table_codes[i] = index; | |
| 403 } | |
| 404 } | |
| 405 return table_index; | |
| 406 } | |
| 407 | |
| 408 | |
| 24 | 409 /* Build VLC decoding tables suitable for use with get_vlc(). |
| 410 | |
| 411 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
| 412 bigger it is, the faster is the decoding. But it should not be too | |
| 413 big to save memory and L1 cache. '9' is a good compromise. | |
| 414 | |
| 415 'nb_codes' : number of vlcs codes | |
| 416 | |
| 417 'bits' : table which gives the size (in bits) of each vlc code. | |
| 418 | |
| 419 'codes' : table which gives the bit pattern of of each vlc code. | |
| 420 | |
| 421 'xxx_wrap' : give the number of bytes between each entry of the | |
| 422 'bits' or 'codes' tables. | |
| 423 | |
| 424 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
| 425 or 'codes' tables. | |
| 426 | |
| 427 'wrap' and 'size' allows to use any memory configuration and types | |
| 428 (byte/word/long) to store the 'bits' and 'codes' tables. | |
| 429 */ | |
| 0 | 430 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
| 431 const void *bits, int bits_wrap, int bits_size, | |
| 432 const void *codes, int codes_wrap, int codes_size) | |
| 433 { | |
| 434 vlc->bits = nb_bits; | |
| 435 vlc->table_bits = NULL; | |
| 436 vlc->table_codes = NULL; | |
| 437 vlc->table_allocated = 0; | |
| 438 vlc->table_size = 0; | |
| 439 #ifdef DEBUG_VLC | |
| 440 printf("build table nb_codes=%d\n", nb_codes); | |
| 441 #endif | |
| 442 | |
| 443 if (build_table(vlc, nb_bits, nb_codes, | |
| 444 bits, bits_wrap, bits_size, | |
| 445 codes, codes_wrap, codes_size, | |
| 446 0, 0) < 0) { | |
| 447 if (vlc->table_bits) | |
| 448 free(vlc->table_bits); | |
| 449 if (vlc->table_codes) | |
| 450 free(vlc->table_codes); | |
| 451 return -1; | |
| 452 } | |
| 453 return 0; | |
| 454 } | |
| 455 | |
| 456 | |
| 457 void free_vlc(VLC *vlc) | |
| 458 { | |
| 459 free(vlc->table_bits); | |
| 460 free(vlc->table_codes); | |
| 461 } | |
| 462 | |
| 324 | 463 int ff_gcd(int a, int b){ |
| 464 if(b) return ff_gcd(b, a%b); | |
| 465 else return a; | |
| 466 } |
