3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/golomb.h
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 unsigned exp golomb code, constraint to a max of 31.
80 * the return value is undefined if the stored value exceeds 31.
82 static inline int get_ue_golomb_31(GetBitContext *gb){
87 buf=GET_CACHE(re, gb);
90 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
93 return ff_ue_golomb_vlc_code[buf];
96 static inline int svq3_get_ue_golomb(GetBitContext *gb){
100 UPDATE_CACHE(re, gb);
101 buf=GET_CACHE(re, gb);
105 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
106 CLOSE_READER(re, gb);
108 return ff_interleaved_ue_golomb_vlc_code[buf];
114 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
116 if (ff_interleaved_golomb_vlc_len[buf] != 9){
117 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
118 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
121 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
122 UPDATE_CACHE(re, gb);
123 buf = GET_CACHE(re, gb);
126 CLOSE_READER(re, gb);
132 * read unsigned truncated exp golomb code.
134 static inline int get_te0_golomb(GetBitContext *gb, int range){
137 if(range==1) return 0;
138 else if(range==2) return get_bits1(gb)^1;
139 else return get_ue_golomb(gb);
143 * read unsigned truncated exp golomb code.
145 static inline int get_te_golomb(GetBitContext *gb, int range){
148 if(range==2) return get_bits1(gb)^1;
149 else return get_ue_golomb(gb);
154 * read signed exp golomb code.
156 static inline int get_se_golomb(GetBitContext *gb){
161 UPDATE_CACHE(re, gb);
162 buf=GET_CACHE(re, gb);
166 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
167 CLOSE_READER(re, gb);
169 return ff_se_golomb_vlc_code[buf];
171 log= 2*av_log2(buf) - 31;
174 LAST_SKIP_BITS(re, gb, 32 - log);
175 CLOSE_READER(re, gb);
177 if(buf&1) buf= -(buf>>1);
184 static inline int svq3_get_se_golomb(GetBitContext *gb){
189 UPDATE_CACHE(re, gb);
190 buf=GET_CACHE(re, gb);
194 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
195 CLOSE_READER(re, gb);
197 return ff_interleaved_se_golomb_vlc_code[buf];
199 LAST_SKIP_BITS(re, gb, 8);
200 UPDATE_CACHE(re, gb);
201 buf |= 1 | (GET_CACHE(re, gb) >> 8);
203 if((buf & 0xAAAAAAAA) == 0)
206 for(log=31; (buf & 0x80000000) == 0; log--){
207 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
210 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
211 CLOSE_READER(re, gb);
213 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
217 static inline int dirac_get_se_golomb(GetBitContext *gb){
221 ret = svq3_get_ue_golomb(gb);
225 UPDATE_CACHE(re, gb);
226 buf = SHOW_SBITS(re, gb, 1);
227 LAST_SKIP_BITS(re, gb, 1);
228 ret = (ret ^ buf) - buf;
229 CLOSE_READER(re, gb);
236 * read unsigned golomb rice code (ffv1).
238 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
243 UPDATE_CACHE(re, gb);
244 buf=GET_CACHE(re, gb);
251 LAST_SKIP_BITS(re, gb, 32 + k - log);
252 CLOSE_READER(re, gb);
256 buf >>= 32 - limit - esc_len;
257 LAST_SKIP_BITS(re, gb, esc_len + limit);
258 CLOSE_READER(re, gb);
260 return buf + limit - 1;
265 * read unsigned golomb rice code (jpegls).
267 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
272 UPDATE_CACHE(re, gb);
273 buf=GET_CACHE(re, gb);
277 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
280 LAST_SKIP_BITS(re, gb, 32 + k - log);
281 CLOSE_READER(re, gb);
286 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
287 LAST_SKIP_BITS(re, gb, 1);
288 UPDATE_CACHE(re, gb);
290 SKIP_BITS(re, gb, 1);
294 buf = SHOW_UBITS(re, gb, k);
295 LAST_SKIP_BITS(re, gb, k);
300 CLOSE_READER(re, gb);
302 }else if(i == limit - 1){
303 buf = SHOW_UBITS(re, gb, esc_len);
304 LAST_SKIP_BITS(re, gb, esc_len);
305 CLOSE_READER(re, gb);
314 * read signed golomb rice code (ffv1).
316 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
317 int v= get_ur_golomb(gb, k, limit, esc_len);
320 if (v&1) return v>>1;
323 // return (v>>1) ^ -(v&1);
327 * read signed golomb rice code (flac).
329 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
330 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
331 return (v>>1) ^ -(v&1);
335 * read unsigned golomb rice code (shorten).
337 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
338 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
342 * read signed golomb rice code (shorten).
344 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
346 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
357 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
358 int show= show_bits(s, 24);
359 int pos= get_bits_count(s);
360 int i= get_ue_golomb(s);
361 int len= get_bits_count(s) - pos;
362 int bits= show>>(24-len);
364 print_bin(bits, len);
366 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
371 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
372 int show= show_bits(s, 24);
373 int pos= get_bits_count(s);
374 int i= get_se_golomb(s);
375 int len= get_bits_count(s) - pos;
376 int bits= show>>(24-len);
378 print_bin(bits, len);
380 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
385 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
386 int show= show_bits(s, 24);
387 int pos= get_bits_count(s);
388 int i= get_te0_golomb(s, r);
389 int len= get_bits_count(s) - pos;
390 int bits= show>>(24-len);
392 print_bin(bits, len);
394 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
399 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
400 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
401 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
402 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
407 * write unsigned exp golomb code.
409 static inline void set_ue_golomb(PutBitContext *pb, int i){
421 put_bits(pb, ff_ue_golomb_len[i], i+1);
425 put_bits(pb, 2*e+1, i+1);
430 * write truncated unsigned exp golomb code.
432 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
436 if(range==2) put_bits(pb, 1, i^1);
437 else set_ue_golomb(pb, i);
441 * write signed exp golomb code. 16 bits at most.
443 static inline void set_se_golomb(PutBitContext *pb, int i){
444 // if (i>32767 || i<-32767)
445 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
451 if(i<0) i^= -1; //FIXME check if gcc does the right thing
456 set_ue_golomb(pb, i);
460 * write unsigned golomb rice code (ffv1).
462 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
469 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
471 put_bits(pb, limit + esc_len, i - limit + 1);
476 * write unsigned golomb rice code (jpegls).
478 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
497 put_bits(pb, limit , 1);
498 put_bits(pb, esc_len, i - 1);
503 * write signed golomb rice code (ffv1).
505 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
511 set_ur_golomb(pb, v, k, limit, esc_len);
515 * write signed golomb rice code (flac).
517 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
523 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
526 #endif /* AVCODEC_GOLOMB_H */