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 <assert.h>
13 #include <limits.h>
14 #include <stdlib.h>
15 
16 #include "config/aom_config.h"
17 
18 #include "aom_dsp/bitwriter_buffer.h"
19 
aom_wb_is_byte_aligned(const struct aom_write_bit_buffer * wb)20 int aom_wb_is_byte_aligned(const struct aom_write_bit_buffer *wb) {
21   return (wb->bit_offset % CHAR_BIT == 0);
22 }
23 
aom_wb_bytes_written(const struct aom_write_bit_buffer * wb)24 uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
25   return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
26 }
27 
aom_wb_write_bit(struct aom_write_bit_buffer * wb,int bit)28 void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
29   const int off = (int)wb->bit_offset;
30   const int p = off / CHAR_BIT;
31   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
32   if (q == CHAR_BIT - 1) {
33     // Zero next char and write bit
34     wb->bit_buffer[p] = bit << q;
35   } else {
36     wb->bit_buffer[p] &= ~(1 << q);
37     wb->bit_buffer[p] |= bit << q;
38   }
39   wb->bit_offset = off + 1;
40 }
41 
aom_wb_overwrite_bit(struct aom_write_bit_buffer * wb,int bit)42 void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
43   // Do not zero bytes but overwrite exisiting values
44   const int off = (int)wb->bit_offset;
45   const int p = off / CHAR_BIT;
46   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
47   wb->bit_buffer[p] &= ~(1 << q);
48   wb->bit_buffer[p] |= bit << q;
49   wb->bit_offset = off + 1;
50 }
51 
aom_wb_write_literal(struct aom_write_bit_buffer * wb,int data,int bits)52 void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
53   assert(bits <= 31);
54   int bit;
55   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
56 }
57 
aom_wb_write_unsigned_literal(struct aom_write_bit_buffer * wb,uint32_t data,int bits)58 void aom_wb_write_unsigned_literal(struct aom_write_bit_buffer *wb,
59                                    uint32_t data, int bits) {
60   assert(bits <= 32);
61   int bit;
62   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
63 }
64 
aom_wb_overwrite_literal(struct aom_write_bit_buffer * wb,int data,int bits)65 void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
66                               int bits) {
67   int bit;
68   for (bit = bits - 1; bit >= 0; bit--)
69     aom_wb_overwrite_bit(wb, (data >> bit) & 1);
70 }
71 
aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer * wb,int data,int bits)72 void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
73                                      int bits) {
74   aom_wb_write_literal(wb, data, bits + 1);
75 }
76 
aom_wb_write_uvlc(struct aom_write_bit_buffer * wb,uint32_t v)77 void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) {
78   int64_t shift_val = ++v;
79   int leading_zeroes = 1;
80 
81   assert(shift_val > 0);
82 
83   while (shift_val >>= 1) leading_zeroes += 2;
84 
85   aom_wb_write_literal(wb, 0, leading_zeroes >> 1);
86   aom_wb_write_unsigned_literal(wb, v, (leading_zeroes + 1) >> 1);
87 }
88