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
27 * exp golomb vlc stuff
28 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
31 #ifndef AVCODEC_GOLOMB_H
32 #define AVCODEC_GOLOMB_H
35 #include "bitstream.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];
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);
77 static inline int svq3_get_ue_golomb(GetBitContext *gb){
83 buf=GET_CACHE(re, gb);
87 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
90 return ff_interleaved_ue_golomb_vlc_code[buf];
92 LAST_SKIP_BITS(re, gb, 8);
94 buf |= 1 | (GET_CACHE(re, gb) >> 8);
96 if((buf & 0xAAAAAAAA) == 0)
99 for(log=31; (buf & 0x80000000) == 0; log--){
100 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
103 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
104 CLOSE_READER(re, gb);
106 return ((buf << log) >> log) - 1;
111 * read unsigned truncated exp golomb code.
113 static inline int get_te0_golomb(GetBitContext *gb, int range){
116 if(range==1) return 0;
117 else if(range==2) return get_bits1(gb)^1;
118 else return get_ue_golomb(gb);
122 * read unsigned truncated exp golomb code.
124 static inline int get_te_golomb(GetBitContext *gb, int range){
127 if(range==2) return get_bits1(gb)^1;
128 else return get_ue_golomb(gb);
133 * read signed exp golomb code.
135 static inline int get_se_golomb(GetBitContext *gb){
140 UPDATE_CACHE(re, gb);
141 buf=GET_CACHE(re, gb);
145 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
146 CLOSE_READER(re, gb);
148 return ff_se_golomb_vlc_code[buf];
150 log= 2*av_log2(buf) - 31;
153 LAST_SKIP_BITS(re, gb, 32 - log);
154 CLOSE_READER(re, gb);
156 if(buf&1) buf= -(buf>>1);
163 static inline int svq3_get_se_golomb(GetBitContext *gb){
168 UPDATE_CACHE(re, gb);
169 buf=GET_CACHE(re, gb);
173 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
174 CLOSE_READER(re, gb);
176 return ff_interleaved_se_golomb_vlc_code[buf];
178 LAST_SKIP_BITS(re, gb, 8);
179 UPDATE_CACHE(re, gb);
180 buf |= 1 | (GET_CACHE(re, gb) >> 8);
182 if((buf & 0xAAAAAAAA) == 0)
185 for(log=31; (buf & 0x80000000) == 0; log--){
186 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
189 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
190 CLOSE_READER(re, gb);
192 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
197 * read unsigned golomb rice code (ffv1).
199 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
204 UPDATE_CACHE(re, gb);
205 buf=GET_CACHE(re, gb);
212 LAST_SKIP_BITS(re, gb, 32 + k - log);
213 CLOSE_READER(re, gb);
217 buf >>= 32 - limit - esc_len;
218 LAST_SKIP_BITS(re, gb, esc_len + limit);
219 CLOSE_READER(re, gb);
221 return buf + limit - 1;
226 * read unsigned golomb rice code (jpegls).
228 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
233 UPDATE_CACHE(re, gb);
234 buf=GET_CACHE(re, gb);
241 LAST_SKIP_BITS(re, gb, 32 + k - log);
242 CLOSE_READER(re, gb);
247 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
248 LAST_SKIP_BITS(re, gb, 1);
249 UPDATE_CACHE(re, gb);
251 SKIP_BITS(re, gb, 1);
255 buf = SHOW_UBITS(re, gb, k);
256 LAST_SKIP_BITS(re, gb, k);
261 CLOSE_READER(re, gb);
263 }else if(i == limit - 1){
264 buf = SHOW_UBITS(re, gb, esc_len);
265 LAST_SKIP_BITS(re, gb, esc_len);
266 CLOSE_READER(re, gb);
275 * read signed golomb rice code (ffv1).
277 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
278 int v= get_ur_golomb(gb, k, limit, esc_len);
281 if (v&1) return v>>1;
284 // return (v>>1) ^ -(v&1);
288 * read signed golomb rice code (flac).
290 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
291 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
292 return (v>>1) ^ -(v&1);
296 * read unsigned golomb rice code (shorten).
298 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
299 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
303 * read signed golomb rice code (shorten).
305 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
307 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
318 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
319 int show= show_bits(s, 24);
320 int pos= get_bits_count(s);
321 int i= get_ue_golomb(s);
322 int len= get_bits_count(s) - pos;
323 int bits= show>>(24-len);
325 print_bin(bits, len);
327 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
332 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
333 int show= show_bits(s, 24);
334 int pos= get_bits_count(s);
335 int i= get_se_golomb(s);
336 int len= get_bits_count(s) - pos;
337 int bits= show>>(24-len);
339 print_bin(bits, len);
341 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
346 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
347 int show= show_bits(s, 24);
348 int pos= get_bits_count(s);
349 int i= get_te0_golomb(s, r);
350 int len= get_bits_count(s) - pos;
351 int bits= show>>(24-len);
353 print_bin(bits, len);
355 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
360 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
361 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
362 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
363 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
368 * write unsigned exp golomb code.
370 static inline void set_ue_golomb(PutBitContext *pb, int i){
382 put_bits(pb, ff_ue_golomb_len[i], i+1);
386 put_bits(pb, 2*e+1, i+1);
391 * write truncated unsigned exp golomb code.
393 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
397 if(range==2) put_bits(pb, 1, i^1);
398 else set_ue_golomb(pb, i);
402 * write signed exp golomb code. 16 bits at most.
404 static inline void set_se_golomb(PutBitContext *pb, int i){
405 // if (i>32767 || i<-32767)
406 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
412 if(i<0) i^= -1; //FIXME check if gcc does the right thing
417 set_ue_golomb(pb, i);
421 * write unsigned golomb rice code (ffv1).
423 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
430 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
432 put_bits(pb, limit + esc_len, i - limit + 1);
437 * write unsigned golomb rice code (jpegls).
439 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
452 put_bits(pb, k, i&((1<<k)-1));
458 put_bits(pb, limit , 1);
459 put_bits(pb, esc_len, i - 1);
464 * write signed golomb rice code (ffv1).
466 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
472 set_ur_golomb(pb, v, k, limit, esc_len);
476 * write signed golomb rice code (flac).
478 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
484 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
487 #endif // AVCODEC_GOLOMB_H