blob: 9d6cebdbe0ec87bf7df8b871ef9de6acf218de09 [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
Nathan E. Egge8043cc42016-03-06 12:42:47 -050021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26struct daala_reader {
27 const uint8_t *buffer;
28 const uint8_t *buffer_end;
29 od_ec_dec ec;
Michael Bebenita6048d052016-08-25 14:40:54 -070030#if CONFIG_ACCOUNTING
31 Accounting *accounting;
32#endif
Nathan E. Egge8043cc42016-03-06 12:42:47 -050033};
34
35typedef struct daala_reader daala_reader;
36
37int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size);
38const uint8_t *aom_daala_reader_find_end(daala_reader *r);
Nathan E. Eggeb244f392016-09-06 23:48:43 -040039uint32_t aom_daala_reader_tell(const daala_reader *r);
40uint32_t aom_daala_reader_tell_frac(const daala_reader *r);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050041
42static INLINE int aom_daala_read(daala_reader *r, int prob) {
43 if (prob == 128) {
Michael Bebenita63b44c42016-08-23 16:03:39 -070044 return od_ec_dec_bits(&r->ec, 1);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050045 } else {
46 int p = ((prob << 15) + (256 - prob)) >> 8;
Michael Bebenita63b44c42016-08-23 16:03:39 -070047 return od_ec_decode_bool_q15(&r->ec, p);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050048 }
49}
50
51static INLINE int aom_daala_read_bit(daala_reader *r) {
52 return aom_daala_read(r, 128);
53}
54
55static INLINE int aom_daala_reader_has_error(daala_reader *r) {
56 return r->ec.error;
57}
58
Nathan E. Egge43acafd2016-03-06 13:41:53 -050059static INLINE int daala_read_tree_bits(daala_reader *r,
60 const aom_tree_index *tree,
61 const aom_prob *probs) {
62 aom_tree_index i = 0;
63 do {
Nathan E. Egge9ac1f7d2016-08-19 17:16:31 -040064 aom_cdf_prob cdf[16];
Nathan E. Egge43acafd2016-03-06 13:41:53 -050065 aom_tree_index index[16];
66 int path[16];
67 int dist[16];
68 int nsymbs;
69 int symb;
70 nsymbs = tree_to_cdf(tree, probs, i, cdf, index, path, dist);
Michael Bebenita63b44c42016-08-23 16:03:39 -070071 symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs);
Nathan E. Egge43acafd2016-03-06 13:41:53 -050072 OD_ASSERT(symb >= 0 && symb < nsymbs);
73 i = index[symb];
74 } while (i > 0);
75 return -i;
76}
77
Nathan E. Egge9ac1f7d2016-08-19 17:16:31 -040078static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf,
Nathan E. Egge45741e92016-08-19 15:31:43 -040079 int nsymbs) {
Michael Bebenita63b44c42016-08-23 16:03:39 -070080 return od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs);
Nathan E. Egge44460142016-06-20 13:44:22 -040081}
82
Nathan E. Egge8043cc42016-03-06 12:42:47 -050083#ifdef __cplusplus
84} // extern "C"
85#endif
86
87#endif