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 static inline int get_ue_golomb(GetBitContext *gb)
57 #if CACHED_BITSTREAM_READER
58 buf = show_bits_long(gb, 32);
60 if (buf >= (1 << 27)) {
62 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
64 return ff_ue_golomb_vlc_code[buf];
66 int log = 2 * av_log2(buf) - 31;
69 skip_bits_long(gb, 32 - log);
76 buf = GET_CACHE(re, gb);
78 if (buf >= (1 << 27)) {
80 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
83 return ff_ue_golomb_vlc_code[buf];
85 int log = 2 * av_log2(buf) - 31;
86 LAST_SKIP_BITS(re, gb, 32 - log);
89 av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
90 return AVERROR_INVALIDDATA;
101 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
103 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
107 buf = show_bits_long(gb, 32);
108 log = 31 - av_log2(buf);
109 skip_bits_long(gb, log);
111 return get_bits_long(gb, log + 1) - 1;
115 * read unsigned exp golomb code, constraint to a max of 31.
116 * the return value is undefined if the stored value exceeds 31.
118 static inline int get_ue_golomb_31(GetBitContext *gb)
122 #if CACHED_BITSTREAM_READER
123 buf = show_bits_long(gb, 32);
126 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
130 UPDATE_CACHE(re, gb);
131 buf = GET_CACHE(re, gb);
134 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
135 CLOSE_READER(re, gb);
138 return ff_ue_golomb_vlc_code[buf];
141 static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
145 #if CACHED_BITSTREAM_READER
146 buf = show_bits_long(gb, 32);
148 if (buf & 0xAA800000) {
150 skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
152 return ff_interleaved_ue_golomb_vlc_code[buf];
158 skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
160 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
161 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
162 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
165 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
166 buf = show_bits_long(gb, 32);
167 } while (get_bits_left(gb) > 0);
173 UPDATE_CACHE(re, gb);
174 buf = GET_CACHE(re, gb);
176 if (buf & 0xAA800000) {
178 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
179 CLOSE_READER(re, gb);
181 return ff_interleaved_ue_golomb_vlc_code[buf];
187 LAST_SKIP_BITS(re, gb,
188 FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
190 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
191 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
192 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
195 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
196 UPDATE_CACHE(re, gb);
197 buf = GET_CACHE(re, gb);
198 } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
200 CLOSE_READER(re, gb);
207 * read unsigned truncated exp golomb code.
209 static inline int get_te0_golomb(GetBitContext *gb, int range)
211 av_assert2(range >= 1);
216 return get_bits1(gb) ^ 1;
218 return get_ue_golomb(gb);
222 * read unsigned truncated exp golomb code.
224 static inline int get_te_golomb(GetBitContext *gb, int range)
226 av_assert2(range >= 1);
229 return get_bits1(gb) ^ 1;
231 return get_ue_golomb(gb);
235 * read signed exp golomb code.
237 static inline int get_se_golomb(GetBitContext *gb)
241 #if CACHED_BITSTREAM_READER
242 buf = show_bits_long(gb, 32);
244 if (buf >= (1 << 27)) {
246 skip_bits_long(gb, ff_golomb_vlc_len[buf]);
248 return ff_se_golomb_vlc_code[buf];
250 int log = 2 * av_log2(buf) - 31;
253 skip_bits_long(gb, 32 - log);
264 UPDATE_CACHE(re, gb);
265 buf = GET_CACHE(re, gb);
267 if (buf >= (1 << 27)) {
269 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
270 CLOSE_READER(re, gb);
272 return ff_se_golomb_vlc_code[buf];
274 int log = av_log2(buf), sign;
275 LAST_SKIP_BITS(re, gb, 31 - log);
276 UPDATE_CACHE(re, gb);
277 buf = GET_CACHE(re, gb);
281 LAST_SKIP_BITS(re, gb, 32 - log);
282 CLOSE_READER(re, gb);
285 buf = ((buf >> 1) ^ sign) - sign;
292 static inline int get_se_golomb_long(GetBitContext *gb)
294 unsigned int buf = get_ue_golomb_long(gb);
295 int sign = (buf & 1) - 1;
296 return ((buf >> 1) ^ sign) + 1;
299 static inline int get_interleaved_se_golomb(GetBitContext *gb)
303 #if CACHED_BITSTREAM_READER
304 buf = show_bits_long(gb, 32);
306 if (buf & 0xAA800000) {
308 skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
310 return ff_interleaved_se_golomb_vlc_code[buf];
314 buf |= 1 | show_bits_long(gb, 24);
316 if ((buf & 0xAAAAAAAA) == 0)
319 for (log = 31; (buf & 0x80000000) == 0; log--)
320 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
322 skip_bits_long(gb, 63 - 2 * log - 8);
324 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
328 UPDATE_CACHE(re, gb);
329 buf = GET_CACHE(re, gb);
331 if (buf & 0xAA800000) {
333 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
334 CLOSE_READER(re, gb);
336 return ff_interleaved_se_golomb_vlc_code[buf];
339 LAST_SKIP_BITS(re, gb, 8);
340 UPDATE_CACHE(re, gb);
341 buf |= 1 | (GET_CACHE(re, gb) >> 8);
343 if ((buf & 0xAAAAAAAA) == 0)
346 for (log = 31; (buf & 0x80000000) == 0; log--)
347 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
349 LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
350 CLOSE_READER(re, gb);
352 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
357 static inline int dirac_get_se_golomb(GetBitContext *gb)
359 uint32_t ret = get_interleaved_ue_golomb(gb);
362 int sign = -get_bits1(gb);
363 ret = (ret ^ sign) - sign;
370 * read unsigned golomb rice code (ffv1).
372 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
378 #if CACHED_BITSTREAM_READER
379 buf = show_bits_long(gb, 32);
383 if (log > 31 - limit) {
385 buf += (30 - log) << k;
386 skip_bits_long(gb, 32 + k - log);
390 skip_bits_long(gb, limit);
391 buf = get_bits_long(gb, esc_len);
393 return buf + limit - 1;
397 UPDATE_CACHE(re, gb);
398 buf = GET_CACHE(re, gb);
402 if (log > 31 - limit) {
404 buf += (30U - log) << k;
405 LAST_SKIP_BITS(re, gb, 32 + k - log);
406 CLOSE_READER(re, gb);
410 LAST_SKIP_BITS(re, gb, limit);
411 UPDATE_CACHE(re, gb);
413 buf = SHOW_UBITS(re, gb, esc_len);
415 LAST_SKIP_BITS(re, gb, esc_len);
416 CLOSE_READER(re, gb);
418 return buf + limit - 1;
424 * read unsigned golomb rice code (jpegls).
426 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
432 #if CACHED_BITSTREAM_READER
433 buf = show_bits_long(gb, 32);
437 if (log - k >= 1 && 32 - log < limit) {
439 buf += (30 - log) << k;
440 skip_bits_long(gb, 32 + k - log);
446 i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
450 buf = get_bits_long(gb, k);
452 return buf + (i << k);
453 } else if (i == limit - 1) {
454 buf = get_bits_long(gb, esc_len);
462 UPDATE_CACHE(re, gb);
463 buf = GET_CACHE(re, gb);
469 if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
472 buf += (30U - log) << k;
473 LAST_SKIP_BITS(re, gb, 32 + k - log);
474 CLOSE_READER(re, gb);
479 for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
480 if (gb->size_in_bits <= re_index) {
481 CLOSE_READER(re, gb);
484 LAST_SKIP_BITS(re, gb, 1);
485 UPDATE_CACHE(re, gb);
487 SKIP_BITS(re, gb, 1);
491 if (k > MIN_CACHE_BITS - 1) {
492 buf = SHOW_UBITS(re, gb, 16) << (k-16);
493 LAST_SKIP_BITS(re, gb, 16);
494 UPDATE_CACHE(re, gb);
495 buf |= SHOW_UBITS(re, gb, k-16);
496 LAST_SKIP_BITS(re, gb, k-16);
498 buf = SHOW_UBITS(re, gb, k);
499 LAST_SKIP_BITS(re, gb, k);
505 buf += ((SUINT)i << k);
506 } else if (i == limit - 1) {
507 buf = SHOW_UBITS(re, gb, esc_len);
508 LAST_SKIP_BITS(re, gb, esc_len);
514 CLOSE_READER(re, gb);
521 * read signed golomb rice code (ffv1).
523 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
526 unsigned v = get_ur_golomb(gb, k, limit, esc_len);
527 return (v >> 1) ^ -(v & 1);
531 * read signed golomb rice code (flac).
533 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
536 unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
537 return (v >> 1) ^ -(v & 1);
541 * read unsigned golomb rice code (shorten).
543 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
545 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
549 * read signed golomb rice code (shorten).
551 static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
553 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
554 return (uvar >> 1) ^ -(uvar & 1);
559 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
562 int show = show_bits(s, 24);
563 int pos = get_bits_count(s);
564 int i = get_ue_golomb(s);
565 int len = get_bits_count(s) - pos;
566 int bits = show >> (24 - len);
568 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
569 bits, len, i, pos, file, func, line);
574 static inline int get_se(GetBitContext *s, const char *file, const char *func,
577 int show = show_bits(s, 24);
578 int pos = get_bits_count(s);
579 int i = get_se_golomb(s);
580 int len = get_bits_count(s) - pos;
581 int bits = show >> (24 - len);
583 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
584 bits, len, i, pos, file, func, line);
589 static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
592 int show = show_bits(s, 24);
593 int pos = get_bits_count(s);
594 int i = get_te0_golomb(s, r);
595 int len = get_bits_count(s) - pos;
596 int bits = show >> (24 - len);
598 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
599 bits, len, i, pos, file, func, line);
604 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
605 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
606 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
607 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
612 * write unsigned exp golomb code. 2^16 - 2 at most
614 static inline void set_ue_golomb(PutBitContext *pb, int i)
617 av_assert2(i <= 0xFFFE);
620 put_bits(pb, ff_ue_golomb_len[i], i + 1);
622 int e = av_log2(i + 1);
623 put_bits(pb, 2 * e + 1, i + 1);
628 * write unsigned exp golomb code. 2^32-2 at most.
630 static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
632 av_assert2(i <= (UINT32_MAX - 1));
635 put_bits(pb, ff_ue_golomb_len[i], i + 1);
637 int e = av_log2(i + 1);
638 put_bits64(pb, 2 * e + 1, i + 1);
643 * write truncated unsigned exp golomb code.
645 static inline void set_te_golomb(PutBitContext *pb, int i, int range)
647 av_assert2(range >= 1);
648 av_assert2(i <= range);
651 put_bits(pb, 1, i ^ 1);
653 set_ue_golomb(pb, i);
657 * write signed exp golomb code. 16 bits at most.
659 static inline void set_se_golomb(PutBitContext *pb, int i)
663 i ^= -1; //FIXME check if gcc does the right thing
664 set_ue_golomb(pb, i);
668 * write unsigned golomb rice code (ffv1).
670 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
679 put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
681 put_bits(pb, limit + esc_len, i - limit + 1);
685 * write unsigned golomb rice code (jpegls).
687 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
688 int limit, int esc_len)
708 put_bits(pb, limit, 1);
709 put_bits(pb, esc_len, i - 1);
714 * write signed golomb rice code (ffv1).
716 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
724 set_ur_golomb(pb, v, k, limit, esc_len);
728 * write signed golomb rice code (flac).
730 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
731 int limit, int esc_len)
738 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
741 #endif /* AVCODEC_GOLOMB_H */