blob: c341ca3c54a2db29e3483c5eef70662fe2bf2582 [file] [log] [blame]
Jim Bankoski423590a2014-03-03 15:40:13 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jim Bankoski423590a2014-03-03 15:40:13 -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 Bankoski423590a2014-03-03 15:40:13 -080010 */
11
James Zernc5a7eef2014-08-02 14:14:22 -070012#include <limits.h>
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040013#include <stdlib.h>
Yaowu Xuc5ad31e2015-07-20 11:05:04 -070014
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
Yaowu Xu1fcef812015-07-20 11:18:57 -070016#include "./bitwriter_buffer.h"
Jim Bankoski423590a2014-03-03 15:40:13 -080017
Yaowu Xuf883b422016-08-30 14:01:10 -070018size_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
Jim Bankoski423590a2014-03-03 15:40:13 -080019 return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
20}
21
Yaowu Xuf883b422016-08-30 14:01:10 -070022void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
Jim Bankoski423590a2014-03-03 15:40:13 -080023 const int off = (int)wb->bit_offset;
24 const int p = off / CHAR_BIT;
25 const int q = CHAR_BIT - 1 - off % CHAR_BIT;
Sarah Parkercaaf9f92016-11-11 04:48:29 +000026 if (q == CHAR_BIT - 1) {
Thomas Daviesfaa7fcf2016-11-14 11:59:43 +000027 // Zero next char and write bit
Sarah Parkercaaf9f92016-11-11 04:48:29 +000028 wb->bit_buffer[p] = bit << q;
29 } else {
30 wb->bit_buffer[p] &= ~(1 << q);
31 wb->bit_buffer[p] |= bit << q;
32 }
Jim Bankoski423590a2014-03-03 15:40:13 -080033 wb->bit_offset = off + 1;
34}
35
Thomas Daviesfaa7fcf2016-11-14 11:59:43 +000036void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
37 // Do not zero bytes but overwrite exisiting values
38 const int off = (int)wb->bit_offset;
39 const int p = off / CHAR_BIT;
40 const int q = CHAR_BIT - 1 - off % CHAR_BIT;
41 wb->bit_buffer[p] &= ~(1 << q);
42 wb->bit_buffer[p] |= bit << q;
43 wb->bit_offset = off + 1;
44}
45
Yaowu Xuf883b422016-08-30 14:01:10 -070046void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
Jim Bankoski423590a2014-03-03 15:40:13 -080047 int bit;
Yaowu Xuf883b422016-08-30 14:01:10 -070048 for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
Jim Bankoski423590a2014-03-03 15:40:13 -080049}
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040050
Thomas Daviesfaa7fcf2016-11-14 11:59:43 +000051void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
52 int bits) {
53 int bit;
54 for (bit = bits - 1; bit >= 0; bit--)
55 aom_wb_overwrite_bit(wb, (data >> bit) & 1);
56}
57
Yaowu Xuf883b422016-08-30 14:01:10 -070058void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
clang-format1214cee2016-08-08 22:59:08 -070059 int bits) {
Yaowu Xuf883b422016-08-30 14:01:10 -070060 aom_wb_write_literal(wb, data, bits + 1);
Ronald S. Bultjea3df3432015-09-08 14:26:42 -040061}