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 |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 21 | #if CONFIG_BITSTREAM_DEBUG |
| 22 | #include <stdio.h> |
| 23 | #include "aom_util/debug_util.h" |
| 24 | #endif // CONFIG_BITSTREAM_DEBUG |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | struct daala_reader { |
| 31 | const uint8_t *buffer; |
| 32 | const uint8_t *buffer_end; |
| 33 | od_ec_dec ec; |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 34 | #if CONFIG_ACCOUNTING |
| 35 | Accounting *accounting; |
| 36 | #endif |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | typedef struct daala_reader daala_reader; |
| 40 | |
| 41 | int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size); |
| 42 | const uint8_t *aom_daala_reader_find_end(daala_reader *r); |
Nathan E. Egge | b244f39 | 2016-09-06 23:48:43 -0400 | [diff] [blame] | 43 | uint32_t aom_daala_reader_tell(const daala_reader *r); |
| 44 | uint32_t aom_daala_reader_tell_frac(const daala_reader *r); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 45 | |
| 46 | static INLINE int aom_daala_read(daala_reader *r, int prob) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 47 | int bit; |
| 48 | int p = ((prob << 15) + (256 - prob)) >> 8; |
| 49 | #if CONFIG_BITSTREAM_DEBUG |
David Barker | 4c12cc5 | 2016-11-17 14:43:33 +0000 | [diff] [blame] | 50 | /*{ |
| 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. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 56 | } |
David Barker | 4c12cc5 | 2016-11-17 14:43:33 +0000 | [diff] [blame] | 57 | }*/ |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | if (prob == 128) { |
| 61 | bit = od_ec_dec_bits(&r->ec, 1, "aom_bits"); |
| 62 | } else { |
| 63 | bit = od_ec_decode_bool_q15(&r->ec, p); |
| 64 | } |
| 65 | |
| 66 | #if CONFIG_BITSTREAM_DEBUG |
| 67 | { |
| 68 | int i; |
| 69 | int ref_bit, ref_nsymbs; |
| 70 | aom_cdf_prob ref_cdf[16]; |
| 71 | const int queue_r = bitstream_queue_get_read(); |
| 72 | const int frame_idx = bitstream_queue_get_frame_read(); |
| 73 | bitstream_queue_pop(&ref_bit, ref_cdf, &ref_nsymbs); |
| 74 | if (ref_nsymbs != 2) { |
| 75 | fprintf(stderr, |
| 76 | "\n *** [bit] nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs " |
| 77 | "%d queue_r %d\n", |
| 78 | frame_idx, 2, ref_nsymbs, queue_r); |
| 79 | assert(0); |
| 80 | } |
| 81 | if ((ref_nsymbs != 2) || (ref_cdf[0] != (aom_cdf_prob)p) || |
| 82 | (ref_cdf[1] != 32767)) { |
| 83 | fprintf(stderr, |
| 84 | "\n *** [bit] cdf error, frame_idx_r %d cdf {%d, %d} ref_cdf {%d", |
| 85 | frame_idx, p, 32767, ref_cdf[0]); |
| 86 | for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]); |
| 87 | fprintf(stderr, "} queue_r %d\n", queue_r); |
| 88 | assert(0); |
| 89 | } |
| 90 | if (bit != ref_bit) { |
| 91 | fprintf(stderr, |
| 92 | "\n *** [bit] symb error, frame_idx_r %d symb %d ref_symb %d " |
| 93 | "queue_r %d\n", |
| 94 | frame_idx, bit, ref_bit, queue_r); |
| 95 | assert(0); |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | return bit; |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static INLINE int aom_daala_read_bit(daala_reader *r) { |
| 104 | return aom_daala_read(r, 128); |
| 105 | } |
| 106 | |
| 107 | static INLINE int aom_daala_reader_has_error(daala_reader *r) { |
| 108 | return r->ec.error; |
| 109 | } |
| 110 | |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 111 | static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf, |
| 112 | int nsymbs) { |
| 113 | int symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs); |
| 114 | |
| 115 | #if CONFIG_BITSTREAM_DEBUG |
| 116 | { |
| 117 | int i; |
| 118 | int cdf_error = 0; |
| 119 | int ref_symb, ref_nsymbs; |
| 120 | aom_cdf_prob ref_cdf[16]; |
| 121 | const int queue_r = bitstream_queue_get_read(); |
| 122 | const int frame_idx = bitstream_queue_get_frame_read(); |
| 123 | bitstream_queue_pop(&ref_symb, ref_cdf, &ref_nsymbs); |
| 124 | if (nsymbs != ref_nsymbs) { |
| 125 | fprintf(stderr, |
| 126 | "\n *** nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs %d " |
| 127 | "queue_r %d\n", |
| 128 | frame_idx, nsymbs, ref_nsymbs, queue_r); |
| 129 | cdf_error = 0; |
| 130 | assert(0); |
| 131 | } else { |
| 132 | for (i = 0; i < nsymbs; ++i) |
| 133 | if (cdf[i] != ref_cdf[i]) cdf_error = 1; |
| 134 | } |
| 135 | if (cdf_error) { |
| 136 | fprintf(stderr, "\n *** cdf error, frame_idx_r %d cdf {%d", frame_idx, |
| 137 | cdf[0]); |
| 138 | for (i = 1; i < nsymbs; ++i) fprintf(stderr, ", %d", cdf[i]); |
| 139 | fprintf(stderr, "} ref_cdf {%d", ref_cdf[0]); |
| 140 | for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]); |
| 141 | fprintf(stderr, "} queue_r %d\n", queue_r); |
| 142 | assert(0); |
| 143 | } |
| 144 | if (symb != ref_symb) { |
| 145 | fprintf( |
| 146 | stderr, |
| 147 | "\n *** symb error, frame_idx_r %d symb %d ref_symb %d queue_r %d\n", |
| 148 | frame_idx, symb, ref_symb, queue_r); |
| 149 | assert(0); |
| 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | return symb; |
| 155 | } |
| 156 | |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 157 | static INLINE int daala_read_tree_bits(daala_reader *r, |
| 158 | const aom_tree_index *tree, |
| 159 | const aom_prob *probs) { |
| 160 | aom_tree_index i = 0; |
| 161 | do { |
Nathan E. Egge | 9ac1f7d | 2016-08-19 17:16:31 -0400 | [diff] [blame] | 162 | aom_cdf_prob cdf[16]; |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 163 | aom_tree_index index[16]; |
| 164 | int path[16]; |
| 165 | int dist[16]; |
| 166 | int nsymbs; |
| 167 | int symb; |
| 168 | nsymbs = tree_to_cdf(tree, probs, i, cdf, index, path, dist); |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 169 | symb = daala_read_symbol(r, cdf, nsymbs); |
Nathan E. Egge | 43acafd | 2016-03-06 13:41:53 -0500 | [diff] [blame] | 170 | OD_ASSERT(symb >= 0 && symb < nsymbs); |
| 171 | i = index[symb]; |
| 172 | } while (i > 0); |
| 173 | return -i; |
| 174 | } |
| 175 | |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 176 | #ifdef __cplusplus |
| 177 | } // extern "C" |
| 178 | #endif |
| 179 | |
| 180 | #endif |