decode.c 20.2 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 10 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
 * @author Moritz Hanke, Deutsches Klimarechenzentrum
 * @author Joerg Behrens, Deutsches Klimarechenzentrum
 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
 *
 * @section LICENSE
 * Copyright 2012
 *
 * 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 50 51 52
 * @section DESCRIPTION
 *
 * Adaptive Entropy Decoder
 * Based on CCSDS documents 121.0-B-2 and 120.0-G-2
 *
 */
53

54 55 56 57 58 59
#include <config.h>

#if HAVE_STDINT_H
# include <stdint.h>
#endif

60 61 62 63 64
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
65
#include "libaec.h"
66
#include "decode.h"
67

68 69 70 71 72
#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
73
#define FLUSH(KIND)                                                     \
74 75 76
    static void flush_##KIND(struct aec_stream *strm)                   \
    {                                                                   \
        int i;                                                          \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
77
        int64_t x, d, th, D, lower, m;                                  \
78 79 80 81
        int64_t data;                                                   \
        struct internal_state *state = strm->state;                     \
                                                                        \
        if (state->pp) {                                                \
82 83
            if (state->flush_start == 0 && state->buf_i > 0) {          \
                state->last_out = state->buf[0];                        \
84 85
                                                                        \
                if (strm->flags & AEC_DATA_SIGNED) {                    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
86
                    m = 1ULL << (strm->bits_per_sample - 1);            \
87
                    /* Reference samples have to be sign extended */    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88
                    state->last_out = (state->last_out ^ m) - m;        \
89
                }                                                       \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
90
                put_##KIND(strm, state->last_out);                      \
91 92 93
                state->flush_start = 1;                                 \
            }                                                           \
                                                                        \
94 95
            for (i = state->flush_start; i < state->buf_i; i++) {       \
                d = state->buf[i];                                      \
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                x = state->last_out;                                    \
                lower = x - state->xmin;                                \
                th = MIN(lower, state->xmax - x);                       \
                                                                        \
                if (d <= 2 * th) {                                      \
                    if (d & 1)                                          \
                        D = - (d + 1) / 2;                              \
                    else                                                \
                        D = d / 2;                                      \
                } else {                                                \
                    if (th == lower)                                    \
                        D = d - th;                                     \
                    else                                                \
                        D = th - d;                                     \
                }                                                       \
                data = x + D;                                           \
                state->last_out = data;                                 \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
113
                put_##KIND(strm, data);                                 \
114 115
            }                                                           \
        } else {                                                        \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
116 117
            for (i = state->flush_start; i < state->buf_i; i++)         \
                put_##KIND(strm, state->buf[i]);                        \
118
        }                                                               \
119
        if (state->buf_i == state->buf_size) {                          \
120 121
            state->flush_start = 0;                                     \
        } else {                                                        \
122
            state->flush_start = state->buf_i;                          \
123 124
        }                                                               \
    }
125

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data >> 24;
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
}

static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
}

static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
}

static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 24;
}

static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data >> 16;
}

static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
}

static inline void put_8(struct aec_stream *strm, uint32_t data)
{
    *strm->next_out++ = data;
}

FLUSH(msb_32);
FLUSH(msb_24);
FLUSH(msb_16);
FLUSH(lsb_32);
FLUSH(lsb_24);
FLUSH(lsb_16);
FLUSH(8);
181

182
static inline void put_sample(struct aec_stream *strm, uint32_t s)
183
{
184
    struct internal_state *state = strm->state;
185

186
    state->buf[state->buf_i++] = s;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
187 188
    strm->avail_out -= state->bytes_per_sample;
    strm->total_out += state->bytes_per_sample;
189

190
    if (state->buf_i == state->buf_size) {
191
        state->flush_output(strm);
192
        state->buf_i = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
193
    }
194 195
}

196 197 198 199 200 201 202 203 204 205 206 207
static inline void fill_acc(struct aec_stream *strm)
{
    int b = (63 - strm->state->bitp) >> 3;

    strm->avail_in -= b;
    strm->total_in += b;
    strm->state->bitp += b << 3;
    while (b--)
        strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
}

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

       No checking whatsoever. Read bits are dumped.
     */

215
    struct internal_state *state = strm->state;
216

217 218
    if (state->bitp < n)
        fill_acc(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
219

220 221 222 223
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

224
static inline uint32_t direct_get_fs(struct aec_stream *strm)
225 226 227 228
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
229 230 231 232
       Essentially counts the number of 0 bits until a 1 is
       encountered. The longest FS we can safely detect is 56 bits. If
       no FS is found in accumulator then we top it up to at least 56
       bits.
233 234
     */

235
    uint32_t fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
236
    struct internal_state *state = strm->state;
237

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
238
    if ((state->acc & ((1ULL << state->bitp) - 1)) == 0)
239
        fill_acc(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
240 241 242 243

    state->bitp--;
    while ((state->acc & (1ULL << state->bitp)) == 0) {
        state->bitp--;
244
        fs++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
245
    }
246 247 248 249

    return fs;
}

250
static inline uint32_t bits_ask(struct aec_stream *strm, int n)
251 252 253 254 255 256 257 258 259 260 261 262 263
{
    while (strm->state->bitp < n) {
        if (strm->avail_in == 0)
            return 0;
        strm->avail_in--;
        strm->total_in++;
        strm->state->acc <<= 8;
        strm->state->acc |= *strm->next_in++;
        strm->state->bitp += 8;
    }
    return 1;
}

264
static inline uint32_t bits_get(struct aec_stream *strm, int n)
265 266 267 268 269
{
    return (strm->state->acc >> (strm->state->bitp - n))
        & ((1ULL << n) - 1);
}

270
static inline void bits_drop(struct aec_stream *strm, int n)
271 272 273 274
{
    strm->state->bitp -= n;
}

275
static inline uint32_t fs_ask(struct aec_stream *strm)
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
    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->total_in++;
            strm->state->acc <<= 8;
            strm->state->acc |= *strm->next_in++;
            strm->state->bitp += 8;
        }
        strm->state->fs++;
        strm->state->bitp--;
    }
    return 1;
}

295
static inline uint32_t fs_get(struct aec_stream *strm)
296 297 298 299
{
    return strm->state->fs;
}

300
static inline void fs_drop(struct aec_stream *strm)
301 302 303 304 305
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

306
static inline uint32_t copysample(struct aec_stream *strm)
307
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
308
    if (bits_ask(strm, strm->bits_per_sample) == 0
309 310
        || strm->avail_out == 0)
        return 0;
311

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
312 313
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
314 315
    return 1;
}
316 317 318 319 320

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

321
    if (state->pp && state->buf_i == 0)
322 323 324 325
        state->ref = 1;
    else
        state->ref = 0;

326 327 328 329
    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);
330 331 332 333 334 335 336 337 338 339 340
    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 {
341
        if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
342 343 344
            return M_EXIT;
        put_sample(strm, (state->block[state->i] << k) + bits_get(strm, k));
        bits_drop(strm, k);
345 346 347 348 349 350 351 352 353 354 355
    } while(state->i--);

    state->mode = m_id;
    return M_CONTINUE;
}

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

    do {
356 357 358 359
        if (fs_ask(strm) == 0)
            return M_EXIT;
        state->block[state->i] = fs_get(strm);
        fs_drop(strm);
360 361 362 363 364 365 366 367 368
    } while(state->i--);

    state->i = state->n - 1;
    state->mode = m_split_output;
    return M_CONTINUE;
}

static int m_split(struct aec_stream *strm)
{
369 370
    int i, k;
    uint32_t sample;
371 372
    struct internal_state *state = strm->state;

373 374 375 376 377 378 379 380 381 382 383 384 385 386
    if (BUFFERSPACE(strm)) {
        k = state->id - 1;

        if (state->ref)
            put_sample(strm, direct_get(strm, strm->bits_per_sample));

        for (i = state->ref; i < strm->block_size; i++)
            state->block[i] = direct_get_fs(strm) << k;

        for (i = state->ref; i < strm->block_size; i++) {
            sample = state->block[i] + direct_get(strm, k);
            put_sample(strm, sample);
        }

387 388 389 390 391
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
392 393
        if (copysample(strm) == 0)
            return M_EXIT;
394 395 396 397 398 399 400 401 402 403 404 405 406 407
        state->n = strm->block_size - 1;
    } else {
        state->n = strm->block_size;
    }

    state->i = state->n - 1;
    state->mode = m_split_fs;
    return M_CONTINUE;
}

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

408 409 410 411 412
    do {
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
413 414 415 416 417 418 419

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_zero_block(struct aec_stream *strm)
{
420
    int i, zero_blocks, b;
421 422
    struct internal_state *state = strm->state;

423 424 425 426
    if (fs_ask(strm) == 0)
        return M_EXIT;
    zero_blocks = fs_get(strm) + 1;
    fs_drop(strm);
427 428

    if (zero_blocks == ROS) {
429
        b = state->buf_i / strm->block_size;
430 431 432 433 434 435
        zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

    if (state->ref)
436
        i = zero_blocks * strm->block_size - 1;
437
    else
438
        i = zero_blocks * strm->block_size;
439

440 441 442
    if (strm->avail_out >= i * state->bytes_per_sample) {
        while (i--)
            put_sample(strm, 0);
443 444 445 446
        state->mode = m_id;
        return M_CONTINUE;
    }

447
    strm->state->i = i;
448 449 450 451 452 453
    state->mode = m_zero_output;
    return M_CONTINUE;
}

static int m_se_decode(struct aec_stream *strm)
{
454
    int32_t m, d1;
455 456 457
    struct internal_state *state = strm->state;

    while(state->i < strm->block_size) {
458 459 460
        if (fs_ask(strm) == 0)
            return M_EXIT;
        m = fs_get(strm);
461
        d1 = m - state->se_table[2 * m + 1];
462 463

        if ((state->i & 1) == 0) {
464 465 466
            if (strm->avail_out == 0)
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
467 468 469
            state->i++;
        }

470 471 472
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, d1);
473
        state->i++;
474
        fs_drop(strm);
475 476 477 478 479 480 481 482
    }

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_se(struct aec_stream *strm)
{
483 484
    int i;
    int32_t m, d1;
485 486
    struct internal_state *state = strm->state;

487 488 489 490 491 492 493 494 495 496 497 498 499 500
    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++;
        }
501 502 503 504 505 506 507 508 509 510 511 512 513
        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;

514 515
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
516 517 518 519 520 521 522 523 524 525 526 527 528 529

    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;

530 531 532 533
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
534 535 536 537 538 539 540 541
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

542 543 544 545
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
546 547 548 549 550 551 552

    state->mode = m_id;
    return M_CONTINUE;
}

static int m_uncomp(struct aec_stream *strm)
{
553 554
    int i;
    uint32_t sample;
555 556
    struct internal_state *state = strm->state;

557 558 559 560 561
    if (BUFFERSPACE(strm)) {
        for (i = 0; i < strm->block_size; i++) {
            sample = direct_get(strm, strm->bits_per_sample);
            put_sample(strm, sample);
        }
562 563 564 565 566 567 568 569 570
        state->mode = m_id;
        return M_CONTINUE;
    }

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

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
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
586
int aec_decode_init(struct aec_stream *strm)
587
{
588
    int i, modi;
589
    struct internal_state *state;
590

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
591
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
592
        return AEC_CONF_ERROR;
593

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
594
    state = malloc(sizeof(struct internal_state));
595
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
596
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
597

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
598
    state->se_table = malloc(182 * sizeof(int));
599 600 601 602 603
    if (state->se_table == NULL)
        return AEC_MEM_ERROR;

    create_se_table(state->se_table);

604 605
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
606
    if (strm->bits_per_sample > 16) {
607
        state->id_len = 5;
608

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
609 610
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
611
            if (strm->flags & AEC_DATA_MSB)
612
                state->flush_output = flush_msb_24;
613
            else
614
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
615
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
616
            state->bytes_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
617
            if (strm->flags & AEC_DATA_MSB)
618
                state->flush_output = flush_msb_32;
619
            else
620
                state->flush_output = flush_lsb_32;
621 622
        }
        state->out_blklen = strm->block_size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
623
            * state->bytes_per_sample;
624
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
625 626
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
627
        state->id_len = 4;
628
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
629
        if (strm->flags & AEC_DATA_MSB)
630
            state->flush_output = flush_msb_16;
631
        else
632
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
633
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
634
        state->bytes_per_sample = 1;
635
        state->id_len = 3;
636
        state->out_blklen = strm->block_size;
637
        state->flush_output = flush_8;
638 639
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
640
    if (strm->flags & AEC_DATA_SIGNED) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
641 642
        state->xmin = -(1ULL << (strm->bits_per_sample - 1));
        state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
643
    } else {
644
        state->xmin = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
645
        state->xmax = (1ULL << strm->bits_per_sample) - 1;
646
    }
647

648
    state->ref_int = strm->block_size * strm->rsi;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
649
    state->in_blklen = (strm->block_size * strm->bits_per_sample
650
                        + state->id_len) / 8 + 9;
651 652

    modi = 1UL << state->id_len;
653
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
654
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
655
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
656

657
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
658
    for (i = 1; i < modi - 1; i++) {
659
        state->id_table[i] = m_split;
660
    }
661
    state->id_table[modi - 1] = m_uncomp;
662

663
    state->block = malloc(strm->block_size * sizeof(uint32_t));
664
    if (state->block == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
665
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
666

667
    state->buf_size = strm->rsi * strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
668
    state->buf = malloc(state->buf_size * sizeof(uint32_t));
669
    if (state->buf == NULL)
670 671
        return AEC_MEM_ERROR;

672 673 674
    strm->total_in = 0;
    strm->total_out = 0;

675
    state->buf_i = 0;
676
    state->flush_start = 0;
677
    state->bitp = 0;
678
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
679
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
680
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
681
    return AEC_OK;
682 683
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
684
int aec_decode(struct aec_stream *strm, int flush)
685
{
686 687 688 689 690 691 692 693 694
    /**
       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.
    */

695 696 697 698
    struct internal_state *state= strm->state;

    while (state->mode(strm) == M_CONTINUE);
    state->flush_output(strm);
699 700
    return AEC_OK;
}
701

702 703
int aec_decode_end(struct aec_stream *strm)
{
704
    struct internal_state *state= strm->state;
705

706 707
    free(state->block);
    free(state->id_table);
708
    free(state->se_table);
709
    free(state->buf);
710
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
711
    return AEC_OK;
712
}
713

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
714
int aec_buffer_decode(struct aec_stream *strm)
715 716 717 718 719 720 721 722 723 724 725
{
    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;
}