Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 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 |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 10 | #include "./vpx_config.h" |
Yaowu Xu | 87d2c3c | 2015-07-17 14:09:05 -0700 | [diff] [blame] | 11 | #include "./bitreader_buffer.h" |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 12 | |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 13 | size_t vpx_rb_bytes_read(struct vpx_read_bit_buffer *rb) { |
hkuang | 3cecce9 | 2014-12-12 13:54:40 -0800 | [diff] [blame] | 14 | return (rb->bit_offset + 7) >> 3; |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 15 | } |
| 16 | |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 17 | int vpx_rb_read_bit(struct vpx_read_bit_buffer *rb) { |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 18 | const size_t off = rb->bit_offset; |
hkuang | 3cecce9 | 2014-12-12 13:54:40 -0800 | [diff] [blame] | 19 | const size_t p = off >> 3; |
| 20 | const int q = 7 - (int)(off & 0x7); |
| 21 | if (rb->bit_buffer + p < rb->bit_buffer_end) { |
| 22 | const int bit = (rb->bit_buffer[p] >> q) & 1; |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 23 | rb->bit_offset = off + 1; |
| 24 | return bit; |
hkuang | 3cecce9 | 2014-12-12 13:54:40 -0800 | [diff] [blame] | 25 | } else { |
| 26 | rb->error_handler(rb->error_handler_data); |
| 27 | return 0; |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 31 | int vpx_rb_read_literal(struct vpx_read_bit_buffer *rb, int bits) { |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 32 | int value = 0, bit; |
| 33 | for (bit = bits - 1; bit >= 0; bit--) |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 34 | value |= vpx_rb_read_bit(rb) << bit; |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 35 | return value; |
| 36 | } |
| 37 | |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 38 | int vpx_rb_read_signed_literal(struct vpx_read_bit_buffer *rb, |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 39 | int bits) { |
Yaowu Xu | cbce003 | 2015-07-20 13:55:06 -0700 | [diff] [blame] | 40 | const int value = vpx_rb_read_literal(rb, bits); |
| 41 | return vpx_rb_read_bit(rb) ? -value : value; |
Jim Bankoski | a1fbe1e | 2014-03-03 15:21:22 -0800 | [diff] [blame] | 42 | } |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 43 | |
| 44 | int vpx_rb_read_inv_signed_literal(struct vpx_read_bit_buffer *rb, |
| 45 | int bits) { |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 46 | const int nbits = sizeof(unsigned) * 8 - bits - 1; |
Alex Converse | 42b7c44 | 2015-11-19 15:04:20 -0800 | [diff] [blame] | 47 | const unsigned value = (unsigned)vpx_rb_read_literal(rb, bits + 1) << nbits; |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 48 | return ((int) value) >> nbits; |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 49 | } |