decode.c 26.3 KB
Newer Older
1 2
/**
 * @file decode.c
3 4
 *
 * @section LICENSE
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
5
 * Copyright 2012 - 2019
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.
 *
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
48 49
 *
 */
50

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
51 52 53
#include "config.h"
#include "decode.h"
#include "libaec.h"
54 55 56 57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
141 142 143

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

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

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

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

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

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

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

189 190 191 192 193 194 195
FLUSH(msb_32)
FLUSH(msb_24)
FLUSH(msb_16)
FLUSH(lsb_32)
FLUSH(lsb_24)
FLUSH(lsb_16)
FLUSH(8)
196

197
static inline void put_sample(struct aec_stream *strm, uint32_t s)
198 199 200
{
    struct internal_state *state = strm->state;

201
    *state->rsip++ = s;
202
    strm->avail_out -= state->bytes_per_sample;
203 204
}

205
static inline uint32_t direct_get(struct aec_stream *strm, int n)
206 207
{
    /**
208
       Get n bit from input stream
209 210 211 212

       No checking whatsoever. Read bits are dumped.
     */

213
    struct internal_state *state = strm->state;
214
    if (state->bitp < n)
215
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
216
        int b = (63 - state->bitp) >> 3;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 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
        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;
    }
264

265
    state->bitp -= n;
266
    return (state->acc >> state->bitp) & (UINT64_MAX >> (64 - n));
267 268
}

269
static inline uint32_t direct_get_fs(struct aec_stream *strm)
270 271 272 273
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

274
       Essentially counts the number of 0 bits until a 1 is
275
       encountered.
276 277
     */

278
    uint32_t fs = 0;
279
    struct internal_state *state = strm->state;
280

281 282 283 284
    if (state->bitp)
        state->acc &= UINT64_MAX >> (64 - state->bitp);
    else
        state->acc = 0;
285

286
    while (state->acc == 0) {
287 288 289
        if (strm->avail_in < 7)
            return 0;

290 291 292 293 294 295 296 297 298 299
        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;
300
        fs += state->bitp;
301
        state->bitp = 56;
302
    }
303

304
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
305 306 307 308
#ifndef __has_builtin
#define __has_builtin(x) 0  /* Compatibility with non-clang compilers. */
#endif
#if HAVE_DECL___BUILTIN_CLZLL || __has_builtin(__builtin_clzll)
309
        int i = 63 - __builtin_clzll(state->acc);
310
#elif HAVE_BSR64
311 312
        unsigned long i;
        _BitScanReverse64(&i, state->acc);
313
#else
314 315 316
        int i = state->bitp - 1;
        while ((state->acc & (UINT64_C(1) << i)) == 0)
            i--;
317
#endif
318 319 320
        fs += state->bitp - i - 1;
        state->bitp = i;
    }
321 322 323
    return fs;
}

324
static inline uint32_t bits_ask(struct aec_stream *strm, int n)
325 326 327 328 329 330 331 332 333 334 335 336
{
    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;
}

337
static inline uint32_t bits_get(struct aec_stream *strm, int n)
338 339
{
    return (strm->state->acc >> (strm->state->bitp - n))
340
        & (UINT64_MAX >> (64 - n));
341 342
}

343
static inline void bits_drop(struct aec_stream *strm, int n)
344 345 346 347
{
    strm->state->bitp -= n;
}

348
static inline uint32_t fs_ask(struct aec_stream *strm)
349 350 351
{
    if (bits_ask(strm, 1) == 0)
        return 0;
352
    while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
353 354 355 356 357 358 359 360 361 362 363 364 365 366
        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;
}

367
static inline void fs_drop(struct aec_stream *strm)
368 369 370 371 372
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

373
static inline uint32_t copysample(struct aec_stream *strm)
374
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
375
    if (bits_ask(strm, strm->bits_per_sample) == 0
376
        || strm->avail_out < strm->state->bytes_per_sample)
377
        return 0;
378

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
379 380
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
381 382
    return 1;
}
383

384
static inline int m_id(struct aec_stream *strm)
385 386
{
    struct internal_state *state = strm->state;
387 388 389 390 391 392 393 394 395 396 397 398 399
    if (strm->avail_in >= strm->state->in_blklen) {
        state->id = direct_get(strm, state->id_len);
    } else {
        if (bits_ask(strm, state->id_len) == 0) {
            state->mode = m_id;
            return M_EXIT;
        }
        state->id = bits_get(strm, state->id_len);
        bits_drop(strm, state->id_len);
    }
    state->mode = state->id_table[state->id];
    return(state->mode(strm));
}
400

401 402 403 404
static int m_next_cds(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
    if (state->rsi_size == RSI_USED_SIZE(state)) {
405 406 407 408
        state->flush_output(strm);
        state->flush_start = state->rsi_buffer;
        state->rsip = state->rsi_buffer;
        if (state->pp) {
409
            state->ref = 1;
410 411
            state->encoded_block_size = strm->block_size - 1;
        }
412 413
        if (strm->flags & AEC_PAD_RSI)
            state->bitp -= state->bitp % 8;
414
    } else {
415
        state->ref = 0;
416
        state->encoded_block_size = strm->block_size;
417
    }
418
    return m_id(strm);
419 420 421 422 423 424 425 426
}

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

    do {
427
        if (bits_ask(strm, k) == 0 || strm->avail_out < state->bytes_per_sample)
428
            return M_EXIT;
429 430 431 432
        if (k)
            *state->rsip++ += bits_get(strm, k);
        else
            state->rsip++;
433
        strm->avail_out -= state->bytes_per_sample;
434
        bits_drop(strm, k);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
435
    } while(++state->sample_counter < state->encoded_block_size);
436

437
    state->mode = m_next_cds;
438 439 440 441 442 443
    return M_CONTINUE;
}

static int m_split_fs(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
444
    int k = state->id - 1;
445 446

    do {
447 448
        if (fs_ask(strm) == 0)
            return M_EXIT;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
449
        state->rsip[state->sample_counter] = state->fs << k;
450
        fs_drop(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
451
    } while(++state->sample_counter < state->encoded_block_size);
452

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
453
    state->sample_counter = 0;
454
    state->mode = m_split_output;
455

456 457 458 459 460 461 462
    return M_CONTINUE;
}

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

463
    if (BUFFERSPACE(strm)) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
464 465
        int k = state->id - 1;
        size_t binary_part = (k * state->encoded_block_size) / 8 + 9;
466 467

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
470
        for (size_t i = 0; i < state->encoded_block_size; i++)
471
            state->rsip[i] = direct_get_fs(strm) << k;
472

473
        if (k) {
474
            if (strm->avail_in < binary_part)
475 476
                return M_ERROR;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
477
            for (size_t i = 0; i < state->encoded_block_size; i++)
478 479
                *state->rsip++ += direct_get(strm, k);
        } else {
480
            state->rsip += state->encoded_block_size;
481
        }
482

483
        strm->avail_out -= state->out_blklen;
484
        state->mode = m_next_cds;
485
    } else {
486 487
        if (state->ref && (copysample(strm) == 0))
            return M_EXIT;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
488
        state->sample_counter = 0;
489
        state->mode = m_split_fs;
490 491 492 493 494 495 496 497
    }
    return M_CONTINUE;
}

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

498
    do {
499
        if (strm->avail_out < state->bytes_per_sample)
500 501
            return M_EXIT;
        put_sample(strm, 0);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
502
    } while(--state->sample_counter);
503

504
    state->mode = m_next_cds;
505 506 507 508 509 510
    return M_CONTINUE;
}

static int m_zero_block(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
511 512 513
    uint32_t zero_blocks;
    uint32_t zero_samples;
    uint32_t zero_bytes;
514

515 516
    if (fs_ask(strm) == 0)
        return M_EXIT;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
517

518
    zero_blocks = state->fs + 1;
519
    fs_drop(strm);
520 521

    if (zero_blocks == ROS) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
522
        int b = (int)RSI_USED_SIZE(state) / strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
523
        zero_blocks = MIN((int)(strm->rsi - b), 64 - (b % 64));
524 525 526 527
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
528 529
    zero_samples = zero_blocks * strm->block_size - state->ref;
    if (state->rsi_size - RSI_USED_SIZE(state) < zero_samples)
530
        return M_ERROR;
531

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
532
    zero_bytes = zero_samples * state->bytes_per_sample;
533
    if (strm->avail_out >= zero_bytes) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
534 535
        memset(state->rsip, 0, zero_samples * sizeof(uint32_t));
        state->rsip += zero_samples;
536
        strm->avail_out -= zero_bytes;
537
        state->mode = m_next_cds;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
538 539 540
    } else {
        state->sample_counter = zero_samples;
        state->mode = m_zero_output;
541 542 543 544 545 546 547 548
    }
    return M_CONTINUE;
}

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
549 550 551
    while(state->sample_counter < strm->block_size) {
        int32_t m;
        int32_t d1;
552 553
        if (fs_ask(strm) == 0)
            return M_EXIT;
554
        m = state->fs;
555 556
        if (m > SE_TABLE_SIZE)
            return M_ERROR;
557
        d1 = m - state->se_table[2 * m + 1];
558

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
559
        if ((state->sample_counter & 1) == 0) {
560
            if (strm->avail_out < state->bytes_per_sample)
561 562
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
563
            state->sample_counter++;
564 565
        }

566
        if (strm->avail_out < state->bytes_per_sample)
567 568
            return M_EXIT;
        put_sample(strm, d1);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
569
        state->sample_counter++;
570
        fs_drop(strm);
571 572
    }

573
    state->mode = m_next_cds;
574 575 576 577 578 579 580
    return M_CONTINUE;
}

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

581
    if (BUFFERSPACE(strm)) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
582
        uint32_t i = state->ref;
583 584

        while (i < strm->block_size) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
585 586
            uint32_t m = direct_get_fs(strm);
            int32_t d1;
587 588 589 590

            if (m > SE_TABLE_SIZE)
                return M_ERROR;

591 592 593 594 595 596 597 598 599
            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++;
        }
600
        state->mode = m_next_cds;
601 602
    } else {
        state->mode = m_se_decode;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
603
        state->sample_counter = state->ref;
604 605 606 607 608 609 610 611
    }
    return M_CONTINUE;
}

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

612 613
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
614

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
615
    if (state->id == 1)
616
        state->mode = m_se;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
617 618
    else
        state->mode = m_zero_block;
619 620 621 622 623 624 625
    return M_CONTINUE;
}

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

626 627 628 629
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
630 631 632 633 634 635 636 637
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

638 639 640
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
641
    } while(--state->sample_counter);
642

643
    state->mode = m_next_cds;
644 645 646 647 648 649 650
    return M_CONTINUE;
}

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

651
    if (BUFFERSPACE(strm)) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
652
        for (size_t i = 0; i < strm->block_size; i++)
653
            *state->rsip++ = direct_get(strm, strm->bits_per_sample);
654
        strm->avail_out -= state->out_blklen;
655
        state->mode = m_next_cds;
656
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
657
        state->sample_counter = strm->block_size;
658
        state->mode = m_uncomp_copy;
659 660 661 662
    }
    return M_CONTINUE;
}

663 664
static void create_se_table(int *table)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
665 666 667 668
    int k = 0;
    for (int i = 0; i < 13; i++) {
        int ms = k;
        for (int j = 0; j <= i; j++) {
669 670 671 672 673 674 675
            table[2 * k] = i;
            table[2 * k + 1] = ms;
            k++;
        }
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
676
int aec_decode_init(struct aec_stream *strm)
677
{
678
    struct internal_state *state;
679
    int modi;
680

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
681
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
682
        return AEC_CONF_ERROR;
683

684
    state = malloc(sizeof(struct internal_state));
685
    if (state == NULL)
686
        return AEC_MEM_ERROR;
687
    memset(state, 0, sizeof(struct internal_state));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
688

689 690
    create_se_table(state->se_table);

691 692
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
693
    if (strm->bits_per_sample > 16) {
694
        state->id_len = 5;
695

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
696 697
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
698
            if (strm->flags & AEC_DATA_MSB)
699
                state->flush_output = flush_msb_24;
700
            else
701
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
702
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
703
            state->bytes_per_sample = 4;
704
            if (strm->flags & AEC_DATA_MSB)
705
                state->flush_output = flush_msb_32;
706
            else
707
                state->flush_output = flush_lsb_32;
708
        }
709
        state->out_blklen = strm->block_size * state->bytes_per_sample;
710
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
711 712
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
713
        state->id_len = 4;
714
        state->out_blklen = strm->block_size * 2;
715
        if (strm->flags & AEC_DATA_MSB)
716
            state->flush_output = flush_msb_16;
717
        else
718
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
719
    } else {
720
        if (strm->flags & AEC_RESTRICTED) {
721 722 723 724 725 726 727 728 729 730 731 732
            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
733
        state->bytes_per_sample = 1;
734
        state->out_blklen = strm->block_size;
735
        state->flush_output = flush_8;
736 737
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
738
    if (strm->flags & AEC_DATA_SIGNED) {
739
        state->xmax = (INT64_C(1) << (strm->bits_per_sample - 1)) - 1;
740
        state->xmin = ~state->xmax;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
741
    } else {
742
        state->xmin = 0;
743
        state->xmax = (UINT64_C(1) << strm->bits_per_sample) - 1;
744
    }
745

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
746
    state->in_blklen = (strm->block_size * strm->bits_per_sample
747
                        + state->id_len) / 8 + 16;
748

749
    modi = 1UL << state->id_len;
750
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
751
    if (state->id_table == NULL)
752
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
753

754
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
755
    for (int i = 1; i < modi - 1; i++) {
756
        state->id_table[i] = m_split;
757
    }
758
    state->id_table[modi - 1] = m_uncomp;
759

760 761 762
    state->rsi_size = strm->rsi * strm->block_size;
    state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
    if (state->rsi_buffer == NULL)
763 764
        return AEC_MEM_ERROR;

765
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
766 767 768 769 770 771 772
    if (state->pp) {
        state->ref = 1;
        state->encoded_block_size = strm->block_size - 1;
    } else {
        state->ref = 0;
        state->encoded_block_size = strm->block_size;
    }
773 774 775
    strm->total_in = 0;
    strm->total_out = 0;

776 777
    state->rsip = state->rsi_buffer;
    state->flush_start = state->rsi_buffer;
778
    state->bitp = 0;
779
    state->fs = 0;
780
    state->mode = m_id;
781
    return AEC_OK;
782 783
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
784
int aec_decode(struct aec_stream *strm, int flush)
785
{
786 787 788 789 790 791 792 793 794
    /**
       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.
    */

795 796 797
    struct internal_state *state = strm->state;
    int status;

798 799 800
    strm->total_in += strm->avail_in;
    strm->total_out += strm->avail_out;

801 802 803 804 805 806 807
    do {
        status = state->mode(strm);
    } while (status == M_CONTINUE);

    if (status == M_ERROR)
        return AEC_DATA_ERROR;

808 809 810 811
    if (status == M_EXIT && strm->avail_out > 0 &&
        strm->avail_out < state->bytes_per_sample)
        return AEC_MEM_ERROR;

812
    state->flush_output(strm);
813 814 815 816

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

817 818
    return AEC_OK;
}
819

820 821
int aec_decode_end(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
822
    struct internal_state *state = strm->state;
823

824
    free(state->id_table);
825
    free(state->rsi_buffer);
826
    free(state);
827
    return AEC_OK;
828
}
829

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
830
int aec_buffer_decode(struct aec_stream *strm)
831
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
832
    int status = aec_decode_init(strm);
833 834 835 836 837 838 839
    if (status != AEC_OK)
        return status;

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