bitreader.h 3.84 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * 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.
John Koleszar's avatar
John Koleszar committed
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AOM_DSP_BITREADER_H_
#define AOM_DSP_BITREADER_H_
14

15
#include <assert.h>
16
#include <limits.h>
17

Yaowu Xu's avatar
Yaowu Xu committed
18 19 20
#include "./aom_config.h"
#include "aom/aomdx.h"
#include "aom/aom_integer.h"
21
#if CONFIG_ANS
22
#include "aom_dsp/ansreader.h"
23 24
#elif CONFIG_DAALA_EC
#include "aom_dsp/daalaboolreader.h"
25
#else
26
#include "aom_dsp/dkboolreader.h"
27
#endif
28
#include "aom_dsp/prob.h"
29
#include "av1/common/odintrin.h"
30

31 32 33 34
#ifdef __cplusplus
extern "C" {
#endif

35 36
#if CONFIG_ANS
typedef struct AnsDecoder aom_reader;
37 38
#elif CONFIG_DAALA_EC
typedef struct daala_reader aom_reader;
39
#else
40
typedef struct aom_dk_reader aom_reader;
41
#endif
John Koleszar's avatar
John Koleszar committed
42

43 44 45
static INLINE int aom_reader_init(aom_reader *r, const uint8_t *buffer,
                                  size_t size, aom_decrypt_cb decrypt_cb,
                                  void *decrypt_state) {
46 47 48 49 50
#if CONFIG_ANS
  (void)decrypt_cb;
  (void)decrypt_state;
  assert(size <= INT_MAX);
  return ans_read_init(r, buffer, size);
51 52 53 54
#elif CONFIG_DAALA_EC
  (void)decrypt_cb;
  (void)decrypt_state;
  return aom_daala_reader_init(r, buffer, size);
55
#else
56
  return aom_dk_reader_init(r, buffer, size, decrypt_cb, decrypt_state);
57
#endif
58
}
59

60
static INLINE const uint8_t *aom_reader_find_end(aom_reader *r) {
61 62 63 64
#if CONFIG_ANS
  (void)r;
  assert(0 && "Use the raw buffer size with ANS");
  return NULL;
65 66
#elif CONFIG_DAALA_EC
  return aom_daala_reader_find_end(r);
67
#else
68
  return aom_dk_reader_find_end(r);
69
#endif
70
}
71

Yaowu Xu's avatar
Yaowu Xu committed
72
static INLINE int aom_reader_has_error(aom_reader *r) {
73 74
#if CONFIG_ANS
  return ans_reader_has_error(r);
75 76
#elif CONFIG_DAALA_EC
  return aom_daala_reader_has_error(r);
77
#else
78
  return aom_dk_reader_has_error(r);
79
#endif
James Zern's avatar
James Zern committed
80 81
}

Yaowu Xu's avatar
Yaowu Xu committed
82
static INLINE int aom_read(aom_reader *r, int prob) {
83 84
#if CONFIG_ANS
  return uabs_read(r, prob);
85 86
#elif CONFIG_DAALA_EC
  return aom_daala_read(r, prob);
87
#else
88
  return aom_dk_read(r, prob);
89
#endif
John Koleszar's avatar
John Koleszar committed
90 91
}

92 93 94 95
static INLINE int aom_read_bit(aom_reader *r) {
#if CONFIG_ANS
  return uabs_read_bit(r);  // Non trivial optimization at half probability
#else
96
  return aom_read(r, 128);  // aom_prob_half
97 98
#endif
}
99

Yaowu Xu's avatar
Yaowu Xu committed
100
static INLINE int aom_read_literal(aom_reader *r, int bits) {
101 102 103 104 105
  int literal = 0, bit;

  for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r) << bit;

  return literal;
John Koleszar's avatar
John Koleszar committed
106
}
107

108 109
static INLINE int aom_read_tree_bits(aom_reader *r, const aom_tree_index *tree,
                                     const aom_prob *probs) {
110 111 112 113 114
  aom_tree_index i = 0;

  while ((i = tree[i + aom_read(r, probs[i >> 1])]) > 0) continue;

  return -i;
115
}
116

117 118
static INLINE int aom_read_tree(aom_reader *r, const aom_tree_index *tree,
                                const aom_prob *probs) {
119 120 121
#if CONFIG_DAALA_EC
  return daala_read_tree_bits(r, tree, probs);
#else
122
  return aom_read_tree_bits(r, tree, probs);
123
#endif
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
static INLINE int aom_read_symbol(aom_reader *r, const aom_cdf_prob *cdf,
                                  int nsymbs) {
#if CONFIG_ANS
  (void)nsymbs;
  return rans_read(r, cdf);
#else
  (void)r;
  (void)cdf;
  (void)nsymbs;
  assert(0 && "Unsupported bitreader operation");
  return -1;
#endif
}

140 141 142 143 144 145 146 147 148 149 150 151 152
static INLINE int aom_read_tree_cdf(aom_reader *r, const uint16_t *cdf,
                                    int nsymbs) {
#if CONFIG_DAALA_EC
  return daala_read_tree_cdf(r, cdf, nsymbs);
#else
  (void)r;
  (void)cdf;
  (void)nsymbs;
  assert(0 && "Unsupported bitreader operation");
  return -1;
#endif
}

153 154 155 156
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
157
#endif  // AOM_DSP_BITREADER_H_