decode.c 17.9 KB
Newer Older
1 2 3
/* Adaptive Entropy Decoder            */
/* CCSDS 121.0-B-1 and CCSDS 120.0-G-2 */

4 5 6 7 8 9
#include <config.h>

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

10 11 12 13 14
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
15
#include "libaec.h"
16
#include "decode.h"
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static int se_beta(int m)
{
    switch(m) {
    case 0:
        return 0;
    case 1:
    case 2:
        return 1;
    case 3:
    case 4:
    case 5:
        return 2;
    case 6:
    case 7:
    case 8:
    case 9:
        return 3;
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
        return 4;
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
        return 5;
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
        return 6;
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 33:
    case 34:
    case 35:
        return 7;
    case 36:
    case 37:
    case 38:
    case 39:
    case 40:
    case 41:
    case 42:
    case 43:
    case 44:
        return 8;
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 55:
        return 9;
    case 56:
    case 57:
    case 58:
    case 59:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:
    case 66:
        return 10;
    case 67:
    case 68:
    case 69:
    case 70:
    case 71:
    case 72:
    case 73:
    case 74:
    case 75:
    case 76:
    case 77:
    case 78:
        return 11;
    case 79:
    case 80:
    case 81:
    case 82:
    case 83:
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 89:
    case 90:
    case 91:
        return 12;
    }
}

static int se_ms(int m)
{
    switch(m) {
    case 0:
        return 0;
    case 1:
    case 2:
        return 1;
    case 3:
    case 4:
    case 5:
        return 3;
    case 6:
    case 7:
    case 8:
    case 9:
        return 6;
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
        return 10;
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
        return 15;
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
        return 21;
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 33:
    case 34:
    case 35:
        return 28;
    case 36:
    case 37:
    case 38:
    case 39:
    case 40:
    case 41:
    case 42:
    case 43:
    case 44:
        return 36;
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 55:
        return 45;
    case 56:
    case 57:
    case 58:
    case 59:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:
    case 66:
        return 56;
    case 67:
    case 68:
    case 69:
    case 70:
    case 71:
    case 72:
    case 73:
    case 74:
    case 75:
    case 76:
    case 77:
    case 78:
        return 67;
    case 79:
    case 80:
    case 81:
    case 82:
    case 83:
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 89:
    case 90:
    case 91:
        return 79;
    }
}
237

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
238
static void put_msb_32(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
239 240 241 242 243 244 245 246 247
{
    *strm->next_out++ = data >> 24;
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
    strm->avail_out -= 4;
    strm->total_out += 4;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
248
static void put_msb_24(struct aec_stream *strm, int64_t data)
249 250 251 252 253 254 255 256
{
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
    strm->avail_out -= 3;
    strm->total_out += 3;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
257
static void put_msb_16(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
258 259 260 261 262 263 264
{
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
    strm->avail_out -= 2;
    strm->total_out += 2;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
265
static void put_lsb_32(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
266 267 268 269 270 271 272 273 274
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data >> 16;
    *strm->next_out++ = data >> 24;
    strm->avail_out -= 4;
    strm->total_out += 4;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
275
static void put_lsb_24(struct aec_stream *strm, int64_t data)
276 277 278 279 280 281 282 283
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data >> 16;
    strm->avail_out -= 3;
    strm->total_out += 3;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
284
static void put_lsb_16(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
285 286 287 288 289 290
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    strm->avail_out -= 2;
    strm->total_out += 2;
}
291

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
292
static void put_8(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
293 294 295 296 297
{
    *strm->next_out++ = data;
    strm->avail_out--;
    strm->total_out++;
}
298

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
299
static inline void u_put(struct aec_stream *strm, int64_t sample)
300
{
301 302
    int64_t x, d, th, D, lower, m;
    struct internal_state *state = strm->state;
303

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
304
    if (state->pp && (state->samples_out % state->ref_int != 0)) {
305 306
        d = sample;
        x = state->last_out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
307 308
        lower = x - state->xmin;
        th = MIN(lower, state->xmax - x);
309

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
310
        if (d <= 2 * th) {
311 312 313 314 315
            if (d & 1)
                D = - (d + 1) / 2;
            else
                D = d / 2;
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
316
            if (th == lower)
317 318 319 320 321
                D = d - th;
            else
                D = th - d;
        }
        sample = x + D;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
322 323
    } else {
        if (strm->flags & AEC_DATA_SIGNED) {
324
            m = 1ULL << (strm->bit_per_sample - 1);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
325
            /* Reference samples have to be sign extended */
326
            sample = (sample ^ m) - m;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
327 328
        }
    }
329 330
    state->last_out = sample;
    state->put_sample(strm, sample);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
331
    state->samples_out++;
332 333
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
334
static inline int64_t u_get(struct aec_stream *strm, unsigned int n)
335 336 337 338 339 340 341
{
    /**
       Unsafe get n bit from input stream

       No checking whatsoever. Read bits are dumped.
     */

342
    struct internal_state *state;
343 344

    state = strm->state;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
345
    while (state->bitp < n) {
346 347
        strm->avail_in--;
        strm->total_in++;
348
        state->acc = (state->acc << 8) | *strm->next_in++;
349 350 351 352 353 354
        state->bitp += 8;
    }
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
355
static inline int64_t u_get_fs(struct aec_stream *strm)
356 357 358 359 360 361 362 363
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

       Essentially counts the number of 0 bits until a
       1 is encountered. TODO: faster version.
     */

364
    int64_t fs = 0;
365 366 367 368 369 370 371

    while(u_get(strm, 1) == 0)
        fs++;

    return fs;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
372
static inline void fast_split(struct aec_stream *strm)
373
{
374
    int i, k;
375
    struct internal_state *state;
376 377 378 379

    state = strm->state;
    k = state->id - 1;

380
    if (state->ref)
381
        u_put(strm, u_get(strm, strm->bit_per_sample));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
382

383
    for (i = state->ref; i < strm->block_size; i++)
384 385
        state->block[i] = u_get_fs(strm) << k;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
386
    for (i = state->ref; i < strm->block_size; i++) {
387 388 389 390 391
        state->block[i] += u_get(strm, k);
        u_put(strm, state->block[i]);
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
392
static inline void fast_zero(struct aec_stream *strm)
393 394 395 396 397 398 399
{
    int i = strm->state->i;

    while (i--)
        u_put(strm, 0);
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
400
static inline void fast_se(struct aec_stream *strm)
401 402
{
    int i;
403
    int64_t m, d1;
404

405
    i = strm->state->ref;
406

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
407
    while (i < strm->block_size) {
408 409
        m = u_get_fs(strm);
        d1 = m - se_ms(m);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
410

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
411
        if ((i & 1) == 0) {
412
            u_put(strm, se_beta(m) - d1);
413 414
            i++;
        }
415
        u_put(strm, d1);
416 417 418 419
        i++;
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
420
static inline void fast_uncomp(struct aec_stream *strm)
421 422 423 424 425
{
    int i;

    for (i = 0; i < strm->block_size; i++)
        u_put(strm, u_get(strm, strm->bit_per_sample));
426 427
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
#define ASK(strm, n)                                     \
    do {                                                 \
        while (strm->state->bitp < (unsigned)(n)) {      \
            if (strm->avail_in == 0)                     \
                return M_EXIT;                           \
            strm->avail_in--;                            \
            strm->total_in++;                            \
            strm->state->acc <<= 8;                      \
            strm->state->acc |= *strm->next_in++;        \
            strm->state->bitp += 8;                      \
        }                                                \
    } while (0)

#define GET(strm, n)                                                    \
    ((strm->state->acc >> (strm->state->bitp - (n))) & ((1ULL << (n)) - 1))

#define DROP(strm, n) strm->state->bitp -= (unsigned)(n)

#define ASKFS(strm)                                                           \
    do {                                                                      \
        ASK(strm, 1);                                                         \
        while ((strm->state->acc & (1ULL << (strm->state->bitp - 1))) == 0) { \
            if (strm->state->bitp == 1) {                                     \
                if (strm->avail_in == 0)                                      \
                    return M_EXIT;                                            \
                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--;                                              \
        }                                                                     \
    } while (0)

#define GETFS(strm) state->fs

#define DROPFS(strm)                            \
    do {                                        \
        strm->state->fs = 0;                    \
        /* Needs to be here for                 \
           ASK/GET/PUT/DROP interleaving. */    \
        strm->state->bitp--;                    \
    } while (0)

#define PUT(strm, sample)                          \
    do {                                           \
        if (strm->avail_out == 0)                  \
            return M_EXIT;                         \
        u_put(strm, (sample));                     \
    } while (0)

#define COPYSAMPLE(strm)                            \
    do {                                            \
        ASK(strm, strm->bit_per_sample);            \
        PUT(strm, GET(strm, strm->bit_per_sample)); \
        DROP(strm, strm->bit_per_sample);           \
    } while (0)


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

    if (state->pp && (state->samples_out / strm->block_size) % strm->rsi == 0)
        state->ref = 1;
    else
        state->ref = 0;

    ASK(strm, state->id_len);
    state->id = GET(strm, state->id_len);
    DROP(strm, state->id_len);
    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 {
        ASK(strm, k);
        PUT(strm, (state->block[state->i] << k) + GET(strm, k));
        DROP(strm, k);
    } 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 {
        ASKFS(strm);
        state->block[state->i] = GETFS(strm);
        DROPFS(strm);
    } 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;

    if (SAFE) {
        fast_split(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
        COPYSAMPLE(strm);
        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;

    do
        PUT(strm, 0);
    while(--state->i);

    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;

    ASKFS(strm);
    zero_blocks = GETFS(strm) + 1;
    DROPFS(strm);

    if (zero_blocks == ROS) {
        b = (state->samples_out / strm->block_size) % strm->rsi;
        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;

    if (strm->avail_out >= state->i * state->byte_per_sample) {
        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) {
        ASKFS(strm);
        m = GETFS(strm);
        d1 = m - se_ms(m);

        if ((state->i & 1) == 0) {
            PUT(strm, se_beta(m) - d1);
            state->i++;
        }

        PUT(strm, d1);
        state->i++;
        DROPFS(strm);
    }

    state->mode = m_id;
    return M_CONTINUE;
}

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

    if (SAFE) {
        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;

    if (state->ref)
        COPYSAMPLE(strm);

    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;

    ASK(strm, 1);
    state->id = GET(strm, 1);
    DROP(strm, 1);
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

    do
        COPYSAMPLE(strm);
    while(--state->i);

    state->mode = m_id;
    return M_CONTINUE;
}

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

    if (SAFE) {
        fast_uncomp(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
694
int aec_decode_init(struct aec_stream *strm)
695
{
696
    int i, modi;
697
    struct internal_state *state;
698 699

    if (strm->bit_per_sample > 32 || strm->bit_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
700
        return AEC_CONF_ERROR;
701

702
    state = (struct internal_state *) malloc(sizeof(struct internal_state));
703
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
704
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
705

706 707
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
708
    if (strm->bit_per_sample > 16) {
709
        state->id_len = 5;
710

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
711
        if (strm->bit_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
712
            state->byte_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
713
            if (strm->flags & AEC_DATA_MSB)
714 715 716
                state->put_sample = put_msb_24;
            else
                state->put_sample = put_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
717
        } else {
718
            state->byte_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
719
            if (strm->flags & AEC_DATA_MSB)
720 721 722 723 724 725
                state->put_sample = put_msb_32;
            else
                state->put_sample = put_lsb_32;
        }
        state->out_blklen = strm->block_size
            * state->byte_per_sample;
726
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
727
    else if (strm->bit_per_sample > 8) {
728
        state->byte_per_sample = 2;
729
        state->id_len = 4;
730
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
731
        if (strm->flags & AEC_DATA_MSB)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
732
            state->put_sample = put_msb_16;
733
        else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
734
            state->put_sample = put_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
735
    } else {
736
        state->byte_per_sample = 1;
737
        state->id_len = 3;
738
        state->out_blklen = strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
739
        state->put_sample = put_8;
740 741
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
742
    if (strm->flags & AEC_DATA_SIGNED) {
743 744
        state->xmin = -(1ULL << (strm->bit_per_sample - 1));
        state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
745
    } else {
746 747 748
        state->xmin = 0;
        state->xmax = (1ULL << strm->bit_per_sample) - 1;
    }
749

750
    state->ref_int = strm->block_size * strm->rsi;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
751
    state->in_blklen = (strm->block_size * strm->bit_per_sample
752 753 754
                        + state->id_len) / 8 + 1;

    modi = 1UL << state->id_len;
755
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
756
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
757
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
758

759
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
760
    for (i = 1; i < modi - 1; i++) {
761
        state->id_table[i] = m_split;
762
    }
763
    state->id_table[modi - 1] = m_uncomp;
764

765
    state->block = (int64_t *)malloc(strm->block_size * sizeof(int64_t));
766
    if (state->block == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
767
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
768

769 770 771
    strm->total_in = 0;
    strm->total_out = 0;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
772
    state->samples_out = 0;
773
    state->bitp = 0;
774
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
775
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
776
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
777
    return AEC_OK;
778 779
}

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

791 792 793
    while (strm->state->mode(strm) == M_CONTINUE);
    return AEC_OK;
}
794

795 796 797
int aec_decode_end(struct aec_stream *strm)
{
    struct internal_state *state;
798

799 800 801 802
    state = strm->state;
    free(state->block);
    free(state->id_table);
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
803
    return AEC_OK;
804
}
805

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
806
int aec_buffer_decode(struct aec_stream *strm)
807 808 809 810 811 812 813 814 815 816 817
{
    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;
}