summaryrefslogtreecommitdiff
path: root/libavcodec/golomb_bs.h
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/golomb_bs.h')
-rw-r--r--libavcodec/golomb_bs.h317
1 files changed, 317 insertions, 0 deletions
diff --git a/libavcodec/golomb_bs.h b/libavcodec/golomb_bs.h
new file mode 100644
index 0000000000..bba126bfc4
--- /dev/null
+++ b/libavcodec/golomb_bs.h
@@ -0,0 +1,317 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_GOLOMB_BS_H
+#define AVCODEC_GOLOMB_BS_H
+
+#include <stdint.h>
+
+#include "bitstream.h"
+#include "golomb_common.h"
+
+/**
+ * Read an unsigned Exp-Golomb code in the range 0 to 8190.
+ *
+ * @returns the read value or a negative error code.
+ */
+static inline int golomb_bs_get_ue(BitstreamContext *bc)
+{
+ unsigned int buf = bitstream_peek(bc, 32);
+
+ if (buf >= (1 << 27)) {
+ buf >>= 32 - 9;
+ bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);
+
+ return ff_ue_golomb_vlc_code[buf];
+ } else {
+ int log = 2 * av_log2(buf) - 31;
+
+ bitstream_skip_cache(bc, 32 - log);
+ if (log < 7)
+ return AVERROR_INVALIDDATA;
+ buf >>= log;
+ buf--;
+
+ return buf;
+ }
+}
+
+/**
+ * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
+ */
+static inline unsigned golomb_bs_get_ue_long(BitstreamContext *bc)
+{
+ unsigned int buf = bitstream_peek(bc, 32);
+ unsigned int log = 31 - av_log2(buf);
+ bitstream_skip_cache(bc, log);
+ return bitstream_read(bc, log + 1) - 1;
+}
+
+/**
+ * read unsigned exp golomb code, constraint to a max of 31.
+ * If the value encountered is not in 0..31, the return value
+ * is outside the range 0..30.
+ */
+static inline int golomb_bs_get_ue_31(BitstreamContext *bc)
+{
+ unsigned int buf = bitstream_peek(bc, 32);
+
+ buf >>= 32 - 9;
+ bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);
+
+ return ff_ue_golomb_vlc_code[buf];
+}
+
+static inline unsigned golomb_bs_get_ue_interleaved(BitstreamContext *bc)
+{
+ uint32_t buf = bitstream_peek(bc, 32);
+
+ if (buf & 0xAA800000) {
+ buf >>= 32 - 8;
+ bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]);
+
+ return ff_interleaved_ue_golomb_vlc_code[buf];
+ } else {
+ unsigned ret = 1;
+
+ do {
+ buf >>= 32 - 8;
+ bitstream_skip_cache(bc, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
+
+ if (ff_interleaved_golomb_vlc_len[buf] != 9) {
+ ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
+ ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
+ break;
+ }
+ ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
+ buf = bitstream_peek(bc, 32);
+ } while (bitstream_bits_left(bc) > 0);
+
+ return ret - 1;
+ }
+}
+
+/**
+ * read unsigned truncated exp golomb code.
+ */
+static inline int golomb_bs_get_te0(BitstreamContext *bc, int range)
+{
+ av_assert2(range >= 1);
+
+ if (range == 1)
+ return 0;
+ else if (range == 2)
+ return bitstream_read(bc, 1) ^ 1;
+ else
+ return golomb_bs_get_ue(bc);
+}
+
+/**
+ * read unsigned truncated exp golomb code.
+ */
+static inline int golomb_bs_get_te(BitstreamContext *bc, int range)
+{
+ av_assert2(range >= 1);
+
+ if (range == 2)
+ return bitstream_read(bc, 1) ^ 1;
+ else
+ return golomb_bs_get_ue(bc);
+}
+
+/**
+ * read signed exp golomb code.
+ */
+static inline int golomb_bs_get_se(BitstreamContext *bc)
+{
+ unsigned int buf;
+
+ buf = bitstream_peek(bc, 32);
+
+ if (buf >= (1 << 27)) {
+ buf >>= 32 - 9;
+ bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);
+
+ return ff_se_golomb_vlc_code[buf];
+ } else {
+ int log = 2 * av_log2(buf) - 31;
+ buf >>= log;
+
+ bitstream_skip_cache(bc, 32 - log);
+
+ if (buf & 1)
+ buf = -(buf >> 1);
+ else
+ buf = (buf >> 1);
+
+ return buf;
+ }
+}
+
+static inline int golomb_bs_get_se_long(BitstreamContext *bc)
+{
+ unsigned int buf = golomb_bs_get_ue_long(bc);
+ int sign = (buf & 1) - 1;
+ return ((buf >> 1) ^ sign) + 1;
+}
+
+static inline int golomb_bs_get_se_interleaved(BitstreamContext *bc)
+{
+ unsigned int buf;
+
+ buf = bitstream_peek(bc, 32);
+
+ if (buf & 0xAA800000) {
+ buf >>= 32 - 8;
+ bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]);
+
+ return ff_interleaved_se_golomb_vlc_code[buf];
+ } else {
+ int log;
+ bitstream_skip_cache(bc, 8);
+ buf |= 1 | bitstream_peek(bc, 24);
+
+ if ((buf & 0xAAAAAAAA) == 0)
+ return INVALID_VLC;
+
+ for (log = 31; (buf & 0x80000000) == 0; log--)
+ buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
+
+ bitstream_skip(bc, 63 - 2 * log - 8);
+
+ return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
+ }
+}
+
+static inline int golomb_bs_get_se_dirac(BitstreamContext *bc)
+{
+ uint32_t ret = golomb_bs_get_ue_interleaved(bc);
+
+ if (ret) {
+ int sign = -bitstream_read(bc, 1);
+ ret = (ret ^ sign) - sign;
+ }
+
+ return ret;
+}
+
+/**
+ * read unsigned golomb rice code (ffv1).
+ */
+static inline int golomb_bs_get_ur(BitstreamContext *bc, int k, int limit,
+ int esc_len)
+{
+ unsigned int buf;
+ int log;
+
+ buf = bitstream_peek(bc, 32);
+
+ log = av_log2(buf);
+
+ if (log > 31 - limit) {
+ buf >>= log - k;
+ buf += (30 - log) << k;
+ bitstream_skip(bc, 32 + k - log);
+
+ return buf;
+ } else {
+ bitstream_skip(bc, limit);
+ buf = bitstream_read(bc, esc_len);
+
+ return buf + limit - 1;
+ }
+}
+
+/**
+ * read unsigned golomb rice code (jpegls).
+ */
+static inline int golomb_bs_get_ur_jpegls(BitstreamContext *bc, int k, int limit,
+ int esc_len)
+{
+ unsigned int buf;
+ int log;
+
+ buf = bitstream_peek(bc, 32);
+
+ log = av_log2(buf);
+
+ if (log - k >= 1 && 32 - log < limit) {
+ buf >>= log - k;
+ buf += (30 - log) << k;
+ bitstream_skip(bc, 32 + k - log);
+
+ return buf;
+ } else {
+ int i;
+ for (i = 0;
+ i < limit && bitstream_peek(bc, 1) == 0 && bitstream_bits_left(bc) > 0;
+ i++)
+ bitstream_skip_cache(bc, 1);
+ bitstream_skip(bc, 1);
+
+ if (i < limit - 1) {
+ buf = bitstream_read(bc, k);
+
+ return buf + (i << k);
+ } else if (i == limit - 1) {
+ buf = bitstream_read(bc, esc_len);
+
+ return buf + 1;
+ } else
+ return -1;
+ }
+}
+
+/**
+ * read signed golomb rice code (ffv1).
+ */
+static inline int golomb_bs_get_sr(BitstreamContext *bc, int k, int limit,
+ int esc_len)
+{
+ unsigned v = golomb_bs_get_ur(bc, k, limit, esc_len);
+ return (v >> 1) ^ -(v & 1);
+}
+
+/**
+ * read signed golomb rice code (flac).
+ */
+static inline int golomb_bs_get_sr_flac(BitstreamContext *bc, int k, int limit,
+ int esc_len)
+{
+ unsigned v = golomb_bs_get_ur_jpegls(bc, k, limit, esc_len);
+ return (v >> 1) ^ -(v & 1);
+}
+
+/**
+ * read unsigned golomb rice code (shorten).
+ */
+static inline unsigned int golomb_bs_get_ur_shorten(BitstreamContext *bc, int k)
+{
+ return golomb_bs_get_ur_jpegls(bc, k, INT_MAX, 0);
+}
+
+/**
+ * read signed golomb rice code (shorten).
+ */
+static inline int golomb_bs_get_sr_shorten(BitstreamContext *bc, int k)
+{
+ int uvar = golomb_bs_get_ur_jpegls(bc, k + 1, INT_MAX, 0);
+ return (uvar >> 1) ^ -(uvar & 1);
+}
+
+#endif /* AVCODEC_GOLOMB_BS_H */