Coverage Report

Created: 2025-12-31 06:49

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