decode.c 23 KB
Newer Older
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
1 2
/**
 * @file decode.c
3
 *
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
4
 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
5 6 7 8 9
 * @author Moritz Hanke, Deutsches Klimarechenzentrum
 * @author Joerg Behrens, Deutsches Klimarechenzentrum
 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
 *
 * @section LICENSE
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
10
 * Copyright 2012 - 2014
11 12 13 14 15 16 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 44 45 46
 *
 * Mathis Rosenhauer,                 Luis Kornblueh
 * Moritz Hanke,
 * Joerg Behrens
 *
 * Deutsches Klimarechenzentrum GmbH  Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 45a                     Bundesstr. 53
 * 20146 Hamburg                      20146 Hamburg
 * Germany                            Germany
 *
 * 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
47 48 49
 * @section DESCRIPTION
 *
 * Adaptive Entropy Decoder
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50
 * Based on CCSDS documents 121.0-B-2 and 120.0-G-3
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
51 52
 *
 */
53 54 55 56 57

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58
#include "libaec.h"
59
#include "decode.h"
60

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
61 62 63 64
#if HAVE_BSR64
#  include <intrin.h>
#endif

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
135 136 137

static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
{
138 139 140 141
    *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
142 143 144 145
}

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

static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
{
153 154
    *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_lsb_32(struct aec_stream *strm, uint32_t data)
{
159 160 161 162
    *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
163 164 165 166
}

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

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
183 184 185 186 187 188 189
FLUSH(msb_32)
FLUSH(msb_24)
FLUSH(msb_16)
FLUSH(lsb_32)
FLUSH(lsb_24)
FLUSH(lsb_16)
FLUSH(8)
190

191
static inline void check_rsi_end(struct aec_stream *strm)
192
{
193
    /**
194
       Flush output if end of RSI reached
195
     */
196
    struct internal_state *state = strm->state;
197

198
    if (state->rsi_size == (size_t)(state->rsip - state->rsi_buffer)) {
199
        state->flush_output(strm);
200 201
        state->flush_start = state->rsi_buffer;
        state->rsip = state->rsi_buffer;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
202
    }
203 204
}

205
static inline void put_sample(struct aec_stream *strm, uint32_t s)
206 207 208
{
    struct internal_state *state = strm->state;

209
    *state->rsip++ = s;
210
    strm->avail_out -= state->bytes_per_sample;
211
    check_rsi_end(strm);
212 213
}

214 215 216 217 218 219
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
220 221 222 223 224 225 226 227 228

    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):
229
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
Moritz Hanke's avatar
Moritz Hanke committed
230 231 232 233 234 235 236 237
      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++;
    };

238 239
}

240
static inline uint32_t direct_get(struct aec_stream *strm, int n)
241 242
{
    /**
243
       Get n bit from input stream
244 245 246 247

       No checking whatsoever. Read bits are dumped.
     */

248
    struct internal_state *state = strm->state;
249

250 251
    if (state->bitp < n)
        fill_acc(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
252

253 254 255 256
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

257
static inline uint32_t direct_get_fs(struct aec_stream *strm)
258 259 260 261
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
262
       Essentially counts the number of 0 bits until a 1 is
263
       encountered.
264 265
     */

266
    uint32_t fs = 0;
267 268 269 270
#if HAVE_DECL___BUILTIN_CLZLL
    int lz;
#elif HAVE_BSR64
    unsigned long lz;
271
#endif
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
272
    struct internal_state *state = strm->state;
273

274 275
    state->acc &= ((1ULL << state->bitp) - 1);

276 277 278
    while (state->acc == 0) {
        fs += state->bitp;
        state->bitp = 0;
279
        fill_acc(strm);
280
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
281

Thomas Jahns's avatar
Thomas Jahns committed
282
#if HAVE_DECL___BUILTIN_CLZLL
283 284 285
    lz = __builtin_clzll(state->acc);
    fs += lz + state->bitp - 64;
    state->bitp = 63 - lz;
286 287 288 289
#elif HAVE_BSR64
    _BitScanReverse64(&lz, state->acc);
    fs += state->bitp - 1 - lz;
    state->bitp = lz;
290
#else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
291 292 293
    state->bitp--;
    while ((state->acc & (1ULL << state->bitp)) == 0) {
        state->bitp--;
294
        fs++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
295
    }
296
#endif
297 298 299
    return fs;
}

300
static inline uint32_t bits_ask(struct aec_stream *strm, int n)
301 302 303 304 305 306 307 308 309 310 311 312
{
    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;
}

313
static inline uint32_t bits_get(struct aec_stream *strm, int n)
314 315 316 317 318
{
    return (strm->state->acc >> (strm->state->bitp - n))
        & ((1ULL << n) - 1);
}

319
static inline void bits_drop(struct aec_stream *strm, int n)
320 321 322 323
{
    strm->state->bitp -= n;
}

324
static inline uint32_t fs_ask(struct aec_stream *strm)
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
{
    if (bits_ask(strm, 1) == 0)
        return 0;
    while ((strm->state->acc & (1ULL << (strm->state->bitp - 1))) == 0) {
        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;
}

343
static inline void fs_drop(struct aec_stream *strm)
344 345 346 347 348
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

349
static inline uint32_t copysample(struct aec_stream *strm)
350
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
351
    if (bits_ask(strm, strm->bits_per_sample) == 0
352 353
        || strm->avail_out == 0)
        return 0;
354

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
355 356
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
357 358
    return 1;
}
359 360 361 362 363

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

364 365
    if (state->rsip == state->rsi_buffer) {
        if(strm->flags & AEC_PAD_RSI)
366
            state->bitp -= state->bitp % 8;
367 368 369
        if (state->pp)
            state->ref = 1;
    } else {
370
        state->ref = 0;
371
    }
372 373 374 375
    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);
376 377 378 379 380 381 382 383 384 385 386
    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 {
387
        if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
388
            return M_EXIT;
389 390
        *state->rsip++ += bits_get(strm, k);
        strm->avail_out -= state->bytes_per_sample;
391
        bits_drop(strm, k);
392
    } while(++state->i < state->n);
393

394
    check_rsi_end(strm);
395 396 397 398 399 400 401
    state->mode = m_id;
    return M_CONTINUE;
}

static int m_split_fs(struct aec_stream *strm)
{
    struct internal_state *state = strm->state;
402
    int k = state->id - 1;
403 404

    do {
405 406
        if (fs_ask(strm) == 0)
            return M_EXIT;
407
        state->rsip[state->i] = state->fs << k;
408
        fs_drop(strm);
409
    } while(++state->i < state->n);
410

411
    state->i = 0;
412 413 414 415 416 417
    state->mode = m_split_output;
    return M_CONTINUE;
}

static int m_split(struct aec_stream *strm)
{
418
    uint32_t i, k;
419 420
    struct internal_state *state = strm->state;

421 422 423 424
    if (BUFFERSPACE(strm)) {
        k = state->id - 1;

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

427
        for (i = 0; i < strm->block_size - state->ref; i++)
428
            state->rsip[i] = direct_get_fs(strm) << k;
429

430
        for (i = state->ref; i < strm->block_size; i++)
431
            *state->rsip++ += direct_get(strm, k);
432

433
        strm->avail_out -= state->out_blklen;
434
        check_rsi_end(strm);
435

436 437 438 439 440
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
441 442
        if (copysample(strm) == 0)
            return M_EXIT;
443 444 445 446 447
        state->n = strm->block_size - 1;
    } else {
        state->n = strm->block_size;
    }

448
    state->i = 0;
449 450 451 452 453 454 455 456
    state->mode = m_split_fs;
    return M_CONTINUE;
}

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

457 458 459 460 461
    do {
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
462 463 464 465 466 467 468

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_zero_block(struct aec_stream *strm)
{
469
    uint32_t i, zero_blocks, b, zero_bytes;
470 471
    struct internal_state *state = strm->state;

472 473
    if (fs_ask(strm) == 0)
        return M_EXIT;
474
    zero_blocks = state->fs + 1;
475
    fs_drop(strm);
476 477

    if (zero_blocks == ROS) {
478
        b = (int)(state->rsip - state->rsi_buffer) / strm->block_size;
479 480 481 482 483 484
        zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

    if (state->ref)
485
        i = zero_blocks * strm->block_size - 1;
486
    else
487
        i = zero_blocks * strm->block_size;
488

489
    zero_bytes = i * state->bytes_per_sample;
490

491
    if (strm->avail_out >= zero_bytes) {
492 493 494
        if (state->rsi_size - (state->rsip - state->rsi_buffer) < i)
            return M_ERROR;

495 496
        memset(state->rsip, 0, i * sizeof(uint32_t));
        state->rsip += i;
497
        strm->avail_out -= zero_bytes;
498
        check_rsi_end(strm);
499

500 501 502 503
        state->mode = m_id;
        return M_CONTINUE;
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
504
    state->i = i;
505 506 507 508 509 510
    state->mode = m_zero_output;
    return M_CONTINUE;
}

static int m_se_decode(struct aec_stream *strm)
{
511
    int32_t m, d1;
512 513 514
    struct internal_state *state = strm->state;

    while(state->i < strm->block_size) {
515 516
        if (fs_ask(strm) == 0)
            return M_EXIT;
517
        m = state->fs;
518
        d1 = m - state->se_table[2 * m + 1];
519 520

        if ((state->i & 1) == 0) {
521 522 523
            if (strm->avail_out == 0)
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
524 525 526
            state->i++;
        }

527 528 529
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, d1);
530
        state->i++;
531
        fs_drop(strm);
532 533 534 535 536 537 538 539
    }

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_se(struct aec_stream *strm)
{
540
    uint32_t i;
541
    int32_t m, d1;
542 543
    struct internal_state *state = strm->state;

544 545 546 547 548 549 550 551 552 553 554 555 556 557
    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++;
        }
558 559 560 561 562 563 564 565 566 567 568 569 570
        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;

571 572
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
573 574 575 576 577 578 579 580 581 582 583 584 585 586

    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;

587 588 589 590
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
591 592 593 594 595 596 597 598
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

599 600 601 602
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
603 604 605 606 607 608 609

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_uncomp(struct aec_stream *strm)
{
610
    uint32_t i;
611 612
    struct internal_state *state = strm->state;

613
    if (BUFFERSPACE(strm)) {
614
        for (i = 0; i < strm->block_size; i++)
615
            *state->rsip++ = direct_get(strm, strm->bits_per_sample);
616
        strm->avail_out -= state->out_blklen;
617
        check_rsi_end(strm);
618

619 620 621 622 623 624 625 626 627
        state->mode = m_id;
        return M_CONTINUE;
    }

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

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
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
643
int aec_decode_init(struct aec_stream *strm)
644
{
645
    int i, modi;
646
    struct internal_state *state;
647

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
648
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
649
        return AEC_CONF_ERROR;
650

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
651
    state = malloc(sizeof(struct internal_state));
652
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
653
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
654

655 656
    create_se_table(state->se_table);

657 658
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
659
    if (strm->bits_per_sample > 16) {
660
        state->id_len = 5;
661

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
662 663
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
664
            if (strm->flags & AEC_DATA_MSB)
665
                state->flush_output = flush_msb_24;
666
            else
667
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
668
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
669
            state->bytes_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
670
            if (strm->flags & AEC_DATA_MSB)
671
                state->flush_output = flush_msb_32;
672
            else
673
                state->flush_output = flush_lsb_32;
674 675
        }
        state->out_blklen = strm->block_size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
676
            * state->bytes_per_sample;
677
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
678 679
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
680
        state->id_len = 4;
681
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
682
        if (strm->flags & AEC_DATA_MSB)
683
            state->flush_output = flush_msb_16;
684
        else
685
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
686
    } else {
687
        if (strm->flags & AEC_RESTRICTED) {
688 689 690 691 692 693 694 695 696 697 698 699
            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
700
        state->bytes_per_sample = 1;
701
        state->out_blklen = strm->block_size;
702
        state->flush_output = flush_8;
703 704
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
705
    if (strm->flags & AEC_DATA_SIGNED) {
706
        state->xmin = -(1LL << (strm->bits_per_sample - 1));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
707
        state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
708
    } else {
709
        state->xmin = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
710
        state->xmax = (1ULL << strm->bits_per_sample) - 1;
711
    }
712

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
713
    state->in_blklen = (strm->block_size * strm->bits_per_sample
714
                        + state->id_len) / 8 + 9;
715 716

    modi = 1UL << state->id_len;
717
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
718
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
719
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
720

721
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
722
    for (i = 1; i < modi - 1; i++) {
723
        state->id_table[i] = m_split;
724
    }
725
    state->id_table[modi - 1] = m_uncomp;
726

727 728 729
    state->rsi_size = strm->rsi * strm->block_size;
    state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
    if (state->rsi_buffer == NULL)
730 731
        return AEC_MEM_ERROR;

732 733 734
    strm->total_in = 0;
    strm->total_out = 0;

735 736
    state->rsip = state->rsi_buffer;
    state->flush_start = state->rsi_buffer;
737
    state->bitp = 0;
738
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
739
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
740
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
741
    return AEC_OK;
742 743
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
744
int aec_decode(struct aec_stream *strm, int flush)
745
{
746 747 748 749 750 751 752 753 754
    /**
       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
755
    struct internal_state *state = strm->state;
756
    int status;
757

758 759 760
    strm->total_in += strm->avail_in;
    strm->total_out += strm->avail_out;

761 762 763 764 765 766 767
    do {
        status = state->mode(strm);
    } while (status == M_CONTINUE);

    if (status == M_ERROR)
        return AEC_DATA_ERROR;

768
    state->flush_output(strm);
769 770 771 772

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

773 774
    return AEC_OK;
}
775

776 777
int aec_decode_end(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
778
    struct internal_state *state = strm->state;
779

780
    free(state->id_table);
781
    free(state->rsi_buffer);
782
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
783
    return AEC_OK;
784
}
785

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
786
int aec_buffer_decode(struct aec_stream *strm)
787 788 789 790 791 792 793 794 795 796 797
{
    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;
}