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>
34
37
38 #define INVALID_VLC 0x80000000
39
44
49
50 /**
51 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
52 */
54 {
56
60
61 if (buf >= (1 << 27)) {
62 buf >>= 32 - 9;
65
67 } else {
68 int log = 2 *
av_log2(buf) - 31;
71 if (log < 7) {
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 */
101 {
103
107
108 buf >>= 32 - 9;
111
113 }
114
116 {
118
122
123 if (buf & 0xAA800000) {
124 buf >>= 32 - 8;
127
129 } else {
130 unsigned ret = 1;
131
132 do {
133 buf >>= 32 - 8;
136
140 break;
141 }
146
148 return ret - 1;
149 }
150 }
151
152 /**
153 * read unsigned truncated exp golomb code.
154 */
156 {
158
159 if (range == 1)
160 return 0;
161 else if (range == 2)
163 else
165 }
166
167 /**
168 * read unsigned truncated exp golomb code.
169 */
171 {
173
174 if (range == 2)
176 else
178 }
179
180 /**
181 * read signed exp golomb code.
182 */
184 {
186
190
191 if (buf >= (1 << 27)) {
192 buf >>= 32 - 9;
195
197 } else {
202
203 buf >>= log;
204
207
208 sign = -(buf & 1);
209 buf = ((buf >> 1) ^ sign) - sign;
210
212 }
213 }
214
216 {
218 int sign = (buf & 1) - 1;
219 return ((buf >> 1) ^ sign) + 1;
220 }
221
223 {
225
229
230 if (buf & 0xAA800000) {
231 buf >>= 32 - 8;
234
236 } else {
237 int log;
241
242 if ((buf & 0xAAAAAAAA) == 0)
244
245 for (log = 31; (buf & 0x80000000) == 0; log--)
246 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
247
250
251 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
252 }
253 }
254
256 {
258
259 if (ret) {
261 ret = (ret ^ sign) - sign;
262 }
263
264 return ret;
265 }
266
267 /**
268 * read unsigned golomb rice code (ffv1).
269 */
271 int esc_len)
272 {
274 int log;
275
279
281
282 if (log > 31 - limit) {
283 buf >>= log - k;
284 buf += (30
U - log) << k;
287
289 } else {
292
294
297
298 return buf + limit - 1;
299 }
300 }
301
302 /**
303 * read unsigned golomb rice code (jpegls).
304 */
306 int esc_len)
307 {
309 int log;
310
314
316
318
320 32 - log < limit) {
321 buf >>= log - k;
322 buf += (30
U - log) << k;
325
327 } else {
328 int i;
329 for (i = 0; i < limit &&
SHOW_UBITS(
re, gb, 1) == 0; i++) {
332 return -1;
333 }
336 }
338
339 if (i < limit - 1) {
340 if (k) {
347 } else {
350 }
351 } else {
352 buf = 0;
353 }
354
355 buf += ((
SUINT)i << k);
356 } else if (i == limit - 1) {
359
360 buf ++;
361 } else {
362 buf = -1;
363 }
366 }
367 }
368
369 /**
370 * read signed golomb rice code (ffv1).
371 */
373 int esc_len)
374 {
376 return (v >> 1) ^ -(v & 1);
377 }
378
379 /**
380 * read signed golomb rice code (flac).
381 */
383 int esc_len)
384 {
386 return (v >> 1) ^ -(v & 1);
387 }
388
389 /**
390 * read unsigned golomb rice code (shorten).
391 */
393 {
395 }
396
397 /**
398 * read signed golomb rice code (shorten).
399 */
401 {
403 return (uvar >> 1) ^ -(uvar & 1);
404 }
405
406 #ifdef TRACE
407
410 {
416
418 bits, len, i, pos, file, func, line);
419
420 return i;
421 }
422
425 {
431
433 bits, len, i, pos, file, func, line);
434
435 return i;
436 }
437
440 {
446
448 bits, len, i, pos, file, func, line);
449
450 return i;
451 }
452
453 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
454 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
455 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
456 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
457
458 #endif /* TRACE */
459
460 /**
461 * write unsigned exp golomb code. 2^16 - 2 at most
462 */
464 {
467
468 if (i < 256)
470 else {
473 }
474 }
475
476 /**
477 * write unsigned exp golomb code. 2^32-2 at most.
478 */
480 {
482
483 if (i < 256)
485 else {
488 }
489 }
490
491 /**
492 * write truncated unsigned exp golomb code.
493 */
495 {
498
499 if (range == 2)
501 else
503 }
504
505 /**
506 * write signed exp golomb code. 16 bits at most.
507 */
509 {
510 i = 2 * i - 1;
511 if (i < 0)
512 i ^= -1; //FIXME check if gcc does the right thing
514 }
515
516 /**
517 * write unsigned golomb rice code (ffv1).
518 */
520 int esc_len)
521 {
522 int e;
523
525
526 e = i >> k;
527 if (e < limit)
528 put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
529 else
530 put_bits(pb, limit + esc_len, i - limit + 1);
531 }
532
533 /**
534 * write unsigned golomb rice code (jpegls).
535 */
537 int limit, int esc_len)
538 {
539 int e;
540
542
543 e = (i >> k) + 1;
544 if (e < limit) {
545 while (e > 31) {
547 e -= 31;
548 }
550 if (k)
552 } else {
553 while (limit > 31) {
555 limit -= 31;
556 }
559 }
560 }
561
562 /**
563 * write signed golomb rice code (ffv1).
564 */
566 int esc_len)
567 {
568 int v;
569
570 v = -2 * i - 1;
571 v ^= (v >> 31);
572
574 }
575
576 /**
577 * write signed golomb rice code (flac).
578 */
580 int limit, int esc_len)
581 {
582 int v;
583
584 v = -2 * i - 1;
585 v ^= (v >> 31);
586
588 }
589
590 #endif /* AVCODEC_GOLOMB_H */
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (flac).
static void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len)
write unsigned golomb rice code (ffv1).
#define BITS_AVAILABLE(name, gb)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
const uint8_t ff_ue_golomb_vlc_code[512]
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static void skip_bits_long(GetBitContext *s, int n)
static void set_ue_golomb(PutBitContext *pb, int i)
write unsigned exp golomb code.
const int8_t ff_interleaved_se_golomb_vlc_code[256]
const uint8_t ff_interleaved_golomb_vlc_len[256]
static int get_te0_golomb(GetBitContext *gb, int range)
read unsigned truncated exp golomb code.
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static void set_te_golomb(PutBitContext *pb, int i, int range)
write truncated unsigned exp golomb code.
static int dirac_get_se_golomb(GetBitContext *gb)
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
const uint8_t ff_golomb_vlc_len[512]
static int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (ffv1).
static void put_bits64(PutBitContext *s, int n, uint64_t value)
Write up to 64 bits into a bitstream.
#define UPDATE_CACHE(name, gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static int get_interleaved_se_golomb(GetBitContext *gb)
const int8_t ff_se_golomb_vlc_code[512]
#define CLOSE_READER(name, gb)
#define SKIP_BITS(name, gb, num)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define LAST_SKIP_BITS(name, gb, num)
static void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len)
write unsigned golomb rice code (jpegls).
#define SHOW_UBITS(name, gb, num)
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
static int get_se_golomb_long(GetBitContext *gb)
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
static void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
write unsigned exp golomb code.
#define OPEN_READER(name, gb)
static unsigned int get_bits1(GetBitContext *s)
const uint8_t ff_interleaved_ue_golomb_vlc_code[256]
static void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (ffv1).
#define GET_CACHE(name, gb)
int(* func)(AVBPrint *dst, const char *in, const char *arg)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int get_te_golomb(GetBitContext *gb, int range)
read unsigned truncated exp golomb code.
const uint8_t ff_ue_golomb_len[256]
static unsigned get_interleaved_ue_golomb(GetBitContext *gb)
static unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
read unsigned golomb rice code (shorten).
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
static int get_sr_golomb_shorten(GetBitContext *gb, int k)
read signed golomb rice code (shorten).