7 #define BUF_SIZE 1024 * 3
19 void (*out)(unsigned char *dest, unsigned int val, int size);
22 static void out_lsb(unsigned char *dest, unsigned int val, int size)
26 for (i = 0; i < size; i++)
27 dest[i] = val >> (8 * i);
30 static void out_msb(unsigned char *dest, unsigned int val, int size)
34 for (i = 0; i < size; i++)
35 dest[i] = val >> (8 * (size - 1 - i));
38 static int update_state(struct aec_stream *strm, struct test_state *state)
40 if (strm->bit_per_sample > 16) {
43 if (strm->bit_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
44 state->byte_per_sample = 3;
46 state->byte_per_sample = 4;
49 else if (strm->bit_per_sample > 8) {
51 state->byte_per_sample = 2;
54 state->byte_per_sample = 1;
57 if (strm->flags & AEC_DATA_MSB)
62 if (strm->flags & AEC_DATA_SIGNED) {
63 state->xmin = -(1ULL << (strm->bit_per_sample - 1));
64 state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
67 state->xmax = (1ULL << strm->bit_per_sample) - 1;
73 int encode_decode(struct aec_stream *strm, struct test_state *state)
77 strm->avail_in = state->buf_len;
78 strm->avail_out = state->cbuf_len;
79 strm->next_in = state->ubuf;
80 strm->next_out = state->cbuf;
82 status = aec_encode_init(strm);
83 if (status != AEC_OK) {
84 printf("Init failed.\n");
88 status = aec_encode(strm, AEC_FLUSH);
89 if (status != AEC_OK) {
90 printf("Encode failed.\n");
96 strm->avail_in = strm->total_out;
97 strm->avail_out = state->buf_len;
98 strm->next_in = state->cbuf;
99 strm->next_out = state->obuf;
100 to = strm->total_out;
102 status = aec_decode_init(strm);
103 if (status != AEC_OK) {
104 printf("Init failed.\n");
108 status = aec_decode(strm, AEC_FLUSH);
109 if (status != AEC_OK) {
110 printf("Decode failed.\n");
114 if (memcmp(state->ubuf, state->obuf, state->buf_len)) {
115 printf("FAIL: Uncompressed output differs from input.\n");
117 printf("\nuncompressed buf");
118 for (i = 0; i < 80; i++) {
121 printf("%02x ", state->ubuf[i]);
123 printf("\n\ncompressed buf len %i", to);
124 for (i = 0; i < 80; i++) {
127 printf("%02x ", state->cbuf[i]);
129 printf("\n\ndecompressed buf");
130 for (i = 0; i < 80; i++) {
133 printf("%02x ", state->obuf[i]);
138 aec_decode_end(strm);
142 int check_block_sizes(struct aec_stream *strm,
143 struct test_state *state,
148 for (bs = 8; bs <= 64; bs *= 2) {
149 strm->block_size = bs;
150 strm->rsi = state->buf_len / (bs * state->byte_per_sample);
152 status = encode_decode(strm, state);
156 if ((state->cbuf[0] >> (8 - state->id_len)) != id) {
157 printf("FAIL: Unexpected block of size %i created %x.\n",
158 bs, state->cbuf[0] >> (8 - state->id_len));
165 int check_zero(struct aec_stream *strm, struct test_state *state)
169 memset(state->ubuf, 0x55, state->buf_len);
171 printf("Checking zero blocks ... ");
172 status = check_block_sizes(strm, state, 0);
180 int check_splitting(struct aec_stream *strm, struct test_state *state, int k)
185 size = state->byte_per_sample;
187 for (tmp = state->ubuf;
188 tmp < state->ubuf + state->buf_len;
189 tmp += 4 * state->byte_per_sample) {
190 state->out(tmp, state->xmin + (1ULL << (k - 1)) - 1, size);
191 state->out(tmp + size, state->xmin, size);
192 state->out(tmp + 2 * size, state->xmin + (1ULL << (k + 1)) - 1, size);
193 state->out(tmp + 3 * size, state->xmin, size);
196 printf("Checking splitting with k=%i ... ", k);
197 status = check_block_sizes(strm, state, k + 1);
205 int check_uncompressed(struct aec_stream *strm, struct test_state *state)
210 size = state->byte_per_sample;
212 for (tmp = state->ubuf;
213 tmp < state->ubuf + state->buf_len;
214 tmp += 2 * state->byte_per_sample) {
215 state->out(tmp, state->xmax, size);
216 state->out(tmp + size, state->xmin, size);
219 printf("Checking uncompressed ... ");
220 status = check_block_sizes(strm, state, (1ULL << state->id_len) - 1);
228 int check_fs(struct aec_stream *strm, struct test_state *state)
233 size = state->byte_per_sample;
235 for (tmp = state->ubuf;
236 tmp < state->ubuf + state->buf_len;
237 tmp += 2 * state->byte_per_sample) {
238 state->out(tmp, state->xmin + 1, size);
239 state->out(tmp + size, state->xmin, size);
242 printf("Checking FS ... ");
243 status = check_block_sizes(strm, state, 1);
251 int check_se(struct aec_stream *strm, struct test_state *state)
256 size = state->byte_per_sample;
258 for (tmp = state->ubuf;
259 tmp < state->ubuf + state->buf_len;
261 state->out(tmp, 0, size);
262 state->out(tmp + size, 0, size);
263 state->out(tmp + 2 * size, 0, size);
264 state->out(tmp + 3 * size, 0, size);
265 state->out(tmp + 4 * size, 0, size);
266 state->out(tmp + 5 * size, 0, size);
267 state->out(tmp + 6 * size, 0, size);
268 state->out(tmp + 7 * size, 1, size);
271 printf("Checking Second Extension ... ");
272 status = check_block_sizes(strm, state, 0);
280 int check_bps(struct aec_stream *strm, struct test_state *state)
284 for (bps = 8; bps <= 32; bps += 8) {
285 strm->bit_per_sample = bps;
287 strm->flags |= AEC_DATA_3BYTE;
289 strm->flags &= ~AEC_DATA_3BYTE;
291 update_state(strm, state);
293 status = check_zero(strm, state);
297 status = check_se(strm, state);
301 status = check_uncompressed(strm, state);
305 status = check_fs(strm, state);
309 for (k = 1; k < bps - 2; k++) {
310 status = check_splitting(strm, state, k);
314 printf("All checks with %i bit per sample passed.\n\n", bps);
322 struct aec_stream strm;
323 struct test_state state;
325 state.buf_len = BUF_SIZE;
326 state.cbuf_len = 2 * BUF_SIZE;
328 state.ubuf = (unsigned char *)malloc(state.buf_len);
329 state.cbuf = (unsigned char *)malloc(state.cbuf_len);
330 state.obuf = (unsigned char *)malloc(state.buf_len);
332 if (!state.ubuf || !state.cbuf || !state.obuf) {
333 printf("Not enough memory.\n");
337 strm.flags = AEC_DATA_PREPROCESS;
339 printf("----------------------------\n");
340 printf("Checking LSB first, unsigned\n");
341 printf("----------------------------\n");
342 status = check_bps(&strm, &state);
346 printf("--------------------------\n");
347 printf("Checking LSB first, signed\n");
348 printf("--------------------------\n");
349 strm.flags |= AEC_DATA_SIGNED;
351 status = check_bps(&strm, &state);
355 strm.flags &= ~AEC_DATA_SIGNED;
356 strm.flags |= AEC_DATA_MSB;
358 printf("----------------------------\n");
359 printf("Checking MSB first, unsigned\n");
360 printf("----------------------------\n");
361 status = check_bps(&strm, &state);
365 printf("--------------------------\n");
366 printf("Checking MSB first, signed\n");
367 printf("--------------------------\n");
368 strm.flags |= AEC_DATA_SIGNED;
370 status = check_bps(&strm, &state);