decode.c 24.3 KB
Newer Older
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
1 2
/**
 * @file decode.c
3 4
 *
 * @section LICENSE
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
5
 * Copyright 2012 - 2014
6
 *
7 8 9 10
 * Mathis Rosenhauer, Moritz Hanke, Joerg Behrens
 * Deutsches Klimarechenzentrum GmbH
 * Bundesstr. 45a
 * 20146 Hamburg Germany
11
 *
12 13 14 15 16
 * Luis Kornblueh
 * Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 53
 * 20146 Hamburg
 * Germany
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
44 45 46
 * @section DESCRIPTION
 *
 * Adaptive Entropy Decoder
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
47
 * Based on CCSDS documents 121.0-B-2 and 120.0-G-3
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
48 49
 *
 */
50 51 52 53 54

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
55
#include "libaec.h"
56
#include "decode.h"
57

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58 59 60 61
#if HAVE_BSR64
#  include <intrin.h>
#endif

62 63 64 65 66
#define ROS 5

#define BUFFERSPACE(strm) (strm->avail_in >= strm->state->in_blklen     \
                           && strm->avail_out >= strm->state->out_blklen)

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
67
#define FLUSH(KIND)                                                     \
68 69
    static void flush_##KIND(struct aec_stream *strm)                   \
    {                                                                   \
Moritz Hanke's avatar
Moritz Hanke committed
70
        uint32_t *flush_end, *bp, half_d;                               \
Moritz Hanke's avatar
Moritz Hanke committed
71
        int32_t data, m;                                                \
72 73
        struct internal_state *state = strm->state;                     \
                                                                        \
74
        flush_end = state->rsip;                                        \
75
        if (state->pp) {                                                \
76 77 78
            if (state->flush_start == state->rsi_buffer                 \
                && state->rsip > state->rsi_buffer) {                   \
                state->last_out = *state->rsi_buffer;                   \
79 80
                                                                        \
                if (strm->flags & AEC_DATA_SIGNED) {                    \
Moritz Hanke's avatar
Moritz Hanke committed
81
                    m = UINT32_C(1) << (strm->bits_per_sample - 1);     \
82
                    /* Reference samples have to be sign extended */    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
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
            }                                                           \
                                                                        \
Moritz Hanke's avatar
Moritz Hanke committed
89 90
            data = state->last_out;                                     \
                                                                        \
Moritz Hanke's avatar
Moritz Hanke committed
91
            if (state->xmin == 0) {                                     \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
92
                                                                        \
Moritz Hanke's avatar
Moritz Hanke committed
93 94 95
              uint32_t xmax, med, d;                                    \
              med = state->xmax / 2 + 1;                                \
              xmax = state->xmax;                                       \
Moritz Hanke's avatar
Moritz Hanke committed
96
                                                                        \
Moritz Hanke's avatar
Moritz Hanke committed
97 98 99
              for (bp = state->flush_start; bp < flush_end; bp++) {     \
                  d = *bp;                                              \
                  half_d = (d >> 1) + (d & 1);                          \
Moritz Hanke's avatar
Moritz Hanke committed
100
                                                                        \
Moritz Hanke's avatar
Moritz Hanke committed
101 102 103 104
                  /*in this case: data >= med == data & med */          \
                  if (data & med) {                                     \
                      /*in this case: xmax - data == xmax ^ data */     \
                      if (half_d <= (xmax ^ data)) {                    \
Moritz Hanke's avatar
Moritz Hanke committed
105 106
                          data += (d >> 1)^(~((d & 1) - 1));            \
                      } else {                                          \
Moritz Hanke's avatar
Moritz Hanke committed
107
                          data = xmax ^ d;                              \
Moritz Hanke's avatar
Moritz Hanke committed
108 109
                      }                                                 \
                  } else {                                              \
Moritz Hanke's avatar
Moritz Hanke committed
110
                      if (half_d <= data) {                             \
Moritz Hanke's avatar
Moritz Hanke committed
111 112
                          data += (d >> 1)^(~((d & 1) - 1));            \
                      } else {                                          \
Moritz Hanke's avatar
Moritz Hanke committed
113
                          data = d;                                     \
Moritz Hanke's avatar
Moritz Hanke committed
114 115 116 117 118 119 120 121
                      }                                                 \
                  }                                                     \
                  put_##KIND(strm, (uint32_t)data);                     \
              }                                                         \
              state->last_out = data;                                   \
                                                                        \
            } else {                                                    \
                                                                        \
122
              int32_t xmax, d;                                          \
Moritz Hanke's avatar
Moritz Hanke committed
123 124 125 126 127 128 129
              xmax = state->xmax;                                       \
                                                                        \
              for (bp = state->flush_start; bp < flush_end; bp++) {     \
                  d = *bp;                                              \
                  half_d = ((uint32_t)d >> 1) + (d & 1);                \
                                                                        \
                  if (data < 0) {                                       \
130
                      if (half_d <= xmax + data + 1) {                  \
Moritz Hanke's avatar
Moritz Hanke committed
131 132
                          data += ((uint32_t)d >> 1)^(~((d & 1) - 1));  \
                      } else {                                          \
133
                          data = d - xmax - 1;                          \
Moritz Hanke's avatar
Moritz Hanke committed
134 135 136 137 138 139 140 141 142 143 144
                      }                                                 \
                  } else {                                              \
                      if (half_d <= xmax - data) {                      \
                          data += ((uint32_t)d >> 1)^(~((d & 1) - 1));  \
                      } else {                                          \
                          data = xmax - d;                              \
                      }                                                 \
                  }                                                     \
                  put_##KIND(strm, (uint32_t)data);                     \
              }                                                         \
              state->last_out = data;                                   \
145 146
            }                                                           \
        } else {                                                        \
147 148
            for (bp = state->flush_start; bp < flush_end; bp++)         \
                put_##KIND(strm, *bp);                                  \
149
        }                                                               \
150
        state->flush_start = state->rsip;                               \
151
    }
152

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
153 154 155

static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
{
156 157 158 159
    *strm->next_out++ = (unsigned char)(data >> 24);
    *strm->next_out++ = (unsigned char)(data >> 16);
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)data;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
160 161 162 163
}

static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
{
164 165 166
    *strm->next_out++ = (unsigned char)(data >> 16);
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)data;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
167 168 169 170
}

static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
{
171 172
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)data;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
173 174 175 176
}

static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
{
177 178 179 180
    *strm->next_out++ = (unsigned char)data;
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)(data >> 16);
    *strm->next_out++ = (unsigned char)(data >> 24);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
181 182 183 184
}

static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
{
185 186 187
    *strm->next_out++ = (unsigned char)data;
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)(data >> 16);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
188 189 190 191
}

static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
{
192 193
    *strm->next_out++ = (unsigned char)data;
    *strm->next_out++ = (unsigned char)(data >> 8);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
194 195 196 197
}

static inline void put_8(struct aec_stream *strm, uint32_t data)
{
198
    *strm->next_out++ = (unsigned char)data;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
199 200
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
201 202 203 204 205 206 207
FLUSH(msb_32)
FLUSH(msb_24)
FLUSH(msb_16)
FLUSH(lsb_32)
FLUSH(lsb_24)
FLUSH(lsb_16)
FLUSH(8)
208

209
static inline void check_rsi_end(struct aec_stream *strm)
210
{
211
    /**
212
       Flush output if end of RSI reached
213
     */
214
    struct internal_state *state = strm->state;
215

216
    if (state->rsi_size == (size_t)(state->rsip - state->rsi_buffer)) {
217
        state->flush_output(strm);
218 219
        state->flush_start = state->rsi_buffer;
        state->rsip = state->rsi_buffer;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
220
    }
221 222
}

223
static inline void put_sample(struct aec_stream *strm, uint32_t s)
224 225 226
{
    struct internal_state *state = strm->state;

227
    *state->rsip++ = s;
228
    strm->avail_out -= state->bytes_per_sample;
229
    check_rsi_end(strm);
230 231
}

232 233 234 235 236 237
static inline void fill_acc(struct aec_stream *strm)
{
    int b = (63 - strm->state->bitp) >> 3;

    strm->avail_in -= b;
    strm->state->bitp += b << 3;
Moritz Hanke's avatar
Moritz Hanke committed
238 239 240 241 242 243 244 245 246

    switch (b) {
      case (7):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
      case (6):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
      case (5):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
      case (4):
247
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
Moritz Hanke's avatar
Moritz Hanke committed
248 249 250 251 252 253 254 255
      case (3):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
      case (2):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
      case (1):
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
    };

256 257
}

258
static inline uint32_t direct_get(struct aec_stream *strm, int n)
259 260
{
    /**
261
       Get n bit from input stream
262 263 264 265

       No checking whatsoever. Read bits are dumped.
     */

266
    struct internal_state *state = strm->state;
267

268 269
    if (state->bitp < n)
        fill_acc(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
270

271
    state->bitp -= n;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
272
    return (state->acc >> state->bitp) & ((UINT64_C(1) << n) - 1);
273 274
}

275
static inline uint32_t direct_get_fs(struct aec_stream *strm)
276 277 278 279
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
280
       Essentially counts the number of 0 bits until a 1 is
281
       encountered.
282 283
     */

284
    uint32_t fs = 0;
285 286 287 288
#if HAVE_DECL___BUILTIN_CLZLL
    int lz;
#elif HAVE_BSR64
    unsigned long lz;
289
#endif
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
290
    struct internal_state *state = strm->state;
291

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
292
    state->acc &= ((UINT64_C(1) << state->bitp) - 1);
293

294 295 296
    while (state->acc == 0) {
        fs += state->bitp;
        state->bitp = 0;
297
        fill_acc(strm);
298
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
299

Thomas Jahns's avatar
Thomas Jahns committed
300
#if HAVE_DECL___BUILTIN_CLZLL
301 302 303
    lz = __builtin_clzll(state->acc);
    fs += lz + state->bitp - 64;
    state->bitp = 63 - lz;
304 305 306 307
#elif HAVE_BSR64
    _BitScanReverse64(&lz, state->acc);
    fs += state->bitp - 1 - lz;
    state->bitp = lz;
308
#else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
309
    state->bitp--;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
310
    while ((state->acc & (UINT64_C(1) << state->bitp)) == 0) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
311
        state->bitp--;
312
        fs++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
313
    }
314
#endif
315 316 317
    return fs;
}

318
static inline uint32_t bits_ask(struct aec_stream *strm, int n)
319 320 321 322 323 324 325 326 327 328 329 330
{
    while (strm->state->bitp < n) {
        if (strm->avail_in == 0)
            return 0;
        strm->avail_in--;
        strm->state->acc <<= 8;
        strm->state->acc |= *strm->next_in++;
        strm->state->bitp += 8;
    }
    return 1;
}

331
static inline uint32_t bits_get(struct aec_stream *strm, int n)
332 333
{
    return (strm->state->acc >> (strm->state->bitp - n))
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
334
        & ((UINT64_C(1) << n) - 1);
335 336
}

337
static inline void bits_drop(struct aec_stream *strm, int n)
338 339 340 341
{
    strm->state->bitp -= n;
}

342
static inline uint32_t fs_ask(struct aec_stream *strm)
343 344 345
{
    if (bits_ask(strm, 1) == 0)
        return 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
346
    while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
347 348 349 350 351 352 353 354 355 356 357 358 359 360
        if (strm->state->bitp == 1) {
            if (strm->avail_in == 0)
                return 0;
            strm->avail_in--;
            strm->state->acc <<= 8;
            strm->state->acc |= *strm->next_in++;
            strm->state->bitp += 8;
        }
        strm->state->fs++;
        strm->state->bitp--;
    }
    return 1;
}

361
static inline void fs_drop(struct aec_stream *strm)
362 363 364 365 366
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

367
static inline uint32_t copysample(struct aec_stream *strm)
368
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
369
    if (bits_ask(strm, strm->bits_per_sample) == 0
370 371
        || strm->avail_out == 0)
        return 0;
372

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
373 374
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
375 376
    return 1;
}
377 378 379 380 381

static int m_id(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;

382 383
    if (state->rsip == state->rsi_buffer) {
        if(strm->flags & AEC_PAD_RSI)
384
            state->bitp -= state->bitp % 8;
385 386 387
        if (state->pp)
            state->ref = 1;
    } else {
388
        state->ref = 0;
389
    }
390 391 392 393
    if (bits_ask(strm, state->id_len) == 0)
        return M_EXIT;
    state->id = bits_get(strm, state->id_len);
    bits_drop(strm, state->id_len);
394 395 396 397 398 399 400 401 402 403 404
    state->mode = state->id_table[state->id];

    return M_CONTINUE;
}

static int m_split_output(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
    int k = state->id - 1;

    do {
405
        if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
406
            return M_EXIT;
407 408
        *state->rsip++ += bits_get(strm, k);
        strm->avail_out -= state->bytes_per_sample;
409
        bits_drop(strm, k);
410
    } while(++state->i < state->n);
411

412
    check_rsi_end(strm);
413 414 415 416 417 418 419
    state->mode = m_id;
    return M_CONTINUE;
}

static int m_split_fs(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
420
    int k = state->id - 1;
421 422

    do {
423 424
        if (fs_ask(strm) == 0)
            return M_EXIT;
425
        state->rsip[state->i] = state->fs << k;
426
        fs_drop(strm);
427
    } while(++state->i < state->n);
428

429
    state->i = 0;
430 431 432 433 434 435
    state->mode = m_split_output;
    return M_CONTINUE;
}

static int m_split(struct aec_stream *strm)
{
436
    uint32_t i, k;
437 438
    struct internal_state *state = strm->state;

439 440 441 442
    if (BUFFERSPACE(strm)) {
        k = state->id - 1;

        if (state->ref)
443
            *state->rsip++ = direct_get(strm, strm->bits_per_sample);
444

445
        for (i = 0; i < strm->block_size - state->ref; i++)
446
            state->rsip[i] = direct_get_fs(strm) << k;
447

448
        for (i = state->ref; i < strm->block_size; i++)
449
            *state->rsip++ += direct_get(strm, k);
450

451
        strm->avail_out -= state->out_blklen;
452
        check_rsi_end(strm);
453

454 455 456 457 458
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
459 460
        if (copysample(strm) == 0)
            return M_EXIT;
461 462 463 464 465
        state->n = strm->block_size - 1;
    } else {
        state->n = strm->block_size;
    }

466
    state->i = 0;
467 468 469 470 471 472 473 474
    state->mode = m_split_fs;
    return M_CONTINUE;
}

static int m_zero_output(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;

475 476 477 478 479
    do {
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
480 481 482 483 484 485 486

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_zero_block(struct aec_stream *strm)
{
487
    uint32_t i, zero_blocks, b, zero_bytes;
488 489
    struct internal_state *state = strm->state;

490 491
    if (fs_ask(strm) == 0)
        return M_EXIT;
492
    zero_blocks = state->fs + 1;
493
    fs_drop(strm);
494 495

    if (zero_blocks == ROS) {
496
        b = (int)(state->rsip - state->rsi_buffer) / strm->block_size;
497 498 499 500 501 502
        zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

    if (state->ref)
503
        i = zero_blocks * strm->block_size - 1;
504
    else
505
        i = zero_blocks * strm->block_size;
506

507
    zero_bytes = i * state->bytes_per_sample;
508

509
    if (strm->avail_out >= zero_bytes) {
510 511 512
        if (state->rsi_size - (state->rsip - state->rsi_buffer) < i)
            return M_ERROR;

513 514
        memset(state->rsip, 0, i * sizeof(uint32_t));
        state->rsip += i;
515
        strm->avail_out -= zero_bytes;
516
        check_rsi_end(strm);
517

518 519 520 521
        state->mode = m_id;
        return M_CONTINUE;
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
522
    state->i = i;
523 524 525 526 527 528
    state->mode = m_zero_output;
    return M_CONTINUE;
}

static int m_se_decode(struct aec_stream *strm)
{
529
    int32_t m, d1;
530 531 532
    struct internal_state *state = strm->state;

    while(state->i < strm->block_size) {
533 534
        if (fs_ask(strm) == 0)
            return M_EXIT;
535
        m = state->fs;
536
        d1 = m - state->se_table[2 * m + 1];
537 538

        if ((state->i & 1) == 0) {
539 540 541
            if (strm->avail_out == 0)
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
542 543 544
            state->i++;
        }

545 546 547
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, d1);
548
        state->i++;
549
        fs_drop(strm);
550 551 552 553 554 555 556 557
    }

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_se(struct aec_stream *strm)
{
558
    uint32_t i;
559
    int32_t m, d1;
560 561
    struct internal_state *state = strm->state;

562 563 564 565 566 567 568 569 570 571 572 573 574 575
    if (BUFFERSPACE(strm)) {
        i = state->ref;

        while (i < strm->block_size) {
            m = direct_get_fs(strm);
            d1 = m - state->se_table[2 * m + 1];

            if ((i & 1) == 0) {
                put_sample(strm, state->se_table[2 * m] - d1);
                i++;
            }
            put_sample(strm, d1);
            i++;
        }
576 577 578 579 580 581 582 583 584 585 586 587 588
        state->mode = m_id;
        return M_CONTINUE;
    }

    state->mode = m_se_decode;
    state->i = state->ref;
    return M_CONTINUE;
}

static int m_low_entropy_ref(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;

589 590
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
591 592 593 594 595 596 597 598 599 600 601 602 603 604

    if(state->id == 1) {
        state->mode = m_se;
        return M_CONTINUE;
    }

    state->mode = m_zero_block;
    return M_CONTINUE;
}

static int m_low_entropy(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;

605 606 607 608
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
609 610 611 612 613 614 615 616
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

static int m_uncomp_copy(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;

617 618 619 620
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
621 622 623 624 625 626 627

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_uncomp(struct aec_stream *strm)
{
628
    uint32_t i;
629 630
    struct internal_state *state = strm->state;

631
    if (BUFFERSPACE(strm)) {
632
        for (i = 0; i < strm->block_size; i++)
633
            *state->rsip++ = direct_get(strm, strm->bits_per_sample);
634
        strm->avail_out -= state->out_blklen;
635
        check_rsi_end(strm);
636

637 638 639 640 641 642 643 644 645
        state->mode = m_id;
        return M_CONTINUE;
    }

    state->i = strm->block_size;
    state->mode = m_uncomp_copy;
    return M_CONTINUE;
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
static void create_se_table(int *table)
{
    int i, j, k, ms;

    k = 0;
    for (i = 0; i < 13; i++) {
        ms = k;
        for (j = 0; j <= i; j++) {
            table[2 * k] = i;
            table[2 * k + 1] = ms;
            k++;
        }
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
661
int aec_decode_init(struct aec_stream *strm)
662
{
663
    int i, modi;
664
    struct internal_state *state;
665

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
666
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
667
        return AEC_CONF_ERROR;
668

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
669
    state = malloc(sizeof(struct internal_state));
670
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
671
        return AEC_MEM_ERROR;
672
    memset(state, 0, sizeof(struct internal_state));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
673

674 675
    create_se_table(state->se_table);

676 677
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
678
    if (strm->bits_per_sample > 16) {
679
        state->id_len = 5;
680

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
681 682
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
683
            if (strm->flags & AEC_DATA_MSB)
684
                state->flush_output = flush_msb_24;
685
            else
686
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
687
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
688
            state->bytes_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
689
            if (strm->flags & AEC_DATA_MSB)
690
                state->flush_output = flush_msb_32;
691
            else
692
                state->flush_output = flush_lsb_32;
693 694
        }
        state->out_blklen = strm->block_size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
695
            * state->bytes_per_sample;
696
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
697 698
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
699
        state->id_len = 4;
700
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
701
        if (strm->flags & AEC_DATA_MSB)
702
            state->flush_output = flush_msb_16;
703
        else
704
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
705
    } else {
706
        if (strm->flags & AEC_RESTRICTED) {
707 708 709 710 711 712 713 714 715 716 717 718
            if (strm->bits_per_sample <= 4) {
                if (strm->bits_per_sample <= 2)
                    state->id_len = 1;
                else
                    state->id_len = 2;
            } else {
                return AEC_CONF_ERROR;
            }
        } else {
            state->id_len = 3;
        }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
719
        state->bytes_per_sample = 1;
720
        state->out_blklen = strm->block_size;
721
        state->flush_output = flush_8;
722 723
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
724
    if (strm->flags & AEC_DATA_SIGNED) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
725 726
        state->xmin = -(INT64_C(1) << (strm->bits_per_sample - 1));
        state->xmax = (UINT64_C(1) << (strm->bits_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
727
    } else {
728
        state->xmin = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
729
        state->xmax = (UINT64_C(1) << strm->bits_per_sample) - 1;
730
    }
731

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
732
    state->in_blklen = (strm->block_size * strm->bits_per_sample
733
                        + state->id_len) / 8 + 9;
734 735

    modi = 1UL << state->id_len;
736
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
737
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
738
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
739

740
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
741
    for (i = 1; i < modi - 1; i++) {
742
        state->id_table[i] = m_split;
743
    }
744
    state->id_table[modi - 1] = m_uncomp;
745

746 747 748
    state->rsi_size = strm->rsi * strm->block_size;
    state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
    if (state->rsi_buffer == NULL)
749 750
        return AEC_MEM_ERROR;

751
    state->ref = 0;
752 753 754
    strm->total_in = 0;
    strm->total_out = 0;

755 756
    state->rsip = state->rsi_buffer;
    state->flush_start = state->rsi_buffer;
757
    state->bitp = 0;
758
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
759
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
760
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
761
    return AEC_OK;
762 763
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
764
int aec_decode(struct aec_stream *strm, int flush)
765
{
766 767 768 769 770 771 772 773 774
    /**
       Finite-state machine implementation of the adaptive entropy
       decoder.

       Can work with one byte input und one sample output buffers. If
       enough buffer space is available, then faster implementations
       of the states are called. Inspired by zlib.
    */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
775
    struct internal_state *state = strm->state;
776
    int status;
777

778 779 780
    strm->total_in += strm->avail_in;
    strm->total_out += strm->avail_out;

781 782 783 784 785 786 787
    do {
        status = state->mode(strm);
    } while (status == M_CONTINUE);

    if (status == M_ERROR)
        return AEC_DATA_ERROR;

788
    state->flush_output(strm);
789 790 791 792

    strm->total_in -= strm->avail_in;
    strm->total_out -= strm->avail_out;

793 794
    return AEC_OK;
}
795

796 797
int aec_decode_end(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
798
    struct internal_state *state = strm->state;
799

800
    free(state->id_table);
801
    free(state->rsi_buffer);
802
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
803
    return AEC_OK;
804
}
805

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
806
int aec_buffer_decode(struct aec_stream *strm)
807 808 809 810 811 812 813 814 815 816 817
{
    int status;

    status = aec_decode_init(strm);
    if (status != AEC_OK)
        return status;

    status = aec_decode(strm, AEC_FLUSH);
    aec_decode_end(strm);
    return status;
}