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 * @file
25 * @brief
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
28 */
29
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
32
33 #include <stdint.h>
36
37 #define INVALID_VLC 0x80000000
38
43
48
49
50 /**
51 * read unsigned exp golomb code.
52 */
55 int log;
56
60
61 if(buf >= (1<<27)){
62 buf >>= 32 - 9;
65
67 }else{
71 if (CONFIG_FTRAPV && log < 0) {
74 }
75 buf>>= log;
76 buf--;
77
79 }
80 }
81
82 /**
83 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
84 */
86 {
88
92
94 }
95
96 /**
97 * read unsigned exp golomb code, constraint to a max of 31.
98 * the return value is undefined if the stored value exceeds 31.
99 */
102
106
107 buf >>= 32 - 9;
110
112 }
113
115 {
117
121
122 if(buf&0xAA800000){
123 buf >>= 32 - 8;
126
128 }else{
130
131 do {
132 buf >>= 32 - 8;
134
138 break;
139 }
144
146 return ret - 1;
147 }
148 }
149
150 /**
151 * read unsigned truncated exp golomb code.
152 */
155
156 if(range==1) return 0;
157 else if(range==2)
return get_bits1(gb)^1;
159 }
160
161 /**
162 * read unsigned truncated exp golomb code.
163 */
166
169 }
170
171
172 /**
173 * read signed exp golomb code.
174 */
177 int log;
178
182
183 if(buf >= (1<<27)){
184 buf >>= 32 - 9;
187
189 }else{
194
195 buf>>= log;
196
199
200 if(buf&1) buf= -(buf>>1);
201 else buf= (buf>>1);
202
204 }
205 }
206
209 int log;
210
214
215 if(buf&0xAA800000){
216 buf >>= 32 - 8;
219
221 }else{
225
226 if((buf & 0xAAAAAAAA) == 0)
228
229 for(log=31; (buf & 0x80000000) == 0; log--){
230 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
231 }
232
235
236 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
237 }
238 }
239
243
245
246 if (ret) {
251 ret = (ret ^
buf) - buf;
253 }
254
256 }
257
258 /**
259 * read unsigned golomb rice code (ffv1).
260 */
263 int log;
264
268
270
271 if(log > 31-limit){
272 buf >>= log - k;
273 buf += (30-log)<<k;
276
278 }else{
281
283
286
287 return buf + limit - 1;
288 }
289 }
290
291 /**
292 * read unsigned golomb rice code (jpegls).
293 */
296 int log;
297
301
303
305 buf >>= log - k;
306 buf += (30-log)<<k;
309
311 }else{
312 int i;
313 for (i = 0; i < limit &&
SHOW_UBITS(
re, gb, 1) == 0; i++) {
315 return -1;
318 }
320
321 if(i < limit - 1){
322 if(k){
325 }else{
326 buf=0;
327 }
328
330 return buf + (i<<k);
331 }else if(i == limit - 1){
335
336 return buf + 1;
337 }else
338 return -1;
339 }
340 }
341
342 /**
343 * read signed golomb rice code (ffv1).
344 */
347
348 v++;
349 if (v&1) return v>>1;
350 else return -(v>>1);
351
352 // return (v>>1) ^ -(v&1);
353 }
354
355 /**
356 * read signed golomb rice code (flac).
357 */
360 return (v>>1) ^ -(v&1);
361 }
362
363 /**
364 * read unsigned golomb rice code (shorten).
365 */
368 }
369
370 /**
371 * read signed golomb rice code (shorten).
372 */
374 {
376 if (uvar & 1)
377 return ~(uvar >> 1);
378 else
379 return uvar >> 1;
380 }
381
382
383
384 #ifdef TRACE
385
388 {
394
395 print_bin(bits, len);
396
397 av_log(NULL,
AV_LOG_DEBUG,
"%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
398
399 return i;
400 }
401
404 {
410
411 print_bin(bits, len);
412
413 av_log(NULL,
AV_LOG_DEBUG,
"%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
414
415 return i;
416 }
417
424
425 print_bin(bits, len);
426
427 av_log(NULL,
AV_LOG_DEBUG,
"%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
428
429 return i;
430 }
431
432 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
433 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
434 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
435 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
436
437 #endif
438
439 /**
440 * write unsigned exp golomb code.
441 */
443 int e;
444
446
447 #if 0
448 if(i=0){
450 return;
451 }
452 #endif
453 if(i<256)
455 else{
457
459 }
460 }
461
462 /**
463 * write truncated unsigned exp golomb code.
464 */
468
471 }
472
473 /**
474 * write signed exp golomb code. 16 bits at most.
475 */
477 #if 0
478 if(i<=0) i= -2*i;
479 else i= 2*i-1;
480 #elif 1
481 i= 2*i-1;
482 if(i<0) i^= -1; //FIXME check if gcc does the right thing
483 #else
484 i= 2*i-1;
485 i^= (i>>31);
486 #endif
488 }
489
490 /**
491 * write unsigned golomb rice code (ffv1).
492 */
494 int e;
495
497
498 e= i>>k;
499 if(e<limit){
500 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
501 }else{
502 put_bits(pb, limit + esc_len, i - limit + 1);
503 }
504 }
505
506 /**
507 * write unsigned golomb rice code (jpegls).
508 */
510 int e;
511
513
514 e= (i>>k) + 1;
515 if(e<limit){
516 while(e > 31) {
518 e -= 31;
519 }
521 if(k)
523 }else{
524 while(limit > 31) {
526 limit -= 31;
527 }
530 }
531 }
532
533 /**
534 * write signed golomb rice code (ffv1).
535 */
538
539 v = -2*i-1;
540 v ^= (v>>31);
541
543 }
544
545 /**
546 * write signed golomb rice code (flac).
547 */
550
551 v = -2*i-1;
552 v ^= (v>>31);
553
555 }
556
557 #endif /* AVCODEC_GOLOMB_H */