3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * exp golomb vlc stuff
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #define INVALID_VLC 0x80000000
30 extern const uint8_t ff_golomb_vlc_len[512];
31 extern const uint8_t ff_ue_golomb_vlc_code[512];
32 extern const int8_t ff_se_golomb_vlc_code[512];
33 extern const uint8_t ff_ue_golomb_len[256];
35 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
36 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
37 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
41 * read unsigned exp golomb code.
43 static inline int get_ue_golomb(GetBitContext *gb){
49 buf=GET_CACHE(re, gb);
53 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
56 return ff_ue_golomb_vlc_code[buf];
58 log= 2*av_log2(buf) - 31;
61 LAST_SKIP_BITS(re, gb, 32 - log);
68 static inline int svq3_get_ue_golomb(GetBitContext *gb){
74 buf=GET_CACHE(re, gb);
78 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
81 return ff_interleaved_ue_golomb_vlc_code[buf];
84 if((buf & 0xAAAAAAAA) == 0)
87 for(log=31; (buf & 0x80000000) == 0; log--){
88 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
91 LAST_SKIP_BITS(re, gb, 63 - 2*log);
94 return ((buf << log) >> log) - 1;
99 * read unsigned truncated exp golomb code.
101 static inline int get_te0_golomb(GetBitContext *gb, int range){
104 if(range==1) return 0;
105 else if(range==2) return get_bits1(gb)^1;
106 else return get_ue_golomb(gb);
110 * read unsigned truncated exp golomb code.
112 static inline int get_te_golomb(GetBitContext *gb, int range){
115 if(range==2) return get_bits1(gb)^1;
116 else return get_ue_golomb(gb);
121 * read signed exp golomb code.
123 static inline int get_se_golomb(GetBitContext *gb){
128 UPDATE_CACHE(re, gb);
129 buf=GET_CACHE(re, gb);
133 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
134 CLOSE_READER(re, gb);
136 return ff_se_golomb_vlc_code[buf];
138 log= 2*av_log2(buf) - 31;
141 LAST_SKIP_BITS(re, gb, 32 - log);
142 CLOSE_READER(re, gb);
144 if(buf&1) buf= -(buf>>1);
151 static inline int svq3_get_se_golomb(GetBitContext *gb){
156 UPDATE_CACHE(re, gb);
157 buf=GET_CACHE(re, gb);
161 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
162 CLOSE_READER(re, gb);
164 return ff_interleaved_se_golomb_vlc_code[buf];
167 if((buf & 0xAAAAAAAA) == 0)
170 for(log=31; (buf & 0x80000000) == 0; log--){
171 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
174 LAST_SKIP_BITS(re, gb, 63 - 2*log);
175 CLOSE_READER(re, gb);
177 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
182 * read unsigned golomb rice code (ffv1).
184 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
189 UPDATE_CACHE(re, gb);
190 buf=GET_CACHE(re, gb);
197 LAST_SKIP_BITS(re, gb, 32 + k - log);
198 CLOSE_READER(re, gb);
202 buf >>= 32 - limit - esc_len;
203 LAST_SKIP_BITS(re, gb, esc_len + limit);
204 CLOSE_READER(re, gb);
206 return buf + limit - 1;
211 * read unsigned golomb rice code (jpegls).
213 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
218 UPDATE_CACHE(re, gb);
219 buf=GET_CACHE(re, gb);
226 LAST_SKIP_BITS(re, gb, 32 + k - log);
227 CLOSE_READER(re, gb);
232 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
233 LAST_SKIP_BITS(re, gb, 1);
234 UPDATE_CACHE(re, gb);
236 SKIP_BITS(re, gb, 1);
240 buf = SHOW_UBITS(re, gb, k);
241 LAST_SKIP_BITS(re, gb, k);
246 CLOSE_READER(re, gb);
248 }else if(i == limit - 1){
249 buf = SHOW_UBITS(re, gb, esc_len);
250 LAST_SKIP_BITS(re, gb, esc_len);
251 CLOSE_READER(re, gb);
261 static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
262 int show= show_bits(s, 24);
263 int pos= get_bits_count(s);
264 int i= get_ue_golomb(s);
265 int len= get_bits_count(s) - pos;
266 int bits= show>>(24-len);
268 print_bin(bits, len);
270 printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
275 static inline int get_se(GetBitContext *s, char *file, char *func, int line){
276 int show= show_bits(s, 24);
277 int pos= get_bits_count(s);
278 int i= get_se_golomb(s);
279 int len= get_bits_count(s) - pos;
280 int bits= show>>(24-len);
282 print_bin(bits, len);
284 printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
289 static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
290 int show= show_bits(s, 24);
291 int pos= get_bits_count(s);
292 int i= get_te0_golomb(s, r);
293 int len= get_bits_count(s) - pos;
294 int bits= show>>(24-len);
296 print_bin(bits, len);
298 printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
303 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
304 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
305 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
306 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
311 * write unsigned exp golomb code.
313 static inline void set_ue_golomb(PutBitContext *pb, int i){
325 put_bits(pb, ff_ue_golomb_len[i], i+1);
329 put_bits(pb, 2*e+1, i+1);
334 * write truncated unsigned exp golomb code.
336 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
340 if(range==2) put_bits(pb, 1, i^1);
341 else set_ue_golomb(pb, i);
345 * write signed exp golomb code.
347 static inline void set_se_golomb(PutBitContext *pb, int i){
353 if(i<0) i^= -1; //FIXME check if gcc does the right thing
358 set_ue_golomb(pb, i);
362 * write unsigned golomb rice code (ffv1).
364 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
371 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
373 put_bits(pb, limit + esc_len, i - limit + 1);
378 * write unsigned golomb rice code (jpegls).
380 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
389 put_bits(pb, k, i&((1<<k)-1));
391 put_bits(pb, limit , 1);
392 put_bits(pb, esc_len, i - 1);