2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* 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.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#ifndef AOM_DSP_BITREADER_H_
|
|
|
|
#define AOM_DSP_BITREADER_H_
|
2012-12-19 03:31:19 +04:00
|
|
|
|
2016-09-24 00:21:02 +03:00
|
|
|
#include <assert.h>
|
2016-09-28 21:33:20 +03:00
|
|
|
#include <limits.h>
|
2016-09-24 00:21:02 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "./aom_config.h"
|
|
|
|
#include "aom/aomdx.h"
|
|
|
|
#include "aom/aom_integer.h"
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
2016-09-28 21:33:20 +03:00
|
|
|
#include "aom_dsp/ansreader.h"
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
#include "aom_dsp/daalaboolreader.h"
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
#include "aom_dsp/dkboolreader.h"
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_dsp/prob.h"
|
2016-06-20 20:44:22 +03:00
|
|
|
#include "av1/common/odintrin.h"
|
2010-05-06 01:58:19 +04:00
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
#include "av1/common/accounting.h"
|
|
|
|
#define ACCT_STR_NAME acct_str
|
|
|
|
#define ACCT_STR_PARAM , const char *ACCT_STR_NAME
|
|
|
|
#define ACCT_STR_ARG(s) , s
|
|
|
|
#else
|
|
|
|
#define ACCT_STR_PARAM
|
|
|
|
#define ACCT_STR_ARG(s)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define aom_read(r, prob, ACCT_STR_NAME) \
|
|
|
|
aom_read_(r, prob ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
#define aom_read_bit(r, ACCT_STR_NAME) \
|
|
|
|
aom_read_bit_(r ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
#define aom_read_tree(r, tree, probs, ACCT_STR_NAME) \
|
|
|
|
aom_read_tree_(r, tree, probs ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
#define aom_read_literal(r, bits, ACCT_STR_NAME) \
|
|
|
|
aom_read_literal_(r, bits ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
#define aom_read_tree_bits(r, tree, probs, ACCT_STR_NAME) \
|
|
|
|
aom_read_tree_bits_(r, tree, probs ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
#define aom_read_symbol(r, cdf, nsymbs, ACCT_STR_NAME) \
|
|
|
|
aom_read_symbol_(r, cdf, nsymbs ACCT_STR_ARG(ACCT_STR_NAME))
|
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
typedef struct AnsDecoder aom_reader;
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
typedef struct daala_reader aom_reader;
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
typedef struct aom_dk_reader aom_reader;
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-06-07 01:12:06 +03:00
|
|
|
static INLINE int aom_reader_init(aom_reader *r, const uint8_t *buffer,
|
|
|
|
size_t size, aom_decrypt_cb decrypt_cb,
|
|
|
|
void *decrypt_state) {
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)decrypt_cb;
|
|
|
|
(void)decrypt_state;
|
|
|
|
assert(size <= INT_MAX);
|
|
|
|
return ans_read_init(r, buffer, size);
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
(void)decrypt_cb;
|
|
|
|
(void)decrypt_state;
|
|
|
|
return aom_daala_reader_init(r, buffer, size);
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
return aom_dk_reader_init(r, buffer, size, decrypt_cb, decrypt_state);
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2016-06-07 01:12:06 +03:00
|
|
|
}
|
2010-05-06 01:58:19 +04:00
|
|
|
|
2016-06-07 01:12:06 +03:00
|
|
|
static INLINE const uint8_t *aom_reader_find_end(aom_reader *r) {
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)r;
|
|
|
|
assert(0 && "Use the raw buffer size with ANS");
|
|
|
|
return NULL;
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
return aom_daala_reader_find_end(r);
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
return aom_dk_reader_find_end(r);
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2016-06-07 01:12:06 +03:00
|
|
|
}
|
2013-04-16 01:54:19 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_reader_has_error(aom_reader *r) {
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
return ans_reader_has_error(r);
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
return aom_daala_reader_has_error(r);
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
return aom_dk_reader_has_error(r);
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2015-07-02 05:10:43 +03:00
|
|
|
}
|
|
|
|
|
2016-08-25 21:27:56 +03:00
|
|
|
// Returns the position in the bit reader in bits.
|
2016-08-04 02:13:04 +03:00
|
|
|
static INLINE ptrdiff_t aom_reader_tell(const aom_reader *r) {
|
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)r;
|
|
|
|
assert(0 && "aom_reader_tell() is unimplemented for ANS");
|
|
|
|
return 0;
|
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
return aom_daala_reader_tell(r);
|
|
|
|
#else
|
|
|
|
return aom_dk_reader_tell(r);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-25 21:27:56 +03:00
|
|
|
// Returns the position in the bit reader in 1/8th bits.
|
|
|
|
static INLINE ptrdiff_t aom_reader_tell_frac(const aom_reader *r) {
|
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)r;
|
|
|
|
assert(0 && "aom_reader_tell_frac() is unimplemented for ANS");
|
|
|
|
return 0;
|
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
return aom_daala_reader_tell_frac(r);
|
|
|
|
#else
|
|
|
|
return aom_dk_reader_tell_frac(r);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
static INLINE void aom_process_accounting(const aom_reader *r ACCT_STR_PARAM) {
|
|
|
|
if (r->accounting != NULL) {
|
|
|
|
uint32_t tell_frac;
|
|
|
|
tell_frac = aom_reader_tell_frac(r);
|
|
|
|
aom_accounting_record(r->accounting, ACCT_STR_NAME,
|
|
|
|
tell_frac - r->accounting->last_tell_frac);
|
|
|
|
r->accounting->last_tell_frac = tell_frac;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static INLINE int aom_read_(aom_reader *r, int prob ACCT_STR_PARAM) {
|
|
|
|
int ret;
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = uabs_read(r, prob);
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = aom_daala_read(r, prob);
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = aom_dk_read(r, prob);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2016-08-26 00:40:54 +03:00
|
|
|
return ret;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
static INLINE int aom_read_bit_(aom_reader *r ACCT_STR_PARAM) {
|
|
|
|
int ret;
|
2016-09-24 00:21:02 +03:00
|
|
|
#if CONFIG_ANS
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = uabs_read_bit(r); // Non trivial optimization at half probability
|
2016-09-24 00:21:02 +03:00
|
|
|
#else
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = aom_read(r, 128, NULL); // aom_prob_half
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
2016-09-24 00:21:02 +03:00
|
|
|
#endif
|
2016-08-26 00:40:54 +03:00
|
|
|
return ret;
|
2016-09-24 00:21:02 +03:00
|
|
|
}
|
2013-04-16 01:54:19 +04:00
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
static INLINE int aom_read_literal_(aom_reader *r, int bits ACCT_STR_PARAM) {
|
2016-06-16 16:00:39 +03:00
|
|
|
int literal = 0, bit;
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r, NULL) << bit;
|
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
|
|
|
#endif
|
2016-06-16 16:00:39 +03:00
|
|
|
return literal;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2010-12-16 18:46:31 +03:00
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
static INLINE int aom_read_tree_bits_(aom_reader *r, const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs ACCT_STR_PARAM) {
|
2016-06-16 16:00:39 +03:00
|
|
|
aom_tree_index i = 0;
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
while ((i = tree[i + aom_read(r, probs[i >> 1], NULL)]) > 0) continue;
|
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
|
|
|
#endif
|
2016-06-16 16:00:39 +03:00
|
|
|
return -i;
|
2013-11-27 00:38:58 +04:00
|
|
|
}
|
2012-04-12 20:24:03 +04:00
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
static INLINE int aom_read_tree_(aom_reader *r, const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs ACCT_STR_PARAM) {
|
|
|
|
int ret;
|
2016-03-06 21:41:53 +03:00
|
|
|
#if CONFIG_DAALA_EC
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = daala_read_tree_bits(r, tree, probs);
|
2016-03-06 21:41:53 +03:00
|
|
|
#else
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = aom_read_tree_bits(r, tree, probs, NULL);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
2016-03-06 21:41:53 +03:00
|
|
|
#endif
|
2016-08-26 00:40:54 +03:00
|
|
|
return ret;
|
2016-06-19 19:02:33 +03:00
|
|
|
}
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
static INLINE int aom_read_symbol_(aom_reader *r, const aom_cdf_prob *cdf,
|
|
|
|
int nsymbs ACCT_STR_PARAM) {
|
|
|
|
int ret;
|
2016-10-13 01:59:58 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)nsymbs;
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = rans_read(r, cdf);
|
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
ret = daala_read_symbol(r, cdf, nsymbs);
|
2016-10-13 01:59:58 +03:00
|
|
|
#else
|
|
|
|
(void)r;
|
|
|
|
(void)cdf;
|
|
|
|
(void)nsymbs;
|
|
|
|
assert(0 && "Unsupported bitreader operation");
|
2016-08-26 00:40:54 +03:00
|
|
|
ret = -1;
|
2016-10-13 01:59:58 +03:00
|
|
|
#endif
|
2016-08-26 00:40:54 +03:00
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
|
2016-06-20 20:44:22 +03:00
|
|
|
#endif
|
2016-08-26 00:40:54 +03:00
|
|
|
return ret;
|
2016-06-20 20:44:22 +03:00
|
|
|
}
|
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#endif // AOM_DSP_BITREADER_H_
|