xref: /aosp_15_r20/external/libaom/aom_dsp/bitwriter_buffer.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitwriter_buffer.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/recenter.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/bitops.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_is_byte_aligned(const struct aom_write_bit_buffer * wb)22*77c1e3ccSAndroid Build Coastguard Worker int aom_wb_is_byte_aligned(const struct aom_write_bit_buffer *wb) {
23*77c1e3ccSAndroid Build Coastguard Worker   return (wb->bit_offset % CHAR_BIT == 0);
24*77c1e3ccSAndroid Build Coastguard Worker }
25*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_bytes_written(const struct aom_write_bit_buffer * wb)26*77c1e3ccSAndroid Build Coastguard Worker uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
27*77c1e3ccSAndroid Build Coastguard Worker   return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
28*77c1e3ccSAndroid Build Coastguard Worker }
29*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_bit(struct aom_write_bit_buffer * wb,int bit)30*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
31*77c1e3ccSAndroid Build Coastguard Worker   const int off = (int)wb->bit_offset;
32*77c1e3ccSAndroid Build Coastguard Worker   const int p = off / CHAR_BIT;
33*77c1e3ccSAndroid Build Coastguard Worker   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
34*77c1e3ccSAndroid Build Coastguard Worker   if (q == CHAR_BIT - 1) {
35*77c1e3ccSAndroid Build Coastguard Worker     // Zero next char and write bit
36*77c1e3ccSAndroid Build Coastguard Worker     wb->bit_buffer[p] = bit << q;
37*77c1e3ccSAndroid Build Coastguard Worker   } else {
38*77c1e3ccSAndroid Build Coastguard Worker     wb->bit_buffer[p] &= ~(1 << q);
39*77c1e3ccSAndroid Build Coastguard Worker     wb->bit_buffer[p] |= bit << q;
40*77c1e3ccSAndroid Build Coastguard Worker   }
41*77c1e3ccSAndroid Build Coastguard Worker   wb->bit_offset = off + 1;
42*77c1e3ccSAndroid Build Coastguard Worker }
43*77c1e3ccSAndroid Build Coastguard Worker 
overwrite_bit(struct aom_write_bit_buffer * wb,int bit)44*77c1e3ccSAndroid Build Coastguard Worker static void overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
45*77c1e3ccSAndroid Build Coastguard Worker   // Do not zero bytes but overwrite exisiting values
46*77c1e3ccSAndroid Build Coastguard Worker   const int off = (int)wb->bit_offset;
47*77c1e3ccSAndroid Build Coastguard Worker   const int p = off / CHAR_BIT;
48*77c1e3ccSAndroid Build Coastguard Worker   const int q = CHAR_BIT - 1 - off % CHAR_BIT;
49*77c1e3ccSAndroid Build Coastguard Worker   wb->bit_buffer[p] &= ~(1 << q);
50*77c1e3ccSAndroid Build Coastguard Worker   wb->bit_buffer[p] |= bit << q;
51*77c1e3ccSAndroid Build Coastguard Worker   wb->bit_offset = off + 1;
52*77c1e3ccSAndroid Build Coastguard Worker }
53*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_literal(struct aom_write_bit_buffer * wb,int data,int bits)54*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
55*77c1e3ccSAndroid Build Coastguard Worker   assert(bits <= 31);
56*77c1e3ccSAndroid Build Coastguard Worker   int bit;
57*77c1e3ccSAndroid Build Coastguard Worker   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_unsigned_literal(struct aom_write_bit_buffer * wb,uint32_t data,int bits)60*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_unsigned_literal(struct aom_write_bit_buffer *wb,
61*77c1e3ccSAndroid Build Coastguard Worker                                    uint32_t data, int bits) {
62*77c1e3ccSAndroid Build Coastguard Worker   assert(bits <= 32);
63*77c1e3ccSAndroid Build Coastguard Worker   int bit;
64*77c1e3ccSAndroid Build Coastguard Worker   for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
65*77c1e3ccSAndroid Build Coastguard Worker }
66*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_overwrite_literal(struct aom_write_bit_buffer * wb,int data,int bits)67*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
68*77c1e3ccSAndroid Build Coastguard Worker                               int bits) {
69*77c1e3ccSAndroid Build Coastguard Worker   int bit;
70*77c1e3ccSAndroid Build Coastguard Worker   for (bit = bits - 1; bit >= 0; bit--) overwrite_bit(wb, (data >> bit) & 1);
71*77c1e3ccSAndroid Build Coastguard Worker }
72*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer * wb,int data,int bits)73*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
74*77c1e3ccSAndroid Build Coastguard Worker                                      int bits) {
75*77c1e3ccSAndroid Build Coastguard Worker   aom_wb_write_literal(wb, data, bits + 1);
76*77c1e3ccSAndroid Build Coastguard Worker }
77*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_uvlc(struct aom_write_bit_buffer * wb,uint32_t v)78*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) {
79*77c1e3ccSAndroid Build Coastguard Worker   int64_t shift_val = ++v;
80*77c1e3ccSAndroid Build Coastguard Worker   int leading_zeroes = 1;
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker   assert(shift_val > 0);
83*77c1e3ccSAndroid Build Coastguard Worker 
84*77c1e3ccSAndroid Build Coastguard Worker   while (shift_val >>= 1) leading_zeroes += 2;
85*77c1e3ccSAndroid Build Coastguard Worker 
86*77c1e3ccSAndroid Build Coastguard Worker   aom_wb_write_literal(wb, 0, leading_zeroes >> 1);
87*77c1e3ccSAndroid Build Coastguard Worker   aom_wb_write_unsigned_literal(wb, v, (leading_zeroes + 1) >> 1);
88*77c1e3ccSAndroid Build Coastguard Worker }
89*77c1e3ccSAndroid Build Coastguard Worker 
wb_write_primitive_quniform(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t v)90*77c1e3ccSAndroid Build Coastguard Worker static void wb_write_primitive_quniform(struct aom_write_bit_buffer *wb,
91*77c1e3ccSAndroid Build Coastguard Worker                                         uint16_t n, uint16_t v) {
92*77c1e3ccSAndroid Build Coastguard Worker   if (n <= 1) return;
93*77c1e3ccSAndroid Build Coastguard Worker   const int l = get_msb(n) + 1;
94*77c1e3ccSAndroid Build Coastguard Worker   const int m = (1 << l) - n;
95*77c1e3ccSAndroid Build Coastguard Worker   if (v < m) {
96*77c1e3ccSAndroid Build Coastguard Worker     aom_wb_write_literal(wb, v, l - 1);
97*77c1e3ccSAndroid Build Coastguard Worker   } else {
98*77c1e3ccSAndroid Build Coastguard Worker     aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1);
99*77c1e3ccSAndroid Build Coastguard Worker     aom_wb_write_bit(wb, (v - m) & 1);
100*77c1e3ccSAndroid Build Coastguard Worker   }
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker 
wb_write_primitive_subexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,uint16_t v)103*77c1e3ccSAndroid Build Coastguard Worker static void wb_write_primitive_subexpfin(struct aom_write_bit_buffer *wb,
104*77c1e3ccSAndroid Build Coastguard Worker                                          uint16_t n, uint16_t k, uint16_t v) {
105*77c1e3ccSAndroid Build Coastguard Worker   int i = 0;
106*77c1e3ccSAndroid Build Coastguard Worker   int mk = 0;
107*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
108*77c1e3ccSAndroid Build Coastguard Worker     int b = (i ? k + i - 1 : k);
109*77c1e3ccSAndroid Build Coastguard Worker     int a = (1 << b);
110*77c1e3ccSAndroid Build Coastguard Worker     if (n <= mk + 3 * a) {
111*77c1e3ccSAndroid Build Coastguard Worker       wb_write_primitive_quniform(wb, n - mk, v - mk);
112*77c1e3ccSAndroid Build Coastguard Worker       break;
113*77c1e3ccSAndroid Build Coastguard Worker     } else {
114*77c1e3ccSAndroid Build Coastguard Worker       int t = (v >= mk + a);
115*77c1e3ccSAndroid Build Coastguard Worker       aom_wb_write_bit(wb, t);
116*77c1e3ccSAndroid Build Coastguard Worker       if (t) {
117*77c1e3ccSAndroid Build Coastguard Worker         i = i + 1;
118*77c1e3ccSAndroid Build Coastguard Worker         mk += a;
119*77c1e3ccSAndroid Build Coastguard Worker       } else {
120*77c1e3ccSAndroid Build Coastguard Worker         aom_wb_write_literal(wb, v - mk, b);
121*77c1e3ccSAndroid Build Coastguard Worker         break;
122*77c1e3ccSAndroid Build Coastguard Worker       }
123*77c1e3ccSAndroid Build Coastguard Worker     }
124*77c1e3ccSAndroid Build Coastguard Worker   }
125*77c1e3ccSAndroid Build Coastguard Worker }
126*77c1e3ccSAndroid Build Coastguard Worker 
wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,uint16_t ref,uint16_t v)127*77c1e3ccSAndroid Build Coastguard Worker static void wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
128*77c1e3ccSAndroid Build Coastguard Worker                                             uint16_t n, uint16_t k,
129*77c1e3ccSAndroid Build Coastguard Worker                                             uint16_t ref, uint16_t v) {
130*77c1e3ccSAndroid Build Coastguard Worker   wb_write_primitive_subexpfin(wb, n, k, recenter_finite_nonneg(n, ref, v));
131*77c1e3ccSAndroid Build Coastguard Worker }
132*77c1e3ccSAndroid Build Coastguard Worker 
aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer * wb,uint16_t n,uint16_t k,int16_t ref,int16_t v)133*77c1e3ccSAndroid Build Coastguard Worker void aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
134*77c1e3ccSAndroid Build Coastguard Worker                                                 uint16_t n, uint16_t k,
135*77c1e3ccSAndroid Build Coastguard Worker                                                 int16_t ref, int16_t v) {
136*77c1e3ccSAndroid Build Coastguard Worker   ref += n - 1;
137*77c1e3ccSAndroid Build Coastguard Worker   v += n - 1;
138*77c1e3ccSAndroid Build Coastguard Worker   const uint16_t scaled_n = (n << 1) - 1;
139*77c1e3ccSAndroid Build Coastguard Worker   wb_write_primitive_refsubexpfin(wb, scaled_n, k, ref, v);
140*77c1e3ccSAndroid Build Coastguard Worker }
141