bitreader.h 6.51 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
#include "./aom_config.h"
19 20 21 22
#if CONFIG_EC_ADAPT && !CONFIG_EC_MULTISYMBOL
#error "CONFIG_EC_ADAPT is enabled without enabling CONFIG_EC_MULTISYMBOL."
#endif

Yaowu Xu's avatar
Yaowu Xu committed
23 24
#include "aom/aomdx.h"
#include "aom/aom_integer.h"
25
#if CONFIG_ANS
26
#include "aom_dsp/ansreader.h"
27 28
#elif CONFIG_DAALA_EC
#include "aom_dsp/daalaboolreader.h"
29
#else
30
#include "aom_dsp/dkboolreader.h"
31
#endif
32
#include "aom_dsp/prob.h"
33
#include "av1/common/odintrin.h"
34

Michael Bebenita's avatar
Michael Bebenita committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#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))

58 59 60 61
#ifdef __cplusplus
extern "C" {
#endif

62 63
#if CONFIG_ANS
typedef struct AnsDecoder aom_reader;
64 65
#elif CONFIG_DAALA_EC
typedef struct daala_reader aom_reader;
66
#else
67
typedef struct aom_dk_reader aom_reader;
68
#endif
John Koleszar's avatar
John Koleszar committed
69

70 71 72
static INLINE int aom_reader_init(aom_reader *r, const uint8_t *buffer,
                                  size_t size, aom_decrypt_cb decrypt_cb,
                                  void *decrypt_state) {
73 74 75 76 77
#if CONFIG_ANS
  (void)decrypt_cb;
  (void)decrypt_state;
  assert(size <= INT_MAX);
  return ans_read_init(r, buffer, size);
78 79 80 81
#elif CONFIG_DAALA_EC
  (void)decrypt_cb;
  (void)decrypt_state;
  return aom_daala_reader_init(r, buffer, size);
82
#else
83
  return aom_dk_reader_init(r, buffer, size, decrypt_cb, decrypt_state);
84
#endif
85
}
86

87
static INLINE const uint8_t *aom_reader_find_end(aom_reader *r) {
88 89 90 91
#if CONFIG_ANS
  (void)r;
  assert(0 && "Use the raw buffer size with ANS");
  return NULL;
92 93
#elif CONFIG_DAALA_EC
  return aom_daala_reader_find_end(r);
94
#else
95
  return aom_dk_reader_find_end(r);
96
#endif
97
}
98

Yaowu Xu's avatar
Yaowu Xu committed
99
static INLINE int aom_reader_has_error(aom_reader *r) {
100 101
#if CONFIG_ANS
  return ans_reader_has_error(r);
102 103
#elif CONFIG_DAALA_EC
  return aom_daala_reader_has_error(r);
104
#else
105
  return aom_dk_reader_has_error(r);
106
#endif
James Zern's avatar
James Zern committed
107 108
}

109
// Returns the position in the bit reader in bits.
110
static INLINE uint32_t aom_reader_tell(const aom_reader *r) {
111 112 113 114 115 116 117 118 119 120 121
#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
}

122
// Returns the position in the bit reader in 1/8th bits.
123
static INLINE uint32_t aom_reader_tell_frac(const aom_reader *r) {
124 125 126 127 128 129 130 131 132 133 134
#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
}

Michael Bebenita's avatar
Michael Bebenita committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148
#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;
149
#if CONFIG_ANS
Michael Bebenita's avatar
Michael Bebenita committed
150
  ret = uabs_read(r, prob);
151
#elif CONFIG_DAALA_EC
Michael Bebenita's avatar
Michael Bebenita committed
152
  ret = aom_daala_read(r, prob);
153
#else
Michael Bebenita's avatar
Michael Bebenita committed
154 155 156 157
  ret = aom_dk_read(r, prob);
#endif
#if CONFIG_ACCOUNTING
  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
158
#endif
Michael Bebenita's avatar
Michael Bebenita committed
159
  return ret;
John Koleszar's avatar
John Koleszar committed
160 161
}

Michael Bebenita's avatar
Michael Bebenita committed
162 163
static INLINE int aom_read_bit_(aom_reader *r ACCT_STR_PARAM) {
  int ret;
164
#if CONFIG_ANS
Michael Bebenita's avatar
Michael Bebenita committed
165
  ret = uabs_read_bit(r);  // Non trivial optimization at half probability
166
#else
Michael Bebenita's avatar
Michael Bebenita committed
167 168 169 170
  ret = aom_read(r, 128, NULL);  // aom_prob_half
#endif
#if CONFIG_ACCOUNTING
  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
171
#endif
Michael Bebenita's avatar
Michael Bebenita committed
172
  return ret;
173
}
174

Michael Bebenita's avatar
Michael Bebenita committed
175
static INLINE int aom_read_literal_(aom_reader *r, int bits ACCT_STR_PARAM) {
176 177
  int literal = 0, bit;

Michael Bebenita's avatar
Michael Bebenita committed
178 179 180 181
  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
182
  return literal;
John Koleszar's avatar
John Koleszar committed
183
}
184

Michael Bebenita's avatar
Michael Bebenita committed
185 186
static INLINE int aom_read_tree_bits_(aom_reader *r, const aom_tree_index *tree,
                                      const aom_prob *probs ACCT_STR_PARAM) {
187 188
  aom_tree_index i = 0;

Michael Bebenita's avatar
Michael Bebenita committed
189 190 191 192
  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
193
  return -i;
194
}
195

Michael Bebenita's avatar
Michael Bebenita committed
196 197 198
static INLINE int aom_read_tree_(aom_reader *r, const aom_tree_index *tree,
                                 const aom_prob *probs ACCT_STR_PARAM) {
  int ret;
199
#if CONFIG_DAALA_EC
Michael Bebenita's avatar
Michael Bebenita committed
200
  ret = daala_read_tree_bits(r, tree, probs);
201
#else
Michael Bebenita's avatar
Michael Bebenita committed
202 203 204 205
  ret = aom_read_tree_bits(r, tree, probs, NULL);
#endif
#if CONFIG_ACCOUNTING
  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
206
#endif
Michael Bebenita's avatar
Michael Bebenita committed
207
  return ret;
208 209
}

210
#if CONFIG_EC_MULTISYMBOL
211
static INLINE int aom_read_symbol_(aom_reader *r, aom_cdf_prob *cdf,
Michael Bebenita's avatar
Michael Bebenita committed
212 213
                                   int nsymbs ACCT_STR_PARAM) {
  int ret;
214
#if CONFIG_ANS
215
  (void)nsymbs;
Michael Bebenita's avatar
Michael Bebenita committed
216 217 218
  ret = rans_read(r, cdf);
#elif CONFIG_DAALA_EC
  ret = daala_read_symbol(r, cdf, nsymbs);
219
#else
220 221 222
#error \
    "CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
  "coder. Enable daala_ec or ans for a valid configuration."
223
#endif
224

225
#if CONFIG_EC_ADAPT
226 227 228
  update_cdf(cdf, ret, nsymbs);
#endif

Michael Bebenita's avatar
Michael Bebenita committed
229 230
#if CONFIG_ACCOUNTING
  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
231
#endif
Michael Bebenita's avatar
Michael Bebenita committed
232
  return ret;
233
}
234
#endif  // CONFIG_EC_MULTISYMBOL
235

236 237 238 239
#ifdef __cplusplus
}  // extern "C"
#endif

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