blob: cffdf411a54e30ab5fcc1b9d7b81a433ab9c7679 [file] [log] [blame]
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080010 */
Yaowu Xuf883b422016-08-30 14:01:10 -070011#include "./aom_config.h"
Yaowu Xu87d2c3c2015-07-17 14:09:05 -070012#include "./bitreader_buffer.h"
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080013
Yaowu Xuf883b422016-08-30 14:01:10 -070014size_t aom_rb_bytes_read(struct aom_read_bit_buffer *rb) {
hkuang3cecce92014-12-12 13:54:40 -080015 return (rb->bit_offset + 7) >> 3;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080016}
17
Yaowu Xuf883b422016-08-30 14:01:10 -070018int aom_rb_read_bit(struct aom_read_bit_buffer *rb) {
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080019 const size_t off = rb->bit_offset;
hkuang3cecce92014-12-12 13:54:40 -080020 const size_t p = off >> 3;
21 const int q = 7 - (int)(off & 0x7);
22 if (rb->bit_buffer + p < rb->bit_buffer_end) {
23 const int bit = (rb->bit_buffer[p] >> q) & 1;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080024 rb->bit_offset = off + 1;
25 return bit;
hkuang3cecce92014-12-12 13:54:40 -080026 } else {
27 rb->error_handler(rb->error_handler_data);
28 return 0;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080029 }
30}
31
Yaowu Xuf883b422016-08-30 14:01:10 -070032int aom_rb_read_literal(struct aom_read_bit_buffer *rb, int bits) {
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080033 int value = 0, bit;
Yaowu Xuf883b422016-08-30 14:01:10 -070034 for (bit = bits - 1; bit >= 0; bit--) value |= aom_rb_read_bit(rb) << bit;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080035 return value;
36}
37
Yaowu Xuf883b422016-08-30 14:01:10 -070038int aom_rb_read_signed_literal(struct aom_read_bit_buffer *rb, int bits) {
39 const int value = aom_rb_read_literal(rb, bits);
40 return aom_rb_read_bit(rb) ? -value : value;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080041}
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040042
Yaowu Xuf883b422016-08-30 14:01:10 -070043int aom_rb_read_inv_signed_literal(struct aom_read_bit_buffer *rb, int bits) {
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040044 const int nbits = sizeof(unsigned) * 8 - bits - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -070045 const unsigned value = (unsigned)aom_rb_read_literal(rb, bits + 1) << nbits;
clang-format1214cee2016-08-08 22:59:08 -070046 return ((int)value) >> nbits;
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040047}