blob: 6376bad4aa5020bf27d39054ea32fcc0b8a8faf0 [file] [log] [blame]
Nathan E. Egge8043cc42016-03-06 12:42:47 -05001/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * 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.
10 */
11
12#ifndef AOM_DSP_DAALABOOLREADER_H_
13#define AOM_DSP_DAALABOOLREADER_H_
14
Michael Bebenita6048d052016-08-25 14:40:54 -070015#include "aom/aom_integer.h"
Nathan E. Egge8043cc42016-03-06 12:42:47 -050016#include "aom_dsp/entdec.h"
17#include "aom_dsp/prob.h"
Michael Bebenita6048d052016-08-25 14:40:54 -070018#if CONFIG_ACCOUNTING
19#include "av1/common/accounting.h"
20#endif
David Barkerfa2865b2016-11-11 09:25:56 +000021#if CONFIG_BITSTREAM_DEBUG
22#include <stdio.h>
23#include "aom_util/debug_util.h"
24#endif // CONFIG_BITSTREAM_DEBUG
Nathan E. Egge8043cc42016-03-06 12:42:47 -050025
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30struct daala_reader {
31 const uint8_t *buffer;
32 const uint8_t *buffer_end;
33 od_ec_dec ec;
Michael Bebenita6048d052016-08-25 14:40:54 -070034#if CONFIG_ACCOUNTING
35 Accounting *accounting;
36#endif
Nathan E. Egge8043cc42016-03-06 12:42:47 -050037};
38
39typedef struct daala_reader daala_reader;
40
41int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size);
42const uint8_t *aom_daala_reader_find_end(daala_reader *r);
Nathan E. Eggeb244f392016-09-06 23:48:43 -040043uint32_t aom_daala_reader_tell(const daala_reader *r);
44uint32_t aom_daala_reader_tell_frac(const daala_reader *r);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050045
46static INLINE int aom_daala_read(daala_reader *r, int prob) {
David Barkerfa2865b2016-11-11 09:25:56 +000047 int bit;
48 int p = ((prob << 15) + (256 - prob)) >> 8;
49#if CONFIG_BITSTREAM_DEBUG
David Barker4c12cc52016-11-17 14:43:33 +000050/*{
51 const int queue_r = bitstream_queue_get_read();
52 const int frame_idx = bitstream_queue_get_frame_read();
53 if (frame_idx == 0 && queue_r == 0) {
54 fprintf(stderr, "\n *** bitstream queue at frame_idx_r %d queue_r %d\n",
55 frame_idx, queue_r);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050056 }
David Barker4c12cc52016-11-17 14:43:33 +000057}*/
David Barkerfa2865b2016-11-11 09:25:56 +000058#endif
59
Nathan E. Egge08c99eb2016-11-09 13:40:26 -050060 bit = od_ec_decode_bool_q15(&r->ec, p);
David Barkerfa2865b2016-11-11 09:25:56 +000061
62#if CONFIG_BITSTREAM_DEBUG
63 {
64 int i;
65 int ref_bit, ref_nsymbs;
66 aom_cdf_prob ref_cdf[16];
67 const int queue_r = bitstream_queue_get_read();
68 const int frame_idx = bitstream_queue_get_frame_read();
69 bitstream_queue_pop(&ref_bit, ref_cdf, &ref_nsymbs);
70 if (ref_nsymbs != 2) {
71 fprintf(stderr,
72 "\n *** [bit] nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs "
73 "%d queue_r %d\n",
74 frame_idx, 2, ref_nsymbs, queue_r);
75 assert(0);
76 }
77 if ((ref_nsymbs != 2) || (ref_cdf[0] != (aom_cdf_prob)p) ||
78 (ref_cdf[1] != 32767)) {
79 fprintf(stderr,
80 "\n *** [bit] cdf error, frame_idx_r %d cdf {%d, %d} ref_cdf {%d",
81 frame_idx, p, 32767, ref_cdf[0]);
82 for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
83 fprintf(stderr, "} queue_r %d\n", queue_r);
84 assert(0);
85 }
86 if (bit != ref_bit) {
87 fprintf(stderr,
88 "\n *** [bit] symb error, frame_idx_r %d symb %d ref_symb %d "
89 "queue_r %d\n",
90 frame_idx, bit, ref_bit, queue_r);
91 assert(0);
92 }
93 }
94#endif
95
96 return bit;
Nathan E. Egge8043cc42016-03-06 12:42:47 -050097}
98
99static INLINE int aom_daala_read_bit(daala_reader *r) {
Nathan E. Egge08c99eb2016-11-09 13:40:26 -0500100 return od_ec_dec_bits(&r->ec, 1, "aom_bits");
Nathan E. Egge8043cc42016-03-06 12:42:47 -0500101}
102
103static INLINE int aom_daala_reader_has_error(daala_reader *r) {
104 return r->ec.error;
105}
106
David Barkerfa2865b2016-11-11 09:25:56 +0000107static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf,
108 int nsymbs) {
109 int symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs);
110
111#if CONFIG_BITSTREAM_DEBUG
112 {
113 int i;
114 int cdf_error = 0;
115 int ref_symb, ref_nsymbs;
116 aom_cdf_prob ref_cdf[16];
117 const int queue_r = bitstream_queue_get_read();
118 const int frame_idx = bitstream_queue_get_frame_read();
119 bitstream_queue_pop(&ref_symb, ref_cdf, &ref_nsymbs);
120 if (nsymbs != ref_nsymbs) {
121 fprintf(stderr,
122 "\n *** nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs %d "
123 "queue_r %d\n",
124 frame_idx, nsymbs, ref_nsymbs, queue_r);
125 cdf_error = 0;
126 assert(0);
127 } else {
128 for (i = 0; i < nsymbs; ++i)
129 if (cdf[i] != ref_cdf[i]) cdf_error = 1;
130 }
131 if (cdf_error) {
132 fprintf(stderr, "\n *** cdf error, frame_idx_r %d cdf {%d", frame_idx,
133 cdf[0]);
134 for (i = 1; i < nsymbs; ++i) fprintf(stderr, ", %d", cdf[i]);
135 fprintf(stderr, "} ref_cdf {%d", ref_cdf[0]);
136 for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
137 fprintf(stderr, "} queue_r %d\n", queue_r);
138 assert(0);
139 }
140 if (symb != ref_symb) {
141 fprintf(
142 stderr,
143 "\n *** symb error, frame_idx_r %d symb %d ref_symb %d queue_r %d\n",
144 frame_idx, symb, ref_symb, queue_r);
145 assert(0);
146 }
147 }
148#endif
149
150 return symb;
151}
152
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500153static INLINE int daala_read_tree_bits(daala_reader *r,
154 const aom_tree_index *tree,
155 const aom_prob *probs) {
156 aom_tree_index i = 0;
157 do {
Nathan E. Egge9ac1f7d2016-08-19 17:16:31 -0400158 aom_cdf_prob cdf[16];
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500159 aom_tree_index index[16];
160 int path[16];
161 int dist[16];
162 int nsymbs;
163 int symb;
164 nsymbs = tree_to_cdf(tree, probs, i, cdf, index, path, dist);
David Barkerfa2865b2016-11-11 09:25:56 +0000165 symb = daala_read_symbol(r, cdf, nsymbs);
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500166 OD_ASSERT(symb >= 0 && symb < nsymbs);
167 i = index[symb];
168 } while (i > 0);
169 return -i;
170}
171
Nathan E. Egge8043cc42016-03-06 12:42:47 -0500172#ifdef __cplusplus
173} // extern "C"
174#endif
175
176#endif