-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetbits.c
executable file
·180 lines (145 loc) · 4.29 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
// based on tmn codec source file getbits.c from tmndecode:
/************************************************************************
*
* getbits.c, bit level routines for tmndecode (H.263 decoder)
* Copyright (C) 1995, 1996 Telenor R&D, Norway
* Karl Olav Lillevold <[email protected]>
*
* Contacts:
* Karl Olav Lillevold <[email protected]>, or
* Robert Danielsen <[email protected]>
*
* Telenor Research and Development http://www.nta.no/brukere/DVC/
* P.O.Box 83 tel.: +47 63 84 84 00
* N-2007 Kjeller, Norway fax.: +47 63 81 00 76
*
************************************************************************/
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. Telenor Research and
* Development 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.
*
* Telenor Research and Development does not represent or warrant that
* the programs furnished hereunder are free of infringement of any
* third-party patents.
*
* Commercial implementations of H.263, 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.
* */
/*
* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
* and mpeg2play, (C) 1994 Stefan Eckart
*
*/
#include <stdlib.h>
#include "codec.h"
#ifdef WIN32
#include <io.h>
#endif
unsigned int getbits(int n);
void flushbits(int n);
static unsigned char inbfr[16];
static unsigned char *rdptr;
static unsigned char rdbfr[2051];
FILE *infile;
int incnt, getbitcnt;
/* to mask the n least significant bits of an integer */
static unsigned int msk[33] =
{
0x00000000,0x00000001,0x00000003,0x00000007,
0x0000000f,0x0000001f,0x0000003f,0x0000007f,
0x000000ff,0x000001ff,0x000003ff,0x000007ff,
0x00000fff,0x00001fff,0x00003fff,0x00007fff,
0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
0x000fffff,0x001fffff,0x003fffff,0x007fffff,
0x00ffffff,0x01ffffff,0x03ffffff,0x07ffffff,
0x0fffffff,0x1fffffff,0x3fffffff,0x7fffffff,
0xffffffff
};
/* initialize buffer, call once before first getbits or showbits */
void initgetbits(CONTEXT context)
{
incnt = 0;
rdptr = rdbfr + 2048;
getbitcnt = 0;
infile = context.i_stream;
}
void fillbfr()
{
int l;
inbfr[0] = inbfr[8];
inbfr[1] = inbfr[9];
inbfr[2] = inbfr[10];
inbfr[3] = inbfr[11];
if (rdptr>=rdbfr+2048)
{
// l = read(infile,rdbfr,2048);
l = fread(rdbfr,1,2048,infile);
rdptr = rdbfr;
if (l<2048)
{
if (l<0)
l = 0;
while (l<2048) /* Add recognizable sequence end code */
{
rdbfr[l++] = 0;
rdbfr[l++] = 0;
#define SE_CODE 31
rdbfr[l++] = (1<<7) | (SE_CODE<<2);
}
}
}
for (l=0; l<8; l++)
inbfr[l+4] = rdptr[l];
rdptr+= 8;
incnt+= 64;
}
/* return next n bits (right adjusted) without advancing */
unsigned int showbits(n)
int n;
{
unsigned char *v;
unsigned int b;
int c;
if (incnt<n)
fillbfr();
v = inbfr + ((96 - incnt)>>3);
b = (v[0]<<24) | (v[1]<<16) | (v[2]<<8) | v[3];
c = ((incnt-1) & 7) + 25;
return (b>>(c-n)) & msk[n];
}
/* return next bit (could be made faster than getbits(1)) */
unsigned int getbits1()
{
return getbits(1);
}
/* advance by n bits */
void flushbits(int n)
{
getbitcnt+= n;
incnt-= n;
if (incnt < 0)
fillbfr();
}
/* return next n bits (right adjusted) */
unsigned int getbits(int n)
{
unsigned int l;
l = showbits(n);
flushbits(n);
return l;
}