3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * exp golomb vlc stuff
26 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
29 #define INVALID_VLC 0x80000000
31 extern const uint8_t ff_golomb_vlc_len[512];
32 extern const uint8_t ff_ue_golomb_vlc_code[512];
33 extern const int8_t ff_se_golomb_vlc_code[512];
34 extern const uint8_t ff_ue_golomb_len[256];
36 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
37 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
38 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
42 * read unsigned exp golomb code.
44 static inline int get_ue_golomb(GetBitContext *gb){
50 buf=GET_CACHE(re, gb);
54 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
57 return ff_ue_golomb_vlc_code[buf];
59 log= 2*av_log2(buf) - 31;
62 LAST_SKIP_BITS(re, gb, 32 - log);
69 static inline int svq3_get_ue_golomb(GetBitContext *gb){
75 buf=GET_CACHE(re, gb);
79 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
82 return ff_interleaved_ue_golomb_vlc_code[buf];
84 LAST_SKIP_BITS(re, gb, 8);
86 buf |= 1 | (GET_CACHE(re, gb) >> 8);
88 if((buf & 0xAAAAAAAA) == 0)
91 for(log=31; (buf & 0x80000000) == 0; log--){
92 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
95 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
98 return ((buf << log) >> log) - 1;
103 * read unsigned truncated exp golomb code.
105 static inline int get_te0_golomb(GetBitContext *gb, int range){
108 if(range==1) return 0;
109 else if(range==2) return get_bits1(gb)^1;
110 else return get_ue_golomb(gb);
114 * read unsigned truncated exp golomb code.
116 static inline int get_te_golomb(GetBitContext *gb, int range){
119 if(range==2) return get_bits1(gb)^1;
120 else return get_ue_golomb(gb);
125 * read signed exp golomb code.
127 static inline int get_se_golomb(GetBitContext *gb){
132 UPDATE_CACHE(re, gb);
133 buf=GET_CACHE(re, gb);
137 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
138 CLOSE_READER(re, gb);
140 return ff_se_golomb_vlc_code[buf];
142 log= 2*av_log2(buf) - 31;
145 LAST_SKIP_BITS(re, gb, 32 - log);
146 CLOSE_READER(re, gb);
148 if(buf&1) buf= -(buf>>1);
155 static inline int svq3_get_se_golomb(GetBitContext *gb){
160 UPDATE_CACHE(re, gb);
161 buf=GET_CACHE(re, gb);
165 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
166 CLOSE_READER(re, gb);
168 return ff_interleaved_se_golomb_vlc_code[buf];
171 if((buf & 0xAAAAAAAA) == 0)
174 for(log=31; (buf & 0x80000000) == 0; log--){
175 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
178 LAST_SKIP_BITS(re, gb, 63 - 2*log);
179 CLOSE_READER(re, gb);
181 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
186 * read unsigned golomb rice code (ffv1).
188 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
193 UPDATE_CACHE(re, gb);
194 buf=GET_CACHE(re, gb);
201 LAST_SKIP_BITS(re, gb, 32 + k - log);
202 CLOSE_READER(re, gb);
206 buf >>= 32 - limit - esc_len;
207 LAST_SKIP_BITS(re, gb, esc_len + limit);
208 CLOSE_READER(re, gb);
210 return buf + limit - 1;
215 * read unsigned golomb rice code (jpegls).
217 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
222 UPDATE_CACHE(re, gb);
223 buf=GET_CACHE(re, gb);
230 LAST_SKIP_BITS(re, gb, 32 + k - log);
231 CLOSE_READER(re, gb);
236 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
237 LAST_SKIP_BITS(re, gb, 1);
238 UPDATE_CACHE(re, gb);
240 SKIP_BITS(re, gb, 1);
244 buf = SHOW_UBITS(re, gb, k);
245 LAST_SKIP_BITS(re, gb, k);
250 CLOSE_READER(re, gb);
252 }else if(i == limit - 1){
253 buf = SHOW_UBITS(re, gb, esc_len);
254 LAST_SKIP_BITS(re, gb, esc_len);
255 CLOSE_READER(re, gb);
264 * read signed golomb rice code (ffv1).
266 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
267 int v= get_ur_golomb(gb, k, limit, esc_len);
270 if (v&1) return v>>1;
273 // return (v>>1) ^ -(v&1);
277 * read signed golomb rice code (flac).
279 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
280 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
281 return (v>>1) ^ -(v&1);
286 static inline int get_ue(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_ue_golomb(s);
290 int len= get_bits_count(s) - pos;
291 int bits= show>>(24-len);
293 print_bin(bits, len);
295 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
300 static inline int get_se(GetBitContext *s, char *file, char *func, int line){
301 int show= show_bits(s, 24);
302 int pos= get_bits_count(s);
303 int i= get_se_golomb(s);
304 int len= get_bits_count(s) - pos;
305 int bits= show>>(24-len);
307 print_bin(bits, len);
309 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
314 static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
315 int show= show_bits(s, 24);
316 int pos= get_bits_count(s);
317 int i= get_te0_golomb(s, r);
318 int len= get_bits_count(s) - pos;
319 int bits= show>>(24-len);
321 print_bin(bits, len);
323 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
328 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
329 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
330 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
331 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
336 * write unsigned exp golomb code.
338 static inline void set_ue_golomb(PutBitContext *pb, int i){
350 put_bits(pb, ff_ue_golomb_len[i], i+1);
354 put_bits(pb, 2*e+1, i+1);
359 * write truncated unsigned exp golomb code.
361 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
365 if(range==2) put_bits(pb, 1, i^1);
366 else set_ue_golomb(pb, i);
370 * write signed exp golomb code.
372 static inline void set_se_golomb(PutBitContext *pb, int i){
378 if(i<0) i^= -1; //FIXME check if gcc does the right thing
383 set_ue_golomb(pb, i);
387 * write unsigned golomb rice code (ffv1).
389 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
396 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
398 put_bits(pb, limit + esc_len, i - limit + 1);
403 * write unsigned golomb rice code (jpegls).
405 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
414 put_bits(pb, k, i&((1<<k)-1));
416 put_bits(pb, limit , 1);
417 put_bits(pb, esc_len, i - 1);
422 * write signed golomb rice code (ffv1).
424 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
430 set_ur_golomb(pb, v, k, limit, esc_len);
434 * write signed golomb rice code (flac).
436 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
442 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);