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
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];
49 * read unsigned exp golomb code.
51 static inline int get_ue_golomb(GetBitContext *gb){
57 buf=GET_CACHE(re, gb);
61 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
64 return ff_ue_golomb_vlc_code[buf];
66 log= 2*av_log2(buf) - 31;
69 LAST_SKIP_BITS(re, gb, 32 - log);
76 static inline int svq3_get_ue_golomb(GetBitContext *gb){
82 buf=GET_CACHE(re, gb);
86 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
89 return ff_interleaved_ue_golomb_vlc_code[buf];
91 LAST_SKIP_BITS(re, gb, 8);
93 buf |= 1 | (GET_CACHE(re, gb) >> 8);
95 if((buf & 0xAAAAAAAA) == 0)
98 for(log=31; (buf & 0x80000000) == 0; log--){
99 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
102 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
103 CLOSE_READER(re, gb);
105 return ((buf << log) >> log) - 1;
110 * read unsigned truncated exp golomb code.
112 static inline int get_te0_golomb(GetBitContext *gb, int range){
115 if(range==1) return 0;
116 else if(range==2) return get_bits1(gb)^1;
117 else return get_ue_golomb(gb);
121 * read unsigned truncated exp golomb code.
123 static inline int get_te_golomb(GetBitContext *gb, int range){
126 if(range==2) return get_bits1(gb)^1;
127 else return get_ue_golomb(gb);
132 * read signed exp golomb code.
134 static inline int get_se_golomb(GetBitContext *gb){
139 UPDATE_CACHE(re, gb);
140 buf=GET_CACHE(re, gb);
144 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
145 CLOSE_READER(re, gb);
147 return ff_se_golomb_vlc_code[buf];
149 log= 2*av_log2(buf) - 31;
152 LAST_SKIP_BITS(re, gb, 32 - log);
153 CLOSE_READER(re, gb);
155 if(buf&1) buf= -(buf>>1);
162 static inline int svq3_get_se_golomb(GetBitContext *gb){
167 UPDATE_CACHE(re, gb);
168 buf=GET_CACHE(re, gb);
172 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
173 CLOSE_READER(re, gb);
175 return ff_interleaved_se_golomb_vlc_code[buf];
177 LAST_SKIP_BITS(re, gb, 8);
178 UPDATE_CACHE(re, gb);
179 buf |= 1 | (GET_CACHE(re, gb) >> 8);
181 if((buf & 0xAAAAAAAA) == 0)
184 for(log=31; (buf & 0x80000000) == 0; log--){
185 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
188 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
189 CLOSE_READER(re, gb);
191 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
196 * read unsigned golomb rice code (ffv1).
198 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
203 UPDATE_CACHE(re, gb);
204 buf=GET_CACHE(re, gb);
211 LAST_SKIP_BITS(re, gb, 32 + k - log);
212 CLOSE_READER(re, gb);
216 buf >>= 32 - limit - esc_len;
217 LAST_SKIP_BITS(re, gb, esc_len + limit);
218 CLOSE_READER(re, gb);
220 return buf + limit - 1;
225 * read unsigned golomb rice code (jpegls).
227 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
232 UPDATE_CACHE(re, gb);
233 buf=GET_CACHE(re, gb);
240 LAST_SKIP_BITS(re, gb, 32 + k - log);
241 CLOSE_READER(re, gb);
246 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
247 LAST_SKIP_BITS(re, gb, 1);
248 UPDATE_CACHE(re, gb);
250 SKIP_BITS(re, gb, 1);
254 buf = SHOW_UBITS(re, gb, k);
255 LAST_SKIP_BITS(re, gb, k);
260 CLOSE_READER(re, gb);
262 }else if(i == limit - 1){
263 buf = SHOW_UBITS(re, gb, esc_len);
264 LAST_SKIP_BITS(re, gb, esc_len);
265 CLOSE_READER(re, gb);
274 * read signed golomb rice code (ffv1).
276 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
277 int v= get_ur_golomb(gb, k, limit, esc_len);
280 if (v&1) return v>>1;
283 // return (v>>1) ^ -(v&1);
287 * read signed golomb rice code (flac).
289 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
290 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
291 return (v>>1) ^ -(v&1);
295 * read unsigned golomb rice code (shorten).
297 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
298 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
302 * read signed golomb rice code (shorten).
304 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
306 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
317 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
318 int show= show_bits(s, 24);
319 int pos= get_bits_count(s);
320 int i= get_ue_golomb(s);
321 int len= get_bits_count(s) - pos;
322 int bits= show>>(24-len);
324 print_bin(bits, len);
326 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
331 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
332 int show= show_bits(s, 24);
333 int pos= get_bits_count(s);
334 int i= get_se_golomb(s);
335 int len= get_bits_count(s) - pos;
336 int bits= show>>(24-len);
338 print_bin(bits, len);
340 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
345 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
346 int show= show_bits(s, 24);
347 int pos= get_bits_count(s);
348 int i= get_te0_golomb(s, r);
349 int len= get_bits_count(s) - pos;
350 int bits= show>>(24-len);
352 print_bin(bits, len);
354 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
359 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
360 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
361 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
362 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
367 * write unsigned exp golomb code.
369 static inline void set_ue_golomb(PutBitContext *pb, int i){
381 put_bits(pb, ff_ue_golomb_len[i], i+1);
385 put_bits(pb, 2*e+1, i+1);
390 * write truncated unsigned exp golomb code.
392 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
396 if(range==2) put_bits(pb, 1, i^1);
397 else set_ue_golomb(pb, i);
401 * write signed exp golomb code. 16 bits at most.
403 static inline void set_se_golomb(PutBitContext *pb, int i){
404 // if (i>32767 || i<-32767)
405 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
411 if(i<0) i^= -1; //FIXME check if gcc does the right thing
416 set_ue_golomb(pb, i);
420 * write unsigned golomb rice code (ffv1).
422 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
429 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
431 put_bits(pb, limit + esc_len, i - limit + 1);
436 * write unsigned golomb rice code (jpegls).
438 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
451 put_bits(pb, k, i&((1<<k)-1));
457 put_bits(pb, limit , 1);
458 put_bits(pb, esc_len, i - 1);
463 * write signed golomb rice code (ffv1).
465 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
471 set_ur_golomb(pb, v, k, limit, esc_len);
475 * write signed golomb rice code (flac).
477 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
483 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
486 #endif // AVCODEC_GOLOMB_H