diff options
author | trav90 <travawine@palemoon.org> | 2018-10-15 21:45:30 -0500 |
---|---|---|
committer | trav90 <travawine@palemoon.org> | 2018-10-15 21:45:30 -0500 |
commit | 68569dee1416593955c1570d638b3d9250b33012 (patch) | |
tree | d960f017cd7eba3f125b7e8a813789ee2e076310 /third_party/aom/aom_dsp/answriter.h | |
parent | 07c17b6b98ed32fcecff15c083ab0fd878de3cf0 (diff) | |
download | UXP-68569dee1416593955c1570d638b3d9250b33012.tar UXP-68569dee1416593955c1570d638b3d9250b33012.tar.gz UXP-68569dee1416593955c1570d638b3d9250b33012.tar.lz UXP-68569dee1416593955c1570d638b3d9250b33012.tar.xz UXP-68569dee1416593955c1570d638b3d9250b33012.zip |
Import aom library
This is the reference implementation for the Alliance for Open Media's av1 video code.
The commit used was 4d668d7feb1f8abd809d1bca0418570a7f142a36.
Diffstat (limited to 'third_party/aom/aom_dsp/answriter.h')
-rw-r--r-- | third_party/aom/aom_dsp/answriter.h | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/third_party/aom/aom_dsp/answriter.h b/third_party/aom/aom_dsp/answriter.h new file mode 100644 index 000000000..353acf1a9 --- /dev/null +++ b/third_party/aom/aom_dsp/answriter.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2016, Alliance for Open Media. All rights reserved + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +#ifndef AOM_DSP_ANSWRITER_H_ +#define AOM_DSP_ANSWRITER_H_ +// An implementation of Asymmetric Numeral Systems +// http://arxiv.org/abs/1311.2540v2 +// Implements encoding of: +// * rABS (range Asymmetric Binary Systems), a boolean coder +// * rANS (range Asymmetric Numeral Systems), a multi-symbol coder + +#include <assert.h> +#include "./aom_config.h" +#include "aom/aom_integer.h" +#include "aom_dsp/ans.h" +#include "aom_dsp/prob.h" +#include "aom_ports/mem_ops.h" +#include "av1/common/odintrin.h" + +#if RANS_PRECISION <= OD_DIVU_DMAX +#define ANS_DIVREM(quotient, remainder, dividend, divisor) \ + do { \ + quotient = OD_DIVU_SMALL((dividend), (divisor)); \ + remainder = (dividend) - (quotient) * (divisor); \ + } while (0) +#else +#define ANS_DIVREM(quotient, remainder, dividend, divisor) \ + do { \ + quotient = (dividend) / (divisor); \ + remainder = (dividend) % (divisor); \ + } while (0) +#endif + +#define ANS_DIV8(dividend, divisor) OD_DIVU_SMALL((dividend), (divisor)) + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +struct AnsCoder { + uint8_t *buf; + int buf_offset; + uint32_t state; +}; + +static INLINE void ans_write_init(struct AnsCoder *const ans, + uint8_t *const buf) { + ans->buf = buf; + ans->buf_offset = 0; + ans->state = L_BASE; +} + +static INLINE int ans_write_end(struct AnsCoder *const ans) { + uint32_t state; + int ans_size; + assert(ans->state >= L_BASE); + assert(ans->state < L_BASE * IO_BASE); + state = ans->state - L_BASE; + if (state < (1u << 15)) { + mem_put_le16(ans->buf + ans->buf_offset, (0x00u << 15) + state); + ans_size = ans->buf_offset + 2; +#if ANS_REVERSE +#if L_BASE * IO_BASE > (1 << 23) + } else if (state < (1u << 22)) { + mem_put_le24(ans->buf + ans->buf_offset, (0x02u << 22) + state); + ans_size = ans->buf_offset + 3; + } else if (state < (1u << 30)) { + mem_put_le32(ans->buf + ans->buf_offset, (0x03u << 30) + state); + ans_size = ans->buf_offset + 4; +#else + } else if (state < (1u << 23)) { + mem_put_le24(ans->buf + ans->buf_offset, (0x01u << 23) + state); + ans_size = ans->buf_offset + 3; +#endif +#else + } else if (state < (1u << 22)) { + mem_put_le24(ans->buf + ans->buf_offset, (0x02u << 22) + state); + ans_size = ans->buf_offset + 3; + } else if (state < (1u << 29)) { + mem_put_le32(ans->buf + ans->buf_offset, (0x07u << 29) + state); + ans_size = ans->buf_offset + 4; +#endif + } else { + assert(0 && "State is too large to be serialized"); + return ans->buf_offset; + } +#if ANS_REVERSE + { + int i; + uint8_t tmp; + for (i = 0; i < (ans_size >> 1); i++) { + tmp = ans->buf[i]; + ans->buf[i] = ans->buf[ans_size - 1 - i]; + ans->buf[ans_size - 1 - i] = tmp; + } + ans->buf += ans_size; + ans->buf_offset = 0; + ans->state = L_BASE; + } +#endif + return ans_size; +} + +// Write one boolean using rABS where p0 is the probability of the value being +// zero. +static INLINE void rabs_write(struct AnsCoder *ans, int value, AnsP8 p0) { + const AnsP8 p = ANS_P8_PRECISION - p0; + const unsigned l_s = value ? p : p0; + unsigned state = ans->state; + while (state >= L_BASE / ANS_P8_PRECISION * IO_BASE * l_s) { + ans->buf[ans->buf_offset++] = state % IO_BASE; + state /= IO_BASE; + } + const unsigned quotient = ANS_DIV8(state, l_s); + const unsigned remainder = state - quotient * l_s; + ans->state = quotient * ANS_P8_PRECISION + remainder + (value ? p0 : 0); +} + +// Encode one symbol using rANS. +// cum_prob: The cumulative probability before this symbol (the offset of +// the symbol in the symbol cycle) +// prob: The probability of this symbol (l_s from the paper) +// RANS_PRECISION takes the place of m from the paper. +static INLINE void rans_write(struct AnsCoder *ans, aom_cdf_prob cum_prob, + aom_cdf_prob prob) { + unsigned quotient, remainder; + while (ans->state >= L_BASE / RANS_PRECISION * IO_BASE * prob) { + ans->buf[ans->buf_offset++] = ans->state % IO_BASE; + ans->state /= IO_BASE; + } + ANS_DIVREM(quotient, remainder, ans->state, prob); + ans->state = quotient * RANS_PRECISION + remainder + cum_prob; +} + +#undef ANS_DIV8 +#undef ANS_DIVREM +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus +#endif // AOM_DSP_ANSWRITER_H_ |