blob: d189e745df7b3fe0aa23073e2d47c9289878f910 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AOM_DSP_BITREADER_H_
13#define AOM_DSP_BITREADER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Alex Converseacef60b2016-09-23 14:21:02 -070015#include <assert.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "./aom_config.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom/aomdx.h"
19#include "aom/aom_integer.h"
Alex Converseacef60b2016-09-23 14:21:02 -070020#if CONFIG_ANS
21#include "aom_dsp/ans.h"
22#else
Alex Converseeb00cb22016-06-06 15:12:06 -070023#include "aom_dsp/dkboolreader.h"
Alex Converseacef60b2016-09-23 14:21:02 -070024#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "aom_dsp/prob.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
27#ifdef __cplusplus
28extern "C" {
29#endif
30
Alex Converseacef60b2016-09-23 14:21:02 -070031#if CONFIG_ANS
32typedef struct AnsDecoder aom_reader;
33#else
Alex Converseeb00cb22016-06-06 15:12:06 -070034typedef struct aom_dk_reader aom_reader;
Alex Converseacef60b2016-09-23 14:21:02 -070035#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070036
Alex Converseeb00cb22016-06-06 15:12:06 -070037static INLINE int aom_reader_init(aom_reader *r, const uint8_t *buffer,
38 size_t size, aom_decrypt_cb decrypt_cb,
39 void *decrypt_state) {
Alex Converseacef60b2016-09-23 14:21:02 -070040#if CONFIG_ANS
41 (void)decrypt_cb;
42 (void)decrypt_state;
43 assert(size <= INT_MAX);
44 return ans_read_init(r, buffer, size);
45#else
Alex Converseeb00cb22016-06-06 15:12:06 -070046 return aom_dk_reader_init(r, buffer, size, decrypt_cb, decrypt_state);
Alex Converseacef60b2016-09-23 14:21:02 -070047#endif
Alex Converseeb00cb22016-06-06 15:12:06 -070048}
Yaowu Xuc27fc142016-08-22 16:08:15 -070049
Alex Converseeb00cb22016-06-06 15:12:06 -070050static INLINE const uint8_t *aom_reader_find_end(aom_reader *r) {
Alex Converseacef60b2016-09-23 14:21:02 -070051#if CONFIG_ANS
52 (void)r;
53 assert(0 && "Use the raw buffer size with ANS");
54 return NULL;
55#else
Alex Converseeb00cb22016-06-06 15:12:06 -070056 return aom_dk_reader_find_end(r);
Alex Converseacef60b2016-09-23 14:21:02 -070057#endif
Alex Converseeb00cb22016-06-06 15:12:06 -070058}
Yaowu Xuc27fc142016-08-22 16:08:15 -070059
Yaowu Xuf883b422016-08-30 14:01:10 -070060static INLINE int aom_reader_has_error(aom_reader *r) {
Alex Converseacef60b2016-09-23 14:21:02 -070061#if CONFIG_ANS
62 return ans_reader_has_error(r);
63#else
Alex Converseeb00cb22016-06-06 15:12:06 -070064 return aom_dk_reader_has_error(r);
Alex Converseacef60b2016-09-23 14:21:02 -070065#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070066}
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068static INLINE int aom_read(aom_reader *r, int prob) {
Alex Converseacef60b2016-09-23 14:21:02 -070069#if CONFIG_ANS
70 return uabs_read(r, prob);
71#else
Alex Converseeb00cb22016-06-06 15:12:06 -070072 return aom_dk_read(r, prob);
Alex Converseacef60b2016-09-23 14:21:02 -070073#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070074}
75
Alex Converseacef60b2016-09-23 14:21:02 -070076static INLINE int aom_read_bit(aom_reader *r) {
77#if CONFIG_ANS
78 return uabs_read_bit(r); // Non trivial optimization at half probability
79#else
80 return aom_dk_read_bit(r);
81#endif
82}
Yaowu Xuc27fc142016-08-22 16:08:15 -070083
Yaowu Xuf883b422016-08-30 14:01:10 -070084static INLINE int aom_read_literal(aom_reader *r, int bits) {
Alex Converseacef60b2016-09-23 14:21:02 -070085#if CONFIG_ANS
86 return uabs_read_literal(r, bits);
87#else
Alex Converseeb00cb22016-06-06 15:12:06 -070088 return aom_dk_read_literal(r, bits);
Alex Converseacef60b2016-09-23 14:21:02 -070089#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070090}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static INLINE int aom_read_tree(aom_reader *r, const aom_tree_index *tree,
93 const aom_prob *probs) {
Alex Converseacef60b2016-09-23 14:21:02 -070094#if CONFIG_ANS
95 return uabs_read_tree(r, tree, probs);
96#else
Alex Converseeb00cb22016-06-06 15:12:06 -070097 return aom_dk_read_tree(r, tree, probs);
Alex Converseacef60b2016-09-23 14:21:02 -070098#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070099}
100
101#ifdef __cplusplus
102} // extern "C"
103#endif
104
Yaowu Xuf883b422016-08-30 14:01:10 -0700105#endif // AOM_DSP_BITREADER_H_