Coverage Report

Created: 2026-05-30 06:10

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
0
{
32
0
  data = buffer;
33
0
  bytes_remaining = len;
34
0
}
35
36
void bitreader::refill()
37
0
{
38
0
  int shift = 64-nextbits_cnt;
39
40
0
  while (shift >= 8 && bytes_remaining) {
41
0
    uint64_t newval = *data++;
42
0
    bytes_remaining--;
43
44
0
    shift -= 8;
45
0
    newval <<= shift;
46
0
    nextbits |= newval;
47
0
  }
48
49
0
  nextbits_cnt = 64-shift;
50
0
}
51
52
uint32_t bitreader::get_bits(int n)
53
0
{
54
0
  if (n == 0) return 0;
55
0
  assert(n<=32);
56
57
0
  if (nextbits_cnt < n) {
58
0
    refill();
59
0
  }
60
61
0
  uint64_t val = nextbits;
62
0
  val >>= 64-n;
63
64
0
  nextbits <<= n;
65
0
  nextbits_cnt -= n;
66
67
0
  return val;
68
0
}
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
0
{
103
0
  if (nextbits_cnt < n) {
104
0
    refill();
105
0
  }
106
107
0
  nextbits <<= n;
108
0
  nextbits_cnt -= n;
109
0
}
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
0
{
119
0
  int nskip = (nextbits_cnt & 7);
120
121
0
  nextbits <<= nskip;
122
0
  nextbits_cnt -= nskip;
123
0
}
124
125
void bitreader::prepare_for_CABAC()
126
0
{
127
0
  skip_to_byte_boundary();
128
129
0
  int rewind = nextbits_cnt/8;
130
0
  data -= rewind;
131
0
  bytes_remaining += rewind;
132
0
  nextbits = 0;
133
0
  nextbits_cnt = 0;
134
0
}
135
136
uint32_t bitreader::get_uvlc()
137
0
{
138
0
  int num_zeros=0;
139
140
0
  while (get_bits(1)==0) {
141
0
    num_zeros++;
142
143
0
    if (num_zeros > MAX_UVLC_LEADING_ZEROS) { return UVLC_ERROR; }
144
0
  }
145
146
0
  if (num_zeros != 0) {
147
0
    uint32_t offset = get_bits(num_zeros);
148
0
    uint32_t value = offset + (static_cast<uint32_t>(1)<<num_zeros)-1;
149
0
    assert(value>0);
150
0
    return value;
151
0
  } else {
152
0
    return 0;
153
0
  }
154
0
}
155
156
int32_t bitreader::get_svlc()
157
0
{
158
0
  uint32_t v = get_uvlc();
159
0
  if (v==0) return 0;
160
0
  if (v==UVLC_ERROR) return SVLC_ERROR;
161
162
0
  bool negative = ((v&1)==0);
163
0
  return negative ? -static_cast<int32_t>(v/2) : static_cast<int32_t>((v+1)/2);
164
0
}
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
}