decode.c 20.3 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 73 74 75 76
#define FLUSH(KIND, PUTBLOCK)                                           \
    static void flush_##KIND(struct aec_stream *strm)                   \
    {                                                                   \
        int i;                                                          \
        int64_t x, d, th, D, lower, m, sample;                          \
        int64_t data;                                                   \
        struct internal_state *state = strm->state;                     \
                                                                        \
        if (state->pp) {                                                \
77 78
            if (state->flush_start == 0 && state->buf_i > 0) {          \
                state->last_out = state->buf[0];                        \
79 80
                                                                        \
                if (strm->flags & AEC_DATA_SIGNED) {                    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
81
                    m = 1ULL << (strm->bits_per_sample - 1);            \
82
                    /* Reference samples have to be sign extended */    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
83
                    state->last_out = (state->last_out ^ m) - m;        \
84 85 86 87 88 89 90
                }                                                       \
                                                                        \
                data = state->last_out;                                 \
                PUTBLOCK;                                               \
                state->flush_start = 1;                                 \
            }                                                           \
                                                                        \
91 92
            for (i = state->flush_start; i < state->buf_i; i++) {       \
                d = state->buf[i];                                      \
93 94 95 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;                                 \
                PUTBLOCK;                                               \
            }                                                           \
        } else {                                                        \
113 114
            for (i = state->flush_start; i < state->buf_i; i++) {       \
                data = state->buf[i];                                   \
115 116 117
                PUTBLOCK;                                               \
            }                                                           \
        }                                                               \
118
        if (state->buf_i == state->buf_size) {                          \
119 120
            state->flush_start = 0;                                     \
        } else {                                                        \
121
            state->flush_start = state->buf_i;                          \
122 123
        }                                                               \
    }
124

125 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
FLUSH(msb_32,
      do {
          *strm->next_out++ = data >> 24;
          *strm->next_out++ = data >> 16;
          *strm->next_out++ = data >> 8;
          *strm->next_out++ = data;
      } while(0)
    )

FLUSH(msb_24,
      do {
          *strm->next_out++ = data >> 16;
          *strm->next_out++ = data >> 8;
          *strm->next_out++ = data;
      } while(0)
    )

FLUSH(msb_16,
      do {
          *strm->next_out++ = data >> 8;
          *strm->next_out++ = data;
      } while(0)
    )

FLUSH(lsb_32,
      do {
          *strm->next_out++ = data;
          *strm->next_out++ = data >> 8;
          *strm->next_out++ = data >> 16;
          *strm->next_out++ = data >> 24;
      } while(0)
    )

FLUSH(lsb_24,
      do {
          *strm->next_out++ = data;
          *strm->next_out++ = data >> 8;
          *strm->next_out++ = data >> 16;
      } while(0)
    )

FLUSH(lsb_16,
      do {
          *strm->next_out++ = data;
          *strm->next_out++ = data >> 8;
      } while(0)
    )

FLUSH(8,
      *strm->next_out++ = data;
    )

static void put_sample(struct aec_stream *strm, uint32_t s)
178
{
179
    struct internal_state *state = strm->state;
180

181
    state->buf[state->buf_i++] = s;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
182 183
    strm->avail_out -= state->bytes_per_sample;
    strm->total_out += state->bytes_per_sample;
184

185
    if (state->buf_i == state->buf_size) {
186
        state->flush_output(strm);
187
        state->buf_i = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
188
    }
189 190
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
static int64_t direct_get(struct aec_stream *strm, unsigned int n)
192 193
{
    /**
194
       Get n bit from input stream
195 196 197 198

       No checking whatsoever. Read bits are dumped.
     */

199
    struct internal_state *state = strm->state;
200

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
201
    while (state->bitp < n) {
202 203
        strm->avail_in--;
        strm->total_in++;
204
        state->acc = (state->acc << 8) | *strm->next_in++;
205 206
        state->bitp += 8;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
207

208 209 210 211
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
212
static int64_t direct_get_fs(struct aec_stream *strm)
213 214 215 216
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
217 218 219 220
       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.
221 222
     */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
223
    struct internal_state *state = strm->state;
224
    int64_t fs = 0;
225

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
226 227 228 229 230 231 232 233 234 235 236 237
    if ((state->acc & ((1ULL << state->bitp) - 1)) == 0)
        while (state->bitp < 56) {
            strm->avail_in--;
            strm->total_in++;
            state->acc = (state->acc << 8) | *strm->next_in++;
            state->bitp += 8;
        }

    state->bitp--;

    while ((state->acc & (1ULL << state->bitp)) == 0) {
        state->bitp--;
238
        fs++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
239
    }
240 241 242 243

    return fs;
}

244
static void fast_split(struct aec_stream *strm)
245
{
246
    int i, k;
247
    struct internal_state *state= strm->state;
248 249 250

    k = state->id - 1;

251
    if (state->ref)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
252
        put_sample(strm, direct_get(strm, strm->bits_per_sample));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
253

254
    for (i = state->ref; i < strm->block_size; i++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
255
        state->block[i] = direct_get_fs(strm) << k;
256

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
257 258
    for (i = state->ref; i < strm->block_size; i++)
        put_sample(strm, state->block[i] + direct_get(strm, k));
259 260
}

261
static void fast_zero(struct aec_stream *strm)
262 263 264 265
{
    int i = strm->state->i;

    while (i--)
266
        put_sample(strm, 0);
267 268
}

269
static void fast_se(struct aec_stream *strm)
270 271
{
    int i;
272
    int64_t m, d1;
273
    struct internal_state *state= strm->state;
274

275
    i = state->ref;
276

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
277
    while (i < strm->block_size) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
278
        m = direct_get_fs(strm);
279
        d1 = m - state->se_table[2 * m + 1];
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
280

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
281
        if ((i & 1) == 0) {
282
            put_sample(strm, state->se_table[2 * m] - d1);
283 284
            i++;
        }
285
        put_sample(strm, d1);
286 287 288 289
        i++;
    }
}

290
static void fast_uncomp(struct aec_stream *strm)
291 292 293 294
{
    int i;

    for (i = 0; i < strm->block_size; i++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
295
        put_sample(strm, direct_get(strm, strm->bits_per_sample));
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
static uint32_t bits_ask(struct aec_stream *strm, int n)
{
    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;
}

static uint32_t bits_get(struct aec_stream *strm, int n)
{
    return (strm->state->acc >> (strm->state->bitp - n))
        & ((1ULL << n) - 1);
}

static void bits_drop(struct aec_stream *strm, int n)
{
    strm->state->bitp -= n;
}

static uint32_t fs_ask(struct aec_stream *strm)
{
    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;
}

static uint32_t fs_get(struct aec_stream *strm)
{
    return strm->state->fs;
}

static void fs_drop(struct aec_stream *strm)
{
    strm->state->fs = 0;
    strm->state->bitp--;
}

static uint32_t copysample(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
356
    if (bits_ask(strm, strm->bits_per_sample) == 0
357 358
        || strm->avail_out == 0)
        return 0;
359

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
360 361
    put_sample(strm, bits_get(strm, strm->bits_per_sample));
    bits_drop(strm, strm->bits_per_sample);
362 363
    return 1;
}
364 365 366 367 368

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

369
    if (state->pp && state->buf_i == 0)
370 371 372 373
        state->ref = 1;
    else
        state->ref = 0;

374 375 376 377
    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);
378 379 380 381 382 383 384 385 386 387 388
    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 {
389 390 391 392 393
        if (bits_ask(strm, k) == 0
            || strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, (state->block[state->i] << k) + bits_get(strm, k));
        bits_drop(strm, k);
394 395 396 397 398 399 400 401 402 403 404
    } 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 {
405 406 407 408
        if (fs_ask(strm) == 0)
            return M_EXIT;
        state->block[state->i] = fs_get(strm);
        fs_drop(strm);
409 410 411 412 413 414 415 416 417 418 419
    } while(state->i--);

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

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

420
    if (SAFE(strm)) {
421 422 423 424 425 426
        fast_split(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
427 428
        if (copysample(strm) == 0)
            return M_EXIT;
429 430 431 432 433 434 435 436 437 438 439 440 441 442
        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;

443 444 445 446 447
    do {
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
448 449 450 451 452 453 454 455 456 457

    state->mode = m_id;
    return M_CONTINUE;
}

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

458 459 460 461
    if (fs_ask(strm) == 0)
        return M_EXIT;
    zero_blocks = fs_get(strm) + 1;
    fs_drop(strm);
462 463

    if (zero_blocks == ROS) {
464
        b = state->buf_i / strm->block_size;
465 466 467 468 469 470 471 472 473 474
        zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
    } else if (zero_blocks > ROS) {
        zero_blocks--;
    }

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
475
    if (strm->avail_out >= state->i * state->bytes_per_sample) {
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
        fast_zero(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

    state->mode = m_zero_output;
    return M_CONTINUE;
}

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

    while(state->i < strm->block_size) {
491 492 493
        if (fs_ask(strm) == 0)
            return M_EXIT;
        m = fs_get(strm);
494
        d1 = m - state->se_table[2 * m + 1];
495 496

        if ((state->i & 1) == 0) {
497 498 499
            if (strm->avail_out == 0)
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
500 501 502
            state->i++;
        }

503 504 505
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, d1);
506
        state->i++;
507
        fs_drop(strm);
508 509 510 511 512 513 514 515 516 517
    }

    state->mode = m_id;
    return M_CONTINUE;
}

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

518
    if (SAFE(strm)) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532
        fast_se(strm);
        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;

533 534
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
535 536 537 538 539 540 541 542 543 544 545 546 547 548

    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;

549 550 551 552
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
553 554 555 556 557 558 559 560
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

561 562 563 564
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
565 566 567 568 569 570 571 572 573

    state->mode = m_id;
    return M_CONTINUE;
}

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

574
    if (SAFE(strm)) {
575 576 577 578 579 580 581 582 583 584
        fast_uncomp(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

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

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
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
600
int aec_decode_init(struct aec_stream *strm)
601
{
602
    int i, modi;
603
    struct internal_state *state;
604

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
605
    if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
606
        return AEC_CONF_ERROR;
607

608
    state = (struct internal_state *) malloc(sizeof(struct internal_state));
609
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
610
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
611

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
612
    state->se_table = (int *) malloc(182 * sizeof(int));
613 614 615 616 617
    if (state->se_table == NULL)
        return AEC_MEM_ERROR;

    create_se_table(state->se_table);

618 619
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
620
    if (strm->bits_per_sample > 16) {
621
        state->id_len = 5;
622

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
623 624
        if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
            state->bytes_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
625
            if (strm->flags & AEC_DATA_MSB)
626
                state->flush_output = flush_msb_24;
627
            else
628
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
629
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
630
            state->bytes_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
631
            if (strm->flags & AEC_DATA_MSB)
632
                state->flush_output = flush_msb_32;
633
            else
634
                state->flush_output = flush_lsb_32;
635 636
        }
        state->out_blklen = strm->block_size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
637
            * state->bytes_per_sample;
638
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
639 640
    else if (strm->bits_per_sample > 8) {
        state->bytes_per_sample = 2;
641
        state->id_len = 4;
642
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
643
        if (strm->flags & AEC_DATA_MSB)
644
            state->flush_output = flush_msb_16;
645
        else
646
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
647
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
648
        state->bytes_per_sample = 1;
649
        state->id_len = 3;
650
        state->out_blklen = strm->block_size;
651
        state->flush_output = flush_8;
652 653
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
654
    if (strm->flags & AEC_DATA_SIGNED) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
655 656
        state->xmin = -(1ULL << (strm->bits_per_sample - 1));
        state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
657
    } else {
658
        state->xmin = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
659
        state->xmax = (1ULL << strm->bits_per_sample) - 1;
660
    }
661

662
    state->ref_int = strm->block_size * strm->rsi;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
663
    state->in_blklen = (strm->block_size * strm->bits_per_sample
664 665 666
                        + state->id_len) / 8 + 1;

    modi = 1UL << state->id_len;
667
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
668
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
669
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
670

671
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
672
    for (i = 1; i < modi - 1; i++) {
673
        state->id_table[i] = m_split;
674
    }
675
    state->id_table[modi - 1] = m_uncomp;
676

677
    state->block = (int64_t *)malloc(strm->block_size * sizeof(int64_t));
678
    if (state->block == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
679
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
680

681
    state->buf_size = strm->rsi * strm->block_size;
682 683
    state->buf = (uint32_t *)malloc(state->buf_size * sizeof(uint32_t));
    if (state->buf == NULL)
684 685
        return AEC_MEM_ERROR;

686 687 688
    strm->total_in = 0;
    strm->total_out = 0;

689
    state->buf_i = 0;
690
    state->flush_start = 0;
691
    state->bitp = 0;
692
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
693
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
694
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
695
    return AEC_OK;
696 697
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
698
int aec_decode(struct aec_stream *strm, int flush)
699
{
700 701 702 703 704 705 706 707 708
    /**
       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.
    */

709 710 711 712
    struct internal_state *state= strm->state;

    while (state->mode(strm) == M_CONTINUE);
    state->flush_output(strm);
713 714
    return AEC_OK;
}
715

716 717
int aec_decode_end(struct aec_stream *strm)
{
718
    struct internal_state *state= strm->state;
719

720 721
    free(state->block);
    free(state->id_table);
722
    free(state->se_table);
723
    free(state->buf);
724
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
725
    return AEC_OK;
726
}
727

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
728
int aec_buffer_decode(struct aec_stream *strm)
729 730 731 732 733 734 735 736 737 738 739
{
    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;
}