Coverage Report

Created: 2025-12-31 07:57

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
800M
#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
427k
#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
9.84M
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
9.84M
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
75
9.84M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_reader_has_error
vp9_decodeframe.c:vpx_reader_has_error
Line
Count
Source
59
9.84M
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
9.84M
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
75
9.84M
}
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
174M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
174M
  unsigned int bit = 0;
79
174M
  BD_VALUE value;
80
174M
  BD_VALUE bigsplit;
81
174M
  int count;
82
174M
  unsigned int range;
83
174M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
174M
  if (r->count < 0) vpx_reader_fill(r);
86
87
174M
  value = r->value;
88
174M
  count = r->count;
89
90
174M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
174M
  range = split;
93
94
174M
  if (value >= bigsplit) {
95
73.8M
    range = r->range - split;
96
73.8M
    value = value - bigsplit;
97
73.8M
    bit = 1;
98
73.8M
  }
99
100
174M
  {
101
174M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
174M
    range <<= shift;
103
174M
    value <<= shift;
104
174M
    count -= shift;
105
174M
  }
106
174M
  r->value = value;
107
174M
  r->count = count;
108
174M
  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
174M
  return bit;
136
174M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read
vp9_decodeframe.c:vpx_read
Line
Count
Source
77
23.8M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
23.8M
  unsigned int bit = 0;
79
23.8M
  BD_VALUE value;
80
23.8M
  BD_VALUE bigsplit;
81
23.8M
  int count;
82
23.8M
  unsigned int range;
83
23.8M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
23.8M
  if (r->count < 0) vpx_reader_fill(r);
86
87
23.8M
  value = r->value;
88
23.8M
  count = r->count;
89
90
23.8M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
23.8M
  range = split;
93
94
23.8M
  if (value >= bigsplit) {
95
11.2M
    range = r->range - split;
96
11.2M
    value = value - bigsplit;
97
11.2M
    bit = 1;
98
11.2M
  }
99
100
23.8M
  {
101
23.8M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
23.8M
    range <<= shift;
103
23.8M
    value <<= shift;
104
23.8M
    count -= shift;
105
23.8M
  }
106
23.8M
  r->value = value;
107
23.8M
  r->count = count;
108
23.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
23.8M
  return bit;
136
23.8M
}
Unexecuted instantiation: vp9_detokenize.c:vpx_read
Unexecuted instantiation: vp9_decoder.c:vpx_read
vp9_dsubexp.c:vpx_read
Line
Count
Source
77
44.6M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
44.6M
  unsigned int bit = 0;
79
44.6M
  BD_VALUE value;
80
44.6M
  BD_VALUE bigsplit;
81
44.6M
  int count;
82
44.6M
  unsigned int range;
83
44.6M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
44.6M
  if (r->count < 0) vpx_reader_fill(r);
86
87
44.6M
  value = r->value;
88
44.6M
  count = r->count;
89
90
44.6M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
44.6M
  range = split;
93
94
44.6M
  if (value >= bigsplit) {
95
3.65M
    range = r->range - split;
96
3.65M
    value = value - bigsplit;
97
3.65M
    bit = 1;
98
3.65M
  }
99
100
44.6M
  {
101
44.6M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
44.6M
    range <<= shift;
103
44.6M
    value <<= shift;
104
44.6M
    count -= shift;
105
44.6M
  }
106
44.6M
  r->value = value;
107
44.6M
  r->count = count;
108
44.6M
  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
44.6M
  return bit;
136
44.6M
}
bitreader.c:vpx_read
Line
Count
Source
77
301k
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
301k
  unsigned int bit = 0;
79
301k
  BD_VALUE value;
80
301k
  BD_VALUE bigsplit;
81
301k
  int count;
82
301k
  unsigned int range;
83
301k
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
301k
  if (r->count < 0) vpx_reader_fill(r);
86
87
301k
  value = r->value;
88
301k
  count = r->count;
89
90
301k
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
301k
  range = split;
93
94
301k
  if (value >= bigsplit) {
95
22.8k
    range = r->range - split;
96
22.8k
    value = value - bigsplit;
97
22.8k
    bit = 1;
98
22.8k
  }
99
100
301k
  {
101
301k
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
301k
    range <<= shift;
103
301k
    value <<= shift;
104
301k
    count -= shift;
105
301k
  }
106
301k
  r->value = value;
107
301k
  r->count = count;
108
301k
  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
301k
  return bit;
136
301k
}
vp9_decodemv.c:vpx_read
Line
Count
Source
77
105M
static INLINE int vpx_read(vpx_reader *r, int prob) {
78
105M
  unsigned int bit = 0;
79
105M
  BD_VALUE value;
80
105M
  BD_VALUE bigsplit;
81
105M
  int count;
82
105M
  unsigned int range;
83
105M
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
84
85
105M
  if (r->count < 0) vpx_reader_fill(r);
86
87
105M
  value = r->value;
88
105M
  count = r->count;
89
90
105M
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
91
92
105M
  range = split;
93
94
105M
  if (value >= bigsplit) {
95
58.9M
    range = r->range - split;
96
58.9M
    value = value - bigsplit;
97
58.9M
    bit = 1;
98
58.9M
  }
99
100
105M
  {
101
105M
    const unsigned char shift = vpx_norm[(unsigned char)range];
102
105M
    range <<= shift;
103
105M
    value <<= shift;
104
105M
    count -= shift;
105
105M
  }
106
105M
  r->value = value;
107
105M
  r->count = count;
108
105M
  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
105M
  return bit;
136
105M
}
137
138
6.30M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
6.30M
  return vpx_read(r, 128);  // vpx_prob_half
140
6.30M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_bit
vp9_decodeframe.c:vpx_read_bit
Line
Count
Source
138
1.16M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
1.16M
  return vpx_read(r, 128);  // vpx_prob_half
140
1.16M
}
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.84M
static INLINE int vpx_read_bit(vpx_reader *r) {
139
4.84M
  return vpx_read(r, 128);  // vpx_prob_half
140
4.84M
}
bitreader.c:vpx_read_bit
Line
Count
Source
138
301k
static INLINE int vpx_read_bit(vpx_reader *r) {
139
301k
  return vpx_read(r, 128);  // vpx_prob_half
140
301k
}
Unexecuted instantiation: vp9_decodemv.c:vpx_read_bit
141
142
915k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
915k
  int literal = 0, bit;
144
145
5.04M
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
915k
  return literal;
148
915k
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_literal
vp9_decodeframe.c:vpx_read_literal
Line
Count
Source
142
197k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
197k
  int literal = 0, bit;
144
145
950k
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
197k
  return literal;
148
197k
}
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
718k
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
143
718k
  int literal = 0, bit;
144
145
4.09M
  for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
146
147
718k
  return literal;
148
718k
}
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
41.4M
                                const vpx_prob *probs) {
152
41.4M
  vpx_tree_index i = 0;
153
154
92.8M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
41.4M
  return -i;
157
41.4M
}
Unexecuted instantiation: vp9_dx_iface.c:vpx_read_tree
vp9_decodeframe.c:vpx_read_tree
Line
Count
Source
151
9.63M
                                const vpx_prob *probs) {
152
9.63M
  vpx_tree_index i = 0;
153
154
16.6M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
9.63M
  return -i;
157
9.63M
}
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
31.8M
                                const vpx_prob *probs) {
152
31.8M
  vpx_tree_index i = 0;
153
154
76.1M
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
155
156
31.8M
  return -i;
157
31.8M
}
158
159
#ifdef __cplusplus
160
}  // extern "C"
161
#endif
162
163
#endif  // VPX_VPX_DSP_BITREADER_H_