1 /************************************************************************
2 *
3 * getbits.c, bit level routines for tmndecode (H.263 decoder)
4 * Copyright (C) 1995, 1996 Telenor R&D, Norway
5 *
6 * Contacts:
7 * Robert Danielsen <Robert.Danielsen@nta.no>
8 *
9 * Telenor Research and Development http://www.nta.no/brukere/DVC/
10 * P.O.Box 83 tel.: +47 63 84 84 00
11 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76
12 *
13 * Copyright (C) 1997 University of BC, Canada
14 * Modified by: Michael Gallant <mikeg@ee.ubc.ca>
15 * Guy Cote <guyc@ee.ubc.ca>
16 * Berna Erol <bernae@ee.ubc.ca>
17 *
18 * Contacts:
19 * Michael Gallant <mikeg@ee.ubc.ca>
20 *
21 * UBC Image Processing Laboratory http://www.ee.ubc.ca/image
22 * 2356 Main Mall tel.: +1 604 822 4051
23 * Vancouver BC Canada V6T1Z4 fax.: +1 604 822 5949
24 *
25 ************************************************************************/
26
27 /* Disclaimer of Warranty
28 *
29 * These software programs are available to the user without any license fee
30 * or royalty on an "as is" basis. The University of British Columbia
31 * disclaims any and all warranties, whether express, implied, or
32 * statuary, including any implied warranties or merchantability or of
33 * fitness for a particular purpose. In no event shall the
34 * copyright-holder be liable for any incidental, punitive, or
35 * consequential damages of any kind whatsoever arising from the use of
36 * these programs.
37 *
38 * This disclaimer of warranty extends to the user of these programs and
39 * user's customers, employees, agents, transferees, successors, and
40 * assigns.
41 *
42 * The University of British Columbia does not represent or warrant that the
43 * programs furnished hereunder are free of infringement of any
44 * third-party patents.
45 *
46 * Commercial implementations of H.263, including shareware, are subject to
47 * royalty fees to patent holders. Many of these patents are general
48 * enough such that they are unavoidable regardless of implementation
49 * design.
50 *
51 */
52
53
54 /* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
55 * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de>
56 *
57 */
58
59
60 #include <stdlib.h>
61
62 #include "config.h"
63 #include "tmndec.h"
64 #include "global.h"
65 #ifdef WIN32
66 #include <io.h>
67 #endif
68
69
70 /* to mask the n least significant bits of an integer */
71
72 static unsigned int msk[33] =
73 {
74 0x00000000, 0x00000001, 0x00000003, 0x00000007,
75 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
76 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
77 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
78 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
79 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
80 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
81 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
82 0xffffffff
83 };
84
85 #ifdef VIC
86 char *vic_packetbuf;
87 #endif
88
89 /* initialize buffer, call once before first getbits or showbits */
90
91 void initbits ()
92 {
93 ld->incnt = 0;
94 ld->rdptr = ld->rdbfr + 2048;
95 ld->bitcnt = 0;
96 }
97
98 void fillbfr ()
99 {
100 int l;
101
102 ld->inbfr[0] = ld->inbfr[8];
103 ld->inbfr[1] = ld->inbfr[9];
104 ld->inbfr[2] = ld->inbfr[10];
105 ld->inbfr[3] = ld->inbfr[11];
106
107 if (ld->rdptr >= ld->rdbfr + 2048)
108 {
109 #ifdef VIC
110 memcpy(ld->rdbfr, vic_packetbuf, 2048);
111 vic_packetbuf += 2048;
112 l = 2048;
113 #else
114 l = read (ld->infile, ld->rdbfr, 2048);
115 #endif
116 ld->rdptr = ld->rdbfr;
117 if (l < 2048)
118 {
119 if (l < 0)
120 l = 0;
121
122 while (l < 2048) /* Add recognizable sequence end code */
123 {
124 ld->rdbfr[l++] = 0;
125 ld->rdbfr[l++] = 0;
126 ld->rdbfr[l++] = (1 << 7) | (SE_CODE << 2);
127 }
128 }
129 }
130 for (l = 0; l < 8; l++)
131 ld->inbfr[l + 4] = ld->rdptr[l];
132
133 ld->rdptr += 8;
134 ld->incnt += 64;
135 }
136
137
138 /* return next n bits (right adjusted) without advancing */
139
140 unsigned int showbits (int n)
141 {
142 unsigned char *v;
143 unsigned int b;
144 int c;
145
146 if (ld->incnt < n)
147 fillbfr ();
148
149 v = ld->inbfr + ((96 - ld->incnt) >> 3);
150 b = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3];
151 c = ((ld->incnt - 1) & 7) + 25;
152 return (b >> (c - n)) & msk[n];
153 }
154
155
156 /* return next bit (could be made faster than getbits(1)) */
157
158 unsigned int getbits1 ()
159 {
160 return getbits (1);
161 }
162
163
164 /* advance by n bits */
165
166 void flushbits (int n)
167 {
168
169 ld->bitcnt += n;
170 ld->incnt -= n;
171 if (ld->incnt < 0)
172 fillbfr ();
173 }
174
175
176 /* return next n bits (right adjusted) */
177
178 unsigned int getbits (int n)
179 {
180 unsigned int l;
181
182 l = showbits (n);
183 flushbits (n);
184
185 return l;
186 }
187
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.