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 #define INVALID_VLC 0x80000000
33 extern const uint8_t ff_golomb_vlc_len[512];
34 extern const uint8_t ff_ue_golomb_vlc_code[512];
35 extern const int8_t ff_se_golomb_vlc_code[512];
36 extern const uint8_t ff_ue_golomb_len[256];
38 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
39 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
40 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
44 * read unsigned exp golomb code.
46 static inline int get_ue_golomb(GetBitContext *gb){
52 buf=GET_CACHE(re, gb);
56 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
59 return ff_ue_golomb_vlc_code[buf];
61 log= 2*av_log2(buf) - 31;
64 LAST_SKIP_BITS(re, gb, 32 - log);
71 static inline int svq3_get_ue_golomb(GetBitContext *gb){
77 buf=GET_CACHE(re, gb);
81 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
84 return ff_interleaved_ue_golomb_vlc_code[buf];
86 LAST_SKIP_BITS(re, gb, 8);
88 buf |= 1 | (GET_CACHE(re, gb) >> 8);
90 if((buf & 0xAAAAAAAA) == 0)
93 for(log=31; (buf & 0x80000000) == 0; log--){
94 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
97 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
100 return ((buf << log) >> log) - 1;
105 * read unsigned truncated exp golomb code.
107 static inline int get_te0_golomb(GetBitContext *gb, int range){
110 if(range==1) return 0;
111 else if(range==2) return get_bits1(gb)^1;
112 else return get_ue_golomb(gb);
116 * read unsigned truncated exp golomb code.
118 static inline int get_te_golomb(GetBitContext *gb, int range){
121 if(range==2) return get_bits1(gb)^1;
122 else return get_ue_golomb(gb);
127 * read signed exp golomb code.
129 static inline int get_se_golomb(GetBitContext *gb){
134 UPDATE_CACHE(re, gb);
135 buf=GET_CACHE(re, gb);
139 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
140 CLOSE_READER(re, gb);
142 return ff_se_golomb_vlc_code[buf];
144 log= 2*av_log2(buf) - 31;
147 LAST_SKIP_BITS(re, gb, 32 - log);
148 CLOSE_READER(re, gb);
150 if(buf&1) buf= -(buf>>1);
157 static inline int svq3_get_se_golomb(GetBitContext *gb){
162 UPDATE_CACHE(re, gb);
163 buf=GET_CACHE(re, gb);
167 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
168 CLOSE_READER(re, gb);
170 return ff_interleaved_se_golomb_vlc_code[buf];
172 LAST_SKIP_BITS(re, gb, 8);
173 UPDATE_CACHE(re, gb);
174 buf |= 1 | (GET_CACHE(re, gb) >> 8);
176 if((buf & 0xAAAAAAAA) == 0)
179 for(log=31; (buf & 0x80000000) == 0; log--){
180 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
183 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
184 CLOSE_READER(re, gb);
186 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
191 * read unsigned golomb rice code (ffv1).
193 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
198 UPDATE_CACHE(re, gb);
199 buf=GET_CACHE(re, gb);
206 LAST_SKIP_BITS(re, gb, 32 + k - log);
207 CLOSE_READER(re, gb);
211 buf >>= 32 - limit - esc_len;
212 LAST_SKIP_BITS(re, gb, esc_len + limit);
213 CLOSE_READER(re, gb);
215 return buf + limit - 1;
220 * read unsigned golomb rice code (jpegls).
222 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
227 UPDATE_CACHE(re, gb);
228 buf=GET_CACHE(re, gb);
235 LAST_SKIP_BITS(re, gb, 32 + k - log);
236 CLOSE_READER(re, gb);
241 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
242 LAST_SKIP_BITS(re, gb, 1);
243 UPDATE_CACHE(re, gb);
245 SKIP_BITS(re, gb, 1);
249 buf = SHOW_UBITS(re, gb, k);
250 LAST_SKIP_BITS(re, gb, k);
255 CLOSE_READER(re, gb);
257 }else if(i == limit - 1){
258 buf = SHOW_UBITS(re, gb, esc_len);
259 LAST_SKIP_BITS(re, gb, esc_len);
260 CLOSE_READER(re, gb);
269 * read signed golomb rice code (ffv1).
271 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
272 int v= get_ur_golomb(gb, k, limit, esc_len);
275 if (v&1) return v>>1;
278 // return (v>>1) ^ -(v&1);
282 * read signed golomb rice code (flac).
284 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
285 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
286 return (v>>1) ^ -(v&1);
290 * read unsigned golomb rice code (shorten).
292 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
293 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
297 * read signed golomb rice code (shorten).
299 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
301 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
312 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
313 int show= show_bits(s, 24);
314 int pos= get_bits_count(s);
315 int i= get_ue_golomb(s);
316 int len= get_bits_count(s) - pos;
317 int bits= show>>(24-len);
319 print_bin(bits, len);
321 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
326 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
327 int show= show_bits(s, 24);
328 int pos= get_bits_count(s);
329 int i= get_se_golomb(s);
330 int len= get_bits_count(s) - pos;
331 int bits= show>>(24-len);
333 print_bin(bits, len);
335 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
340 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
341 int show= show_bits(s, 24);
342 int pos= get_bits_count(s);
343 int i= get_te0_golomb(s, r);
344 int len= get_bits_count(s) - pos;
345 int bits= show>>(24-len);
347 print_bin(bits, len);
349 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
354 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
355 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
356 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
357 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
362 * write unsigned exp golomb code.
364 static inline void set_ue_golomb(PutBitContext *pb, int i){
376 put_bits(pb, ff_ue_golomb_len[i], i+1);
380 put_bits(pb, 2*e+1, i+1);
385 * write truncated unsigned exp golomb code.
387 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
391 if(range==2) put_bits(pb, 1, i^1);
392 else set_ue_golomb(pb, i);
396 * write signed exp golomb code. 16 bits at most.
398 static inline void set_se_golomb(PutBitContext *pb, int i){
399 // if (i>32767 || i<-32767)
400 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
406 if(i<0) i^= -1; //FIXME check if gcc does the right thing
411 set_ue_golomb(pb, i);
415 * write unsigned golomb rice code (ffv1).
417 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
424 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
426 put_bits(pb, limit + esc_len, i - limit + 1);
431 * write unsigned golomb rice code (jpegls).
433 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
446 put_bits(pb, k, i&((1<<k)-1));
448 put_bits(pb, limit , 1);
449 put_bits(pb, esc_len, i - 1);
454 * write signed golomb rice code (ffv1).
456 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
462 set_ur_golomb(pb, v, k, limit, esc_len);
466 * write signed golomb rice code (flac).
468 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
474 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);