6 static void out_lsb(unsigned char *dest, unsigned long long int val, int size)
10 for (i = 0; i < size; i++)
11 dest[i] = (unsigned char)(val >> (8 * i));
14 static void out_msb(unsigned char *dest, unsigned long long int val, int size)
18 for (i = 0; i < size; i++)
19 dest[i] = (unsigned char)(val >> (8 * (size - 1 - i)));
22 int update_state(struct test_state *state)
24 struct aec_stream *strm = state->strm;
26 if (strm->bits_per_sample > 16) {
29 if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
30 state->bytes_per_sample = 3;
32 state->bytes_per_sample = 4;
35 else if (strm->bits_per_sample > 8) {
37 state->bytes_per_sample = 2;
40 state->bytes_per_sample = 1;
43 if (strm->flags & AEC_DATA_MSB)
48 if (strm->flags & AEC_DATA_SIGNED) {
49 state->xmin = -(1LL << (strm->bits_per_sample - 1));
50 state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
53 state->xmax = (1ULL << strm->bits_per_sample) - 1;
59 int encode_decode_small(struct test_state *state)
62 size_t compressed_size;
63 size_t n_in, avail_in, avail_out, total_out;
64 struct aec_stream *strm = state->strm;
66 status = aec_encode_init(strm);
67 if (status != AEC_OK) {
68 printf("Init failed.\n");
76 strm->next_in = state->ubuf;
77 strm->avail_in = state->bytes_per_sample;
79 strm->next_out = state->cbuf;
81 while ((avail_in || avail_out) && total_out < state->cbuf_len) {
82 if (strm->avail_in == 0 && avail_in) {
83 n_in += state->bytes_per_sample;
84 if (n_in < state->buf_len) {
85 strm->avail_in = state->bytes_per_sample;
86 strm->next_in = state->ubuf + n_in;
92 status = aec_encode(strm, AEC_NO_FLUSH);
93 if (status != AEC_OK) {
94 printf("Decode failed.\n");
98 if (strm->total_out - total_out > 0
99 && total_out < state->cbuf_len) {
100 total_out = strm->total_out;
102 strm->next_out = state->cbuf + total_out;
109 status = aec_encode(strm, AEC_FLUSH);
110 if (status != AEC_OK) {
111 printf("Encode failed.\n");
115 aec_encode_end(strm);
117 compressed_size = strm->total_out;
120 strm->next_in = state->cbuf;
122 strm->avail_out = state->bytes_per_sample;
123 strm->next_out = state->obuf;
125 status = aec_decode_init(strm);
126 if (status != AEC_OK) {
127 printf("Init failed.\n");
135 strm->next_in = state->cbuf;
137 strm->avail_out = state->bytes_per_sample;
138 strm->next_out = state->obuf;
140 while ((avail_in || avail_out) && total_out < state->buf_len) {
141 if (strm->avail_in == 0 && avail_in) {
143 if (n_in < compressed_size) {
145 strm->next_in = state->cbuf + n_in;
151 status = aec_decode(strm, AEC_NO_FLUSH);
152 if (status != AEC_OK) {
153 printf("Decode failed.\n");
157 if (strm->total_out - total_out > 0
158 && total_out < state->buf_len) {
159 total_out = strm->total_out;
160 strm->avail_out = state->bytes_per_sample;
161 strm->next_out = state->obuf + total_out;
168 status = aec_decode(strm, AEC_FLUSH);
169 if (status != AEC_OK) {
170 printf("Decode failed.\n");
174 if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
175 printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
177 printf("\nuncompressed buf");
178 for (i = 0; i < 80; i++) {
181 printf("%02x ", state->ubuf[i]);
183 printf("\n\ncompressed buf len %li", compressed_size);
184 for (i = 0; i < 80; i++) {
187 printf("%02x ", state->cbuf[i]);
189 printf("\n\ndecompressed buf");
190 for (i = 0; i < 80; i++) {
192 printf("\n%04i ", i);
193 printf("%02x ", state->obuf[i]);
198 aec_decode_end(strm);
202 int encode_decode_large(struct test_state *state)
210 struct aec_stream *strm = state->strm;
212 strm->avail_in = state->ibuf_len;
213 strm->avail_out = state->cbuf_len;
214 strm->next_in = state->ubuf;
215 strm->next_out = state->cbuf;
217 status = aec_encode_init(strm);
218 if (status != AEC_OK) {
219 printf("Init failed.\n");
223 snprintf(fbase, sizeof(fbase), "BPS%02iID%iBS%02iRSI%04iFLG%04i",
224 strm->bits_per_sample,
229 snprintf(fname, sizeof(fname), "%s.dat", fbase);
230 if ((fp = fopen(fname, "wb")) == NULL) {
231 fprintf(stderr, "ERROR: cannot open dump file %s\n", fname);
234 fputc(strm->bits_per_sample, fp);
235 bflags = strm->block_size >> 8;
236 if (strm->flags | AEC_DATA_MSB)
238 if (strm->flags | AEC_DATA_SIGNED)
240 if (strm->flags | AEC_DATA_3BYTE)
242 bflags |= 0x20; /* encode */
244 fwrite(strm->next_in, strm->avail_in, 1, fp);
248 status = aec_encode(strm, AEC_FLUSH);
249 if (status != AEC_OK) {
250 printf("Encode failed.\n");
254 aec_encode_end(strm);
257 snprintf(fname, sizeof(fname), "%s.rz", fbase);
258 if ((fp = fopen(fname, "wb")) == NULL) {
259 fprintf(stderr, "ERROR: cannot open dump file %s\n", fname);
262 fputc(strm->bits_per_sample, fp);
265 fwrite(state->cbuf, strm->total_out, 1, fp);
269 strm->avail_in = strm->total_out;
270 strm->avail_out = state->buf_len;
271 strm->next_in = state->cbuf;
272 strm->next_out = state->obuf;
273 to = strm->total_out;
275 status = aec_decode_init(strm);
276 if (status != AEC_OK) {
277 printf("Init failed.\n");
281 status = aec_decode(strm, AEC_FLUSH);
282 if (status != AEC_OK) {
283 printf("Decode failed.\n");
287 if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
288 printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
290 printf("\nuncompressed buf");
291 for (i = 0; i < 80; i++) {
294 printf("%02x ", state->ubuf[i]);
296 printf("\n\ncompressed buf len %li", to);
297 for (i = 0; i < 80; i++) {
300 printf("%02x ", state->cbuf[i]);
302 printf("\n\ndecompressed buf");
303 for (i = 0; i < 80; i++) {
306 printf("%02x ", state->obuf[i]);
311 aec_decode_end(strm);