/src/aom/aom_dsp/bitreader_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 | | |
14 | | #include "config/aom_config.h" |
15 | | |
16 | | #include "aom_dsp/bitreader_buffer.h" |
17 | | #include "aom_dsp/recenter.h" |
18 | | #include "aom_ports/bitops.h" |
19 | | |
20 | 0 | size_t aom_rb_bytes_read(const struct aom_read_bit_buffer *rb) { |
21 | 0 | return (rb->bit_offset + 7) >> 3; |
22 | 0 | } |
23 | | |
24 | 0 | int aom_rb_read_bit(struct aom_read_bit_buffer *rb) { |
25 | 0 | const uint32_t off = rb->bit_offset; |
26 | 0 | const uint32_t p = off >> 3; |
27 | 0 | const int q = 7 - (int)(off & 0x7); |
28 | 0 | if (rb->bit_buffer + p < rb->bit_buffer_end) { |
29 | 0 | const int bit = (rb->bit_buffer[p] >> q) & 1; |
30 | 0 | rb->bit_offset = off + 1; |
31 | 0 | return bit; |
32 | 0 | } else { |
33 | 0 | if (rb->error_handler) rb->error_handler(rb->error_handler_data); |
34 | 0 | return 0; |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | 0 | int aom_rb_read_literal(struct aom_read_bit_buffer *rb, int bits) { |
39 | 0 | assert(bits <= 31); |
40 | 0 | int value = 0, bit; |
41 | 0 | for (bit = bits - 1; bit >= 0; bit--) value |= aom_rb_read_bit(rb) << bit; |
42 | 0 | return value; |
43 | 0 | } |
44 | | |
45 | | uint32_t aom_rb_read_unsigned_literal(struct aom_read_bit_buffer *rb, |
46 | 0 | int bits) { |
47 | 0 | assert(bits <= 32); |
48 | 0 | uint32_t value = 0; |
49 | 0 | int bit; |
50 | 0 | for (bit = bits - 1; bit >= 0; bit--) |
51 | 0 | value |= (uint32_t)aom_rb_read_bit(rb) << bit; |
52 | 0 | return value; |
53 | 0 | } |
54 | | |
55 | 0 | int aom_rb_read_inv_signed_literal(struct aom_read_bit_buffer *rb, int bits) { |
56 | 0 | const int nbits = sizeof(unsigned) * 8 - bits - 1; |
57 | 0 | const unsigned value = (unsigned)aom_rb_read_literal(rb, bits + 1) << nbits; |
58 | 0 | return ((int)value) >> nbits; |
59 | 0 | } |
60 | | |
61 | 0 | uint32_t aom_rb_read_uvlc(struct aom_read_bit_buffer *rb) { |
62 | 0 | int leading_zeros = 0; |
63 | 0 | while (leading_zeros < 32 && !aom_rb_read_bit(rb)) ++leading_zeros; |
64 | | // Maximum 32 bits. |
65 | 0 | if (leading_zeros == 32) return UINT32_MAX; |
66 | 0 | const uint32_t base = (1u << leading_zeros) - 1; |
67 | 0 | const uint32_t value = aom_rb_read_literal(rb, leading_zeros); |
68 | 0 | return base + value; |
69 | 0 | } |
70 | | |
71 | | static uint16_t aom_rb_read_primitive_quniform(struct aom_read_bit_buffer *rb, |
72 | 0 | uint16_t n) { |
73 | 0 | if (n <= 1) return 0; |
74 | 0 | const int l = get_msb(n) + 1; |
75 | 0 | const int m = (1 << l) - n; |
76 | 0 | const int v = aom_rb_read_literal(rb, l - 1); |
77 | 0 | return v < m ? v : (v << 1) - m + aom_rb_read_bit(rb); |
78 | 0 | } |
79 | | |
80 | | static uint16_t aom_rb_read_primitive_subexpfin(struct aom_read_bit_buffer *rb, |
81 | 0 | uint16_t n, uint16_t k) { |
82 | 0 | int i = 0; |
83 | 0 | int mk = 0; |
84 | |
|
85 | 0 | while (1) { |
86 | 0 | int b = (i ? k + i - 1 : k); |
87 | 0 | int a = (1 << b); |
88 | |
|
89 | 0 | if (n <= mk + 3 * a) { |
90 | 0 | return aom_rb_read_primitive_quniform(rb, n - mk) + mk; |
91 | 0 | } |
92 | | |
93 | 0 | if (!aom_rb_read_bit(rb)) { |
94 | 0 | return aom_rb_read_literal(rb, b) + mk; |
95 | 0 | } |
96 | | |
97 | 0 | i = i + 1; |
98 | 0 | mk += a; |
99 | 0 | } |
100 | | |
101 | 0 | assert(0); |
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | | static uint16_t aom_rb_read_primitive_refsubexpfin( |
106 | 0 | struct aom_read_bit_buffer *rb, uint16_t n, uint16_t k, uint16_t ref) { |
107 | 0 | return inv_recenter_finite_nonneg(n, ref, |
108 | 0 | aom_rb_read_primitive_subexpfin(rb, n, k)); |
109 | 0 | } |
110 | | |
111 | | int16_t aom_rb_read_signed_primitive_refsubexpfin( |
112 | 0 | struct aom_read_bit_buffer *rb, uint16_t n, uint16_t k, int16_t ref) { |
113 | 0 | ref += n - 1; |
114 | 0 | const uint16_t scaled_n = (n << 1) - 1; |
115 | 0 | return aom_rb_read_primitive_refsubexpfin(rb, scaled_n, k, ref) - n + 1; |
116 | 0 | } |