b4382d236bf5ed22c5fa8e6553d743c02334c50a
[libav.git] / libavcodec / golomb.h
1 /*
2 * exp golomb vlc stuff
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24 /**
25 * @file golomb.h
26 * @brief
27 * exp golomb vlc stuff
28 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
29 */
30
31 #include <stdint.h>
32 #include "bitstream.h"
33
34 #define INVALID_VLC 0x80000000
35
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];
40
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];
44
45
46 /**
47 * read unsigned exp golomb code.
48 */
49 static inline int get_ue_golomb(GetBitContext *gb){
50 unsigned int buf;
51 int log;
52
53 OPEN_READER(re, gb);
54 UPDATE_CACHE(re, gb);
55 buf=GET_CACHE(re, gb);
56
57 if(buf >= (1<<27)){
58 buf >>= 32 - 9;
59 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
60 CLOSE_READER(re, gb);
61
62 return ff_ue_golomb_vlc_code[buf];
63 }else{
64 log= 2*av_log2(buf) - 31;
65 buf>>= log;
66 buf--;
67 LAST_SKIP_BITS(re, gb, 32 - log);
68 CLOSE_READER(re, gb);
69
70 return buf;
71 }
72 }
73
74 static inline int svq3_get_ue_golomb(GetBitContext *gb){
75 uint32_t buf;
76 int log;
77
78 OPEN_READER(re, gb);
79 UPDATE_CACHE(re, gb);
80 buf=GET_CACHE(re, gb);
81
82 if(buf&0xAA800000){
83 buf >>= 32 - 8;
84 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
85 CLOSE_READER(re, gb);
86
87 return ff_interleaved_ue_golomb_vlc_code[buf];
88 }else{
89 LAST_SKIP_BITS(re, gb, 8);
90 UPDATE_CACHE(re, gb);
91 buf |= 1 | (GET_CACHE(re, gb) >> 8);
92
93 if((buf & 0xAAAAAAAA) == 0)
94 return INVALID_VLC;
95
96 for(log=31; (buf & 0x80000000) == 0; log--){
97 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
98 }
99
100 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
101 CLOSE_READER(re, gb);
102
103 return ((buf << log) >> log) - 1;
104 }
105 }
106
107 /**
108 * read unsigned truncated exp golomb code.
109 */
110 static inline int get_te0_golomb(GetBitContext *gb, int range){
111 assert(range >= 1);
112
113 if(range==1) return 0;
114 else if(range==2) return get_bits1(gb)^1;
115 else return get_ue_golomb(gb);
116 }
117
118 /**
119 * read unsigned truncated exp golomb code.
120 */
121 static inline int get_te_golomb(GetBitContext *gb, int range){
122 assert(range >= 1);
123
124 if(range==2) return get_bits1(gb)^1;
125 else return get_ue_golomb(gb);
126 }
127
128
129 /**
130 * read signed exp golomb code.
131 */
132 static inline int get_se_golomb(GetBitContext *gb){
133 unsigned int buf;
134 int log;
135
136 OPEN_READER(re, gb);
137 UPDATE_CACHE(re, gb);
138 buf=GET_CACHE(re, gb);
139
140 if(buf >= (1<<27)){
141 buf >>= 32 - 9;
142 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
143 CLOSE_READER(re, gb);
144
145 return ff_se_golomb_vlc_code[buf];
146 }else{
147 log= 2*av_log2(buf) - 31;
148 buf>>= log;
149
150 LAST_SKIP_BITS(re, gb, 32 - log);
151 CLOSE_READER(re, gb);
152
153 if(buf&1) buf= -(buf>>1);
154 else buf= (buf>>1);
155
156 return buf;
157 }
158 }
159
160 static inline int svq3_get_se_golomb(GetBitContext *gb){
161 unsigned int buf;
162 int log;
163
164 OPEN_READER(re, gb);
165 UPDATE_CACHE(re, gb);
166 buf=GET_CACHE(re, gb);
167
168 if(buf&0xAA800000){
169 buf >>= 32 - 8;
170 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
171 CLOSE_READER(re, gb);
172
173 return ff_interleaved_se_golomb_vlc_code[buf];
174 }else{
175 LAST_SKIP_BITS(re, gb, 8);
176 UPDATE_CACHE(re, gb);
177 buf |= 1 | (GET_CACHE(re, gb) >> 8);
178
179 if((buf & 0xAAAAAAAA) == 0)
180 return INVALID_VLC;
181
182 for(log=31; (buf & 0x80000000) == 0; log--){
183 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
184 }
185
186 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
187 CLOSE_READER(re, gb);
188
189 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
190 }
191 }
192
193 /**
194 * read unsigned golomb rice code (ffv1).
195 */
196 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
197 unsigned int buf;
198 int log;
199
200 OPEN_READER(re, gb);
201 UPDATE_CACHE(re, gb);
202 buf=GET_CACHE(re, gb);
203
204 log= av_log2(buf);
205
206 if(log > 31-limit){
207 buf >>= log - k;
208 buf += (30-log)<<k;
209 LAST_SKIP_BITS(re, gb, 32 + k - log);
210 CLOSE_READER(re, gb);
211
212 return buf;
213 }else{
214 buf >>= 32 - limit - esc_len;
215 LAST_SKIP_BITS(re, gb, esc_len + limit);
216 CLOSE_READER(re, gb);
217
218 return buf + limit - 1;
219 }
220 }
221
222 /**
223 * read unsigned golomb rice code (jpegls).
224 */
225 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
226 unsigned int buf;
227 int log;
228
229 OPEN_READER(re, gb);
230 UPDATE_CACHE(re, gb);
231 buf=GET_CACHE(re, gb);
232
233 log= av_log2(buf);
234
235 if(log > 31-11){
236 buf >>= log - k;
237 buf += (30-log)<<k;
238 LAST_SKIP_BITS(re, gb, 32 + k - log);
239 CLOSE_READER(re, gb);
240
241 return buf;
242 }else{
243 int i;
244 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
245 LAST_SKIP_BITS(re, gb, 1);
246 UPDATE_CACHE(re, gb);
247 }
248 SKIP_BITS(re, gb, 1);
249
250 if(i < limit - 1){
251 if(k){
252 buf = SHOW_UBITS(re, gb, k);
253 LAST_SKIP_BITS(re, gb, k);
254 }else{
255 buf=0;
256 }
257
258 CLOSE_READER(re, gb);
259 return buf + (i<<k);
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);
264
265 return buf + 1;
266 }else
267 return -1;
268 }
269 }
270
271 /**
272 * read signed golomb rice code (ffv1).
273 */
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);
276
277 v++;
278 if (v&1) return v>>1;
279 else return -(v>>1);
280
281 // return (v>>1) ^ -(v&1);
282 }
283
284 /**
285 * read signed golomb rice code (flac).
286 */
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);
290 }
291
292 /**
293 * read unsigned golomb rice code (shorten).
294 */
295 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
296 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
297 }
298
299 /**
300 * read signed golomb rice code (shorten).
301 */
302 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
303 {
304 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
305 if (uvar & 1)
306 return ~(uvar >> 1);
307 else
308 return uvar >> 1;
309 }
310
311
312
313 #ifdef TRACE
314
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);
321
322 print_bin(bits, len);
323
324 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
325
326 return i;
327 }
328
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);
335
336 print_bin(bits, len);
337
338 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
339
340 return i;
341 }
342
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);
349
350 print_bin(bits, len);
351
352 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
353
354 return i;
355 }
356
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__)
361
362 #endif
363
364 /**
365 * write unsigned exp golomb code.
366 */
367 static inline void set_ue_golomb(PutBitContext *pb, int i){
368 int e;
369
370 assert(i>=0);
371
372 #if 0
373 if(i=0){
374 put_bits(pb, 1, 1);
375 return;
376 }
377 #endif
378 if(i<256)
379 put_bits(pb, ff_ue_golomb_len[i], i+1);
380 else{
381 e= av_log2(i+1);
382
383 put_bits(pb, 2*e+1, i+1);
384 }
385 }
386
387 /**
388 * write truncated unsigned exp golomb code.
389 */
390 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
391 assert(range >= 1);
392 assert(i<=range);
393
394 if(range==2) put_bits(pb, 1, i^1);
395 else set_ue_golomb(pb, i);
396 }
397
398 /**
399 * write signed exp golomb code. 16 bits at most.
400 */
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);
404 #if 0
405 if(i<=0) i= -2*i;
406 else i= 2*i-1;
407 #elif 1
408 i= 2*i-1;
409 if(i<0) i^= -1; //FIXME check if gcc does the right thing
410 #else
411 i= 2*i-1;
412 i^= (i>>31);
413 #endif
414 set_ue_golomb(pb, i);
415 }
416
417 /**
418 * write unsigned golomb rice code (ffv1).
419 */
420 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
421 int e;
422
423 assert(i>=0);
424
425 e= i>>k;
426 if(e<limit){
427 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
428 }else{
429 put_bits(pb, limit + esc_len, i - limit + 1);
430 }
431 }
432
433 /**
434 * write unsigned golomb rice code (jpegls).
435 */
436 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
437 int e;
438
439 assert(i>=0);
440
441 e= (i>>k) + 1;
442 if(e<limit){
443 while(e > 31) {
444 put_bits(pb, 31, 0);
445 e -= 31;
446 }
447 put_bits(pb, e, 1);
448 if(k)
449 put_bits(pb, k, i&((1<<k)-1));
450 }else{
451 while(limit > 31) {
452 put_bits(pb, 31, 0);
453 limit -= 31;
454 }
455 put_bits(pb, limit , 1);
456 put_bits(pb, esc_len, i - 1);
457 }
458 }
459
460 /**
461 * write signed golomb rice code (ffv1).
462 */
463 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
464 int v;
465
466 v = -2*i-1;
467 v ^= (v>>31);
468
469 set_ur_golomb(pb, v, k, limit, esc_len);
470 }
471
472 /**
473 * write signed golomb rice code (flac).
474 */
475 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
476 int v;
477
478 v = -2*i-1;
479 v ^= (v>>31);
480
481 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
482 }