annotate common.c @ 157:bc12fd7e6153 libavcodec

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