decode.c 26 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 - 2016
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>

55
#include "decode.h"
56
#include "libaec.h"
57

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

62
#define ROS 5
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
63
#define RSI_USED(state) ((size_t)(state->rsip - state->rsi_buffer))
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
64
#define BUFFERSPACE(strm) (strm->avail_in >= strm->state->in_blklen      \
65 66
                           && strm->avail_out >= strm->state->out_blklen)

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
143 144 145

static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
{
146 147 148 149
    *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
150 151 152 153
}

static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
{
154 155 156
    *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
157 158 159 160
}

static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
{
161 162
    *strm->next_out++ = (unsigned char)(data >> 8);
    *strm->next_out++ = (unsigned char)data;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163 164 165 166
}

static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
{
167 168 169 170
    *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
171 172 173 174
}

static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
{
175 176 177
    *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
178 179 180 181
}

static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
{
182 183
    *strm->next_out++ = (unsigned char)data;
    *strm->next_out++ = (unsigned char)(data >> 8);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
184 185 186 187
}

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191 192 193 194 195 196 197
FLUSH(msb_32)
FLUSH(msb_24)
FLUSH(msb_16)
FLUSH(lsb_32)
FLUSH(lsb_24)
FLUSH(lsb_16)
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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
206
    if (state->rsi_size == RSI_USED(state)) {
207
        state->flush_output(strm);
208 209
        state->flush_start = state->rsi_buffer;
        state->rsip = state->rsi_buffer;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
210
    }
211 212
}

213
static inline void put_sample(struct aec_stream *strm, uint32_t s)
214 215 216
{
    struct internal_state *state = strm->state;

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 228 229

       No checking whatsoever. Read bits are dumped.
     */

230
    struct internal_state *state = strm->state;
231
    int b;
232
    if (state->bitp < n)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    {
        b = (63 - state->bitp) >> 3;
        if (b == 6) {
            state->acc = (state->acc << 48)
                | ((uint64_t)strm->next_in[0] << 40)
                | ((uint64_t)strm->next_in[1] << 32)
                | ((uint64_t)strm->next_in[2] << 24)
                | ((uint64_t)strm->next_in[3] << 16)
                | ((uint64_t)strm->next_in[4] << 8)
                | (uint64_t)strm->next_in[5];
        } else if (b == 7) {
            state->acc = (state->acc << 56)
                | ((uint64_t)strm->next_in[0] << 48)
                | ((uint64_t)strm->next_in[1] << 40)
                | ((uint64_t)strm->next_in[2] << 32)
                | ((uint64_t)strm->next_in[3] << 24)
                | ((uint64_t)strm->next_in[4] << 16)
                | ((uint64_t)strm->next_in[5] << 8)
                | (uint64_t)strm->next_in[6];
        } else if (b == 5) {
            state->acc = (state->acc << 40)
                | ((uint64_t)strm->next_in[0] << 32)
                | ((uint64_t)strm->next_in[1] << 24)
                | ((uint64_t)strm->next_in[2] << 16)
                | ((uint64_t)strm->next_in[3] << 8)
                | (uint64_t)strm->next_in[4];
        } else if (b == 4) {
            state->acc = (state->acc << 32)
                | ((uint64_t)strm->next_in[0] << 24)
                | ((uint64_t)strm->next_in[1] << 16)
                | ((uint64_t)strm->next_in[2] << 8)
                | (uint64_t)strm->next_in[3];
        } else if (b == 3) {
            state->acc = (state->acc << 24)
                | ((uint64_t)strm->next_in[0] << 16)
                | ((uint64_t)strm->next_in[1] << 8)
                | (uint64_t)strm->next_in[2];
        } else if (b == 2) {
            state->acc = (state->acc << 16)
                | ((uint64_t)strm->next_in[0] << 8)
                | (uint64_t)strm->next_in[1];
        } else if (b == 1) {
            state->acc = (state->acc << 8)
                | (uint64_t)strm->next_in[0];
        }
        strm->next_in += b;
        strm->avail_in -= b;
        state->bitp += b << 3;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
282

283
    state->bitp -= n;
284
    return (state->acc >> state->bitp) & (UINT64_MAX >> (64 - n));
285 286
}

287
static inline uint32_t direct_get_fs(struct aec_stream *strm)
288 289 290 291
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
292
       Essentially counts the number of 0 bits until a 1 is
293
       encountered.
294 295
     */

296
    uint32_t fs = 0;
297 298 299 300
#if HAVE_BSR64
    unsigned long i;
#else
    int i;
301
#endif
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
302
    struct internal_state *state = strm->state;
303

304 305 306 307
    if (state->bitp)
        state->acc &= UINT64_MAX >> (64 - state->bitp);
    else
        state->acc = 0;
308

309
    while (state->acc == 0) {
310 311 312
        if (strm->avail_in < 7)
            return 0;

313 314 315 316 317 318 319 320 321 322
        state->acc = (state->acc << 56)
            | ((uint64_t)strm->next_in[0] << 48)
            | ((uint64_t)strm->next_in[1] << 40)
            | ((uint64_t)strm->next_in[2] << 32)
            | ((uint64_t)strm->next_in[3] << 24)
            | ((uint64_t)strm->next_in[4] << 16)
            | ((uint64_t)strm->next_in[5] << 8)
            | (uint64_t)strm->next_in[6];
        strm->next_in += 7;
        strm->avail_in -= 7;
323
        fs += state->bitp;
324
        state->bitp = 56;
325
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
326

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
327 328 329 330
#ifndef __has_builtin
#define __has_builtin(x) 0  /* Compatibility with non-clang compilers. */
#endif
#if HAVE_DECL___BUILTIN_CLZLL || __has_builtin(__builtin_clzll)
331
    i = 63 - __builtin_clzll(state->acc);
332
#elif HAVE_BSR64
333
    _BitScanReverse64(&i, state->acc);
334
#else
335 336 337
    i = state->bitp - 1;
    while ((state->acc & (UINT64_C(1) << i)) == 0)
        i--;
338
#endif
339 340
    fs += state->bitp - i - 1;
    state->bitp = i;
341 342 343
    return fs;
}

344
static inline uint32_t bits_ask(struct aec_stream *strm, int n)
345 346 347 348 349 350 351 352 353 354 355 356
{
    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;
}

357
static inline uint32_t bits_get(struct aec_stream *strm, int n)
358 359
{
    return (strm->state->acc >> (strm->state->bitp - n))
360
        & (UINT64_MAX >> (64 - n));
361 362
}

363
static inline void bits_drop(struct aec_stream *strm, int n)
364 365 366 367
{
    strm->state->bitp -= n;
}

368
static inline uint32_t fs_ask(struct aec_stream *strm)
369 370 371
{
    if (bits_ask(strm, 1) == 0)
        return 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
372
    while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
373 374 375 376 377 378 379 380 381 382 383 384 385 386
        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;
}

387
static inline void fs_drop(struct aec_stream *strm)
388 389 390 391 392
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

393
static inline uint32_t copysample(struct aec_stream *strm)
394
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
395
    if (bits_ask(strm, strm->bits_per_sample) == 0
396
        || strm->avail_out < strm->state->bytes_per_sample)
397
        return 0;
398

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
399 400
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
401 402
    return 1;
}
403 404 405 406 407

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

408 409
    if (state->rsip == state->rsi_buffer) {
        if(strm->flags & AEC_PAD_RSI)
410
            state->bitp -= state->bitp % 8;
411 412 413
        if (state->pp)
            state->ref = 1;
    } else {
414
        state->ref = 0;
415
    }
416 417 418 419
    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);
420 421 422 423 424 425 426 427 428 429 430
    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 {
431
        if (bits_ask(strm, k) == 0 || strm->avail_out < state->bytes_per_sample)
432
            return M_EXIT;
433 434 435 436
        if (k)
            *state->rsip++ += bits_get(strm, k);
        else
            state->rsip++;
437
        strm->avail_out -= state->bytes_per_sample;
438
        bits_drop(strm, k);
439
    } while(++state->i < state->n);
440

441
    check_rsi_end(strm);
442 443 444 445 446 447 448
    state->mode = m_id;
    return M_CONTINUE;
}

static int m_split_fs(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
449
    int k = state->id - 1;
450 451

    do {
452 453
        if (fs_ask(strm) == 0)
            return M_EXIT;
454
        state->rsip[state->i] = state->fs << k;
455
        fs_drop(strm);
456
    } while(++state->i < state->n);
457

458
    state->i = 0;
459
    state->mode = m_split_output;
460

461 462 463 464 465
    return M_CONTINUE;
}

static int m_split(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
466 467
    size_t i;
    int k;
468 469
    struct internal_state *state = strm->state;

470 471 472 473
    if (BUFFERSPACE(strm)) {
        k = state->id - 1;

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

476
        for (i = 0; i < strm->block_size - state->ref; i++)
477
            state->rsip[i] = direct_get_fs(strm) << k;
478

479
        if (k) {
480 481 482
            if (strm->avail_in < (k * strm->block_size) / 8 + 9)
                return M_ERROR;

483 484 485 486 487
            for (i = state->ref; i < strm->block_size; i++)
                *state->rsip++ += direct_get(strm, k);
        } else {
            state->rsip += strm->block_size - state->ref;
        }
488

489
        strm->avail_out -= state->out_blklen;
490
        check_rsi_end(strm);
491

492 493 494 495 496
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
497 498
        if (copysample(strm) == 0)
            return M_EXIT;
499 500 501 502 503
        state->n = strm->block_size - 1;
    } else {
        state->n = strm->block_size;
    }

504
    state->i = 0;
505 506 507 508 509 510 511 512
    state->mode = m_split_fs;
    return M_CONTINUE;
}

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

513
    do {
514
        if (strm->avail_out < state->bytes_per_sample)
515 516 517
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
518 519 520 521 522 523 524

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_zero_block(struct aec_stream *strm)
{
525
    uint32_t i, zero_blocks, b, zero_bytes;
526 527
    struct internal_state *state = strm->state;

528 529
    if (fs_ask(strm) == 0)
        return M_EXIT;
530
    zero_blocks = state->fs + 1;
531
    fs_drop(strm);
532 533

    if (zero_blocks == ROS) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
534
        b = (int)RSI_USED(state) / strm->block_size;
535 536 537 538 539 540
        zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

    if (state->ref)
541
        i = zero_blocks * strm->block_size - 1;
542
    else
543
        i = zero_blocks * strm->block_size;
544

545
    zero_bytes = i * state->bytes_per_sample;
546

547
    if (strm->avail_out >= zero_bytes) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
548
        if (state->rsi_size - RSI_USED(state) < i)
549 550
            return M_ERROR;

551 552
        memset(state->rsip, 0, i * sizeof(uint32_t));
        state->rsip += i;
553
        strm->avail_out -= zero_bytes;
554
        check_rsi_end(strm);
555

556 557 558 559
        state->mode = m_id;
        return M_CONTINUE;
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
560
    state->i = i;
561 562 563 564 565 566
    state->mode = m_zero_output;
    return M_CONTINUE;
}

static int m_se_decode(struct aec_stream *strm)
{
567
    int32_t m, d1;
568 569 570
    struct internal_state *state = strm->state;

    while(state->i < strm->block_size) {
571 572
        if (fs_ask(strm) == 0)
            return M_EXIT;
573
        m = state->fs;
574 575
        if (m > SE_TABLE_SIZE)
            return M_ERROR;
576
        d1 = m - state->se_table[2 * m + 1];
577 578

        if ((state->i & 1) == 0) {
579
            if (strm->avail_out < state->bytes_per_sample)
580 581
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
582 583 584
            state->i++;
        }

585
        if (strm->avail_out < state->bytes_per_sample)
586 587
            return M_EXIT;
        put_sample(strm, d1);
588
        state->i++;
589
        fs_drop(strm);
590 591 592 593 594 595 596 597
    }

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_se(struct aec_stream *strm)
{
598
    uint32_t i;
599 600
    uint32_t m;
    int32_t d1;
601 602
    struct internal_state *state = strm->state;

603 604 605 606 607
    if (BUFFERSPACE(strm)) {
        i = state->ref;

        while (i < strm->block_size) {
            m = direct_get_fs(strm);
608 609 610 611

            if (m > SE_TABLE_SIZE)
                return M_ERROR;

612 613 614 615 616 617 618 619 620
            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++;
        }
621 622 623 624 625 626 627 628 629 630 631 632 633
        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;

634 635
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
636 637 638 639 640 641 642 643 644 645 646 647 648 649

    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;

650 651 652 653
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
654 655 656 657 658 659 660 661
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

662 663 664 665
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
666 667 668 669 670 671 672

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_uncomp(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
673
    size_t i;
674 675
    struct internal_state *state = strm->state;

676
    if (BUFFERSPACE(strm)) {
677
        for (i = 0; i < strm->block_size; i++)
678
            *state->rsip++ = direct_get(strm, strm->bits_per_sample);
679
        strm->avail_out -= state->out_blklen;
680
        check_rsi_end(strm);
681

682 683 684 685 686 687 688 689 690
        state->mode = m_id;
        return M_CONTINUE;
    }

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

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
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
706
int aec_decode_init(struct aec_stream *strm)
707
{
708
    int i, modi;
709
    struct internal_state *state;
710

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
711
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
712
        return AEC_CONF_ERROR;
713

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
714
    state = malloc(sizeof(struct internal_state));
715
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
716
        return AEC_MEM_ERROR;
717
    memset(state, 0, sizeof(struct internal_state));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
718

719 720
    create_se_table(state->se_table);

721 722
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
723
    if (strm->bits_per_sample > 16) {
724
        state->id_len = 5;
725

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
726 727
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
728
            if (strm->flags & AEC_DATA_MSB)
729
                state->flush_output = flush_msb_24;
730
            else
731
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
732
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
733
            state->bytes_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
734
            if (strm->flags & AEC_DATA_MSB)
735
                state->flush_output = flush_msb_32;
736
            else
737
                state->flush_output = flush_lsb_32;
738 739
        }
        state->out_blklen = strm->block_size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
740
            * state->bytes_per_sample;
741
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
742 743
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
744
        state->id_len = 4;
745
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
746
        if (strm->flags & AEC_DATA_MSB)
747
            state->flush_output = flush_msb_16;
748
        else
749
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
750
    } else {
751
        if (strm->flags & AEC_RESTRICTED) {
752 753 754 755 756 757 758 759 760 761 762 763
            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
764
        state->bytes_per_sample = 1;
765
        state->out_blklen = strm->block_size;
766
        state->flush_output = flush_8;
767 768
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
769
    if (strm->flags & AEC_DATA_SIGNED) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
770 771
        state->xmax = UINT32_MAX >> (32 - strm->bits_per_sample + 1);
        state->xmin = ~state->xmax;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
772
    } else {
773
        state->xmin = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
774
        state->xmax = UINT32_MAX >> (32 - strm->bits_per_sample);
775
    }
776

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
777
    state->in_blklen = (strm->block_size * strm->bits_per_sample
778
                        + state->id_len) / 8 + 16;
779 780

    modi = 1UL << state->id_len;
781
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
782
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
783
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
784

785
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
786
    for (i = 1; i < modi - 1; i++) {
787
        state->id_table[i] = m_split;
788
    }
789
    state->id_table[modi - 1] = m_uncomp;
790

791 792 793
    state->rsi_size = strm->rsi * strm->block_size;
    state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
    if (state->rsi_buffer == NULL)
794 795
        return AEC_MEM_ERROR;

796
    state->ref = 0;
797 798 799
    strm->total_in = 0;
    strm->total_out = 0;

800 801
    state->rsip = state->rsi_buffer;
    state->flush_start = state->rsi_buffer;
802
    state->bitp = 0;
803
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
804
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
805
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
806
    return AEC_OK;
807 808
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
809
int aec_decode(struct aec_stream *strm, int flush)
810
{
811 812 813 814 815 816 817 818 819
    /**
       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
820
    struct internal_state *state = strm->state;
821
    int status;
822

823 824 825
    strm->total_in += strm->avail_in;
    strm->total_out += strm->avail_out;

826 827 828 829 830 831 832
    do {
        status = state->mode(strm);
    } while (status == M_CONTINUE);

    if (status == M_ERROR)
        return AEC_DATA_ERROR;

833 834 835 836
    if (status == M_EXIT && strm->avail_out > 0 &&
        strm->avail_out < state->bytes_per_sample)
        return AEC_MEM_ERROR;

837
    state->flush_output(strm);
838 839 840 841

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

842 843
    return AEC_OK;
}
844

845 846
int aec_decode_end(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
847
    struct internal_state *state = strm->state;
848

849
    free(state->id_table);
850
    free(state->rsi_buffer);
851
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
852
    return AEC_OK;
853
}
854

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
855
int aec_buffer_decode(struct aec_stream *strm)
856 857 858 859 860 861 862 863 864 865 866
{
    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;
}