blob: 0ed0de4ac29364f057cfeb26e08ed90cd82d103e [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
15#include "aom_dsp/entdec.h"
16#include "aom_dsp/prob.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22struct daala_reader {
23 const uint8_t *buffer;
24 const uint8_t *buffer_end;
25 od_ec_dec ec;
26};
27
28typedef struct daala_reader daala_reader;
29
30int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size);
31const uint8_t *aom_daala_reader_find_end(daala_reader *r);
32
33static INLINE int aom_daala_read(daala_reader *r, int prob) {
34 if (prob == 128) {
35 return od_ec_dec_bits(&r->ec, 1, "aom_bits");
36 } else {
37 int p = ((prob << 15) + (256 - prob)) >> 8;
38 return od_ec_decode_bool_q15(&r->ec, p, "aom");
39 }
40}
41
42static INLINE int aom_daala_read_bit(daala_reader *r) {
43 return aom_daala_read(r, 128);
44}
45
46static INLINE int aom_daala_reader_has_error(daala_reader *r) {
47 return r->ec.error;
48}
49
Nathan E. Egge43acafd2016-03-06 13:41:53 -050050static INLINE int daala_read_tree_bits(daala_reader *r,
51 const aom_tree_index *tree,
52 const aom_prob *probs) {
53 aom_tree_index i = 0;
54 do {
Nathan E. Egge9ac1f7d2016-08-19 17:16:31 -040055 aom_cdf_prob cdf[16];
Nathan E. Egge43acafd2016-03-06 13:41:53 -050056 aom_tree_index index[16];
57 int path[16];
58 int dist[16];
59 int nsymbs;
60 int symb;
61 nsymbs = tree_to_cdf(tree, probs, i, cdf, index, path, dist);
62 symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs, "aom");
63 OD_ASSERT(symb >= 0 && symb < nsymbs);
64 i = index[symb];
65 } while (i > 0);
66 return -i;
67}
68
Nathan E. Egge9ac1f7d2016-08-19 17:16:31 -040069static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf,
Nathan E. Egge45741e92016-08-19 15:31:43 -040070 int nsymbs) {
Nathan E. Egge44460142016-06-20 13:44:22 -040071 return od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs, "aom");
72}
73
Nathan E. Egge8043cc42016-03-06 12:42:47 -050074#ifdef __cplusplus
75} // extern "C"
76#endif
77
78#endif