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];
83 LAST_SKIP_BITS(re, gb, 8);
85 buf |= 1 | (GET_CACHE(re, gb) >> 8);
87 if((buf & 0xAAAAAAAA) == 0)
90 for(log=31; (buf & 0x80000000) == 0; log--){
91 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
94 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
97 return ((buf << log) >> log) - 1;
102 * read unsigned truncated exp golomb code.
104 static inline int get_te0_golomb(GetBitContext *gb, int range){
107 if(range==1) return 0;
108 else if(range==2) return get_bits1(gb)^1;
109 else return get_ue_golomb(gb);
113 * read unsigned truncated exp golomb code.
115 static inline int get_te_golomb(GetBitContext *gb, int range){
118 if(range==2) return get_bits1(gb)^1;
119 else return get_ue_golomb(gb);
124 * read signed exp golomb code.
126 static inline int get_se_golomb(GetBitContext *gb){
131 UPDATE_CACHE(re, gb);
132 buf=GET_CACHE(re, gb);
136 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
137 CLOSE_READER(re, gb);
139 return ff_se_golomb_vlc_code[buf];
141 log= 2*av_log2(buf) - 31;
144 LAST_SKIP_BITS(re, gb, 32 - log);
145 CLOSE_READER(re, gb);
147 if(buf&1) buf= -(buf>>1);
154 static inline int svq3_get_se_golomb(GetBitContext *gb){
159 UPDATE_CACHE(re, gb);
160 buf=GET_CACHE(re, gb);
164 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
165 CLOSE_READER(re, gb);
167 return ff_interleaved_se_golomb_vlc_code[buf];
170 if((buf & 0xAAAAAAAA) == 0)
173 for(log=31; (buf & 0x80000000) == 0; log--){
174 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
177 LAST_SKIP_BITS(re, gb, 63 - 2*log);
178 CLOSE_READER(re, gb);
180 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
185 * read unsigned golomb rice code (ffv1).
187 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
192 UPDATE_CACHE(re, gb);
193 buf=GET_CACHE(re, gb);
200 LAST_SKIP_BITS(re, gb, 32 + k - log);
201 CLOSE_READER(re, gb);
205 buf >>= 32 - limit - esc_len;
206 LAST_SKIP_BITS(re, gb, esc_len + limit);
207 CLOSE_READER(re, gb);
209 return buf + limit - 1;
214 * read unsigned golomb rice code (jpegls).
216 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
221 UPDATE_CACHE(re, gb);
222 buf=GET_CACHE(re, gb);
229 LAST_SKIP_BITS(re, gb, 32 + k - log);
230 CLOSE_READER(re, gb);
235 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
236 LAST_SKIP_BITS(re, gb, 1);
237 UPDATE_CACHE(re, gb);
239 SKIP_BITS(re, gb, 1);
243 buf = SHOW_UBITS(re, gb, k);
244 LAST_SKIP_BITS(re, gb, k);
249 CLOSE_READER(re, gb);
251 }else if(i == limit - 1){
252 buf = SHOW_UBITS(re, gb, esc_len);
253 LAST_SKIP_BITS(re, gb, esc_len);
254 CLOSE_READER(re, gb);
263 * read unsigned golomb rice code (flac).
265 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
266 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
267 return (v>>1) ^ -(v&1);
272 static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
273 int show= show_bits(s, 24);
274 int pos= get_bits_count(s);
275 int i= get_ue_golomb(s);
276 int len= get_bits_count(s) - pos;
277 int bits= show>>(24-len);
279 print_bin(bits, len);
281 printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
286 static inline int get_se(GetBitContext *s, char *file, char *func, int line){
287 int show= show_bits(s, 24);
288 int pos= get_bits_count(s);
289 int i= get_se_golomb(s);
290 int len= get_bits_count(s) - pos;
291 int bits= show>>(24-len);
293 print_bin(bits, len);
295 printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
300 static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
301 int show= show_bits(s, 24);
302 int pos= get_bits_count(s);
303 int i= get_te0_golomb(s, r);
304 int len= get_bits_count(s) - pos;
305 int bits= show>>(24-len);
307 print_bin(bits, len);
309 printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
314 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
315 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
316 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
317 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
322 * write unsigned exp golomb code.
324 static inline void set_ue_golomb(PutBitContext *pb, int i){
336 put_bits(pb, ff_ue_golomb_len[i], i+1);
340 put_bits(pb, 2*e+1, i+1);
345 * write truncated unsigned exp golomb code.
347 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
351 if(range==2) put_bits(pb, 1, i^1);
352 else set_ue_golomb(pb, i);
356 * write signed exp golomb code.
358 static inline void set_se_golomb(PutBitContext *pb, int i){
364 if(i<0) i^= -1; //FIXME check if gcc does the right thing
369 set_ue_golomb(pb, i);
373 * write unsigned golomb rice code (ffv1).
375 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
382 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
384 put_bits(pb, limit + esc_len, i - limit + 1);
389 * write unsigned golomb rice code (jpegls).
391 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
400 put_bits(pb, k, i&((1<<k)-1));
402 put_bits(pb, limit , 1);
403 put_bits(pb, esc_len, i - 1);