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
34 #include "bitstream.h"
36 #define INVALID_VLC 0x80000000
38 extern const uint8_t ff_golomb_vlc_len[512];
39 extern const uint8_t ff_ue_golomb_vlc_code[512];
40 extern const int8_t ff_se_golomb_vlc_code[512];
41 extern const uint8_t ff_ue_golomb_len[256];
43 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
44 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
45 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
46 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
50 * read unsigned exp golomb code.
52 static inline int get_ue_golomb(GetBitContext *gb){
58 buf=GET_CACHE(re, gb);
62 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
65 return ff_ue_golomb_vlc_code[buf];
67 log= 2*av_log2(buf) - 31;
70 LAST_SKIP_BITS(re, gb, 32 - log);
78 * read unsigned exp golomb code, constraint to a max of 31.
79 * the return value is undefined if the stored value exceeds 31.
81 static inline int get_ue_golomb_31(GetBitContext *gb){
86 buf=GET_CACHE(re, gb);
89 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
92 return ff_ue_golomb_vlc_code[buf];
95 static inline int svq3_get_ue_golomb(GetBitContext *gb){
100 buf=GET_CACHE(re, gb);
104 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
105 CLOSE_READER(re, gb);
107 return ff_interleaved_ue_golomb_vlc_code[buf];
113 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
115 if (ff_interleaved_golomb_vlc_len[buf] != 9){
116 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
117 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
120 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
121 UPDATE_CACHE(re, gb);
122 buf = GET_CACHE(re, gb);
125 CLOSE_READER(re, gb);
131 * read unsigned truncated exp golomb code.
133 static inline int get_te0_golomb(GetBitContext *gb, int range){
136 if(range==1) return 0;
137 else if(range==2) return get_bits1(gb)^1;
138 else return get_ue_golomb(gb);
142 * read unsigned truncated exp golomb code.
144 static inline int get_te_golomb(GetBitContext *gb, int range){
147 if(range==2) return get_bits1(gb)^1;
148 else return get_ue_golomb(gb);
153 * read signed exp golomb code.
155 static inline int get_se_golomb(GetBitContext *gb){
160 UPDATE_CACHE(re, gb);
161 buf=GET_CACHE(re, gb);
165 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
166 CLOSE_READER(re, gb);
168 return ff_se_golomb_vlc_code[buf];
170 log= 2*av_log2(buf) - 31;
173 LAST_SKIP_BITS(re, gb, 32 - log);
174 CLOSE_READER(re, gb);
176 if(buf&1) buf= -(buf>>1);
183 static inline int svq3_get_se_golomb(GetBitContext *gb){
188 UPDATE_CACHE(re, gb);
189 buf=GET_CACHE(re, gb);
193 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
194 CLOSE_READER(re, gb);
196 return ff_interleaved_se_golomb_vlc_code[buf];
198 LAST_SKIP_BITS(re, gb, 8);
199 UPDATE_CACHE(re, gb);
200 buf |= 1 | (GET_CACHE(re, gb) >> 8);
202 if((buf & 0xAAAAAAAA) == 0)
205 for(log=31; (buf & 0x80000000) == 0; log--){
206 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
209 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
210 CLOSE_READER(re, gb);
212 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
216 static inline int dirac_get_se_golomb(GetBitContext *gb){
220 ret = svq3_get_ue_golomb(gb);
224 UPDATE_CACHE(re, gb);
225 buf = SHOW_SBITS(re, gb, 1);
226 LAST_SKIP_BITS(re, gb, 1);
227 ret = (ret ^ buf) - buf;
228 CLOSE_READER(re, gb);
235 * read unsigned golomb rice code (ffv1).
237 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
242 UPDATE_CACHE(re, gb);
243 buf=GET_CACHE(re, gb);
250 LAST_SKIP_BITS(re, gb, 32 + k - log);
251 CLOSE_READER(re, gb);
255 buf >>= 32 - limit - esc_len;
256 LAST_SKIP_BITS(re, gb, esc_len + limit);
257 CLOSE_READER(re, gb);
259 return buf + limit - 1;
264 * read unsigned golomb rice code (jpegls).
266 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
271 UPDATE_CACHE(re, gb);
272 buf=GET_CACHE(re, gb);
276 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
279 LAST_SKIP_BITS(re, gb, 32 + k - log);
280 CLOSE_READER(re, gb);
285 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
286 LAST_SKIP_BITS(re, gb, 1);
287 UPDATE_CACHE(re, gb);
289 SKIP_BITS(re, gb, 1);
293 buf = SHOW_UBITS(re, gb, k);
294 LAST_SKIP_BITS(re, gb, k);
299 CLOSE_READER(re, gb);
301 }else if(i == limit - 1){
302 buf = SHOW_UBITS(re, gb, esc_len);
303 LAST_SKIP_BITS(re, gb, esc_len);
304 CLOSE_READER(re, gb);
313 * read signed golomb rice code (ffv1).
315 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
316 int v= get_ur_golomb(gb, k, limit, esc_len);
319 if (v&1) return v>>1;
322 // return (v>>1) ^ -(v&1);
326 * read signed golomb rice code (flac).
328 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
329 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
330 return (v>>1) ^ -(v&1);
334 * read unsigned golomb rice code (shorten).
336 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
337 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
341 * read signed golomb rice code (shorten).
343 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
345 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
356 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
357 int show= show_bits(s, 24);
358 int pos= get_bits_count(s);
359 int i= get_ue_golomb(s);
360 int len= get_bits_count(s) - pos;
361 int bits= show>>(24-len);
363 print_bin(bits, len);
365 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
370 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
371 int show= show_bits(s, 24);
372 int pos= get_bits_count(s);
373 int i= get_se_golomb(s);
374 int len= get_bits_count(s) - pos;
375 int bits= show>>(24-len);
377 print_bin(bits, len);
379 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
384 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
385 int show= show_bits(s, 24);
386 int pos= get_bits_count(s);
387 int i= get_te0_golomb(s, r);
388 int len= get_bits_count(s) - pos;
389 int bits= show>>(24-len);
391 print_bin(bits, len);
393 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
398 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
399 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
400 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
401 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
406 * write unsigned exp golomb code.
408 static inline void set_ue_golomb(PutBitContext *pb, int i){
420 put_bits(pb, ff_ue_golomb_len[i], i+1);
424 put_bits(pb, 2*e+1, i+1);
429 * write truncated unsigned exp golomb code.
431 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
435 if(range==2) put_bits(pb, 1, i^1);
436 else set_ue_golomb(pb, i);
440 * write signed exp golomb code. 16 bits at most.
442 static inline void set_se_golomb(PutBitContext *pb, int i){
443 // if (i>32767 || i<-32767)
444 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
450 if(i<0) i^= -1; //FIXME check if gcc does the right thing
455 set_ue_golomb(pb, i);
459 * write unsigned golomb rice code (ffv1).
461 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
468 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
470 put_bits(pb, limit + esc_len, i - limit + 1);
475 * write unsigned golomb rice code (jpegls).
477 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
496 put_bits(pb, limit , 1);
497 put_bits(pb, esc_len, i - 1);
502 * write signed golomb rice code (ffv1).
504 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
510 set_ur_golomb(pb, v, k, limit, esc_len);
514 * write signed golomb rice code (flac).
516 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
522 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
525 #endif /* AVCODEC_GOLOMB_H */