decode.c 15.5 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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
18
static void put_msb_32(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
19 20 21 22 23 24 25 26 27
{
    *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
28
static void put_msb_24(struct aec_stream *strm, int64_t data)
29 30 31 32 33 34 35 36
{
    *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
37
static void put_msb_16(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
38 39 40 41 42 43 44
{
    *strm->next_out++ = data >> 8;
    *strm->next_out++ = data;
    strm->avail_out -= 2;
    strm->total_out += 2;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
45
static void put_lsb_32(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
46 47 48 49 50 51 52 53 54
{
    *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
55
static void put_lsb_24(struct aec_stream *strm, int64_t data)
56 57 58 59 60 61 62 63
{
    *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
64
static void put_lsb_16(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
65 66 67 68 69 70
{
    *strm->next_out++ = data;
    *strm->next_out++ = data >> 8;
    strm->avail_out -= 2;
    strm->total_out += 2;
}
71

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
72
static void put_8(struct aec_stream *strm, int64_t data)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
73 74 75 76 77
{
    *strm->next_out++ = data;
    strm->avail_out--;
    strm->total_out++;
}
78

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
79
static inline void u_put(struct aec_stream *strm, int64_t sample)
80
{
81 82
    int64_t x, d, th, D, lower, m;
    struct internal_state *state = strm->state;
83

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
84
    if (state->pp && (state->samples_out % state->ref_int != 0)) {
85 86
        d = sample;
        x = state->last_out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
87 88
        lower = x - state->xmin;
        th = MIN(lower, state->xmax - x);
89

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
90
        if (d <= 2 * th) {
91 92 93 94 95
            if (d & 1)
                D = - (d + 1) / 2;
            else
                D = d / 2;
        } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
96
            if (th == lower)
97 98 99 100 101
                D = d - th;
            else
                D = th - d;
        }
        sample = x + D;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102 103
    } else {
        if (strm->flags & AEC_DATA_SIGNED) {
104
            m = 1ULL << (strm->bit_per_sample - 1);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
105
            /* Reference samples have to be sign extended */
106
            sample = (sample ^ m) - m;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107 108
        }
    }
109 110
    state->last_out = sample;
    state->put_sample(strm, sample);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
111
    state->samples_out++;
112 113
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
static inline int64_t u_get(struct aec_stream *strm, unsigned int n)
115 116 117 118 119 120 121
{
    /**
       Unsafe get n bit from input stream

       No checking whatsoever. Read bits are dumped.
     */

122
    struct internal_state *state;
123 124

    state = strm->state;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125
    while (state->bitp < n) {
126 127
        strm->avail_in--;
        strm->total_in++;
128
        state->acc = (state->acc << 8) | *strm->next_in++;
129 130 131 132 133 134
        state->bitp += 8;
    }
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
135
static inline int64_t u_get_fs(struct aec_stream *strm)
136 137 138 139 140 141 142 143
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

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

144
    int64_t fs = 0;
145 146 147 148 149 150 151

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

    return fs;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
152
static inline void fast_split(struct aec_stream *strm)
153
{
154
    int i, k;
155
    struct internal_state *state= strm->state;
156 157 158

    k = state->id - 1;

159
    if (state->ref)
160
        u_put(strm, u_get(strm, strm->bit_per_sample));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
161

162
    for (i = state->ref; i < strm->block_size; i++)
163 164
        state->block[i] = u_get_fs(strm) << k;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
165
    for (i = state->ref; i < strm->block_size; i++) {
166 167 168 169 170
        state->block[i] += u_get(strm, k);
        u_put(strm, state->block[i]);
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
171
static inline void fast_zero(struct aec_stream *strm)
172 173 174 175 176 177 178
{
    int i = strm->state->i;

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
179
static inline void fast_se(struct aec_stream *strm)
180 181
{
    int i;
182
    int64_t m, d1;
183
    struct internal_state *state= strm->state;
184

185
    i = state->ref;
186

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
187
    while (i < strm->block_size) {
188
        m = u_get_fs(strm);
189
        d1 = m - state->se_table[2 * m + 1];
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
190

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
        if ((i & 1) == 0) {
192
            u_put(strm, state->se_table[2 * m] - d1);
193 194
            i++;
        }
195
        u_put(strm, d1);
196 197 198 199
        i++;
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
200
static inline void fast_uncomp(struct aec_stream *strm)
201 202 203 204 205
{
    int i;

    for (i = 0; i < strm->block_size; i++)
        u_put(strm, u_get(strm, strm->bit_per_sample));
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
#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);
389
        d1 = m - state->se_table[2 * m + 1];
390 391

        if ((state->i & 1) == 0) {
392
            PUT(strm, state->se_table[2 * m] - d1);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 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
            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;
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
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
489
int aec_decode_init(struct aec_stream *strm)
490
{
491
    int i, modi;
492
    struct internal_state *state;
493 494

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

497
    state = (struct internal_state *) malloc(sizeof(struct internal_state));
498
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
499
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
500

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
501
    state->se_table = (int *) malloc(182 * sizeof(int));
502 503 504 505 506
    if (state->se_table == NULL)
        return AEC_MEM_ERROR;

    create_se_table(state->se_table);

507 508
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
509
    if (strm->bit_per_sample > 16) {
510
        state->id_len = 5;
511

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
512
        if (strm->bit_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
513
            state->byte_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
514
            if (strm->flags & AEC_DATA_MSB)
515 516 517
                state->put_sample = put_msb_24;
            else
                state->put_sample = put_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
518
        } else {
519
            state->byte_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
520
            if (strm->flags & AEC_DATA_MSB)
521 522 523 524 525 526
                state->put_sample = put_msb_32;
            else
                state->put_sample = put_lsb_32;
        }
        state->out_blklen = strm->block_size
            * state->byte_per_sample;
527
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
528
    else if (strm->bit_per_sample > 8) {
529
        state->byte_per_sample = 2;
530
        state->id_len = 4;
531
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
532
        if (strm->flags & AEC_DATA_MSB)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
533
            state->put_sample = put_msb_16;
534
        else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
535
            state->put_sample = put_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
536
    } else {
537
        state->byte_per_sample = 1;
538
        state->id_len = 3;
539
        state->out_blklen = strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
540
        state->put_sample = put_8;
541 542
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
543
    if (strm->flags & AEC_DATA_SIGNED) {
544 545
        state->xmin = -(1ULL << (strm->bit_per_sample - 1));
        state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
546
    } else {
547 548 549
        state->xmin = 0;
        state->xmax = (1ULL << strm->bit_per_sample) - 1;
    }
550

551
    state->ref_int = strm->block_size * strm->rsi;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
552
    state->in_blklen = (strm->block_size * strm->bit_per_sample
553 554 555
                        + state->id_len) / 8 + 1;

    modi = 1UL << state->id_len;
556
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
557
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
558
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
559

560
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
561
    for (i = 1; i < modi - 1; i++) {
562
        state->id_table[i] = m_split;
563
    }
564
    state->id_table[modi - 1] = m_uncomp;
565

566
    state->block = (int64_t *)malloc(strm->block_size * sizeof(int64_t));
567
    if (state->block == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
568
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
569

570 571 572
    strm->total_in = 0;
    strm->total_out = 0;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
573
    state->samples_out = 0;
574
    state->bitp = 0;
575
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
576
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
577
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
578
    return AEC_OK;
579 580
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
581
int aec_decode(struct aec_stream *strm, int flush)
582
{
583 584 585 586 587 588 589 590 591
    /**
       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.
    */

592 593 594
    while (strm->state->mode(strm) == M_CONTINUE);
    return AEC_OK;
}
595

596 597
int aec_decode_end(struct aec_stream *strm)
{
598
    struct internal_state *state= strm->state;
599

600 601 602
    free(state->block);
    free(state->id_table);
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
603
    return AEC_OK;
604
}
605

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
606
int aec_buffer_decode(struct aec_stream *strm)
607 608 609 610 611 612 613 614 615 616 617
{
    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;
}