add multiple inclusion guards to headers
[libav.git] / libavcodec / golomb.h
CommitLineData
0da71265
MN
1/*
2 * exp golomb vlc stuff
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4579adb0 4 * Copyright (c) 2004 Alex Beregszaszi
0da71265 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
0da71265
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
0da71265 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
0da71265
MN
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0da71265
MN
21 *
22 */
115329f1 23
0da71265
MN
24/**
25 * @file golomb.h
115329f1 26 * @brief
0da71265 27 * exp golomb vlc stuff
4579adb0 28 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
0da71265
MN
29 */
30
699b3f99
MR
31#ifndef AVCODEC_GOLOMB_H
32#define AVCODEC_GOLOMB_H
33
99545457
MR
34#include <stdint.h>
35#include "bitstream.h"
36
8b82a956
MN
37#define INVALID_VLC 0x80000000
38
0da71265
MN
39extern const uint8_t ff_golomb_vlc_len[512];
40extern const uint8_t ff_ue_golomb_vlc_code[512];
41extern const int8_t ff_se_golomb_vlc_code[512];
42extern const uint8_t ff_ue_golomb_len[256];
43
4704097a
MN
44extern const uint8_t ff_interleaved_golomb_vlc_len[256];
45extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
46extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
47
115329f1 48
0da71265
MN
49 /**
50 * read unsigned exp golomb code.
51 */
52static inline int get_ue_golomb(GetBitContext *gb){
53 unsigned int buf;
54 int log;
115329f1 55
0da71265
MN
56 OPEN_READER(re, gb);
57 UPDATE_CACHE(re, gb);
58 buf=GET_CACHE(re, gb);
115329f1 59
0da71265
MN
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);
115329f1 64
0da71265
MN
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);
115329f1 72
0da71265
MN
73 return buf;
74 }
75}
76
8b82a956 77static inline int svq3_get_ue_golomb(GetBitContext *gb){
4704097a 78 uint32_t buf;
8b82a956
MN
79 int log;
80
81 OPEN_READER(re, gb);
82 UPDATE_CACHE(re, gb);
4704097a 83 buf=GET_CACHE(re, gb);
115329f1 84
4704097a
MN
85 if(buf&0xAA800000){
86 buf >>= 32 - 8;
87 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
88 CLOSE_READER(re, gb);
115329f1 89
4704097a
MN
90 return ff_interleaved_ue_golomb_vlc_code[buf];
91 }else{
cceb990e
MN
92 LAST_SKIP_BITS(re, gb, 8);
93 UPDATE_CACHE(re, gb);
94 buf |= 1 | (GET_CACHE(re, gb) >> 8);
95
4704097a
MN
96 if((buf & 0xAAAAAAAA) == 0)
97 return INVALID_VLC;
8b82a956 98
4704097a
MN
99 for(log=31; (buf & 0x80000000) == 0; log--){
100 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
101 }
8b82a956 102
cceb990e 103 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
4704097a 104 CLOSE_READER(re, gb);
8b82a956 105
4704097a
MN
106 return ((buf << log) >> log) - 1;
107 }
8b82a956
MN
108}
109
0da71265
MN
110/**
111 * read unsigned truncated exp golomb code.
112 */
113static inline int get_te0_golomb(GetBitContext *gb, int range){
114 assert(range >= 1);
115329f1 115
0da71265 116 if(range==1) return 0;
1924f3ce 117 else if(range==2) return get_bits1(gb)^1;
0da71265
MN
118 else return get_ue_golomb(gb);
119}
120
121/**
122 * read unsigned truncated exp golomb code.
123 */
124static inline int get_te_golomb(GetBitContext *gb, int range){
125 assert(range >= 1);
115329f1 126
1924f3ce 127 if(range==2) return get_bits1(gb)^1;
0da71265
MN
128 else return get_ue_golomb(gb);
129}
130
131
132/**
133 * read signed exp golomb code.
134 */
135static inline int get_se_golomb(GetBitContext *gb){
136 unsigned int buf;
137 int log;
115329f1 138
0da71265
MN
139 OPEN_READER(re, gb);
140 UPDATE_CACHE(re, gb);
141 buf=GET_CACHE(re, gb);
115329f1 142
0da71265
MN
143 if(buf >= (1<<27)){
144 buf >>= 32 - 9;
145 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
146 CLOSE_READER(re, gb);
115329f1 147
0da71265
MN
148 return ff_se_golomb_vlc_code[buf];
149 }else{
150 log= 2*av_log2(buf) - 31;
151 buf>>= log;
115329f1 152
0da71265
MN
153 LAST_SKIP_BITS(re, gb, 32 - log);
154 CLOSE_READER(re, gb);
115329f1 155
0da71265
MN
156 if(buf&1) buf= -(buf>>1);
157 else buf= (buf>>1);
158
159 return buf;
160 }
161}
162
8b82a956
MN
163static inline int svq3_get_se_golomb(GetBitContext *gb){
164 unsigned int buf;
165 int log;
166
167 OPEN_READER(re, gb);
168 UPDATE_CACHE(re, gb);
4704097a 169 buf=GET_CACHE(re, gb);
8b82a956 170
4704097a
MN
171 if(buf&0xAA800000){
172 buf >>= 32 - 8;
173 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
174 CLOSE_READER(re, gb);
115329f1 175
4704097a
MN
176 return ff_interleaved_se_golomb_vlc_code[buf];
177 }else{
895345da
MN
178 LAST_SKIP_BITS(re, gb, 8);
179 UPDATE_CACHE(re, gb);
180 buf |= 1 | (GET_CACHE(re, gb) >> 8);
181
4704097a
MN
182 if((buf & 0xAAAAAAAA) == 0)
183 return INVALID_VLC;
8b82a956 184
4704097a
MN
185 for(log=31; (buf & 0x80000000) == 0; log--){
186 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
187 }
8b82a956 188
895345da 189 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
4704097a 190 CLOSE_READER(re, gb);
8b82a956 191
4704097a
MN
192 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
193 }
8b82a956
MN
194}
195
11e659c2 196/**
d9e6a6c6 197 * read unsigned golomb rice code (ffv1).
11e659c2
MN
198 */
199static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
200 unsigned int buf;
201 int log;
115329f1 202
11e659c2
MN
203 OPEN_READER(re, gb);
204 UPDATE_CACHE(re, gb);
205 buf=GET_CACHE(re, gb);
206
207 log= av_log2(buf);
d9e6a6c6 208
11e659c2
MN
209 if(log > 31-limit){
210 buf >>= log - k;
211 buf += (30-log)<<k;
212 LAST_SKIP_BITS(re, gb, 32 + k - log);
213 CLOSE_READER(re, gb);
115329f1 214
11e659c2 215 return buf;
d9e6a6c6
MN
216 }else{
217 buf >>= 32 - limit - esc_len;
218 LAST_SKIP_BITS(re, gb, esc_len + limit);
11e659c2 219 CLOSE_READER(re, gb);
115329f1 220
d9e6a6c6
MN
221 return buf + limit - 1;
222 }
223}
224
225/**
226 * read unsigned golomb rice code (jpegls).
227 */
228static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
229 unsigned int buf;
230 int log;
115329f1 231
d9e6a6c6
MN
232 OPEN_READER(re, gb);
233 UPDATE_CACHE(re, gb);
234 buf=GET_CACHE(re, gb);
235
236 log= av_log2(buf);
115329f1 237
ce329952 238 if(log > 31-11){
d9e6a6c6
MN
239 buf >>= log - k;
240 buf += (30-log)<<k;
241 LAST_SKIP_BITS(re, gb, 32 + k - log);
242 CLOSE_READER(re, gb);
115329f1 243
d9e6a6c6
MN
244 return buf;
245 }else{
246 int i;
247 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
248 LAST_SKIP_BITS(re, gb, 1);
249 UPDATE_CACHE(re, gb);
250 }
251 SKIP_BITS(re, gb, 1);
252
253 if(i < limit - 1){
254 if(k){
255 buf = SHOW_UBITS(re, gb, k);
256 LAST_SKIP_BITS(re, gb, k);
257 }else{
258 buf=0;
259 }
260
261 CLOSE_READER(re, gb);
262 return buf + (i<<k);
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);
115329f1 267
d9e6a6c6
MN
268 return buf + 1;
269 }else
270 return -1;
271 }
11e659c2
MN
272}
273
ac2570a8 274/**
4579adb0
AB
275 * read signed golomb rice code (ffv1).
276 */
a6c01275 277static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
4579adb0 278 int v= get_ur_golomb(gb, k, limit, esc_len);
115329f1 279
4579adb0
AB
280 v++;
281 if (v&1) return v>>1;
282 else return -(v>>1);
115329f1 283
4579adb0
AB
284// return (v>>1) ^ -(v&1);
285}
4579adb0 286
a6c01275 287/**
4579adb0 288 * read signed golomb rice code (flac).
ac2570a8
MN
289 */
290static 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);
293}
294
85ad5695
MN
295/**
296 * read unsigned golomb rice code (shorten).
297 */
298static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
bb270c08 299 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
85ad5695
MN
300}
301
302/**
303 * read signed golomb rice code (shorten).
304 */
305static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
306{
307 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
308 if (uvar & 1)
309 return ~(uvar >> 1);
310 else
311 return uvar >> 1;
312}
313
314
315
0da71265
MN
316#ifdef TRACE
317
61f040dd 318static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
0da71265
MN
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);
115329f1 324
0da71265 325 print_bin(bits, len);
115329f1 326
4579adb0 327 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
115329f1 328
0da71265
MN
329 return i;
330}
331
61f040dd 332static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
0da71265
MN
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);
115329f1 338
0da71265 339 print_bin(bits, len);
115329f1 340
4579adb0 341 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
115329f1 342
0da71265
MN
343 return i;
344}
345
61f040dd 346static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
0da71265
MN
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);
115329f1 352
0da71265 353 print_bin(bits, len);
115329f1 354
4579adb0 355 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
115329f1 356
0da71265
MN
357 return i;
358}
359
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__)
364
365#endif
366
367/**
368 * write unsigned exp golomb code.
369 */
370static inline void set_ue_golomb(PutBitContext *pb, int i){
371 int e;
115329f1 372
0da71265
MN
373 assert(i>=0);
374
375#if 0
376 if(i=0){
377 put_bits(pb, 1, 1);
378 return;
379 }
380#endif
381 if(i<256)
382 put_bits(pb, ff_ue_golomb_len[i], i+1);
383 else{
384 e= av_log2(i+1);
115329f1 385
0da71265
MN
386 put_bits(pb, 2*e+1, i+1);
387 }
388}
389
390/**
391 * write truncated unsigned exp golomb code.
392 */
393static inline void set_te_golomb(PutBitContext *pb, int i, int range){
394 assert(range >= 1);
395 assert(i<=range);
396
1924f3ce 397 if(range==2) put_bits(pb, 1, i^1);
0da71265
MN
398 else set_ue_golomb(pb, i);
399}
400
401/**
e134e10f 402 * write signed exp golomb code. 16 bits at most.
0da71265
MN
403 */
404static inline void set_se_golomb(PutBitContext *pb, int i){
e134e10f 405// if (i>32767 || i<-32767)
bb270c08 406// av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
115329f1 407#if 0
0da71265
MN
408 if(i<=0) i= -2*i;
409 else i= 2*i-1;
410#elif 1
411 i= 2*i-1;
412 if(i<0) i^= -1; //FIXME check if gcc does the right thing
413#else
414 i= 2*i-1;
415 i^= (i>>31);
416#endif
417 set_ue_golomb(pb, i);
418}
11e659c2
MN
419
420/**
d9e6a6c6 421 * write unsigned golomb rice code (ffv1).
11e659c2
MN
422 */
423static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
424 int e;
115329f1 425
11e659c2 426 assert(i>=0);
115329f1 427
11e659c2
MN
428 e= i>>k;
429 if(e<limit){
430 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
431 }else{
d9e6a6c6
MN
432 put_bits(pb, limit + esc_len, i - limit + 1);
433 }
434}
435
436/**
437 * write unsigned golomb rice code (jpegls).
438 */
439static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
440 int e;
115329f1 441
d9e6a6c6 442 assert(i>=0);
115329f1 443
d9e6a6c6
MN
444 e= (i>>k) + 1;
445 if(e<limit){
9e96ab03
MN
446 while(e > 31) {
447 put_bits(pb, 31, 0);
448 e -= 31;
449 }
d9e6a6c6 450 put_bits(pb, e, 1);
ce329952
MN
451 if(k)
452 put_bits(pb, k, i&((1<<k)-1));
d9e6a6c6 453 }else{
76aca7a8
KS
454 while(limit > 31) {
455 put_bits(pb, 31, 0);
456 limit -= 31;
457 }
d9e6a6c6
MN
458 put_bits(pb, limit , 1);
459 put_bits(pb, esc_len, i - 1);
11e659c2
MN
460 }
461}
4579adb0
AB
462
463/**
464 * write signed golomb rice code (ffv1).
465 */
a6c01275 466static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
4579adb0
AB
467 int v;
468
469 v = -2*i-1;
470 v ^= (v>>31);
471
472 set_ur_golomb(pb, v, k, limit, esc_len);
473}
474
475/**
476 * write signed golomb rice code (flac).
477 */
478static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
479 int v;
480
481 v = -2*i-1;
482 v ^= (v>>31);
483
484 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
485}
699b3f99
MR
486
487#endif