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 #include "aom_dsp/recenter.h"
20 #include "aom_ports/bitops.h"
21 
aom_wb_is_byte_aligned(const struct aom_write_bit_buffer * wb)22 int aom_wb_is_byte_aligned(const struct aom_write_bit_buffer *wb) {
23   return (wb->bit_offset % CHAR_BIT == 0);
24 }
25 
aom_wb_bytes_written(const struct aom_write_bit_buffer * wb)26 uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
27   return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
28 }
29 
aom_wb_write_bit(struct aom_write_bit_buffer * wb,int bit)30 void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
31   const int off = (int)wb->bit_offset;
32   const int p = off / CHAR_BIT;
33   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
34   if (q == CHAR_BIT - 1) {
35     // Zero next char and write bit
36     wb->bit_buffer[p] = bit << q;
37   } else {
38     wb->bit_buffer[p] &= ~(1 << q);
39     wb->bit_buffer[p] |= bit << q;
40   }
41   wb->bit_offset = off + 1;
42 }
43 
aom_wb_overwrite_bit(struct aom_write_bit_buffer * wb,int bit)44 void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
45   // Do not zero bytes but overwrite exisiting values
46   const int off = (int)wb->bit_offset;
47   const int p = off / CHAR_BIT;
48   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
49   wb->bit_buffer[p] &= ~(1 << q);
50   wb->bit_buffer[p] |= bit << q;
51   wb->bit_offset = off + 1;
52 }
53 
aom_wb_write_literal(struct aom_write_bit_buffer * wb,int data,int bits)54 void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
55   assert(bits <= 31);
56   int bit;
57   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
58 }
59 
aom_wb_write_unsigned_literal(struct aom_write_bit_buffer * wb,uint32_t data,int bits)60 void aom_wb_write_unsigned_literal(struct aom_write_bit_buffer *wb,
61                                    uint32_t data, int bits) {
62   assert(bits <= 32);
63   int bit;
64   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
65 }
66 
aom_wb_overwrite_literal(struct aom_write_bit_buffer * wb,int data,int bits)67 void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
68                               int bits) {
69   int bit;
70   for (bit = bits - 1; bit >= 0; bit--)
71     aom_wb_overwrite_bit(wb, (data >> bit) & 1);
72 }
73 
aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer * wb,int data,int bits)74 void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
75                                      int bits) {
76   aom_wb_write_literal(wb, data, bits + 1);
77 }
78 
aom_wb_write_uvlc(struct aom_write_bit_buffer * wb,uint32_t v)79 void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) {
80   int64_t shift_val = ++v;
81   int leading_zeroes = 1;
82 
83   assert(shift_val > 0);
84 
85   while (shift_val >>= 1) leading_zeroes += 2;
86 
87   aom_wb_write_literal(wb, 0, leading_zeroes >> 1);
88   aom_wb_write_unsigned_literal(wb, v, (leading_zeroes + 1) >> 1);
89 }
90 
wb_write_primitive_quniform(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t v)91 static void wb_write_primitive_quniform(struct aom_write_bit_buffer *wb,
92                                         uint16_t n, uint16_t v) {
93   if (n <= 1) return;
94   const int l = get_msb(n) + 1;
95   const int m = (1 << l) - n;
96   if (v < m) {
97     aom_wb_write_literal(wb, v, l - 1);
98   } else {
99     aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1);
100     aom_wb_write_bit(wb, (v - m) & 1);
101   }
102 }
103 
wb_write_primitive_subexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,uint16_t v)104 static void wb_write_primitive_subexpfin(struct aom_write_bit_buffer *wb,
105                                          uint16_t n, uint16_t k, uint16_t v) {
106   int i = 0;
107   int mk = 0;
108   while (1) {
109     int b = (i ? k + i - 1 : k);
110     int a = (1 << b);
111     if (n <= mk + 3 * a) {
112       wb_write_primitive_quniform(wb, n - mk, v - mk);
113       break;
114     } else {
115       int t = (v >= mk + a);
116       aom_wb_write_bit(wb, t);
117       if (t) {
118         i = i + 1;
119         mk += a;
120       } else {
121         aom_wb_write_literal(wb, v - mk, b);
122         break;
123       }
124     }
125   }
126 }
127 
wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,uint16_t ref,uint16_t v)128 static void wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
129                                             uint16_t n, uint16_t k,
130                                             uint16_t ref, uint16_t v) {
131   wb_write_primitive_subexpfin(wb, n, k, recenter_finite_nonneg(n, ref, v));
132 }
133 
aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,int16_t ref,int16_t v)134 void aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
135                                                 uint16_t n, uint16_t k,
136                                                 int16_t ref, int16_t v) {
137   ref += n - 1;
138   v += n - 1;
139   const uint16_t scaled_n = (n << 1) - 1;
140   wb_write_primitive_refsubexpfin(wb, scaled_n, k, ref, v);
141 }
142