3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
6 * This file is part of FFmpeg.
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.
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.
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
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
38 #define INVALID_VLC 0x80000000
40 extern const uint8_t ff_golomb_vlc_len[512];
41 extern const uint8_t ff_ue_golomb_vlc_code[512];
42 extern const int8_t ff_se_golomb_vlc_code[512];
43 extern const uint8_t ff_ue_golomb_len[256];
45 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
46 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
47 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
48 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
51 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
53 * @returns the read value or a negative error code.
55 static inline int get_ue_golomb(GetBitContext *gb)
59 #if CACHED_BITSTREAM_READER
60 buf = show_bits_long(gb, 32);
62 if (buf >= (1 << 27)) {
64 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
66 return ff_ue_golomb_vlc_code[buf];
68 int log = 2 * av_log2(buf) - 31;
70 skip_bits_long(gb, 32 - log);
72 return AVERROR_INVALIDDATA;
81 buf = GET_CACHE(re, gb);
83 if (buf >= (1 << 27)) {
85 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
88 return ff_ue_golomb_vlc_code[buf];
90 int log = 2 * av_log2(buf) - 31;
91 LAST_SKIP_BITS(re, gb, 32 - log);
94 return AVERROR_INVALIDDATA;
104 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
106 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
110 buf = show_bits_long(gb, 32);
111 log = 31 - av_log2(buf);
112 skip_bits_long(gb, log);
114 return get_bits_long(gb, log + 1) - 1;
118 * read unsigned exp golomb code, constraint to a max of 31.
119 * If the value encountered is not in 0..31, the return value
120 * is outside the range 0..30.
122 static inline int get_ue_golomb_31(GetBitContext *gb)
126 #if CACHED_BITSTREAM_READER
127 buf = show_bits_long(gb, 32);
130 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
134 UPDATE_CACHE(re, gb);
135 buf = GET_CACHE(re, gb);
138 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
139 CLOSE_READER(re, gb);
142 return ff_ue_golomb_vlc_code[buf];
145 static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
149 #if CACHED_BITSTREAM_READER
150 buf = show_bits_long(gb, 32);
152 if (buf & 0xAA800000) {
154 skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
156 return ff_interleaved_ue_golomb_vlc_code[buf];
162 skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
164 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
165 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
166 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
169 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
170 buf = show_bits_long(gb, 32);
171 } while (get_bits_left(gb) > 0);
177 UPDATE_CACHE(re, gb);
178 buf = GET_CACHE(re, gb);
180 if (buf & 0xAA800000) {
182 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
183 CLOSE_READER(re, gb);
185 return ff_interleaved_ue_golomb_vlc_code[buf];
191 LAST_SKIP_BITS(re, gb,
192 FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
194 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
195 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
196 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
199 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
200 UPDATE_CACHE(re, gb);
201 buf = GET_CACHE(re, gb);
202 } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
204 CLOSE_READER(re, gb);
211 * read unsigned truncated exp golomb code.
213 static inline int get_te0_golomb(GetBitContext *gb, int range)
215 av_assert2(range >= 1);
220 return get_bits1(gb) ^ 1;
222 return get_ue_golomb(gb);
226 * read unsigned truncated exp golomb code.
228 static inline int get_te_golomb(GetBitContext *gb, int range)
230 av_assert2(range >= 1);
233 return get_bits1(gb) ^ 1;
235 return get_ue_golomb(gb);
239 * read signed exp golomb code.
241 static inline int get_se_golomb(GetBitContext *gb)
245 #if CACHED_BITSTREAM_READER
246 buf = show_bits_long(gb, 32);
248 if (buf >= (1 << 27)) {
250 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
252 return ff_se_golomb_vlc_code[buf];
254 int log = 2 * av_log2(buf) - 31;
257 skip_bits_long(gb, 32 - log);
268 UPDATE_CACHE(re, gb);
269 buf = GET_CACHE(re, gb);
271 if (buf >= (1 << 27)) {
273 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
274 CLOSE_READER(re, gb);
276 return ff_se_golomb_vlc_code[buf];
278 int log = av_log2(buf), sign;
279 LAST_SKIP_BITS(re, gb, 31 - log);
280 UPDATE_CACHE(re, gb);
281 buf = GET_CACHE(re, gb);
285 LAST_SKIP_BITS(re, gb, 32 - log);
286 CLOSE_READER(re, gb);
289 buf = ((buf >> 1) ^ sign) - sign;
296 static inline int get_se_golomb_long(GetBitContext *gb)
298 unsigned int buf = get_ue_golomb_long(gb);
299 int sign = (buf & 1) - 1;
300 return ((buf >> 1) ^ sign) + 1;
303 static inline int get_interleaved_se_golomb(GetBitContext *gb)
307 #if CACHED_BITSTREAM_READER
308 buf = show_bits_long(gb, 32);
310 if (buf & 0xAA800000) {
312 skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
314 return ff_interleaved_se_golomb_vlc_code[buf];
318 buf |= 1 | show_bits(gb, 24);
320 if ((buf & 0xAAAAAAAA) == 0)
323 for (log = 31; (buf & 0x80000000) == 0; log--)
324 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
326 skip_bits_long(gb, 63 - 2 * log - 8);
328 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
332 UPDATE_CACHE(re, gb);
333 buf = GET_CACHE(re, gb);
335 if (buf & 0xAA800000) {
337 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
338 CLOSE_READER(re, gb);
340 return ff_interleaved_se_golomb_vlc_code[buf];
343 LAST_SKIP_BITS(re, gb, 8);
344 UPDATE_CACHE(re, gb);
345 buf |= 1 | (GET_CACHE(re, gb) >> 8);
347 if ((buf & 0xAAAAAAAA) == 0)
350 for (log = 31; (buf & 0x80000000) == 0; log--)
351 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
353 LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
354 CLOSE_READER(re, gb);
356 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
361 static inline int dirac_get_se_golomb(GetBitContext *gb)
363 uint32_t ret = get_interleaved_ue_golomb(gb);
366 int sign = -get_bits1(gb);
367 ret = (ret ^ sign) - sign;
374 * read unsigned golomb rice code (ffv1).
376 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
382 #if CACHED_BITSTREAM_READER
383 buf = show_bits_long(gb, 32);
387 if (log > 31 - limit) {
389 buf += (30 - log) << k;
390 skip_bits_long(gb, 32 + k - log);
394 skip_bits_long(gb, limit);
395 buf = get_bits_long(gb, esc_len);
397 return buf + limit - 1;
401 UPDATE_CACHE(re, gb);
402 buf = GET_CACHE(re, gb);
406 if (log > 31 - limit) {
408 buf += (30U - log) << k;
409 LAST_SKIP_BITS(re, gb, 32 + k - log);
410 CLOSE_READER(re, gb);
414 LAST_SKIP_BITS(re, gb, limit);
415 UPDATE_CACHE(re, gb);
417 buf = SHOW_UBITS(re, gb, esc_len);
419 LAST_SKIP_BITS(re, gb, esc_len);
420 CLOSE_READER(re, gb);
422 return buf + limit - 1;
428 * read unsigned golomb rice code (jpegls).
430 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
436 #if CACHED_BITSTREAM_READER
437 buf = show_bits_long(gb, 32);
441 if (log - k >= 1 && 32 - log < limit) {
443 buf += (30 - log) << k;
444 skip_bits_long(gb, 32 + k - log);
450 i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
454 buf = get_bits_long(gb, k);
456 return buf + (i << k);
457 } else if (i == limit - 1) {
458 buf = get_bits_long(gb, esc_len);
466 UPDATE_CACHE(re, gb);
467 buf = GET_CACHE(re, gb);
473 if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
476 buf += (30U - log) << k;
477 LAST_SKIP_BITS(re, gb, 32 + k - log);
478 CLOSE_READER(re, gb);
483 for (i = 0; i + MIN_CACHE_BITS <= limit && SHOW_UBITS(re, gb, MIN_CACHE_BITS) == 0; i += MIN_CACHE_BITS) {
484 if (gb->size_in_bits <= re_index) {
485 CLOSE_READER(re, gb);
488 LAST_SKIP_BITS(re, gb, MIN_CACHE_BITS);
489 UPDATE_CACHE(re, gb);
491 for (; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
492 SKIP_BITS(re, gb, 1);
494 LAST_SKIP_BITS(re, gb, 1);
495 UPDATE_CACHE(re, gb);
499 if (k > MIN_CACHE_BITS - 1) {
500 buf = SHOW_UBITS(re, gb, 16) << (k-16);
501 LAST_SKIP_BITS(re, gb, 16);
502 UPDATE_CACHE(re, gb);
503 buf |= SHOW_UBITS(re, gb, k-16);
504 LAST_SKIP_BITS(re, gb, k-16);
506 buf = SHOW_UBITS(re, gb, k);
507 LAST_SKIP_BITS(re, gb, k);
513 buf += ((SUINT)i << k);
514 } else if (i == limit - 1) {
515 buf = SHOW_UBITS(re, gb, esc_len);
516 LAST_SKIP_BITS(re, gb, esc_len);
522 CLOSE_READER(re, gb);
529 * read signed golomb rice code (ffv1).
531 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
534 unsigned v = get_ur_golomb(gb, k, limit, esc_len);
535 return (v >> 1) ^ -(v & 1);
539 * read signed golomb rice code (flac).
541 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
544 unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
545 return (v >> 1) ^ -(v & 1);
549 * read unsigned golomb rice code (shorten).
551 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
553 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
557 * read signed golomb rice code (shorten).
559 static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
561 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
562 return (uvar >> 1) ^ -(uvar & 1);
567 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
570 int show = show_bits(s, 24);
571 int pos = get_bits_count(s);
572 int i = get_ue_golomb(s);
573 int len = get_bits_count(s) - pos;
574 int bits = show >> (24 - len);
576 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
577 bits, len, i, pos, file, func, line);
582 static inline int get_se(GetBitContext *s, const char *file, const char *func,
585 int show = show_bits(s, 24);
586 int pos = get_bits_count(s);
587 int i = get_se_golomb(s);
588 int len = get_bits_count(s) - pos;
589 int bits = show >> (24 - len);
591 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
592 bits, len, i, pos, file, func, line);
597 static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
600 int show = show_bits(s, 24);
601 int pos = get_bits_count(s);
602 int i = get_te0_golomb(s, r);
603 int len = get_bits_count(s) - pos;
604 int bits = show >> (24 - len);
606 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
607 bits, len, i, pos, file, func, line);
612 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
613 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
614 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
615 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
620 * write unsigned exp golomb code. 2^16 - 2 at most
622 static inline void set_ue_golomb(PutBitContext *pb, int i)
625 av_assert2(i <= 0xFFFE);
628 put_bits(pb, ff_ue_golomb_len[i], i + 1);
630 int e = av_log2(i + 1);
631 put_bits(pb, 2 * e + 1, i + 1);
636 * write unsigned exp golomb code. 2^32-2 at most.
638 static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
640 av_assert2(i <= (UINT32_MAX - 1));
643 put_bits(pb, ff_ue_golomb_len[i], i + 1);
645 int e = av_log2(i + 1);
646 put_bits64(pb, 2 * e + 1, i + 1);
651 * write truncated unsigned exp golomb code.
653 static inline void set_te_golomb(PutBitContext *pb, int i, int range)
655 av_assert2(range >= 1);
656 av_assert2(i <= range);
659 put_bits(pb, 1, i ^ 1);
661 set_ue_golomb(pb, i);
665 * write signed exp golomb code. 16 bits at most.
667 static inline void set_se_golomb(PutBitContext *pb, int i)
671 i ^= -1; //FIXME check if gcc does the right thing
672 set_ue_golomb(pb, i);
676 * write unsigned golomb rice code (ffv1).
678 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
687 put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
689 put_bits(pb, limit + esc_len, i - limit + 1);
693 * write unsigned golomb rice code (jpegls).
695 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
696 int limit, int esc_len)
716 put_bits(pb, limit, 1);
717 put_bits(pb, esc_len, i - 1);
722 * write signed golomb rice code (ffv1).
724 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
732 set_ur_golomb(pb, v, k, limit, esc_len);
736 * write signed golomb rice code (flac).
738 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
739 int limit, int esc_len)
746 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
749 #endif /* AVCODEC_GOLOMB_H */