Remove dependency on stdint in API
[platform/upstream/libaec.git] / src / encode_accessors.c
1 #include <config.h>
2
3 #if HAVE_STDINT_H
4 # include <stdint.h>
5 #endif
6
7 #include <string.h>
8 #include "libaec.h"
9 #include "encode.h"
10 #include "encode_accessors.h"
11
12 uint32_t get_8(struct aec_stream *strm)
13 {
14     strm->avail_in--;
15     strm->total_in++;
16     return *strm->next_in++;
17 }
18
19 uint32_t get_lsb_24(struct aec_stream *strm)
20 {
21     uint32_t data;
22
23     data = ((uint32_t)strm->next_in[2] << 16)
24         | ((uint32_t)strm->next_in[1] << 8)
25         | (uint32_t)strm->next_in[0];
26
27     strm->next_in += 3;
28     strm->total_in += 3;
29     strm->avail_in -= 3;
30     return data;
31 }
32
33 uint32_t get_msb_24(struct aec_stream *strm)
34 {
35     uint32_t data;
36
37     data = ((uint32_t)strm->next_in[0] << 16)
38         | ((uint32_t)strm->next_in[1] << 8)
39         | (uint32_t)strm->next_in[2];
40
41     strm->next_in += 3;
42     strm->total_in += 3;
43     strm->avail_in -= 3;
44     return data;
45 }
46
47 #ifdef WORDS_BIGENDIAN
48
49 uint32_t get_lsb_16(struct aec_stream *strm)
50 {
51     uint32_t data;
52
53     data = ((uint32_t)strm->next_in[1] << 8)
54         | (uint32_t)strm->next_in[0];
55
56     strm->next_in += 2;
57     strm->total_in += 2;
58     strm->avail_in -= 2;
59     return data;
60 }
61
62 uint32_t get_msb_16(struct aec_stream *strm)
63 {
64     uint32_t data;
65
66     data = *(uint16_t *)strm->next_in;
67     strm->next_in += 2;
68     strm->total_in += 2;
69     strm->avail_in -= 2;
70     return data;
71 }
72
73 uint32_t get_lsb_32(struct aec_stream *strm)
74 {
75     uint32_t data;
76
77     data = ((uint32_t)strm->next_in[3] << 24)
78         | ((uint32_t)strm->next_in[2] << 16)
79         | ((uint32_t)strm->next_in[1] << 8)
80         | (uint32_t)strm->next_in[0];
81
82     strm->next_in += 4;
83     strm->total_in += 4;
84     strm->avail_in -= 4;
85     return data;
86 }
87
88 uint32_t get_msb_32(struct aec_stream *strm)
89 {
90     uint32_t data;
91
92     data = *(uint32_t *)strm->next_in;
93     strm->next_in += 4;
94     strm->total_in += 4;
95     strm->avail_in -= 4;
96     return data;
97 }
98 #else /* not WORDS_BIGENDIAN */
99
100 uint32_t get_lsb_16(struct aec_stream *strm)
101 {
102     uint32_t data;
103
104     data = *(uint16_t *)strm->next_in;
105     strm->next_in += 2;
106     strm->total_in += 2;
107     strm->avail_in -= 2;
108     return data;
109 }
110
111 uint32_t get_msb_16(struct aec_stream *strm)
112 {
113     uint32_t data;
114
115     data = ((uint32_t)strm->next_in[0] << 8)
116         | (uint32_t)strm->next_in[1];
117
118     strm->next_in += 2;
119     strm->total_in += 2;
120     strm->avail_in -= 2;
121     return data;
122 }
123
124 uint32_t get_lsb_32(struct aec_stream *strm)
125 {
126     uint32_t data;
127
128     data = *(uint32_t *)strm->next_in;
129     strm->next_in += 4;
130     strm->total_in += 4;
131     strm->avail_in -= 4;
132     return data;
133 }
134
135 uint32_t get_msb_32(struct aec_stream *strm)
136 {
137     uint32_t data;
138
139     data = ((uint32_t)strm->next_in[0] << 24)
140         | ((uint32_t)strm->next_in[1] << 16)
141         | ((uint32_t)strm->next_in[2] << 8)
142         | (uint32_t)strm->next_in[3];
143
144     strm->next_in += 4;
145     strm->total_in += 4;
146     strm->avail_in -= 4;
147     return data;
148 }
149 #endif /* not WORDS_BIGENDIAN */
150
151 #define GET_BLOCK_8(BS)                                              \
152     static void get_block_8_bs_##BS(struct aec_stream *strm)         \
153     {                                                                \
154         int i, j;                                                    \
155         uint32_t *block = strm->state->block_buf;                    \
156                                                                      \
157         for (i = 0; i < strm->rsi; i++)                              \
158             for (j = 0; j < BS; j++)                                 \
159                 block[i * BS + j] = strm->next_in[i * BS + j];       \
160                                                                      \
161         strm->next_in += BS * strm->rsi;                             \
162         strm->total_in += BS * strm->rsi;                            \
163         strm->avail_in -= BS * strm->rsi;                            \
164     }
165
166 #define GET_BLOCK_NATIVE_16(BS)                                      \
167     static void get_block_native_16_bs_##BS(struct aec_stream *strm) \
168     {                                                                \
169         int i, j;                                                    \
170         uint32_t *block = strm->state->block_buf;                    \
171         uint16_t *next_in = (uint16_t *)strm->next_in;               \
172                                                                      \
173         for (i = 0; i < strm->rsi; i++)                              \
174             for (j = 0; j < BS; j++)                                 \
175                 block[i * BS + j] = (uint32_t)next_in[i * BS + j];   \
176                                                                      \
177         strm->next_in += 2 * BS * strm->rsi;                         \
178         strm->total_in += 2 * BS * strm->rsi;                        \
179         strm->avail_in -= 2 * BS * strm->rsi;                        \
180     }
181
182 #define GET_BLOCK_LSB_16(BS)                                         \
183     static void get_block_lsb_16_bs_##BS(struct aec_stream *strm)    \
184     {                                                                \
185         int i, j;                                                    \
186         uint32_t *block = strm->state->block_buf;                    \
187                                                                      \
188         for (i = 0; i < strm->rsi; i++)                              \
189             for (j = 0; j < BS; j++)                                 \
190                 block[i * BS + j] =                                  \
191                     (uint32_t)strm->next_in[2 * (i * BS + j)]        \
192                     | ((uint32_t)strm->next_in[2 * (i * BS + j) + 1] \
193                        << 8);                                        \
194                                                                      \
195         strm->next_in += 2 * BS * strm->rsi;                         \
196         strm->total_in += 2 * BS * strm->rsi;                        \
197         strm->avail_in -= 2 * BS * strm->rsi;                        \
198     }
199
200 #define GET_BLOCK_MSB_16(BS)                                         \
201     static void get_block_msb_16_bs_##BS(struct aec_stream *strm)    \
202     {                                                                \
203         int i, j;                                                    \
204         uint32_t *block = strm->state->block_buf;                    \
205                                                                      \
206         for (i = 0; i < strm->rsi; i++)                              \
207             for (j = 0; j < BS; j++)                                 \
208                 block[i * BS + j] =                                  \
209                     ((uint32_t)strm->next_in[2 * (i * BS + j)] << 8) \
210                     | (uint32_t)strm->next_in[2 * (i * BS + j) + 1]; \
211                                                                      \
212         strm->next_in += 2 * BS * strm->rsi;                         \
213         strm->total_in += 2 * BS * strm->rsi;                        \
214         strm->avail_in -= 2 * BS * strm->rsi;                        \
215     }
216
217 #define GET_BLOCK_LSB_24(BS)                                         \
218     static void get_block_lsb_24_bs_##BS(struct aec_stream *strm)    \
219     {                                                                \
220         int i, j;                                                    \
221         uint32_t *block = strm->state->block_buf;                    \
222                                                                      \
223         for (i = 0; i < strm->rsi; i++)                              \
224             for (j = 0; j < BS; j++)                                 \
225                 block[i * BS + j] =                                  \
226                     (uint32_t)strm->next_in[3 * (i * BS + j)]        \
227                     | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
228                        << 8)                                         \
229                     | ((uint32_t)strm->next_in[3 * (i * BS + j) + 2] \
230                        << 16);                                       \
231                                                                      \
232         strm->next_in += 3 * BS * strm->rsi;                         \
233         strm->total_in += 3 * BS * strm->rsi;                        \
234         strm->avail_in -= 3 * BS * strm->rsi;                        \
235     }
236
237 #define GET_BLOCK_MSB_24(BS)                                         \
238     static void get_block_msb_24_bs_##BS(struct aec_stream *strm)    \
239     {                                                                \
240         int i, j;                                                    \
241         uint32_t *block = strm->state->block_buf;                    \
242                                                                      \
243         for (i = 0; i < strm->rsi; i++)                              \
244             for (j = 0; j < BS; j++)                                 \
245                 block[i * BS + j] =                                  \
246                     ((uint32_t)strm->next_in[3 * (i * BS + j)]       \
247                        << 16)                                        \
248                     | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
249                        << 8)                                         \
250                     | (uint32_t)strm->next_in[3 * (i * BS + j) + 2]; \
251                                                                      \
252         strm->next_in += 3 * BS * strm->rsi;                         \
253         strm->total_in += 3 * BS * strm->rsi;                        \
254         strm->avail_in -= 3 * BS * strm->rsi;                        \
255     }
256
257 #define GET_BLOCK_NATIVE_32(BS)                                      \
258     static void get_block_native_32_bs_##BS(struct aec_stream *strm) \
259     {                                                                \
260         memcpy(strm->state->block_buf,                               \
261                strm->next_in,                                        \
262                4 * BS * strm->rsi);                                  \
263                                                                      \
264         strm->next_in += 4 * BS * strm->rsi;                         \
265         strm->total_in += 4 * BS * strm->rsi;                        \
266         strm->avail_in -= 4 * BS * strm->rsi;                        \
267     }
268
269 #define GET_BLOCK_LSB_32(BS)                                         \
270     static void get_block_lsb_32_bs_##BS(struct aec_stream *strm)    \
271     {                                                                \
272         int i, j;                                                    \
273         uint32_t *block = strm->state->block_buf;                    \
274                                                                      \
275         for (i = 0; i < strm->rsi; i++)                              \
276             for (j = 0; j < BS; j++)                                 \
277                 block[i * BS + j] =                                  \
278                     (uint32_t)strm->next_in[4 * (i * BS + j)]        \
279                     | ((uint32_t)strm->next_in[4 * (i * BS + j) + 1] \
280                        << 8)                                         \
281                     | ((uint32_t)strm->next_in[4 * (i * BS + j) + 2] \
282                        << 16)                                        \
283                     | ((uint32_t)strm->next_in[4 * (i * BS + j) + 3] \
284                        << 24);                                       \
285                                                                      \
286         strm->next_in += 4 * BS * strm->rsi;                         \
287         strm->total_in += 4 * BS * strm->rsi;                        \
288         strm->avail_in -= 4 * BS * strm->rsi;                        \
289     }
290
291 #define GET_BLOCK_MSB_32(BS)                                         \
292     static void get_block_msb_32_bs_##BS(struct aec_stream *strm)    \
293     {                                                                \
294         int i, j;                                                    \
295         uint32_t *block = strm->state->block_buf;                    \
296                                                                      \
297         for (i = 0; i < strm->rsi; i++)                              \
298             for (j = 0; j < BS; j++)                                 \
299                 block[i * BS + j] =                                  \
300                     ((uint32_t)strm->next_in[4 * (i * BS + j)]       \
301                      << 24)                                          \
302                     | ((uint32_t)strm->next_in[4 * (i * BS + j) + 1] \
303                        << 16)                                        \
304                     | ((uint32_t)strm->next_in[4 * (i * BS + j) + 2] \
305                        << 8)                                         \
306                     | (uint32_t)strm->next_in[4 * (i * BS + j) + 3]; \
307                                                                      \
308         strm->next_in += 4 * BS * strm->rsi;                         \
309         strm->total_in += 4 * BS * strm->rsi;                        \
310         strm->avail_in -= 4 * BS * strm->rsi;                        \
311     }
312
313 #define GET_BLOCK_FUNCS(A, B)                               \
314     void (*get_block_funcs_##A[])(struct aec_stream *) = {  \
315         get_block_##B##_bs_8,                               \
316         get_block_##B##_bs_16,                              \
317         get_block_##B##_bs_32,                              \
318         get_block_##B##_bs_64,                              \
319     }
320
321 GET_BLOCK_8(8);
322 GET_BLOCK_8(16);
323 GET_BLOCK_8(32);
324 GET_BLOCK_8(64);
325
326 GET_BLOCK_FUNCS(8, 8);
327
328 GET_BLOCK_LSB_24(8);
329 GET_BLOCK_LSB_24(16);
330 GET_BLOCK_LSB_24(32);
331 GET_BLOCK_LSB_24(64);
332
333 GET_BLOCK_FUNCS(lsb_24, lsb_24);
334
335 GET_BLOCK_MSB_24(8);
336 GET_BLOCK_MSB_24(16);
337 GET_BLOCK_MSB_24(32);
338 GET_BLOCK_MSB_24(64);
339
340 GET_BLOCK_FUNCS(msb_24, msb_24);
341
342 GET_BLOCK_NATIVE_16(8);
343 GET_BLOCK_NATIVE_16(16);
344 GET_BLOCK_NATIVE_16(32);
345 GET_BLOCK_NATIVE_16(64);
346
347 GET_BLOCK_NATIVE_32(8);
348 GET_BLOCK_NATIVE_32(16);
349 GET_BLOCK_NATIVE_32(32);
350 GET_BLOCK_NATIVE_32(64);
351
352 #ifdef WORDS_BIGENDIAN
353
354 GET_BLOCK_LSB_16(8);
355 GET_BLOCK_LSB_16(16);
356 GET_BLOCK_LSB_16(32);
357 GET_BLOCK_LSB_16(64);
358
359 GET_BLOCK_LSB_32(8);
360 GET_BLOCK_LSB_32(16);
361 GET_BLOCK_LSB_32(32);
362 GET_BLOCK_LSB_32(64);
363
364 GET_BLOCK_FUNCS(lsb_16, lsb_16);
365 GET_BLOCK_FUNCS(msb_16, native_16);
366 GET_BLOCK_FUNCS(lsb_32, lsb_32);
367 GET_BLOCK_FUNCS(msb_32, native_32);
368
369 #else /* not WORDS_BIGENDIAN */
370
371 GET_BLOCK_MSB_16(8);
372 GET_BLOCK_MSB_16(16);
373 GET_BLOCK_MSB_16(32);
374 GET_BLOCK_MSB_16(64);
375
376 GET_BLOCK_MSB_32(8);
377 GET_BLOCK_MSB_32(16);
378 GET_BLOCK_MSB_32(32);
379 GET_BLOCK_MSB_32(64);
380
381 GET_BLOCK_FUNCS(lsb_16, native_16);
382 GET_BLOCK_FUNCS(msb_16, msb_16);
383 GET_BLOCK_FUNCS(lsb_32, native_32);
384 GET_BLOCK_FUNCS(msb_32, msb_32);
385
386 #endif /* not WORDS_BIGENDIAN */