Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 4 | * 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 Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
James Zern | c5a7eef | 2014-08-02 14:14:22 -0700 | [diff] [blame] | 12 | #include <limits.h> |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 13 | #include <stdlib.h> |
Yaowu Xu | c5ad31e | 2015-07-20 11:05:04 -0700 | [diff] [blame] | 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "./aom_config.h" |
Yaowu Xu | 1fcef81 | 2015-07-20 11:18:57 -0700 | [diff] [blame] | 16 | #include "./bitwriter_buffer.h" |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 17 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | size_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) { |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 19 | return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0); |
| 20 | } |
| 21 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 22 | void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) { |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 23 | 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 Parker | caaf9f9 | 2016-11-11 04:48:29 +0000 | [diff] [blame] | 26 | if (q == CHAR_BIT - 1) { |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 27 | // Zero next char and write bit |
Sarah Parker | caaf9f9 | 2016-11-11 04:48:29 +0000 | [diff] [blame] | 28 | wb->bit_buffer[p] = bit << q; |
| 29 | } else { |
| 30 | wb->bit_buffer[p] &= ~(1 << q); |
| 31 | wb->bit_buffer[p] |= bit << q; |
| 32 | } |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 33 | wb->bit_offset = off + 1; |
| 34 | } |
| 35 | |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 36 | void 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 46 | void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) { |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 47 | int bit; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 48 | for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1); |
Jim Bankoski | 423590a | 2014-03-03 15:40:13 -0800 | [diff] [blame] | 49 | } |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 50 | |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 51 | void 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 58 | void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data, |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 59 | int bits) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 60 | aom_wb_write_literal(wb, data, bits + 1); |
Ronald S. Bultje | a3df343 | 2015-09-08 14:26:42 -0400 | [diff] [blame] | 61 | } |