Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "types.h"
63
#include "enc_types.h"
7 7u83 64
#include "bitstream.h"
2 7u83 65
#include "file.h"
66
#include "utility.h"
67
 
68
 
69
/*
70
    BITMASKS
71
 
72
    The nth value in this array can be used to extract the bottom n
73
    bits of a value.
74
*/
75
 
7 7u83 76
static unsigned long mask[] = {
2 7u83 77
    0, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
7 7u83 78
};
2 7u83 79
 
80
 
81
/*
82
    SIZE OF A STANDARD BITSTREAM
83
 
84
    Each bitstream contains BITSTREAM_SIZE bytes.  It is worth garbage
85
    collecting if it has CRITICAL_SIZE bytes free when it is closed.
86
*/
87
 
88
#define BITSTREAM_SIZE		100
89
#define CRITICAL_SIZE		50
90
 
91
 
92
/*
93
    LIST OF FREE BITSTREAMS
94
 
95
    Bitstreams are allocated from this list.
96
*/
97
 
7 7u83 98
static bitstream *free_bitstreams = null;
2 7u83 99
 
100
 
101
/*
102
    CREATE A NEW BITSTREAM
103
 
104
    A new bitstream is allocated and initialized.
105
*/
106
 
7 7u83 107
bitstream *
108
new_bitstream(void)
2 7u83 109
{
7 7u83 110
    unsigned i;
111
    bitstream *p;
112
    if (free_bitstreams == null) {
113
	p = alloc_nof(bitstream, 1);
114
	p->length = BITSTREAM_SIZE;
115
	p->source = alloc_nof(byte, BITSTREAM_SIZE + 10);
2 7u83 116
    } else {
7 7u83 117
	p = free_bitstreams;
118
	free_bitstreams = p->next;
2 7u83 119
    }
7 7u83 120
    for (i = 0; i < p->length; i++)p->source[i] = 0;
121
    p->bytes = 0;
122
    p->bits = 0;
123
    p->next = null;
124
    p->end = p;
125
    return(p);
2 7u83 126
}
127
 
128
 
129
#if 0
130
 
131
/*
132
    CLOSE OFF A BITSTREAM
133
 
134
    The bitstream p is closed off and any free space is garbage collected.
135
    (Not currently used.)
136
*/
137
 
138
static void close_bitstream
7 7u83 139
(bitstream *p)
2 7u83 140
{
7 7u83 141
    bitstream *q;
142
    int used = p->end->bytes + 4;
143
    int left = p->end->length - used;
144
    if (left < CRITICAL_SIZE) return;
145
    q = alloc_nof(bitstream, 1);
146
    q->length = left;
147
    q->source = p->end->source + used;
148
    q->next = free_bitstreams;
149
    free_bitstreams = q;
150
    p->end->length = used;
151
    return;
2 7u83 152
}
153
 
154
#endif
155
 
156
 
157
/*
158
    PRINT A BITSTREAM
159
 
160
    The bitstream p is dumped to the output file.
161
*/
162
 
7 7u83 163
void
164
print_bitstream(bitstream *p)
2 7u83 165
{
7 7u83 166
    unsigned r = 0;
167
    unsigned long buff = 0;
168
    for (; p; p = p->next) {
169
	unsigned i;
170
	for (i = 0; i < p->bytes; i++) {
171
	    byte b = p->source[i];
172
	    if (r == 0) {
173
		IGNORE fputc((int)b, output);
2 7u83 174
	    } else {
7 7u83 175
		buff = (buff << BYTESIZE) | ((unsigned long)b);
176
		IGNORE fputc((int)((buff >> r) & 0xff), output);
177
		buff &= mask[r];
2 7u83 178
	    }
179
	}
7 7u83 180
	if (p->bits) {
181
	    byte b = p->source[p->bytes];
182
	    b = (byte)((unsigned)b >> (BYTESIZE - p->bits));
183
	    buff = (buff << p->bits) | ((unsigned long)b);
184
	    r += p->bits;
185
	    if (r >= BYTESIZE) {
186
		r -= BYTESIZE;
187
		IGNORE fputc((int)((buff >> r) & 0xff), output);
188
		buff &= mask[r];
2 7u83 189
	    }
190
	}
191
    }
7 7u83 192
    if (r) {
193
	buff <<= (BYTESIZE - r);
194
	IGNORE fputc((int)buff, output);
2 7u83 195
    }
7 7u83 196
    return;
2 7u83 197
}
198
 
199
 
200
/*
201
    FIND THE LENGTH OF A BITSTREAM
202
 
203
    The length of the bitstream p (in bits) is returned.
204
*/
205
 
7 7u83 206
long
207
bitstream_length(bitstream *p)
2 7u83 208
{
7 7u83 209
    unsigned n = 0;
210
    for (; p; p = p->next)n += (BYTESIZE * p->bytes) + p->bits;
211
    return((long)n);
2 7u83 212
}
213
 
214
 
215
/*
216
    JOIN TWO BITSTREAMS
217
 
218
    The bitstream q is appended to p.
219
*/
220
 
7 7u83 221
void
222
join_bitstreams(bitstream *p, bitstream *q)
2 7u83 223
{
224
#if 0
7 7u83 225
    close_bitstream(p);
2 7u83 226
#endif
7 7u83 227
    p->end->next = q;
228
    p->end = q->end;
229
    return;
2 7u83 230
}
231
 
232
 
233
/*
234
    ADD A NUMBER OF BITS TO THE END OF A BITSTREAM
235
 
236
    n bits of value v are added to the end of the bitstream p.
237
*/
238
 
7 7u83 239
void
240
enc_bits(bitstream *p, int n, long v)
2 7u83 241
{
7 7u83 242
    byte *t;
243
    bitstream *q = p->end;
244
    unsigned m = (unsigned)n;
245
    unsigned left = BYTESIZE - q->bits;
246
    unsigned long w = (unsigned long)v;
247
    if (left == 0) {
248
	left = BYTESIZE;
249
	q->bits = 0;
250
	q->bytes++;
251
	if (q->bytes >= q->length) {
252
	    q->next = new_bitstream();
253
	    q = q->next;
254
	    p->end = q;
2 7u83 255
	}
7 7u83 256
	q->source[q->bytes] = 0;
2 7u83 257
    }
7 7u83 258
    if (m > left) {
259
	enc_bits(p,(int)(m - left), (long)(w >> left));
260
	enc_bits(p,(int)left,(long)(w & mask[left]));
261
	return;
2 7u83 262
    }
7 7u83 263
    w <<= (left - m);
264
    t = q->source + q->bytes;
265
    *t = (byte)(*t | (byte)w);
266
    q->bits += m;
267
    return;
2 7u83 268
}
269
 
270
 
271
/*
272
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
273
 
274
    The bitstream p is aligned to a byte boundary.
275
*/
276
 
7 7u83 277
void
278
align_bitstream(bitstream *p)
2 7u83 279
{
7 7u83 280
    int bit = (int)(bitstream_length(p)% BYTESIZE);
281
    if (bit)enc_bits(p, BYTESIZE - bit,(long)0);
282
    return;
2 7u83 283
}