File: ibitstream.cc

package info (click to toggle)
maplay 1.2b-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 432 kB
  • ctags: 441
  • sloc: cpp: 5,421; sh: 97; makefile: 86
file content (251 lines) | stat: -rw-r--r-- 6,109 bytes parent folder | download
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
/*
 *  @(#) ibitstream.cc 1.11, last edit: 30 Nov 1994 16:11:26
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *  Changes from version 1.2 to 1.3:
 *    - Modifed by Pete French in June 1994 - for a fat binary program
 *      we cannot have a correct value for DAMN_INTEL_BYTE_ORDER so this
 *      will now define it based on the __LITTLE_ENDIAN__ define provided
 *      by the NeXTStep compiler.
 *
 *  Changes from version 1.1 to 1.2:
 *    - third argument in open syscall added
 *    - minor bug in get_header() fixed
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <iostream.h>
#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ > 0
#include <endian.h>
#else
#include <bytesex.h>
#endif
#if __BYTEORDER == __LITTLE_ENDIAN
#define DAMN_INTEL_BYTE_ORDER 1
#endif
#include "all.h"
#include "ibitstream.h"


#define swap_int32(int_32) (((int_32) << 24) | (((int_32) << 8) & 0x00ff0000) | \
			   (((int_32) >> 8) & 0x0000ff00) | ((int_32) >> 24))


Ibitstream::Ibitstream (int filedescriptor)		// constructor
{
  fd = filedescriptor;
  wordpointer = buffer;
  bitindex = 0;
}


Ibitstream::Ibitstream (const char *filename)		// constructor
{
  if ((fd = open (filename, O_RDONLY, 0)) < 0)
  {
    cerr << "can't open file \"" << filename << "\" for reading!\n";
    exit (1);
  }
  wordpointer = buffer;
  bitindex = 0;
}


Ibitstream::~Ibitstream (void)				// destructor
{
  close (fd);
}


boil Ibitstream::get_header (uint32 *headerstring)
{
  int readvalue;

  if ((readvalue = read (fd, (char *)headerstring, 4)) != 4)
  {
    if (readvalue < 0)
    {
      if (errno == EINTR)
	readvalue = 0;
      else
      {
	perror ("read");
	exit (1);
      }
    }
    else if (!readvalue)
      return False;		// EOF
    // header has not been read completely (this may happen while using a pipe)
    int length = 4 - readvalue;
    char *buffer_pos = (char *)headerstring;
    do
    {
      buffer_pos += readvalue;
      readvalue = read (fd, buffer_pos, length);
      if (readvalue < 0)
      {
	if (errno == EINTR)
	  readvalue = 0;
	else
	{
	  perror ("read");
	  exit (1);
	}
      }
      else if (!readvalue)
	return False;
    }
    while (length -= readvalue);
  }
#ifdef DAMN_INTEL_BYTE_ORDER
  register uint32 header = *headerstring;
  *headerstring = swap_int32 (header);
#endif
  return True;
}


boil Ibitstream::read_frame (uint32 bytesize)
{
  int readvalue;

  if (bytesize > (bufferintsize << 2))
  {
    cerr << "Internal error: framelength > bufferlength?!\n";
    exit (1);
  }

  if ((readvalue = read (fd, (char *)buffer, bytesize)) != bytesize)
  {
    if (readvalue < 0)
    {
      if (errno == EINTR)
	readvalue = 0;
      else
      {
	perror ("read");
	exit (1);
      }
    }
    else if (!readvalue)
      return False;		// EOF
    // frame has not been read completely (this may happen while using a pipe)
    int length = bytesize - readvalue;
    char *buffer_pos = (char *)buffer;
    do
    {
      buffer_pos += readvalue;
      readvalue = read (fd, buffer_pos, length);
      if (readvalue < 0)
      {
	if (errno == EINTR)
	  readvalue = 0;
	else
	{
	  perror ("read");
	  exit (1);
	}
      }
      else if (!readvalue)
	return False;
    }
    while (length -= readvalue);
  }

  wordpointer = buffer;
  bitindex = 0;
  framesize = bytesize;
#ifdef DAMN_INTEL_BYTE_ORDER
  register uint32 *wordp, word;
  for (wordp = buffer + ((bytesize - 1) >> 2); wordp >= buffer; --wordp)
  {
    word = *wordp;
    *wordp = swap_int32 (word);
  }
#endif
  return True;
}


uint32 Ibitstream::get_bits (uint32 number_of_bits)
{
  static uint32 bitmask[17] =
  {
    0,	// dummy
    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF
  };
  uint32 returnvalue;
  uint32 sum = bitindex + number_of_bits;

#ifdef DEBUG
  if (number_of_bits < 1 || number_of_bits > 16)
  {
    cerr << "illegal parameter in Ibitstream::get_bits() !\n";
    exit (1);
  }
#endif

  if (sum <= 32)
  {
    // all bits contained in *wordpointer
    returnvalue = (*wordpointer >> (32 - sum)) & bitmask[number_of_bits];
    if ((bitindex += number_of_bits) == 32)
    {
      bitindex = 0;
      if ((char *)++wordpointer > (char *)buffer + framesize)
      {
	cerr << "Ibitstream::get_bits(): no more bits in buffer!\n";
	exit (1);
      }
    }
    return returnvalue;
  }

  // bits are in *wordpointer and *(wordpointer + 1)
  // bitindex has to be > 16
#ifndef DAMN_INTEL_BYTE_ORDER
  *(int16 *)&returnvalue = *((int16 *)wordpointer + 1);
  if ((char *)++wordpointer > (char *)buffer + framesize)
  {
    cerr << "Ibitstream::get_bits(): no more bits in buffer!\n";
    exit (1);
  }
  *((int16 *)&returnvalue + 1) = *(int16 *)wordpointer;
#else
  *((int16 *)&returnvalue + 1) = *(int16 *)wordpointer;
  if ((char *)++wordpointer > (char *)buffer + framesize)
  {
    cerr << "Ibitstream::get_bits(): no more bits in buffer!\n";
    exit (1);
  }
  *(int16 *)&returnvalue = *((int16 *)wordpointer + 1);
#endif
  returnvalue >>= 48 - sum;	// returnvalue >>= 16 - (number_of_bits - (32 - bitindex))
  returnvalue &= bitmask[number_of_bits];
  bitindex = sum - 32;
  return returnvalue;
}