get/set_sr_golomb() cleanup
[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
MN
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/**
23 * @file golomb.h
24 * @brief
25 * exp golomb vlc stuff
4579adb0 26 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
0da71265
MN
27 */
28
8b82a956
MN
29#define INVALID_VLC 0x80000000
30
0da71265
MN
31extern const uint8_t ff_golomb_vlc_len[512];
32extern const uint8_t ff_ue_golomb_vlc_code[512];
33extern const int8_t ff_se_golomb_vlc_code[512];
34extern const uint8_t ff_ue_golomb_len[256];
35
4704097a
MN
36extern const uint8_t ff_interleaved_golomb_vlc_len[256];
37extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
38extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
39
0da71265
MN
40
41 /**
42 * read unsigned exp golomb code.
43 */
44static inline int get_ue_golomb(GetBitContext *gb){
45 unsigned int buf;
46 int log;
47
48 OPEN_READER(re, gb);
49 UPDATE_CACHE(re, gb);
50 buf=GET_CACHE(re, gb);
51
52 if(buf >= (1<<27)){
53 buf >>= 32 - 9;
54 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
55 CLOSE_READER(re, gb);
56
57 return ff_ue_golomb_vlc_code[buf];
58 }else{
59 log= 2*av_log2(buf) - 31;
60 buf>>= log;
61 buf--;
62 LAST_SKIP_BITS(re, gb, 32 - log);
63 CLOSE_READER(re, gb);
64
65 return buf;
66 }
67}
68
8b82a956 69static inline int svq3_get_ue_golomb(GetBitContext *gb){
4704097a 70 uint32_t buf;
8b82a956
MN
71 int log;
72
73 OPEN_READER(re, gb);
74 UPDATE_CACHE(re, gb);
4704097a
MN
75 buf=GET_CACHE(re, gb);
76
77 if(buf&0xAA800000){
78 buf >>= 32 - 8;
79 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
80 CLOSE_READER(re, gb);
81
82 return ff_interleaved_ue_golomb_vlc_code[buf];
83 }else{
cceb990e
MN
84 LAST_SKIP_BITS(re, gb, 8);
85 UPDATE_CACHE(re, gb);
86 buf |= 1 | (GET_CACHE(re, gb) >> 8);
87
4704097a
MN
88 if((buf & 0xAAAAAAAA) == 0)
89 return INVALID_VLC;
8b82a956 90
4704097a
MN
91 for(log=31; (buf & 0x80000000) == 0; log--){
92 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
93 }
8b82a956 94
cceb990e 95 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
4704097a 96 CLOSE_READER(re, gb);
8b82a956 97
4704097a
MN
98 return ((buf << log) >> log) - 1;
99 }
8b82a956
MN
100}
101
0da71265
MN
102/**
103 * read unsigned truncated exp golomb code.
104 */
105static inline int get_te0_golomb(GetBitContext *gb, int range){
106 assert(range >= 1);
107
108 if(range==1) return 0;
1924f3ce 109 else if(range==2) return get_bits1(gb)^1;
0da71265
MN
110 else return get_ue_golomb(gb);
111}
112
113/**
114 * read unsigned truncated exp golomb code.
115 */
116static inline int get_te_golomb(GetBitContext *gb, int range){
117 assert(range >= 1);
118
1924f3ce 119 if(range==2) return get_bits1(gb)^1;
0da71265
MN
120 else return get_ue_golomb(gb);
121}
122
123
124/**
125 * read signed exp golomb code.
126 */
127static inline int get_se_golomb(GetBitContext *gb){
128 unsigned int buf;
129 int log;
130
131 OPEN_READER(re, gb);
132 UPDATE_CACHE(re, gb);
133 buf=GET_CACHE(re, gb);
134
135 if(buf >= (1<<27)){
136 buf >>= 32 - 9;
137 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
138 CLOSE_READER(re, gb);
139
140 return ff_se_golomb_vlc_code[buf];
141 }else{
142 log= 2*av_log2(buf) - 31;
143 buf>>= log;
144
145 LAST_SKIP_BITS(re, gb, 32 - log);
146 CLOSE_READER(re, gb);
147
148 if(buf&1) buf= -(buf>>1);
149 else buf= (buf>>1);
150
151 return buf;
152 }
153}
154
8b82a956
MN
155static inline int svq3_get_se_golomb(GetBitContext *gb){
156 unsigned int buf;
157 int log;
158
159 OPEN_READER(re, gb);
160 UPDATE_CACHE(re, gb);
4704097a 161 buf=GET_CACHE(re, gb);
8b82a956 162
4704097a
MN
163 if(buf&0xAA800000){
164 buf >>= 32 - 8;
165 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
166 CLOSE_READER(re, gb);
167
168 return ff_interleaved_se_golomb_vlc_code[buf];
169 }else{
170 buf |=1;
171 if((buf & 0xAAAAAAAA) == 0)
172 return INVALID_VLC;
8b82a956 173
4704097a
MN
174 for(log=31; (buf & 0x80000000) == 0; log--){
175 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
176 }
8b82a956 177
4704097a
MN
178 LAST_SKIP_BITS(re, gb, 63 - 2*log);
179 CLOSE_READER(re, gb);
8b82a956 180
4704097a
MN
181 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
182 }
8b82a956
MN
183}
184
11e659c2 185/**
d9e6a6c6 186 * read unsigned golomb rice code (ffv1).
11e659c2
MN
187 */
188static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
189 unsigned int buf;
190 int log;
191
192 OPEN_READER(re, gb);
193 UPDATE_CACHE(re, gb);
194 buf=GET_CACHE(re, gb);
195
196 log= av_log2(buf);
d9e6a6c6 197
11e659c2
MN
198 if(log > 31-limit){
199 buf >>= log - k;
200 buf += (30-log)<<k;
201 LAST_SKIP_BITS(re, gb, 32 + k - log);
202 CLOSE_READER(re, gb);
203
204 return buf;
d9e6a6c6
MN
205 }else{
206 buf >>= 32 - limit - esc_len;
207 LAST_SKIP_BITS(re, gb, esc_len + limit);
11e659c2
MN
208 CLOSE_READER(re, gb);
209
d9e6a6c6
MN
210 return buf + limit - 1;
211 }
212}
213
214/**
215 * read unsigned golomb rice code (jpegls).
216 */
217static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
218 unsigned int buf;
219 int log;
220
221 OPEN_READER(re, gb);
222 UPDATE_CACHE(re, gb);
223 buf=GET_CACHE(re, gb);
224
225 log= av_log2(buf);
226
ce329952 227 if(log > 31-11){
d9e6a6c6
MN
228 buf >>= log - k;
229 buf += (30-log)<<k;
230 LAST_SKIP_BITS(re, gb, 32 + k - log);
231 CLOSE_READER(re, gb);
232
233 return buf;
234 }else{
235 int i;
236 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
237 LAST_SKIP_BITS(re, gb, 1);
238 UPDATE_CACHE(re, gb);
239 }
240 SKIP_BITS(re, gb, 1);
241
242 if(i < limit - 1){
243 if(k){
244 buf = SHOW_UBITS(re, gb, k);
245 LAST_SKIP_BITS(re, gb, k);
246 }else{
247 buf=0;
248 }
249
250 CLOSE_READER(re, gb);
251 return buf + (i<<k);
252 }else if(i == limit - 1){
253 buf = SHOW_UBITS(re, gb, esc_len);
254 LAST_SKIP_BITS(re, gb, esc_len);
255 CLOSE_READER(re, gb);
256
257 return buf + 1;
258 }else
259 return -1;
260 }
11e659c2
MN
261}
262
ac2570a8 263/**
4579adb0
AB
264 * read signed golomb rice code (ffv1).
265 */
a6c01275 266static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
4579adb0
AB
267 int v= get_ur_golomb(gb, k, limit, esc_len);
268
269 v++;
270 if (v&1) return v>>1;
271 else return -(v>>1);
272
273// return (v>>1) ^ -(v&1);
274}
4579adb0 275
a6c01275 276/**
4579adb0 277 * read signed golomb rice code (flac).
ac2570a8
MN
278 */
279static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
280 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
281 return (v>>1) ^ -(v&1);
282}
283
0da71265
MN
284#ifdef TRACE
285
286static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
287 int show= show_bits(s, 24);
288 int pos= get_bits_count(s);
289 int i= get_ue_golomb(s);
290 int len= get_bits_count(s) - pos;
291 int bits= show>>(24-len);
292
293 print_bin(bits, len);
294
4579adb0 295 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
0da71265
MN
296
297 return i;
298}
299
300static inline int get_se(GetBitContext *s, char *file, char *func, int line){
301 int show= show_bits(s, 24);
302 int pos= get_bits_count(s);
303 int i= get_se_golomb(s);
304 int len= get_bits_count(s) - pos;
305 int bits= show>>(24-len);
306
307 print_bin(bits, len);
308
4579adb0 309 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
0da71265
MN
310
311 return i;
312}
313
314static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
315 int show= show_bits(s, 24);
316 int pos= get_bits_count(s);
317 int i= get_te0_golomb(s, r);
318 int len= get_bits_count(s) - pos;
319 int bits= show>>(24-len);
320
321 print_bin(bits, len);
322
4579adb0 323 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
0da71265
MN
324
325 return i;
326}
327
328#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
329#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
330#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
331#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
332
333#endif
334
335/**
336 * write unsigned exp golomb code.
337 */
338static inline void set_ue_golomb(PutBitContext *pb, int i){
339 int e;
340
341 assert(i>=0);
342
343#if 0
344 if(i=0){
345 put_bits(pb, 1, 1);
346 return;
347 }
348#endif
349 if(i<256)
350 put_bits(pb, ff_ue_golomb_len[i], i+1);
351 else{
352 e= av_log2(i+1);
353
354 put_bits(pb, 2*e+1, i+1);
355 }
356}
357
358/**
359 * write truncated unsigned exp golomb code.
360 */
361static inline void set_te_golomb(PutBitContext *pb, int i, int range){
362 assert(range >= 1);
363 assert(i<=range);
364
1924f3ce 365 if(range==2) put_bits(pb, 1, i^1);
0da71265
MN
366 else set_ue_golomb(pb, i);
367}
368
369/**
370 * write signed exp golomb code.
371 */
372static inline void set_se_golomb(PutBitContext *pb, int i){
373#if 0
374 if(i<=0) i= -2*i;
375 else i= 2*i-1;
376#elif 1
377 i= 2*i-1;
378 if(i<0) i^= -1; //FIXME check if gcc does the right thing
379#else
380 i= 2*i-1;
381 i^= (i>>31);
382#endif
383 set_ue_golomb(pb, i);
384}
11e659c2
MN
385
386/**
d9e6a6c6 387 * write unsigned golomb rice code (ffv1).
11e659c2
MN
388 */
389static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
390 int e;
391
392 assert(i>=0);
393
394 e= i>>k;
395 if(e<limit){
396 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
397 }else{
d9e6a6c6
MN
398 put_bits(pb, limit + esc_len, i - limit + 1);
399 }
400}
401
402/**
403 * write unsigned golomb rice code (jpegls).
404 */
405static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
406 int e;
407
408 assert(i>=0);
409
410 e= (i>>k) + 1;
411 if(e<limit){
412 put_bits(pb, e, 1);
ce329952
MN
413 if(k)
414 put_bits(pb, k, i&((1<<k)-1));
d9e6a6c6
MN
415 }else{
416 put_bits(pb, limit , 1);
417 put_bits(pb, esc_len, i - 1);
11e659c2
MN
418 }
419}
4579adb0
AB
420
421/**
422 * write signed golomb rice code (ffv1).
423 */
a6c01275 424static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
4579adb0
AB
425 int v;
426
427 v = -2*i-1;
428 v ^= (v>>31);
429
430 set_ur_golomb(pb, v, k, limit, esc_len);
431}
432
433/**
434 * write signed golomb rice code (flac).
435 */
436static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
437 int v;
438
439 v = -2*i-1;
440 v ^= (v>>31);
441
442 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
443}