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