-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbits.c
280 lines (226 loc) · 6.1 KB
/
getbits.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* getbits.c, bit level routines */
/*
* All modifications (mpeg2decode -> mpeg2play) are
* Copyright (C) 1994, Stefan Eckart. All Rights Reserved.
*/
/* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <byteswap.h>
#include <unistd.h>
#include "config.h"
#include "global.h"
static int nextpacket(void);
/* initialize buffer, call once before first getbits or showbits */
void initbits()
{
ld->outcnt = MAXBITS;
ld->bytesInPacket = 0;
flushbits32();
}
/* align to start of next startcode */
void startcode()
{
/* byte align */
if(ld->outcnt & 7)
flushbits(8 - (ld->outcnt & 7));
while (showbits(24) != 1)
flushbits(8);
}
static void padbuffer(void)
{
unsigned char *ptr = (unsigned char*)(ld->rdBfr);
unsigned int *rdptr;
ptr += (MAXBITS - ld->outcnt) >> 3;
while((unsigned long)ptr & 3L) {
*ptr++ = 0;
ld->outcnt -= 8;
}
rdptr = (unsigned int*)ptr;
while(ld->outcnt > 0) {
#ifdef BIG_ENDIAN
*rdptr++ = SEQ_END_CODE;
#else
*rdptr++ = 0xB7010000;
#endif
ld->outcnt -= 32;
}
}
static void readbytes(int n)
{
unsigned char *ptr = (unsigned char*)(ld->rdBfr);
int num;
ptr += (MAXBITS - ld->outcnt) >> 3;
num = read(ld->infile, ptr, n);
if(num < 0) num = 0;
ld->outcnt -= (num << 3);
if(num != n) /* file came up short */
padbuffer();
}
void flushend(int n)
{
int i;
if(MAXBITS > ld->outcnt) {
if(n == 32)
ld->Bfr = (ld->rdBfr[MAXDWORDS - 1] << (ld->outcnt & 31));
else
ld->Bfr |= (ld->rdBfr[MAXDWORDS - 1] << (ld->outcnt & 31)) >> (32 - n);
n -= (MAXBITS - ld->outcnt);
ld->outcnt = MAXBITS;
}
if(sysstream) {
while(ld->outcnt) { /* ld->outcnt is bits needed at this point */
if(ld->bytesInPacket) {
int bytesToRead = ld->outcnt >> 3;
if(bytesToRead > ld->bytesInPacket)
bytesToRead = ld->bytesInPacket;
readbytes(bytesToRead);
ld->bytesInPacket -= bytesToRead;
} else ld->bytesInPacket = nextpacket();
}
} else readbytes(MAXBYTES);
#ifndef BIG_ENDIAN
for(i = 0; i < MAXDWORDS; i++)
ld->rdBfr[i] = bswap_32(ld->rdBfr[i]);
#endif
if(n) {
if(n == 32)
ld->Bfr = ld->rdBfr[0];
else
ld->Bfr |= ld->rdBfr[0] >> (32 - n);
ld->outcnt += n;
}
}
/* MPEG-1 system layer demultiplexer */
static inline int getbyte()
{
unsigned char data;
read(ld->infile, &data, 1);
return data;
}
static inline int getword()
{
unsigned short data;
read(ld->infile, &data, 2);
#ifndef BIG_ENDIAN
data = bswap_16(data);
#endif
return data;
}
static inline int getlong()
{
unsigned int data;
if(!read(ld->infile, &data, 4))
return ISO_END_CODE;
#ifndef BIG_ENDIAN
data = bswap_32(data);
#endif
return data;
}
static inline void skipbytes(int n)
{
lseek(ld->infile, n, SEEK_CUR);
}
/* parse system layer, ignore everything we don't need */
static int nextpacket(void)
{
unsigned int code;
int length;
while(1) {
code = getlong();
switch(code)
{
case PACK_START_CODE: /* pack header */
/* skip pack header (system_clock_reference and mux_rate) */
code = getword();
if((code >> 12) == 2) { /* MPEG 1 */
skipbytes(6);
} else
if(code & 0x4000) {
skipbytes(7);
skipbytes(getbyte() & 7);
}
break;
case 0x1e0: /* video stream 0 packet */
length = getword() - 1; /* packet length */
code = getbyte();
if((code >> 6) == 0x02) {
int skip;
skipbytes(1);
skip = getbyte();
skipbytes(skip);
length -= (skip + 2);
return length;
}
while(code == 0xff) {
code = getbyte();
length--;
}
if((code & 0x40) == 0x40) { /* skip STD_buffer_scale */
skipbytes(1);
code = getbyte();
length -= 2;
}
if(code>=0x30) {
if(code>=0x40) {
fprintf(stderr,"Error in packet header\n");
exit(1);
}
/* skip presentation and decoding time stamps */
skipbytes(9);
length -= 9;
}
else if(code>=0x20) {
/* skip presentation time stamps */
skipbytes(4);
length -= 4;
}
else if(code!=0x0f) {
fprintf(stderr,"Error in packet header\n");
exit(1);
}
if(length < 0) {
fprintf(stderr,"Bad packet length\n");
exit(1);
}
return length;
case ISO_END_CODE: /* end */
/* simulate a buffer full of sequence end codes */
padbuffer();
return 0;
default:
if(code>=SYSTEM_START_CODE) {
/* skip system headers and non-video packets*/
skipbytes(getword());
} else {
fprintf(stderr,"Unexpected startcode %08x in system layer\n",code);
exit(1);
}
break;
}
}
}