blob: 595b9bb1233b75fb0598f6d11dac7965358fef2b [file] [log] [blame]
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -08001/*
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. Bultjea3df3432015-09-08 14:26:42 -040010#include "./vpx_config.h"
Yaowu Xu87d2c3c2015-07-17 14:09:05 -070011#include "./bitreader_buffer.h"
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080012
Yaowu Xucbce0032015-07-20 13:55:06 -070013size_t vpx_rb_bytes_read(struct vpx_read_bit_buffer *rb) {
hkuang3cecce92014-12-12 13:54:40 -080014 return (rb->bit_offset + 7) >> 3;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080015}
16
Yaowu Xucbce0032015-07-20 13:55:06 -070017int vpx_rb_read_bit(struct vpx_read_bit_buffer *rb) {
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080018 const size_t off = rb->bit_offset;
hkuang3cecce92014-12-12 13:54:40 -080019 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 Bankoskia1fbe1e2014-03-03 15:21:22 -080023 rb->bit_offset = off + 1;
24 return bit;
hkuang3cecce92014-12-12 13:54:40 -080025 } else {
26 rb->error_handler(rb->error_handler_data);
27 return 0;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080028 }
29}
30
Yaowu Xucbce0032015-07-20 13:55:06 -070031int vpx_rb_read_literal(struct vpx_read_bit_buffer *rb, int bits) {
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080032 int value = 0, bit;
33 for (bit = bits - 1; bit >= 0; bit--)
Yaowu Xucbce0032015-07-20 13:55:06 -070034 value |= vpx_rb_read_bit(rb) << bit;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080035 return value;
36}
37
Yaowu Xucbce0032015-07-20 13:55:06 -070038int vpx_rb_read_signed_literal(struct vpx_read_bit_buffer *rb,
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080039 int bits) {
Yaowu Xucbce0032015-07-20 13:55:06 -070040 const int value = vpx_rb_read_literal(rb, bits);
41 return vpx_rb_read_bit(rb) ? -value : value;
Jim Bankoskia1fbe1e2014-03-03 15:21:22 -080042}
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040043
44int vpx_rb_read_inv_signed_literal(struct vpx_read_bit_buffer *rb,
45 int bits) {
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040046 const int nbits = sizeof(unsigned) * 8 - bits - 1;
Alex Converse42b7c442015-11-19 15:04:20 -080047 const unsigned value = (unsigned)vpx_rb_read_literal(rb, bits + 1) << nbits;
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040048 return ((int) value) >> nbits;
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040049}