4d89fdf17b5e72489e291609a4a07ce8a18e743a
[platform/upstream/libaec.git] / tests / check_aec.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "check_aec.h"
5
6 static void out_lsb(unsigned char *dest, unsigned long long int val, int size)
7 {
8     int i;
9
10     for (i = 0; i < size; i++)
11         dest[i] = (unsigned char)(val >> (8 * i));
12 }
13
14 static void out_msb(unsigned char *dest, unsigned long long int val, int size)
15 {
16     int i;
17
18     for (i = 0; i < size; i++)
19         dest[i] = (unsigned char)(val >> (8 * (size - 1 - i)));
20 }
21
22 int update_state(struct test_state *state)
23 {
24     struct aec_stream *strm = state->strm;
25
26     if (strm->bits_per_sample > 16) {
27         state->id_len = 5;
28
29         if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
30             state->bytes_per_sample = 3;
31         } else {
32             state->bytes_per_sample = 4;
33         }
34     }
35     else if (strm->bits_per_sample > 8) {
36         state->id_len = 4;
37         state->bytes_per_sample = 2;
38     } else {
39         state->id_len = 3;
40         state->bytes_per_sample = 1;
41     }
42
43     if (strm->flags & AEC_DATA_MSB)
44         state->out = out_msb;
45     else
46         state->out = out_lsb;
47
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;
51     } else {
52         state->xmin = 0;
53         state->xmax = (1ULL << strm->bits_per_sample) - 1;
54     }
55
56     return 0;
57 }
58
59 int encode_decode_small(struct test_state *state)
60 {
61     int status, i;
62     size_t compressed_size;
63     size_t n_in, avail_in, avail_out, total_out;
64     struct aec_stream *strm = state->strm;
65
66     status = aec_encode_init(strm);
67     if (status != AEC_OK) {
68         printf("Init failed.\n");
69         return 99;
70     }
71
72     n_in = 0;
73     avail_in = 1;
74     avail_out = 1;
75     total_out = 0;
76     strm->next_in = state->ubuf;
77     strm->avail_in = state->bytes_per_sample;
78     strm->avail_out = 1;
79     strm->next_out = state->cbuf;
80
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;
87             } else {
88                 avail_in = 0;
89             }
90         }
91
92         status = aec_encode(strm, AEC_NO_FLUSH);
93         if (status != AEC_OK) {
94             printf("Decode failed.\n");
95             return 99;
96         }
97
98         if (strm->total_out - total_out > 0
99             && total_out < state->cbuf_len) {
100             total_out = strm->total_out;
101             strm->avail_out = 1;
102             strm->next_out = state->cbuf + total_out;
103             avail_out = 1;
104         } else {
105             avail_out = 0;
106         }
107     }
108
109     status = aec_encode(strm, AEC_FLUSH);
110     if (status != AEC_OK) {
111         printf("Encode failed.\n");
112         return 99;
113     }
114
115     aec_encode_end(strm);
116
117     compressed_size = strm->total_out;
118
119     strm->avail_in = 1;
120     strm->next_in = state->cbuf;
121
122     strm->avail_out = state->bytes_per_sample;
123     strm->next_out = state->obuf;
124
125     status = aec_decode_init(strm);
126     if (status != AEC_OK) {
127         printf("Init failed.\n");
128         return 99;
129     }
130
131     n_in = 0;
132     avail_in = 1;
133     avail_out = 1;
134     total_out = 0;
135     strm->next_in = state->cbuf;
136     strm->avail_in = 1;
137     strm->avail_out = state->bytes_per_sample;
138     strm->next_out = state->obuf;
139
140     while ((avail_in || avail_out) && total_out < state->buf_len) {
141         if (strm->avail_in == 0 && avail_in) {
142             n_in++;
143             if (n_in < compressed_size) {
144                 strm->avail_in = 1;
145                 strm->next_in = state->cbuf + n_in;
146             } else {
147                 avail_in = 0;
148             }
149         }
150
151         status = aec_decode(strm, AEC_NO_FLUSH);
152         if (status != AEC_OK) {
153             printf("Decode failed.\n");
154             return 99;
155         }
156
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;
162             avail_out = 1;
163         } else {
164             avail_out = 0;
165         }
166     }
167
168     status = aec_decode(strm, AEC_FLUSH);
169     if (status != AEC_OK) {
170         printf("Decode failed.\n");
171         return 99;
172     }
173
174     if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
175         printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
176
177         printf("\nuncompressed buf");
178         for (i = 0; i < 80; i++) {
179             if (i % 8 == 0)
180                 printf("\n");
181             printf("%02x ", state->ubuf[i]);
182         }
183         printf("\n\ncompressed buf len %li", compressed_size);
184         for (i = 0; i < 80; i++) {
185             if (i % 8 == 0)
186                 printf("\n");
187             printf("%02x ", state->cbuf[i]);
188         }
189         printf("\n\ndecompressed buf");
190         for (i = 0; i < 80; i++) {
191             if (i % 8 == 0)
192                 printf("\n%04i ", i);
193             printf("%02x ", state->obuf[i]);
194         }
195         printf("\n");
196         return 99;
197     }
198     aec_decode_end(strm);
199     return 0;
200 }
201
202 int encode_decode_large(struct test_state *state)
203 {
204     int status, i;
205     size_t to;
206     struct aec_stream *strm = state->strm;
207
208     strm->avail_in = state->ibuf_len;
209     strm->avail_out = state->cbuf_len;
210     strm->next_in = state->ubuf;
211     strm->next_out = state->cbuf;
212
213     status = aec_encode_init(strm);
214     if (status != AEC_OK) {
215         printf("Init failed.\n");
216         return 99;
217     }
218
219     status = aec_encode(strm, AEC_FLUSH);
220     if (status != AEC_OK) {
221         printf("Encode failed.\n");
222         return 99;
223     }
224
225     aec_encode_end(strm);
226
227     strm->avail_in = strm->total_out;
228     strm->avail_out = state->buf_len;
229     strm->next_in = state->cbuf;
230     strm->next_out = state->obuf;
231     to = strm->total_out;
232
233     status = aec_decode_init(strm);
234     if (status != AEC_OK) {
235         printf("Init failed.\n");
236         return 99;
237     }
238
239     status = aec_decode(strm, AEC_FLUSH);
240     if (status != AEC_OK) {
241         printf("Decode failed.\n");
242         return 99;
243     }
244
245     if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
246         printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
247
248         printf("\nuncompressed buf");
249         for (i = 0; i < 80; i++) {
250             if (i % 8 == 0)
251                 printf("\n");
252             printf("%02x ", state->ubuf[i]);
253         }
254         printf("\n\ncompressed buf len %li", to);
255         for (i = 0; i < 80; i++) {
256             if (i % 8 == 0)
257                 printf("\n");
258             printf("%02x ", state->cbuf[i]);
259         }
260         printf("\n\ndecompressed buf");
261         for (i = 0; i < 80; i++) {
262             if (i % 8 == 0)
263                 printf("\n");
264             printf("%02x ", state->obuf[i]);
265         }
266         printf("\n");
267         return 99;
268     }
269     aec_decode_end(strm);
270     return 0;
271 }