bitreader.h 6.65 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
#if CONFIG_ANS
  (void)decrypt_cb;
  (void)decrypt_state;
Alex Converse's avatar
Alex Converse committed
76
  if (size > INT_MAX) return 1;
77
  return ans_read_init(r, buffer, (int)size);
78
79
80
#elif CONFIG_DAALA_EC
  (void)decrypt_cb;
  (void)decrypt_state;
Yaowu Xu's avatar
Yaowu Xu committed
81
  return aom_daala_reader_init(r, buffer, (int)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
167
168
#elif CONFIG_DAALA_EC
  // Note this uses raw bits and is not the same as aom_daala_read(r, 128);
  ret = aom_daala_read_bit(r);
169
#else
Michael Bebenita's avatar
Michael Bebenita committed
170
171
172
173
  ret = aom_read(r, 128, NULL);  // aom_prob_half
#endif
#if CONFIG_ACCOUNTING
  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
174
#endif
Michael Bebenita's avatar
Michael Bebenita committed
175
  return ret;
176
}
177

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

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

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

Michael Bebenita's avatar
Michael Bebenita committed
192
193
194
195
  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
196
  return -i;
197
}
198

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

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

228
#if CONFIG_EC_ADAPT
229
230
231
  update_cdf(cdf, ret, nsymbs);
#endif

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

239
240
241
242
#ifdef __cplusplus
}  // extern "C"
#endif

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