removes on loop in FLUSH
[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                                                                         \
93               uint32_t xmax, med, d;                                    \
94               med = state->xmax / 2 + 1;                                \
95               xmax = state->xmax;                                       \
96                                                                         \
97               for (bp = state->flush_start; bp < flush_end; bp++) {     \
98                   d = *bp;                                              \
99                   half_d = (d >> 1) + (d & 1);                          \
100                   /*in this case: data >= med == data & med */          \
101                   uint32_t mask = (data & med)?xmax:0;                  \
102                                                                         \
103                   /*in this case: xmax - data == xmax ^ data */         \
104                   if (half_d <= (mask ^ data)) {                        \
105                       data += (d >> 1)^(~((d & 1) - 1));                \
106                   } else {                                              \
107                       data = mask ^ d;                                  \
108                   }                                                     \
109                   put_##KIND(strm, (uint32_t)data);                     \
110               }                                                         \
111               state->last_out = data;                                   \
112                                                                         \
113             } else {                                                    \
114                                                                         \
115               int32_t xmax, d;                                          \
116               xmax = state->xmax;                                       \
117                                                                         \
118               for (bp = state->flush_start; bp < flush_end; bp++) {     \
119                   d = *bp;                                              \
120                   half_d = ((uint32_t)d >> 1) + (d & 1);                \
121                                                                         \
122                   if (data < 0) {                                       \
123                       if (half_d <= xmax + data + 1) {                  \
124                           data += ((uint32_t)d >> 1)^(~((d & 1) - 1));  \
125                       } else {                                          \
126                           data = d - xmax - 1;                          \
127                       }                                                 \
128                   } else {                                              \
129                       if (half_d <= xmax - data) {                      \
130                           data += ((uint32_t)d >> 1)^(~((d & 1) - 1));  \
131                       } else {                                          \
132                           data = xmax - d;                              \
133                       }                                                 \
134                   }                                                     \
135                   put_##KIND(strm, (uint32_t)data);                     \
136               }                                                         \
137               state->last_out = data;                                   \
138             }                                                           \
139         } else {                                                        \
140             for (bp = state->flush_start; bp < flush_end; bp++)         \
141                 put_##KIND(strm, *bp);                                  \
142         }                                                               \
143         state->flush_start = state->rsip;                               \
144     }
145
146
147 static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
148 {
149     *strm->next_out++ = (unsigned char)(data >> 24);
150     *strm->next_out++ = (unsigned char)(data >> 16);
151     *strm->next_out++ = (unsigned char)(data >> 8);
152     *strm->next_out++ = (unsigned char)data;
153 }
154
155 static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
156 {
157     *strm->next_out++ = (unsigned char)(data >> 16);
158     *strm->next_out++ = (unsigned char)(data >> 8);
159     *strm->next_out++ = (unsigned char)data;
160 }
161
162 static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
163 {
164     *strm->next_out++ = (unsigned char)(data >> 8);
165     *strm->next_out++ = (unsigned char)data;
166 }
167
168 static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
169 {
170     *strm->next_out++ = (unsigned char)data;
171     *strm->next_out++ = (unsigned char)(data >> 8);
172     *strm->next_out++ = (unsigned char)(data >> 16);
173     *strm->next_out++ = (unsigned char)(data >> 24);
174 }
175
176 static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
177 {
178     *strm->next_out++ = (unsigned char)data;
179     *strm->next_out++ = (unsigned char)(data >> 8);
180     *strm->next_out++ = (unsigned char)(data >> 16);
181 }
182
183 static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
184 {
185     *strm->next_out++ = (unsigned char)data;
186     *strm->next_out++ = (unsigned char)(data >> 8);
187 }
188
189 static inline void put_8(struct aec_stream *strm, uint32_t data)
190 {
191     *strm->next_out++ = (unsigned char)data;
192 }
193
194 FLUSH(msb_32)
195 FLUSH(msb_24)
196 FLUSH(msb_16)
197 FLUSH(lsb_32)
198 FLUSH(lsb_24)
199 FLUSH(lsb_16)
200 FLUSH(8)
201
202 static inline void check_rsi_end(struct aec_stream *strm)
203 {
204     /**
205        Flush output if end of RSI reached
206      */
207     struct internal_state *state = strm->state;
208
209     if (state->rsi_size == (size_t)(state->rsip - state->rsi_buffer)) {
210         state->flush_output(strm);
211         state->flush_start = state->rsi_buffer;
212         state->rsip = state->rsi_buffer;
213     }
214 }
215
216 static inline void put_sample(struct aec_stream *strm, uint32_t s)
217 {
218     struct internal_state *state = strm->state;
219
220     *state->rsip++ = s;
221     strm->avail_out -= state->bytes_per_sample;
222     check_rsi_end(strm);
223 }
224
225 static inline void fill_acc(struct aec_stream *strm)
226 {
227     int b = (63 - strm->state->bitp) >> 3;
228
229     strm->avail_in -= b;
230     strm->state->bitp += b << 3;
231
232     switch (b) {
233       case (7):
234         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
235       case (6):
236         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
237       case (5):
238         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
239       case (4):
240         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
241       case (3):
242         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
243       case (2):
244         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
245       case (1):
246         strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
247     };
248
249 }
250
251 static inline uint32_t direct_get(struct aec_stream *strm, int n)
252 {
253     /**
254        Get n bit from input stream
255
256        No checking whatsoever. Read bits are dumped.
257      */
258
259     struct internal_state *state = strm->state;
260
261     if (state->bitp < n)
262         fill_acc(strm);
263
264     state->bitp -= n;
265     return (state->acc >> state->bitp) & ((UINT64_C(1) << n) - 1);
266 }
267
268 static inline uint32_t direct_get_fs(struct aec_stream *strm)
269 {
270     /**
271        Interpret a Fundamental Sequence from the input buffer.
272
273        Essentially counts the number of 0 bits until a 1 is
274        encountered.
275      */
276
277     uint32_t fs = 0;
278 #if HAVE_DECL___BUILTIN_CLZLL
279     int lz;
280 #elif HAVE_BSR64
281     unsigned long lz;
282 #endif
283     struct internal_state *state = strm->state;
284
285     state->acc &= ((UINT64_C(1) << state->bitp) - 1);
286
287     while (state->acc == 0) {
288         fs += state->bitp;
289         state->bitp = 0;
290         fill_acc(strm);
291     }
292
293 #if HAVE_DECL___BUILTIN_CLZLL
294     lz = __builtin_clzll(state->acc);
295     fs += lz + state->bitp - 64;
296     state->bitp = 63 - lz;
297 #elif HAVE_BSR64
298     _BitScanReverse64(&lz, state->acc);
299     fs += state->bitp - 1 - lz;
300     state->bitp = lz;
301 #else
302     state->bitp--;
303     while ((state->acc & (UINT64_C(1) << state->bitp)) == 0) {
304         state->bitp--;
305         fs++;
306     }
307 #endif
308     return fs;
309 }
310
311 static inline uint32_t bits_ask(struct aec_stream *strm, int n)
312 {
313     while (strm->state->bitp < n) {
314         if (strm->avail_in == 0)
315             return 0;
316         strm->avail_in--;
317         strm->state->acc <<= 8;
318         strm->state->acc |= *strm->next_in++;
319         strm->state->bitp += 8;
320     }
321     return 1;
322 }
323
324 static inline uint32_t bits_get(struct aec_stream *strm, int n)
325 {
326     return (strm->state->acc >> (strm->state->bitp - n))
327         & ((UINT64_C(1) << n) - 1);
328 }
329
330 static inline void bits_drop(struct aec_stream *strm, int n)
331 {
332     strm->state->bitp -= n;
333 }
334
335 static inline uint32_t fs_ask(struct aec_stream *strm)
336 {
337     if (bits_ask(strm, 1) == 0)
338         return 0;
339     while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
340         if (strm->state->bitp == 1) {
341             if (strm->avail_in == 0)
342                 return 0;
343             strm->avail_in--;
344             strm->state->acc <<= 8;
345             strm->state->acc |= *strm->next_in++;
346             strm->state->bitp += 8;
347         }
348         strm->state->fs++;
349         strm->state->bitp--;
350     }
351     return 1;
352 }
353
354 static inline void fs_drop(struct aec_stream *strm)
355 {
356     strm->state->fs = 0;
357     strm->state->bitp--;
358 }
359
360 static inline uint32_t copysample(struct aec_stream *strm)
361 {
362     if (bits_ask(strm, strm->bits_per_sample) == 0
363         || strm->avail_out == 0)
364         return 0;
365
366     put_sample(strm, bits_get(strm, strm->bits_per_sample));
367     bits_drop(strm, strm->bits_per_sample);
368     return 1;
369 }
370
371 static int m_id(struct aec_stream *strm)
372 {
373     struct internal_state *state = strm->state;
374
375     if (state->rsip == state->rsi_buffer) {
376         if(strm->flags & AEC_PAD_RSI)
377             state->bitp -= state->bitp % 8;
378         if (state->pp)
379             state->ref = 1;
380     } else {
381         state->ref = 0;
382     }
383     if (bits_ask(strm, state->id_len) == 0)
384         return M_EXIT;
385     state->id = bits_get(strm, state->id_len);
386     bits_drop(strm, state->id_len);
387     state->mode = state->id_table[state->id];
388
389     return M_CONTINUE;
390 }
391
392 static int m_split_output(struct aec_stream *strm)
393 {
394     struct internal_state *state = strm->state;
395     int k = state->id - 1;
396
397     do {
398         if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
399             return M_EXIT;
400         *state->rsip++ += bits_get(strm, k);
401         strm->avail_out -= state->bytes_per_sample;
402         bits_drop(strm, k);
403     } while(++state->i < state->n);
404
405     check_rsi_end(strm);
406     state->mode = m_id;
407     return M_CONTINUE;
408 }
409
410 static int m_split_fs(struct aec_stream *strm)
411 {
412     struct internal_state *state = strm->state;
413     int k = state->id - 1;
414
415     do {
416         if (fs_ask(strm) == 0)
417             return M_EXIT;
418         state->rsip[state->i] = state->fs << k;
419         fs_drop(strm);
420     } while(++state->i < state->n);
421
422     state->i = 0;
423     state->mode = m_split_output;
424     return M_CONTINUE;
425 }
426
427 static int m_split(struct aec_stream *strm)
428 {
429     uint32_t i, k;
430     struct internal_state *state = strm->state;
431
432     if (BUFFERSPACE(strm)) {
433         k = state->id - 1;
434
435         if (state->ref)
436             *state->rsip++ = direct_get(strm, strm->bits_per_sample);
437
438         for (i = 0; i < strm->block_size - state->ref; i++)
439             state->rsip[i] = direct_get_fs(strm) << k;
440
441         for (i = state->ref; i < strm->block_size; i++)
442             *state->rsip++ += direct_get(strm, k);
443
444         strm->avail_out -= state->out_blklen;
445         check_rsi_end(strm);
446
447         state->mode = m_id;
448         return M_CONTINUE;
449     }
450
451     if (state->ref) {
452         if (copysample(strm) == 0)
453             return M_EXIT;
454         state->n = strm->block_size - 1;
455     } else {
456         state->n = strm->block_size;
457     }
458
459     state->i = 0;
460     state->mode = m_split_fs;
461     return M_CONTINUE;
462 }
463
464 static int m_zero_output(struct aec_stream *strm)
465 {
466     struct internal_state *state = strm->state;
467
468     do {
469         if (strm->avail_out == 0)
470             return M_EXIT;
471         put_sample(strm, 0);
472     } while(--state->i);
473
474     state->mode = m_id;
475     return M_CONTINUE;
476 }
477
478 static int m_zero_block(struct aec_stream *strm)
479 {
480     uint32_t i, zero_blocks, b, zero_bytes;
481     struct internal_state *state = strm->state;
482
483     if (fs_ask(strm) == 0)
484         return M_EXIT;
485     zero_blocks = state->fs + 1;
486     fs_drop(strm);
487
488     if (zero_blocks == ROS) {
489         b = (int)(state->rsip - state->rsi_buffer) / strm->block_size;
490         zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
491     } else if (zero_blocks > ROS) {
492         zero_blocks--;
493     }
494
495     if (state->ref)
496         i = zero_blocks * strm->block_size - 1;
497     else
498         i = zero_blocks * strm->block_size;
499
500     zero_bytes = i * state->bytes_per_sample;
501
502     if (strm->avail_out >= zero_bytes) {
503         if (state->rsi_size - (state->rsip - state->rsi_buffer) < i)
504             return M_ERROR;
505
506         memset(state->rsip, 0, i * sizeof(uint32_t));
507         state->rsip += i;
508         strm->avail_out -= zero_bytes;
509         check_rsi_end(strm);
510
511         state->mode = m_id;
512         return M_CONTINUE;
513     }
514
515     state->i = i;
516     state->mode = m_zero_output;
517     return M_CONTINUE;
518 }
519
520 static int m_se_decode(struct aec_stream *strm)
521 {
522     int32_t m, d1;
523     struct internal_state *state = strm->state;
524
525     while(state->i < strm->block_size) {
526         if (fs_ask(strm) == 0)
527             return M_EXIT;
528         m = state->fs;
529         d1 = m - state->se_table[2 * m + 1];
530
531         if ((state->i & 1) == 0) {
532             if (strm->avail_out == 0)
533                 return M_EXIT;
534             put_sample(strm, state->se_table[2 * m] - d1);
535             state->i++;
536         }
537
538         if (strm->avail_out == 0)
539             return M_EXIT;
540         put_sample(strm, d1);
541         state->i++;
542         fs_drop(strm);
543     }
544
545     state->mode = m_id;
546     return M_CONTINUE;
547 }
548
549 static int m_se(struct aec_stream *strm)
550 {
551     uint32_t i;
552     int32_t m, d1;
553     struct internal_state *state = strm->state;
554
555     if (BUFFERSPACE(strm)) {
556         i = state->ref;
557
558         while (i < strm->block_size) {
559             m = direct_get_fs(strm);
560             d1 = m - state->se_table[2 * m + 1];
561
562             if ((i & 1) == 0) {
563                 put_sample(strm, state->se_table[2 * m] - d1);
564                 i++;
565             }
566             put_sample(strm, d1);
567             i++;
568         }
569         state->mode = m_id;
570         return M_CONTINUE;
571     }
572
573     state->mode = m_se_decode;
574     state->i = state->ref;
575     return M_CONTINUE;
576 }
577
578 static int m_low_entropy_ref(struct aec_stream *strm)
579 {
580     struct internal_state *state = strm->state;
581
582     if (state->ref && copysample(strm) == 0)
583         return M_EXIT;
584
585     if(state->id == 1) {
586         state->mode = m_se;
587         return M_CONTINUE;
588     }
589
590     state->mode = m_zero_block;
591     return M_CONTINUE;
592 }
593
594 static int m_low_entropy(struct aec_stream *strm)
595 {
596     struct internal_state *state = strm->state;
597
598     if (bits_ask(strm, 1) == 0)
599         return M_EXIT;
600     state->id = bits_get(strm, 1);
601     bits_drop(strm, 1);
602     state->mode = m_low_entropy_ref;
603     return M_CONTINUE;
604 }
605
606 static int m_uncomp_copy(struct aec_stream *strm)
607 {
608     struct internal_state *state = strm->state;
609
610     do {
611         if (copysample(strm) == 0)
612             return M_EXIT;
613     } while(--state->i);
614
615     state->mode = m_id;
616     return M_CONTINUE;
617 }
618
619 static int m_uncomp(struct aec_stream *strm)
620 {
621     uint32_t i;
622     struct internal_state *state = strm->state;
623
624     if (BUFFERSPACE(strm)) {
625         for (i = 0; i < strm->block_size; i++)
626             *state->rsip++ = direct_get(strm, strm->bits_per_sample);
627         strm->avail_out -= state->out_blklen;
628         check_rsi_end(strm);
629
630         state->mode = m_id;
631         return M_CONTINUE;
632     }
633
634     state->i = strm->block_size;
635     state->mode = m_uncomp_copy;
636     return M_CONTINUE;
637 }
638
639 static void create_se_table(int *table)
640 {
641     int i, j, k, ms;
642
643     k = 0;
644     for (i = 0; i < 13; i++) {
645         ms = k;
646         for (j = 0; j <= i; j++) {
647             table[2 * k] = i;
648             table[2 * k + 1] = ms;
649             k++;
650         }
651     }
652 }
653
654 int aec_decode_init(struct aec_stream *strm)
655 {
656     int i, modi;
657     struct internal_state *state;
658
659     if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
660         return AEC_CONF_ERROR;
661
662     state = malloc(sizeof(struct internal_state));
663     if (state == NULL)
664         return AEC_MEM_ERROR;
665     memset(state, 0, sizeof(struct internal_state));
666
667     create_se_table(state->se_table);
668
669     strm->state = state;
670
671     if (strm->bits_per_sample > 16) {
672         state->id_len = 5;
673
674         if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
675             state->bytes_per_sample = 3;
676             if (strm->flags & AEC_DATA_MSB)
677                 state->flush_output = flush_msb_24;
678             else
679                 state->flush_output = flush_lsb_24;
680         } else {
681             state->bytes_per_sample = 4;
682             if (strm->flags & AEC_DATA_MSB)
683                 state->flush_output = flush_msb_32;
684             else
685                 state->flush_output = flush_lsb_32;
686         }
687         state->out_blklen = strm->block_size
688             * state->bytes_per_sample;
689     }
690     else if (strm->bits_per_sample > 8) {
691         state->bytes_per_sample = 2;
692         state->id_len = 4;
693         state->out_blklen = strm->block_size * 2;
694         if (strm->flags & AEC_DATA_MSB)
695             state->flush_output = flush_msb_16;
696         else
697             state->flush_output = flush_lsb_16;
698     } else {
699         if (strm->flags & AEC_RESTRICTED) {
700             if (strm->bits_per_sample <= 4) {
701                 if (strm->bits_per_sample <= 2)
702                     state->id_len = 1;
703                 else
704                     state->id_len = 2;
705             } else {
706                 return AEC_CONF_ERROR;
707             }
708         } else {
709             state->id_len = 3;
710         }
711
712         state->bytes_per_sample = 1;
713         state->out_blklen = strm->block_size;
714         state->flush_output = flush_8;
715     }
716
717     if (strm->flags & AEC_DATA_SIGNED) {
718         state->xmin = -(INT64_C(1) << (strm->bits_per_sample - 1));
719         state->xmax = (UINT64_C(1) << (strm->bits_per_sample - 1)) - 1;
720     } else {
721         state->xmin = 0;
722         state->xmax = (UINT64_C(1) << strm->bits_per_sample) - 1;
723     }
724
725     state->in_blklen = (strm->block_size * strm->bits_per_sample
726                         + state->id_len) / 8 + 9;
727
728     modi = 1UL << state->id_len;
729     state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
730     if (state->id_table == NULL)
731         return AEC_MEM_ERROR;
732
733     state->id_table[0] = m_low_entropy;
734     for (i = 1; i < modi - 1; i++) {
735         state->id_table[i] = m_split;
736     }
737     state->id_table[modi - 1] = m_uncomp;
738
739     state->rsi_size = strm->rsi * strm->block_size;
740     state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
741     if (state->rsi_buffer == NULL)
742         return AEC_MEM_ERROR;
743
744     state->ref = 0;
745     strm->total_in = 0;
746     strm->total_out = 0;
747
748     state->rsip = state->rsi_buffer;
749     state->flush_start = state->rsi_buffer;
750     state->bitp = 0;
751     state->fs = 0;
752     state->pp = strm->flags & AEC_DATA_PREPROCESS;
753     state->mode = m_id;
754     return AEC_OK;
755 }
756
757 int aec_decode(struct aec_stream *strm, int flush)
758 {
759     /**
760        Finite-state machine implementation of the adaptive entropy
761        decoder.
762
763        Can work with one byte input und one sample output buffers. If
764        enough buffer space is available, then faster implementations
765        of the states are called. Inspired by zlib.
766     */
767
768     struct internal_state *state = strm->state;
769     int status;
770
771     strm->total_in += strm->avail_in;
772     strm->total_out += strm->avail_out;
773
774     do {
775         status = state->mode(strm);
776     } while (status == M_CONTINUE);
777
778     if (status == M_ERROR)
779         return AEC_DATA_ERROR;
780
781     state->flush_output(strm);
782
783     strm->total_in -= strm->avail_in;
784     strm->total_out -= strm->avail_out;
785
786     return AEC_OK;
787 }
788
789 int aec_decode_end(struct aec_stream *strm)
790 {
791     struct internal_state *state = strm->state;
792
793     free(state->id_table);
794     free(state->rsi_buffer);
795     free(state);
796     return AEC_OK;
797 }
798
799 int aec_buffer_decode(struct aec_stream *strm)
800 {
801     int status;
802
803     status = aec_decode_init(strm);
804     if (status != AEC_OK)
805         return status;
806
807     status = aec_decode(strm, AEC_FLUSH);
808     aec_decode_end(strm);
809     return status;
810 }