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
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;
69 LAST_SKIP_BITS(re, gb, 32 - log);
71 if (CONFIG_FTRAPV && log < 0) {
72 av_log(0, AV_LOG_ERROR, "Invalid UE golomb code\n");
73 return AVERROR_INVALIDDATA;
83 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
85 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
89 buf = show_bits_long(gb, 32);
90 log = 31 - av_log2(buf);
91 skip_bits_long(gb, log);
93 return get_bits_long(gb, log + 1) - 1;
97 * read unsigned exp golomb code, constraint to a max of 31.
98 * the return value is undefined if the stored value exceeds 31.
100 static inline int get_ue_golomb_31(GetBitContext *gb){
104 UPDATE_CACHE(re, gb);
105 buf=GET_CACHE(re, gb);
108 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
109 CLOSE_READER(re, gb);
111 return ff_ue_golomb_vlc_code[buf];
114 static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
119 UPDATE_CACHE(re, gb);
120 buf=GET_CACHE(re, gb);
124 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
125 CLOSE_READER(re, gb);
127 return ff_interleaved_ue_golomb_vlc_code[buf];
133 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
135 if (ff_interleaved_golomb_vlc_len[buf] != 9){
136 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
137 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
140 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
141 UPDATE_CACHE(re, gb);
142 buf = GET_CACHE(re, gb);
143 } while (ret<0x8000000U && HAVE_BITS_REMAINING(re, gb));
145 CLOSE_READER(re, gb);
151 * read unsigned truncated exp golomb code.
153 static inline int get_te0_golomb(GetBitContext *gb, int range){
154 av_assert2(range >= 1);
156 if(range==1) return 0;
157 else if(range==2) return get_bits1(gb)^1;
158 else return get_ue_golomb(gb);
162 * read unsigned truncated exp golomb code.
164 static inline int get_te_golomb(GetBitContext *gb, int range){
165 av_assert2(range >= 1);
167 if(range==2) return get_bits1(gb)^1;
168 else return get_ue_golomb(gb);
173 * read signed exp golomb code.
175 static inline int get_se_golomb(GetBitContext *gb){
180 UPDATE_CACHE(re, gb);
181 buf=GET_CACHE(re, gb);
185 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
186 CLOSE_READER(re, gb);
188 return ff_se_golomb_vlc_code[buf];
191 LAST_SKIP_BITS(re, gb, 31 - log);
192 UPDATE_CACHE(re, gb);
193 buf = GET_CACHE(re, gb);
197 LAST_SKIP_BITS(re, gb, 32 - log);
198 CLOSE_READER(re, gb);
200 if(buf&1) buf= -(buf>>1);
207 static inline int svq3_get_se_golomb(GetBitContext *gb){
212 UPDATE_CACHE(re, gb);
213 buf=GET_CACHE(re, gb);
217 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
218 CLOSE_READER(re, gb);
220 return ff_interleaved_se_golomb_vlc_code[buf];
222 LAST_SKIP_BITS(re, gb, 8);
223 UPDATE_CACHE(re, gb);
224 buf |= 1 | (GET_CACHE(re, gb) >> 8);
226 if((buf & 0xAAAAAAAA) == 0)
229 for(log=31; (buf & 0x80000000) == 0; log--){
230 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
233 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
234 CLOSE_READER(re, gb);
236 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
240 static inline int dirac_get_se_golomb(GetBitContext *gb){
244 ret = svq3_get_ue_golomb(gb);
248 UPDATE_CACHE(re, gb);
249 buf = SHOW_SBITS(re, gb, 1);
250 LAST_SKIP_BITS(re, gb, 1);
251 ret = (ret ^ buf) - buf;
252 CLOSE_READER(re, gb);
259 * read unsigned golomb rice code (ffv1).
261 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
266 UPDATE_CACHE(re, gb);
267 buf=GET_CACHE(re, gb);
274 LAST_SKIP_BITS(re, gb, 32 + k - log);
275 CLOSE_READER(re, gb);
279 LAST_SKIP_BITS(re, gb, limit);
280 UPDATE_CACHE(re, gb);
282 buf = SHOW_UBITS(re, gb, esc_len);
284 LAST_SKIP_BITS(re, gb, esc_len);
285 CLOSE_READER(re, gb);
287 return buf + limit - 1;
292 * read unsigned golomb rice code (jpegls).
294 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
299 UPDATE_CACHE(re, gb);
300 buf=GET_CACHE(re, gb);
304 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
307 LAST_SKIP_BITS(re, gb, 32 + k - log);
308 CLOSE_READER(re, gb);
313 for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
314 if (gb->size_in_bits <= re_index)
316 LAST_SKIP_BITS(re, gb, 1);
317 UPDATE_CACHE(re, gb);
319 SKIP_BITS(re, gb, 1);
323 buf = SHOW_UBITS(re, gb, k);
324 LAST_SKIP_BITS(re, gb, k);
329 CLOSE_READER(re, gb);
331 }else if(i == limit - 1){
332 buf = SHOW_UBITS(re, gb, esc_len);
333 LAST_SKIP_BITS(re, gb, esc_len);
334 CLOSE_READER(re, gb);
343 * read signed golomb rice code (ffv1).
345 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
346 int v= get_ur_golomb(gb, k, limit, esc_len);
349 if (v&1) return v>>1;
352 // return (v>>1) ^ -(v&1);
356 * read signed golomb rice code (flac).
358 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
359 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
360 return (v>>1) ^ -(v&1);
364 * read unsigned golomb rice code (shorten).
366 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
367 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
371 * read signed golomb rice code (shorten).
373 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
375 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
386 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
389 int show= show_bits(s, 24);
390 int pos= get_bits_count(s);
391 int i= get_ue_golomb(s);
392 int len= get_bits_count(s) - pos;
393 int bits= show>>(24-len);
395 print_bin(bits, len);
397 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
402 static inline int get_se(GetBitContext *s, const char *file, const char *func,
405 int show= show_bits(s, 24);
406 int pos= get_bits_count(s);
407 int i= get_se_golomb(s);
408 int len= get_bits_count(s) - pos;
409 int bits= show>>(24-len);
411 print_bin(bits, len);
413 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
418 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
419 int show= show_bits(s, 24);
420 int pos= get_bits_count(s);
421 int i= get_te0_golomb(s, r);
422 int len= get_bits_count(s) - pos;
423 int bits= show>>(24-len);
425 print_bin(bits, len);
427 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
432 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
433 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
434 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
435 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
440 * write unsigned exp golomb code.
442 static inline void set_ue_golomb(PutBitContext *pb, int i){
454 put_bits(pb, ff_ue_golomb_len[i], i+1);
458 put_bits(pb, 2*e+1, i+1);
463 * write truncated unsigned exp golomb code.
465 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
466 av_assert2(range >= 1);
467 av_assert2(i<=range);
469 if(range==2) put_bits(pb, 1, i^1);
470 else set_ue_golomb(pb, i);
474 * write signed exp golomb code. 16 bits at most.
476 static inline void set_se_golomb(PutBitContext *pb, int i){
482 if(i<0) i^= -1; //FIXME check if gcc does the right thing
487 set_ue_golomb(pb, i);
491 * write unsigned golomb rice code (ffv1).
493 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
500 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
502 put_bits(pb, limit + esc_len, i - limit + 1);
507 * write unsigned golomb rice code (jpegls).
509 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
528 put_bits(pb, limit , 1);
529 put_bits(pb, esc_len, i - 1);
534 * write signed golomb rice code (ffv1).
536 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
542 set_ur_golomb(pb, v, k, limit, esc_len);
546 * write signed golomb rice code (flac).
548 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
554 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
557 #endif /* AVCODEC_GOLOMB_H */