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