Rename var: val -> energy
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / golomb.h
blobf2bc7fda60d9922bd98d8934644749211ccfaa5d
1 /*
2 * exp golomb vlc stuff
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
23 /**
24 * @file golomb.h
25 * @brief
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
30 #ifndef FFMPEG_GOLOMB_H
31 #define FFMPEG_GOLOMB_H
33 #include <stdint.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];
46 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
49 /**
50 * read unsigned exp golomb code.
52 static inline int get_ue_golomb(GetBitContext *gb){
53 unsigned int buf;
54 int log;
56 OPEN_READER(re, gb);
57 UPDATE_CACHE(re, gb);
58 buf=GET_CACHE(re, gb);
60 if(buf >= (1<<27)){
61 buf >>= 32 - 9;
62 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
63 CLOSE_READER(re, gb);
65 return ff_ue_golomb_vlc_code[buf];
66 }else{
67 log= 2*av_log2(buf) - 31;
68 buf>>= log;
69 buf--;
70 LAST_SKIP_BITS(re, gb, 32 - log);
71 CLOSE_READER(re, gb);
73 return buf;
77 static inline int svq3_get_ue_golomb(GetBitContext *gb){
78 uint32_t buf;
80 OPEN_READER(re, gb);
81 UPDATE_CACHE(re, gb);
82 buf=GET_CACHE(re, gb);
84 if(buf&0xAA800000){
85 buf >>= 32 - 8;
86 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
87 CLOSE_READER(re, gb);
89 return ff_interleaved_ue_golomb_vlc_code[buf];
90 }else{
91 int ret = 1;
93 while (1) {
94 buf >>= 32 - 8;
95 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
97 if (ff_interleaved_golomb_vlc_len[buf] != 9){
98 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
99 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
100 break;
102 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
103 UPDATE_CACHE(re, gb);
104 buf = GET_CACHE(re, gb);
107 CLOSE_READER(re, gb);
108 return ret - 1;
113 * read unsigned truncated exp golomb code.
115 static inline int get_te0_golomb(GetBitContext *gb, int range){
116 assert(range >= 1);
118 if(range==1) return 0;
119 else if(range==2) return get_bits1(gb)^1;
120 else return get_ue_golomb(gb);
124 * read unsigned truncated exp golomb code.
126 static inline int get_te_golomb(GetBitContext *gb, int range){
127 assert(range >= 1);
129 if(range==2) return get_bits1(gb)^1;
130 else return get_ue_golomb(gb);
135 * read signed exp golomb code.
137 static inline int get_se_golomb(GetBitContext *gb){
138 unsigned int buf;
139 int log;
141 OPEN_READER(re, gb);
142 UPDATE_CACHE(re, gb);
143 buf=GET_CACHE(re, gb);
145 if(buf >= (1<<27)){
146 buf >>= 32 - 9;
147 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
148 CLOSE_READER(re, gb);
150 return ff_se_golomb_vlc_code[buf];
151 }else{
152 log= 2*av_log2(buf) - 31;
153 buf>>= log;
155 LAST_SKIP_BITS(re, gb, 32 - log);
156 CLOSE_READER(re, gb);
158 if(buf&1) buf= -(buf>>1);
159 else buf= (buf>>1);
161 return buf;
165 static inline int svq3_get_se_golomb(GetBitContext *gb){
166 unsigned int buf;
167 int log;
169 OPEN_READER(re, gb);
170 UPDATE_CACHE(re, gb);
171 buf=GET_CACHE(re, gb);
173 if(buf&0xAA800000){
174 buf >>= 32 - 8;
175 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
176 CLOSE_READER(re, gb);
178 return ff_interleaved_se_golomb_vlc_code[buf];
179 }else{
180 LAST_SKIP_BITS(re, gb, 8);
181 UPDATE_CACHE(re, gb);
182 buf |= 1 | (GET_CACHE(re, gb) >> 8);
184 if((buf & 0xAAAAAAAA) == 0)
185 return INVALID_VLC;
187 for(log=31; (buf & 0x80000000) == 0; log--){
188 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
191 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
192 CLOSE_READER(re, gb);
194 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
198 static inline int dirac_get_se_golomb(GetBitContext *gb){
199 uint32_t buf;
200 uint32_t ret;
202 ret = svq3_get_ue_golomb(gb);
204 if (ret) {
205 OPEN_READER(re, gb);
206 UPDATE_CACHE(re, gb);
207 buf = SHOW_SBITS(re, gb, 1);
208 LAST_SKIP_BITS(re, gb, 1);
209 ret = (ret ^ buf) - buf;
210 CLOSE_READER(re, gb);
213 return ret;
217 * read unsigned golomb rice code (ffv1).
219 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
220 unsigned int buf;
221 int log;
223 OPEN_READER(re, gb);
224 UPDATE_CACHE(re, gb);
225 buf=GET_CACHE(re, gb);
227 log= av_log2(buf);
229 if(log > 31-limit){
230 buf >>= log - k;
231 buf += (30-log)<<k;
232 LAST_SKIP_BITS(re, gb, 32 + k - log);
233 CLOSE_READER(re, gb);
235 return buf;
236 }else{
237 buf >>= 32 - limit - esc_len;
238 LAST_SKIP_BITS(re, gb, esc_len + limit);
239 CLOSE_READER(re, gb);
241 return buf + limit - 1;
246 * read unsigned golomb rice code (jpegls).
248 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
249 unsigned int buf;
250 int log;
252 OPEN_READER(re, gb);
253 UPDATE_CACHE(re, gb);
254 buf=GET_CACHE(re, gb);
256 log= av_log2(buf);
258 if(log > 31-11){
259 buf >>= log - k;
260 buf += (30-log)<<k;
261 LAST_SKIP_BITS(re, gb, 32 + k - log);
262 CLOSE_READER(re, gb);
264 return buf;
265 }else{
266 int i;
267 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
268 LAST_SKIP_BITS(re, gb, 1);
269 UPDATE_CACHE(re, gb);
271 SKIP_BITS(re, gb, 1);
273 if(i < limit - 1){
274 if(k){
275 buf = SHOW_UBITS(re, gb, k);
276 LAST_SKIP_BITS(re, gb, k);
277 }else{
278 buf=0;
281 CLOSE_READER(re, gb);
282 return buf + (i<<k);
283 }else if(i == limit - 1){
284 buf = SHOW_UBITS(re, gb, esc_len);
285 LAST_SKIP_BITS(re, gb, esc_len);
286 CLOSE_READER(re, gb);
288 return buf + 1;
289 }else
290 return -1;
295 * read signed golomb rice code (ffv1).
297 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
298 int v= get_ur_golomb(gb, k, limit, esc_len);
300 v++;
301 if (v&1) return v>>1;
302 else return -(v>>1);
304 // return (v>>1) ^ -(v&1);
308 * read signed golomb rice code (flac).
310 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
311 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
312 return (v>>1) ^ -(v&1);
316 * read unsigned golomb rice code (shorten).
318 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
319 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
323 * read signed golomb rice code (shorten).
325 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
327 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
328 if (uvar & 1)
329 return ~(uvar >> 1);
330 else
331 return uvar >> 1;
336 #ifdef TRACE
338 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
339 int show= show_bits(s, 24);
340 int pos= get_bits_count(s);
341 int i= get_ue_golomb(s);
342 int len= get_bits_count(s) - pos;
343 int bits= show>>(24-len);
345 print_bin(bits, len);
347 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
349 return i;
352 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
353 int show= show_bits(s, 24);
354 int pos= get_bits_count(s);
355 int i= get_se_golomb(s);
356 int len= get_bits_count(s) - pos;
357 int bits= show>>(24-len);
359 print_bin(bits, len);
361 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
363 return i;
366 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
367 int show= show_bits(s, 24);
368 int pos= get_bits_count(s);
369 int i= get_te0_golomb(s, r);
370 int len= get_bits_count(s) - pos;
371 int bits= show>>(24-len);
373 print_bin(bits, len);
375 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
377 return i;
380 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
381 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
382 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
383 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
385 #endif
388 * write unsigned exp golomb code.
390 static inline void set_ue_golomb(PutBitContext *pb, int i){
391 int e;
393 assert(i>=0);
395 #if 0
396 if(i=0){
397 put_bits(pb, 1, 1);
398 return;
400 #endif
401 if(i<256)
402 put_bits(pb, ff_ue_golomb_len[i], i+1);
403 else{
404 e= av_log2(i+1);
406 put_bits(pb, 2*e+1, i+1);
411 * write truncated unsigned exp golomb code.
413 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
414 assert(range >= 1);
415 assert(i<=range);
417 if(range==2) put_bits(pb, 1, i^1);
418 else set_ue_golomb(pb, i);
422 * write signed exp golomb code. 16 bits at most.
424 static inline void set_se_golomb(PutBitContext *pb, int i){
425 // if (i>32767 || i<-32767)
426 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
427 #if 0
428 if(i<=0) i= -2*i;
429 else i= 2*i-1;
430 #elif 1
431 i= 2*i-1;
432 if(i<0) i^= -1; //FIXME check if gcc does the right thing
433 #else
434 i= 2*i-1;
435 i^= (i>>31);
436 #endif
437 set_ue_golomb(pb, i);
441 * write unsigned golomb rice code (ffv1).
443 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
444 int e;
446 assert(i>=0);
448 e= i>>k;
449 if(e<limit){
450 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
451 }else{
452 put_bits(pb, limit + esc_len, i - limit + 1);
457 * write unsigned golomb rice code (jpegls).
459 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
460 int e;
462 assert(i>=0);
464 e= (i>>k) + 1;
465 if(e<limit){
466 while(e > 31) {
467 put_bits(pb, 31, 0);
468 e -= 31;
470 put_bits(pb, e, 1);
471 if(k)
472 put_bits(pb, k, i&((1<<k)-1));
473 }else{
474 while(limit > 31) {
475 put_bits(pb, 31, 0);
476 limit -= 31;
478 put_bits(pb, limit , 1);
479 put_bits(pb, esc_len, i - 1);
484 * write signed golomb rice code (ffv1).
486 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
487 int v;
489 v = -2*i-1;
490 v ^= (v>>31);
492 set_ur_golomb(pb, v, k, limit, esc_len);
496 * write signed golomb rice code (flac).
498 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
499 int v;
501 v = -2*i-1;
502 v ^= (v>>31);
504 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
507 #endif /* FFMPEG_GOLOMB_H */