1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
15*77c1e3ccSAndroid Build Coastguard Worker
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitreader_buffer.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/recenter.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/bitops.h"
19*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_bytes_read(const struct aom_read_bit_buffer * rb)20*77c1e3ccSAndroid Build Coastguard Worker size_t aom_rb_bytes_read(const struct aom_read_bit_buffer *rb) {
21*77c1e3ccSAndroid Build Coastguard Worker return (rb->bit_offset + 7) >> 3;
22*77c1e3ccSAndroid Build Coastguard Worker }
23*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_bit(struct aom_read_bit_buffer * rb)24*77c1e3ccSAndroid Build Coastguard Worker int aom_rb_read_bit(struct aom_read_bit_buffer *rb) {
25*77c1e3ccSAndroid Build Coastguard Worker const uint32_t off = rb->bit_offset;
26*77c1e3ccSAndroid Build Coastguard Worker const uint32_t p = off >> 3;
27*77c1e3ccSAndroid Build Coastguard Worker const int q = 7 - (int)(off & 0x7);
28*77c1e3ccSAndroid Build Coastguard Worker if (rb->bit_buffer + p < rb->bit_buffer_end) {
29*77c1e3ccSAndroid Build Coastguard Worker const int bit = (rb->bit_buffer[p] >> q) & 1;
30*77c1e3ccSAndroid Build Coastguard Worker rb->bit_offset = off + 1;
31*77c1e3ccSAndroid Build Coastguard Worker return bit;
32*77c1e3ccSAndroid Build Coastguard Worker } else {
33*77c1e3ccSAndroid Build Coastguard Worker if (rb->error_handler) rb->error_handler(rb->error_handler_data);
34*77c1e3ccSAndroid Build Coastguard Worker return 0;
35*77c1e3ccSAndroid Build Coastguard Worker }
36*77c1e3ccSAndroid Build Coastguard Worker }
37*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_literal(struct aom_read_bit_buffer * rb,int bits)38*77c1e3ccSAndroid Build Coastguard Worker int aom_rb_read_literal(struct aom_read_bit_buffer *rb, int bits) {
39*77c1e3ccSAndroid Build Coastguard Worker assert(bits <= 31);
40*77c1e3ccSAndroid Build Coastguard Worker int value = 0, bit;
41*77c1e3ccSAndroid Build Coastguard Worker for (bit = bits - 1; bit >= 0; bit--) value |= aom_rb_read_bit(rb) << bit;
42*77c1e3ccSAndroid Build Coastguard Worker return value;
43*77c1e3ccSAndroid Build Coastguard Worker }
44*77c1e3ccSAndroid Build Coastguard Worker
45*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
aom_rb_read_unsigned_literal(struct aom_read_bit_buffer * rb,int bits)46*77c1e3ccSAndroid Build Coastguard Worker uint32_t aom_rb_read_unsigned_literal(struct aom_read_bit_buffer *rb,
47*77c1e3ccSAndroid Build Coastguard Worker int bits) {
48*77c1e3ccSAndroid Build Coastguard Worker assert(bits <= 32);
49*77c1e3ccSAndroid Build Coastguard Worker uint32_t value = 0;
50*77c1e3ccSAndroid Build Coastguard Worker int bit;
51*77c1e3ccSAndroid Build Coastguard Worker for (bit = bits - 1; bit >= 0; bit--)
52*77c1e3ccSAndroid Build Coastguard Worker value |= (uint32_t)aom_rb_read_bit(rb) << bit;
53*77c1e3ccSAndroid Build Coastguard Worker return value;
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_inv_signed_literal(struct aom_read_bit_buffer * rb,int bits)56*77c1e3ccSAndroid Build Coastguard Worker int aom_rb_read_inv_signed_literal(struct aom_read_bit_buffer *rb, int bits) {
57*77c1e3ccSAndroid Build Coastguard Worker const int nbits = sizeof(unsigned) * 8 - bits - 1;
58*77c1e3ccSAndroid Build Coastguard Worker const unsigned value = (unsigned)aom_rb_read_literal(rb, bits + 1) << nbits;
59*77c1e3ccSAndroid Build Coastguard Worker return ((int)value) >> nbits;
60*77c1e3ccSAndroid Build Coastguard Worker }
61*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_DECODER
62*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_uvlc(struct aom_read_bit_buffer * rb)63*77c1e3ccSAndroid Build Coastguard Worker uint32_t aom_rb_read_uvlc(struct aom_read_bit_buffer *rb) {
64*77c1e3ccSAndroid Build Coastguard Worker int leading_zeros = 0;
65*77c1e3ccSAndroid Build Coastguard Worker while (leading_zeros < 32 && !aom_rb_read_bit(rb)) ++leading_zeros;
66*77c1e3ccSAndroid Build Coastguard Worker // Maximum 32 bits.
67*77c1e3ccSAndroid Build Coastguard Worker if (leading_zeros == 32) return UINT32_MAX;
68*77c1e3ccSAndroid Build Coastguard Worker const uint32_t base = (1u << leading_zeros) - 1;
69*77c1e3ccSAndroid Build Coastguard Worker const uint32_t value = aom_rb_read_literal(rb, leading_zeros);
70*77c1e3ccSAndroid Build Coastguard Worker return base + value;
71*77c1e3ccSAndroid Build Coastguard Worker }
72*77c1e3ccSAndroid Build Coastguard Worker
73*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
aom_rb_read_primitive_quniform(struct aom_read_bit_buffer * rb,uint16_t n)74*77c1e3ccSAndroid Build Coastguard Worker static uint16_t aom_rb_read_primitive_quniform(struct aom_read_bit_buffer *rb,
75*77c1e3ccSAndroid Build Coastguard Worker uint16_t n) {
76*77c1e3ccSAndroid Build Coastguard Worker if (n <= 1) return 0;
77*77c1e3ccSAndroid Build Coastguard Worker const int l = get_msb(n) + 1;
78*77c1e3ccSAndroid Build Coastguard Worker const int m = (1 << l) - n;
79*77c1e3ccSAndroid Build Coastguard Worker const int v = aom_rb_read_literal(rb, l - 1);
80*77c1e3ccSAndroid Build Coastguard Worker return v < m ? v : (v << 1) - m + aom_rb_read_bit(rb);
81*77c1e3ccSAndroid Build Coastguard Worker }
82*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_primitive_subexpfin(struct aom_read_bit_buffer * rb,uint16_t n,uint16_t k)83*77c1e3ccSAndroid Build Coastguard Worker static uint16_t aom_rb_read_primitive_subexpfin(struct aom_read_bit_buffer *rb,
84*77c1e3ccSAndroid Build Coastguard Worker uint16_t n, uint16_t k) {
85*77c1e3ccSAndroid Build Coastguard Worker int i = 0;
86*77c1e3ccSAndroid Build Coastguard Worker int mk = 0;
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Worker while (1) {
89*77c1e3ccSAndroid Build Coastguard Worker int b = (i ? k + i - 1 : k);
90*77c1e3ccSAndroid Build Coastguard Worker int a = (1 << b);
91*77c1e3ccSAndroid Build Coastguard Worker
92*77c1e3ccSAndroid Build Coastguard Worker if (n <= mk + 3 * a) {
93*77c1e3ccSAndroid Build Coastguard Worker return aom_rb_read_primitive_quniform(rb, n - mk) + mk;
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker
96*77c1e3ccSAndroid Build Coastguard Worker if (!aom_rb_read_bit(rb)) {
97*77c1e3ccSAndroid Build Coastguard Worker return aom_rb_read_literal(rb, b) + mk;
98*77c1e3ccSAndroid Build Coastguard Worker }
99*77c1e3ccSAndroid Build Coastguard Worker
100*77c1e3ccSAndroid Build Coastguard Worker i = i + 1;
101*77c1e3ccSAndroid Build Coastguard Worker mk += a;
102*77c1e3ccSAndroid Build Coastguard Worker }
103*77c1e3ccSAndroid Build Coastguard Worker
104*77c1e3ccSAndroid Build Coastguard Worker assert(0);
105*77c1e3ccSAndroid Build Coastguard Worker return 0;
106*77c1e3ccSAndroid Build Coastguard Worker }
107*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_primitive_refsubexpfin(struct aom_read_bit_buffer * rb,uint16_t n,uint16_t k,uint16_t ref)108*77c1e3ccSAndroid Build Coastguard Worker static uint16_t aom_rb_read_primitive_refsubexpfin(
109*77c1e3ccSAndroid Build Coastguard Worker struct aom_read_bit_buffer *rb, uint16_t n, uint16_t k, uint16_t ref) {
110*77c1e3ccSAndroid Build Coastguard Worker return inv_recenter_finite_nonneg(n, ref,
111*77c1e3ccSAndroid Build Coastguard Worker aom_rb_read_primitive_subexpfin(rb, n, k));
112*77c1e3ccSAndroid Build Coastguard Worker }
113*77c1e3ccSAndroid Build Coastguard Worker
aom_rb_read_signed_primitive_refsubexpfin(struct aom_read_bit_buffer * rb,uint16_t n,uint16_t k,int16_t ref)114*77c1e3ccSAndroid Build Coastguard Worker int16_t aom_rb_read_signed_primitive_refsubexpfin(
115*77c1e3ccSAndroid Build Coastguard Worker struct aom_read_bit_buffer *rb, uint16_t n, uint16_t k, int16_t ref) {
116*77c1e3ccSAndroid Build Coastguard Worker ref += n - 1;
117*77c1e3ccSAndroid Build Coastguard Worker const uint16_t scaled_n = (n << 1) - 1;
118*77c1e3ccSAndroid Build Coastguard Worker return aom_rb_read_primitive_refsubexpfin(rb, scaled_n, k, ref) - n + 1;
119*77c1e3ccSAndroid Build Coastguard Worker }
120*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_AV1_DECODER
121