Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vpx_dsp/bitreader.h
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#ifndef VPX_VPX_DSP_BITREADER_H_
12
#define VPX_VPX_DSP_BITREADER_H_
13
14
#include <stddef.h>
15
#include <stdio.h>
16
#include <limits.h>
17
18
#include "./vpx_config.h"
19
#include "vpx_ports/mem.h"
20
#include "vpx/vp8dx.h"
21
#include "vpx/vpx_integer.h"
22
#include "vpx_dsp/prob.h"
23
#if CONFIG_BITSTREAM_DEBUG
24
#include "vpx_util/vpx_debug_util.h"
25
#endif  // CONFIG_BITSTREAM_DEBUG
26
27
#ifdef __cplusplus
28
extern "C" {
29
#endif
30
31
typedef size_t BD_VALUE;
32
33
722M
#define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
34
35
// This is meant to be a large, positive constant that can still be efficiently
36
// loaded as an immediate (on platforms like ARM, for example).
37
// Even relatively modest values like 100 would work fine.
38
390k
#define LOTS_OF_BITS 0x40000000
39
40
typedef struct {
41
  // Be careful when reordering this struct, it may impact the cache negatively.
42
  BD_VALUE value;
43
  unsigned int range;
44
  int count;
45
  const uint8_t *buffer_end;
46
  const uint8_t *buffer;
47
  vpx_decrypt_cb decrypt_cb;
48
  void *decrypt_state;
49
  uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
50
} vpx_reader;
51
52
int vpx_reader_init(vpx_reader *r, const uint8_t *buffer, size_t size,
53
                    vpx_decrypt_cb decrypt_cb, void *decrypt_state);
54
55
void vpx_reader_fill(vpx_reader *r);
56
57
const uint8_t *vpx_reader_find_end(vpx_reader *r);
58
59
8.69M
static INLINE int vpx_reader_has_error(vpx_reader *r) {
60
  // Check if we have reached the end of the buffer.
61
  //
62
  // Variable 'count' stores the number of bits in the 'value' buffer, minus
63
  // 8. The top byte is part of the algorithm, and the remainder is buffered
64
  // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
65
  // occupied, 8 for the algorithm and 8 in the buffer.
66
  //
67
  // When reading a byte from the user's buffer, count is filled with 8 and
68
  // one byte is filled into the value buffer. When we reach the end of the
69
  // data, count is additionally filled with LOTS_OF_BITS. So when
70
  // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
71
  //
72
  // 1 if we have tried to decode bits after the end of stream was encountered.
73
  // 0 No error.
74
8.69M
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
75
8.69M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_reader_has_error
vp9_decodeframe.c:vpx_reader_has_error
Line
Count
Source
59
8.69M
static INLINE int vpx_reader_has_error(vpx_reader *r) {
60
  // Check if we have reached the end of the buffer.
61
  //
62
  // Variable 'count' stores the number of bits in the 'value' buffer, minus
63
  // 8. The top byte is part of the algorithm, and the remainder is buffered
64
  // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
65
  // occupied, 8 for the algorithm and 8 in the buffer.
66
  //
67
  // When reading a byte from the user's buffer, count is filled with 8 and
68
  // one byte is filled into the value buffer. When we reach the end of the
69
  // data, count is additionally filled with LOTS_OF_BITS. So when
70
  // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
71
  //
72
  // 1 if we have tried to decode bits after the end of stream was encountered.
73
  // 0 No error.
74
8.69M
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
75
8.69M
}
Unexecuted instantiation: vp9_detokenize.c:vpx_reader_has_error
Unexecuted instantiation: vp9_decoder.c:vpx_reader_has_error
Unexecuted instantiation: vp9_dsubexp.c:vpx_reader_has_error
Unexecuted instantiation: bitreader.c:vpx_reader_has_error
Unexecuted instantiation: vp9_decodemv.c:vpx_reader_has_error
76
77
157M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
157M
  unsigned int bit = 0;
79
157M
  BD_VALUE value;
80
157M
  BD_VALUE bigsplit;
81
157M
  int count;
82
157M
  unsigned int range;
83
157M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
157M
  if (r->count < 0) vpx_reader_fill(r);
86
87
157M
  value = r->value;
88
157M
  count = r->count;
89
90
157M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
157M
  range = split;
93
94
157M
  if (value >= bigsplit) {
95
67.4M
    range = r->range - split;
96
67.4M
    value = value - bigsplit;
97
67.4M
    bit = 1;
98
67.4M
  }
99
100
157M
  {
101
157M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
157M
    range <<= shift;
103
157M
    value <<= shift;
104
157M
    count -= shift;
105
157M
  }
106
157M
  r->value = value;
107
157M
  r->count = count;
108
157M
  r->range = range;
109
110
#if CONFIG_BITSTREAM_DEBUG
111
  {
112
    const int queue_r = bitstream_queue_get_read();
113
    const int frame_idx = bitstream_queue_get_frame_read();
114
    int ref_result, ref_prob;
115
    bitstream_queue_pop(&ref_result, &ref_prob);
116
    if ((int)bit != ref_result) {
117
      fprintf(stderr,
118
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
119
              "queue_r %d\n",
120
              frame_idx, bit, ref_result, queue_r);
121
122
      assert(0);
123
    }
124
    if (prob != ref_prob) {
125
      fprintf(stderr,
126
              "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
127
              "queue_r %d\n",
128
              frame_idx, prob, ref_prob, queue_r);
129
130
      assert(0);
131
    }
132
  }
133
#endif
134
135
157M
  return bit;
136
157M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read
vp9_decodeframe.c:vpx_read
Line
Count
Source
77
21.3M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
21.3M
  unsigned int bit = 0;
79
21.3M
  BD_VALUE value;
80
21.3M
  BD_VALUE bigsplit;
81
21.3M
  int count;
82
21.3M
  unsigned int range;
83
21.3M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
21.3M
  if (r->count < 0) vpx_reader_fill(r);
86
87
21.3M
  value = r->value;
88
21.3M
  count = r->count;
89
90
21.3M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
21.3M
  range = split;
93
94
21.3M
  if (value >= bigsplit) {
95
10.1M
    range = r->range - split;
96
10.1M
    value = value - bigsplit;
97
10.1M
    bit = 1;
98
10.1M
  }
99
100
21.3M
  {
101
21.3M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
21.3M
    range <<= shift;
103
21.3M
    value <<= shift;
104
21.3M
    count -= shift;
105
21.3M
  }
106
21.3M
  r->value = value;
107
21.3M
  r->count = count;
108
21.3M
  r->range = range;
109
110
#if CONFIG_BITSTREAM_DEBUG
111
  {
112
    const int queue_r = bitstream_queue_get_read();
113
    const int frame_idx = bitstream_queue_get_frame_read();
114
    int ref_result, ref_prob;
115
    bitstream_queue_pop(&ref_result, &ref_prob);
116
    if ((int)bit != ref_result) {
117
      fprintf(stderr,
118
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
119
              "queue_r %d\n",
120
              frame_idx, bit, ref_result, queue_r);
121
122
      assert(0);
123
    }
124
    if (prob != ref_prob) {
125
      fprintf(stderr,
126
              "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
127
              "queue_r %d\n",
128
              frame_idx, prob, ref_prob, queue_r);
129
130
      assert(0);
131
    }
132
  }
133
#endif
134
135
21.3M
  return bit;
136
21.3M
}
Unexecuted instantiation: vp9_detokenize.c:vpx_read
Unexecuted instantiation: vp9_decoder.c:vpx_read
vp9_dsubexp.c:vpx_read
Line
Count
Source
77
40.7M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
40.7M
  unsigned int bit = 0;
79
40.7M
  BD_VALUE value;
80
40.7M
  BD_VALUE bigsplit;
81
40.7M
  int count;
82
40.7M
  unsigned int range;
83
40.7M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
40.7M
  if (r->count < 0) vpx_reader_fill(r);
86
87
40.7M
  value = r->value;
88
40.7M
  count = r->count;
89
90
40.7M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
40.7M
  range = split;
93
94
40.7M
  if (value >= bigsplit) {
95
3.55M
    range = r->range - split;
96
3.55M
    value = value - bigsplit;
97
3.55M
    bit = 1;
98
3.55M
  }
99
100
40.7M
  {
101
40.7M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
40.7M
    range <<= shift;
103
40.7M
    value <<= shift;
104
40.7M
    count -= shift;
105
40.7M
  }
106
40.7M
  r->value = value;
107
40.7M
  r->count = count;
108
40.7M
  r->range = range;
109
110
#if CONFIG_BITSTREAM_DEBUG
111
  {
112
    const int queue_r = bitstream_queue_get_read();
113
    const int frame_idx = bitstream_queue_get_frame_read();
114
    int ref_result, ref_prob;
115
    bitstream_queue_pop(&ref_result, &ref_prob);
116
    if ((int)bit != ref_result) {
117
      fprintf(stderr,
118
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
119
              "queue_r %d\n",
120
              frame_idx, bit, ref_result, queue_r);
121
122
      assert(0);
123
    }
124
    if (prob != ref_prob) {
125
      fprintf(stderr,
126
              "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
127
              "queue_r %d\n",
128
              frame_idx, prob, ref_prob, queue_r);
129
130
      assert(0);
131
    }
132
  }
133
#endif
134
135
40.7M
  return bit;
136
40.7M
}
bitreader.c:vpx_read
Line
Count
Source
77
272k
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
272k
  unsigned int bit = 0;
79
272k
  BD_VALUE value;
80
272k
  BD_VALUE bigsplit;
81
272k
  int count;
82
272k
  unsigned int range;
83
272k
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
272k
  if (r->count < 0) vpx_reader_fill(r);
86
87
272k
  value = r->value;
88
272k
  count = r->count;
89
90
272k
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
272k
  range = split;
93
94
272k
  if (value >= bigsplit) {
95
16.6k
    range = r->range - split;
96
16.6k
    value = value - bigsplit;
97
16.6k
    bit = 1;
98
16.6k
  }
99
100
272k
  {
101
272k
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
272k
    range <<= shift;
103
272k
    value <<= shift;
104
272k
    count -= shift;
105
272k
  }
106
272k
  r->value = value;
107
272k
  r->count = count;
108
272k
  r->range = range;
109
110
#if CONFIG_BITSTREAM_DEBUG
111
  {
112
    const int queue_r = bitstream_queue_get_read();
113
    const int frame_idx = bitstream_queue_get_frame_read();
114
    int ref_result, ref_prob;
115
    bitstream_queue_pop(&ref_result, &ref_prob);
116
    if ((int)bit != ref_result) {
117
      fprintf(stderr,
118
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
119
              "queue_r %d\n",
120
              frame_idx, bit, ref_result, queue_r);
121
122
      assert(0);
123
    }
124
    if (prob != ref_prob) {
125
      fprintf(stderr,
126
              "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
127
              "queue_r %d\n",
128
              frame_idx, prob, ref_prob, queue_r);
129
130
      assert(0);
131
    }
132
  }
133
#endif
134
135
272k
  return bit;
136
272k
}
vp9_decodemv.c:vpx_read
Line
Count
Source
77
94.8M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
94.8M
  unsigned int bit = 0;
79
94.8M
  BD_VALUE value;
80
94.8M
  BD_VALUE bigsplit;
81
94.8M
  int count;
82
94.8M
  unsigned int range;
83
94.8M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
94.8M
  if (r->count < 0) vpx_reader_fill(r);
86
87
94.8M
  value = r->value;
88
94.8M
  count = r->count;
89
90
94.8M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
94.8M
  range = split;
93
94
94.8M
  if (value >= bigsplit) {
95
53.6M
    range = r->range - split;
96
53.6M
    value = value - bigsplit;
97
53.6M
    bit = 1;
98
53.6M
  }
99
100
94.8M
  {
101
94.8M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
94.8M
    range <<= shift;
103
94.8M
    value <<= shift;
104
94.8M
    count -= shift;
105
94.8M
  }
106
94.8M
  r->value = value;
107
94.8M
  r->count = count;
108
94.8M
  r->range = range;
109
110
#if CONFIG_BITSTREAM_DEBUG
111
  {
112
    const int queue_r = bitstream_queue_get_read();
113
    const int frame_idx = bitstream_queue_get_frame_read();
114
    int ref_result, ref_prob;
115
    bitstream_queue_pop(&ref_result, &ref_prob);
116
    if ((int)bit != ref_result) {
117
      fprintf(stderr,
118
              "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
119
              "queue_r %d\n",
120
              frame_idx, bit, ref_result, queue_r);
121
122
      assert(0);
123
    }
124
    if (prob != ref_prob) {
125
      fprintf(stderr,
126
              "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
127
              "queue_r %d\n",
128
              frame_idx, prob, ref_prob, queue_r);
129
130
      assert(0);
131
    }
132
  }
133
#endif
134
135
94.8M
  return bit;
136
94.8M
}
137
138
5.96M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
5.96M
  return vpx_read(r, 128);  // vpx_prob_half
140
5.96M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_bit
vp9_decodeframe.c:vpx_read_bit
Line
Count
Source
138
1.11M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
1.11M
  return vpx_read(r, 128);  // vpx_prob_half
140
1.11M
}
Unexecuted instantiation: vp9_detokenize.c:vpx_read_bit
Unexecuted instantiation: vp9_decoder.c:vpx_read_bit
vp9_dsubexp.c:vpx_read_bit
Line
Count
Source
138
4.57M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
4.57M
  return vpx_read(r, 128);  // vpx_prob_half
140
4.57M
}
bitreader.c:vpx_read_bit
Line
Count
Source
138
272k
static INLINE int vpx_read_bit(vpx_reader *r) {
139
272k
  return vpx_read(r, 128);  // vpx_prob_half
140
272k
}
Unexecuted instantiation: vp9_decodemv.c:vpx_read_bit
141
142
843k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
843k
  int literal = 0, bit;
144
145
4.75M
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
843k
  return literal;
148
843k
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_literal
vp9_decodeframe.c:vpx_read_literal
Line
Count
Source
142
185k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
185k
  int literal = 0, bit;
144
145
926k
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
185k
  return literal;
148
185k
}
Unexecuted instantiation: vp9_detokenize.c:vpx_read_literal
Unexecuted instantiation: vp9_decoder.c:vpx_read_literal
vp9_dsubexp.c:vpx_read_literal
Line
Count
Source
142
657k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
657k
  int literal = 0, bit;
144
145
3.83M
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
657k
  return literal;
148
657k
}
Unexecuted instantiation: bitreader.c:vpx_read_literal
Unexecuted instantiation: vp9_decodemv.c:vpx_read_literal
149
150
static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
151
36.7M
                                const vpx_prob *probs) {
152
36.7M
  vpx_tree_index i = 0;
153
154
83.4M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
36.7M
  return -i;
157
36.7M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_tree
vp9_decodeframe.c:vpx_read_tree
Line
Count
Source
151
8.41M
                                const vpx_prob *probs) {
152
8.41M
  vpx_tree_index i = 0;
153
154
14.5M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
8.41M
  return -i;
157
8.41M
}
Unexecuted instantiation: vp9_detokenize.c:vpx_read_tree
Unexecuted instantiation: vp9_decoder.c:vpx_read_tree
Unexecuted instantiation: vp9_dsubexp.c:vpx_read_tree
Unexecuted instantiation: bitreader.c:vpx_read_tree
vp9_decodemv.c:vpx_read_tree
Line
Count
Source
151
28.3M
                                const vpx_prob *probs) {
152
28.3M
  vpx_tree_index i = 0;
153
154
68.9M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
28.3M
  return -i;
157
28.3M
}
158
159
#ifdef __cplusplus
160
}  // extern "C"
161
#endif
162
163
#endif  // VPX_VPX_DSP_BITREADER_H_