John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
Yaowu Xu | 87d2c3c | 2015-07-17 14:09:05 -0700 | [diff] [blame] | 11 | #ifndef VPX_DSP_BITREADER_H_ |
| 12 | #define VPX_DSP_BITREADER_H_ |
Ronald S. Bultje | 4cca47b | 2012-12-18 15:31:19 -0800 | [diff] [blame] | 13 | |
Timothy B. Terriberry | c17b62e | 2010-05-05 17:58:19 -0400 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <limits.h> |
Dmitry Kovalev | 7f99c3c | 2013-03-05 14:12:16 -0800 | [diff] [blame] | 16 | |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 17 | #include "./vpx_config.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 18 | #include "vpx_ports/mem.h" |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 19 | #include "vpx/vp8dx.h" |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 20 | #include "vpx/vpx_integer.h" |
Yaowu Xu | 87d2c3c | 2015-07-17 14:09:05 -0700 | [diff] [blame] | 21 | #include "vpx_dsp/prob.h" |
Timothy B. Terriberry | c17b62e | 2010-05-05 17:58:19 -0400 | [diff] [blame] | 22 | |
James Zern | 40aa910 | 2014-01-18 12:16:11 -0800 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 27 | typedef size_t BD_VALUE; |
| 28 | |
| 29 | #define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT) |
| 30 | |
James Zern | 1e0aa94 | 2015-07-01 19:10:43 -0700 | [diff] [blame] | 31 | // This is meant to be a large, positive constant that can still be efficiently |
| 32 | // loaded as an immediate (on platforms like ARM, for example). |
| 33 | // Even relatively modest values like 100 would work fine. |
| 34 | #define LOTS_OF_BITS 0x40000000 |
| 35 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 36 | typedef struct { |
James Zern | e1f55e0 | 2014-11-24 19:27:07 -0800 | [diff] [blame] | 37 | // Be careful when reordering this struct, it may impact the cache negatively. |
| 38 | BD_VALUE value; |
| 39 | unsigned int range; |
| 40 | int count; |
Dmitry Kovalev | 67d0600 | 2013-04-15 14:54:19 -0700 | [diff] [blame] | 41 | const uint8_t *buffer_end; |
| 42 | const uint8_t *buffer; |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 43 | vpx_decrypt_cb decrypt_cb; |
| 44 | void *decrypt_state; |
James Zern | e1f55e0 | 2014-11-24 19:27:07 -0800 | [diff] [blame] | 45 | uint8_t clear_buffer[sizeof(BD_VALUE) + 1]; |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 46 | } vpx_reader; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 47 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 48 | int vpx_reader_init(vpx_reader *r, |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 49 | const uint8_t *buffer, |
| 50 | size_t size, |
| 51 | vpx_decrypt_cb decrypt_cb, |
| 52 | void *decrypt_state); |
Johann | bb9c95e | 2011-02-04 16:00:00 -0500 | [diff] [blame] | 53 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 54 | void vpx_reader_fill(vpx_reader *r); |
Timothy B. Terriberry | c17b62e | 2010-05-05 17:58:19 -0400 | [diff] [blame] | 55 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 56 | const uint8_t *vpx_reader_find_end(vpx_reader *r); |
Dmitry Kovalev | 67d0600 | 2013-04-15 14:54:19 -0700 | [diff] [blame] | 57 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 58 | static INLINE int vpx_reader_has_error(vpx_reader *r) { |
James Zern | 1e0aa94 | 2015-07-01 19:10:43 -0700 | [diff] [blame] | 59 | // Check if we have reached the end of the buffer. |
| 60 | // |
| 61 | // Variable 'count' stores the number of bits in the 'value' buffer, minus |
| 62 | // 8. The top byte is part of the algorithm, and the remainder is buffered |
| 63 | // to be shifted into it. So if count == 8, the top 16 bits of 'value' are |
| 64 | // occupied, 8 for the algorithm and 8 in the buffer. |
| 65 | // |
| 66 | // When reading a byte from the user's buffer, count is filled with 8 and |
| 67 | // one byte is filled into the value buffer. When we reach the end of the |
| 68 | // data, count is additionally filled with LOTS_OF_BITS. So when |
| 69 | // count == LOTS_OF_BITS - 1, the user's data has been exhausted. |
| 70 | // |
| 71 | // 1 if we have tried to decode bits after the end of stream was encountered. |
| 72 | // 0 No error. |
| 73 | return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS; |
| 74 | } |
| 75 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 76 | static INLINE int vpx_read(vpx_reader *r, int prob) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 77 | unsigned int bit = 0; |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 78 | BD_VALUE value; |
| 79 | BD_VALUE bigsplit; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 80 | int count; |
| 81 | unsigned int range; |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 82 | unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT; |
Scott LaVarnway | 67a1f98 | 2011-06-20 14:44:16 -0400 | [diff] [blame] | 83 | |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 84 | if (r->count < 0) |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 85 | vpx_reader_fill(r); |
Scott LaVarnway | 67a1f98 | 2011-06-20 14:44:16 -0400 | [diff] [blame] | 86 | |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 87 | value = r->value; |
| 88 | count = r->count; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 89 | |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 90 | bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 91 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 92 | range = split; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 93 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 94 | if (value >= bigsplit) { |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 95 | range = r->range - split; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 96 | value = value - bigsplit; |
| 97 | bit = 1; |
| 98 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 99 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 100 | { |
Alex Converse | 6aa2163 | 2015-11-19 15:07:55 -0800 | [diff] [blame] | 101 | register int shift = vpx_norm[range]; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 102 | range <<= shift; |
| 103 | value <<= shift; |
| 104 | count -= shift; |
| 105 | } |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 106 | r->value = value; |
| 107 | r->count = count; |
| 108 | r->range = range; |
Scott LaVarnway | 67a1f98 | 2011-06-20 14:44:16 -0400 | [diff] [blame] | 109 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 110 | return bit; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 113 | static INLINE int vpx_read_bit(vpx_reader *r) { |
| 114 | return vpx_read(r, 128); // vpx_prob_half |
Dmitry Kovalev | 67d0600 | 2013-04-15 14:54:19 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 117 | static INLINE int vpx_read_literal(vpx_reader *r, int bits) { |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 118 | int literal = 0, bit; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 119 | |
Dmitry Kovalev | 0b44624 | 2013-04-19 10:37:24 -0700 | [diff] [blame] | 120 | for (bit = bits - 1; bit >= 0; bit--) |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 121 | literal |= vpx_read_bit(r) << bit; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 122 | |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 123 | return literal; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 124 | } |
Henrik Lundin | 67fb3a5 | 2010-12-16 16:46:31 +0100 | [diff] [blame] | 125 | |
Yaowu Xu | 70ad668 | 2015-07-20 14:04:21 -0700 | [diff] [blame] | 126 | static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree, |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 127 | const vpx_prob *probs) { |
Yaowu Xu | 70ad668 | 2015-07-20 14:04:21 -0700 | [diff] [blame] | 128 | vpx_tree_index i = 0; |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 129 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 130 | while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) |
Dmitry Kovalev | d7efe06 | 2013-11-26 12:38:58 -0800 | [diff] [blame] | 131 | continue; |
| 132 | |
| 133 | return -i; |
| 134 | } |
Deb Mukherjee | c6f1bf4 | 2012-04-12 09:24:03 -0700 | [diff] [blame] | 135 | |
James Zern | 40aa910 | 2014-01-18 12:16:11 -0800 | [diff] [blame] | 136 | #ifdef __cplusplus |
| 137 | } // extern "C" |
| 138 | #endif |
| 139 | |
Yaowu Xu | 87d2c3c | 2015-07-17 14:09:05 -0700 | [diff] [blame] | 140 | #endif // VPX_DSP_BITREADER_H_ |