Coverage Report

Created: 2026-02-14 07:09

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