1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_DSP_BITREADER_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_BITREADER_H_
14*77c1e3ccSAndroid Build Coastguard Worker
15*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
17*77c1e3ccSAndroid Build Coastguard Worker
18*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
19*77c1e3ccSAndroid Build Coastguard Worker
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomdx.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/entdec.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/odintrin.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/prob.h"
25*77c1e3ccSAndroid Build Coastguard Worker
26*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
27*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
28*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_BITSTREAM_DEBUG
29*77c1e3ccSAndroid Build Coastguard Worker
30*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/accounting.h"
32*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR_NAME acct_str
33*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR_PARAM , const char *ACCT_STR_NAME
34*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR_ARG(s) , s
35*77c1e3ccSAndroid Build Coastguard Worker #else
36*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR_PARAM
37*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR_ARG(s)
38*77c1e3ccSAndroid Build Coastguard Worker #endif
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker #define aom_read(r, prob, ACCT_STR_NAME) \
41*77c1e3ccSAndroid Build Coastguard Worker aom_read_(r, prob ACCT_STR_ARG(ACCT_STR_NAME))
42*77c1e3ccSAndroid Build Coastguard Worker #define aom_read_bit(r, ACCT_STR_NAME) \
43*77c1e3ccSAndroid Build Coastguard Worker aom_read_bit_(r ACCT_STR_ARG(ACCT_STR_NAME))
44*77c1e3ccSAndroid Build Coastguard Worker #define aom_read_tree(r, tree, probs, ACCT_STR_NAME) \
45*77c1e3ccSAndroid Build Coastguard Worker aom_read_tree_(r, tree, probs ACCT_STR_ARG(ACCT_STR_NAME))
46*77c1e3ccSAndroid Build Coastguard Worker #define aom_read_literal(r, bits, ACCT_STR_NAME) \
47*77c1e3ccSAndroid Build Coastguard Worker aom_read_literal_(r, bits ACCT_STR_ARG(ACCT_STR_NAME))
48*77c1e3ccSAndroid Build Coastguard Worker #define aom_read_cdf(r, cdf, nsymbs, ACCT_STR_NAME) \
49*77c1e3ccSAndroid Build Coastguard Worker aom_read_cdf_(r, cdf, nsymbs ACCT_STR_ARG(ACCT_STR_NAME))
50*77c1e3ccSAndroid Build Coastguard Worker #define aom_read_symbol(r, cdf, nsymbs, ACCT_STR_NAME) \
51*77c1e3ccSAndroid Build Coastguard Worker aom_read_symbol_(r, cdf, nsymbs ACCT_STR_ARG(ACCT_STR_NAME))
52*77c1e3ccSAndroid Build Coastguard Worker
53*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
54*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
55*77c1e3ccSAndroid Build Coastguard Worker #endif
56*77c1e3ccSAndroid Build Coastguard Worker
57*77c1e3ccSAndroid Build Coastguard Worker struct aom_reader {
58*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *buffer;
59*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *buffer_end;
60*77c1e3ccSAndroid Build Coastguard Worker od_ec_dec ec;
61*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
62*77c1e3ccSAndroid Build Coastguard Worker Accounting *accounting;
63*77c1e3ccSAndroid Build Coastguard Worker #endif
64*77c1e3ccSAndroid Build Coastguard Worker uint8_t allow_update_cdf;
65*77c1e3ccSAndroid Build Coastguard Worker };
66*77c1e3ccSAndroid Build Coastguard Worker
67*77c1e3ccSAndroid Build Coastguard Worker typedef struct aom_reader aom_reader;
68*77c1e3ccSAndroid Build Coastguard Worker
69*77c1e3ccSAndroid Build Coastguard Worker int aom_reader_init(aom_reader *r, const uint8_t *buffer, size_t size);
70*77c1e3ccSAndroid Build Coastguard Worker
71*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *aom_reader_find_begin(aom_reader *r);
72*77c1e3ccSAndroid Build Coastguard Worker
73*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *aom_reader_find_end(aom_reader *r);
74*77c1e3ccSAndroid Build Coastguard Worker
75*77c1e3ccSAndroid Build Coastguard Worker // Returns true if the bit reader has tried to decode more data from the buffer
76*77c1e3ccSAndroid Build Coastguard Worker // than was actually provided.
77*77c1e3ccSAndroid Build Coastguard Worker int aom_reader_has_overflowed(const aom_reader *r);
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker // Returns the position in the bit reader in bits.
80*77c1e3ccSAndroid Build Coastguard Worker uint32_t aom_reader_tell(const aom_reader *r);
81*77c1e3ccSAndroid Build Coastguard Worker
82*77c1e3ccSAndroid Build Coastguard Worker // Returns the position in the bit reader in 1/8th bits.
83*77c1e3ccSAndroid Build Coastguard Worker uint32_t aom_reader_tell_frac(const aom_reader *r);
84*77c1e3ccSAndroid Build Coastguard Worker
85*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
aom_process_accounting(const aom_reader * r ACCT_STR_PARAM)86*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_process_accounting(const aom_reader *r ACCT_STR_PARAM) {
87*77c1e3ccSAndroid Build Coastguard Worker if (r->accounting != NULL) {
88*77c1e3ccSAndroid Build Coastguard Worker uint32_t tell_frac;
89*77c1e3ccSAndroid Build Coastguard Worker tell_frac = aom_reader_tell_frac(r);
90*77c1e3ccSAndroid Build Coastguard Worker aom_accounting_record(r->accounting, ACCT_STR_NAME,
91*77c1e3ccSAndroid Build Coastguard Worker tell_frac - r->accounting->last_tell_frac);
92*77c1e3ccSAndroid Build Coastguard Worker r->accounting->last_tell_frac = tell_frac;
93*77c1e3ccSAndroid Build Coastguard Worker }
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker
aom_update_symb_counts(const aom_reader * r,int is_binary)96*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_update_symb_counts(const aom_reader *r, int is_binary) {
97*77c1e3ccSAndroid Build Coastguard Worker if (r->accounting != NULL) {
98*77c1e3ccSAndroid Build Coastguard Worker r->accounting->syms.num_multi_syms += !is_binary;
99*77c1e3ccSAndroid Build Coastguard Worker r->accounting->syms.num_binary_syms += !!is_binary;
100*77c1e3ccSAndroid Build Coastguard Worker }
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker #endif
103*77c1e3ccSAndroid Build Coastguard Worker
aom_read_(aom_reader * r,int prob ACCT_STR_PARAM)104*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_read_(aom_reader *r, int prob ACCT_STR_PARAM) {
105*77c1e3ccSAndroid Build Coastguard Worker int p = (0x7FFFFF - (prob << 15) + prob) >> 8;
106*77c1e3ccSAndroid Build Coastguard Worker int bit = od_ec_decode_bool_q15(&r->ec, p);
107*77c1e3ccSAndroid Build Coastguard Worker
108*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
109*77c1e3ccSAndroid Build Coastguard Worker {
110*77c1e3ccSAndroid Build Coastguard Worker int i;
111*77c1e3ccSAndroid Build Coastguard Worker int ref_bit, ref_nsymbs;
112*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob ref_cdf[16];
113*77c1e3ccSAndroid Build Coastguard Worker const int queue_r = bitstream_queue_get_read();
114*77c1e3ccSAndroid Build Coastguard Worker const int frame_idx = aom_bitstream_queue_get_frame_read();
115*77c1e3ccSAndroid Build Coastguard Worker bitstream_queue_pop(&ref_bit, ref_cdf, &ref_nsymbs);
116*77c1e3ccSAndroid Build Coastguard Worker if (ref_nsymbs != 2) {
117*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
118*77c1e3ccSAndroid Build Coastguard Worker "\n *** [bit] nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs "
119*77c1e3ccSAndroid Build Coastguard Worker "%d queue_r %d\n",
120*77c1e3ccSAndroid Build Coastguard Worker frame_idx, 2, ref_nsymbs, queue_r);
121*77c1e3ccSAndroid Build Coastguard Worker assert(0);
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker if ((ref_nsymbs != 2) || (ref_cdf[0] != (aom_cdf_prob)p) ||
124*77c1e3ccSAndroid Build Coastguard Worker (ref_cdf[1] != 32767)) {
125*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
126*77c1e3ccSAndroid Build Coastguard Worker "\n *** [bit] cdf error, frame_idx_r %d cdf {%d, %d} ref_cdf {%d",
127*77c1e3ccSAndroid Build Coastguard Worker frame_idx, p, 32767, ref_cdf[0]);
128*77c1e3ccSAndroid Build Coastguard Worker for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
129*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "} queue_r %d\n", queue_r);
130*77c1e3ccSAndroid Build Coastguard Worker assert(0);
131*77c1e3ccSAndroid Build Coastguard Worker }
132*77c1e3ccSAndroid Build Coastguard Worker if (bit != ref_bit) {
133*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
134*77c1e3ccSAndroid Build Coastguard Worker "\n *** [bit] symb error, frame_idx_r %d symb %d ref_symb %d "
135*77c1e3ccSAndroid Build Coastguard Worker "queue_r %d\n",
136*77c1e3ccSAndroid Build Coastguard Worker frame_idx, bit, ref_bit, queue_r);
137*77c1e3ccSAndroid Build Coastguard Worker assert(0);
138*77c1e3ccSAndroid Build Coastguard Worker }
139*77c1e3ccSAndroid Build Coastguard Worker }
140*77c1e3ccSAndroid Build Coastguard Worker #endif
141*77c1e3ccSAndroid Build Coastguard Worker
142*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
143*77c1e3ccSAndroid Build Coastguard Worker if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
144*77c1e3ccSAndroid Build Coastguard Worker aom_update_symb_counts(r, 1);
145*77c1e3ccSAndroid Build Coastguard Worker #endif
146*77c1e3ccSAndroid Build Coastguard Worker return bit;
147*77c1e3ccSAndroid Build Coastguard Worker }
148*77c1e3ccSAndroid Build Coastguard Worker
aom_read_bit_(aom_reader * r ACCT_STR_PARAM)149*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_read_bit_(aom_reader *r ACCT_STR_PARAM) {
150*77c1e3ccSAndroid Build Coastguard Worker int ret;
151*77c1e3ccSAndroid Build Coastguard Worker ret = aom_read(r, 128, NULL); // aom_prob_half
152*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
153*77c1e3ccSAndroid Build Coastguard Worker if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
154*77c1e3ccSAndroid Build Coastguard Worker #endif
155*77c1e3ccSAndroid Build Coastguard Worker return ret;
156*77c1e3ccSAndroid Build Coastguard Worker }
157*77c1e3ccSAndroid Build Coastguard Worker
aom_read_literal_(aom_reader * r,int bits ACCT_STR_PARAM)158*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_read_literal_(aom_reader *r, int bits ACCT_STR_PARAM) {
159*77c1e3ccSAndroid Build Coastguard Worker int literal = 0, bit;
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r, NULL) << bit;
162*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
163*77c1e3ccSAndroid Build Coastguard Worker if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
164*77c1e3ccSAndroid Build Coastguard Worker #endif
165*77c1e3ccSAndroid Build Coastguard Worker return literal;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker
aom_read_cdf_(aom_reader * r,const aom_cdf_prob * cdf,int nsymbs ACCT_STR_PARAM)168*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_read_cdf_(aom_reader *r, const aom_cdf_prob *cdf,
169*77c1e3ccSAndroid Build Coastguard Worker int nsymbs ACCT_STR_PARAM) {
170*77c1e3ccSAndroid Build Coastguard Worker int symb;
171*77c1e3ccSAndroid Build Coastguard Worker assert(cdf != NULL);
172*77c1e3ccSAndroid Build Coastguard Worker symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs);
173*77c1e3ccSAndroid Build Coastguard Worker
174*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
175*77c1e3ccSAndroid Build Coastguard Worker {
176*77c1e3ccSAndroid Build Coastguard Worker int i;
177*77c1e3ccSAndroid Build Coastguard Worker int cdf_error = 0;
178*77c1e3ccSAndroid Build Coastguard Worker int ref_symb, ref_nsymbs;
179*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob ref_cdf[16];
180*77c1e3ccSAndroid Build Coastguard Worker const int queue_r = bitstream_queue_get_read();
181*77c1e3ccSAndroid Build Coastguard Worker const int frame_idx = aom_bitstream_queue_get_frame_read();
182*77c1e3ccSAndroid Build Coastguard Worker bitstream_queue_pop(&ref_symb, ref_cdf, &ref_nsymbs);
183*77c1e3ccSAndroid Build Coastguard Worker if (nsymbs != ref_nsymbs) {
184*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
185*77c1e3ccSAndroid Build Coastguard Worker "\n *** nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs %d "
186*77c1e3ccSAndroid Build Coastguard Worker "queue_r %d\n",
187*77c1e3ccSAndroid Build Coastguard Worker frame_idx, nsymbs, ref_nsymbs, queue_r);
188*77c1e3ccSAndroid Build Coastguard Worker cdf_error = 0;
189*77c1e3ccSAndroid Build Coastguard Worker assert(0);
190*77c1e3ccSAndroid Build Coastguard Worker } else {
191*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < nsymbs; ++i)
192*77c1e3ccSAndroid Build Coastguard Worker if (cdf[i] != ref_cdf[i]) cdf_error = 1;
193*77c1e3ccSAndroid Build Coastguard Worker }
194*77c1e3ccSAndroid Build Coastguard Worker if (cdf_error) {
195*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "\n *** cdf error, frame_idx_r %d cdf {%d", frame_idx,
196*77c1e3ccSAndroid Build Coastguard Worker cdf[0]);
197*77c1e3ccSAndroid Build Coastguard Worker for (i = 1; i < nsymbs; ++i) fprintf(stderr, ", %d", cdf[i]);
198*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "} ref_cdf {%d", ref_cdf[0]);
199*77c1e3ccSAndroid Build Coastguard Worker for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
200*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "} queue_r %d\n", queue_r);
201*77c1e3ccSAndroid Build Coastguard Worker assert(0);
202*77c1e3ccSAndroid Build Coastguard Worker }
203*77c1e3ccSAndroid Build Coastguard Worker if (symb != ref_symb) {
204*77c1e3ccSAndroid Build Coastguard Worker fprintf(
205*77c1e3ccSAndroid Build Coastguard Worker stderr,
206*77c1e3ccSAndroid Build Coastguard Worker "\n *** symb error, frame_idx_r %d symb %d ref_symb %d queue_r %d\n",
207*77c1e3ccSAndroid Build Coastguard Worker frame_idx, symb, ref_symb, queue_r);
208*77c1e3ccSAndroid Build Coastguard Worker assert(0);
209*77c1e3ccSAndroid Build Coastguard Worker }
210*77c1e3ccSAndroid Build Coastguard Worker }
211*77c1e3ccSAndroid Build Coastguard Worker #endif
212*77c1e3ccSAndroid Build Coastguard Worker
213*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
214*77c1e3ccSAndroid Build Coastguard Worker if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
215*77c1e3ccSAndroid Build Coastguard Worker aom_update_symb_counts(r, (nsymbs == 2));
216*77c1e3ccSAndroid Build Coastguard Worker #endif
217*77c1e3ccSAndroid Build Coastguard Worker return symb;
218*77c1e3ccSAndroid Build Coastguard Worker }
219*77c1e3ccSAndroid Build Coastguard Worker
aom_read_symbol_(aom_reader * r,aom_cdf_prob * cdf,int nsymbs ACCT_STR_PARAM)220*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_read_symbol_(aom_reader *r, aom_cdf_prob *cdf,
221*77c1e3ccSAndroid Build Coastguard Worker int nsymbs ACCT_STR_PARAM) {
222*77c1e3ccSAndroid Build Coastguard Worker int ret;
223*77c1e3ccSAndroid Build Coastguard Worker ret = aom_read_cdf(r, cdf, nsymbs, ACCT_STR_NAME);
224*77c1e3ccSAndroid Build Coastguard Worker if (r->allow_update_cdf) update_cdf(cdf, ret, nsymbs);
225*77c1e3ccSAndroid Build Coastguard Worker return ret;
226*77c1e3ccSAndroid Build Coastguard Worker }
227*77c1e3ccSAndroid Build Coastguard Worker
228*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
229*77c1e3ccSAndroid Build Coastguard Worker } // extern "C"
230*77c1e3ccSAndroid Build Coastguard Worker #endif
231*77c1e3ccSAndroid Build Coastguard Worker
232*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AOM_DSP_BITREADER_H_
233