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 |
Luc Trudeau | 83fbd57 | 2017-04-21 11:24:34 -0400 | [diff] [blame] | 19 | #include "av1/decoder/accounting.h" |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 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 |
Yunqing Wang | 0e141b5 | 2017-11-02 15:08:58 -0700 | [diff] [blame] | 37 | uint8_t allow_update_cdf; |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | typedef struct daala_reader daala_reader; |
| 41 | |
| 42 | int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size); |
| 43 | const uint8_t *aom_daala_reader_find_end(daala_reader *r); |
Nathan E. Egge | b244f39 | 2016-09-06 23:48:43 -0400 | [diff] [blame] | 44 | uint32_t aom_daala_reader_tell(const daala_reader *r); |
| 45 | uint32_t aom_daala_reader_tell_frac(const daala_reader *r); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 46 | |
| 47 | static INLINE int aom_daala_read(daala_reader *r, int prob) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 48 | int bit; |
Timothy B. Terriberry | d5b89d0 | 2017-03-25 14:28:50 -0700 | [diff] [blame] | 49 | int p = (0x7FFFFF - (prob << 15) + prob) >> 8; |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 50 | #if CONFIG_BITSTREAM_DEBUG |
David Barker | 4c12cc5 | 2016-11-17 14:43:33 +0000 | [diff] [blame] | 51 | /*{ |
| 52 | const int queue_r = bitstream_queue_get_read(); |
| 53 | const int frame_idx = bitstream_queue_get_frame_read(); |
| 54 | if (frame_idx == 0 && queue_r == 0) { |
| 55 | fprintf(stderr, "\n *** bitstream queue at frame_idx_r %d queue_r %d\n", |
| 56 | frame_idx, queue_r); |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 57 | } |
David Barker | 4c12cc5 | 2016-11-17 14:43:33 +0000 | [diff] [blame] | 58 | }*/ |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Timothy B. Terriberry | d5b89d0 | 2017-03-25 14:28:50 -0700 | [diff] [blame] | 61 | bit = od_ec_decode_bool_q15(&r->ec, p); |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 62 | |
| 63 | #if CONFIG_BITSTREAM_DEBUG |
| 64 | { |
| 65 | int i; |
| 66 | int ref_bit, ref_nsymbs; |
| 67 | aom_cdf_prob ref_cdf[16]; |
| 68 | const int queue_r = bitstream_queue_get_read(); |
| 69 | const int frame_idx = bitstream_queue_get_frame_read(); |
| 70 | bitstream_queue_pop(&ref_bit, ref_cdf, &ref_nsymbs); |
| 71 | if (ref_nsymbs != 2) { |
| 72 | fprintf(stderr, |
| 73 | "\n *** [bit] nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs " |
| 74 | "%d queue_r %d\n", |
| 75 | frame_idx, 2, ref_nsymbs, queue_r); |
| 76 | assert(0); |
| 77 | } |
| 78 | if ((ref_nsymbs != 2) || (ref_cdf[0] != (aom_cdf_prob)p) || |
| 79 | (ref_cdf[1] != 32767)) { |
| 80 | fprintf(stderr, |
| 81 | "\n *** [bit] cdf error, frame_idx_r %d cdf {%d, %d} ref_cdf {%d", |
| 82 | frame_idx, p, 32767, ref_cdf[0]); |
| 83 | for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]); |
| 84 | fprintf(stderr, "} queue_r %d\n", queue_r); |
| 85 | assert(0); |
| 86 | } |
| 87 | if (bit != ref_bit) { |
| 88 | fprintf(stderr, |
| 89 | "\n *** [bit] symb error, frame_idx_r %d symb %d ref_symb %d " |
| 90 | "queue_r %d\n", |
| 91 | frame_idx, bit, ref_bit, queue_r); |
| 92 | assert(0); |
| 93 | } |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | return bit; |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 100 | static INLINE int aom_daala_reader_has_error(daala_reader *r) { |
| 101 | return r->ec.error; |
| 102 | } |
| 103 | |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 104 | static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf, |
| 105 | int nsymbs) { |
Timothy B. Terriberry | ead5287 | 2017-03-07 20:27:34 -0800 | [diff] [blame] | 106 | int symb; |
Debargha Mukherjee | 4c1cb1f | 2017-10-04 23:08:57 -0700 | [diff] [blame] | 107 | assert(cdf != NULL); |
Timothy B. Terriberry | ead5287 | 2017-03-07 20:27:34 -0800 | [diff] [blame] | 108 | symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs); |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 109 | |
| 110 | #if CONFIG_BITSTREAM_DEBUG |
| 111 | { |
| 112 | int i; |
| 113 | int cdf_error = 0; |
| 114 | int ref_symb, ref_nsymbs; |
| 115 | aom_cdf_prob ref_cdf[16]; |
| 116 | const int queue_r = bitstream_queue_get_read(); |
| 117 | const int frame_idx = bitstream_queue_get_frame_read(); |
| 118 | bitstream_queue_pop(&ref_symb, ref_cdf, &ref_nsymbs); |
| 119 | if (nsymbs != ref_nsymbs) { |
| 120 | fprintf(stderr, |
| 121 | "\n *** nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs %d " |
| 122 | "queue_r %d\n", |
| 123 | frame_idx, nsymbs, ref_nsymbs, queue_r); |
| 124 | cdf_error = 0; |
| 125 | assert(0); |
| 126 | } else { |
| 127 | for (i = 0; i < nsymbs; ++i) |
| 128 | if (cdf[i] != ref_cdf[i]) cdf_error = 1; |
| 129 | } |
| 130 | if (cdf_error) { |
| 131 | fprintf(stderr, "\n *** cdf error, frame_idx_r %d cdf {%d", frame_idx, |
| 132 | cdf[0]); |
| 133 | for (i = 1; i < nsymbs; ++i) fprintf(stderr, ", %d", cdf[i]); |
| 134 | fprintf(stderr, "} ref_cdf {%d", ref_cdf[0]); |
| 135 | for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]); |
| 136 | fprintf(stderr, "} queue_r %d\n", queue_r); |
| 137 | assert(0); |
| 138 | } |
| 139 | if (symb != ref_symb) { |
| 140 | fprintf( |
| 141 | stderr, |
| 142 | "\n *** symb error, frame_idx_r %d symb %d ref_symb %d queue_r %d\n", |
| 143 | frame_idx, symb, ref_symb, queue_r); |
| 144 | assert(0); |
| 145 | } |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | return symb; |
| 150 | } |
| 151 | |
Nathan E. Egge | 8043cc4 | 2016-03-06 12:42:47 -0500 | [diff] [blame] | 152 | #ifdef __cplusplus |
| 153 | } // extern "C" |
| 154 | #endif |
| 155 | |
| 156 | #endif |