-
Notifications
You must be signed in to change notification settings - Fork 1
/
getbit.h
225 lines (200 loc) · 4.82 KB
/
getbit.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Mutated into DGIndex. Modifications Copyright (C) 2004, Donald Graft
*
* Copyright (C) Chia-chen Kuo - April 2001
*
* This file is part of DVD2AVI, a free MPEG-2 decoder
*
* DVD2AVI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* DVD2AVI 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifdef GETBIT_GLOBAL
#define GXTN
#else
#define GXTN extern
#endif
void Initialize_Buffer(void);
void Fill_Buffer(void);
void Next_Packet(void);
void Flush_Buffer_All(unsigned int N);
unsigned int Get_Bits_All(unsigned int N);
void Next_File(void);
GXTN unsigned char *Rdbfr, *Rdptr, *Rdmax;
GXTN unsigned int BitsLeft, CurrentBfr, NextBfr, Val, Read;
GXTN __int64 CurrentPackHeaderPosition;
__forceinline static unsigned int Show_Bits(unsigned int N)
{
if (N <= BitsLeft)
{
return (CurrentBfr << (32 - BitsLeft)) >> (32 - N);
}
else
{
N -= BitsLeft;
return (((CurrentBfr << (32 - BitsLeft)) >> (32 - BitsLeft)) << N) + (NextBfr >> (32 - N));
}
}
__forceinline static unsigned int Get_Bits(unsigned int N)
{
if (N < BitsLeft)
{
Val = (CurrentBfr << (32 - BitsLeft)) >> (32 - N);
BitsLeft -= N;
return Val;
}
else
return Get_Bits_All(N);
}
__forceinline static void Flush_Buffer(unsigned int N)
{
if (N < BitsLeft)
BitsLeft -= N;
else
Flush_Buffer_All(N);
}
int _donread(int fd, void *buffer, unsigned int count);
__forceinline static unsigned int Get_Byte()
{
extern unsigned char *buffer_invalid;
if (Rdptr >= buffer_invalid)
{
// Ran out of good data.
if (LoopPlayback)
ThreadKill(END_OF_DATA_KILL);
Stop_Flag = 1;
return 0xff;
}
while (Rdptr >= Rdbfr+BUFFER_SIZE)
{
// Read = _donread(Infile[CurrentFile], Rdbfr, BUFFER_SIZE);
Read = fread(Rdbfr, 1, BUFFER_SIZE, Infile[CurrentFile]);
if (Read < BUFFER_SIZE)
Next_File();
Rdptr -= BUFFER_SIZE;
Rdmax -= BUFFER_SIZE;
}
return *Rdptr++;
}
__forceinline static void Fill_Next()
{
extern unsigned char *buffer_invalid;
if (Rdptr >= buffer_invalid)
{
// Ran out of good data.
if (LoopPlayback)
ThreadKill(END_OF_DATA_KILL);
Stop_Flag = 1;
NextBfr = 0xffffffff;
return;
}
CurrentPackHeaderPosition = PackHeaderPosition;
if (SystemStream_Flag != ELEMENTARY_STREAM && Rdptr > Rdmax - 4 && !AudioOnly_Flag)
{
if (Rdptr >= Rdmax)
Next_Packet();
NextBfr = Get_Byte() << 24;
if (Rdptr >= Rdmax)
Next_Packet();
NextBfr += Get_Byte() << 16;
if (Rdptr >= Rdmax)
Next_Packet();
NextBfr += Get_Byte() << 8;
if (Rdptr >= Rdmax)
Next_Packet();
NextBfr += Get_Byte();
}
else if (Rdptr <= Rdbfr+BUFFER_SIZE - 4)
{
#if 1
NextBfr = (*Rdptr << 24) + (*(Rdptr+1) << 16) + (*(Rdptr+2) << 8) + *(Rdptr+3);
#else
if (Rdptr < buffer_invalid)
{
NextBfr = (*Rdptr << 24);
if (Rdptr+1 < buffer_invalid)
{
NextBfr += (*(Rdptr+1) << 16);
if (Rdptr+2 < buffer_invalid)
{
NextBfr += (*(Rdptr+2) << 8);
if (Rdptr+3 < buffer_invalid)
{
NextBfr += (*(Rdptr+3));
}
else
{
NextBfr += 0xff;
Stop_Flag = 1;
}
}
else
{
NextBfr += 0xffff;
Stop_Flag = 1;
}
}
else
{
NextBfr += 0xffffff;
Stop_Flag = 1;
}
}
else
{
NextBfr = 0xffffffff;
Stop_Flag = 1;
}
#endif
Rdptr += 4;
}
else
{
if (Rdptr >= Rdbfr+BUFFER_SIZE)
Fill_Buffer();
NextBfr = *Rdptr++ << 24;
if (Rdptr >= Rdbfr+BUFFER_SIZE)
Fill_Buffer();
NextBfr += *Rdptr++ << 16;
if (Rdptr >= Rdbfr+BUFFER_SIZE)
Fill_Buffer();
NextBfr += *Rdptr++ << 8;
if (Rdptr >= Rdbfr+BUFFER_SIZE)
Fill_Buffer();
NextBfr += *Rdptr++;
}
}
__forceinline static void next_start_code()
{
unsigned int show;
// This is contrary to the spec but is more resilient to some
// stream corruption scenarios.
BitsLeft = ((BitsLeft + 7) / 8) * 8;
while (1)
{
show = Show_Bits(24);
if (Stop_Flag == true)
return;
if (show == 0x000001)
return;
Flush_Buffer(8);
}
}
__forceinline static unsigned int Get_Short()
{
unsigned int i, j;
i = Get_Byte();
j = Get_Byte();
return ((i << 8) | j);
}