Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
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.
10 : */
11 :
12 : #include <limits.h>
13 : #include <stdlib.h>
14 :
15 : #include "./aom_config.h"
16 : #include "./bitwriter_buffer.h"
17 :
18 0 : uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
19 0 : return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
20 : }
21 :
22 0 : void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
23 0 : const int off = (int)wb->bit_offset;
24 0 : const int p = off / CHAR_BIT;
25 0 : const int q = CHAR_BIT - 1 - off % CHAR_BIT;
26 0 : if (q == CHAR_BIT - 1) {
27 : // Zero next char and write bit
28 0 : wb->bit_buffer[p] = bit << q;
29 : } else {
30 0 : wb->bit_buffer[p] &= ~(1 << q);
31 0 : wb->bit_buffer[p] |= bit << q;
32 : }
33 0 : wb->bit_offset = off + 1;
34 0 : }
35 :
36 0 : void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
37 : // Do not zero bytes but overwrite exisiting values
38 0 : const int off = (int)wb->bit_offset;
39 0 : const int p = off / CHAR_BIT;
40 0 : const int q = CHAR_BIT - 1 - off % CHAR_BIT;
41 0 : wb->bit_buffer[p] &= ~(1 << q);
42 0 : wb->bit_buffer[p] |= bit << q;
43 0 : wb->bit_offset = off + 1;
44 0 : }
45 :
46 0 : void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
47 : int bit;
48 0 : for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
49 0 : }
50 :
51 0 : void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
52 : int bits) {
53 : int bit;
54 0 : for (bit = bits - 1; bit >= 0; bit--)
55 0 : aom_wb_overwrite_bit(wb, (data >> bit) & 1);
56 0 : }
57 :
58 0 : void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
59 : int bits) {
60 0 : aom_wb_write_literal(wb, data, bits + 1);
61 0 : }
|