Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 1 | /* |
| 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 Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 15 | #include "aom/aom_integer.h" |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 16 | #include "aom_dsp/entdec.h" |
| 17 | #include "aom_dsp/prob.h" |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 18 | #if CONFIG_ACCOUNTING |
| 19 | #include "av1/common/accounting.h" |
| 20 | #endif |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | struct daala_reader { |
| 27 | const uint8_t *buffer; |
| 28 | const uint8_t *buffer_end; |
| 29 | od_ec_dec ec; |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 30 | #if CONFIG_ACCOUNTING |
| 31 | Accounting *accounting; |
| 32 | #endif |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | typedef struct daala_reader daala_reader; |
| 36 | |
| 37 | int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size); |
| 38 | const uint8_t *aom_daala_reader_find_end(daala_reader *r); |
Nathan E. Egge | b244f39 | 2016-09-06 23:48:43 -0400 | [diff] [blame] | 39 | uint32_t aom_daala_reader_tell(const daala_reader *r); |
| 40 | uint32_t aom_daala_reader_tell_frac(const daala_reader *r); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 41 | |
| 42 | static INLINE int aom_daala_read(daala_reader *r, int prob) { |
| 43 | if (prob == 128) { |
Michael Bebenita | 63b44c4 | 2016-08-23 16:03:39 -0700 | [diff] [blame] | 44 | return od_ec_dec_bits(&r->ec, 1); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 45 | } else { |
| 46 | int p = ((prob << 15) + (256 - prob)) >> 8; |
Michael Bebenita | 63b44c4 | 2016-08-23 16:03:39 -0700 | [diff] [blame] | 47 | return od_ec_decode_bool_q15(&r->ec, p); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | static INLINE int aom_daala_read_bit(daala_reader *r) { |
| 52 | return aom_daala_read(r, 128); |
| 53 | } |
| 54 | |
| 55 | static INLINE int aom_daala_reader_has_error(daala_reader *r) { |
| 56 | return r->ec.error; |
| 57 | } |
| 58 | |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 59 | static 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. Egge | 9ac1f7d | 2016-08-19 17:16:31 -0400 | [diff] [blame] | 64 | aom_cdf_prob cdf[16]; |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 65 | 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 Bebenita | 63b44c4 | 2016-08-23 16:03:39 -0700 | [diff] [blame] | 71 | symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs); |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 72 | OD_ASSERT(symb >= 0 && symb < nsymbs); |
| 73 | i = index[symb]; |
| 74 | } while (i > 0); |
| 75 | return -i; |
| 76 | } |
| 77 | |
Nathan E. Egge | 9ac1f7d | 2016-08-19 17:16:31 -0400 | [diff] [blame] | 78 | static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf, |
Nathan E. Egge | 45741e9 | 2016-08-19 15:31:43 -0400 | [diff] [blame] | 79 | int nsymbs) { |
Michael Bebenita | 63b44c4 | 2016-08-23 16:03:39 -0700 | [diff] [blame] | 80 | return od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs); |
Nathan E. Egge | 4446014 | 2016-06-20 13:44:22 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | } // extern "C" |
| 85 | #endif |
| 86 | |
| 87 | #endif |