/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 | 1.26k | int aom_wb_is_byte_aligned(const struct aom_write_bit_buffer *wb) { |
23 | 1.26k | return (wb->bit_offset % CHAR_BIT == 0); |
24 | 1.26k | } |
25 | | |
26 | 6.31k | uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) { |
27 | 6.31k | return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0); |
28 | 6.31k | } |
29 | | |
30 | 137k | void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) { |
31 | 137k | const int off = (int)wb->bit_offset; |
32 | 137k | const int p = off / CHAR_BIT; |
33 | 137k | const int q = CHAR_BIT - 1 - off % CHAR_BIT; |
34 | 137k | if (q == CHAR_BIT - 1) { |
35 | | // Zero next char and write bit |
36 | 17.8k | wb->bit_buffer[p] = bit << q; |
37 | 119k | } else { |
38 | 119k | wb->bit_buffer[p] &= ~(1 << q); |
39 | 119k | wb->bit_buffer[p] |= bit << q; |
40 | 119k | } |
41 | 137k | wb->bit_offset = off + 1; |
42 | 137k | } |
43 | | |
44 | 0 | void aom_wb_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 | 32.5k | void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) { |
55 | 32.5k | assert(bits <= 31); |
56 | 32.5k | int bit; |
57 | 132k | for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1); |
58 | 32.5k | } |
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--) |
71 | 0 | aom_wb_overwrite_bit(wb, (data >> bit) & 1); |
72 | 0 | } |
73 | | |
74 | | void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data, |
75 | 0 | int bits) { |
76 | 0 | aom_wb_write_literal(wb, data, bits + 1); |
77 | 0 | } |
78 | | |
79 | 0 | void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) { |
80 | 0 | int64_t shift_val = ++v; |
81 | 0 | int leading_zeroes = 1; |
82 | |
|
83 | 0 | assert(shift_val > 0); |
84 | |
|
85 | 0 | while (shift_val >>= 1) leading_zeroes += 2; |
86 | |
|
87 | 0 | aom_wb_write_literal(wb, 0, leading_zeroes >> 1); |
88 | 0 | aom_wb_write_unsigned_literal(wb, v, (leading_zeroes + 1) >> 1); |
89 | 0 | } |
90 | | |
91 | | static void wb_write_primitive_quniform(struct aom_write_bit_buffer *wb, |
92 | 0 | uint16_t n, uint16_t v) { |
93 | 0 | if (n <= 1) return; |
94 | 0 | const int l = get_msb(n) + 1; |
95 | 0 | const int m = (1 << l) - n; |
96 | 0 | if (v < m) { |
97 | 0 | aom_wb_write_literal(wb, v, l - 1); |
98 | 0 | } else { |
99 | 0 | aom_wb_write_literal(wb, m + ((v - m) >> 1), l - 1); |
100 | 0 | aom_wb_write_bit(wb, (v - m) & 1); |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | | static void wb_write_primitive_subexpfin(struct aom_write_bit_buffer *wb, |
105 | 0 | uint16_t n, uint16_t k, uint16_t v) { |
106 | 0 | int i = 0; |
107 | 0 | int mk = 0; |
108 | 0 | while (1) { |
109 | 0 | int b = (i ? k + i - 1 : k); |
110 | 0 | int a = (1 << b); |
111 | 0 | if (n <= mk + 3 * a) { |
112 | 0 | wb_write_primitive_quniform(wb, n - mk, v - mk); |
113 | 0 | break; |
114 | 0 | } else { |
115 | 0 | int t = (v >= mk + a); |
116 | 0 | aom_wb_write_bit(wb, t); |
117 | 0 | if (t) { |
118 | 0 | i = i + 1; |
119 | 0 | mk += a; |
120 | 0 | } else { |
121 | 0 | aom_wb_write_literal(wb, v - mk, b); |
122 | 0 | break; |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | static void wb_write_primitive_refsubexpfin(struct aom_write_bit_buffer *wb, |
129 | | uint16_t n, uint16_t k, |
130 | 0 | uint16_t ref, uint16_t v) { |
131 | 0 | wb_write_primitive_subexpfin(wb, n, k, recenter_finite_nonneg(n, ref, v)); |
132 | 0 | } |
133 | | |
134 | | void aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer *wb, |
135 | | uint16_t n, uint16_t k, |
136 | 0 | int16_t ref, int16_t v) { |
137 | 0 | ref += n - 1; |
138 | 0 | v += n - 1; |
139 | 0 | const uint16_t scaled_n = (n << 1) - 1; |
140 | 0 | wb_write_primitive_refsubexpfin(wb, scaled_n, k, ref, v); |
141 | 0 | } |