michael@0: /* michael@0: * Copyright (c) 2013 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #ifndef VP9_DECODER_VP9_READ_BIT_BUFFER_H_ michael@0: #define VP9_DECODER_VP9_READ_BIT_BUFFER_H_ michael@0: michael@0: #include michael@0: michael@0: #include "vpx/vpx_integer.h" michael@0: michael@0: typedef void (*vp9_rb_error_handler)(void *data, size_t bit_offset); michael@0: michael@0: struct vp9_read_bit_buffer { michael@0: const uint8_t *bit_buffer; michael@0: const uint8_t *bit_buffer_end; michael@0: size_t bit_offset; michael@0: michael@0: void *error_handler_data; michael@0: vp9_rb_error_handler error_handler; michael@0: }; michael@0: michael@0: static size_t vp9_rb_bytes_read(struct vp9_read_bit_buffer *rb) { michael@0: return rb->bit_offset / CHAR_BIT + (rb->bit_offset % CHAR_BIT > 0); michael@0: } michael@0: michael@0: static int vp9_rb_read_bit(struct vp9_read_bit_buffer *rb) { michael@0: const size_t off = rb->bit_offset; michael@0: const size_t p = off / CHAR_BIT; michael@0: const int q = CHAR_BIT - 1 - (int)off % CHAR_BIT; michael@0: if (rb->bit_buffer + p >= rb->bit_buffer_end) { michael@0: rb->error_handler(rb->error_handler_data, rb->bit_offset); michael@0: return 0; michael@0: } else { michael@0: const int bit = (rb->bit_buffer[p] & (1 << q)) >> q; michael@0: rb->bit_offset = off + 1; michael@0: return bit; michael@0: } michael@0: } michael@0: michael@0: static int vp9_rb_read_literal(struct vp9_read_bit_buffer *rb, int bits) { michael@0: int value = 0, bit; michael@0: for (bit = bits - 1; bit >= 0; bit--) michael@0: value |= vp9_rb_read_bit(rb) << bit; michael@0: return value; michael@0: } michael@0: michael@0: static int vp9_rb_read_signed_literal(struct vp9_read_bit_buffer *rb, michael@0: int bits) { michael@0: const int value = vp9_rb_read_literal(rb, bits); michael@0: return vp9_rb_read_bit(rb) ? -value : value; michael@0: } michael@0: michael@0: #endif // VP9_DECODER_VP9_READ_BIT_BUFFER_H_