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