decode.c 18.5 KB
Newer Older
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
1 2 3 4 5 6 7 8 9
/**
 * @file decode.c
 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
 * @section DESCRIPTION
 *
 * Adaptive Entropy Decoder
 * Based on CCSDS documents 121.0-B-2 and 120.0-G-2
 *
 */
10

11 12 13 14 15 16
#include <config.h>

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

17 18 19 20 21
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
22
#include "libaec.h"
23
#include "decode.h"
24

25 26 27 28 29 30 31 32 33
#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) {                                                \
34 35
            if (state->flush_start == 0 && state->buf_i > 0) {          \
                state->last_out = state->buf[0];                        \
36 37 38 39
                                                                        \
                if (strm->flags & AEC_DATA_SIGNED) {                    \
                    m = 1ULL << (strm->bit_per_sample - 1);             \
                    /* Reference samples have to be sign extended */    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
40
                    state->last_out = (state->last_out ^ m) - m;        \
41 42 43 44 45 46 47
                }                                                       \
                                                                        \
                data = state->last_out;                                 \
                PUTBLOCK;                                               \
                state->flush_start = 1;                                 \
            }                                                           \
                                                                        \
48 49
            for (i = state->flush_start; i < state->buf_i; i++) {       \
                d = state->buf[i];                                      \
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                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 {                                                        \
70 71
            for (i = state->flush_start; i < state->buf_i; i++) {       \
                data = state->buf[i];                                   \
72 73 74
                PUTBLOCK;                                               \
            }                                                           \
        }                                                               \
75
        if (state->buf_i == state->buf_size) {                          \
76 77
            state->flush_start = 0;                                     \
        } else {                                                        \
78
            state->flush_start = state->buf_i;                          \
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
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)
135
{
136
    struct internal_state *state = strm->state;
137

138
    state->buf[state->buf_i++] = s;
139 140
    strm->avail_out -= state->byte_per_sample;
    strm->total_out += state->byte_per_sample;
141

142
    if (state->buf_i == state->buf_size) {
143
        state->flush_output(strm);
144
        state->buf_i = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
145
    }
146 147
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
148
static int64_t direct_get(struct aec_stream *strm, unsigned int n)
149 150
{
    /**
151
       Get n bit from input stream
152 153 154 155

       No checking whatsoever. Read bits are dumped.
     */

156
    struct internal_state *state = strm->state;
157

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
158
    while (state->bitp < n) {
159 160
        strm->avail_in--;
        strm->total_in++;
161
        state->acc = (state->acc << 8) | *strm->next_in++;
162 163
        state->bitp += 8;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
164

165 166 167 168
    state->bitp -= n;
    return (state->acc >> state->bitp) & ((1ULL << n) - 1);
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
169
static int64_t direct_get_fs(struct aec_stream *strm)
170 171 172 173
{
    /**
       Interpret a Fundamental Sequence from the input buffer.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
174 175 176 177
       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.
178 179
     */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
180
    struct internal_state *state = strm->state;
181
    int64_t fs = 0;
182

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
183 184 185 186 187 188 189 190 191 192 193 194
    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--;
195
        fs++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
196
    }
197 198 199 200

    return fs;
}

201
static void fast_split(struct aec_stream *strm)
202
{
203
    int i, k;
204
    struct internal_state *state= strm->state;
205 206 207

    k = state->id - 1;

208
    if (state->ref)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
209
        put_sample(strm, direct_get(strm, strm->bit_per_sample));
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
210

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
214 215
    for (i = state->ref; i < strm->block_size; i++)
        put_sample(strm, state->block[i] + direct_get(strm, k));
216 217
}

218
static void fast_zero(struct aec_stream *strm)
219 220 221 222
{
    int i = strm->state->i;

    while (i--)
223
        put_sample(strm, 0);
224 225
}

226
static void fast_se(struct aec_stream *strm)
227 228
{
    int i;
229
    int64_t m, d1;
230
    struct internal_state *state= strm->state;
231

232
    i = state->ref;
233

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
234
    while (i < strm->block_size) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
235
        m = direct_get_fs(strm);
236
        d1 = m - state->se_table[2 * m + 1];
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
237

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
238
        if ((i & 1) == 0) {
239
            put_sample(strm, state->se_table[2 * m] - d1);
240 241
            i++;
        }
242
        put_sample(strm, d1);
243 244 245 246
        i++;
    }
}

247
static void fast_uncomp(struct aec_stream *strm)
248 249 250 251
{
    int i;

    for (i = 0; i < strm->block_size; i++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
252
        put_sample(strm, direct_get(strm, strm->bit_per_sample));
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
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)
{
    if (bits_ask(strm, strm->bit_per_sample) == 0
        || strm->avail_out == 0)
        return 0;
316

317 318 319 320
    put_sample(strm, bits_get(strm, strm->bit_per_sample));
    bits_drop(strm, strm->bit_per_sample);
    return 1;
}
321 322 323 324 325

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

326
    if (state->pp && state->buf_i == 0)
327 328 329 330
        state->ref = 1;
    else
        state->ref = 0;

331 332 333 334
    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);
335 336 337 338 339 340 341 342 343 344 345
    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 {
346 347 348 349 350
        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);
351 352 353 354 355 356 357 358 359 360 361
    } 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 {
362 363 364 365
        if (fs_ask(strm) == 0)
            return M_EXIT;
        state->block[state->i] = fs_get(strm);
        fs_drop(strm);
366 367 368 369 370 371 372 373 374 375 376
    } 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;

377
    if (SAFE(strm)) {
378 379 380 381 382 383
        fast_split(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

    if (state->ref) {
384 385
        if (copysample(strm) == 0)
            return M_EXIT;
386 387 388 389 390 391 392 393 394 395 396 397 398 399
        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;

400 401 402 403 404
    do {
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, 0);
    } while(--state->i);
405 406 407 408 409 410 411 412 413 414

    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;

415 416 417 418
    if (fs_ask(strm) == 0)
        return M_EXIT;
    zero_blocks = fs_get(strm) + 1;
    fs_drop(strm);
419 420

    if (zero_blocks == ROS) {
421
        b = state->buf_i / strm->block_size;
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
        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) {
448 449 450
        if (fs_ask(strm) == 0)
            return M_EXIT;
        m = fs_get(strm);
451
        d1 = m - state->se_table[2 * m + 1];
452 453

        if ((state->i & 1) == 0) {
454 455 456
            if (strm->avail_out == 0)
                return M_EXIT;
            put_sample(strm, state->se_table[2 * m] - d1);
457 458 459
            state->i++;
        }

460 461 462
        if (strm->avail_out == 0)
            return M_EXIT;
        put_sample(strm, d1);
463
        state->i++;
464
        fs_drop(strm);
465 466 467 468 469 470 471 472 473 474
    }

    state->mode = m_id;
    return M_CONTINUE;
}

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

475
    if (SAFE(strm)) {
476 477 478 479 480 481 482 483 484 485 486 487 488 489
        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;

490 491
    if (state->ref && copysample(strm) == 0)
        return M_EXIT;
492 493 494 495 496 497 498 499 500 501 502 503 504 505

    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;

506 507 508 509
    if (bits_ask(strm, 1) == 0)
        return M_EXIT;
    state->id = bits_get(strm, 1);
    bits_drop(strm, 1);
510 511 512 513 514 515 516 517
    state->mode = m_low_entropy_ref;
    return M_CONTINUE;
}

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

518 519 520 521
    do {
        if (copysample(strm) == 0)
            return M_EXIT;
    } while(--state->i);
522 523 524 525 526 527 528 529 530

    state->mode = m_id;
    return M_CONTINUE;
}

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

531
    if (SAFE(strm)) {
532 533 534 535 536 537 538 539 540 541
        fast_uncomp(strm);
        state->mode = m_id;
        return M_CONTINUE;
    }

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

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
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
557
int aec_decode_init(struct aec_stream *strm)
558
{
559
    int i, modi;
560
    struct internal_state *state;
561 562

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

565
    state = (struct internal_state *) malloc(sizeof(struct internal_state));
566
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
567
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
568

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
569
    state->se_table = (int *) malloc(182 * sizeof(int));
570 571 572 573 574
    if (state->se_table == NULL)
        return AEC_MEM_ERROR;

    create_se_table(state->se_table);

575 576
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
577
    if (strm->bit_per_sample > 16) {
578
        state->id_len = 5;
579

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
580
        if (strm->bit_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
581
            state->byte_per_sample = 3;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
582
            if (strm->flags & AEC_DATA_MSB)
583
                state->flush_output = flush_msb_24;
584
            else
585
                state->flush_output = flush_lsb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
586
        } else {
587
            state->byte_per_sample = 4;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
588
            if (strm->flags & AEC_DATA_MSB)
589
                state->flush_output = flush_msb_32;
590
            else
591
                state->flush_output = flush_lsb_32;
592 593 594
        }
        state->out_blklen = strm->block_size
            * state->byte_per_sample;
595
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
596
    else if (strm->bit_per_sample > 8) {
597
        state->byte_per_sample = 2;
598
        state->id_len = 4;
599
        state->out_blklen = strm->block_size * 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
600
        if (strm->flags & AEC_DATA_MSB)
601
            state->flush_output = flush_msb_16;
602
        else
603
            state->flush_output = flush_lsb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
604
    } else {
605
        state->byte_per_sample = 1;
606
        state->id_len = 3;
607
        state->out_blklen = strm->block_size;
608
        state->flush_output = flush_8;
609 610
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
611
    if (strm->flags & AEC_DATA_SIGNED) {
612 613
        state->xmin = -(1ULL << (strm->bit_per_sample - 1));
        state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
614
    } else {
615 616 617
        state->xmin = 0;
        state->xmax = (1ULL << strm->bit_per_sample) - 1;
    }
618

619
    state->ref_int = strm->block_size * strm->rsi;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
620
    state->in_blklen = (strm->block_size * strm->bit_per_sample
621 622 623
                        + state->id_len) / 8 + 1;

    modi = 1UL << state->id_len;
624
    state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
625
    if (state->id_table == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
626
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
627

628
    state->id_table[0] = m_low_entropy;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
629
    for (i = 1; i < modi - 1; i++) {
630
        state->id_table[i] = m_split;
631
    }
632
    state->id_table[modi - 1] = m_uncomp;
633

634
    state->block = (int64_t *)malloc(strm->block_size * sizeof(int64_t));
635
    if (state->block == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
636
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
637

638
    state->buf_size = strm->rsi * strm->block_size;
639 640
    state->buf = (uint32_t *)malloc(state->buf_size * sizeof(uint32_t));
    if (state->buf == NULL)
641 642
        return AEC_MEM_ERROR;

643 644 645
    strm->total_in = 0;
    strm->total_out = 0;

646
    state->buf_i = 0;
647
    state->flush_start = 0;
648
    state->bitp = 0;
649
    state->fs = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
650
    state->pp = strm->flags & AEC_DATA_PREPROCESS;
651
    state->mode = m_id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
652
    return AEC_OK;
653 654
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
655
int aec_decode(struct aec_stream *strm, int flush)
656
{
657 658 659 660 661 662 663 664 665
    /**
       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.
    */

666 667 668 669
    struct internal_state *state= strm->state;

    while (state->mode(strm) == M_CONTINUE);
    state->flush_output(strm);
670 671
    return AEC_OK;
}
672

673 674
int aec_decode_end(struct aec_stream *strm)
{
675
    struct internal_state *state= strm->state;
676

677 678
    free(state->block);
    free(state->id_table);
679
    free(state->se_table);
680
    free(state->buf);
681
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
682
    return AEC_OK;
683
}
684

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
685
int aec_buffer_decode(struct aec_stream *strm)
686 687 688 689 690 691 692 693 694 695 696
{
    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;
}