Coverage Report

Created: 2026-09-14 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/bitstream.cc
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "bitstream.h"
22
#include "de265.h"
23
24
#include <stdlib.h>
25
#include <string.h>
26
#include <assert.h>
27
28
29
30
bitreader::bitreader(unsigned char* buffer, int len)
31
134k
{
32
134k
  data = buffer;
33
134k
  bytes_remaining = len;
34
134k
}
35
36
void bitreader::refill()
37
6.55M
{
38
6.55M
  int shift = 64-nextbits_cnt;
39
40
11.8M
  while (shift >= 8 && bytes_remaining) {
41
5.28M
    uint64_t newval = *data++;
42
5.28M
    bytes_remaining--;
43
44
5.28M
    shift -= 8;
45
5.28M
    newval <<= shift;
46
5.28M
    nextbits |= newval;
47
5.28M
  }
48
49
6.55M
  nextbits_cnt = 64-shift;
50
6.55M
}
51
52
uint32_t bitreader::get_bits(int n)
53
28.6M
{
54
28.6M
  if (n == 0) return 0;
55
28.6M
  assert(n<=32);
56
57
28.6M
  if (nextbits_cnt < n) {
58
6.39M
    refill();
59
6.39M
  }
60
61
28.6M
  uint64_t val = nextbits;
62
28.6M
  val >>= 64-n;
63
64
28.6M
  nextbits <<= n;
65
28.6M
  nextbits_cnt -= n;
66
67
28.6M
  return val;
68
28.6M
}
69
70
uint32_t bitreader::get_bits_fast(int n)
71
0
{
72
0
  if (n == 0) return 0;
73
0
  assert(n<=32);
74
75
0
  assert(nextbits_cnt >= n);
76
77
0
  uint64_t val = nextbits;
78
0
  val >>= 64-n;
79
80
0
  nextbits <<= n;
81
0
  nextbits_cnt -= n;
82
83
0
  return val;
84
0
}
85
86
uint32_t bitreader::peek_bits(int n)
87
0
{
88
0
  if (n == 0) return 0;
89
0
  assert(n<=32);
90
91
0
  if (nextbits_cnt < n) {
92
0
    refill();
93
0
  }
94
95
0
  uint64_t val = nextbits;
96
0
  val >>= 64-n;
97
98
0
  return val;
99
0
}
100
101
void bitreader::skip_bits(int n)
102
287k
{
103
287k
  if (nextbits_cnt < n) {
104
155k
    refill();
105
155k
  }
106
107
287k
  nextbits <<= n;
108
287k
  nextbits_cnt -= n;
109
287k
}
110
111
void bitreader::skip_bits_fast(int n)
112
0
{
113
0
  nextbits <<= n;
114
0
  nextbits_cnt -= n;
115
0
}
116
117
void bitreader::skip_to_byte_boundary()
118
23.8k
{
119
23.8k
  int nskip = (nextbits_cnt & 7);
120
121
23.8k
  nextbits <<= nskip;
122
23.8k
  nextbits_cnt -= nskip;
123
23.8k
}
124
125
void bitreader::prepare_for_CABAC()
126
23.8k
{
127
23.8k
  skip_to_byte_boundary();
128
129
23.8k
  int rewind = nextbits_cnt/8;
130
23.8k
  data -= rewind;
131
23.8k
  bytes_remaining += rewind;
132
23.8k
  nextbits = 0;
133
23.8k
  nextbits_cnt = 0;
134
23.8k
}
135
136
uint32_t bitreader::get_uvlc()
137
6.36M
{
138
6.36M
  int num_zeros=0;
139
140
15.1M
  while (get_bits(1)==0) {
141
8.76M
    num_zeros++;
142
143
8.76M
    if (num_zeros > MAX_UVLC_LEADING_ZEROS) { return UVLC_ERROR; }
144
8.76M
  }
145
146
6.36M
  if (num_zeros != 0) {
147
2.89M
    uint32_t offset = get_bits(num_zeros);
148
2.89M
    uint32_t value = offset + (static_cast<uint32_t>(1)<<num_zeros)-1;
149
2.89M
    assert(value>0);
150
2.89M
    return value;
151
3.46M
  } else {
152
3.46M
    return 0;
153
3.46M
  }
154
6.36M
}
155
156
int32_t bitreader::get_svlc()
157
5.26M
{
158
5.26M
  uint32_t v = get_uvlc();
159
5.26M
  if (v==0) return 0;
160
2.34M
  if (v==UVLC_ERROR) return SVLC_ERROR;
161
162
2.34M
  bool negative = ((v&1)==0);
163
2.34M
  return negative ? -static_cast<int32_t>(v/2) : static_cast<int32_t>((v+1)/2);
164
2.34M
}
165
166
bool bitreader::check_rbsp_trailing_bits()
167
0
{
168
0
  int stop_bit = get_bits(1);
169
0
  assert(stop_bit==1);
170
0
  (void)stop_bit;
171
172
0
  while (nextbits_cnt>0 || bytes_remaining>0) {
173
0
    int filler = get_bits(1);
174
0
    if (filler!=0) {
175
0
      return false;
176
0
    }
177
0
  }
178
179
0
  return true;
180
0
}