Slightly faster loading of accumulator.
[platform/upstream/libaec.git] / src / decode.c
1 /**
2  * @file decode.c
3  *
4  * @section LICENSE
5  * Copyright 2012 - 2014
6  *
7  * Mathis Rosenhauer, Moritz Hanke, Joerg Behrens
8  * Deutsches Klimarechenzentrum GmbH
9  * Bundesstr. 45a
10  * 20146 Hamburg Germany
11  *
12  * Luis Kornblueh
13  * Max-Planck-Institut fuer Meteorologie
14  * Bundesstr. 53
15  * 20146 Hamburg
16  * Germany
17  *
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above
27  *    copyright notice, this list of conditions and the following
28  *    disclaimer in the documentation and/or other materials provided
29  *    with the distribution.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * @section DESCRIPTION
45  *
46  * Adaptive Entropy Decoder
47  * Based on CCSDS documents 121.0-B-2 and 120.0-G-3
48  *
49  */
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "libaec.h"
56 #include "decode.h"
57
58 #if HAVE_BSR64
59 #  include <intrin.h>
60 #endif
61
62 #define ROS 5
63
64 #define BUFFERSPACE(strm) (strm->avail_in >= strm->state->in_blklen      \
65                            && strm->avail_out >= strm->state->out_blklen)
66
67 #define FLUSH(KIND)                                                      \
68     static void flush_##KIND(struct aec_stream *strm)                    \
69     {                                                                    \
70         uint32_t *flush_end, *bp, half_d;                                \
71         int32_t data, m;                                                 \
72         struct internal_state *state = strm->state;                      \
73                                                                          \
74         flush_end = state->rsip;                                         \
75         if (state->pp) {                                                 \
76             if (state->flush_start == state->rsi_buffer                  \
77                 && state->rsip > state->rsi_buffer) {                    \
78                 state->last_out = *state->rsi_buffer;                    \
79                                                                          \
80                 if (strm->flags & AEC_DATA_SIGNED) {                     \
81                     m = UINT32_C(1) << (strm->bits_per_sample - 1);      \
82                     /* Reference samples have to be sign extended */     \
83                     state->last_out = (state->last_out ^ m) - m;         \
84                 }                                                        \
85                 put_##KIND(strm, (uint32_t)state->last_out);             \
86                 state->flush_start++;                                    \
87             }                                                            \
88                                                                          \
89             data = state->last_out;                                      \
90                                                                          \
91             if (state->xmin == 0) {                                      \
92                 uint32_t xmax, med, d;                                   \
93                 med = state->xmax / 2 + 1;                               \
94                 xmax = state->xmax;                                      \
95                                                                          \
96                 for (bp = state->flush_start; bp < flush_end; bp++) {    \
97                     d = *bp;                                             \
98                     half_d = (d >> 1) + (d & 1);                         \
99                     /*in this case: data >= med == data & med */         \
100                     uint32_t mask = (data & med)?xmax:0;                 \
101                                                                          \
102                     /*in this case: xmax - data == xmax ^ data */        \
103                     if (half_d <= (mask ^ data)) {                       \
104                         data += (d >> 1)^(~((d & 1) - 1));               \
105                     } else {                                             \
106                         data = mask ^ d;                                 \
107                     }                                                    \
108                     put_##KIND(strm, (uint32_t)data);                    \
109                 }                                                        \
110                 state->last_out = data;                                  \
111             } else {                                                     \
112                 int32_t xmax, d;                                         \
113                 xmax = state->xmax;                                      \
114                                                                          \
115                 for (bp = state->flush_start; bp < flush_end; bp++) {    \
116                     d = *bp;                                             \
117                     half_d = ((uint32_t)d >> 1) + (d & 1);               \
118                                                                          \
119                     if (data < 0) {                                      \
120                         if (half_d <= xmax + data + 1) {                 \
121                             data += ((uint32_t)d >> 1)^(~((d & 1) - 1)); \
122                         } else {                                         \
123                             data = d - xmax - 1;                         \
124                         }                                                \
125                     } else {                                             \
126                         if (half_d <= xmax - data) {                     \
127                             data += ((uint32_t)d >> 1)^(~((d & 1) - 1)); \
128                         } else {                                         \
129                             data = xmax - d;                             \
130                         }                                                \
131                     }                                                    \
132                     put_##KIND(strm, (uint32_t)data);                    \
133                 }                                                        \
134                 state->last_out = data;                                  \
135             }                                                            \
136         } else {                                                         \
137             for (bp = state->flush_start; bp < flush_end; bp++)          \
138                 put_##KIND(strm, *bp);                                   \
139         }                                                                \
140         state->flush_start = state->rsip;                                \
141     }
142
143
144 static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
145 {
146     *strm->next_out++ = (unsigned char)(data >> 24);
147     *strm->next_out++ = (unsigned char)(data >> 16);
148     *strm->next_out++ = (unsigned char)(data >> 8);
149     *strm->next_out++ = (unsigned char)data;
150 }
151
152 static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
153 {
154     *strm->next_out++ = (unsigned char)(data >> 16);
155     *strm->next_out++ = (unsigned char)(data >> 8);
156     *strm->next_out++ = (unsigned char)data;
157 }
158
159 static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
160 {
161     *strm->next_out++ = (unsigned char)(data >> 8);
162     *strm->next_out++ = (unsigned char)data;
163 }
164
165 static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
166 {
167     *strm->next_out++ = (unsigned char)data;
168     *strm->next_out++ = (unsigned char)(data >> 8);
169     *strm->next_out++ = (unsigned char)(data >> 16);
170     *strm->next_out++ = (unsigned char)(data >> 24);
171 }
172
173 static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
174 {
175     *strm->next_out++ = (unsigned char)data;
176     *strm->next_out++ = (unsigned char)(data >> 8);
177     *strm->next_out++ = (unsigned char)(data >> 16);
178 }
179
180 static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
181 {
182     *strm->next_out++ = (unsigned char)data;
183     *strm->next_out++ = (unsigned char)(data >> 8);
184 }
185
186 static inline void put_8(struct aec_stream *strm, uint32_t data)
187 {
188     *strm->next_out++ = (unsigned char)data;
189 }
190
191 FLUSH(msb_32)
192 FLUSH(msb_24)
193 FLUSH(msb_16)
194 FLUSH(lsb_32)
195 FLUSH(lsb_24)
196 FLUSH(lsb_16)
197 FLUSH(8)
198
199 static inline void check_rsi_end(struct aec_stream *strm)
200 {
201     /**
202        Flush output if end of RSI reached
203      */
204     struct internal_state *state = strm->state;
205
206     if (state->rsi_size == (size_t)(state->rsip - state->rsi_buffer)) {
207         state->flush_output(strm);
208         state->flush_start = state->rsi_buffer;
209         state->rsip = state->rsi_buffer;
210     }
211 }
212
213 static inline void put_sample(struct aec_stream *strm, uint32_t s)
214 {
215     struct internal_state *state = strm->state;
216
217     *state->rsip++ = s;
218     strm->avail_out -= state->bytes_per_sample;
219     check_rsi_end(strm);
220 }
221
222 static inline uint32_t direct_get(struct aec_stream *strm, int n)
223 {
224     /**
225        Get n bit from input stream
226
227        No checking whatsoever. Read bits are dumped.
228      */
229
230     struct internal_state *state = strm->state;
231     int b;
232
233     if (state->bitp < n)
234     {
235         b = (63 - state->bitp) >> 3;
236         if (b == 6) {
237             state->acc = (state->acc << 48)
238                 | ((uint64_t)strm->next_in[0] << 40)
239                 | ((uint64_t)strm->next_in[1] << 32)
240                 | ((uint64_t)strm->next_in[2] << 24)
241                 | ((uint64_t)strm->next_in[3] << 16)
242                 | ((uint64_t)strm->next_in[4] << 8)
243                 | (uint64_t)strm->next_in[5];
244         } else if (b == 7) {
245             state->acc = (state->acc << 56)
246                 | ((uint64_t)strm->next_in[0] << 48)
247                 | ((uint64_t)strm->next_in[1] << 40)
248                 | ((uint64_t)strm->next_in[2] << 32)
249                 | ((uint64_t)strm->next_in[3] << 24)
250                 | ((uint64_t)strm->next_in[4] << 16)
251                 | ((uint64_t)strm->next_in[5] << 8)
252                 | (uint64_t)strm->next_in[6];
253         } else if (b == 5) {
254             state->acc = (state->acc << 40)
255                 | ((uint64_t)strm->next_in[0] << 32)
256                 | ((uint64_t)strm->next_in[1] << 24)
257                 | ((uint64_t)strm->next_in[2] << 16)
258                 | ((uint64_t)strm->next_in[3] << 8)
259                 | (uint64_t)strm->next_in[4];
260         } else if (b == 4) {
261             state->acc = (state->acc << 32)
262                 | ((uint64_t)strm->next_in[0] << 24)
263                 | ((uint64_t)strm->next_in[1] << 16)
264                 | ((uint64_t)strm->next_in[2] << 8)
265                 | (uint64_t)strm->next_in[3];
266         } else if (b == 3) {
267             state->acc = (state->acc << 24)
268                 | ((uint64_t)strm->next_in[0] << 16)
269                 | ((uint64_t)strm->next_in[1] << 8)
270                 | (uint64_t)strm->next_in[2];
271         } else if (b == 2) {
272             state->acc = (state->acc << 16)
273                 | ((uint64_t)strm->next_in[0] << 8)
274                 | (uint64_t)strm->next_in[1];
275         } else if (b == 1) {
276             state->acc = (state->acc << 8)
277                 | (uint64_t)strm->next_in[0];
278         }
279         strm->next_in += b;
280         strm->avail_in -= b;
281         state->bitp += b << 3;
282     }
283
284     state->bitp -= n;
285     return (state->acc >> state->bitp) & (UINT64_MAX >> (64 - n));
286 }
287
288 static inline uint32_t direct_get_fs(struct aec_stream *strm)
289 {
290     /**
291        Interpret a Fundamental Sequence from the input buffer.
292
293        Essentially counts the number of 0 bits until a 1 is
294        encountered.
295      */
296
297     uint32_t fs = 0;
298 #if HAVE_BSR64
299     unsigned long i;
300 #else
301     int i;
302 #endif
303     struct internal_state *state = strm->state;
304
305     if (state->bitp)
306         state->acc &= UINT64_MAX >> (64 - state->bitp);
307     else
308         state->acc = 0;
309
310     while (state->acc == 0) {
311         state->acc = (state->acc << 56)
312             | ((uint64_t)strm->next_in[0] << 48)
313             | ((uint64_t)strm->next_in[1] << 40)
314             | ((uint64_t)strm->next_in[2] << 32)
315             | ((uint64_t)strm->next_in[3] << 24)
316             | ((uint64_t)strm->next_in[4] << 16)
317             | ((uint64_t)strm->next_in[5] << 8)
318             | (uint64_t)strm->next_in[6];
319         strm->next_in += 7;
320         strm->avail_in -= 7;
321         fs += state->bitp;
322         state->bitp = 56;
323     }
324
325 #if HAVE_DECL___BUILTIN_CLZLL
326     i = 63 - __builtin_clzll(state->acc);
327 #elif HAVE_BSR64
328     _BitScanReverse64(&i, state->acc);
329 #else
330     i = state->bitp - 1;
331     while ((state->acc & (UINT64_C(1) << i)) == 0)
332         i--;
333 #endif
334     fs += state->bitp - i - 1;
335     state->bitp = i;
336     return fs;
337 }
338
339 static inline uint32_t bits_ask(struct aec_stream *strm, int n)
340 {
341     while (strm->state->bitp < n) {
342         if (strm->avail_in == 0)
343             return 0;
344         strm->avail_in--;
345         strm->state->acc <<= 8;
346         strm->state->acc |= *strm->next_in++;
347         strm->state->bitp += 8;
348     }
349     return 1;
350 }
351
352 static inline uint32_t bits_get(struct aec_stream *strm, int n)
353 {
354     return (strm->state->acc >> (strm->state->bitp - n))
355         & (UINT64_MAX >> (64 - n));
356 }
357
358 static inline void bits_drop(struct aec_stream *strm, int n)
359 {
360     strm->state->bitp -= n;
361 }
362
363 static inline uint32_t fs_ask(struct aec_stream *strm)
364 {
365     if (bits_ask(strm, 1) == 0)
366         return 0;
367     while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
368         if (strm->state->bitp == 1) {
369             if (strm->avail_in == 0)
370                 return 0;
371             strm->avail_in--;
372             strm->state->acc <<= 8;
373             strm->state->acc |= *strm->next_in++;
374             strm->state->bitp += 8;
375         }
376         strm->state->fs++;
377         strm->state->bitp--;
378     }
379     return 1;
380 }
381
382 static inline void fs_drop(struct aec_stream *strm)
383 {
384     strm->state->fs = 0;
385     strm->state->bitp--;
386 }
387
388 static inline uint32_t copysample(struct aec_stream *strm)
389 {
390     if (bits_ask(strm, strm->bits_per_sample) == 0
391         || strm->avail_out == 0)
392         return 0;
393
394     put_sample(strm, bits_get(strm, strm->bits_per_sample));
395     bits_drop(strm, strm->bits_per_sample);
396     return 1;
397 }
398
399 static int m_id(struct aec_stream *strm)
400 {
401     struct internal_state *state = strm->state;
402
403     if (state->rsip == state->rsi_buffer) {
404         if(strm->flags & AEC_PAD_RSI)
405             state->bitp -= state->bitp % 8;
406         if (state->pp)
407             state->ref = 1;
408     } else {
409         state->ref = 0;
410     }
411     if (bits_ask(strm, state->id_len) == 0)
412         return M_EXIT;
413     state->id = bits_get(strm, state->id_len);
414     bits_drop(strm, state->id_len);
415     state->mode = state->id_table[state->id];
416
417     return M_CONTINUE;
418 }
419
420 static int m_split_output(struct aec_stream *strm)
421 {
422     struct internal_state *state = strm->state;
423     int k = state->id - 1;
424
425     do {
426         if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
427             return M_EXIT;
428         if (k)
429             *state->rsip++ += bits_get(strm, k);
430         else
431             state->rsip++;
432         strm->avail_out -= state->bytes_per_sample;
433         bits_drop(strm, k);
434     } while(++state->i < state->n);
435
436     check_rsi_end(strm);
437     state->mode = m_id;
438     return M_CONTINUE;
439 }
440
441 static int m_split_fs(struct aec_stream *strm)
442 {
443     struct internal_state *state = strm->state;
444     int k = state->id - 1;
445
446     do {
447         if (fs_ask(strm) == 0)
448             return M_EXIT;
449         state->rsip[state->i] = state->fs << k;
450         fs_drop(strm);
451     } while(++state->i < state->n);
452
453     state->i = 0;
454     state->mode = m_split_output;
455
456     return M_CONTINUE;
457 }
458
459 static int m_split(struct aec_stream *strm)
460 {
461     uint32_t i, k;
462     struct internal_state *state = strm->state;
463
464     if (BUFFERSPACE(strm)) {
465         k = state->id - 1;
466
467         if (state->ref)
468             *state->rsip++ = direct_get(strm, strm->bits_per_sample);
469
470         for (i = 0; i < strm->block_size - state->ref; i++)
471             state->rsip[i] = direct_get_fs(strm) << k;
472
473         if (k) {
474             for (i = state->ref; i < strm->block_size; i++)
475                 *state->rsip++ += direct_get(strm, k);
476         } else {
477             state->rsip += strm->block_size - state->ref;
478         }
479
480         strm->avail_out -= state->out_blklen;
481         check_rsi_end(strm);
482
483         state->mode = m_id;
484         return M_CONTINUE;
485     }
486
487     if (state->ref) {
488         if (copysample(strm) == 0)
489             return M_EXIT;
490         state->n = strm->block_size - 1;
491     } else {
492         state->n = strm->block_size;
493     }
494
495     state->i = 0;
496     state->mode = m_split_fs;
497     return M_CONTINUE;
498 }
499
500 static int m_zero_output(struct aec_stream *strm)
501 {
502     struct internal_state *state = strm->state;
503
504     do {
505         if (strm->avail_out == 0)
506             return M_EXIT;
507         put_sample(strm, 0);
508     } while(--state->i);
509
510     state->mode = m_id;
511     return M_CONTINUE;
512 }
513
514 static int m_zero_block(struct aec_stream *strm)
515 {
516     uint32_t i, zero_blocks, b, zero_bytes;
517     struct internal_state *state = strm->state;
518
519     if (fs_ask(strm) == 0)
520         return M_EXIT;
521     zero_blocks = state->fs + 1;
522     fs_drop(strm);
523
524     if (zero_blocks == ROS) {
525         b = (int)(state->rsip - state->rsi_buffer) / strm->block_size;
526         zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
527     } else if (zero_blocks > ROS) {
528         zero_blocks--;
529     }
530
531     if (state->ref)
532         i = zero_blocks * strm->block_size - 1;
533     else
534         i = zero_blocks * strm->block_size;
535
536     zero_bytes = i * state->bytes_per_sample;
537
538     if (strm->avail_out >= zero_bytes) {
539         if (state->rsi_size - (state->rsip - state->rsi_buffer) < i)
540             return M_ERROR;
541
542         memset(state->rsip, 0, i * sizeof(uint32_t));
543         state->rsip += i;
544         strm->avail_out -= zero_bytes;
545         check_rsi_end(strm);
546
547         state->mode = m_id;
548         return M_CONTINUE;
549     }
550
551     state->i = i;
552     state->mode = m_zero_output;
553     return M_CONTINUE;
554 }
555
556 static int m_se_decode(struct aec_stream *strm)
557 {
558     int32_t m, d1;
559     struct internal_state *state = strm->state;
560
561     while(state->i < strm->block_size) {
562         if (fs_ask(strm) == 0)
563             return M_EXIT;
564         m = state->fs;
565         d1 = m - state->se_table[2 * m + 1];
566
567         if ((state->i & 1) == 0) {
568             if (strm->avail_out == 0)
569                 return M_EXIT;
570             put_sample(strm, state->se_table[2 * m] - d1);
571             state->i++;
572         }
573
574         if (strm->avail_out == 0)
575             return M_EXIT;
576         put_sample(strm, d1);
577         state->i++;
578         fs_drop(strm);
579     }
580
581     state->mode = m_id;
582     return M_CONTINUE;
583 }
584
585 static int m_se(struct aec_stream *strm)
586 {
587     uint32_t i;
588     int32_t m, d1;
589     struct internal_state *state = strm->state;
590
591     if (BUFFERSPACE(strm)) {
592         i = state->ref;
593
594         while (i < strm->block_size) {
595             m = direct_get_fs(strm);
596             d1 = m - state->se_table[2 * m + 1];
597
598             if ((i & 1) == 0) {
599                 put_sample(strm, state->se_table[2 * m] - d1);
600                 i++;
601             }
602             put_sample(strm, d1);
603             i++;
604         }
605         state->mode = m_id;
606         return M_CONTINUE;
607     }
608
609     state->mode = m_se_decode;
610     state->i = state->ref;
611     return M_CONTINUE;
612 }
613
614 static int m_low_entropy_ref(struct aec_stream *strm)
615 {
616     struct internal_state *state = strm->state;
617
618     if (state->ref && copysample(strm) == 0)
619         return M_EXIT;
620
621     if(state->id == 1) {
622         state->mode = m_se;
623         return M_CONTINUE;
624     }
625
626     state->mode = m_zero_block;
627     return M_CONTINUE;
628 }
629
630 static int m_low_entropy(struct aec_stream *strm)
631 {
632     struct internal_state *state = strm->state;
633
634     if (bits_ask(strm, 1) == 0)
635         return M_EXIT;
636     state->id = bits_get(strm, 1);
637     bits_drop(strm, 1);
638     state->mode = m_low_entropy_ref;
639     return M_CONTINUE;
640 }
641
642 static int m_uncomp_copy(struct aec_stream *strm)
643 {
644     struct internal_state *state = strm->state;
645
646     do {
647         if (copysample(strm) == 0)
648             return M_EXIT;
649     } while(--state->i);
650
651     state->mode = m_id;
652     return M_CONTINUE;
653 }
654
655 static int m_uncomp(struct aec_stream *strm)
656 {
657     uint32_t i;
658     struct internal_state *state = strm->state;
659
660     if (BUFFERSPACE(strm)) {
661         for (i = 0; i < strm->block_size; i++)
662             *state->rsip++ = direct_get(strm, strm->bits_per_sample);
663         strm->avail_out -= state->out_blklen;
664         check_rsi_end(strm);
665
666         state->mode = m_id;
667         return M_CONTINUE;
668     }
669
670     state->i = strm->block_size;
671     state->mode = m_uncomp_copy;
672     return M_CONTINUE;
673 }
674
675 static void create_se_table(int *table)
676 {
677     int i, j, k, ms;
678
679     k = 0;
680     for (i = 0; i < 13; i++) {
681         ms = k;
682         for (j = 0; j <= i; j++) {
683             table[2 * k] = i;
684             table[2 * k + 1] = ms;
685             k++;
686         }
687     }
688 }
689
690 int aec_decode_init(struct aec_stream *strm)
691 {
692     int i, modi;
693     struct internal_state *state;
694
695     if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
696         return AEC_CONF_ERROR;
697
698     state = malloc(sizeof(struct internal_state));
699     if (state == NULL)
700         return AEC_MEM_ERROR;
701     memset(state, 0, sizeof(struct internal_state));
702
703     create_se_table(state->se_table);
704
705     strm->state = state;
706
707     if (strm->bits_per_sample > 16) {
708         state->id_len = 5;
709
710         if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
711             state->bytes_per_sample = 3;
712             if (strm->flags & AEC_DATA_MSB)
713                 state->flush_output = flush_msb_24;
714             else
715                 state->flush_output = flush_lsb_24;
716         } else {
717             state->bytes_per_sample = 4;
718             if (strm->flags & AEC_DATA_MSB)
719                 state->flush_output = flush_msb_32;
720             else
721                 state->flush_output = flush_lsb_32;
722         }
723         state->out_blklen = strm->block_size
724             * state->bytes_per_sample;
725     }
726     else if (strm->bits_per_sample > 8) {
727         state->bytes_per_sample = 2;
728         state->id_len = 4;
729         state->out_blklen = strm->block_size * 2;
730         if (strm->flags & AEC_DATA_MSB)
731             state->flush_output = flush_msb_16;
732         else
733             state->flush_output = flush_lsb_16;
734     } else {
735         if (strm->flags & AEC_RESTRICTED) {
736             if (strm->bits_per_sample <= 4) {
737                 if (strm->bits_per_sample <= 2)
738                     state->id_len = 1;
739                 else
740                     state->id_len = 2;
741             } else {
742                 return AEC_CONF_ERROR;
743             }
744         } else {
745             state->id_len = 3;
746         }
747
748         state->bytes_per_sample = 1;
749         state->out_blklen = strm->block_size;
750         state->flush_output = flush_8;
751     }
752
753     if (strm->flags & AEC_DATA_SIGNED) {
754         state->xmax = UINT32_MAX >> (32 - strm->bits_per_sample + 1);
755         state->xmin = ~state->xmax;
756     } else {
757         state->xmin = 0;
758         state->xmax = UINT32_MAX >> (32 - strm->bits_per_sample);
759     }
760
761     state->in_blklen = (strm->block_size * strm->bits_per_sample
762                         + state->id_len) / 8 + 9;
763
764     modi = 1UL << state->id_len;
765     state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
766     if (state->id_table == NULL)
767         return AEC_MEM_ERROR;
768
769     state->id_table[0] = m_low_entropy;
770     for (i = 1; i < modi - 1; i++) {
771         state->id_table[i] = m_split;
772     }
773     state->id_table[modi - 1] = m_uncomp;
774
775     state->rsi_size = strm->rsi * strm->block_size;
776     state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
777     if (state->rsi_buffer == NULL)
778         return AEC_MEM_ERROR;
779
780     state->ref = 0;
781     strm->total_in = 0;
782     strm->total_out = 0;
783
784     state->rsip = state->rsi_buffer;
785     state->flush_start = state->rsi_buffer;
786     state->bitp = 0;
787     state->fs = 0;
788     state->pp = strm->flags & AEC_DATA_PREPROCESS;
789     state->mode = m_id;
790     return AEC_OK;
791 }
792
793 int aec_decode(struct aec_stream *strm, int flush)
794 {
795     /**
796        Finite-state machine implementation of the adaptive entropy
797        decoder.
798
799        Can work with one byte input und one sample output buffers. If
800        enough buffer space is available, then faster implementations
801        of the states are called. Inspired by zlib.
802     */
803
804     struct internal_state *state = strm->state;
805     int status;
806
807     strm->total_in += strm->avail_in;
808     strm->total_out += strm->avail_out;
809
810     do {
811         status = state->mode(strm);
812     } while (status == M_CONTINUE);
813
814     if (status == M_ERROR)
815         return AEC_DATA_ERROR;
816
817     state->flush_output(strm);
818
819     strm->total_in -= strm->avail_in;
820     strm->total_out -= strm->avail_out;
821
822     return AEC_OK;
823 }
824
825 int aec_decode_end(struct aec_stream *strm)
826 {
827     struct internal_state *state = strm->state;
828
829     free(state->id_table);
830     free(state->rsi_buffer);
831     free(state);
832     return AEC_OK;
833 }
834
835 int aec_buffer_decode(struct aec_stream *strm)
836 {
837     int status;
838
839     status = aec_decode_init(strm);
840     if (status != AEC_OK)
841         return status;
842
843     status = aec_decode(strm, AEC_FLUSH);
844     aec_decode_end(strm);
845     return status;
846 }