3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
37 #define INVALID_VLC 0x80000000
39 extern const uint8_t ff_golomb_vlc_len[512];
40 extern const uint8_t ff_ue_golomb_vlc_code[512];
41 extern const int8_t ff_se_golomb_vlc_code[512];
42 extern const uint8_t ff_ue_golomb_len[256];
44 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
45 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
46 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
47 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
51 * read unsigned exp golomb code.
53 static inline int get_ue_golomb(GetBitContext *gb){
59 buf=GET_CACHE(re, gb);
63 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
66 return ff_ue_golomb_vlc_code[buf];
68 log= 2*av_log2(buf) - 31;
71 LAST_SKIP_BITS(re, gb, 32 - log);
79 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
81 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
85 buf = show_bits_long(gb, 32);
86 log = 31 - av_log2(buf);
87 skip_bits_long(gb, log);
89 return get_bits_long(gb, log + 1) - 1;
93 * read unsigned exp golomb code, constraint to a max of 31.
94 * the return value is undefined if the stored value exceeds 31.
96 static inline int get_ue_golomb_31(GetBitContext *gb){
100 UPDATE_CACHE(re, gb);
101 buf=GET_CACHE(re, gb);
104 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
105 CLOSE_READER(re, gb);
107 return ff_ue_golomb_vlc_code[buf];
110 static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
115 UPDATE_CACHE(re, gb);
116 buf=GET_CACHE(re, gb);
120 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
121 CLOSE_READER(re, gb);
123 return ff_interleaved_ue_golomb_vlc_code[buf];
129 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
131 if (ff_interleaved_golomb_vlc_len[buf] != 9){
132 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
133 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
136 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
137 UPDATE_CACHE(re, gb);
138 buf = GET_CACHE(re, gb);
139 } while (HAVE_BITS_REMAINING(re, gb));
141 CLOSE_READER(re, gb);
147 * read unsigned truncated exp golomb code.
149 static inline int get_te0_golomb(GetBitContext *gb, int range){
152 if(range==1) return 0;
153 else if(range==2) return get_bits1(gb)^1;
154 else return get_ue_golomb(gb);
158 * read unsigned truncated exp golomb code.
160 static inline int get_te_golomb(GetBitContext *gb, int range){
163 if(range==2) return get_bits1(gb)^1;
164 else return get_ue_golomb(gb);
169 * read signed exp golomb code.
171 static inline int get_se_golomb(GetBitContext *gb){
176 UPDATE_CACHE(re, gb);
177 buf=GET_CACHE(re, gb);
181 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
182 CLOSE_READER(re, gb);
184 return ff_se_golomb_vlc_code[buf];
186 log= 2*av_log2(buf) - 31;
189 LAST_SKIP_BITS(re, gb, 32 - log);
190 CLOSE_READER(re, gb);
192 if(buf&1) buf= -(buf>>1);
199 static inline int svq3_get_se_golomb(GetBitContext *gb){
204 UPDATE_CACHE(re, gb);
205 buf=GET_CACHE(re, gb);
209 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
210 CLOSE_READER(re, gb);
212 return ff_interleaved_se_golomb_vlc_code[buf];
214 LAST_SKIP_BITS(re, gb, 8);
215 UPDATE_CACHE(re, gb);
216 buf |= 1 | (GET_CACHE(re, gb) >> 8);
218 if((buf & 0xAAAAAAAA) == 0)
221 for(log=31; (buf & 0x80000000) == 0; log--){
222 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
225 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
226 CLOSE_READER(re, gb);
228 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
232 static inline int dirac_get_se_golomb(GetBitContext *gb){
236 ret = svq3_get_ue_golomb(gb);
240 UPDATE_CACHE(re, gb);
241 buf = SHOW_SBITS(re, gb, 1);
242 LAST_SKIP_BITS(re, gb, 1);
243 ret = (ret ^ buf) - buf;
244 CLOSE_READER(re, gb);
251 * read unsigned golomb rice code (ffv1).
253 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
258 UPDATE_CACHE(re, gb);
259 buf=GET_CACHE(re, gb);
266 LAST_SKIP_BITS(re, gb, 32 + k - log);
267 CLOSE_READER(re, gb);
271 LAST_SKIP_BITS(re, gb, limit);
272 UPDATE_CACHE(re, gb);
274 buf = SHOW_UBITS(re, gb, esc_len);
276 LAST_SKIP_BITS(re, gb, esc_len);
277 CLOSE_READER(re, gb);
279 return buf + limit - 1;
284 * read unsigned golomb rice code (jpegls).
286 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
291 UPDATE_CACHE(re, gb);
292 buf=GET_CACHE(re, gb);
296 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
299 LAST_SKIP_BITS(re, gb, 32 + k - log);
300 CLOSE_READER(re, gb);
305 for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0 && HAVE_BITS_REMAINING(re, gb); i++) {
306 LAST_SKIP_BITS(re, gb, 1);
307 UPDATE_CACHE(re, gb);
309 SKIP_BITS(re, gb, 1);
313 buf = SHOW_UBITS(re, gb, k);
314 LAST_SKIP_BITS(re, gb, k);
319 CLOSE_READER(re, gb);
321 }else if(i == limit - 1){
322 buf = SHOW_UBITS(re, gb, esc_len);
323 LAST_SKIP_BITS(re, gb, esc_len);
324 CLOSE_READER(re, gb);
333 * read signed golomb rice code (ffv1).
335 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
336 int v= get_ur_golomb(gb, k, limit, esc_len);
339 if (v&1) return v>>1;
342 // return (v>>1) ^ -(v&1);
346 * read signed golomb rice code (flac).
348 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
349 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
350 return (v>>1) ^ -(v&1);
354 * read unsigned golomb rice code (shorten).
356 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
357 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
361 * read signed golomb rice code (shorten).
363 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
365 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
376 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
379 int show= show_bits(s, 24);
380 int pos= get_bits_count(s);
381 int i= get_ue_golomb(s);
382 int len= get_bits_count(s) - pos;
383 int bits= show>>(24-len);
385 print_bin(bits, len);
387 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
392 static inline int get_se(GetBitContext *s, const char *file, const char *func,
395 int show= show_bits(s, 24);
396 int pos= get_bits_count(s);
397 int i= get_se_golomb(s);
398 int len= get_bits_count(s) - pos;
399 int bits= show>>(24-len);
401 print_bin(bits, len);
403 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
408 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
409 int show= show_bits(s, 24);
410 int pos= get_bits_count(s);
411 int i= get_te0_golomb(s, r);
412 int len= get_bits_count(s) - pos;
413 int bits= show>>(24-len);
415 print_bin(bits, len);
417 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
422 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
423 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
424 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
425 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
430 * write unsigned exp golomb code.
432 static inline void set_ue_golomb(PutBitContext *pb, int i){
444 put_bits(pb, ff_ue_golomb_len[i], i+1);
448 put_bits(pb, 2*e+1, i+1);
453 * write truncated unsigned exp golomb code.
455 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
459 if(range==2) put_bits(pb, 1, i^1);
460 else set_ue_golomb(pb, i);
464 * write signed exp golomb code. 16 bits at most.
466 static inline void set_se_golomb(PutBitContext *pb, int i){
472 if(i<0) i^= -1; //FIXME check if gcc does the right thing
477 set_ue_golomb(pb, i);
481 * write unsigned golomb rice code (ffv1).
483 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
490 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
492 put_bits(pb, limit + esc_len, i - limit + 1);
497 * write unsigned golomb rice code (jpegls).
499 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
518 put_bits(pb, limit , 1);
519 put_bits(pb, esc_len, i - 1);
524 * write signed golomb rice code (ffv1).
526 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
532 set_ur_golomb(pb, v, k, limit, esc_len);
536 * write signed golomb rice code (flac).
538 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
544 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
547 #endif /* AVCODEC_GOLOMB_H */