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 unsigned exp golomb code.
53 static inline int get_ue_golomb(GetBitContext *gb)
59 buf = GET_CACHE(re, gb);
61 if (buf >= (1 << 27)) {
63 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
66 return ff_ue_golomb_vlc_code[buf];
68 int log = 2 * av_log2(buf) - 31;
69 LAST_SKIP_BITS(re, gb, 32 - log);
71 if (CONFIG_FTRAPV && log < 0) {
72 av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
73 return AVERROR_INVALIDDATA;
83 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
85 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
89 buf = show_bits_long(gb, 32);
90 log = 31 - av_log2(buf);
91 skip_bits_long(gb, log);
93 return get_bits_long(gb, log + 1) - 1;
97 * read unsigned exp golomb code, constraint to a max of 31.
98 * the return value is undefined if the stored value exceeds 31.
100 static inline int get_ue_golomb_31(GetBitContext *gb)
105 UPDATE_CACHE(re, gb);
106 buf = GET_CACHE(re, gb);
109 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
110 CLOSE_READER(re, gb);
112 return ff_ue_golomb_vlc_code[buf];
115 static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
120 UPDATE_CACHE(re, gb);
121 buf = GET_CACHE(re, gb);
123 if (buf & 0xAA800000) {
125 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
126 CLOSE_READER(re, gb);
128 return ff_interleaved_ue_golomb_vlc_code[buf];
134 LAST_SKIP_BITS(re, gb,
135 FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
137 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
138 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
139 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
142 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
143 UPDATE_CACHE(re, gb);
144 buf = GET_CACHE(re, gb);
145 } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
147 CLOSE_READER(re, gb);
153 * read unsigned truncated exp golomb code.
155 static inline int get_te0_golomb(GetBitContext *gb, int range)
157 av_assert2(range >= 1);
162 return get_bits1(gb) ^ 1;
164 return get_ue_golomb(gb);
168 * read unsigned truncated exp golomb code.
170 static inline int get_te_golomb(GetBitContext *gb, int range)
172 av_assert2(range >= 1);
175 return get_bits1(gb) ^ 1;
177 return get_ue_golomb(gb);
181 * read signed exp golomb code.
183 static inline int get_se_golomb(GetBitContext *gb)
188 UPDATE_CACHE(re, gb);
189 buf = GET_CACHE(re, gb);
191 if (buf >= (1 << 27)) {
193 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
194 CLOSE_READER(re, gb);
196 return ff_se_golomb_vlc_code[buf];
198 int log = av_log2(buf), sign;
199 LAST_SKIP_BITS(re, gb, 31 - log);
200 UPDATE_CACHE(re, gb);
201 buf = GET_CACHE(re, gb);
205 LAST_SKIP_BITS(re, gb, 32 - log);
206 CLOSE_READER(re, gb);
209 buf = ((buf >> 1) ^ sign) - sign;
215 static inline int get_se_golomb_long(GetBitContext *gb)
217 unsigned int buf = get_ue_golomb_long(gb);
220 sign = (buf & 1) - 1;
221 return ((buf >> 1) ^ sign) + 1;
224 static inline int svq3_get_se_golomb(GetBitContext *gb)
229 UPDATE_CACHE(re, gb);
230 buf = GET_CACHE(re, gb);
232 if (buf & 0xAA800000) {
234 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
235 CLOSE_READER(re, gb);
237 return ff_interleaved_se_golomb_vlc_code[buf];
240 LAST_SKIP_BITS(re, gb, 8);
241 UPDATE_CACHE(re, gb);
242 buf |= 1 | (GET_CACHE(re, gb) >> 8);
244 if ((buf & 0xAAAAAAAA) == 0)
247 for (log = 31; (buf & 0x80000000) == 0; log--)
248 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
250 LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
251 CLOSE_READER(re, gb);
253 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
257 static inline int dirac_get_se_golomb(GetBitContext *gb)
259 uint32_t ret = svq3_get_ue_golomb(gb);
263 sign = -get_bits1(gb);
264 ret = (ret ^ sign) - sign;
271 * read unsigned golomb rice code (ffv1).
273 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
280 UPDATE_CACHE(re, gb);
281 buf = GET_CACHE(re, gb);
285 if (log > 31 - limit) {
287 buf += (30 - log) << k;
288 LAST_SKIP_BITS(re, gb, 32 + k - log);
289 CLOSE_READER(re, gb);
293 LAST_SKIP_BITS(re, gb, limit);
294 UPDATE_CACHE(re, gb);
296 buf = SHOW_UBITS(re, gb, esc_len);
298 LAST_SKIP_BITS(re, gb, esc_len);
299 CLOSE_READER(re, gb);
301 return buf + limit - 1;
306 * read unsigned golomb rice code (jpegls).
308 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
315 UPDATE_CACHE(re, gb);
316 buf = GET_CACHE(re, gb);
320 if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
323 buf += (30 - log) << k;
324 LAST_SKIP_BITS(re, gb, 32 + k - log);
325 CLOSE_READER(re, gb);
330 for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
331 if (gb->size_in_bits <= re_index)
333 LAST_SKIP_BITS(re, gb, 1);
334 UPDATE_CACHE(re, gb);
336 SKIP_BITS(re, gb, 1);
340 buf = SHOW_UBITS(re, gb, k);
341 LAST_SKIP_BITS(re, gb, k);
346 CLOSE_READER(re, gb);
347 return buf + (i << k);
348 } else if (i == limit - 1) {
349 buf = SHOW_UBITS(re, gb, esc_len);
350 LAST_SKIP_BITS(re, gb, esc_len);
351 CLOSE_READER(re, gb);
360 * read signed golomb rice code (ffv1).
362 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
365 int v = get_ur_golomb(gb, k, limit, esc_len);
370 return ((v >> 1) ^ sign) - sign;
374 * read signed golomb rice code (flac).
376 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
379 int v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
380 return (v >> 1) ^ -(v & 1);
384 * read unsigned golomb rice code (shorten).
386 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
388 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
392 * read signed golomb rice code (shorten).
394 static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
396 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
397 return (uvar >> 1) ^ -(uvar & 1);
402 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
405 int show = show_bits(s, 24);
406 int pos = get_bits_count(s);
407 int i = get_ue_golomb(s);
408 int len = get_bits_count(s) - pos;
409 int bits = show >> (24 - len);
411 print_bin(bits, len);
413 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n",
414 bits, len, i, pos, file, func, line);
419 static inline int get_se(GetBitContext *s, const char *file, const char *func,
422 int show = show_bits(s, 24);
423 int pos = get_bits_count(s);
424 int i = get_se_golomb(s);
425 int len = get_bits_count(s) - pos;
426 int bits = show >> (24 - len);
428 print_bin(bits, len);
430 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n",
431 bits, len, i, pos, file, func, line);
436 static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
439 int show = show_bits(s, 24);
440 int pos = get_bits_count(s);
441 int i = get_te0_golomb(s, r);
442 int len = get_bits_count(s) - pos;
443 int bits = show >> (24 - len);
445 print_bin(bits, len);
447 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n",
448 bits, len, i, pos, file, func, line);
453 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
454 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
455 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
456 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
461 * write unsigned exp golomb code.
463 static inline void set_ue_golomb(PutBitContext *pb, int i)
474 put_bits(pb, ff_ue_golomb_len[i], i + 1);
476 int e = av_log2(i + 1);
477 put_bits(pb, 2 * e + 1, i + 1);
482 * write truncated unsigned exp golomb code.
484 static inline void set_te_golomb(PutBitContext *pb, int i, int range)
486 av_assert2(range >= 1);
487 av_assert2(i <= range);
490 put_bits(pb, 1, i ^ 1);
492 set_ue_golomb(pb, i);
496 * write signed exp golomb code. 16 bits at most.
498 static inline void set_se_golomb(PutBitContext *pb, int i)
508 i ^= -1; //FIXME check if gcc does the right thing
513 set_ue_golomb(pb, i);
517 * write unsigned golomb rice code (ffv1).
519 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
528 put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
530 put_bits(pb, limit + esc_len, i - limit + 1);
534 * write unsigned golomb rice code (jpegls).
536 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
537 int limit, int esc_len)
557 put_bits(pb, limit, 1);
558 put_bits(pb, esc_len, i - 1);
563 * write signed golomb rice code (ffv1).
565 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
573 set_ur_golomb(pb, v, k, limit, esc_len);
577 * write signed golomb rice code (flac).
579 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
580 int limit, int esc_len)
587 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
590 #endif /* AVCODEC_GOLOMB_H */