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_BIT_WRITE_BUFFER_H_ michael@0: #define VP9_BIT_WRITE_BUFFER_H_ michael@0: michael@0: #include michael@0: michael@0: #include "vpx/vpx_integer.h" michael@0: michael@0: struct vp9_write_bit_buffer { michael@0: uint8_t *bit_buffer; michael@0: size_t bit_offset; michael@0: }; michael@0: michael@0: static size_t vp9_rb_bytes_written(struct vp9_write_bit_buffer *wb) { michael@0: return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0); michael@0: } michael@0: michael@0: static void vp9_wb_write_bit(struct vp9_write_bit_buffer *wb, int bit) { michael@0: const int off = wb->bit_offset; michael@0: const int p = off / CHAR_BIT; michael@0: const int q = CHAR_BIT - 1 - off % CHAR_BIT; michael@0: if (q == CHAR_BIT -1) { michael@0: wb->bit_buffer[p] = bit << q; michael@0: } else { michael@0: wb->bit_buffer[p] &= ~(1 << q); michael@0: wb->bit_buffer[p] |= bit << q; michael@0: } michael@0: wb->bit_offset = off + 1; michael@0: } michael@0: michael@0: static void vp9_wb_write_literal(struct vp9_write_bit_buffer *wb, michael@0: int data, int bits) { michael@0: int bit; michael@0: for (bit = bits - 1; bit >= 0; bit--) michael@0: vp9_wb_write_bit(wb, (data >> bit) & 1); michael@0: } michael@0: michael@0: michael@0: #endif // VP9_BIT_WRITE_BUFFER_H_