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