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
32 #include "bitstream.h"
34 #define INVALID_VLC 0x80000000
36 extern const uint8_t ff_golomb_vlc_len[512];
37 extern const uint8_t ff_ue_golomb_vlc_code[512];
38 extern const int8_t ff_se_golomb_vlc_code[512];
39 extern const uint8_t ff_ue_golomb_len[256];
41 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
42 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
43 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
47 * read unsigned exp golomb code.
49 static inline int get_ue_golomb(GetBitContext *gb){
55 buf=GET_CACHE(re, gb);
59 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
62 return ff_ue_golomb_vlc_code[buf];
64 log= 2*av_log2(buf) - 31;
67 LAST_SKIP_BITS(re, gb, 32 - log);
74 static inline int svq3_get_ue_golomb(GetBitContext *gb){
80 buf=GET_CACHE(re, gb);
84 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
87 return ff_interleaved_ue_golomb_vlc_code[buf];
89 LAST_SKIP_BITS(re, gb, 8);
91 buf |= 1 | (GET_CACHE(re, gb) >> 8);
93 if((buf & 0xAAAAAAAA) == 0)
96 for(log=31; (buf & 0x80000000) == 0; log--){
97 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
100 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
101 CLOSE_READER(re, gb);
103 return ((buf << log) >> log) - 1;
108 * read unsigned truncated exp golomb code.
110 static inline int get_te0_golomb(GetBitContext *gb, int range){
113 if(range==1) return 0;
114 else if(range==2) return get_bits1(gb)^1;
115 else return get_ue_golomb(gb);
119 * read unsigned truncated exp golomb code.
121 static inline int get_te_golomb(GetBitContext *gb, int range){
124 if(range==2) return get_bits1(gb)^1;
125 else return get_ue_golomb(gb);
130 * read signed exp golomb code.
132 static inline int get_se_golomb(GetBitContext *gb){
137 UPDATE_CACHE(re, gb);
138 buf=GET_CACHE(re, gb);
142 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
143 CLOSE_READER(re, gb);
145 return ff_se_golomb_vlc_code[buf];
147 log= 2*av_log2(buf) - 31;
150 LAST_SKIP_BITS(re, gb, 32 - log);
151 CLOSE_READER(re, gb);
153 if(buf&1) buf= -(buf>>1);
160 static inline int svq3_get_se_golomb(GetBitContext *gb){
165 UPDATE_CACHE(re, gb);
166 buf=GET_CACHE(re, gb);
170 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
171 CLOSE_READER(re, gb);
173 return ff_interleaved_se_golomb_vlc_code[buf];
175 LAST_SKIP_BITS(re, gb, 8);
176 UPDATE_CACHE(re, gb);
177 buf |= 1 | (GET_CACHE(re, gb) >> 8);
179 if((buf & 0xAAAAAAAA) == 0)
182 for(log=31; (buf & 0x80000000) == 0; log--){
183 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
186 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
187 CLOSE_READER(re, gb);
189 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
194 * read unsigned golomb rice code (ffv1).
196 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
201 UPDATE_CACHE(re, gb);
202 buf=GET_CACHE(re, gb);
209 LAST_SKIP_BITS(re, gb, 32 + k - log);
210 CLOSE_READER(re, gb);
214 buf >>= 32 - limit - esc_len;
215 LAST_SKIP_BITS(re, gb, esc_len + limit);
216 CLOSE_READER(re, gb);
218 return buf + limit - 1;
223 * read unsigned golomb rice code (jpegls).
225 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
230 UPDATE_CACHE(re, gb);
231 buf=GET_CACHE(re, gb);
238 LAST_SKIP_BITS(re, gb, 32 + k - log);
239 CLOSE_READER(re, gb);
244 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
245 LAST_SKIP_BITS(re, gb, 1);
246 UPDATE_CACHE(re, gb);
248 SKIP_BITS(re, gb, 1);
252 buf = SHOW_UBITS(re, gb, k);
253 LAST_SKIP_BITS(re, gb, k);
258 CLOSE_READER(re, gb);
260 }else if(i == limit - 1){
261 buf = SHOW_UBITS(re, gb, esc_len);
262 LAST_SKIP_BITS(re, gb, esc_len);
263 CLOSE_READER(re, gb);
272 * read signed golomb rice code (ffv1).
274 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
275 int v= get_ur_golomb(gb, k, limit, esc_len);
278 if (v&1) return v>>1;
281 // return (v>>1) ^ -(v&1);
285 * read signed golomb rice code (flac).
287 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
288 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
289 return (v>>1) ^ -(v&1);
293 * read unsigned golomb rice code (shorten).
295 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
296 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
300 * read signed golomb rice code (shorten).
302 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
304 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
315 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
316 int show= show_bits(s, 24);
317 int pos= get_bits_count(s);
318 int i= get_ue_golomb(s);
319 int len= get_bits_count(s) - pos;
320 int bits= show>>(24-len);
322 print_bin(bits, len);
324 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
329 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
330 int show= show_bits(s, 24);
331 int pos= get_bits_count(s);
332 int i= get_se_golomb(s);
333 int len= get_bits_count(s) - pos;
334 int bits= show>>(24-len);
336 print_bin(bits, len);
338 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
343 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
344 int show= show_bits(s, 24);
345 int pos= get_bits_count(s);
346 int i= get_te0_golomb(s, r);
347 int len= get_bits_count(s) - pos;
348 int bits= show>>(24-len);
350 print_bin(bits, len);
352 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
357 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
358 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
359 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
360 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
365 * write unsigned exp golomb code.
367 static inline void set_ue_golomb(PutBitContext *pb, int i){
379 put_bits(pb, ff_ue_golomb_len[i], i+1);
383 put_bits(pb, 2*e+1, i+1);
388 * write truncated unsigned exp golomb code.
390 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
394 if(range==2) put_bits(pb, 1, i^1);
395 else set_ue_golomb(pb, i);
399 * write signed exp golomb code. 16 bits at most.
401 static inline void set_se_golomb(PutBitContext *pb, int i){
402 // if (i>32767 || i<-32767)
403 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
409 if(i<0) i^= -1; //FIXME check if gcc does the right thing
414 set_ue_golomb(pb, i);
418 * write unsigned golomb rice code (ffv1).
420 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
427 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
429 put_bits(pb, limit + esc_len, i - limit + 1);
434 * write unsigned golomb rice code (jpegls).
436 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
449 put_bits(pb, k, i&((1<<k)-1));
455 put_bits(pb, limit , 1);
456 put_bits(pb, esc_len, i - 1);
461 * write signed golomb rice code (ffv1).
463 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
469 set_ur_golomb(pb, v, k, limit, esc_len);
473 * write signed golomb rice code (flac).
475 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
481 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);