Mercurial > libavcodec.hg
annotate common.c @ 264:28c5c62b1c4c libavcodec
support decoding (with mplayer) of 3 .mp4 files from mphq
optimize h263_pred_motion()
author | michaelni |
---|---|
date | Wed, 13 Mar 2002 19:26:38 +0000 |
parents | 99a9f903f0e3 |
children | 5cb2978e701f |
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 |
111 /* bit input functions */ | |
112 | |
113 void init_get_bits(GetBitContext *s, | |
114 UINT8 *buffer, int buffer_size) | |
115 { | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
116 #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
|
117 s->index=0; |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
118 s->buffer= buffer; |
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
119 #else |
0 | 120 s->buf = buffer; |
121 s->buf_ptr = buffer; | |
122 s->buf_end = buffer + buffer_size; | |
123 s->bit_cnt = 0; | |
124 s->bit_buf = 0; | |
125 while (s->buf_ptr < s->buf_end && | |
126 s->bit_cnt < 32) { | |
127 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
128 s->bit_cnt += 8; | |
129 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
130 #endif |
0 | 131 } |
132 | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
133 #ifndef ALT_BITSTREAM_READER |
0 | 134 /* 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
|
135 /* 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
|
136 unsigned int get_bits_long(GetBitContext *s, int n) |
0 | 137 { |
138 unsigned int val; | |
139 int bit_cnt; | |
140 unsigned int bit_buf; | |
141 | |
142 #ifdef STATS | |
143 st_bit_counts[st_current_index] += n; | |
144 #endif | |
145 | |
146 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
|
147 bit_cnt = s->bit_cnt - n; |
0 | 148 |
20
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
149 // if (bit_cnt >= 0) { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
150 // val = bit_buf >> (32 - n); |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
151 // bit_buf <<= n; |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
152 // } else |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
153 { |
907b67420d84
inlineing common case of get_bits() -> gives 2speedup. more optim coming soon...
arpi_esp
parents:
8
diff
changeset
|
154 UINT8 *buf_ptr; |
0 | 155 val = bit_buf >> (32 - n); |
156 buf_ptr = s->buf_ptr; | |
157 buf_ptr += 4; | |
158 /* handle common case: we can read everything */ | |
159 if (buf_ptr <= s->buf_end) { | |
8 | 160 #if ARCH_X86 |
161 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
162 #else | |
163 bit_buf = (buf_ptr[-4] << 24) | | |
164 (buf_ptr[-3] << 16) | | |
0 | 165 (buf_ptr[-2] << 8) | |
8 | 166 (buf_ptr[-1]); |
167 #endif | |
0 | 168 } else { |
169 buf_ptr -= 4; | |
170 bit_buf = 0; | |
171 if (buf_ptr < s->buf_end) | |
172 bit_buf |= *buf_ptr++ << 24; | |
173 if (buf_ptr < s->buf_end) | |
174 bit_buf |= *buf_ptr++ << 16; | |
175 if (buf_ptr < s->buf_end) | |
176 bit_buf |= *buf_ptr++ << 8; | |
177 if (buf_ptr < s->buf_end) | |
178 bit_buf |= *buf_ptr++; | |
179 } | |
180 s->buf_ptr = buf_ptr; | |
181 val |= bit_buf >> (32 + bit_cnt); | |
182 bit_buf <<= - bit_cnt; | |
183 bit_cnt += 32; | |
184 } | |
185 s->bit_buf = bit_buf; | |
186 s->bit_cnt = bit_cnt; | |
187 return val; | |
188 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
189 #endif |
0 | 190 |
191 void align_get_bits(GetBitContext *s) | |
192 { | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
193 #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
|
194 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
|
195 #else |
0 | 196 int n; |
197 n = s->bit_cnt & 7; | |
198 if (n > 0) { | |
199 get_bits(s, n); | |
200 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
201 #endif |
0 | 202 } |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
203 |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
204 int check_marker(GetBitContext *s, char *msg) |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
205 { |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
206 int bit= get_bits1(s); |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
207 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
|
208 |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
209 return bit; |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
210 } |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
211 |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
212 #ifndef ALT_BITSTREAM_READER |
144 | 213 /* This function is identical to get_bits_long(), the */ |
214 /* only diference is that it doesn't touch the buffer */ | |
215 /* it is usefull to see the buffer. */ | |
216 | |
217 unsigned int show_bits_long(GetBitContext *s, int n) | |
218 { | |
219 unsigned int val; | |
220 int bit_cnt; | |
221 unsigned int bit_buf; | |
222 UINT8 *buf_ptr; | |
223 | |
224 bit_buf = s->bit_buf; | |
225 bit_cnt = s->bit_cnt - n; | |
226 | |
227 val = bit_buf >> (32 - n); | |
228 buf_ptr = s->buf_ptr; | |
229 buf_ptr += 4; | |
230 | |
231 /* handle common case: we can read everything */ | |
232 if (buf_ptr <= s->buf_end) { | |
233 #ifdef ARCH_X86 | |
234 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
235 #else | |
236 bit_buf = (buf_ptr[-4] << 24) | | |
237 (buf_ptr[-3] << 16) | | |
238 (buf_ptr[-2] << 8) | | |
239 (buf_ptr[-1]); | |
240 #endif | |
241 } else { | |
242 buf_ptr -= 4; | |
243 bit_buf = 0; | |
244 if (buf_ptr < s->buf_end) | |
245 bit_buf |= *buf_ptr++ << 24; | |
246 if (buf_ptr < s->buf_end) | |
247 bit_buf |= *buf_ptr++ << 16; | |
248 if (buf_ptr < s->buf_end) | |
249 bit_buf |= *buf_ptr++ << 8; | |
250 if (buf_ptr < s->buf_end) | |
251 bit_buf |= *buf_ptr++; | |
252 } | |
253 val |= bit_buf >> (32 + bit_cnt); | |
254 bit_buf <<= - bit_cnt; | |
255 bit_cnt += 32; | |
256 | |
257 return val; | |
258 } | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
259 #endif |
0 | 260 |
261 /* VLC decoding */ | |
262 | |
263 //#define DEBUG_VLC | |
264 | |
265 #define GET_DATA(v, table, i, wrap, size) \ | |
266 {\ | |
267 UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
268 switch(size) {\ | |
269 case 1:\ | |
270 v = *(UINT8 *)ptr;\ | |
271 break;\ | |
272 case 2:\ | |
273 v = *(UINT16 *)ptr;\ | |
274 break;\ | |
275 default:\ | |
276 v = *(UINT32 *)ptr;\ | |
277 break;\ | |
278 }\ | |
279 } | |
280 | |
281 | |
282 static int alloc_table(VLC *vlc, int size) | |
283 { | |
284 int index; | |
285 index = vlc->table_size; | |
286 vlc->table_size += size; | |
287 if (vlc->table_size > vlc->table_allocated) { | |
288 vlc->table_allocated += (1 << vlc->bits); | |
289 vlc->table_bits = realloc(vlc->table_bits, | |
290 sizeof(INT8) * vlc->table_allocated); | |
291 vlc->table_codes = realloc(vlc->table_codes, | |
292 sizeof(INT16) * vlc->table_allocated); | |
293 if (!vlc->table_bits || | |
294 !vlc->table_codes) | |
295 return -1; | |
296 } | |
297 return index; | |
298 } | |
299 | |
300 static int build_table(VLC *vlc, int table_nb_bits, | |
301 int nb_codes, | |
302 const void *bits, int bits_wrap, int bits_size, | |
303 const void *codes, int codes_wrap, int codes_size, | |
304 UINT32 code_prefix, int n_prefix) | |
305 { | |
306 int i, j, k, n, table_size, table_index, nb, n1, index; | |
307 UINT32 code; | |
308 INT8 *table_bits; | |
309 INT16 *table_codes; | |
310 | |
311 table_size = 1 << table_nb_bits; | |
312 table_index = alloc_table(vlc, table_size); | |
313 #ifdef DEBUG_VLC | |
314 printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
315 table_index, table_size, code_prefix, n_prefix); | |
316 #endif | |
317 if (table_index < 0) | |
318 return -1; | |
319 table_bits = &vlc->table_bits[table_index]; | |
320 table_codes = &vlc->table_codes[table_index]; | |
321 | |
322 for(i=0;i<table_size;i++) { | |
323 table_bits[i] = 0; | |
324 table_codes[i] = -1; | |
325 } | |
326 | |
327 /* first pass: map codes and compute auxillary table sizes */ | |
328 for(i=0;i<nb_codes;i++) { | |
329 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
330 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
331 /* we accept tables with holes */ | |
332 if (n <= 0) | |
333 continue; | |
334 #if defined(DEBUG_VLC) && 0 | |
335 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
336 #endif | |
337 /* if code matches the prefix, it is in the table */ | |
338 n -= n_prefix; | |
339 if (n > 0 && (code >> n) == code_prefix) { | |
340 if (n <= table_nb_bits) { | |
341 /* no need to add another table */ | |
342 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
343 nb = 1 << (table_nb_bits - n); | |
344 for(k=0;k<nb;k++) { | |
345 #ifdef DEBUG_VLC | |
346 printf("%4x: code=%d n=%d\n", | |
347 j, i, n); | |
348 #endif | |
349 if (table_bits[j] != 0) { | |
350 fprintf(stderr, "incorrect codes\n"); | |
351 exit(1); | |
352 } | |
353 table_bits[j] = n; | |
354 table_codes[j] = i; | |
355 j++; | |
356 } | |
357 } else { | |
358 n -= table_nb_bits; | |
359 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
360 #ifdef DEBUG_VLC | |
361 printf("%4x: n=%d (subtable)\n", | |
362 j, n); | |
363 #endif | |
364 /* compute table size */ | |
365 n1 = -table_bits[j]; | |
366 if (n > n1) | |
367 n1 = n; | |
368 table_bits[j] = -n1; | |
369 } | |
370 } | |
371 } | |
372 | |
373 /* second pass : fill auxillary tables recursively */ | |
374 for(i=0;i<table_size;i++) { | |
375 n = table_bits[i]; | |
376 if (n < 0) { | |
377 n = -n; | |
378 if (n > table_nb_bits) { | |
379 n = table_nb_bits; | |
380 table_bits[i] = -n; | |
381 } | |
382 index = build_table(vlc, n, nb_codes, | |
383 bits, bits_wrap, bits_size, | |
384 codes, codes_wrap, codes_size, | |
385 (code_prefix << table_nb_bits) | i, | |
386 n_prefix + table_nb_bits); | |
387 if (index < 0) | |
388 return -1; | |
389 /* note: realloc has been done, so reload tables */ | |
390 table_bits = &vlc->table_bits[table_index]; | |
391 table_codes = &vlc->table_codes[table_index]; | |
392 table_codes[i] = index; | |
393 } | |
394 } | |
395 return table_index; | |
396 } | |
397 | |
398 | |
24 | 399 /* Build VLC decoding tables suitable for use with get_vlc(). |
400 | |
401 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
402 bigger it is, the faster is the decoding. But it should not be too | |
403 big to save memory and L1 cache. '9' is a good compromise. | |
404 | |
405 'nb_codes' : number of vlcs codes | |
406 | |
407 'bits' : table which gives the size (in bits) of each vlc code. | |
408 | |
409 'codes' : table which gives the bit pattern of of each vlc code. | |
410 | |
411 'xxx_wrap' : give the number of bytes between each entry of the | |
412 'bits' or 'codes' tables. | |
413 | |
414 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
415 or 'codes' tables. | |
416 | |
417 'wrap' and 'size' allows to use any memory configuration and types | |
418 (byte/word/long) to store the 'bits' and 'codes' tables. | |
419 */ | |
0 | 420 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
421 const void *bits, int bits_wrap, int bits_size, | |
422 const void *codes, int codes_wrap, int codes_size) | |
423 { | |
424 vlc->bits = nb_bits; | |
425 vlc->table_bits = NULL; | |
426 vlc->table_codes = NULL; | |
427 vlc->table_allocated = 0; | |
428 vlc->table_size = 0; | |
429 #ifdef DEBUG_VLC | |
430 printf("build table nb_codes=%d\n", nb_codes); | |
431 #endif | |
432 | |
433 if (build_table(vlc, nb_bits, nb_codes, | |
434 bits, bits_wrap, bits_size, | |
435 codes, codes_wrap, codes_size, | |
436 0, 0) < 0) { | |
437 if (vlc->table_bits) | |
438 free(vlc->table_bits); | |
439 if (vlc->table_codes) | |
440 free(vlc->table_codes); | |
441 return -1; | |
442 } | |
443 return 0; | |
444 } | |
445 | |
446 | |
447 void free_vlc(VLC *vlc) | |
448 { | |
449 free(vlc->table_bits); | |
450 free(vlc->table_codes); | |
451 } | |
452 |