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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2000-2009 Josh Coalson
* Copyright (C) 2011-2023 Xiph.Org Foundation
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "FLAC/assert.h"
#include "share/compat.h"
#include "private/bitreader.h" /* from the libFLAC private include area */
#include "bitreader.h"
#include <stdio.h>
#include <string.h> /* for memcpy() */
/*
* WATCHOUT! Since FLAC__BitReader is a private structure, we use a copy of
* the definition here to get at the internals. Make sure this is kept up
* to date with what is in ../libFLAC/bitreader.c
*/
#if (ENABLE_64_BIT_WORDS == 0)
typedef FLAC__uint32 brword;
#define FLAC__BYTES_PER_WORD 4
#define FLAC__BITS_PER_WORD 32
#else
typedef FLAC__uint64 brword;
#define FLAC__BYTES_PER_WORD 8
#define FLAC__BITS_PER_WORD 64
#endif
struct FLAC__BitReader {
/* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
/* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
brword *buffer;
uint32_t capacity; /* in words */
uint32_t words; /* # of completed words in buffer */
uint32_t bytes; /* # of bytes in incomplete word at buffer[words] */
uint32_t consumed_words; /* #words ... */
uint32_t consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
uint32_t read_crc16; /* the running frame CRC */
uint32_t crc16_offset; /* the number of words in the current buffer that should not be CRC'd */
uint32_t crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
FLAC__bool read_limit_set; /* whether reads are limited */
uint32_t read_limit; /* the remaining size of what can be read */
uint32_t last_seen_framesync; /* the location of the last seen framesync, if it is in the buffer, in bits from front of buffer */
FLAC__BitReaderReadCallback read_callback;
void *client_data;
};
static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data);
static void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
{
uint32_t i, j;
if(br == 0) {
fprintf(out, "bitreader is NULL\n");
}
else {
fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
for(i = 0; i < br->words; i++) {
fprintf(out, "%08X: ", i);
for(j = 0; j < FLAC__BITS_PER_WORD; j++)
if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
fprintf(out, ".");
else
fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
fprintf(out, "\n");
}
if(br->bytes > 0) {
fprintf(out, "%08X: ", i);
for(j = 0; j < br->bytes*8; j++)
if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
fprintf(out, ".");
else
fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (br->bytes*8-j-1)) ? 1:0);
fprintf(out, "\n");
}
}
}
FLAC__bool test_bitreader(void)
{
FLAC__BitReader *br;
FLAC__bool ok;
uint32_t i;
uint32_t words, bits; /* what we think br->consumed_words and br->consumed_bits should be */
FLAC__uint16 crc,expected_crcs[4] = { 0x5e4c, 0x7f6b, 0x2272, 0x42bf };
FLAC__byte data[32];
FLAC__uint32 val_uint32;
FLAC__uint64 val_uint64;
for (i = 0; i < 32; i++)
data[i] = i * 8 + 7;
printf("\n+++ libFLAC unit test: bitreader\n\n");
/*
* test new -> delete
*/
printf("testing new... ");
br = FLAC__bitreader_new();
if(0 == br) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitreader_delete(br);
printf("OK\n");
/*
* test new -> init -> delete
*/
printf("testing new... ");
br = FLAC__bitreader_new();
if(0 == br) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
if(!FLAC__bitreader_init(br, read_callback, data)) {
printf("FAILED, returned false\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitreader_delete(br);
printf("OK\n");
/*
* test new -> init -> clear -> delete
*/
printf("testing new... ");
br = FLAC__bitreader_new();
if(0 == br) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
if(!FLAC__bitreader_init(br, read_callback, data)) {
printf("FAILED, returned false\n");
return false;
}
printf("OK\n");
printf("testing clear... ");
if(!FLAC__bitreader_clear(br)) {
printf("FAILED, returned false\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitreader_delete(br);
printf("OK\n");
/*
* test normal usage
*/
printf("testing new... ");
br = FLAC__bitreader_new();
if(0 == br) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
if(!FLAC__bitreader_init(br, read_callback, data)) {
printf("FAILED, returned false\n");
return false;
}
printf("OK\n");
printf("testing clear... ");
if(!FLAC__bitreader_clear(br)) {
printf("FAILED, returned false\n");
return false;
}
printf("OK\n");
words = bits = 0;
printf("capacity = %u\n", br->capacity);
printf("testing raw reads... ");
ok =
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 10) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
FLAC__bitreader_read_raw_uint64(br, &val_uint64, 64) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 12)
;
if(!ok) {
printf("FAILED\n");
FLAC__bitreader_dump(br, stdout);
return false;
}
/* we read 152 bits (=19 bytes) from the bitreader */
words = 152 / FLAC__BITS_PER_WORD;
bits = 152 - words*FLAC__BITS_PER_WORD;
if(br->consumed_words != words) {
printf("FAILED word count %u != %u\n", br->consumed_words, words);
FLAC__bitreader_dump(br, stdout);
return false;
}
if(br->consumed_bits != bits) {
printf("FAILED bit count %u != %u\n", br->consumed_bits, bits);
FLAC__bitreader_dump(br, stdout);
return false;
}
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != expected_crcs[0]) {
printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[0]);
FLAC__bitreader_dump(br, stdout);
return false;
}
printf("OK\n");
FLAC__bitreader_dump(br, stdout);
printf("testing CRC reset... ");
FLAC__bitreader_clear(br);
FLAC__bitreader_reset_read_crc16(br, 0xFFFF);
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != 0xFFFF) {
printf("FAILED reported CRC 0x%04x does not match expected 0xFFFF\n", crc);
FLAC__bitreader_dump(br, stdout);
return false;
}
FLAC__bitreader_reset_read_crc16(br, 0);
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != 0) {
printf("FAILED reported CRC 0x%04x does not match expected 0x0000\n", crc);
FLAC__bitreader_dump(br, stdout);
return false;
}
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 16);
FLAC__bitreader_reset_read_crc16(br, 0);
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32);
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != expected_crcs[1]) {
printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[1]);
FLAC__bitreader_dump(br, stdout);
return false;
}
printf("OK\n");
printf("testing unaligned < 32 bit reads... ");
FLAC__bitreader_clear(br);
FLAC__bitreader_skip_bits_no_crc(br, 8);
FLAC__bitreader_reset_read_crc16(br, 0);
ok =
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8)
;
if(!ok) {
printf("FAILED\n");
FLAC__bitreader_dump(br, stdout);
return false;
}
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != expected_crcs[2]) {
printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[2]);
FLAC__bitreader_dump(br, stdout);
return false;
}
printf("OK\n");
FLAC__bitreader_dump(br, stdout);
printf("testing unaligned < 64 bit reads... ");
FLAC__bitreader_clear(br);
FLAC__bitreader_skip_bits_no_crc(br, 8);
FLAC__bitreader_reset_read_crc16(br, 0);
ok =
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32)
;
if(!ok) {
printf("FAILED\n");
FLAC__bitreader_dump(br, stdout);
return false;
}
crc = FLAC__bitreader_get_read_crc16(br);
if(crc != expected_crcs[3]) {
printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[3]);
FLAC__bitreader_dump(br, stdout);
return false;
}
printf("OK\n");
FLAC__bitreader_dump(br, stdout);
printf("testing free... ");
FLAC__bitreader_free(br);
printf("OK\n");
printf("testing delete... ");
FLAC__bitreader_delete(br);
printf("OK\n");
printf("\nPASSED!\n");
return true;
}
/*----------------------------------------------------------------------------*/
static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data)
{
if (*bytes > 32)
*bytes = 32;
memcpy(buffer, data, *bytes);
return true;
}
|